EPS/SVG/PDF code ... more comments and better font finder including removal of hardc...
[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 static const char *last_unknown_font = NULL;
39
40 #define is_font(p, name) \
41   (!strcmp(p->postscript_name, name) || !strcmp(p->fullname, name))
42
43 static const afm_fontinfo *afm_searchfont(const char *name)
44 {
45   int i;
46   const afm_fontinfo *p = afm_last_used_font;
47   if (p && is_font(p, name))
48     return p;
49   p = afm_fontinfolist;
50   for (i = 0; i < afm_fontinfo_count; i++, p++) {
51     if (is_font(p, name)) {
52       afm_last_used_font = p;
53       return p;
54     }
55   }
56   return NULL;
57 }
58
59
60 /* returns always a font, never NULL.
61    The rest of the code depends on the result never being NULL.
62    See rrd_afm.h */
63 static const afm_fontinfo *afm_findfont(const char *name)
64 {
65   const afm_fontinfo *p = afm_searchfont(name);
66   if (p)
67     return p;
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;
71   }
72   p = afm_searchfont(RRD_AFM_DEFAULT_FONT);
73   if (p)
74     return p;
75   return afm_fontinfolist; // anything, just anything.
76 }
77
78 const char *afm_get_font_postscript_name(const char* font)
79 {
80   const afm_fontinfo *p = afm_findfont(font);
81   return p->postscript_name;
82 }
83
84 const char *afm_get_font_name(const char* font)
85 {
86   const afm_fontinfo *p = afm_findfont(font);
87   return p->fullname;
88 }
89
90 double afm_get_ascender(const char* font, double size)
91 {
92   const afm_fontinfo *p = afm_findfont(font);
93   return size * p->ascender / 1000.0;
94 }
95
96 double afm_get_descender(const char* font, double size)
97 {
98   const afm_fontinfo *p = afm_findfont(font);
99   return size * p->descender / 1000.0;
100 }
101
102 static int afm_find_char_index(const afm_fontinfo *fontinfo,
103     afm_cunicode ch1)
104 {
105   int idx = ch1 - 32;
106   afm_cuint16 *indexP;
107   int numIndexChars, i;
108   if (idx <= 0)
109     return 0;
110   if (idx <= 126 - 32)
111     return idx;
112   indexP = fontinfo->highchars_index;
113   if (indexP == 0)
114     return 0;
115   numIndexChars = fontinfo->highchars_count;
116   DLOG((stderr, " find highbit, num = %d\n", numIndexChars));
117   if (ch1 >= 161 && ch1 <= 255) {
118     idx = ch1 - 161;
119     DLOG((stderr, "  161, idx = %d -> %d\n", idx, indexP[idx]));
120     if (idx < numIndexChars && indexP[idx] == ch1) {
121       idx += 127 - 32;
122       DLOG((stderr, "  161-guessed ok to %d\n", idx));
123       return idx;
124     }
125   }
126   for (i = 0; i < numIndexChars; i++) {
127     DLOG((stderr, "    compares to %d -> %d\n", indexP[i], i));
128     if (indexP[i] == ch1)
129       return i + 127 - 32;
130   }
131   DLOG((stderr, "Did not find %d in highchars_index ??\n", ch1));
132   return 0;
133 }
134
135 #if ENABLE_LIGATURES
136 static afm_cunicode afm_find_combined_ligature(const afm_fontinfo *fontinfo,
137     afm_cunicode ch1, afm_cunicode ch2)
138 {
139   afm_cunicode *p = fontinfo->ligatures;
140   int num = fontinfo->ligatures_count;
141   int i;
142   if (!num)
143     return 0;
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"));
150       return p[2];
151     }
152   }
153   return 0;
154 }
155 #endif
156
157 #define READ_ESCAPED(p, val) \
158   if ((val = *p++) == 0) { \
159     val = 254 + *p++; \
160   } else if (!--val) { \
161     val = *p++ << 8; \
162     val |= *p++; \
163   }
164
165
166 static long afm_find_kern(const afm_fontinfo *fontinfo,
167     int kern_idx, afm_cunicode ch2)
168 {
169   afm_cuint8 *p8 = fontinfo->kerning_data + kern_idx;
170   int num;
171   READ_ESCAPED(p8, num);
172   DLOG((stderr, " find kern, num pairs = %d\n", num));
173   while (num > 0) {
174     afm_unicode ch;
175     READ_ESCAPED(p8, ch);
176     DLOG((stderr, "     pair-char = %d\n", ch));
177     if (ch == ch2) {
178       DLOG((stderr, " got kern = %d\n", *(afm_csint8*)p8));
179       return *(afm_csint8*)p8;
180     }
181     p8++;
182     num--;
183   }
184   return 0;
185 }
186
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)
190 {
191   const afm_fontinfo *fontinfo = afm_findfont(font);
192   long width = 0;
193   double widthf;
194   const unsigned char *up = (const unsigned char*)text;
195   DLOG((stderr, "================= %s\n", text));
196   if (fontinfo == NULL)
197     return size * strlen(text);
198   while (1) {
199     afm_unicode ch1, ch2;
200     int idx1, kern_idx;
201     if ((ch1 = *up) == 0)
202       break;
203     ch1 = afm_host2unicode(ch1); /* unsafe macro */
204     ch2 = *++up;
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));
211 #if ENABLE_LIGATURES
212     if (ch2) {
213       int ch1_new = afm_find_combined_ligature(fontinfo, ch1, ch2);
214       DLOG((stderr, "  lig-ch = %d\n", ch1_new));
215       if (ch1_new) {
216         ch1 = 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 : ' '));
221       }
222     }
223 #endif
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));
229       if (kern_idx > 0)
230         width += afm_find_kern(fontinfo, kern_idx, ch2);
231     }
232   }
233   widthf = (width * 6 / 1000.0) * size;
234   DLOG((stderr, "Returns %ld (%ld) -> %f\n", width, width * 6, widthf));
235   return widthf;
236 }
237
238 #ifdef __APPLE__
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 */
256 };
257 #endif