git: Add more gitignore files.
[gfxprim.git] / libs / text / GP_TextMetric.c
blob45371fedf039be360d98691917d0c84c1150240d
1 /*****************************************************************************
2 * This file is part of gfxprim library. *
3 * *
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. *
8 * *
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. *
13 * *
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 *
18 * *
19 * Copyright (C) 2009-2010 Jiri "BlueBear" Dluhos *
20 * <jiri.bluebear.dluhos@gmail.com> *
21 * *
22 * Copyright (C) 2009-2012 Cyril Hrubis <metan@ucw.cz> *
23 * *
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);
35 if (glyph == NULL)
36 glyph = GP_GetGlyphBitmap(style->font, ' ');
38 return glyph;
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,
60 const char *str)
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]));
67 return max;
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
126 * the joy of life.
128 unsigned int GP_TextWidth(const GP_TextStyle *style, const char *str)
130 unsigned int i, len;
132 if (style == NULL)
133 style = &GP_DefaultStyle;
135 if (str == NULL || str[0] == '\0')
136 return 0;
138 /* special case, one letter */
139 if (str[1] == '\0') {
140 return glyph_width(style, str[0]);
143 /* first letter */
144 len = first_glyph_width(style, str[0]) + style->char_xspace;
146 /* middle letters */
147 for (i = 1; str[i+1] != '\0'; i++)
148 len += glyph_advance_x(style, str[i]) + style->char_xspace;
150 /* last letter */
151 len += last_glyph_width(style, str[i]);
153 return len;
157 * Return max advance now. We will do something better maybe.
159 GP_Size GP_TextMaxWidth(const GP_TextStyle *style, unsigned int len)
161 if (style == NULL)
162 style = &GP_DefaultStyle;
164 if (len == 0)
165 return 0;
167 return multiply_width(style, len * style->font->max_glyph_advance) +
168 (len - 1) * style->char_xspace;
172 * Here too.
174 GP_Size GP_TextMaxStrWidth(const GP_TextStyle *style, const char *str,
175 unsigned int len)
177 if (style == NULL)
178 style = &GP_DefaultStyle;
180 if (len == 0 || str == NULL)
181 return 0;
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)
196 if (style == NULL)
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)
206 if (style == NULL)
207 style = &GP_DefaultStyle;
210 return multiply_height(style, style->font->ascend);
213 GP_Size GP_TextDescent(const GP_TextStyle *style)
215 if (style == NULL)
216 style = &GP_DefaultStyle;
218 return multiply_height(style, style->font->descend);