gen.sh: limit the number of scripts to include
[neatmkfn.git] / trfn.c
blob933d64ce5ab0c6844ee515bfc4a6be1f41002bf4
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_trname[256]; /* font troff name */
29 static char trfn_psname[256]; /* font ps name */
30 /* character type */
31 static int trfn_asc; /* minimum height of glyphs with ascender */
32 static int trfn_desc; /* minimum depth of glyphs with descender */
34 /* lookup tables */
35 static struct tab *tab_agl; /* adobe glyph list table */
36 static struct tab *tab_alts; /* character aliases table */
38 static int utf8len(int c)
40 if (c > 0 && c <= 0x7f)
41 return 1;
42 if (c >= 0xfc)
43 return 6;
44 if (c >= 0xf8)
45 return 5;
46 if (c >= 0xf0)
47 return 4;
48 if (c >= 0xe0)
49 return 3;
50 if (c >= 0xc0)
51 return 2;
52 return c != 0;
55 static int utf8get(char **src)
57 int result;
58 int l = 1;
59 char *s = *src;
60 if (~((unsigned char) **src) & 0xc0)
61 return (unsigned char) *(*src)++;
62 while (l < 6 && (unsigned char) *s & (0x40 >> l))
63 l++;
64 result = (0x3f >> l) & (unsigned char) *s++;
65 while (l--)
66 result = (result << 6) | ((unsigned char) *s++ & 0x3f);
67 *src = s;
68 return result;
71 static void utf8put(char **d, int c)
73 int l = 0;
74 if (c > 0xffff) {
75 *(*d)++ = 0xf0 | (c >> 18);
76 l = 3;
77 } else if (c > 0x7ff) {
78 *(*d)++ = 0xe0 | (c >> 12);
79 l = 2;
80 } else if (c > 0x7f) {
81 *(*d)++ = 0xc0 | (c >> 6);
82 l = 1;
83 } else {
84 *(*d)++ = c > 0 ? c : ' ';
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 ashape(dst, src);
211 return *src && strcmp(src, ".medi") && strcmp(src, ".fina") &&
212 strcmp(src, ".init") && strcmp(src, ".isol");
215 static void trfn_aglexceptions(char *dst)
217 int i;
218 for (i = 0; i < LEN(agl_exceptions); i++)
219 if (!strcmp(agl_exceptions[i][0], dst))
220 strcpy(dst, agl_exceptions[i][1]);
223 static void trfn_lig(char *c)
225 int i;
226 for (i = 0; i < LEN(agl_exceptions); i++)
227 if (!strcmp(agl_exceptions[i][1], c))
228 return;
229 if (c[0] && c[1] && strlen(c) > utf8len((unsigned char) c[0])) {
230 sprintf(strchr(trfn_ligs, '\0'), "%s ", c);
231 } else {
232 for (i = 0; i < LEN(ligs_utf8); i++)
233 if (!strcmp(ligs_utf8[i][0], c))
234 sprintf(strchr(trfn_ligs, '\0'),
235 "%s ", ligs_utf8[i][1]);
239 static int trfn_type(char *s, int lly, int ury)
241 int typ = 0;
242 int c = !s[0] || s[1] ? 0 : (unsigned char) *s;
243 if (c == 't' && !trfn_asc)
244 trfn_asc = ury;
245 if ((c == 'g' || c == 'j' || c == 'p' || c == 'q' || c == 'y') &&
246 (!trfn_desc || trfn_desc < lly))
247 trfn_desc = lly;
248 if (!trfn_desc || !trfn_asc) {
249 if (c > 0 && c < 128)
250 return ctype_ascii[c];
251 return 3;
253 if (!trfn_desc || lly <= trfn_desc)
254 typ |= 1;
255 if (!trfn_asc || ury >= trfn_asc)
256 typ |= 2;
257 return typ;
260 /* n is the position and u is the unicode codepoint */
261 void trfn_char(char *psname, int n, int u, int wid,
262 int llx, int lly, int urx, int ury)
264 char uc[GNLEN]; /* mapping unicode character */
265 char **a_tr; /* troff character names */
266 char pos[GNLEN] = ""; /* postscript character position/name */
267 int typ; /* character type */
268 /* initializing character attributes */
269 if (trfn_name(uc, psname, u))
270 strcpy(uc, "---");
271 trfn_aglexceptions(uc);
272 if (trfn_pos && n >= 0 && n < 256)
273 sprintf(pos, "%d", n);
274 if (trfn_pos && n < 0 && !uc[1] && uc[0] >= 32 && uc[0] <= 125)
275 if (!strchr(psname, '.'))
276 sprintf(pos, "%d", uc[0]);
277 typ = trfn_type(!strchr(psname, '.') ? uc : "", lly, ury);
278 if (!trfn_swid && (!strcmp(" ", uc) || !strcmp(" ", uc)))
279 trfn_swid = WX(wid);
280 /* printing troff charset */
281 if (strchr(uc, ' ')) /* space not allowed in char names */
282 return;
283 if (strcmp("---", uc))
284 trfn_lig(uc);
285 sbuf_printf(&sbuf_char, "char %s\t%d", uc, WX(wid));
286 if (trfn_bbox && (llx || lly || urx || ury))
287 sbuf_printf(&sbuf_char, ",%d,%d,%d,%d",
288 WX(llx), WX(lly), WX(urx), WX(ury));
289 sbuf_printf(&sbuf_char, "\t%d\t%s\t%s\n", typ, psname, pos);
290 a_tr = tab_get(tab_alts, uc);
291 while (a_tr && *a_tr)
292 sbuf_printf(&sbuf_char, "char %s\t\"\n", *a_tr++);
295 void trfn_kern(char *c1, char *c2, int x)
297 if (WX(x) && abs(WX(x)) >= trfn_kmin)
298 sbuf_printf(&sbuf_kern, "kern %s\t%s\t%d\n", c1, c2, WX(x));
301 void trfn_trfont(char *name)
303 if (!trfn_trname[0])
304 strcpy(trfn_trname, name);
307 void trfn_psfont(char *name)
309 if (!trfn_psname[0])
310 strcpy(trfn_psname, name);
313 void trfn_print(void)
315 if (trfn_trname[0])
316 printf("name %s\n", trfn_trname);
317 if (trfn_psname[0])
318 printf("fontname %s\n", trfn_psname);
319 printf("spacewidth %d\n", trfn_swid);
320 if (!trfn_noligs)
321 printf("ligatures %s0\n", trfn_ligs);
322 if (trfn_special)
323 printf("special\n");
324 printf("%s", sbuf_buf(&sbuf_char));
325 printf("%s", sbuf_buf(&sbuf_kern));
328 void trfn_init(int res, int spc, int kmin, int bbox, int ligs, int pos)
330 int i;
331 trfn_div = 7200 / res;
332 trfn_special = spc;
333 trfn_kmin = kmin;
334 trfn_bbox = bbox;
335 trfn_noligs = !ligs;
336 trfn_pos = pos;
337 sbuf_init(&sbuf_char);
338 sbuf_init(&sbuf_kern);
339 tab_agl = tab_alloc(LEN(agl));
340 for (i = 0; i < LEN(agl); i++)
341 tab_put(tab_agl, agl[i][0], agl[i][1]);
342 tab_alts = tab_alloc(LEN(alts));
343 for (i = 0; i < LEN(alts); i++)
344 tab_put(tab_alts, alts[i][0], alts[i] + 1);
347 void trfn_done(void)
349 sbuf_done(&sbuf_char);
350 sbuf_done(&sbuf_kern);
351 tab_free(tab_alts);
352 if (tab_agl)
353 tab_free(tab_agl);