otf: use CFF bbox for all glyphs
[neatmkfn.git] / trfn.c
blob89baaa87067fddb53cbade29cb0b2e880359cb4d
1 #include <ctype.h>
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <string.h>
5 #include "sbuf.h"
6 #include "tab.h"
7 #include "trfn.h"
8 #include "trfn_agl.h"
9 #include "trfn_ch.h"
11 #define WX(w) (((w) < 0 ? (w) - trfn_div / 2 : (w) + trfn_div / 2) / trfn_div)
12 #define LEN(a) ((sizeof(a) / sizeof((a)[0])))
13 #define HEXDIGS "0123456789ABCDEF"
14 #define NCHAR 8 /* number of characters per glyph */
15 #define GNLEN 64 /* glyph name length */
16 #define AGLLEN 8192 /* adobe glyphlist length */
18 static struct sbuf *sbuf_char; /* characters */
19 static struct sbuf *sbuf_kern; /* kerning pairs */
20 static int trfn_div; /* divisor of widths */
21 static int trfn_swid; /* space width */
22 static int trfn_special; /* special flag */
23 static int trfn_kmin; /* minimum kerning value */
24 static int trfn_bbox; /* include bounding box */
25 static int trfn_noligs; /* suppress ligatures */
26 static int trfn_pos; /* include glyph positions */
27 static char trfn_ligs[8192]; /* font ligatures */
28 static char trfn_ligs2[8192]; /* font ligatures, whose length is two */
29 static char trfn_trname[256]; /* font troff name */
30 static char trfn_psname[256]; /* font ps name */
31 /* character type */
32 static int trfn_asc; /* minimum height of glyphs with ascender */
33 static int trfn_desc; /* minimum depth of glyphs with descender */
35 /* lookup tables */
36 static struct tab *tab_agl; /* adobe glyph list table */
37 static struct tab *tab_alts; /* character aliases table */
39 static int utf8len(int c)
41 if (c > 0 && c <= 0x7f)
42 return 1;
43 if (c >= 0xfc)
44 return 6;
45 if (c >= 0xf8)
46 return 5;
47 if (c >= 0xf0)
48 return 4;
49 if (c >= 0xe0)
50 return 3;
51 if (c >= 0xc0)
52 return 2;
53 return c != 0;
56 static int utf8get(char **src)
58 int result;
59 int l = 1;
60 char *s = *src;
61 if (~((unsigned char) **src) & 0xc0)
62 return (unsigned char) *(*src)++;
63 while (l < 6 && (unsigned char) *s & (0x40 >> l))
64 l++;
65 result = (0x3f >> l) & (unsigned char) *s++;
66 while (l--)
67 result = (result << 6) | ((unsigned char) *s++ & 0x3f);
68 *src = s;
69 return result;
72 static void utf8put(char **d, int c)
74 int l = 0;
75 if (c > 0xffff) {
76 *(*d)++ = 0xf0 | (c >> 18);
77 l = 3;
78 } else if (c > 0x7ff) {
79 *(*d)++ = 0xe0 | (c >> 12);
80 l = 2;
81 } else if (c > 0x7f) {
82 *(*d)++ = 0xc0 | (c >> 6);
83 l = 1;
84 } else {
85 *(*d)++ = c > 0 ? c : ' ';
87 while (l--)
88 *(*d)++ = 0x80 | ((c >> (l * 6)) & 0x3f);
89 **d = '\0';
92 static int hexval(char *s, int len)
94 char *digs = HEXDIGS;
95 int n = 0;
96 int i;
97 for (i = 0; i < len; i++) {
98 if (s[i] && strchr(digs, s[i]))
99 n = n * 16 + (strchr(digs, s[i]) - digs);
100 else
101 break;
103 return i < 4 ? -1 : n;
106 static int agl_map(char *d, char *s)
108 char *u = tab_get(tab_agl, s); /* unicode code point like "FB8E" */
109 if (!u)
110 return 1;
111 while (u && *u) {
112 while (*u == ' ')
113 u++;
114 utf8put(&d, hexval(u, 6));
115 u = strchr(u, ' ');
117 *d = '\0';
118 return 0;
121 static int achar_map(char *name)
123 int i;
124 for (i = 0; i < LEN(achars); i++) {
125 struct achar *a = &achars[i];
126 if (!strncmp(a->name, name, strlen(a->name))) {
127 char *postfix = name + strlen(a->name);
128 if (!*postfix)
129 return a->c;
130 if (!strcmp("isolated", postfix))
131 return a->s ? a->s : a->c;
132 if (!strcmp("initial", postfix))
133 return a->i ? a->i : a->c;
134 if (!strcmp("medial", postfix))
135 return a->m ? a->m : a->c;
136 if (!strcmp("final", postfix))
137 return a->f ? a->f : a->c;
140 return 0;
143 static int achar_shape(int c, int pjoin, int njoin)
145 int i;
146 for (i = 0; i < LEN(achars); i++) {
147 struct achar *a = &achars[i];
148 if (a->c == c) {
149 if (!pjoin && !njoin)
150 return a->c;
151 if (!pjoin && njoin)
152 return a->i ? a->i : a->c;
153 if (pjoin && njoin)
154 return a->m ? a->m : a->c;
155 if (pjoin && !njoin)
156 return a->f ? a->f : a->c;
159 return c;
162 static void ashape(char *str, char *ext)
164 int s[NCHAR];
165 char *src = str;
166 int i, l;
167 int bjoin = !strcmp(".medi", ext) || !strcmp(".fina", ext);
168 int ejoin = !strcmp(".medi", ext) || !strcmp(".init", ext);
169 for (l = 0; l < NCHAR && *src; l++)
170 s[l] = utf8get(&src);
171 for (i = 0; i < l; i++)
172 s[i] = achar_shape(s[i], i > 0 || bjoin, i < l - 1 || ejoin);
173 for (i = 0; i < l; i++)
174 utf8put(&str, s[i]);
177 /* find the utf-8 name of src with the given unicode codepoint */
178 static int trfn_name(char *dst, char *src, int codepoint)
180 char ch[GNLEN];
181 char *d = dst;
182 char *s;
183 int i;
184 if (codepoint) {
185 utf8put(&dst, codepoint);
186 return 0;
188 if (!src || src[0] == '.')
189 return 1;
190 while (*src && *src != '.') {
191 s = ch;
192 if (src[0] == '_')
193 src++;
194 while (*src && *src != '_' && *src != '.')
195 *s++ = *src++;
196 *s = '\0';
197 if (!agl_map(d, ch)) {
198 d = strchr(d, '\0');
199 } else if (ch[0] == 'u' && ch[1] == 'n' &&
200 ch[2] == 'i' && hexval(ch + 3, 4) > 0) {
201 for (i = 0; strlen(ch + 3 + 4 * i) >= 4; i++)
202 utf8put(&d, hexval(ch + 3 + 4 * i, 4));
203 } else if (ch[0] == 'u' && hexval(ch + 1, 4) > 0) {
204 utf8put(&d, hexval(ch + 1, 6));
205 } else if (achar_map(ch)) {
206 utf8put(&d, achar_map(ch));
207 } else {
208 return 1;
211 ashape(dst, src);
212 return *src && strcmp(src, ".medi") && strcmp(src, ".fina") &&
213 strcmp(src, ".init") && strcmp(src, ".isol");
216 static void trfn_aglexceptions(char *dst)
218 int i;
219 for (i = 0; i < LEN(agl_exceptions); i++)
220 if (!strcmp(agl_exceptions[i][0], dst))
221 strcpy(dst, agl_exceptions[i][1]);
224 static void trfn_ligput(char *c)
226 char *dst = strlen(c) == 2 ? trfn_ligs2 : trfn_ligs;
227 sprintf(strchr(dst, '\0'), "%s ", c);
230 static void trfn_lig(char *c)
232 int i;
233 for (i = 0; i < LEN(agl_exceptions); i++)
234 if (!strcmp(agl_exceptions[i][1], c))
235 return;
236 if (c[0] && c[1] && strlen(c) > utf8len((unsigned char) c[0])) {
237 trfn_ligput(c);
238 } else {
239 for (i = 0; i < LEN(ligs_utf8); i++)
240 if (!strcmp(ligs_utf8[i][0], c))
241 trfn_ligput(ligs_utf8[i][1]);
245 static int trfn_type(char *s, int lly, int ury)
247 int typ = 0;
248 int c = !s[0] || s[1] ? 0 : (unsigned char) *s;
249 if (c == 't' && !trfn_asc)
250 trfn_asc = ury;
251 if ((c == 'g' || c == 'j' || c == 'p' || c == 'q' || c == 'y') &&
252 (!trfn_desc || trfn_desc < lly))
253 trfn_desc = lly;
254 if (!trfn_desc || !trfn_asc) {
255 if (c > 0 && c < 128)
256 return ctype_ascii[c];
257 return 3;
259 if (!trfn_desc || lly <= trfn_desc)
260 typ |= 1;
261 if (!trfn_asc || ury >= trfn_asc)
262 typ |= 2;
263 return typ;
266 /* n is the position and u is the unicode codepoint */
267 void trfn_char(char *psname, int n, int u, int wid,
268 int llx, int lly, int urx, int ury)
270 char uc[GNLEN]; /* mapping unicode character */
271 char **a_tr; /* troff character names */
272 char pos[GNLEN] = ""; /* postscript character position/name */
273 int typ; /* character type */
274 /* initializing character attributes */
275 if (trfn_name(uc, psname, u))
276 strcpy(uc, "---");
277 trfn_aglexceptions(uc);
278 if (trfn_pos && n >= 0 && n < 256)
279 sprintf(pos, "%d", n);
280 if (trfn_pos && n < 0 && !uc[1] && uc[0] >= 32 && uc[0] <= 125)
281 if (!strchr(psname, '.'))
282 sprintf(pos, "%d", uc[0]);
283 typ = trfn_type(!strchr(psname, '.') ? uc : "", lly, ury);
284 if (!trfn_swid && (!strcmp(" ", uc) || !strcmp(" ", uc)))
285 trfn_swid = WX(wid);
286 /* printing troff charset */
287 if (strchr(uc, ' ')) /* space not allowed in char names */
288 return;
289 if (strcmp("---", uc))
290 trfn_lig(uc);
291 sbuf_printf(sbuf_char, "char %s\t%d", uc, WX(wid));
292 if (trfn_bbox && (llx || lly || urx || ury))
293 sbuf_printf(sbuf_char, ",%d,%d,%d,%d",
294 WX(llx), WX(lly), WX(urx), WX(ury));
295 sbuf_printf(sbuf_char, "\t%d\t%s\t%s\n", typ, psname, pos);
296 a_tr = tab_get(tab_alts, uc);
297 while (a_tr && *a_tr)
298 sbuf_printf(sbuf_char, "char %s\t\"\n", *a_tr++);
301 void trfn_kern(char *c1, char *c2, int x)
303 if (WX(x) && abs(WX(x)) >= trfn_kmin)
304 sbuf_printf(sbuf_kern, "kern %s\t%s\t%d\n", c1, c2, WX(x));
307 void trfn_trfont(char *name)
309 if (!trfn_trname[0])
310 strcpy(trfn_trname, name);
313 void trfn_psfont(char *name)
315 if (!trfn_psname[0])
316 strcpy(trfn_psname, name);
319 void trfn_print(void)
321 if (trfn_trname[0])
322 printf("name %s\n", trfn_trname);
323 if (trfn_psname[0])
324 printf("fontname %s\n", trfn_psname);
325 printf("spacewidth %d\n", trfn_swid);
326 if (!trfn_noligs)
327 printf("ligatures %s%s0\n", trfn_ligs, trfn_ligs2);
328 if (trfn_special)
329 printf("special\n");
330 fputs(sbuf_buf(sbuf_char), stdout);
331 fputs(sbuf_buf(sbuf_kern), stdout);
334 void trfn_init(int res, int spc, int kmin, int bbox, int ligs, int pos)
336 int i;
337 trfn_div = 7200 / res;
338 trfn_special = spc;
339 trfn_kmin = kmin;
340 trfn_bbox = bbox;
341 trfn_noligs = !ligs;
342 trfn_pos = pos;
343 sbuf_char = sbuf_make();
344 sbuf_kern = sbuf_make();
345 tab_agl = tab_alloc(LEN(agl));
346 for (i = 0; i < LEN(agl); i++)
347 tab_put(tab_agl, agl[i][0], agl[i][1]);
348 tab_alts = tab_alloc(LEN(alts));
349 for (i = 0; i < LEN(alts); i++)
350 tab_put(tab_alts, alts[i][0], alts[i] + 1);
353 void trfn_done(void)
355 sbuf_free(sbuf_char);
356 sbuf_free(sbuf_kern);
357 tab_free(tab_alts);
358 if (tab_agl)
359 tab_free(tab_agl);