trfn: check for agl_exceptions[] later in trfn_name()
[neatmkfn.git] / trfn.c
blob046aff16e727c9454e59e2e9598f0c952087bdf7
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 char trfn_ligs[8192]; /* font ligatures */
27 static char trfn_trname[256]; /* font troff name */
28 static char trfn_psname[256]; /* font ps name */
29 /* character type */
30 static int trfn_asc; /* minimum height of glyphs with ascender */
31 static int trfn_desc; /* minimum depth of glyphs with descender */
33 /* lookup tables */
34 static struct tab *tab_agl; /* adobe glyph list table */
35 static struct tab *tab_alts; /* character aliases table */
37 static int utf8len(int c)
39 if (c > 0 && c <= 0x7f)
40 return 1;
41 if (c >= 0xfc)
42 return 6;
43 if (c >= 0xf8)
44 return 5;
45 if (c >= 0xf0)
46 return 4;
47 if (c >= 0xe0)
48 return 3;
49 if (c >= 0xc0)
50 return 2;
51 return c != 0;
54 static int utf8get(char **src)
56 int result;
57 int l = 1;
58 char *s = *src;
59 if (~((unsigned char) **src) & 0xc0)
60 return (unsigned char) *(*src)++;
61 while (l < 6 && (unsigned char) *s & (0x40 >> l))
62 l++;
63 result = (0x3f >> l) & (unsigned char) *s++;
64 while (l--)
65 result = (result << 6) | ((unsigned char) *s++ & 0x3f);
66 *src = s;
67 return result;
70 static void utf8put(char **d, int c)
72 int l;
73 if (c > 0xffff) {
74 *(*d)++ = 0xf0 | (c >> 18);
75 l = 3;
76 } else if (c > 0x7ff) {
77 *(*d)++ = 0xe0 | (c >> 12);
78 l = 2;
79 } else if (c > 0x7f) {
80 *(*d)++ = 0xc0 | (c >> 6);
81 l = 1;
82 } else {
83 *(*d)++ = c > 0 ? c : ' ';
84 l = 0;
86 while (l--)
87 *(*d)++ = 0x80 | ((c >> (l * 6)) & 0x3f);
88 **d = '\0';
91 static int hexval(char *s, int len)
93 char *digs = HEXDIGS;
94 int n = 0;
95 int i;
96 for (i = 0; i < len; i++) {
97 if (s[i] && strchr(digs, s[i]))
98 n = n * 16 + (strchr(digs, s[i]) - digs);
99 else
100 break;
102 return i < 4 ? -1 : n;
105 static int agl_map(char *d, char *s)
107 char *u = tab_get(tab_agl, s); /* unicode code point like "FB8E" */
108 if (!u)
109 return 1;
110 while (u && *u) {
111 while (*u == ' ')
112 u++;
113 utf8put(&d, hexval(u, 6));
114 u = strchr(u, ' ');
116 *d = '\0';
117 return 0;
120 static int achar_map(char *name)
122 int i;
123 for (i = 0; i < LEN(achars); i++) {
124 struct achar *a = &achars[i];
125 if (!strncmp(a->name, name, strlen(a->name))) {
126 char *postfix = name + strlen(a->name);
127 if (!*postfix)
128 return a->c;
129 if (!strcmp("isolated", postfix))
130 return a->s ? a->s : a->c;
131 if (!strcmp("initial", postfix))
132 return a->i ? a->i : a->c;
133 if (!strcmp("medial", postfix))
134 return a->m ? a->m : a->c;
135 if (!strcmp("final", postfix))
136 return a->f ? a->f : a->c;
139 return 0;
142 static int achar_shape(int c, int pjoin, int njoin)
144 int i;
145 for (i = 0; i < LEN(achars); i++) {
146 struct achar *a = &achars[i];
147 if (a->c == c) {
148 if (!pjoin && !njoin)
149 return a->c;
150 if (!pjoin && njoin)
151 return a->i ? a->i : a->c;
152 if (pjoin && njoin)
153 return a->m ? a->m : a->c;
154 if (pjoin && !njoin)
155 return a->f ? a->f : a->c;
158 return c;
161 static void ashape(char *str, char *ext)
163 int s[NCHAR];
164 char *src = str;
165 int i, l;
166 int bjoin = !strcmp(".medi", ext) || !strcmp(".fina", ext);
167 int ejoin = !strcmp(".medi", ext) || !strcmp(".init", ext);
168 for (l = 0; l < NCHAR && *src; l++)
169 s[l] = utf8get(&src);
170 for (i = 0; i < l; i++)
171 s[i] = achar_shape(s[i], i > 0 || bjoin, i < l - 1 || ejoin);
172 for (i = 0; i < l; i++)
173 utf8put(&str, s[i]);
176 /* find the utf-8 name of src with the given unicode codepoint */
177 static int trfn_name(char *dst, char *src, int codepoint)
179 char ch[GNLEN];
180 char *d = dst;
181 char *s;
182 int i;
183 if (codepoint) {
184 utf8put(&dst, codepoint);
185 return 0;
187 if (!src || src[0] == '.')
188 return 1;
189 while (*src && *src != '.') {
190 s = ch;
191 if (src[0] == '_')
192 src++;
193 while (*src && *src != '_' && *src != '.')
194 *s++ = *src++;
195 *s = '\0';
196 if (!agl_map(d, ch)) {
197 d = strchr(d, '\0');
198 } else if (ch[0] == 'u' && ch[1] == 'n' &&
199 ch[2] == 'i' && hexval(ch + 3, 4) > 0) {
200 for (i = 0; strlen(ch + 3 + 4 * i) >= 4; i++)
201 utf8put(&d, hexval(ch + 3 + 4 * i, 4));
202 } else if (ch[0] == 'u' && hexval(ch + 1, 4) > 0) {
203 utf8put(&d, hexval(ch + 1, 6));
204 } else if (achar_map(ch)) {
205 utf8put(&d, achar_map(ch));
206 } else {
207 return 1;
210 for (i = 0; i < LEN(agl_exceptions); i++) {
211 if (!strcmp(agl_exceptions[i][0], dst)) {
212 strcpy(dst, agl_exceptions[i][1]);
213 break;
216 ashape(dst, src);
217 return *src && strcmp(src, ".medi") && strcmp(src, ".fina") &&
218 strcmp(src, ".init") && strcmp(src, ".isol");
221 static void trfn_lig(char *c)
223 int i;
224 for (i = 0; i < LEN(agl_exceptions); i++)
225 if (!strcmp(agl_exceptions[i][1], c))
226 return;
227 if (c[0] && c[1] && strlen(c) > utf8len((unsigned char) c[0])) {
228 sprintf(strchr(trfn_ligs, '\0'), "%s ", c);
229 } else {
230 for (i = 0; i < LEN(ligs_utf8); i++)
231 if (!strcmp(ligs_utf8[i][0], c))
232 sprintf(strchr(trfn_ligs, '\0'),
233 "%s ", ligs_utf8[i][1]);
237 static int trfn_type(char *s, int lly, int ury)
239 int typ = 0;
240 int c = !s[0] || s[1] ? 0 : (unsigned char) *s;
241 if (c == 't' && !trfn_asc)
242 trfn_asc = ury;
243 if ((c == 'g' || c == 'j' || c == 'p' || c == 'q' || c == 'y') &&
244 (!trfn_desc || trfn_desc < lly))
245 trfn_desc = lly;
246 if (!trfn_desc || !trfn_asc) {
247 if (c > 0 && c < 128)
248 return ctype_ascii[c];
249 return 3;
251 if (!trfn_desc || lly <= trfn_desc)
252 typ |= 1;
253 if (!trfn_asc || ury >= trfn_asc)
254 typ |= 2;
255 return typ;
258 /* n is the position and u is the unicode codepoint */
259 void trfn_char(char *psname, int n, int u, int wid,
260 int llx, int lly, int urx, int ury)
262 char uc[GNLEN]; /* mapping unicode character */
263 char **a_tr; /* troff character names */
264 char pos[GNLEN] = ""; /* postscript character position/name */
265 int typ; /* character type */
266 /* initializing character attributes */
267 if (trfn_name(uc, psname, u))
268 strcpy(uc, "---");
269 if (n >= 0 && n < 256)
270 sprintf(pos, "%d", n);
271 if (n < 0 && !uc[1] && uc[0] >= 32 && uc[0] <= 125)
272 if (!strchr(psname, '.'))
273 sprintf(pos, "%d", uc[0]);
274 typ = trfn_type(!strchr(psname, '.') ? uc : "", lly, ury);
275 /* printing troff charset */
276 if (strchr(uc, ' ')) { /* space not allowed in char names */
277 if (!trfn_swid && !strcmp(" ", uc))
278 trfn_swid = WX(wid);
279 return;
281 if (strcmp("---", uc))
282 trfn_lig(uc);
283 sbuf_printf(&sbuf_char, "char %s\t%d", uc, WX(wid));
284 if (trfn_bbox && (llx || lly || urx || ury))
285 sbuf_printf(&sbuf_char, ",%d,%d,%d,%d",
286 WX(llx), WX(lly), WX(urx), WX(ury));
287 sbuf_printf(&sbuf_char, "\t%d\t%s\t%s\n", typ, psname, pos);
288 a_tr = tab_get(tab_alts, uc);
289 while (a_tr && *a_tr)
290 sbuf_printf(&sbuf_char, "char %s\t\"\n", *a_tr++);
293 void trfn_kern(char *c1, char *c2, int x)
295 if (WX(x) && abs(WX(x)) >= trfn_kmin)
296 sbuf_printf(&sbuf_kern, "kern %s\t%s\t%d\n", c1, c2, WX(x));
299 void trfn_trfont(char *name)
301 if (!trfn_trname[0])
302 strcpy(trfn_trname, name);
305 void trfn_psfont(char *name)
307 if (!trfn_psname[0])
308 strcpy(trfn_psname, name);
311 void trfn_print(void)
313 if (trfn_trname[0])
314 printf("name %s\n", trfn_trname);
315 if (trfn_psname[0])
316 printf("fontname %s\n", trfn_psname);
317 printf("spacewidth %d\n", trfn_swid);
318 if (!trfn_noligs)
319 printf("ligatures %s0\n", trfn_ligs);
320 if (trfn_special)
321 printf("special\n");
322 printf("%s", sbuf_buf(&sbuf_char));
323 printf("%s", sbuf_buf(&sbuf_kern));
326 void trfn_init(int res, int spc, int kmin, int bbox, int ligs)
328 int i;
329 trfn_div = 7200 / res;
330 trfn_special = spc;
331 trfn_kmin = kmin;
332 trfn_bbox = bbox;
333 trfn_noligs = !ligs;
334 sbuf_init(&sbuf_char);
335 sbuf_init(&sbuf_kern);
336 tab_agl = tab_alloc(LEN(agl));
337 for (i = 0; i < LEN(agl); i++)
338 tab_put(tab_agl, agl[i][0], agl[i][1]);
339 tab_alts = tab_alloc(LEN(alts));
340 for (i = 0; i < LEN(alts); i++)
341 tab_put(tab_alts, alts[i][0], alts[i] + 1);
344 void trfn_done(void)
346 sbuf_done(&sbuf_char);
347 sbuf_done(&sbuf_kern);
348 tab_free(tab_alts);
349 if (tab_agl)
350 tab_free(tab_agl);