r4493@vps: verhaegs | 2007-04-19 14:44:00 -0400
[AROS.git] / rom / graphics / textfit.c
blobec8962670d9a42fa5a28e9cade585b942ea42ea9
1 /*
2 Copyright © 1995-2001, 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 HISTORY
70 *****************************************************************************/
72 AROS_LIBFUNC_INIT
73 AROS_LIBBASE_EXT_DECL(struct GfxBase *,GfxBase)
75 struct TextFont *tf = rp->Font;
76 ULONG retval = 0;
78 if (strLen && (constrainingBitHeight >= tf->tf_YSize))
80 BOOL ok = TRUE;
82 textExtent->te_Extent.MinX = 0;
83 textExtent->te_Extent.MinY = -tf->tf_Baseline;
84 textExtent->te_Extent.MaxX = 0;
85 textExtent->te_Extent.MaxY = tf->tf_YSize - tf->tf_Baseline - 1;
86 textExtent->te_Width = 0;
87 textExtent->te_Height = tf->tf_YSize;
89 if (constrainingExtent)
91 if ((constrainingExtent->te_Extent.MinY > textExtent->te_Extent.MinY) ||
92 (constrainingExtent->te_Extent.MaxY < textExtent->te_Extent.MaxY) ||
93 (constrainingExtent->te_Height < textExtent->te_Height))
95 ok = FALSE;
99 if (ok)
101 while(strLen--)
103 struct TextExtent char_extent;
104 WORD newwidth, newminx, newmaxx, minx, maxx;
106 TextExtent(rp, string, 1, &char_extent);
107 string += strDirection;
109 newwidth = textExtent->te_Width + char_extent.te_Width;
110 minx = textExtent->te_Width + char_extent.te_Extent.MinX;
111 maxx = textExtent->te_Width + char_extent.te_Extent.MaxX;
113 newminx = (minx < textExtent->te_Extent.MinX) ? minx : textExtent->te_Extent.MinX;
114 newmaxx = (maxx > textExtent->te_Extent.MaxX) ? maxx : textExtent->te_Extent.MaxX;
116 if ((ULONG)(newmaxx - newminx + 1) > constrainingBitWidth) break;
118 if (constrainingExtent)
120 if (constrainingExtent->te_Extent.MinX > newminx) break;
121 if (constrainingExtent->te_Extent.MaxX < newmaxx) break;
122 if (constrainingExtent->te_Width < newwidth) break;
125 textExtent->te_Width = newwidth;
126 textExtent->te_Extent.MinX = newminx;
127 textExtent->te_Extent.MaxX = newmaxx;
129 retval++;
131 } /* while(strLen--) */
133 } /* if (ok) */
135 } /* if (strLen && (constrainingBitHeight >= tf->tf_YSize)) */
137 if (retval == 0)
139 textExtent->te_Width = 0;
140 textExtent->te_Height = 0;
141 textExtent->te_Extent.MinX = 0;
142 textExtent->te_Extent.MinY = 0;
143 textExtent->te_Extent.MaxX = 0;
144 textExtent->te_Extent.MaxY = 0;
147 return retval;
149 AROS_LIBFUNC_EXIT
151 } /* TextFit */