Debian Codegraph @

texk/ttf2pk2/ligkern.c

raw
1 /*
2 * ligkern.c
3 *
4 * This file is part of the ttf2pk package.
5 *
6 * Copyright 1997-1999, 2000 by
7 * Frederic Loyer <[email protected]>
8 * Werner Lemberg <[email protected]>
9 */
10
11 #ifdef HAVE_CONFIG_H
12 #include <config.h>
13 #endif
14
15 #include <string.h>
16 #include <stdlib.h>
17 #include <stddef.h> /* for size_t */
18 #include <ctype.h>
19
20 #include "ttf2tfm.h"
21 #include "ligkern.h"
22 #include "ttfenc.h"
23 #include "texenc.h"
24 #include "newobj.h"
25 #include "errormsg.h"
26
27
28 static char *
29 paramstring(char **curp)
30 {
31 register char *p, *q;
32
33
34 p = *curp;
35 while (*p && !isspace((unsigned char)*p))
36 p++;
37 q = *curp;
38 if (*p != '\0')
39 *p++ = '\0';
40 while (isspace((unsigned char)*p))
41 p++;
42 *curp = p;
43 return q;
44 }
45
46
47 /*
48 * Some routines to remove kerns that match certain patterns.
49 */
50
51 static kern *
52 rmkernmatch(kern *k,
53 char *s)
54 {
55 kern *nk;
56
57
58 while (k && strcmp(k->succ, s) == 0)
59 k = k->next;
60
61 if (k)
62 {
63 for (nk = k; nk; nk = nk->next)
64 while (nk->next && strcmp(nk->next->succ, s) == 0)
65 nk->next = nk->next->next;
66 }
67 return k;
68 }
69
70
71 /*
72 * Recursive to one level.
73 */
74
75 static void
76 rmkern(char *s1, char *s2,
77 ttfinfo *ti,
78 Font *fnt)
79 {
80 if (ti == NULL)
81 {
82 if (strcmp(s1, "*") == 0)
83 {
84 for (ti = fnt->charlist; ti; ti = ti->next)
85 rmkern(s1, s2, ti, fnt);
86 return;
87 }
88 else
89 {
90 ti = findadobe(s1, fnt->charlist);
91 if (ti == NULL)
92 return;
93 }
94 }
95
96 if (strcmp(s2, "*") == 0)
97 ti->kerns = NULL; /* drop them on the floor */
98 else
99 ti->kerns = rmkernmatch(ti->kerns, s2);
100 }
101
102
103 /*
104 * Make the kerning for character S1 equivalent to that for S2.
105 * If either S1 or S2 do not exist, do nothing.
106 * If S1 already has kerning, do nothing.
107 */
108
109 static void
110 addkern(char *s1, char *s2,
111 Font *fnt)
112 {
113 ttfinfo *ti1 = findadobe(s1, fnt->charlist);
114 ttfinfo *ti2 = findadobe(s2, fnt->charlist);
115
116
117 if (ti1 && ti2 && !ti1->kerns)
118 {
119 /* Put the new one at the head of the list, since order is immaterial. */
120 ttfptr *ap = (ttfptr *)mymalloc(sizeof (ttfptr));
121
122
123 ap->next = ti2->kern_equivs;
124 ap->ch = ti1;
125 ti2->kern_equivs = ap;
126 }
127 }
128
129
130 /*
131 * Reads a ligkern line, if this is one. Assumes the first character
132 * passed is `%'.
133 */
134
135 void
136 checkligkern(char *s, Font *fnt)
137 {
138 char *mlist[5];
139 char *os;
140 char *orig_s, *pos;
141 size_t offset[5];
142 int n;
143
144
145 os = newstring(s);
146 orig_s = s;
147
148 s++;
149 while (isspace((unsigned char)*s))
150 s++;
151 if (strncmp(s, "LIGKERN", 7) == 0)
152 {
153 fnt->sawligkern = True;
154 s += 7;
155 while (isspace((unsigned char)*s))
156 s++;
157 pos = s;
158 while (*pos)
159 {
160 for (n = 0; n < 5;)
161 {
162 if (*pos == '\0')
163 break;
164 offset[n] = pos - orig_s;
165 mlist[n] = paramstring(&pos);
166 if (strcmp(mlist[n], ";") == 0)
167 break;
168 n++;
169 }
170
171 if (n > 4)
172 boops(os, pos - orig_s, "Too many parameters in lig kern data.");
173 if (n < 3)
174 boops(os, pos - orig_s, "Too few parameters in lig kern data.");
175
176 if (n == 3 && strcmp(mlist[1], "{}") == 0) /* rmkern command */
177 rmkern(mlist[0], mlist[2], (ttfinfo *)0, fnt);
178 else if (n == 3 && strcmp(mlist[1], "<>") == 0) /* addkern */
179 addkern(mlist[0], mlist[2], fnt);
180 else if (n == 3 && strcmp(mlist[0], "||") == 0 &&
181 strcmp(mlist[1], "=") == 0) /* bc command */
182 {
183 ttfinfo *ti = findadobe("||", fnt->charlist);
184
185
186 if (fnt->boundarychar != -1)
187 boops(os, offset[0], "Multiple boundary character commands?");
188 if (sscanf(mlist[2], "%d", &n) != 1)
189 boops(os, offset[2],
190 "Expected number assignment for boundary char.");
191 if (n < 0 || n > 0xFF)
192 boops(os, offset[2], "Boundary character number must be 0..0xFF.");
193
194 fnt->boundarychar = n;
195 if (ti == NULL)
196 oops("Internal error: boundary char.");
197 ti->outcode = n; /* prime the pump, so to speak, for lig/kerns */
198 }
199 else if (n == 4)
200 {
201 int op = -1;
202 ttfinfo *ti;
203
204
205 for (n = 0; encligops[n]; n++)
206 if (strcmp(mlist[2], encligops[n]) == 0)
207 {
208 op = n;
209 break;
210 }
211 if (op < 0)
212 boops(os, offset[2], "Bad ligature op specified.");
213
214 if (NULL != (ti = findadobe(mlist[0], fnt->charlist)))
215 {
216 lig *lig;
217
218
219 if (findadobe(mlist[1], fnt->charlist))
220 /* remove coincident kerns */
221 rmkern(mlist[0], mlist[1], ti, fnt);
222
223 if (strcmp(mlist[3], "||") == 0)
224 boops(os, offset[3], "You can't lig to the boundary character!");
225
226 if (!fnt->fixedpitch) /* fixed pitch fonts get *0* ligs */
227 {
228 for (lig = ti->ligs; lig; lig = lig->next)
229 if (strcmp(lig->succ, mlist[1]) == 0)
230 break; /* we'll re-use this structure */
231
232 if (lig == NULL)
233 {
234 lig = newlig();
235 lig->succ = newstring(mlist[1]);
236 lig->next = ti->ligs;
237 ti->ligs = lig;
238 }
239 lig->sub = newstring(mlist[3]);
240 lig->op = op;
241
242 if (strcmp(mlist[1], "||") == 0)
243 {
244 lig->boundleft = 1;
245 if (strcmp(mlist[0], "||") == 0)
246 boops(os, offset[0],
247 "You can't lig boundarychar boundarychar!");
248 }
249 else
250 lig->boundleft = 0;
251 }
252 }
253 }
254 else
255 boops(os, offset[0], "Bad form in LIGKERN command.");
256 }
257 }
258
259 free(os);
260 }
261
262
263 void
264 getligkerndefaults(Font *fnt)
265 {
266 int i;
267 char *buffer;
268
269
270 for (i = 0; staticligkern[i]; i++)
271 {
272 buffer = newstring(staticligkern[i]);
273 checkligkern(buffer, fnt);
274 free(buffer);
275 }
276 }
277
278
279 /* end */