Fix the directory layout and build system.
[gfxprim.git] / libs / text / GP_TextMetric.c
blobcac94cf211a7e5d0a9ee78ac91632a22c6af3b96
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-2010 Cyril Hrubis <metan@ucw.cz> *
23 * *
24 *****************************************************************************/
26 #include "core/GP_Common.h"
27 #include "GP_TextMetric.h"
29 static unsigned int SpaceWidth(const GP_TextStyle *style)
31 //TODO: Does space change with pixel_yspace?
32 return style->char_xspace * style->pixel_xmul;
35 // FIXME: This is not quite right - due to offsets, characters
36 // can exceed their bounding box and then the reported width will be
37 // shorter than expected.
38 static unsigned int CharWidth(const GP_TextStyle *style, char ch)
40 unsigned int pixel_multiplier = style->pixel_xmul + style->pixel_xspace;
41 const GP_CharData *data = GP_GetCharData(style->font, ch);
43 if (data == NULL)
44 data = GP_GetCharData(style->font, ' ');
46 return data->pre_offset * pixel_multiplier
47 + data->post_offset * pixel_multiplier;
50 static unsigned int MaxCharsWidth(const GP_TextStyle *style, const char *str)
52 unsigned int max = 0, i;
54 for (i = 0; str[i] != '\0'; i++)
55 max = GP_MAX(max, CharWidth(style, str[i]));
57 return max;
60 unsigned int GP_TextWidth(const GP_TextStyle *style, const char *str)
62 GP_CHECK_TEXT_STYLE(style);
63 GP_CHECK(str, "NULL string specified");
65 unsigned int i, len = 0;
66 unsigned int space = SpaceWidth(style);
68 if (str[0] == '\0')
69 return 0;
71 for (i = 0; str[i] != '\0'; i++)
72 len += CharWidth(style, str[i]);
74 return len + (i - 1) * space;
77 unsigned int GP_TextMaxWidth(const GP_TextStyle *style, unsigned int len)
79 GP_CHECK_TEXT_STYLE(style);
81 unsigned int space_width = SpaceWidth(style);
82 unsigned int char_width = style->font->max_bounding_width
83 * (style->pixel_xmul + style->pixel_xspace);
85 if (len == 0)
86 return 0;
88 return len * char_width + (len - 1) * space_width;
91 unsigned int GP_TextMaxStrWidth(const GP_TextStyle *style, const char *str,
92 unsigned int len)
94 GP_CHECK_TEXT_STYLE(style);
95 GP_CHECK(str, "NULL string specified");
97 unsigned int space_width = SpaceWidth(style);
98 unsigned int char_width;
100 if (len == 0)
101 return 0;
103 char_width = MaxCharsWidth(style, str);
105 return len * char_width + (len - 1) * space_width;
108 unsigned int GP_TextHeight(const GP_TextStyle *style)
110 GP_CHECK_TEXT_STYLE(style);
112 return style->font->height * style->pixel_ymul +
113 (style->font->height - 1) * style->pixel_yspace;
116 unsigned int GP_TextAscent(const GP_TextStyle *style)
118 GP_CHECK_TEXT_STYLE(style);
120 unsigned int h = style->font->height - style->font->baseline;
121 return h * style->pixel_ymul + (h - 1) * style->pixel_yspace;
124 unsigned int GP_TextDescent(const GP_TextStyle *style)
126 GP_CHECK_TEXT_STYLE(style);
128 unsigned int h = style->font->baseline;
129 return h * style->pixel_ymul + (h - 1) * style->pixel_yspace;