1 /****************************************************************************
2 * RRDtool 1.2.4 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"
25 # define DLOG(x) fprintf x
31 /* Adobe SVG View and Batik 1.1.1 can't handle ligatures.
32 So disable it as we just waste speed.
33 Besides, it doesn't matter much in normal text.
35 #define ENABLE_LIGATURES 0
37 static const afm_fontinfo *afm_last_used_font = NULL;
38 static const char *last_unknown_font = NULL;
40 #define is_font(p, name) \
41 (!strcmp(p->postscript_name, name) || !strcmp(p->fullname, name))
43 static const afm_fontinfo *afm_searchfont(const char *name)
46 const afm_fontinfo *p = afm_last_used_font;
47 if (p && is_font(p, name))
50 for (i = 0; i < afm_fontinfo_count; i++, p++) {
51 if (is_font(p, name)) {
52 afm_last_used_font = p;
60 /* returns always a font, never NULL.
61 The rest of the code depends on the result never being NULL.
63 static const afm_fontinfo *afm_findfont(const char *name)
65 const afm_fontinfo *p = afm_searchfont(name);
68 if (!last_unknown_font || strcmp(name, last_unknown_font)) {
69 fprintf(stderr, "Can't find font '%s'\n", name);
70 last_unknown_font = name;
72 p = afm_searchfont(RRD_AFM_DEFAULT_FONT);
75 return afm_fontinfolist; // anything, just anything.
78 const char *afm_get_font_postscript_name(const char* font)
80 const afm_fontinfo *p = afm_findfont(font);
81 return p->postscript_name;
84 const char *afm_get_font_name(const char* font)
86 const afm_fontinfo *p = afm_findfont(font);
90 double afm_get_ascender(const char* font, double size)
92 const afm_fontinfo *p = afm_findfont(font);
93 return size * p->ascender / 1000.0;
96 double afm_get_descender(const char* font, double size)
98 const afm_fontinfo *p = afm_findfont(font);
99 return size * p->descender / 1000.0;
102 static int afm_find_char_index(const afm_fontinfo *fontinfo,
107 int numIndexChars, i;
112 indexP = fontinfo->highchars_index;
115 numIndexChars = fontinfo->highchars_count;
116 DLOG((stderr, " find highbit, num = %d\n", numIndexChars));
117 if (ch1 >= 161 && ch1 <= 255) {
119 DLOG((stderr, " 161, idx = %d -> %d\n", idx, indexP[idx]));
120 if (idx < numIndexChars && indexP[idx] == ch1) {
122 DLOG((stderr, " 161-guessed ok to %d\n", idx));
126 for (i = 0; i < numIndexChars; i++) {
127 DLOG((stderr, " compares to %d -> %d\n", indexP[i], i));
128 if (indexP[i] == ch1)
131 DLOG((stderr, "Did not find %d in highchars_index ??\n", ch1));
136 static afm_cunicode afm_find_combined_ligature(const afm_fontinfo *fontinfo,
137 afm_cunicode ch1, afm_cunicode ch2)
139 afm_cunicode *p = fontinfo->ligatures;
140 int num = fontinfo->ligatures_count;
144 DLOG((stderr, " find-lig, num = %d\n", num));
145 for (i = 0; i < num; i++, p += 3) {
146 DLOG((stderr, " lig: %d + %d -> %d (%c %c %c)\n",
147 p[0], p[1], p[2], p[0], p[1], p[2]));
148 if (ch1 == *p && ch2 == p[1]) {
149 DLOG((stderr, " matches.\n"));
157 #define READ_ESCAPED(p, val) \
158 if ((val = *p++) == 0) { \
160 } else if (!--val) { \
166 static long afm_find_kern(const afm_fontinfo *fontinfo,
167 int kern_idx, afm_cunicode ch2)
169 afm_cuint8 *p8 = fontinfo->kerning_data + kern_idx;
171 READ_ESCAPED(p8, num);
172 DLOG((stderr, " find kern, num pairs = %d\n", num));
175 READ_ESCAPED(p8, ch);
176 DLOG((stderr, " pair-char = %d\n", ch));
178 DLOG((stderr, " got kern = %d\n", *(afm_csint8*)p8));
179 return *(afm_csint8*)p8;
187 /* measure width of a text string */
188 double afm_get_text_width ( double start, const char* font, double size,
189 double tabwidth, const char* text)
191 const afm_fontinfo *fontinfo = afm_findfont(font);
194 const unsigned char *up = (const unsigned char*)text;
195 DLOG((stderr, "================= %s\n", text));
196 if (fontinfo == NULL)
197 return size * strlen(text);
199 afm_unicode ch1, ch2;
201 if ((ch1 = *up) == 0)
203 ch1 = afm_host2unicode(ch1); /* unsafe macro */
205 ch2 = afm_host2unicode(ch2); /* unsafe macro */
206 DLOG((stderr, "------------- Loop: %d + %d (%c%c) at %d\n",
207 ch1, ch2, ch1, ch2 ? ch2 : ' ',
208 (up - (const unsigned char*)text) - 1));
209 idx1 = afm_find_char_index(fontinfo, ch1);
210 DLOG((stderr, " idx1 = %d\n", idx1));
213 int ch1_new = afm_find_combined_ligature(fontinfo, ch1, ch2);
214 DLOG((stderr, " lig-ch = %d\n", ch1_new));
217 idx1 = afm_find_char_index(fontinfo, ch1);
218 ch2 = afm_host2unicode(*++up);
219 DLOG((stderr, " -> idx1 = %d, ch2 = %d (%c)\n",
220 idx1, ch2, ch2 ? ch2 : ' '));
224 width += fontinfo->widths[idx1];
225 DLOG((stderr, "Plain width of %d = %d\n", ch1, fontinfo->widths[idx1]));
226 if (fontinfo->kerning_index && ch2) {
227 kern_idx = fontinfo->kerning_index[idx1];
228 DLOG((stderr, " kern_idx = %d\n", kern_idx));
230 width += afm_find_kern(fontinfo, kern_idx, ch2);
233 widthf = (width * 6 / 1000.0) * size;
234 DLOG((stderr, "Returns %ld (%ld) -> %f\n", width, width * 6, widthf));
239 const unsigned char afm_mac2iso[128] = {
240 '\xC4', '\xC5', '\xC7', '\xC9', '\xD1', '\xD6', '\xDC', '\xE1', /* 80 */
241 '\xE0', '\xE2', '\xE4', '\xE3', '\xE5', '\xE7', '\xE9', '\xE8', /* 88 */
242 '\xEA', '\xEB', '\xED', '\xEC', '\xEE', '\xEF', '\xF1', '\xF3', /* 90 */
243 '\xF2', '\xF4', '\xF6', '\xF5', '\xFA', '\xF9', '\xFB', '\xFC', /* 98 */
244 '\xDD', '\xB0', '\xA2', '\xA3', '\xA7', ' ', '\xB6', '\xDF', /* A0 */
245 '\xAE', '\xA9', ' ', '\xB4', '\xA8', ' ', '\xC6', '\xD8', /* A8 */
246 ' ', '\xB1', '\xBE', ' ', '\xA5', '\xB5', ' ', ' ', /* B0 */
247 '\xBD', '\xBC', ' ', '\xAA', '\xBA', ' ', '\xE6', '\xF8', /* B8 */
248 '\xBF', '\xA1', '\xAC', ' ', ' ', ' ', ' ', '\xAB', /* C0 */
249 '\xBB', ' ', '\xA0', '\xC0', '\xC3', '\xD5', ' ', '\xA6', /* C8 */
250 '\xAD', ' ', '"', '"', '\'', '\'', '\xF7', '\xD7', /* D0 */
251 '\xFF', ' ', ' ', '\xA4', '\xD0', '\xF0', '\xDE', '\xFE', /* D8 */
252 '\xFD', '\xB7', ' ', ' ', ' ', '\xC2', '\xCA', '\xC1', /* E0 */
253 '\xCB', '\xC8', '\xCD', '\xCE', '\xCF', '\xCC', '\xD3', '\xD4', /* E8 */
254 ' ', '\xD2', '\xDA', '\xDB', '\xD9', ' ', ' ', ' ', /* F0 */
255 '\xAF', ' ', ' ', ' ', '\xB8', ' ', ' ', ' ', /* F8 */