Autodoc corrections, additions.
[cake.git] / rom / graphics / textfit.c
blobb54de63628f6388061078401bf33ca9fe20e341f
1 /*
2 Copyright © 1995-2007, 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
74 struct TextFont *tf = rp->Font;
75 ULONG retval = 0;
77 if (strLen && (constrainingBitHeight >= tf->tf_YSize))
79 BOOL ok = TRUE;
81 textExtent->te_Extent.MinX = 0;
82 textExtent->te_Extent.MinY = -tf->tf_Baseline;
83 textExtent->te_Extent.MaxX = 0;
84 textExtent->te_Extent.MaxY = tf->tf_YSize - tf->tf_Baseline - 1;
85 textExtent->te_Width = 0;
86 textExtent->te_Height = tf->tf_YSize;
88 if (constrainingExtent)
90 if ((constrainingExtent->te_Extent.MinY > textExtent->te_Extent.MinY) ||
91 (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) ? minx : textExtent->te_Extent.MinX;
113 newmaxx = (maxx > textExtent->te_Extent.MaxX) ? maxx : textExtent->te_Extent.MaxX;
115 if ((ULONG)(newmaxx - newminx + 1) > constrainingBitWidth) break;
117 if (constrainingExtent)
119 if (constrainingExtent->te_Extent.MinX > newminx) break;
120 if (constrainingExtent->te_Extent.MaxX < newmaxx) break;
121 if (constrainingExtent->te_Width < newwidth) break;
124 textExtent->te_Width = newwidth;
125 textExtent->te_Extent.MinX = newminx;
126 textExtent->te_Extent.MaxX = newmaxx;
128 retval++;
130 } /* while(strLen--) */
132 } /* if (ok) */
134 } /* if (strLen && (constrainingBitHeight >= tf->tf_YSize)) */
136 if (retval == 0)
138 textExtent->te_Width = 0;
139 textExtent->te_Height = 0;
140 textExtent->te_Extent.MinX = 0;
141 textExtent->te_Extent.MinY = 0;
142 textExtent->te_Extent.MaxX = 0;
143 textExtent->te_Extent.MaxY = 0;
146 return retval;
148 AROS_LIBFUNC_EXIT
150 } /* TextFit */