1 // -*- related-file-name: "../include/efont/otfname.hh" -*-
2
3 /* otfname.{cc,hh} -- OpenType name table
4 *
5 * Copyright (c) 2003-2019 Eddie Kohler
6 *
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the Free
9 * Software Foundation; either version 2 of the License, or (at your option)
10 * any later version. This program is distributed in the hope that it will be
11 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
13 * Public License for more details.
14 */
15
16 #ifdef HAVE_CONFIG_H
17 # include <config.h>
18 #endif
19 #include <efont/otfname.hh>
20 #include <lcdf/error.hh>
21 #include <lcdf/straccum.hh>
22 #include <errno.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <algorithm>
26
27 #define USHORT_AT(d) (Data::u16_aligned(d))
28
29 namespace Efont { namespace OpenType {
30
31 static const uint16_t mac_roman_encoding[] = {
32 // 0x80-0x8F
33 0x00C4, 0x00C5, 0x00C7, 0x00C9, // Adieresis Aring Ccedilla Eacute
34 0x00D1, 0x00D6, 0x00DC, 0x00E1, // Ntilde Odieresis Udieresis aacute
35 0x00E0, 0x00E2, 0x00E4, 0x00E3, // agrave acircumflex adieresis atilde
36 0x00E5, 0x00E7, 0x00E9, 0x00E8, // aring ccedilla eacute egrave
37 // 0x90-0x9F
38 0x00EA, 0x00EB, 0x00ED, 0x00EC, // ecircumflex edieresis iacute igrave
39 0x00EE, 0x00EF, 0x00F1, 0x00F3, // icircumflex idieresis ntilde oacute
40 0x00F2, 0x00F4, 0x00F6, 0x00F5, // ograve ocircumflex odieresis otilde
41 0x00FA, 0x00F9, 0x00FB, 0x00FC, // uacute ugrave ucircumflex udieresis
42 // 0xA0-0xAF
43 0x2020, 0x00B0, 0x00A2, 0x00A3, // dagger degree cent sterling
44 0x00A7, 0x2022, 0x00B6, 0x00DF, // section bullet paragraph germandbls
45 0x00AE, 0x00A9, 0x2122, 0x00B4, // registered copyright trademark acute
46 0x00A8, 0x2260, 0x00C6, 0x00D8, // dieresis notequal AE Oslash
47 // 0xB0-0xBF
48 0x221E, 0x00B1, 0x2264, 0x2265, // infinity plusminus lessequal greaterequal
49 0x00A5, 0x00B5, 0x2202, 0x2211, // yen mu partialdiff summation
50 0x220F, 0x03C0, 0x222B, 0x00AA, // product pi integral ordfeminine
51 0x00BA, 0x03A9, 0x00E6, 0x00F8, // ordmasculine Omegagreek ae oslash
52 // 0xC0-0xCF
53 0x00BF, 0x00A1, 0x00AC, 0x221A, // questiondown exclamdown logicalnot radical
54 0x0192, 0x2248, 0x2206, 0x00AB, // florin approxequal increment guillemotleft
55 0x00BB, 0x2026, 0x00A0, 0x00C0, // guillemotright ellipsis nbspace Agrave
56 0x00C3, 0x00D5, 0x0152, 0x0153, // Atilde Otilde OE oe
57 // 0xD0-0xDF
58 0x2013, 0x2014, 0x201C, 0x201D, // endash emdash quotedblleft quotedblright
59 0x2018, 0x2019, 0x00F7, 0x25CA, // quoteleft quoteright divide lozenge
60 0x00FF, 0x0178, 0x2044, 0x20AC, // ydieresis Ydieresis fraction Euro
61 0x2039, 0x203A, 0xFB01, 0xFB02, // guilsinglleft guilsinglright fi fl
62 // 0xE0-0xEF
63 0x2021, 0x00B7, 0x201A, 0x201E, // daggerdbl middot quotesinglbase quotedblbase
64 0x2030, 0x00C2, 0x00CA, 0x00C1, // perthousand Acircumflex Ecircumflex Aacute
65 0x00CB, 0x00C8, 0x00CD, 0x00CE, // Edieresis Egrave Iacute Icircumflex
66 0x00CF, 0x00CC, 0x00D3, 0x00D4, // Idieresis Igrave Oacute Ocircumflex
67 // 0xF0-0xFF
68 0xF8FF, 0x00D2, 0x00DA, 0x00DB, // apple Ograve Uacute Ucircumflex
69 0x00D9, 0x0131, 0x02C6, 0x02DC, // Ugrave dotlessi circumflex tilde
70 0x00AF, 0x02D8, 0x02D9, 0x02DA, // macron breve dotaccent ring
71 0x00B8, 0x02DD, 0x02DB, 0x02C7 // cedilla hungarumlaut ogonek caron
72 };
73
74
Name(const String & s,ErrorHandler * errh)75 Name::Name(const String &s, ErrorHandler *errh)
76 : _str(s) {
77 _str.align(2);
78 _error = parse_header(errh ? errh : ErrorHandler::silent_handler());
79 }
80
81 int
parse_header(ErrorHandler * errh)82 Name::parse_header(ErrorHandler *errh)
83 {
84 // HEADER FORMAT:
85 // USHORT version
86 // USHORT numTables
87 int len = _str.length();
88 const uint8_t *data = _str.udata();
89 if (len == 0)
90 return errh->error("font has no %<name%> table"), -EFAULT;
91 if (HEADER_SIZE > len)
92 return errh->error("%<name%> table too small"), -EFAULT;
93 if (!(data[0] == '\000' && data[1] == '\000'))
94 return errh->error("bad %<name%> version number"), -ERANGE;
95 int count = USHORT_AT(data + 2);
96 if (HEADER_SIZE + count*NAMEREC_SIZE > len)
97 return errh->error("%<name%> table too small"), -EFAULT;
98 return 0;
99 }
100
101 String
name(const_iterator i) const102 Name::name(const_iterator i) const
103 {
104 if (i < end()) {
105 int stringOffset = USHORT_AT(_str.udata() + 4);
106 int length = USHORT_AT(reinterpret_cast<const uint8_t *>(i) + 8);
107 int offset = USHORT_AT(reinterpret_cast<const uint8_t *>(i) + 10);
108 if (stringOffset + offset + length <= _str.length())
109 return _str.substring(stringOffset + offset, length);
110 }
111 return String();
112 }
113
114 String
utf8_name(const_iterator i) const115 Name::utf8_name(const_iterator i) const
116 {
117 // This code can handle Microsoft Unicode BMP and Mac Roman encodings,
118 // but that's it
119 if (!(i < end()))
120 return String();
121 int stringOffset = USHORT_AT(_str.udata() + 4);
122 int length = USHORT_AT(reinterpret_cast<const uint8_t *>(i) + 8);
123 int offset = USHORT_AT(reinterpret_cast<const uint8_t *>(i) + 10);
124 if (stringOffset + offset + length > _str.length())
125 return String();
126 const unsigned char *begins = _str.udata() + stringOffset + offset;
127 const unsigned char *ends = begins + length;
128 if (platform(*i) == P_MICROSOFT && encoding(*i) == E_MS_UNICODE_BMP) {
129 StringAccum sa;
130 for (const unsigned char *s = begins; s + 1 < ends; s += 2)
131 sa.append_utf8(Data::u16(s));
132 return sa.take_string();
133 } else if (platform(*i) == P_MACINTOSH && encoding(*i) == E_MAC_ROMAN) {
134 StringAccum sa;
135 for (const unsigned char *s = begins; s < ends; s++)
136 if (*s >= 0x80) {
137 sa.append(begins, s);
138 sa.append_utf8(mac_roman_encoding[*s & 0x7F]);
139 begins = s + 1;
140 }
141 if (!sa)
142 return _str.substring(begins, ends);
143 else {
144 sa.append(begins, ends);
145 return sa.take_string();
146 }
147 } else
148 return _str.substring(begins, ends);
149 }
150
151 String
english_name(int nameid) const152 Name::english_name(int nameid) const
153 {
154 const_iterator end = this->end();
155 const_iterator it = std::find_if(begin(), end, PlatformPred(nameid, P_MICROSOFT, E_MS_UNICODE_BMP, L_MS_ENGLISH_AMERICAN));
156 if (it == end)
157 it = std::find_if(begin(), end, PlatformPred(nameid, P_MACINTOSH, E_MAC_ROMAN, 0));
158 return utf8_name(it);
159 }
160
161 bool
version_chaincontext_reverse_backtrack() const162 Name::version_chaincontext_reverse_backtrack() const
163 {
164 String vstr = name(std::find_if(begin(), end(), PlatformPred(N_VERSION, 1, 0, 0)));
165 const char *v = vstr.begin(), *endv = vstr.end();
166 if (v + 20 <= endv) {
167 if (v[0] != 'O' || v[1] != 'T' || v[2] != 'F' || v[3] == ';')
168 goto try_core;
169 for (v += 4; v < endv && *v != ';'; v++)
170 /* do nothing */;
171 if (v + 3 >= endv || v[1] != 'P' || v[2] != 'S' || v[3] == ';')
172 goto try_core;
173 for (v += 4; v < endv && *v != ';'; v++)
174 /* do nothing */;
175 if (v + 11 >= endv || memcmp(v + 1, "Core 1.0.", 9) != 0
176 || (v[10] != '2' && v[10] != '3')
177 || (v[11] < '0' || v[11] > '9'))
178 goto try_core;
179 return true;
180 }
181 try_core:
182 v = vstr.begin();
183 if (v + 16 > endv
184 || v[0] != 'C' || v[1] != 'o' || v[2] != 'r' || v[3] != 'e')
185 return false;
186 for (v += 4; v < endv && *v != ';'; v++)
187 /* do nothing */;
188 if (v + 12 > endv || memcmp(v, ";makeotf.lib", 12) != 0)
189 return false;
190 return true;
191 }
192
193 } // namespace Efont::OpenType
194 } // namespace Efont
195