Debian Codegraph @

texk/lcdf-typetools/lcdf-typetools-src/otftotfm/dvipsencoding.cc

raw
1 /* dvipsencoding.{cc,hh} -- store a DVIPS encoding
2 *
3 * Copyright (c) 2003-2019 Eddie Kohler
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the Free
7 * Software Foundation; either version 2 of the License, or (at your option)
8 * any later version. This program is distributed in the hope that it will be
9 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
11 * Public License for more details.
12 */
13
14 #ifdef HAVE_CONFIG_H
15 # include <config.h>
16 #endif
17 #include "dvipsencoding.hh"
18 #include "metrics.hh"
19 #include "secondary.hh"
20 #include <lcdf/error.hh>
21 #include <lcdf/straccum.hh>
22 #include <string.h>
23 #include <stdio.h>
24 #include <ctype.h>
25 #include <stdlib.h>
26 #include <algorithm>
27 #include "util.hh"
28
29 enum { GLYPHLIST_ALTERNATIVE = 0x40000000,
30 GLYPHLIST_USEMAP = GLYPHLIST_ALTERNATIVE,
31 U_EMPTYSLOT = 0xD801,
32 U_ALTSELECTOR = 0xD802 };
33 static HashMap<String, uint32_t> glyphlist((uint32_t) -1);
34 static Vector<uint32_t> glyphmap;
35 static PermString::Initializer perm_initializer;
36 PermString DvipsEncoding::dot_notdef(".notdef");
37
38 #define NEXT_GLYPH_NAME(gn) ("/" + (gn))
39
40 void
41 DvipsEncoding::add_glyphlist(String text)
42 {
43 const char *s = text.begin(), *end = text.end();
44 while (s != end) {
45 // move to first nonblank
46 while (s != end && isspace((unsigned char) *s))
47 ++s;
48 // ignore comments
49 if (s != end && *s == '#') {
50 skip_to_end_of_line:
51 while (s != end && *s != '\n' && *s != '\r')
52 ++s;
53 continue;
54 }
55 // parse glyph name
56 const char *name_start = s;
57 while (s != end && !isspace((unsigned char) *s) && *s != ';')
58 ++s;
59 if (s == name_start)
60 goto skip_to_end_of_line;
61 String glyph_name = text.substring(name_start, s).compact();
62 int map_pos = glyphmap.size();
63 // parse Unicodes
64 while (1) {
65 while (s != end && (*s == ' ' || *s == '\t'))
66 ++s;
67 if (s == end || *s == '\n' || *s == '\r' || *s == '#'
68 || (map_pos == glyphmap.size() && *s != ';' && *s != ','))
69 break;
70 if (*s == ';' || *s == ',') {
71 ++s;
72 while (s != end && (*s == ' ' || *s == '\t'))
73 ++s;
74 if (s == end || !isxdigit((unsigned char) *s))
75 goto skip_to_end_of_line;
76 if (map_pos != glyphmap.size())
77 glyphmap.push_back(GLYPHLIST_ALTERNATIVE);
78 }
79 uint32_t u = 0;
80 while (s != end && isxdigit((unsigned char) *s)) {
81 if (*s >= '0' && *s <= '9')
82 u = (u << 4) + *s - '0';
83 else if (*s >= 'A' && *s <= 'F')
84 u = (u << 4) + *s - 'A' + 10;
85 else
86 u = (u << 4) + *s - 'a' + 10;
87 ++s;
88 }
89 if (u == 0 || u > 0x10FFFF)
90 goto skip_to_end_of_line;
91 glyphmap.push_back(u);
92 if (s != end && !isspace((unsigned char) *s) && *s != ',' && *s != ';')
93 break;
94 }
95 // store result
96 if (map_pos == glyphmap.size() - 1) {
97 glyphlist.insert(glyph_name, glyphmap.back());
98 glyphmap.pop_back();
99 } else {
100 glyphlist.insert(glyph_name, map_pos | GLYPHLIST_USEMAP);
101 glyphmap.push_back(0);
102 }
103 goto skip_to_end_of_line;
104 }
105 }
106
107 static void
108 unicode_add_suffix(Vector<uint32_t> &prefix,
109 int prefix_starting_from,
110 const Vector<uint32_t> &suffix)
111 {
112 int prefix_size = prefix.size();
113 for (Vector<uint32_t>::const_iterator it = suffix.begin();
114 it != suffix.end() && *it != 0;
115 ++it)
116 if (*it == GLYPHLIST_ALTERNATIVE) {
117 prefix.push_back(*it);
118 for (int i = prefix_starting_from; i < prefix_size; ++i)
119 prefix.push_back(prefix[i]);
120 } else
121 prefix.push_back(*it);
122 }
123
124 bool
125 DvipsEncoding::glyphname_unicode(String gn, Vector<uint32_t> &unis)
126 {
127 int unis_first_size = unis.size();
128
129 // drop all characters to the right of the first dot
130 String::iterator dot = std::find(gn.begin(), gn.end(), '.');
131 if (dot > gn.begin() && dot < gn.end())
132 gn = gn.substring(gn.begin(), dot);
133
134 // map the first component, handle later components recursively
135 String::iterator underscore = std::find(gn.begin(), gn.end(), '_');
136 String component = gn.substring(gn.begin(), underscore);
137 int prefix_start = 0;
138 Vector<uint32_t> suffix;
139 if (String gn_suffix = gn.substring(underscore + 1, gn.end())) {
140 if (!glyphname_unicode(gn_suffix, suffix))
141 return false;
142 }
143
144 // check glyphlist
145 int value = glyphlist[component];
146 uint32_t uval;
147 if (value >= 0 && !(value & GLYPHLIST_USEMAP))
148 unis.push_back(value);
149 else if (value >= 0) {
150 for (int i = (value & ~GLYPHLIST_USEMAP);
151 glyphmap[i];
152 ++i)
153 if (glyphmap[i] == GLYPHLIST_ALTERNATIVE) {
154 unicode_add_suffix(unis, prefix_start, suffix);
155 unis.push_back(GLYPHLIST_ALTERNATIVE);
156 prefix_start = unis.size();
157 } else
158 unis.push_back(glyphmap[i]);
159 } else if (component.length() >= 7
160 && (component.length() % 4) == 3
161 && (memcmp(component.data(), "uni", 3) == 0
162 // 16.Aug.2008: Some texnansx.enc have incorrect "Uni"
163 // prefix, but we might as well understand it.
164 || memcmp(component.data(), "Uni", 3) == 0)) {
165 for (const char *s = component.begin() + 3;
166 s < component.end();
167 s += 4)
168 if (parse_unicode_number(s, s + 4, -1, uval))
169 unis.push_back(uval);
170 else {
171 unis.resize(unis_first_size);
172 return false;
173 }
174 } else if (component.length() >= 5
175 && component.length() <= 7
176 && component[0] == 'u'
177 && parse_unicode_number(component.begin() + 1, component.end(), -1, uval))
178 unis.push_back(uval);
179 else
180 return false;
181
182 unicode_add_suffix(unis, prefix_start, suffix);
183 return true;
184 }
185
186
187 DvipsEncoding::DvipsEncoding()
188 : _boundary_char(-1), _altselector_char(-1), _unicoding_map(-1),
189 _warn_missing(false)
190 {
191 }
192
193 void
194 DvipsEncoding::encode(int e, PermString what)
195 {
196 if (e >= _e.size())
197 _e.resize(e + 1, dot_notdef);
198 _e[e] = what;
199 }
200
201 int
202 DvipsEncoding::encoding_of(PermString what, bool encoding_required)
203 {
204 int slot = -1;
205 for (int i = 0; i < _e.size(); i++)
206 if (_e[i] == what) {
207 slot = i;
208 goto use_slot;
209 } else if (!_e[i] || _e[i] == dot_notdef)
210 slot = i;
211 if (what == "||")
212 return _boundary_char;
213 else if (!encoding_required || slot < 0)
214 return -1;
215 use_slot:
216 if (encoding_required) {
217 if (slot >= _encoding_required.size())
218 _encoding_required.resize(slot + 1, false);
219 _encoding_required[slot] = true;
220 this->encode(slot, what);
221 }
222 return slot;
223 }
224
225 static String
226 tokenize(const String &s, int &pos_in, int &line)
227 {
228 const char *data = s.data();
229 int len = s.length();
230 int pos = pos_in;
231 while (1) {
232 // skip whitespace
233 while (pos < len && isspace((unsigned char) data[pos])) {
234 if (data[pos] == '\n')
235 line++;
236 else if (data[pos] == '\r' && (pos + 1 == len || data[pos+1] != '\n'))
237 line++;
238 pos++;
239 }
240
241 if (pos >= len) {
242 pos_in = len;
243 return String();
244 } else if (data[pos] == '%') {
245 for (pos++; pos < len && data[pos] != '\n' && data[pos] != '\r'; pos++)
246 /* nada */;
247 } else if (data[pos] == '[' || data[pos] == ']' || data[pos] == '{' || data[pos] == '}') {
248 pos_in = pos + 1;
249 return s.substring(pos, 1);
250 } else if (data[pos] == '(') {
251 int first = pos, nest = 0;
252 for (pos++; pos < len && (data[pos] != ')' || nest); pos++)
253 switch (data[pos]) {
254 case '(': nest++; break;
255 case ')': nest--; break;
256 case '\\':
257 if (pos + 1 < len)
258 pos++;
259 break;
260 case '\n': line++; break;
261 case '\r':
262 if (pos + 1 == len || data[pos+1] != '\n')
263 line++;
264 break;
265 }
266 pos_in = (pos < len ? pos + 1 : len);
267 return s.substring(first, pos_in - first);
268 } else {
269 int first = pos;
270 while (pos < len && data[pos] == '/')
271 pos++;
272 while (pos < len && data[pos] != '/' && !isspace((unsigned char) data[pos]) && data[pos] != '[' && data[pos] != ']' && data[pos] != '%' && data[pos] != '(' && data[pos] != '{' && data[pos] != '}')
273 pos++;
274 pos_in = pos;
275 return s.substring(first, pos - first);
276 }
277 }
278 }
279
280
281 static String
282 comment_tokenize(const String &s, int &pos_in, int &line)
283 {
284 const char *data = s.data();
285 int len = s.length();
286 int pos = pos_in;
287 while (1) {
288 while (pos < len && data[pos] != '%' && data[pos] != '(') {
289 if (data[pos] == '\n')
290 line++;
291 else if (data[pos] == '\r' && (pos + 1 == len || data[pos+1] != '\n'))
292 line++;
293 pos++;
294 }
295
296 if (pos >= len) {
297 pos_in = len;
298 return String();
299 } else if (data[pos] == '%') {
300 for (pos++; pos < len && (data[pos] == ' ' || data[pos] == '\t'); pos++)
301 /* nada */;
302 int first = pos;
303 for (; pos < len && data[pos] != '\n' && data[pos] != '\r'; pos++)
304 /* nada */;
305 pos_in = pos;
306 if (pos > first)
307 return s.substring(first, pos - first);
308 } else {
309 int nest = 0;
310 for (pos++; pos < len && (data[pos] != ')' || nest); pos++)
311 switch (data[pos]) {
312 case '(': nest++; break;
313 case ')': nest--; break;
314 case '\\':
315 if (pos + 1 < len)
316 pos++;
317 break;
318 case '\n': line++; break;
319 case '\r':
320 if (pos + 1 == len || data[pos+1] != '\n')
321 line++;
322 break;
323 }
324 }
325 }
326 }
327
328 static bool
329 retokenize_isword(char c)
330 {
331 return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')
332 || (c >= '0' && c <= '9') || c == '.' || c == '_';
333 }
334
335
336 static struct { const char *s; int v; } ligkern_ops[] = {
337 { "=:", DvipsEncoding::JL_LIG }, { "|=:", DvipsEncoding::JL_CLIG },
338 { "|=:>", DvipsEncoding::JL_CLIG_S }, { "=:|", DvipsEncoding::JL_LIGC },
339 { "=:|>", DvipsEncoding::JL_LIGC_S }, { "|=:|", DvipsEncoding::JL_CLIGC },
340 { "|=:>", DvipsEncoding::JL_CLIGC_S }, { "|=:|>>", DvipsEncoding::JL_CLIGC_SS },
341 { "{}", DvipsEncoding::JT_KERN }, { "{K}", DvipsEncoding::JT_KERN },
342 { "{L}", DvipsEncoding::JT_LIG }, { "{LK}", DvipsEncoding::JT_NOLIGKERN },
343 { "{KL}", DvipsEncoding::JT_NOLIGKERN }, { "{k}", DvipsEncoding::JT_KERN },
344 { "{l}", DvipsEncoding::JT_LIG }, { "{lk}", DvipsEncoding::JT_NOLIGKERN },
345 { "{kl}", DvipsEncoding::JT_NOLIGKERN },
346 // some encodings have @{@} instead of {}
347 { "@{@}", DvipsEncoding::JT_KERN },
348 { 0, 0 }
349 };
350
351 static int
352 find_ligkern_op(const String &s)
353 {
354 for (int i = 0; ligkern_ops[i].s; i++)
355 if (ligkern_ops[i].s == s)
356 return ligkern_ops[i].v;
357 return 0;
358 }
359
360 inline bool
361 operator==(const DvipsEncoding::Ligature& l1, const DvipsEncoding::Ligature& l2)
362 {
363 return l1.c1 == l2.c1 && l1.c2 == l2.c2;
364 }
365
366 void
367 DvipsEncoding::add_ligkern(const Ligature &l, int override)
368 {
369 Ligature *old = std::find(_lig.begin(), _lig.end(), l);
370 if (old == _lig.end())
371 _lig.push_back(l);
372 else {
373 if ((l.join & JT_KERN) && (override > 0 || !(old->join & JT_KERN))) {
374 old->join |= JT_KERN;
375 old->k = l.k;
376 }
377 if ((l.join & JT_LIG) && (override > 0 || !(old->join & JT_LIG))) {
378 old->join = (old->join & JT_KERN) | (l.join & JT_LIGALL);
379 old->d = l.d;
380 }
381 }
382 }
383
384 int
385 DvipsEncoding::parse_ligkern_words(Vector<String> &v, int override, ErrorHandler *errh)
386 {
387 _file_had_ligkern = true;
388 int op;
389 long l;
390 char *endptr;
391 if (v.size() == 3) {
392 // empty string fails
393 if (!v[0])
394 return -1;
395 // boundary char setting
396 if (v[0] == "||" && v[1] == "=") {
397 char *endptr;
398 long boundary_char = strtol(v[2].c_str(), &endptr, 10);
399 if (override > 0 || _boundary_char < 0)
400 _boundary_char = boundary_char;
401 if (*endptr == 0 && boundary_char < _e.size())
402 return 0;
403 else
404 return errh->error("parse error in boundary character assignment");
405 }
406 // altselector char setting
407 if (v[0] == "^^" && v[1] == "=") {
408 char *endptr;
409 long altselector_char = strtol(v[2].c_str(), &endptr, 10);
410 if (override > 0 || _altselector_char < 0)
411 _altselector_char = altselector_char;
412 if (*endptr == 0 && altselector_char < _e.size())
413 return 0;
414 else
415 return errh->error("parse error in altselector character assignment");
416 }
417 // encoding
418 l = strtol(v[0].c_str(), &endptr, 0);
419 if (endptr == v[0].end() && v[1] == "=") {
420 if (l >= 0 && l < 256) {
421 if (override > 0 || !_e[l])
422 encode(l, v[2]);
423 return 0;
424 } else
425 return errh->error("encoding value %<%d%> out of range", l);
426 }
427
428 // kern operation
429 if (v[1].length() >= 3 && v[1][0] == '{' && v[1].back() == '}') {
430 String middle = v[1].substring(1, v[1].length() - 2);
431 l = strtol(middle.c_str(), &endptr, 0);
432 if (endptr == middle.end()) {
433 op = JT_KERN;
434 goto found_kernop;
435 }
436 }
437 op = find_ligkern_op(v[1]);
438 if (!op || (op & JT_ADDLIG))
439 return -1;
440 found_kernop:
441 int av = (v[0] == "*" ? J_ALL : encoding_of(v[0]));
442 if (av < 0)
443 return errh->warning("bad %<%s%> (%<%s%> has no encoding)", v[1].c_str(), v[0].c_str());
444 int bv = (v[2] == "*" ? J_ALL : encoding_of(v[2]));
445 if (bv < 0)
446 return errh->warning("bad %<%s%> (%<%s%> has no encoding)", v[1].c_str(), v[2].c_str());
447 if ((op & JT_KERN) && l && (av == J_ALL || bv == J_ALL))
448 return errh->warning("%<%s %s %s%> illegal, only {0} works with *", v[0].c_str(), v[1].c_str(), v[2].c_str());
449 Ligature lig = { av, bv, op, static_cast<int>(l), 0 };
450 add_ligkern(lig, override);
451 return 0;
452
453 } else if (v.size() == 4 && ((op = find_ligkern_op(v[2])) & JT_ADDLIG)) {
454 int av = encoding_of(v[0], override > 0);
455 if (av < 0)
456 return (override > 0 ? errh->warning("bad ligature (%<%s%> has no encoding)", v[0].c_str()) : -1);
457 int bv = encoding_of(v[1], override > 0);
458 if (bv < 0)
459 return (override > 0 ? errh->warning("bad ligature (%<%s%> has no encoding)", v[1].c_str()) : -1);
460 int cv = encoding_of(v[3], override > 0);
461 if (cv < 0)
462 return (override > 0 ? errh->warning("bad ligature (%<%s%> has no encoding)", v[3].c_str()) : -1);
463 Ligature lig = { av, bv, op, 0, cv };
464 add_ligkern(lig, override);
465 return 0;
466
467 } else
468 return -EPARSE;
469 }
470
471 int
472 DvipsEncoding::parse_position_words(Vector<String> &v, int override, ErrorHandler *errh)
473 {
474 if (v.size() != 4)
475 return -EPARSE;
476
477 int c = encoding_of(v[0]);
478 if (c < 0)
479 return (override > 0 ? errh->warning("bad positioning (%<%s%> has no encoding)", v[0].c_str()) : -1);
480
481 char *endptr;
482 int pdx, pdy, adx;
483 if (!v[1] || !v[2] || !v[3]
484 || (pdx = strtol(v[1].c_str(), &endptr, 10), *endptr)
485 || (pdy = strtol(v[2].c_str(), &endptr, 10), *endptr)
486 || (adx = strtol(v[3].c_str(), &endptr, 10), *endptr))
487 return errh->error("parse error in POSITION");
488
489 Ligature l = { c, pdx, pdy, adx, 0 };
490 Ligature *old = std::find(_pos.begin(), _pos.end(), l);
491 if (old == _pos.end())
492 _pos.push_back(l);
493 else if (override > 0)
494 *old = l;
495 return 0;
496 }
497
498 int
499 DvipsEncoding::parse_unicoding_words(Vector<String> &v, int override, ErrorHandler *errh)
500 {
501 int av;
502 if (v.size() < 2 || (v[1] != "=" && v[1] != "=:" && v[1] != ":="))
503 return -EPARSE;
504 else if (v[0] == "||" || (av = encoding_of(v[0])) < 0)
505 return errh->warning("bad UNICODING (%<%s%> has no encoding)", v[0].c_str());
506
507 int original_size = _unicoding.size();
508
509 if (v.size() == 2 || (v.size() == 3 && v[2] == dot_notdef))
510 /* no warnings to delete a glyph */;
511 else {
512 for (int i = 2; i < v.size(); i++) {
513 if (_unicoding.size() != original_size)
514 _unicoding.push_back(GLYPHLIST_ALTERNATIVE);
515 if (!glyphname_unicode(v[i], _unicoding)) {
516 errh->warning("can%,t map %<%s%> to Unicode", v[i].c_str());
517 if (i == v.size() - 1 && _unicoding.size() == original_size)
518 errh->warning("target %<%s%> will be deleted from encoding", v[0].c_str());
519 else if (_unicoding.size() != original_size)
520 _unicoding.pop_back();
521 }
522 }
523 }
524
525 _unicoding.push_back(0);
526 if (override > 0 || _unicoding_map[v[0]] < 0)
527 _unicoding_map.insert(v[0], original_size);
528 return 0;
529 }
530
531 const DvipsEncoding::WordType DvipsEncoding::word_types[] = {
532 { "LIGKERN", &DvipsEncoding::parse_ligkern_words },
533 { "POSITION", &DvipsEncoding::parse_position_words },
534 { "UNICODING", &DvipsEncoding::parse_unicoding_words }
535 };
536
537 void
538 DvipsEncoding::parse_word_group(Vector<String> &words, int override, int wt, ErrorHandler *errh)
539 {
540 if (words.size() > 0) {
541 int (DvipsEncoding::*method)(Vector<String> &, int, ErrorHandler *) = word_types[wt].parsefunc;
542 if ((this->*method)(words, override, errh) == -EPARSE) {
543 Vector<String> rewords;
544 for (String *sp = words.begin(); sp != words.end(); sp++) {
545 const char *s = sp->begin(), *ends = sp->end();
546 while (s != ends) {
547 const char *word = s;
548 if (*s == '{') {
549 for (s++; s != ends && *s != '}'; s++)
550 /* nada */;
551 if (s != ends)
552 s++;
553 } else {
554 bool x = retokenize_isword(*s);
555 for (s++; s != ends && x == retokenize_isword(*s); s++)
556 /* nada */;
557 }
558 rewords.push_back(sp->substring(word, s));
559 }
560 }
561 if ((this->*method)(rewords, override, errh) == -EPARSE)
562 errh->error("parse error in %s", word_types[wt].name);
563
564 }
565 words.clear();
566 }
567 }
568
569 int
570 DvipsEncoding::parse_words(const String &s, int override, int wt, ErrorHandler *errh)
571 {
572 Vector<String> words;
573 const char *data = s.data();
574 const char *end = s.end();
575 while (data < end) {
576 while (data < end && isspace((unsigned char) *data))
577 data++;
578 const char *first = data;
579 while (data < end && !isspace((unsigned char) *data) && *data != ';')
580 data++;
581 if (data == first) {
582 data++; // step past semicolon (or harmlessly past EOS)
583 parse_word_group(words, override, wt, errh);
584 } else
585 words.push_back(s.substring(first, data));
586 }
587 parse_word_group(words, override, wt, errh);
588 return 0;
589 }
590
591 String
592 DvipsEncoding::landmark(int line) const
593 {
594 StringAccum sa;
595 sa << _printable_filename << ':' << line;
596 return sa.take_string();
597 }
598
599 static String
600 trim_space(const String &s, int pos)
601 {
602 while (pos < s.length() && isspace((unsigned char) s[pos]))
603 pos++;
604 int epos = s.length();
605 for (int x = 0; x < 2; x++) {
606 while (epos > pos && isspace((unsigned char) s[epos - 1]))
607 epos--;
608 if (epos == pos || s[epos - 1] != ';')
609 break;
610 epos--;
611 }
612 return s.substring(pos, epos - pos);
613 }
614
615 int
616 DvipsEncoding::parse(String filename, bool ignore_ligkern, bool ignore_other, ErrorHandler *errh)
617 {
618 int before = errh->nerrors();
619 String s = read_file(filename, errh);
620 if (errh->nerrors() != before)
621 return -1;
622 _filename = filename;
623 _printable_filename = printable_filename(filename);
624 _file_had_ligkern = false;
625 int pos = 0, line = 1;
626
627 // parse text
628 String token = tokenize(s, pos, line);
629 if (!token || token[0] != '/')
630 return errh->lerror(landmark(line), "parse error, expected name");
631 _name = token.substring(1);
632 _initial_comment = s.substring(0, pos - token.length());
633
634 if (tokenize(s, pos, line) != "[")
635 return errh->lerror(landmark(line), "parse error, expected [");
636
637 while ((token = tokenize(s, pos, line)) && token[0] == '/')
638 _e.push_back(token.substring(1));
639
640 _final_text = token + s.substring(pos);
641
642 // now parse comments
643 pos = 0, line = 1;
644 Vector<String> words;
645 LandmarkErrorHandler lerrh(errh, "");
646 while ((token = comment_tokenize(s, pos, line)))
647 if (token.length() >= 8
648 && memcmp(token.data(), "LIGKERN", 7) == 0
649 && isspace((unsigned char) token[7])
650 && !ignore_ligkern) {
651 lerrh.set_landmark(landmark(line));
652 parse_words(token.substring(8), 1, WT_LIGKERN, &lerrh);
653
654 } else if (token.length() >= 9
655 && memcmp(token.data(), "LIGKERNX", 8) == 0
656 && isspace((unsigned char) token[8])
657 && !ignore_ligkern) {
658 lerrh.set_landmark(landmark(line));
659 parse_words(token.substring(9), 1, WT_LIGKERN, &lerrh);
660
661 } else if (token.length() >= 10
662 && memcmp(token.data(), "UNICODING", 9) == 0
663 && isspace((unsigned char) token[9])
664 && !ignore_other) {
665 lerrh.set_landmark(landmark(line));
666 parse_words(token.substring(10), 1, WT_UNICODING, &lerrh);
667
668 } else if (token.length() >= 9
669 && memcmp(token.data(), "POSITION", 8) == 0
670 && isspace((unsigned char) token[8])
671 && !ignore_other) {
672 lerrh.set_landmark(landmark(line));
673 parse_words(token.substring(9), 1, WT_POSITION, &lerrh);
674
675 } else if (token.length() >= 13
676 && memcmp(token.data(), "CODINGSCHEME", 12) == 0
677 && isspace((unsigned char) token[12])
678 && !ignore_other) {
679 _coding_scheme = trim_space(token, 13);
680 if (_coding_scheme.length() > 39)
681 lerrh.lwarning(landmark(line), "only first 39 chars of CODINGSCHEME are significant");
682 if (std::find(_coding_scheme.begin(), _coding_scheme.end(), '(') < _coding_scheme.end()
683 || std::find(_coding_scheme.begin(), _coding_scheme.end(), ')') < _coding_scheme.end()) {
684 lerrh.lerror(landmark(line), "CODINGSCHEME cannot contain parentheses");
685 _coding_scheme = String();
686 }
687
688 } else if (token.length() >= 11
689 && memcmp(token.data(), "WARNMISSING", 11) == 0
690 && (token.length() == 11 || isspace((unsigned char) token[11]))
691 && !ignore_other) {
692 String value = trim_space(token, 11);
693 if (value == "1" || value == "yes" || value == "true" || !value)
694 _warn_missing = true;
695 else if (value == "0" || value == "no" || value == "false")
696 _warn_missing = false;
697 else
698 lerrh.lerror(landmark(line), "WARNMISSING command not understood");
699 }
700
701 return 0;
702 }
703
704 int
705 DvipsEncoding::parse_ligkern(const String &ligkern_text, int override, ErrorHandler *errh)
706 {
707 return parse_words(ligkern_text, override, WT_LIGKERN, errh);
708 }
709
710 int
711 DvipsEncoding::parse_position(const String &position_text, int override, ErrorHandler *errh)
712 {
713 return parse_words(position_text, override, WT_POSITION, errh);
714 }
715
716 int
717 DvipsEncoding::parse_unicoding(const String &unicoding_text, int override, ErrorHandler *errh)
718 {
719 return parse_words(unicoding_text, override, WT_UNICODING, errh);
720 }
721
722 void
723 DvipsEncoding::bad_codepoint(int code, Metrics &metrics, HashMap<PermString, int> &unencoded)
724 {
725 for (int i = 0; i < _lig.size(); i++) {
726 Ligature &l = _lig[i];
727 if (l.c1 == code || l.c2 == code)
728 l.join = 0;
729 else if ((l.join & JT_ADDLIG) && l.d == code)
730 l.join &= ~JT_LIGALL;
731 }
732
733 if (_warn_missing) {
734 Vector<uint32_t> garbage;
735 bool unicodes_explicit = x_unicodes(_e[code], garbage);
736 if (!unicodes_explicit || garbage.size() > 0) {
737 Vector<Setting> v;
738 v.push_back(Setting(Setting::RULE, 500, 500));
739 v.push_back(Setting(Setting::SPECIAL, String("Warning: missing glyph '") + _e[code] + "'"));
740 metrics.encode_virtual(code, _e[code], 0, v, true);
741 unencoded.insert(_e[code], 1);
742 }
743 }
744 }
745
746 static inline Efont::OpenType::Glyph
747 map_uni(uint32_t uni, const Efont::OpenType::Cmap &cmap, const Metrics &m)
748 {
749 if (uni == U_EMPTYSLOT)
750 return m.emptyslot_glyph();
751 else
752 return cmap.map_uni(uni);
753 }
754
755 bool
756 DvipsEncoding::x_unicodes(PermString chname, Vector<uint32_t> &unicodes) const
757 {
758 int i = _unicoding_map[chname];
759 if (i >= 0) {
760 for (; _unicoding[i] > 0; i++)
761 unicodes.push_back(_unicoding[i]);
762 return true;
763 } else {
764 glyphname_unicode(chname, unicodes);
765 return false;
766 }
767 }
768
769
770 void
771 DvipsEncoding::make_metrics(Metrics &metrics, const FontInfo &finfo, Secondary *secondary, bool literal, ErrorHandler *errh)
772 {
773 // first pass: without secondaries
774 for (int code = 0; code < _e.size(); code++) {
775 PermString chname = _e[code];
776
777 // common case: skip .notdef
778 if (chname == dot_notdef)
779 continue;
780
781 // find first single Unicode glyph supported by the font
782 Efont::OpenType::Glyph glyph = 0;
783 uint32_t glyph_uni = 0;
784 {
785 Vector<uint32_t> unicodes;
786 (void) x_unicodes(chname, unicodes);
787 Vector<uint32_t>::iterator u = unicodes.begin();
788 while (u != unicodes.end() && glyph <= 0) {
789 uint32_t this_uni = u[0];
790 ++u;
791 if (u != unicodes.end()) {
792 if (*u != GLYPHLIST_ALTERNATIVE)
793 break;
794 ++u;
795 }
796
797 glyph = map_uni(this_uni, *finfo.cmap, metrics);
798 if (glyph_uni == 0 || glyph > 0)
799 glyph_uni = this_uni;
800 }
801 }
802
803 // find named glyph, if any
804 Efont::OpenType::Glyph named_glyph = finfo.glyphid(chname);
805 #if 0
806 // 2.May.2008: ff, fi, fl, ffi, and ffl might map to f_f, f_i, f_l,
807 // f_f_i, and f_f_l
808 if (!named_glyph && chname.length() > 0 && chname.length() <= 3
809 && chname[0] == 'f') {
810 if (chname.equals("ff", 2))
811 named_glyph = finfo.glyphid("f_f");
812 else if (chname.equals("fi", 2))
813 named_glyph = finfo.glyphid("f_i");
814 else if (chname.equals("fl", 2))
815 named_glyph = finfo.glyphid("f_l");
816 else if (chname.equals("ffi", 2))
817 named_glyph = finfo.glyphid("f_f_i");
818 else if (chname.equals("ffl", 2))
819 named_glyph = finfo.glyphid("f_f_l");
820 }
821 #endif
822 // do not use a Unicode-mapped glyph if literal
823 if (literal)
824 glyph = named_glyph;
825
826 // If we found a glyph, maybe use its named_glyph variant.
827 if (glyph > 0 && named_glyph > 0
828 && std::find(chname.begin(), chname.end(), '.') < chname.end())
829 glyph = named_glyph;
830
831 // assign slot
832 if (glyph > 0)
833 metrics.encode(code, glyph_uni, glyph);
834 }
835
836 // second pass: with secondaries
837 for (int code = 0; code < _e.size(); code++) {
838 // skip already-encoded characters and .notdef
839 if (literal || metrics.glyph(code) > 0 || _e[code] == dot_notdef)
840 continue;
841
842 PermString chname = _e[code];
843
844 // find all Unicodes
845 Vector<uint32_t> unicodes;
846 bool unicodes_explicit = x_unicodes(chname, unicodes);
847
848 // find named glyph, if any
849 Efont::OpenType::Glyph named_glyph = finfo.glyphid(chname);
850
851 // 1. We were not able to find the glyph using Unicode.
852 // 2. There might be a named_glyph.
853 // May need to try secondaries later. Store this slot.
854 // Try secondaries, if there's explicit unicoding or no named_glyph.
855 if (unicodes_explicit || named_glyph <= 0)
856 for (uint32_t *u = unicodes.begin(); u != unicodes.end(); ) {
857 uint32_t *endu = u + 1;
858 while (endu != unicodes.end() && *endu != GLYPHLIST_ALTERNATIVE)
859 ++endu;
860 if (secondary->encode_uni(code, chname, u, endu, metrics, errh))
861 goto encoded;
862 u = (endu == unicodes.end() ? endu : endu + 1);
863 }
864
865 // 1. We were not able to find the glyph using Unicode or secondaries.
866 // 2. There might be a named_glyph.
867 // Use named glyph, if any. Special case for "UNICODING foo =: ;",
868 // which should turn off the character (even if a named_glyph exists),
869 // UNLESS the glyph was explicitly requested.
870 if (named_glyph > 0
871 && (!unicodes_explicit
872 || unicodes.size() > 0
873 || (_encoding_required.size() > code && _encoding_required[code]))) {
874 uint32_t uni = 0;
875 if (unicodes.size() == 1 || (unicodes.size() > 0 && unicodes[1] == GLYPHLIST_ALTERNATIVE))
876 uni = unicodes[0];
877 metrics.encode(code, uni, named_glyph);
878 }
879
880 encoded:
881 /* all set */;
882 }
883
884 // add altselector
885 if (_altselector_char >= 0 && _altselector_char < _e.size()) {
886 metrics.add_altselector_code(_altselector_char, 0);
887 if (metrics.glyph(_altselector_char) <= 0 && !literal)
888 (void) secondary->encode_uni(_altselector_char, "<altselector>", U_ALTSELECTOR, metrics, errh);
889 }
890
891 // final pass: complain
892 HashMap<PermString, int> unencoded_map;
893 for (int code = 0; code < _e.size(); code++)
894 if (_e[code] != dot_notdef && metrics.glyph(code) <= 0)
895 bad_codepoint(code, metrics, unencoded_map);
896 Vector<String> unencoded;
897 for (HashMap<PermString, int>::iterator it = unencoded_map.begin(); it; ++it)
898 unencoded.push_back(it.key());
899
900 if (unencoded.size() == 1) {
901 errh->warning("%<%s%> glyph not found in font", unencoded[0].c_str());
902 errh->message("(This glyph will appear as a blot and cause warnings if used.)");
903 } else if (unencoded.size() > 1) {
904 std::sort(unencoded.begin(), unencoded.end());
905 StringAccum sa;
906 for (const String* a = unencoded.begin(); a < unencoded.end(); a++)
907 sa << *a << ' ';
908 sa.pop_back();
909 sa.append_break_lines(sa.take_string(), 68, " ");
910 sa.pop_back();
911 errh->warning("%d glyphs not found in font:", unencoded.size());
912 errh->message("%s\n(These glyphs will appear as blots and cause warnings if used.)", sa.c_str());
913 }
914
915 metrics.set_coding_scheme(_coding_scheme);
916 }
917
918 void
919 DvipsEncoding::make_base_mappings(Vector<int> &mappings, const FontInfo &finfo)
920 {
921 mappings.clear();
922 for (int code = 0; code < _e.size(); code++) {
923 PermString chname = _e[code];
924
925 // common case: skip .notdef
926 if (chname == dot_notdef)
927 continue;
928
929 // find named glyph
930 Efont::OpenType::Glyph named_glyph = finfo.glyphid(chname);
931 if (named_glyph > 0) {
932 if (mappings.size() <= named_glyph)
933 mappings.resize(named_glyph + 1, -1);
934 mappings[named_glyph] = code;
935 }
936 }
937 }
938
939
940 void
941 DvipsEncoding::apply_ligkern_lig(Metrics &metrics, ErrorHandler *errh) const
942 {
943 assert((int)J_ALL == (int)Metrics::CODE_ALL);
944 for (const Ligature *l = _lig.begin(); l < _lig.end(); l++) {
945 if (l->c1 < 0 || l->c2 < 0 || l->join < 0 || !(l->join & JT_LIG))
946 continue;
947 metrics.remove_ligatures(l->c1, l->c2);
948 if (!(l->join & JT_ADDLIG))
949 /* nada */;
950 else if ((l->join & JT_LIGALL) == JL_LIG)
951 metrics.add_ligature(l->c1, l->c2, l->d);
952 else if ((l->join & JT_LIGALL) == JL_LIGC)
953 metrics.add_ligature(l->c1, l->c2, metrics.pair_code(l->d, l->c2));
954 else if ((l->join & JT_LIGALL) == JL_CLIG)
955 metrics.add_ligature(l->c1, l->c2, metrics.pair_code(l->c1, l->d));
956 else {
957 static int complex_join_warning = 0;
958 if (!complex_join_warning) {
959 errh->warning("complex LIGKERN ligature removed (I only support %<=:%>, %<=:|%>, and %<|=:%>)");
960 complex_join_warning = 1;
961 }
962 }
963 }
964 }
965
966 void
967 DvipsEncoding::apply_ligkern_kern(Metrics &metrics, ErrorHandler *) const
968 {
969 assert((int)J_ALL == (int)Metrics::CODE_ALL);
970 for (const Ligature *l = _lig.begin(); l < _lig.end(); l++)
971 if (l->c1 >= 0 && l->c2 >= 0 && (l->join & JT_KERN))
972 metrics.set_kern(l->c1, l->c2, l->k);
973 }
974
975 void
976 DvipsEncoding::apply_position(Metrics &metrics, ErrorHandler *) const
977 {
978 for (const Ligature *l = _pos.begin(); l < _pos.end(); l++)
979 if (l->c1 >= 0)
980 metrics.add_single_positioning(l->c1, l->c2, l->join, l->k);
981 }