r4493@vps: verhaegs | 2007-04-19 14:44:00 -0400
[AROS.git] / rom / graphics / textextent.c
blob4220bb7187fd0ca08eeac32eacadb60b6c2d25e6
1 /*
2 Copyright © 1995-2001, The AROS Development Team. All rights reserved.
3 $Id$
5 Desc: Calculate the size a text needs in a specific rastport.
6 Lang: english
7 */
8 #include "graphics_intern.h"
9 #include <graphics/rastport.h>
11 /*****************************************************************************
13 NAME */
14 #include <graphics/rastport.h>
15 #include <graphics/text.h>
16 #include <proto/graphics.h>
18 AROS_LH4(void, TextExtent,
20 /* SYNOPSIS */
21 AROS_LHA(struct RastPort *, rp, A1),
22 AROS_LHA(CONST_STRPTR , string, A0),
23 AROS_LHA(ULONG , count, D0),
24 AROS_LHA(struct TextExtent *, textExtent, A2),
26 /* LOCATION */
27 struct GfxBase *, GfxBase, 115, Graphics)
29 /* FUNCTION
30 This function determines the metric of the space
31 that a text string would render into.
33 INPUTS
34 rp - RastPort
35 string - address of string
36 count - number of characters
37 textExtent - storing place for the result
38 te_Width - same as TextLength() result: the rp_cp_x
39 advance that rendering this text would cause.
40 te_Height - same as tf_YSize. The height of the
41 font.
42 te_Extent.MinX - the offset to the left side of the
43 rectangle this would render into. Often zero.
44 te_Extent.MinY - same as -tf_Baseline. The offset
45 from the baseline to the top of the rectangle
46 this would render into.
47 te_Extent.MaxX - the offset of the left side of the
48 rectangle this would render into. Often the
49 same as te_Width-1.
50 te_Extent.MaxY - same as tf_YSize-tf_Baseline-1.
51 The offset from the baseline to the bottom of
52 the rectangle this would render into.
54 RESULT
56 NOTES
58 EXAMPLE
60 BUGS
62 SEE ALSO
64 INTERNALS
66 HISTORY
67 29-10-95 digulla automatically created from
68 graphics_lib.fd and clib/graphics_protos.h
70 *****************************************************************************/
72 AROS_LIBFUNC_INIT
73 AROS_LIBBASE_EXT_DECL(struct GfxBase *,GfxBase)
75 struct TextFont *tf = rp->Font;
77 textExtent->te_Width = TextLength(rp, string, count);
78 textExtent->te_Height = tf->tf_YSize;
79 textExtent->te_Extent.MinY = -tf->tf_Baseline;
80 textExtent->te_Extent.MaxY = textExtent->te_Height - 1 - tf->tf_Baseline;
82 /* MinX/MaxX can be a bit more complicated if there are kerning/space tables */
84 if ((tf->tf_Flags & FPF_PROPORTIONAL) || tf->tf_CharKern || tf->tf_CharSpace)
86 WORD idx;
87 WORD defaultidx = NUMCHARS(tf) - 1; /* Last glyph is the default glyph */
88 WORD x, x2;
89 UBYTE c;
91 textExtent->te_Extent.MinX = 0;
92 textExtent->te_Extent.MaxX = 0;
93 x = 0;
95 if (count)
97 while(count--)
99 c = *string++;
101 if ( c < tf->tf_LoChar || c > tf->tf_HiChar)
103 idx = defaultidx;
105 else
107 idx = c - tf->tf_LoChar;
110 #define CHECK_MINMAX(x) \
111 if ((x) < textExtent->te_Extent.MinX) textExtent->te_Extent.MinX = (x); \
112 if ((x) > textExtent->te_Extent.MaxX) textExtent->te_Extent.MaxX = (x);
114 x += ((WORD *)tf->tf_CharKern)[idx];
115 CHECK_MINMAX(x);
117 x2 = x + ( ( ((ULONG *)tf->tf_CharLoc)[idx] ) & 0xFFFF);
118 CHECK_MINMAX(x2);
120 x += ((WORD *)tf->tf_CharSpace)[idx];
121 CHECK_MINMAX(x);
123 x += rp->TxSpacing;
124 CHECK_MINMAX(x);
126 } /* while(count--) */
128 textExtent->te_Extent.MaxX--;
130 } /* if (count) */
132 } /* if ((tf->tf_Flags & FPF_PROPORTIONAL) || tf->tf_CharKern || tf->tf_CharSpace) */
133 else
135 /* Normal non-proportional Font */
136 textExtent->te_Extent.MinX = 0;
137 textExtent->te_Extent.MaxX = textExtent->te_Width - 1;
140 if (rp->AlgoStyle & FSF_BOLD)
142 textExtent->te_Extent.MaxX += tf->tf_BoldSmear;
145 if (rp->AlgoStyle & FSF_ITALIC)
147 /* ###### ######
148 ** ## ## ## ##
149 ** ## ## ## ##
150 ** ## ## ===> ## ##
151 ** ## ## ## ##
152 **..##..##.. ..##..##..
153 ** ## ## ## ##
154 ** ###### ######
157 textExtent->te_Extent.MaxX += tf->tf_Baseline / 2;
158 textExtent->te_Extent.MinX -= (tf->tf_YSize - tf->tf_Baseline) / 2;
161 AROS_LIBFUNC_EXIT
163 } /* TextExtent */