1 /****************************************************************************
2 * RRDtool 1.2.16 Copyright by Tobi Oetiker, 1997-2006
3 ****************************************************************************
4 * rrd_afm.h Parsing afm tables to find width of strings.
5 ****************************************************************************/
7 #if defined(_WIN32) && !defined(__CYGWIN__) && !defined(__CYGWIN32__) && !defined(HAVE_CONFIG_H)
8 #include "../win32/config.h"
9 #else
10 #ifdef HAVE_CONFIG_H
11 #include "../rrd_config.h"
12 #endif
13 #endif
15 #include "rrd_afm.h"
16 #include "rrd_afm_data.h"
18 #include <stdio.h>
20 #ifdef HAVE_STRING_H
21 #include <string.h>
22 #endif
24 #include "unused.h"
26 #if 0
27 # define DEBUG 1
28 # define DLOG(x) fprintf x
29 #else
30 # define DEBUG 0
31 # define DLOG(x)
32 #endif
34 /* Adobe SVG View and Batik 1.1.1 can't handle ligatures.
35 So disable it as we just waste speed.
36 Besides, it doesn't matter much in normal text.
37 */
38 #define ENABLE_LIGATURES 0
40 static const afm_fontinfo *afm_last_used_font = NULL;
41 static const char *last_unknown_font = NULL;
43 #define is_font(p, name) \
44 (!strcmp(p->postscript_name, name) || !strcmp(p->fullname, name))
46 static const afm_fontinfo *afm_searchfont(const char *name)
47 {
48 int i;
49 const afm_fontinfo *p = afm_last_used_font;
50 if (p && is_font(p, name))
51 return p;
52 p = afm_fontinfolist;
53 for (i = 0; i < afm_fontinfo_count; i++, p++) {
54 if (is_font(p, name)) {
55 afm_last_used_font = p;
56 return p;
57 }
58 }
59 return NULL;
60 }
63 /* returns always a font, never NULL.
64 The rest of the code depends on the result never being NULL.
65 See rrd_afm.h */
66 static const afm_fontinfo *afm_findfont(const char *name)
67 {
68 const afm_fontinfo *p = afm_searchfont(name);
69 if (p)
70 return p;
71 if (!last_unknown_font || strcmp(name, last_unknown_font)) {
72 fprintf(stderr, "Can't find font '%s'\n", name);
73 last_unknown_font = name;
74 }
75 p = afm_searchfont(RRD_AFM_DEFAULT_FONT);
76 if (p)
77 return p;
78 return afm_fontinfolist; /* anything, just anything. */
79 }
81 const char *afm_get_font_postscript_name(const char* font)
82 {
83 const afm_fontinfo *p = afm_findfont(font);
84 return p->postscript_name;
85 }
87 const char *afm_get_font_name(const char* font)
88 {
89 const afm_fontinfo *p = afm_findfont(font);
90 return p->fullname;
91 }
93 double afm_get_ascender(const char* font, double size)
94 {
95 const afm_fontinfo *p = afm_findfont(font);
96 return size * p->ascender / 1000.0;
97 }
99 double afm_get_descender(const char* font, double size)
100 {
101 const afm_fontinfo *p = afm_findfont(font);
102 return size * p->descender / 1000.0;
103 }
105 static int afm_find_char_index(const afm_fontinfo *fontinfo,
106 afm_cunicode ch1)
107 {
108 int idx = ch1 - 32;
109 afm_cuint16 *indexP;
110 int numIndexChars, i;
111 if (idx <= 0)
112 return 0;
113 if (idx <= 126 - 32)
114 return idx;
115 indexP = fontinfo->highchars_index;
116 if (indexP == 0)
117 return 0;
118 numIndexChars = fontinfo->highchars_count;
119 DLOG((stderr, " find highbit, num = %d\n", numIndexChars));
120 if (ch1 >= 161 && ch1 <= 255) {
121 idx = ch1 - 161;
122 DLOG((stderr, " 161, idx = %d -> %d\n", idx, indexP[idx]));
123 if (idx < numIndexChars && indexP[idx] == ch1) {
124 idx += 127 - 32;
125 DLOG((stderr, " 161-guessed ok to %d\n", idx));
126 return idx;
127 }
128 }
129 for (i = 0; i < numIndexChars; i++) {
130 DLOG((stderr, " compares to %d -> %d\n", indexP[i], i));
131 if (indexP[i] == ch1)
132 return i + 127 - 32;
133 }
134 DLOG((stderr, "Did not find %d in highchars_index ??\n", ch1));
135 return 0;
136 }
138 #if ENABLE_LIGATURES
139 static afm_cunicode afm_find_combined_ligature(const afm_fontinfo *fontinfo,
140 afm_cunicode ch1, afm_cunicode ch2)
141 {
142 afm_cunicode *p = fontinfo->ligatures;
143 int num = fontinfo->ligatures_count;
144 int i;
145 if (!num)
146 return 0;
147 DLOG((stderr, " find-lig, num = %d\n", num));
148 for (i = 0; i < num; i++, p += 3) {
149 DLOG((stderr, " lig: %d + %d -> %d (%c %c %c)\n",
150 p[0], p[1], p[2], p[0], p[1], p[2]));
151 if (ch1 == *p && ch2 == p[1]) {
152 DLOG((stderr, " matches.\n"));
153 return p[2];
154 }
155 }
156 return 0;
157 }
158 #endif
160 #define READ_ESCAPED(p, val) \
161 if ((val = *p++) == 0) { \
162 val = 254 + *p++; \
163 } else if (!--val) { \
164 val = *p++ << 8; \
165 val |= *p++; \
166 }
169 static long afm_find_kern(const afm_fontinfo *fontinfo,
170 int kern_idx, afm_cunicode ch2)
171 {
172 afm_cuint8 *p8 = fontinfo->kerning_data + kern_idx;
173 int num;
174 READ_ESCAPED(p8, num);
175 DLOG((stderr, " find kern, num pairs = %d\n", num));
176 while (num > 0) {
177 afm_unicode ch;
178 READ_ESCAPED(p8, ch);
179 DLOG((stderr, " pair-char = %d\n", ch));
180 if (ch == ch2) {
181 DLOG((stderr, " got kern = %d\n", *(afm_csint8*)p8));
182 return *(afm_csint8*)p8;
183 }
184 p8++;
185 num--;
186 }
187 return 0;
188 }
190 /* measure width of a text string */
191 double afm_get_text_width( double start, const char* font, double size,
192 double tabwidth, const char* text)
193 {
194 #ifdef HAVE_MBSTOWCS
195 size_t clen = strlen(text) + 1;
196 wchar_t *cstr = malloc(sizeof(wchar_t) * clen); /* yes we are allocating probably too much here, I know */
197 int text_count = mbstowcs(cstr, text, clen);
198 double w;
199 if (text_count == -1)
200 text_count = mbstowcs(cstr, "Enc-Err", 6);
201 #ifdef __APPLE__
202 while (text_count > 0) {
203 text_count--;
204 cstr[text_count] = afm_fix_osx_charset(cstr[text_count]); /* unsafe macro */
205 }
206 #endif
207 w = afm_get_text_width_wide(start, font, size, tabwidth, cstr);
208 free(cstr);
209 return w;
210 #else
211 return afm_get_text_width_wide(start, font, size, tabwidth, text);
212 #endif
213 }
215 double afm_get_text_width_wide( double UNUSED(start), const char* font, double size,
216 double UNUSED(tabwidth), const afm_char* text)
217 {
218 const afm_fontinfo *fontinfo = afm_findfont(font);
219 long width = 0;
220 double widthf;
221 const afm_char *up = text;
222 DLOG((stderr, "================= %s\n", text));
223 if (fontinfo == NULL) {
224 while (*up)
225 up++;
226 return size * (up - text);
227 }
228 while (1) {
229 afm_unicode ch1, ch2;
230 int idx1, kern_idx;
231 if ((ch1 = *up) == 0)
232 break;
233 ch2 = *++up;
234 DLOG((stderr, "------------- Loop: %d + %d (%c%c) at %d\n",
235 ch1, ch2, ch1, ch2 ? ch2 : ' ',
236 (up - (const unsigned char*)text) - 1));
237 idx1 = afm_find_char_index(fontinfo, ch1);
238 DLOG((stderr, " idx1 = %d\n", idx1));
239 #if ENABLE_LIGATURES
240 if (ch2) {
241 int ch1_new = afm_find_combined_ligature(fontinfo, ch1, ch2);
242 DLOG((stderr, " lig-ch = %d\n", ch1_new));
243 if (ch1_new) {
244 ch1 = ch1_new;
245 idx1 = afm_find_char_index(fontinfo, ch1);
246 ch2 = *++up;
247 DLOG((stderr, " -> idx1 = %d, ch2 = %d (%c)\n",
248 idx1, ch2, ch2 ? ch2 : ' '));
249 }
250 }
251 #endif
252 width += fontinfo->widths[idx1];
253 DLOG((stderr, "Plain width of %d = %d\n", ch1, fontinfo->widths[idx1]));
254 if (fontinfo->kerning_index && ch2) {
255 kern_idx = fontinfo->kerning_index[idx1];
256 DLOG((stderr, " kern_idx = %d\n", kern_idx));
257 if (kern_idx > 0)
258 width += afm_find_kern(fontinfo, kern_idx, ch2);
259 }
260 }
261 widthf = (width * 6 / 1000.0) * size;
262 DLOG((stderr, "Returns %ld (%ld) -> %f\n", width, width * 6, widthf));
263 return widthf;
264 }
266 #ifdef __APPLE__
267 const unsigned char afm_mac2iso[128] = {
268 '\xC4', '\xC5', '\xC7', '\xC9', '\xD1', '\xD6', '\xDC', '\xE1', /* 80 */
269 '\xE0', '\xE2', '\xE4', '\xE3', '\xE5', '\xE7', '\xE9', '\xE8', /* 88 */
270 '\xEA', '\xEB', '\xED', '\xEC', '\xEE', '\xEF', '\xF1', '\xF3', /* 90 */
271 '\xF2', '\xF4', '\xF6', '\xF5', '\xFA', '\xF9', '\xFB', '\xFC', /* 98 */
272 '\xDD', '\xB0', '\xA2', '\xA3', '\xA7', ' ', '\xB6', '\xDF', /* A0 */
273 '\xAE', '\xA9', ' ', '\xB4', '\xA8', ' ', '\xC6', '\xD8', /* A8 */
274 ' ', '\xB1', '\xBE', ' ', '\xA5', '\xB5', ' ', ' ', /* B0 */
275 '\xBD', '\xBC', ' ', '\xAA', '\xBA', ' ', '\xE6', '\xF8', /* B8 */
276 '\xBF', '\xA1', '\xAC', ' ', ' ', ' ', ' ', '\xAB', /* C0 */
277 '\xBB', ' ', '\xA0', '\xC0', '\xC3', '\xD5', ' ', '\xA6', /* C8 */
278 '\xAD', ' ', '"', '"', '\'', '\'', '\xF7', '\xD7', /* D0 */
279 '\xFF', ' ', ' ', '\xA4', '\xD0', '\xF0', '\xDE', '\xFE', /* D8 */
280 '\xFD', '\xB7', ' ', ' ', ' ', '\xC2', '\xCA', '\xC1', /* E0 */
281 '\xCB', '\xC8', '\xCD', '\xCE', '\xCF', '\xCC', '\xD3', '\xD4', /* E8 */
282 ' ', '\xD2', '\xDA', '\xDB', '\xD9', ' ', ' ', ' ', /* F0 */
283 '\xAF', ' ', ' ', ' ', '\xB8', ' ', ' ', ' ', /* F8 */
284 };
285 #endif