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