1 /****************************************************************************
2 * RRDtool 1.2.10 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"
9 #else
10 #include "config.h"
11 #endif
13 #include "rrd_afm.h"
14 #include "rrd_afm_data.h"
16 #include <stdio.h>
18 #ifdef HAVE_STRING_H
19 #include <string.h>
20 #endif
22 #include "unused.h"
24 #if 0
25 # define DEBUG 1
26 # define DLOG(x) fprintf x
27 #else
28 # define DEBUG 0
29 # define DLOG(x)
30 #endif
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.
35 */
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)
45 {
46 int i;
47 const afm_fontinfo *p = afm_last_used_font;
48 if (p && is_font(p, name))
49 return p;
50 p = afm_fontinfolist;
51 for (i = 0; i < afm_fontinfo_count; i++, p++) {
52 if (is_font(p, name)) {
53 afm_last_used_font = p;
54 return p;
55 }
56 }
57 return NULL;
58 }
61 /* returns always a font, never NULL.
62 The rest of the code depends on the result never being NULL.
63 See rrd_afm.h */
64 static const afm_fontinfo *afm_findfont(const char *name)
65 {
66 const afm_fontinfo *p = afm_searchfont(name);
67 if (p)
68 return p;
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;
72 }
73 p = afm_searchfont(RRD_AFM_DEFAULT_FONT);
74 if (p)
75 return p;
76 return afm_fontinfolist; /* anything, just anything. */
77 }
79 const char *afm_get_font_postscript_name(const char* font)
80 {
81 const afm_fontinfo *p = afm_findfont(font);
82 return p->postscript_name;
83 }
85 const char *afm_get_font_name(const char* font)
86 {
87 const afm_fontinfo *p = afm_findfont(font);
88 return p->fullname;
89 }
91 double afm_get_ascender(const char* font, double size)
92 {
93 const afm_fontinfo *p = afm_findfont(font);
94 return size * p->ascender / 1000.0;
95 }
97 double afm_get_descender(const char* font, double size)
98 {
99 const afm_fontinfo *p = afm_findfont(font);
100 return size * p->descender / 1000.0;
101 }
103 static int afm_find_char_index(const afm_fontinfo *fontinfo,
104 afm_cunicode ch1)
105 {
106 int idx = ch1 - 32;
107 afm_cuint16 *indexP;
108 int numIndexChars, i;
109 if (idx <= 0)
110 return 0;
111 if (idx <= 126 - 32)
112 return idx;
113 indexP = fontinfo->highchars_index;
114 if (indexP == 0)
115 return 0;
116 numIndexChars = fontinfo->highchars_count;
117 DLOG((stderr, " find highbit, num = %d\n", numIndexChars));
118 if (ch1 >= 161 && ch1 <= 255) {
119 idx = ch1 - 161;
120 DLOG((stderr, " 161, idx = %d -> %d\n", idx, indexP[idx]));
121 if (idx < numIndexChars && indexP[idx] == ch1) {
122 idx += 127 - 32;
123 DLOG((stderr, " 161-guessed ok to %d\n", idx));
124 return idx;
125 }
126 }
127 for (i = 0; i < numIndexChars; i++) {
128 DLOG((stderr, " compares to %d -> %d\n", indexP[i], i));
129 if (indexP[i] == ch1)
130 return i + 127 - 32;
131 }
132 DLOG((stderr, "Did not find %d in highchars_index ??\n", ch1));
133 return 0;
134 }
136 #if ENABLE_LIGATURES
137 static afm_cunicode afm_find_combined_ligature(const afm_fontinfo *fontinfo,
138 afm_cunicode ch1, afm_cunicode ch2)
139 {
140 afm_cunicode *p = fontinfo->ligatures;
141 int num = fontinfo->ligatures_count;
142 int i;
143 if (!num)
144 return 0;
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"));
151 return p[2];
152 }
153 }
154 return 0;
155 }
156 #endif
158 #define READ_ESCAPED(p, val) \
159 if ((val = *p++) == 0) { \
160 val = 254 + *p++; \
161 } else if (!--val) { \
162 val = *p++ << 8; \
163 val |= *p++; \
164 }
167 static long afm_find_kern(const afm_fontinfo *fontinfo,
168 int kern_idx, afm_cunicode ch2)
169 {
170 afm_cuint8 *p8 = fontinfo->kerning_data + kern_idx;
171 int num;
172 READ_ESCAPED(p8, num);
173 DLOG((stderr, " find kern, num pairs = %d\n", num));
174 while (num > 0) {
175 afm_unicode ch;
176 READ_ESCAPED(p8, ch);
177 DLOG((stderr, " pair-char = %d\n", ch));
178 if (ch == ch2) {
179 DLOG((stderr, " got kern = %d\n", *(afm_csint8*)p8));
180 return *(afm_csint8*)p8;
181 }
182 p8++;
183 num--;
184 }
185 return 0;
186 }
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)
191 {
192 #ifdef HAVE_MBSTOWCS
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);
196 double w;
197 if (text_count == -1)
198 text_count = mbstowcs(cstr, "Enc-Err", 6);
199 #ifdef __APPLE__
200 while (text_count > 0) {
201 text_count--;
202 cstr[text_count] = afm_fix_osx_charset(cstr[text_count]); /* unsafe macro */
203 }
204 #endif
205 w = afm_get_text_width_wide(start, font, size, tabwidth, cstr);
206 free(cstr);
207 return w;
208 #else
209 return afm_get_text_width_wide(start, font, size, tabwidth, text);
210 #endif
211 }
213 double afm_get_text_width_wide( double UNUSED(start), const char* font, double size,
214 double UNUSED(tabwidth), const afm_char* text)
215 {
216 const afm_fontinfo *fontinfo = afm_findfont(font);
217 long width = 0;
218 double widthf;
219 const afm_char *up = text;
220 DLOG((stderr, "================= %s\n", text));
221 if (fontinfo == NULL) {
222 while (*up)
223 up++;
224 return size * (up - text);
225 }
226 while (1) {
227 afm_unicode ch1, ch2;
228 int idx1, kern_idx;
229 if ((ch1 = *up) == 0)
230 break;
231 ch2 = *++up;
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));
237 #if ENABLE_LIGATURES
238 if (ch2) {
239 int ch1_new = afm_find_combined_ligature(fontinfo, ch1, ch2);
240 DLOG((stderr, " lig-ch = %d\n", ch1_new));
241 if (ch1_new) {
242 ch1 = ch1_new;
243 idx1 = afm_find_char_index(fontinfo, ch1);
244 ch2 = *++up;
245 DLOG((stderr, " -> idx1 = %d, ch2 = %d (%c)\n",
246 idx1, ch2, ch2 ? ch2 : ' '));
247 }
248 }
249 #endif
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));
255 if (kern_idx > 0)
256 width += afm_find_kern(fontinfo, kern_idx, ch2);
257 }
258 }
259 widthf = (width * 6 / 1000.0) * size;
260 DLOG((stderr, "Returns %ld (%ld) -> %f\n", width, width * 6, widthf));
261 return widthf;
262 }
264 #ifdef __APPLE__
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 */
282 };
283 #endif