adopt previous changes. (NicJA)
[AROS.git] / rom / graphics / textfit.c
blob56ed47dcc004b835f3599f78b28da03c76b5e76f
1 /*
2 Copyright © 1995-2012, The AROS Development Team. All rights reserved.
3 $Id$
5 Desc: Tell how many characters will fit into a box.
6 Lang: english
7 */
8 #include "graphics_intern.h"
10 /*****************************************************************************
12 NAME */
13 #include <graphics/rastport.h>
14 #include <graphics/text.h>
15 #include <proto/graphics.h>
17 AROS_LH8(ULONG, TextFit,
19 /* SYNOPSIS */
20 AROS_LHA(struct RastPort *, rp, A1),
21 AROS_LHA(CONST_STRPTR , string, A0),
22 AROS_LHA(ULONG , strLen, D0),
23 AROS_LHA(struct TextExtent *, textExtent, A2),
24 AROS_LHA(struct TextExtent *, constrainingExtent, A3),
25 AROS_LHA(LONG , strDirection, D1),
26 AROS_LHA(ULONG , constrainingBitWidth, D2),
27 AROS_LHA(ULONG , constrainingBitHeight, D3),
29 /* LOCATION */
30 struct GfxBase *, GfxBase, 116, Graphics)
32 /* FUNCTION
33 Tries to fill the given space with as many characters of the
34 font in rp as possible and returns that number.
36 INPUTS
37 rp - Use the settings in this RastPort (e.g. Font)
38 string - Use this string
39 strLen - The length of the string
40 textExtent - The size actually occupied will be returned here
41 constrainingExtent - If non-NULL, the routine will use the
42 dimensions of the box described here
43 strDirection - In which is the next character. Must be either 1
44 or -1. If it is -1, then string must point to the end (the
45 first character to check) of the text to fit (this is for
46 checking text which runs from right to left).
47 constrainingBitWidth - If constrainingExtent is NULL, then this
48 is the width of the bounding box.
49 constrainingBitHeight - If constrainingExtent is NULL, then this
50 is the height of the bounding box.
52 RESULT
53 The number of characters which fit in the bounding box.
54 If any characters fit in the bounding box, then textExtent will
55 tell how large the minimal bounding box for the string is.
57 NOTES
59 EXAMPLE
61 BUGS
63 SEE ALSO
64 TextLength()
66 INTERNALS
68 *****************************************************************************/
70 AROS_LIBFUNC_INIT
72 struct TextFont *tf = rp->Font;
73 ULONG retval = 0;
75 if (strLen && (constrainingBitHeight >= tf->tf_YSize))
77 BOOL ok = TRUE;
79 textExtent->te_Extent.MinX = 0;
80 textExtent->te_Extent.MinY = -tf->tf_Baseline;
81 textExtent->te_Extent.MaxX = 0;
82 textExtent->te_Extent.MaxY = tf->tf_YSize - tf->tf_Baseline - 1;
83 textExtent->te_Width = 0;
84 textExtent->te_Height = tf->tf_YSize;
86 if (constrainingExtent)
88 if (constrainingExtent->te_Extent.MinY > textExtent->te_Extent.MinY
89 ||
90 constrainingExtent->te_Extent.MaxY < textExtent->te_Extent.MaxY
92 constrainingExtent->te_Height < textExtent->te_Height)
94 ok = FALSE;
98 if (ok)
100 while (strLen--)
102 struct TextExtent char_extent;
103 WORD newwidth, newminx, newmaxx, minx, maxx;
105 TextExtent(rp, string, 1, &char_extent);
106 string += strDirection;
108 newwidth = textExtent->te_Width + char_extent.te_Width;
109 minx = textExtent->te_Width + char_extent.te_Extent.MinX;
110 maxx = textExtent->te_Width + char_extent.te_Extent.MaxX;
112 newminx = (minx < textExtent->te_Extent.MinX) ?
113 minx : textExtent->te_Extent.MinX;
114 newmaxx = (maxx > textExtent->te_Extent.MaxX) ?
115 maxx : textExtent->te_Extent.MaxX;
117 if ((ULONG)(newmaxx - newminx + 1) > constrainingBitWidth)
118 break;
120 if (constrainingExtent)
122 if (constrainingExtent->te_Extent.MinX > newminx) break;
123 if (constrainingExtent->te_Extent.MaxX < newmaxx) break;
124 if (constrainingExtent->te_Width < newwidth) break;
127 textExtent->te_Width = newwidth;
128 textExtent->te_Extent.MinX = newminx;
129 textExtent->te_Extent.MaxX = newmaxx;
131 retval++;
133 } /* while(strLen--) */
135 } /* if (ok) */
137 } /* if (strLen && (constrainingBitHeight >= tf->tf_YSize)) */
139 if (retval == 0)
141 textExtent->te_Width = 0;
142 textExtent->te_Height = 0;
143 textExtent->te_Extent.MinX = 0;
144 textExtent->te_Extent.MinY = 0;
145 textExtent->te_Extent.MaxX = 0;
146 textExtent->te_Extent.MaxY = 0;
149 return retval;
151 AROS_LIBFUNC_EXIT
153 } /* TextFit */