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