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