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-2011 Cyril Hrubis <metan@ucw.cz> *
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
);
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.
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
);
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
]));
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
]));
90 unsigned int GP_TextWidth(const GP_TextStyle
*style
, const char *str
)
92 unsigned int i
, len
= 0;
95 style
= &GP_DefaultStyle
;
97 if (str
== NULL
|| str
[0] == '\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
]);
110 GP_Size
GP_TextMaxWidth(const GP_TextStyle
*style
, unsigned int len
)
112 unsigned int glyph_width
;
114 unsigned int space_width
= style
->char_xspace
;
117 style
= &GP_DefaultStyle
;
119 glyph_width
= style
->font
->max_glyph_width
120 * (style
->pixel_xmul
+ style
->pixel_xspace
);
125 return len
* glyph_width
+ (len
- 1) * space_width
;
128 GP_Size
GP_TextMaxStrWidth(const GP_TextStyle
*style
, const char *str
,
131 unsigned int space_width
;
132 unsigned int glyph_width
;
135 style
= &GP_DefaultStyle
;
137 space_width
= max_glyph_space(style
, str
);
139 if (len
== 0 || str
== NULL
)
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
)
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
)
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
)
177 style
= &GP_DefaultStyle
;
179 h
= style
->font
->descend
;
181 return h
* style
->pixel_ymul
+ (h
- 1) * style
->pixel_yspace
;