Debian Codegraph @

texk/web2c/luatexdir/tex/textcodes.c

raw
1 /*
2
3 textcodes.w
4
5 Copyright 2006-2010 Taco Hoekwater <taco@@luatex.org>
6
7 This file is part of LuaTeX.
8
9 LuaTeX is free software; you can redistribute it and/or modify it under
10 the terms of the GNU General Public License as published by the Free
11 Software Foundation; either version 2 of the License, or (at your
12 option) any later version.
13
14 LuaTeX is distributed in the hope that it will be useful, but WITHOUT
15 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
17 License for more details.
18
19 You should have received a copy of the GNU General Public License along
20 with LuaTeX; if not, see <http://www.gnu.org/licenses/>.
21
22 */
23
24 #include "ptexlib.h"
25
26 /*tex
27
28 Contrary to traditional \TEX\ we have catcode tables so that we can switch
29 catcode regimes very fast. We can have many such regimes and they're stored in
30 trees.
31
32 */
33
34 #define CATCODESTACK 8
35 #define CATCODEDEFAULT 12
36 #define CATCODE_MAX 32767
37
38 static sa_tree *catcode_heads = NULL;
39 static int catcode_max = 0;
40 static unsigned char *catcode_valid = NULL;
41
42 void set_cat_code(int h, int n, halfword v, quarterword gl)
43 {
44 sa_tree_item sa_value = { 0 };
45 sa_tree s = catcode_heads[h];
46 if (h > catcode_max)
47 catcode_max = h;
48 if (s == NULL) {
49 sa_value.int_value = CATCODEDEFAULT;
50 s = new_sa_tree(CATCODESTACK, 1, sa_value);
51 catcode_heads[h] = s;
52 }
53 sa_value.int_value = (int) v;
54 set_sa_item(s, n, sa_value, gl);
55 }
56
57 halfword get_cat_code(int h, int n)
58 {
59 sa_tree_item sa_value = { 0 };
60 sa_tree s = catcode_heads[h];
61 if (h > catcode_max)
62 catcode_max = h;
63 if (s == NULL) {
64 sa_value.int_value = CATCODEDEFAULT;
65 s = new_sa_tree(CATCODESTACK, 1, sa_value);
66 catcode_heads[h] = s;
67 }
68 return (halfword) get_sa_item(s, n).int_value;
69 }
70
71 void unsave_cat_codes(int h, quarterword gl)
72 {
73 int k;
74 if (h > catcode_max)
75 catcode_max = h;
76 for (k = 0; k <= catcode_max; k++) {
77 if (catcode_heads[k] != NULL)
78 restore_sa_stack(catcode_heads[k], gl);
79 }
80 }
81
82 static void initializecatcodes(void)
83 {
84 sa_tree_item sa_value = { 0 };
85 catcode_max = 0;
86 catcode_heads = Mxmalloc_array(sa_tree, (CATCODE_MAX + 1));
87 catcode_valid = Mxmalloc_array(unsigned char, (CATCODE_MAX + 1));
88 memset(catcode_heads, 0, sizeof(sa_tree) * (CATCODE_MAX + 1));
89 memset(catcode_valid, 0, sizeof(unsigned char) * (CATCODE_MAX + 1));
90 catcode_valid[0] = 1;
91 sa_value.int_value = CATCODEDEFAULT;
92 catcode_heads[0] = new_sa_tree(CATCODESTACK, 1, sa_value);
93 }
94
95 static void dumpcatcodes(void)
96 {
97 int total = 0;
98 int k;
99 for (k = 0; k <= catcode_max; k++) {
100 if (catcode_valid[k]) {
101 total++;
102 }
103 }
104 dump_int(catcode_max);
105 dump_int(total);
106 for (k = 0; k <= catcode_max; k++) {
107 if (catcode_valid[k]) {
108 dump_int(k);
109 dump_sa_tree(catcode_heads[k],"catcodes");
110 }
111 }
112 }
113
114 static void undumpcatcodes(void)
115 {
116 int total, k, x;
117 xfree(catcode_heads);
118 xfree(catcode_valid);
119 catcode_heads = Mxmalloc_array(sa_tree, (CATCODE_MAX + 1));
120 catcode_valid = Mxmalloc_array(unsigned char, (CATCODE_MAX + 1));
121 memset(catcode_heads, 0, sizeof(sa_tree) * (CATCODE_MAX + 1));
122 memset(catcode_valid, 0, sizeof(unsigned char) * (CATCODE_MAX + 1));
123 undump_int(catcode_max);
124 undump_int(total);
125 for (k = 0; k < total; k++) {
126 undump_int(x);
127 catcode_heads[x] = undump_sa_tree("catcodes");
128 catcode_valid[x] = 1;
129 }
130 }
131
132 int valid_catcode_table(int h)
133 {
134 if (h <= CATCODE_MAX && h >= 0 && catcode_valid[h]) {
135 return 1;
136 }
137 return 0;
138 }
139
140 void copy_cat_codes(int from, int to)
141 {
142 if (from < 0 || from > CATCODE_MAX || catcode_valid[from] == 0) {
143 uexit(1);
144 }
145 if (to > catcode_max)
146 catcode_max = to;
147 destroy_sa_tree(catcode_heads[to]);
148 catcode_heads[to] = copy_sa_tree(catcode_heads[from]);
149 catcode_valid[to] = 1;
150 }
151
152 void initex_cat_codes(int h)
153 {
154 int k;
155 if (h > catcode_max)
156 catcode_max = h;
157 destroy_sa_tree(catcode_heads[h]);
158 catcode_heads[h] = NULL;
159 set_cat_code(h, '\r', car_ret_cmd, 1);
160 set_cat_code(h, ' ', spacer_cmd, 1);
161 set_cat_code(h, '\\', escape_cmd, 1);
162 set_cat_code(h, '%', comment_cmd, 1);
163 set_cat_code(h, 127, invalid_char_cmd, 1);
164 set_cat_code(h, 0, ignore_cmd, 1);
165 set_cat_code(h, 0xFEFF, ignore_cmd, 1);
166 for (k = 'A'; k <= 'Z'; k++) {
167 set_cat_code(h, k, letter_cmd, 1);
168 set_cat_code(h, k + 'a' - 'A', letter_cmd, 1);
169 }
170 catcode_valid[h] = 1;
171 }
172
173 static void freecatcodes(void)
174 {
175 int k;
176 for (k = 0; k <= catcode_max; k++) {
177 if (catcode_valid[k]) {
178 destroy_sa_tree(catcode_heads[k]);
179 }
180 }
181 xfree(catcode_heads);
182 xfree(catcode_valid);
183 }
184
185 /*tex
186
187 The lowercase mapping codes are also stored in a tree.
188
189 */
190
191 #define LCCODESTACK 8
192 #define LCCODEDEFAULT 0
193
194 static sa_tree lccode_head = NULL;
195
196 void set_lc_code(int n, halfword v, quarterword gl)
197 {
198 sa_tree_item sa_value = { 0 };
199 sa_value.int_value = (int) v;
200 set_sa_item(lccode_head, n, sa_value, gl);
201 }
202
203 halfword get_lc_code(int n)
204 {
205 return (halfword) get_sa_item(lccode_head, n).int_value;
206 }
207
208 static void unsavelccodes(quarterword gl)
209 {
210 restore_sa_stack(lccode_head, gl);
211 }
212
213 static void initializelccodes(void)
214 {
215 sa_tree_item sa_value = { 0 };
216 sa_value.int_value = LCCODEDEFAULT;
217 lccode_head = new_sa_tree(LCCODESTACK, 1, sa_value);
218 }
219
220 static void dumplccodes(void)
221 {
222 dump_sa_tree(lccode_head,"lccodes");
223 }
224
225 static void undumplccodes(void)
226 {
227 lccode_head = undump_sa_tree("lccodes");
228 }
229
230 static void freelccodes(void)
231 {
232 destroy_sa_tree(lccode_head);
233 }
234
235 /*tex
236
237 And the uppercase mapping codes are again stored in a tree.
238
239 */
240
241 #define UCCODESTACK 8
242 #define UCCODEDEFAULT 0
243
244 static sa_tree uccode_head = NULL;
245
246 void set_uc_code(int n, halfword v, quarterword gl)
247 {
248 sa_tree_item sa_value = { 0 };
249 sa_value.int_value = (int) v;
250 set_sa_item(uccode_head, n, sa_value, gl);
251 }
252
253 halfword get_uc_code(int n)
254 {
255 return (halfword) get_sa_item(uccode_head, n).int_value;
256 }
257
258 static void unsaveuccodes(quarterword gl)
259 {
260 restore_sa_stack(uccode_head, gl);
261 }
262
263 static void initializeuccodes(void)
264 {
265 sa_tree_item sa_value = { 0 };
266 sa_value.int_value = UCCODEDEFAULT;
267 uccode_head = new_sa_tree(UCCODESTACK, 1, sa_value);
268 }
269
270 static void dumpuccodes(void)
271 {
272 dump_sa_tree(uccode_head,"uccodes");
273 }
274
275 static void undumpuccodes(void)
276 {
277 uccode_head = undump_sa_tree("uccodes");
278 }
279
280 static void freeuccodes(void)
281 {
282 destroy_sa_tree(uccode_head);
283 }
284
285 /*tex
286
287 By now it will be no surprise that the space factors get stored in a tree.
288
289 */
290
291 #define SFCODESTACK 8
292 #define SFCODEDEFAULT 1000
293
294 static sa_tree sfcode_head = NULL;
295
296 void set_sf_code(int n, halfword v, quarterword gl)
297 {
298 sa_tree_item sa_value = { 0 };
299 sa_value.int_value = (int) v;
300 set_sa_item(sfcode_head, n, sa_value, gl);
301 }
302
303 halfword get_sf_code(int n)
304 {
305 return (halfword) get_sa_item(sfcode_head, n).int_value;
306 }
307
308 static void unsavesfcodes(quarterword gl)
309 {
310 restore_sa_stack(sfcode_head, gl);
311 }
312
313 static void initializesfcodes(void)
314 {
315 sa_tree_item sa_value = { 0 };
316 sa_value.int_value = SFCODEDEFAULT;
317 sfcode_head = new_sa_tree(SFCODESTACK, 1, sa_value);
318 }
319
320 static void dumpsfcodes(void)
321 {
322 dump_sa_tree(sfcode_head,"sfcodes");
323 }
324
325 static void undumpsfcodes(void)
326 {
327 sfcode_head = undump_sa_tree("sfcodes");
328 }
329
330 static void freesfcodes(void)
331 {
332 destroy_sa_tree(sfcode_head);
333 }
334
335 /*tex
336
337 The hyphenation codes are indeed stored in a tree and are used instead of
338 lowercase codes when deciding what characters to take into acccount when
339 hyphenating. They are bound to upto |HJCODE_MAX| languages.
340
341 */
342
343 #define HJCODESTACK 8
344 #define HJCODEDEFAULT 0
345 #define HJCODE_MAX 16383
346
347 static sa_tree *hjcode_heads = NULL;
348 static int hjcode_max = 0;
349 static unsigned char *hjcode_valid = NULL;
350
351 /*tex
352
353 Here we set codes but we don't initialize from lccodes.
354
355 */
356
357 void set_hj_code(int h, int n, halfword v, quarterword gl)
358 {
359 sa_tree_item sa_value = { 0 };
360 sa_tree s = hjcode_heads[h];
361 if (h > hjcode_max)
362 hjcode_max = h;
363 if (s == NULL) {
364 sa_value.int_value = HJCODEDEFAULT;
365 s = new_sa_tree(HJCODESTACK, 1, sa_value);
366 hjcode_heads[h] = s;
367 }
368 sa_value.int_value = (int) v;
369 set_sa_item(s, n, sa_value, gl);
370 }
371
372 /*tex
373
374 We just return the lccodes when nothing is set.
375
376 */
377
378 halfword get_hj_code(int h, int n)
379 {
380 sa_tree s = hjcode_heads[h];
381 if (s == NULL) {
382 s = lccode_head;
383 }
384 return (halfword) get_sa_item(s, n).int_value;
385 }
386
387 /*tex
388
389 We don't restore as we can have more languages in a paragraph and hyphenation
390 takes place at a later stage so we would get weird grouping side effects .. so,
391 one can overload settings but management is then upto the used, so no:
392
393 */
394
395 /*
396 static void unsavehjcodes(quarterword gl) { }
397 */
398
399 static void initializehjcodes(void)
400 {
401 /*
402 sa_tree_item sa_value = { 0 };
403 */
404 hjcode_max = 0;
405 hjcode_heads = Mxmalloc_array(sa_tree, (HJCODE_MAX + 1));
406 hjcode_valid = Mxmalloc_array(unsigned char, (HJCODE_MAX + 1));
407 memset(hjcode_heads, 0, sizeof(sa_tree) * (HJCODE_MAX + 1));
408 memset(hjcode_valid, 0, sizeof(unsigned char) * (HJCODE_MAX + 1));
409 /*
410 hjcode_valid[0] = 1;
411 sa_value.int_value = HJCODEDEFAULT;
412 hjcode_heads[0] = new_sa_tree(HJCODESTACK, 1, sa_value);
413 */
414 }
415
416 void hj_codes_from_lc_codes(int h)
417 {
418 sa_tree_item sa_value = { 0 };
419 sa_tree s = hjcode_heads[h];
420 if (s != NULL) {
421 destroy_sa_tree(s);
422 } else if (h > hjcode_max) {
423 hjcode_max = h;
424 }
425 if (s == NULL) {
426 sa_value.int_value = HJCODEDEFAULT;
427 s = new_sa_tree(HJCODESTACK, 1, sa_value);
428 hjcode_heads[h] = s;
429 }
430 hjcode_heads[h] = copy_sa_tree(lccode_head);
431 hjcode_valid[h] = 1;
432 }
433
434 static void dumphjcodes(void)
435 {
436 int total = 0;
437 int k;
438 for (k = 0; k <= hjcode_max; k++) {
439 if (hjcode_valid[k]) {
440 total++;
441 }
442 }
443 dump_int(hjcode_max);
444 dump_int(total);
445 for (k = 0; k <= hjcode_max; k++) {
446 if (hjcode_valid[k]) {
447 dump_int(k);
448 dump_sa_tree(hjcode_heads[k],"hjcodes");
449 }
450 }
451 }
452
453 static void undumphjcodes(void)
454 {
455 int total, k, x;
456 xfree(hjcode_heads);
457 xfree(hjcode_valid);
458 hjcode_heads = Mxmalloc_array(sa_tree, (HJCODE_MAX + 1));
459 hjcode_valid = Mxmalloc_array(unsigned char, (HJCODE_MAX + 1));
460 memset(hjcode_heads, 0, sizeof(sa_tree) * (HJCODE_MAX + 1));
461 memset(hjcode_valid, 0, sizeof(unsigned char) * (HJCODE_MAX + 1));
462 undump_int(hjcode_max);
463 undump_int(total);
464 for (k = 0; k < total; k++) {
465 undump_int(x);
466 hjcode_heads[x] = undump_sa_tree("hjcodes");
467 hjcode_valid[x] = 1;
468 }
469 }
470
471 static void freehjcodes(void)
472 {
473 int k;
474 for (k = 0; k <= hjcode_max; k++) {
475 if (hjcode_valid[k]) {
476 destroy_sa_tree(hjcode_heads[k]);
477 }
478 }
479 xfree(hjcode_heads);
480 xfree(hjcode_valid);
481 }
482
483 /*tex
484
485 The public management functions.
486
487 */
488
489 void unsave_text_codes(quarterword grouplevel)
490 {
491 unsavelccodes(grouplevel);
492 unsaveuccodes(grouplevel);
493 unsavesfcodes(grouplevel);
494 }
495
496 void initialize_text_codes(void)
497 {
498 initializecatcodes();
499 initializelccodes();
500 initializeuccodes();
501 initializesfcodes();
502 initializehjcodes();
503 }
504
505 void free_text_codes(void)
506 {
507 freecatcodes();
508 freelccodes();
509 freeuccodes();
510 freesfcodes();
511 freehjcodes();
512 }
513
514 void dump_text_codes(void)
515 {
516 dumpcatcodes();
517 dumplccodes();
518 dumpuccodes();
519 dumpsfcodes();
520 dumphjcodes();
521 }
522
523 void undump_text_codes(void)
524 {
525 undumpcatcodes();
526 undumplccodes();
527 undumpuccodes();
528 undumpsfcodes();
529 undumphjcodes();
530 }