* switch to courier if the font is unknown
[rrdtool.git] / src / rrd_afm.c
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  ****************************************************************************/
6
7 #if defined(WIN32) && !defined(__CYGWIN__) && !defined(__CYGWIN32__)
8 #include "../confignt/config.h"
9 #else
10 #include "config.h"
11 #endif
12
13 #include "rrd_afm.h"
14 #include "rrd_afm_data.h"
15
16 #include <stdlib.h>
17 #include <stdio.h>
18
19 #ifdef HAVE_STRING_H
20 #include <string.h>
21 #endif
22
23 #if 0
24 # define DEBUG 1
25 # define DLOG(x) fprintf x
26 #else
27 # define DEBUG 0
28 # define DLOG(x) 
29 #endif
30
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.
34 */
35 #define ENABLE_LIGATURES 0
36
37 static const afm_fontinfo *afm_last_used_font = NULL;
38
39 #define is_font(p, name) \
40   (!strcmp(p->postscript_name, name) || !strcmp(p->fullname, name))
41
42 static const afm_fontinfo *afm_searchfont(const char *name)
43 {
44   int i;
45   const afm_fontinfo *p = afm_last_used_font;
46   if (p && is_font(p, name))
47     return p;
48   p = afm_fontinfolist;
49   for (i = 0; i < afm_fontinfo_count; i++, p++) {
50     if (is_font(p, name)) {
51       afm_last_used_font = p;
52       return p;
53     }
54   }
55   return NULL;
56 }
57
58 static const char *last_unknown_font = NULL;
59
60 static const afm_fontinfo *afm_findfont(const char *name)
61 {
62   const afm_fontinfo *p = afm_searchfont(name);
63   if (p)
64     return p;
65   if (!last_unknown_font || strcmp(name, last_unknown_font)) {
66           fprintf(stderr, "Can't find font '%s'\n", name);
67           last_unknown_font = name;
68   }
69   p = afm_searchfont("Courier");
70   if (p)
71     return p;
72   return afm_fontinfolist; // anything, just anything.
73 }
74
75 const char *afm_get_font_postscript_name(const char* font)
76 {
77   const afm_fontinfo *p = afm_findfont(font);
78   return p->postscript_name;
79 }
80
81 double afm_get_ascender(const char* font, double size)
82 {
83   const afm_fontinfo *p = afm_findfont(font);
84   return size * p->ascender / 1000.0;
85 }
86
87 double afm_get_descender(const char* font, double size)
88 {
89   const afm_fontinfo *p = afm_findfont(font);
90   return size * p->descender / 1000.0;
91 }
92
93 static int afm_find_char_index(const afm_fontinfo *fontinfo,
94     afm_cunicode ch1)
95 {
96   int idx = ch1 - 32;
97   afm_cuint16 *indexP;
98   int numIndexChars, i;
99   if (idx <= 0)
100     return 0;
101   if (idx <= 126 - 32)
102     return idx;
103   indexP = fontinfo->highchars_index;
104   if (indexP == 0)
105     return 0;
106   numIndexChars = fontinfo->highchars_count;
107   DLOG((stderr, " find highbit, num = %d\n", numIndexChars));
108   if (ch1 >= 161 && ch1 <= 255) {
109     idx = ch1 - 161;
110     DLOG((stderr, "  161, idx = %d -> %d\n", idx, indexP[idx]));
111     if (idx < numIndexChars && indexP[idx] == ch1) {
112       idx += 127 - 32;
113       DLOG((stderr, "  161-guessed ok to %d\n", idx));
114       return idx;
115     }
116   }
117   for (i = 0; i < numIndexChars; i++) {
118     DLOG((stderr, "    compares to %d -> %d\n", indexP[i], i));
119     if (indexP[i] == ch1)
120       return i + 127 - 32;
121   }
122   DLOG((stderr, "Did not find %d in highchars_index ??\n", ch1));
123   return 0;
124 }
125
126 #if ENABLE_LIGATURES
127 static afm_cunicode afm_find_combined_ligature(const afm_fontinfo *fontinfo,
128     afm_cunicode ch1, afm_cunicode ch2)
129 {
130   afm_cunicode *p = fontinfo->ligatures;
131   int num = fontinfo->ligatures_count;
132   int i;
133   if (!num)
134     return 0;
135   DLOG((stderr, " find-lig, num = %d\n", num));
136   for (i = 0; i < num; i++, p += 3) {
137     DLOG((stderr, "    lig: %d + %d -> %d (%c %c %c)\n",
138         p[0], p[1], p[2], p[0], p[1], p[2]));
139     if (ch1 == *p && ch2 == p[1]) {
140       DLOG((stderr, "   matches.\n"));
141       return p[2];
142     }
143   }
144   return 0;
145 }
146 #endif
147
148 #define READ_ESCAPED(p, val) \
149   if ((val = *p++) == 0) { \
150     val = 254 + *p++; \
151   } else if (!--val) { \
152     val = *p++ << 8; \
153     val |= *p++; \
154   }
155
156
157 static long afm_find_kern(const afm_fontinfo *fontinfo,
158     int kern_idx, afm_cunicode ch2)
159 {
160   afm_cuint8 *p8 = fontinfo->kerning_data + kern_idx;
161   int num;
162   READ_ESCAPED(p8, num);
163   DLOG((stderr, " find kern, num pairs = %d\n", num));
164   while (num > 0) {
165     afm_unicode ch;
166     READ_ESCAPED(p8, ch);
167     DLOG((stderr, "     pair-char = %d\n", ch));
168     if (ch == ch2) {
169       DLOG((stderr, " got kern = %d\n", *(afm_csint8*)p8));
170       return *(afm_csint8*)p8;
171     }
172     p8++;
173     num--;
174   }
175   return 0;
176 }
177
178 /* measure width of a text string */
179 double afm_get_text_width ( double start, const char* font, double size,
180           double tabwidth, const char* text)
181 {
182   const afm_fontinfo *fontinfo = afm_findfont(font);
183   long width = 0;
184   double widthf;
185   const unsigned char *up = (const unsigned char*)text;
186   DLOG((stderr, "================= %s\n", text));
187   if (fontinfo == NULL)
188     return size * strlen(text);
189   while (1) {
190     afm_unicode ch1, ch2;
191     int idx1, kern_idx;
192     if ((ch1 = *up) == 0)
193       break;
194     ch1 = afm_host2unicode(ch1); /* unsafe macro */
195     ch2 = *++up;
196     ch2 = afm_host2unicode(ch2); /* unsafe macro */
197     DLOG((stderr, "------------- Loop: %d + %d (%c%c)   at %d\n",
198           ch1, ch2, ch1, ch2 ? ch2 : ' ',
199           (up - (const unsigned char*)text) - 1));
200     idx1 = afm_find_char_index(fontinfo, ch1);
201     DLOG((stderr, "  idx1 = %d\n", idx1));
202 #if ENABLE_LIGATURES
203     if (ch2) {
204       int ch1_new = afm_find_combined_ligature(fontinfo, ch1, ch2);
205       DLOG((stderr, "  lig-ch = %d\n", ch1_new));
206       if (ch1_new) {
207         ch1 = ch1_new;
208         idx1 = afm_find_char_index(fontinfo, ch1);
209         ch2 = afm_host2unicode(*++up);
210         DLOG((stderr, "  -> idx1 = %d, ch2 = %d (%c)\n", 
211             idx1, ch2, ch2 ? ch2 : ' '));
212       }
213     }
214 #endif
215     width += fontinfo->widths[idx1];
216     DLOG((stderr, "Plain width of %d = %d\n", ch1, fontinfo->widths[idx1]));
217     if (fontinfo->kerning_index && ch2) {
218       kern_idx = fontinfo->kerning_index[idx1];
219       DLOG((stderr, "    kern_idx = %d\n", kern_idx));
220       if (kern_idx > 0)
221         width += afm_find_kern(fontinfo, kern_idx, ch2);
222     }
223   }
224   widthf = (width * 6 / 1000.0) * size;
225   DLOG((stderr, "Returns %ld (%ld) -> %f\n", width, width * 6, widthf));
226   return widthf;
227 }
228
229 #ifdef __APPLE__
230 const unsigned char afm_mac2iso[128] = {
231   '\xC4', '\xC5', '\xC7', '\xC9', '\xD1', '\xD6', '\xDC', '\xE1', /* 80 */
232   '\xE0', '\xE2', '\xE4', '\xE3', '\xE5', '\xE7', '\xE9', '\xE8', /* 88 */
233   '\xEA', '\xEB', '\xED', '\xEC', '\xEE', '\xEF', '\xF1', '\xF3', /* 90 */
234   '\xF2', '\xF4', '\xF6', '\xF5', '\xFA', '\xF9', '\xFB', '\xFC', /* 98 */
235   '\xDD', '\xB0', '\xA2', '\xA3', '\xA7', ' ',    '\xB6', '\xDF', /* A0 */
236   '\xAE', '\xA9', ' ',    '\xB4', '\xA8', ' ',    '\xC6', '\xD8', /* A8 */
237   ' ',    '\xB1', '\xBE', ' ',    '\xA5', '\xB5', ' ',    ' ',    /* B0 */
238   '\xBD', '\xBC', ' ',    '\xAA', '\xBA', ' ',    '\xE6', '\xF8', /* B8 */
239   '\xBF', '\xA1', '\xAC', ' ',    ' ',    ' ',    ' ',    '\xAB', /* C0 */
240   '\xBB', ' ',    '\xA0', '\xC0', '\xC3', '\xD5', ' ',    '\xA6', /* C8 */
241   '\xAD', ' ',    '"',    '"',    '\'',   '\'',   '\xF7', '\xD7', /* D0 */
242   '\xFF', ' ',    ' ',    '\xA4', '\xD0', '\xF0', '\xDE', '\xFE', /* D8 */
243   '\xFD', '\xB7', ' ',    ' ',    ' ',    '\xC2', '\xCA', '\xC1', /* E0 */
244   '\xCB', '\xC8', '\xCD', '\xCE', '\xCF', '\xCC', '\xD3', '\xD4', /* E8 */
245   ' ',    '\xD2', '\xDA', '\xDB', '\xD9', ' ',    ' ',    ' ',    /* F0 */
246   '\xAF', ' ',    ' ',    ' ',    '\xB8', ' ',    ' ',    ' ',    /* F8 */
247 };
248 #endif