10 #define WX(w) (((w) < 0 ? (w) - trfn_div / 2 : (w) + trfn_div / 2) / trfn_div)
11 #define LEN(a) ((sizeof(a) / sizeof((a)[0])))
12 #define HEXDIGS "0123456789abcdef"
13 #define NCHAR 8 /* number of characters per glyph */
14 #define GNLEN 64 /* glyph name length */
15 #define AGLLEN 8192 /* adobe glyphlist length */
17 static struct sbuf sbuf_char
; /* characters */
18 static struct sbuf sbuf_kern
; /* kerning pairs */
19 static int trfn_div
; /* divisor of widths */
20 static int trfn_swid
; /* space width */
21 static int trfn_special
; /* special flag */
22 static int trfn_kmin
; /* minimum kerning value */
23 static int trfn_bbox
; /* include bounding box */
24 static char trfn_ligs
[8192]; /* font ligatures */
25 static char trfn_trname
[256]; /* font troff name */
26 static char trfn_psname
[256]; /* font ps name */
28 static int trfn_asc
; /* minimum height of glyphs with ascender */
29 static int trfn_desc
; /* minimum depth of glyphs with descender */
31 /* adobe glyphlist mapping */
32 static char agl_key
[AGLLEN
][GNLEN
];
33 static char agl_val
[AGLLEN
][GNLEN
];
37 static struct tab
*tab_agl
;
38 static struct tab
*tab_alts
;
40 static int utf8len(int c
)
42 if (c
> 0 && c
<= 0x7f)
57 static int utf8get(char **src
)
62 if (~((unsigned char) **src
) & 0xc0)
63 return (unsigned char) *(*src
)++;
64 while (l
< 6 && (unsigned char) *s
& (0x40 >> l
))
66 result
= (0x3f >> l
) & (unsigned char) *s
++;
68 result
= (result
<< 6) | ((unsigned char) *s
++ & 0x3f);
73 static void utf8put(char **d
, int c
)
77 *(*d
)++ = 0xf0 | (c
>> 18);
79 } else if (c
> 0x7ff) {
80 *(*d
)++ = 0xe0 | (c
>> 12);
82 } else if (c
> 0x7f) {
83 *(*d
)++ = 0xc0 | (c
>> 6);
86 *(*d
)++ = c
> 0 ? c
: ' ';
90 *(*d
)++ = 0x80 | ((c
>> (l
* 6)) & 0x3f);
94 static int hexval(char *s
, int len
)
99 for (i
= 0; i
< len
; i
++) {
100 if (s
[i
] && strchr(digs
, tolower(s
[i
])))
101 n
= n
* 16 + (strchr(digs
, tolower(s
[i
])) - digs
);
105 return len
== 1 ? n
<< 4 : n
;
108 static int agl_read(char *path
)
110 FILE *fin
= fopen(path
, "r");
117 while (fgets(ln
, sizeof(ln
), fin
)) {
119 if (ln
[0] == '#' || !s
)
126 utf8put(&d
, hexval(s
, 6));
130 strcpy(agl_key
[agl_n
], ln
);
131 strcpy(agl_val
[agl_n
], val
);
135 tab_agl
= tab_alloc(agl_n
);
136 for (i
= 0; i
< agl_n
; i
++)
137 tab_put(tab_agl
, agl_key
[i
], agl_val
[i
]);
141 static char *agl_map(char *s
)
143 return tab_get(tab_agl
, s
);
146 static int achar_map(char *name
)
149 for (i
= 0; i
< LEN(achars
); i
++) {
150 struct achar
*a
= &achars
[i
];
151 if (!strncmp(a
->name
, name
, strlen(a
->name
))) {
152 char *postfix
= name
+ strlen(a
->name
);
155 if (!strcmp("isolated", postfix
))
156 return a
->s
? a
->s
: a
->c
;
157 if (!strcmp("initial", postfix
))
158 return a
->i
? a
->i
: a
->c
;
159 if (!strcmp("medial", postfix
))
160 return a
->m
? a
->m
: a
->c
;
161 if (!strcmp("final", postfix
))
162 return a
->f
? a
->f
: a
->c
;
168 static int achar_shape(int c
, int pjoin
, int njoin
)
171 for (i
= 0; i
< LEN(achars
); i
++) {
172 struct achar
*a
= &achars
[i
];
174 if (!pjoin
&& !njoin
)
177 return a
->i
? a
->i
: a
->c
;
179 return a
->m
? a
->m
: a
->c
;
181 return a
->f
? a
->f
: a
->c
;
187 static void ashape(char *str
, char *ext
)
192 int bjoin
= !strcmp(".medi", ext
) || !strcmp(".fina", ext
);
193 int ejoin
= !strcmp(".medi", ext
) || !strcmp(".init", ext
);
194 for (l
= 0; l
< NCHAR
&& *src
; l
++)
195 s
[l
] = utf8get(&src
);
196 for (i
= 0; i
< l
; i
++)
197 s
[i
] = achar_shape(s
[i
], i
> 0 || bjoin
, i
< l
- 1 || ejoin
);
198 for (i
= 0; i
< l
; i
++)
202 /* find the utf-8 name of src with the given unicode codepoint */
203 static int trfn_name(char *dst
, char *src
, int codepoint
)
210 utf8put(&dst
, codepoint
);
213 if (!src
|| src
[0] == '.')
215 while (*src
&& *src
!= '.') {
219 while (*src
&& *src
!= '_' && *src
!= '.')
223 strcpy(d
, agl_map(ch
));
224 for (i
= 0; i
< LEN(agl_exceptions
); i
++) {
225 if (!strcmp(agl_exceptions
[i
][0], d
)) {
226 strcpy(d
, agl_exceptions
[i
][1]);
231 } else if (ch
[0] == 'u' && ch
[1] == 'n' && ch
[2] == 'i') {
232 for (i
= 0; strlen(ch
+ 3 + 4 * i
) >= 4; i
++)
233 utf8put(&d
, hexval(ch
+ 3 + 4 * i
, 4));
234 } else if (ch
[0] == 'u' && ch
[1] && strchr(HEXDIGS
, tolower(ch
[1]))) {
235 utf8put(&d
, hexval(ch
+ 1, 6));
236 } else if (achar_map(ch
)) {
237 utf8put(&d
, achar_map(ch
));
243 return src
&& strcmp(src
, ".medi") && strcmp(src
, ".fina") &&
244 strcmp(src
, ".init") && strcmp(src
, ".isol");
247 static void trfn_lig(char *c
)
250 for (i
= 0; i
< LEN(ligs_exceptions
); i
++)
251 if (!strcmp(ligs_exceptions
[i
], c
))
253 if (c
[0] && c
[1] && strlen(c
) > utf8len((unsigned char) c
[0])) {
254 sprintf(strchr(trfn_ligs
, '\0'), "%s ", c
);
256 for (i
= 0; i
< LEN(ligs_utf8
); i
++)
257 if (!strcmp(ligs_utf8
[i
][0], c
))
258 sprintf(strchr(trfn_ligs
, '\0'),
259 "%s ", ligs_utf8
[i
][1]);
263 static int trfn_type(char *s
, int lly
, int ury
)
266 int c
= !s
[0] || s
[1] ? 0 : (unsigned char) *s
;
267 if (c
== 't' && !trfn_asc
)
269 if ((c
== 'g' || c
== 'j' || c
== 'p' || c
== 'q' || c
== 'y') &&
270 (!trfn_desc
|| trfn_desc
< lly
))
272 if (!trfn_desc
|| !trfn_asc
) {
273 if (c
> 0 && c
< 128)
274 return ctype_ascii
[c
];
277 if (!trfn_desc
|| lly
<= trfn_desc
)
279 if (!trfn_asc
|| ury
>= trfn_asc
)
284 /* n is the position and u is the unicode codepoint */
285 void trfn_char(char *psname
, int n
, int u
, int wid
,
286 int llx
, int lly
, int urx
, int ury
)
288 char uc
[GNLEN
]; /* mapping unicode character */
289 char **a_tr
; /* troff character names */
290 char pos
[GNLEN
] = ""; /* postscript character position/name */
291 int typ
; /* character type */
292 /* initializing character attributes */
293 if (trfn_name(uc
, psname
, u
))
295 if (n
>= 0 && n
< 256)
296 sprintf(pos
, "%d", n
);
297 if (n
< 0 && !uc
[1] && uc
[0] >= 32 && uc
[0] <= 125)
298 if (!strchr(psname
, '.'))
299 sprintf(pos
, "%d", uc
[0]);
300 typ
= trfn_type(!strchr(psname
, '.') ? uc
: "", lly
, ury
);
301 /* printing troff charset */
302 if (strchr(uc
, ' ')) { /* space not allowed in char names */
303 if (!trfn_swid
&& !strcmp(" ", uc
))
307 if (strcmp("---", uc
))
309 sbuf_printf(&sbuf_char
, "char %s\t%d", uc
, WX(wid
));
310 if (trfn_bbox
&& (llx
|| lly
|| urx
|| ury
))
311 sbuf_printf(&sbuf_char
, ",%d,%d,%d,%d",
312 WX(llx
), WX(lly
), WX(urx
), WX(ury
));
313 sbuf_printf(&sbuf_char
, "\t%d\t%s\t%s\n", typ
, psname
, pos
);
314 a_tr
= tab_get(tab_alts
, uc
);
315 while (a_tr
&& *a_tr
)
316 sbuf_printf(&sbuf_char
, "char %s\t\"\n", *a_tr
++);
319 void trfn_kern(char *c1
, char *c2
, int x
)
321 if (WX(x
) && abs(WX(x
)) >= trfn_kmin
)
322 sbuf_printf(&sbuf_kern
, "kern %s\t%s\t%d\n", c1
, c2
, WX(x
));
325 void trfn_trfont(char *name
)
328 strcpy(trfn_trname
, name
);
331 void trfn_psfont(char *name
)
334 strcpy(trfn_psname
, name
);
337 void trfn_print(void)
340 printf("name %s\n", trfn_trname
);
342 printf("fontname %s\n", trfn_psname
);
343 printf("spacewidth %d\n", trfn_swid
);
344 printf("ligatures %s0\n", trfn_ligs
);
347 printf("%s", sbuf_buf(&sbuf_char
));
348 printf("%s", sbuf_buf(&sbuf_kern
));
351 void trfn_init(int res
, int spc
, int kmin
, int bbox
)
354 trfn_div
= 7200 / res
;
358 if (agl_read("glyphlist.txt"))
359 fprintf(stderr
, "mktrfn: could not open glyphlist.txt\n");
360 sbuf_init(&sbuf_char
);
361 sbuf_init(&sbuf_kern
);
362 tab_alts
= tab_alloc(LEN(alts
));
363 for (i
= 0; i
< LEN(alts
); i
++)
364 tab_put(tab_alts
, alts
[i
][0], alts
[i
] + 1);
369 sbuf_done(&sbuf_char
);
370 sbuf_done(&sbuf_kern
);