1 /****************************************************************************
2 * RRDtool 1.2.9 Copyright by Tobi Oetiker, 1997-2005
3 ****************************************************************************
4 * rrd_afm.h Parsing afm tables to find width of strings.
5 ****************************************************************************/
7 #if defined(_WIN32) && !defined(__CYGWIN__) && !defined(__CYGWIN32__)
8 #include "../confignt/config.h"
14 #include "rrd_afm_data.h"
26 # define DLOG(x) fprintf x
32 /* Adobe SVG View and Batik 1.1.1 can't handle ligatures.
33 So disable it as we just waste speed.
34 Besides, it doesn't matter much in normal text.
36 #define ENABLE_LIGATURES 0
38 static const afm_fontinfo *afm_last_used_font = NULL;
39 static const char *last_unknown_font = NULL;
41 #define is_font(p, name) \
42 (!strcmp(p->postscript_name, name) || !strcmp(p->fullname, name))
44 static const afm_fontinfo *afm_searchfont(const char *name)
47 const afm_fontinfo *p = afm_last_used_font;
48 if (p && is_font(p, name))
51 for (i = 0; i < afm_fontinfo_count; i++, p++) {
52 if (is_font(p, name)) {
53 afm_last_used_font = p;
61 /* returns always a font, never NULL.
62 The rest of the code depends on the result never being NULL.
64 static const afm_fontinfo *afm_findfont(const char *name)
66 const afm_fontinfo *p = afm_searchfont(name);
69 if (!last_unknown_font || strcmp(name, last_unknown_font)) {
70 fprintf(stderr, "Can't find font '%s'\n", name);
71 last_unknown_font = name;
73 p = afm_searchfont(RRD_AFM_DEFAULT_FONT);
76 return afm_fontinfolist; /* anything, just anything. */
79 const char *afm_get_font_postscript_name(const char* font)
81 const afm_fontinfo *p = afm_findfont(font);
82 return p->postscript_name;
85 const char *afm_get_font_name(const char* font)
87 const afm_fontinfo *p = afm_findfont(font);
91 double afm_get_ascender(const char* font, double size)
93 const afm_fontinfo *p = afm_findfont(font);
94 return size * p->ascender / 1000.0;
97 double afm_get_descender(const char* font, double size)
99 const afm_fontinfo *p = afm_findfont(font);
100 return size * p->descender / 1000.0;
103 static int afm_find_char_index(const afm_fontinfo *fontinfo,
108 int numIndexChars, i;
113 indexP = fontinfo->highchars_index;
116 numIndexChars = fontinfo->highchars_count;
117 DLOG((stderr, " find highbit, num = %d\n", numIndexChars));
118 if (ch1 >= 161 && ch1 <= 255) {
120 DLOG((stderr, " 161, idx = %d -> %d\n", idx, indexP[idx]));
121 if (idx < numIndexChars && indexP[idx] == ch1) {
123 DLOG((stderr, " 161-guessed ok to %d\n", idx));
127 for (i = 0; i < numIndexChars; i++) {
128 DLOG((stderr, " compares to %d -> %d\n", indexP[i], i));
129 if (indexP[i] == ch1)
132 DLOG((stderr, "Did not find %d in highchars_index ??\n", ch1));
137 static afm_cunicode afm_find_combined_ligature(const afm_fontinfo *fontinfo,
138 afm_cunicode ch1, afm_cunicode ch2)
140 afm_cunicode *p = fontinfo->ligatures;
141 int num = fontinfo->ligatures_count;
145 DLOG((stderr, " find-lig, num = %d\n", num));
146 for (i = 0; i < num; i++, p += 3) {
147 DLOG((stderr, " lig: %d + %d -> %d (%c %c %c)\n",
148 p[0], p[1], p[2], p[0], p[1], p[2]));
149 if (ch1 == *p && ch2 == p[1]) {
150 DLOG((stderr, " matches.\n"));
158 #define READ_ESCAPED(p, val) \
159 if ((val = *p++) == 0) { \
161 } else if (!--val) { \
167 static long afm_find_kern(const afm_fontinfo *fontinfo,
168 int kern_idx, afm_cunicode ch2)
170 afm_cuint8 *p8 = fontinfo->kerning_data + kern_idx;
172 READ_ESCAPED(p8, num);
173 DLOG((stderr, " find kern, num pairs = %d\n", num));
176 READ_ESCAPED(p8, ch);
177 DLOG((stderr, " pair-char = %d\n", ch));
179 DLOG((stderr, " got kern = %d\n", *(afm_csint8*)p8));
180 return *(afm_csint8*)p8;
188 /* measure width of a text string */
189 double afm_get_text_width( double start, const char* font, double size,
190 double tabwidth, const char* text)
193 size_t clen = strlen(text) + 1;
194 wchar_t *cstr = malloc(sizeof(wchar_t) * clen); /* yes we are allocating probably too much here, I know */
195 int text_count = mbstowcs(cstr, text, clen);
197 if (text_count == -1)
198 text_count = mbstowcs(cstr, "Enc-Err", 6);
200 while (text_count > 0) {
202 cstr[text_count] = afm_fix_osx_charset(cstr[text_count]); /* unsafe macro */
205 w = afm_get_text_width_wide(start, font, size, tabwidth, cstr);
209 return afm_get_text_width_wide(start, font, size, tabwidth, text);
213 double afm_get_text_width_wide( double UNUSED(start), const char* font, double size,
214 double UNUSED(tabwidth), const afm_char* text)
216 const afm_fontinfo *fontinfo = afm_findfont(font);
219 const afm_char *up = text;
220 DLOG((stderr, "================= %s\n", text));
221 if (fontinfo == NULL) {
224 return size * (up - text);
227 afm_unicode ch1, ch2;
229 if ((ch1 = *up) == 0)
232 DLOG((stderr, "------------- Loop: %d + %d (%c%c) at %d\n",
233 ch1, ch2, ch1, ch2 ? ch2 : ' ',
234 (up - (const unsigned char*)text) - 1));
235 idx1 = afm_find_char_index(fontinfo, ch1);
236 DLOG((stderr, " idx1 = %d\n", idx1));
239 int ch1_new = afm_find_combined_ligature(fontinfo, ch1, ch2);
240 DLOG((stderr, " lig-ch = %d\n", ch1_new));
243 idx1 = afm_find_char_index(fontinfo, ch1);
245 DLOG((stderr, " -> idx1 = %d, ch2 = %d (%c)\n",
246 idx1, ch2, ch2 ? ch2 : ' '));
250 width += fontinfo->widths[idx1];
251 DLOG((stderr, "Plain width of %d = %d\n", ch1, fontinfo->widths[idx1]));
252 if (fontinfo->kerning_index && ch2) {
253 kern_idx = fontinfo->kerning_index[idx1];
254 DLOG((stderr, " kern_idx = %d\n", kern_idx));
256 width += afm_find_kern(fontinfo, kern_idx, ch2);
259 widthf = (width * 6 / 1000.0) * size;
260 DLOG((stderr, "Returns %ld (%ld) -> %f\n", width, width * 6, widthf));
265 const unsigned char afm_mac2iso[128] = {
266 '\xC4', '\xC5', '\xC7', '\xC9', '\xD1', '\xD6', '\xDC', '\xE1', /* 80 */
267 '\xE0', '\xE2', '\xE4', '\xE3', '\xE5', '\xE7', '\xE9', '\xE8', /* 88 */
268 '\xEA', '\xEB', '\xED', '\xEC', '\xEE', '\xEF', '\xF1', '\xF3', /* 90 */
269 '\xF2', '\xF4', '\xF6', '\xF5', '\xFA', '\xF9', '\xFB', '\xFC', /* 98 */
270 '\xDD', '\xB0', '\xA2', '\xA3', '\xA7', ' ', '\xB6', '\xDF', /* A0 */
271 '\xAE', '\xA9', ' ', '\xB4', '\xA8', ' ', '\xC6', '\xD8', /* A8 */
272 ' ', '\xB1', '\xBE', ' ', '\xA5', '\xB5', ' ', ' ', /* B0 */
273 '\xBD', '\xBC', ' ', '\xAA', '\xBA', ' ', '\xE6', '\xF8', /* B8 */
274 '\xBF', '\xA1', '\xAC', ' ', ' ', ' ', ' ', '\xAB', /* C0 */
275 '\xBB', ' ', '\xA0', '\xC0', '\xC3', '\xD5', ' ', '\xA6', /* C8 */
276 '\xAD', ' ', '"', '"', '\'', '\'', '\xF7', '\xD7', /* D0 */
277 '\xFF', ' ', ' ', '\xA4', '\xD0', '\xF0', '\xDE', '\xFE', /* D8 */
278 '\xFD', '\xB7', ' ', ' ', ' ', '\xC2', '\xCA', '\xC1', /* E0 */
279 '\xCB', '\xC8', '\xCD', '\xCE', '\xCF', '\xCC', '\xD3', '\xD4', /* E8 */
280 ' ', '\xD2', '\xDA', '\xDB', '\xD9', ' ', ' ', ' ', /* F0 */
281 '\xAF', ' ', ' ', ' ', '\xB8', ' ', ' ', ' ', /* F8 */