1 /*****************************************************************************
2 * This file is part of gfxprim library. *
4 * Gfxprim is free software; you can redistribute it and/or *
5 * modify it under the terms of the GNU Lesser General Public *
6 * License as published by the Free Software Foundation; either *
7 * version 2.1 of the License, or (at your option) any later version. *
9 * Gfxprim is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
12 * Lesser General Public License for more details. *
14 * You should have received a copy of the GNU Lesser General Public *
15 * License along with gfxprim; if not, write to the Free Software *
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, *
17 * Boston, MA 02110-1301 USA *
19 * Copyright (C) 2009-2010 Jiri "BlueBear" Dluhos *
20 * <jiri.bluebear.dluhos@gmail.com> *
22 * Copyright (C) 2009-2012 Cyril Hrubis <metan@ucw.cz> *
24 *****************************************************************************/
26 #include "core/GP_Common.h"
27 #include "GP_TextMetric.h"
29 extern GP_TextStyle GP_DefaultStyle
;
31 static const GP_GlyphBitmap
*get_glyph(const GP_TextStyle
*style
, int c
)
33 const GP_GlyphBitmap
*glyph
= GP_GetGlyphBitmap(style
->font
, c
);
36 glyph
= GP_GetGlyphBitmap(style
->font
, ' ');
41 static unsigned int multiply_width(const GP_TextStyle
*style
, unsigned int w
)
43 return w
* style
->pixel_xmul
+ (w
- 1) * style
->pixel_xspace
;
47 * Returns glyph advance.
49 static unsigned int glyph_advance_x(const GP_TextStyle
*style
, int ch
)
51 const GP_GlyphBitmap
*glyph
= get_glyph(style
, ch
);
53 return multiply_width(style
, glyph
->advance_x
);
57 * Return maximal glyph advance for a character from str.
59 static unsigned int max_glyph_advance_x(const GP_TextStyle
*style
,
62 unsigned int max
= 0, i
;
64 for (i
= 0; str
[i
] != '\0'; i
++)
65 max
= GP_MAX(max
, glyph_advance_x(style
, str
[i
]));
71 * Returns _SINGLE_ glyph size, not including the bearing_x and including space
72 * occupied by the glyph bitmap if the bitmap width overflows glyph advance_x.
74 static unsigned int glyph_width(const GP_TextStyle
*style
, int c
)
76 unsigned int size
, advance
;
78 const GP_GlyphBitmap
*glyph
= get_glyph(style
, c
);
80 advance
= multiply_width(style
, glyph
->advance_x
- glyph
->bearing_x
);
81 size
= multiply_width(style
, glyph
->width
);
83 return (size
> advance
) ? size
: advance
;
87 * Returns size occupied by the last glyph. Here we take advance_x into account.
89 static unsigned int last_glyph_width(const GP_TextStyle
*style
, int c
)
91 unsigned int size
, advance
;
93 const GP_GlyphBitmap
*glyph
= get_glyph(style
, c
);
95 advance
= multiply_width(style
, glyph
->advance_x
);
96 size
= multiply_width(style
, glyph
->width
+ glyph
->bearing_x
);
98 return (size
> advance
) ? size
: advance
;
102 * Returns first glyph width, that is and advance minus the bearing_x.
104 static unsigned int first_glyph_width(const GP_TextStyle
*style
, int c
)
106 const GP_GlyphBitmap
*glyph
= get_glyph(style
, c
);
108 return multiply_width(style
, glyph
->advance_x
- glyph
->bearing_x
);
112 * Returns text width.
114 * There are two problems with text width it's start and it's end.
116 * At the start the first letter may have bearing_x negative, making it
117 * overflow out of the bouding box and even in case it's possitive the returned
118 * size would be slightly bigger. This one is easy to fix.
120 * The end of the string is problematic too, some of the glyphs may have
121 * advance smaller than sum of the bitmap width and bearing_x that way we would
122 * return slightly less than is the actuall size of the text bouding box. The
123 * other problem is that the advance is usually grater than the glyph space (as
124 * the exact size of the glyph itself without advance before and space after is
125 * not known), so we likely return some more pixels than is needed, but that's
128 unsigned int GP_TextWidth(const GP_TextStyle
*style
, const char *str
)
133 style
= &GP_DefaultStyle
;
135 if (str
== NULL
|| str
[0] == '\0')
138 /* special case, one letter */
139 if (str
[1] == '\0') {
140 return glyph_width(style
, str
[0]);
144 len
= first_glyph_width(style
, str
[0]) + style
->char_xspace
;
147 for (i
= 1; str
[i
+1] != '\0'; i
++)
148 len
+= glyph_advance_x(style
, str
[i
]) + style
->char_xspace
;
151 len
+= last_glyph_width(style
, str
[i
]);
157 * Return max advance now. We will do something better maybe.
159 GP_Size
GP_TextMaxWidth(const GP_TextStyle
*style
, unsigned int len
)
162 style
= &GP_DefaultStyle
;
167 return multiply_width(style
, len
* style
->font
->max_glyph_advance
) +
168 (len
- 1) * style
->char_xspace
;
174 GP_Size
GP_TextMaxStrWidth(const GP_TextStyle
*style
, const char *str
,
178 style
= &GP_DefaultStyle
;
180 if (len
== 0 || str
== NULL
)
183 return len
* max_glyph_advance_x(style
, str
) +
184 (len
- 1) * style
->char_xspace
;
187 /* Ascend, Descend, Height -- far easier */
189 static unsigned int multiply_height(const GP_TextStyle
*style
, unsigned int h
)
191 return h
* style
->pixel_ymul
+ (h
- 1) * style
->pixel_yspace
;
194 GP_Size
GP_TextHeight(const GP_TextStyle
*style
)
197 style
= &GP_DefaultStyle
;
199 unsigned int height
= style
->font
->ascend
+ style
->font
->descend
;
201 return multiply_height(style
, height
);
204 GP_Size
GP_TextAscent(const GP_TextStyle
*style
)
207 style
= &GP_DefaultStyle
;
210 return multiply_height(style
, style
->font
->ascend
);
213 GP_Size
GP_TextDescent(const GP_TextStyle
*style
)
216 style
= &GP_DefaultStyle
;
218 return multiply_height(style
, style
->font
->descend
);