Text: Text core rewrite + freetype support.
[gfxprim.git] / libs / text / GP_TextMetric.c
blobae746dfa9df77648904c04aaae130144cae92bf4
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-2011 Cyril Hrubis <metan@ucw.cz> *
23 * *
24 *****************************************************************************/
26 #include "core/GP_Common.h"
27 #include "GP_TextMetric.h"
29 extern GP_TextStyle GP_DefaultStyle;
32 * Returns glyph width from the basepoint to the end of the glyph bitmap.
34 static unsigned int glyph_width(const GP_TextStyle *style, char ch)
36 unsigned int pixel_multiplier = style->pixel_xmul + style->pixel_xspace;
37 const GP_GlyphBitmap *glyph = GP_GetGlyphBitmap(style->font, ch);
39 if (glyph == NULL)
40 glyph = GP_GetGlyphBitmap(style->font, ' ');
42 return (glyph->width + glyph->bearing_x) * pixel_multiplier;
46 * Returns space that is added after the glyph.
48 * Eg. the advance minus glyph width.
50 * TODO: kerning
52 static unsigned int glyph_space(const GP_TextStyle *style, char ch)
54 unsigned int pixel_multiplier = style->pixel_xmul + style->pixel_xspace;
55 const GP_GlyphBitmap *glyph = GP_GetGlyphBitmap(style->font, ch);
57 if (glyph == NULL)
58 glyph = GP_GetGlyphBitmap(style->font, ' ');
60 return (glyph->advance_x - glyph->width - glyph->bearing_x)
61 * pixel_multiplier + style->char_xspace;
65 * Returns maximal character width for a given string.
67 static unsigned int max_glyph_width(const GP_TextStyle *style, const char *str)
69 unsigned int max = 0, i;
71 for (i = 0; str[i] != '\0'; i++)
72 max = GP_MAX(max, glyph_width(style, str[i]));
74 return max;
78 * Return maximal character space after an glyph.
80 static unsigned int max_glyph_space(const GP_TextStyle *style, const char *str)
82 unsigned int max = 0, i;
84 for (i = 0; str[i] != '\0'; i++)
85 max = GP_MAX(max, glyph_space(style, str[i]));
87 return max;
90 unsigned int GP_TextWidth(const GP_TextStyle *style, const char *str)
92 unsigned int i, len = 0;
94 if (style == NULL)
95 style = &GP_DefaultStyle;
97 if (str == NULL || str[0] == '\0')
98 return 0;
100 for (i = 0; str[i] != '\0'; i++) {
101 len += glyph_width(style, str[i]);
103 if (str[i+1] != '\0')
104 len += glyph_space(style, str[i]);
107 return len;
110 GP_Size GP_TextMaxWidth(const GP_TextStyle *style, unsigned int len)
112 unsigned int glyph_width;
113 //TODO: !
114 unsigned int space_width = style->char_xspace;
116 if (style == NULL)
117 style = &GP_DefaultStyle;
119 glyph_width = style->font->max_glyph_width
120 * (style->pixel_xmul + style->pixel_xspace);
122 if (len == 0)
123 return 0;
125 return len * glyph_width + (len - 1) * space_width;
128 GP_Size GP_TextMaxStrWidth(const GP_TextStyle *style, const char *str,
129 unsigned int len)
131 unsigned int space_width;
132 unsigned int glyph_width;
134 if (style == NULL)
135 style = &GP_DefaultStyle;
137 space_width = max_glyph_space(style, str);
139 if (len == 0 || str == NULL)
140 return 0;
142 glyph_width = max_glyph_width(style, str);
144 return len * glyph_width + (len - 1) * space_width;
147 GP_Size GP_TextHeight(const GP_TextStyle *style)
149 unsigned int h;
151 if (style == NULL)
152 style = &GP_DefaultStyle;
154 h = style->font->ascend + style->font->descend;
156 return h * style->pixel_ymul +
157 (h - 1) * style->pixel_yspace;
160 GP_Size GP_TextAscent(const GP_TextStyle *style)
162 unsigned int h;
164 if (style == NULL)
165 style = &GP_DefaultStyle;
167 h = style->font->ascend;
169 return h * style->pixel_ymul + (h - 1) * style->pixel_yspace;
172 GP_Size GP_TextDescent(const GP_TextStyle *style)
174 unsigned int h;
176 if (style == NULL)
177 style = &GP_DefaultStyle;
179 h = style->font->descend;
181 return h * style->pixel_ymul + (h - 1) * style->pixel_yspace;