| 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 |
|
| 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 |
| 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 |
| 102 |
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 |
| 115 |
Name::utf8_name(const_iterator i) const |
| 116 |
{ |
| 117 |
// This code can handle Microsoft Unicode 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 |
| 129 |
&& (encoding(*i) == E_MS_UNICODE_BMP || encoding(*i) == E_MS_UNICODE_FULL)) { |
| 130 |
StringAccum sa; |
| 131 |
for (const unsigned char *s = begins; s + 1 < ends; s += 2) { |
| 132 |
unsigned d = Data::u16(s), d2; |
| 133 |
if (d >= 0xD800 && d < 0xE000) { |
| 134 |
if (d < 0xDC00 && s + 3 < ends |
| 135 |
&& (d2 = Data::u16(s + 2)) >= 0xDC00 && d2 < 0xE000) { |
| 136 |
d = 0x10000 + ((d - 0xD800) << 10) + (d2 - 0xDC00); |
| 137 |
s += 2; |
| 138 |
} else { |
| 139 |
d = 0xFFFD; // REPLACEMENT CHARACTER |
| 140 |
} |
| 141 |
} |
| 142 |
sa.append_utf8(d); |
| 143 |
} |
| 144 |
return sa.take_string(); |
| 145 |
} else if (platform(*i) == P_MACINTOSH && encoding(*i) == E_MAC_ROMAN) { |
| 146 |
StringAccum sa; |
| 147 |
for (const unsigned char *s = begins; s < ends; s++) |
| 148 |
if (*s >= 0x80) { |
| 149 |
sa.append(begins, s); |
| 150 |
sa.append_utf8(mac_roman_encoding[*s & 0x7F]); |
| 151 |
begins = s + 1; |
| 152 |
} |
| 153 |
if (!sa) |
| 154 |
return _str.substring(begins, ends); |
| 155 |
else { |
| 156 |
sa.append(begins, ends); |
| 157 |
return sa.take_string(); |
| 158 |
} |
| 159 |
} else |
| 160 |
return _str.substring(begins, ends); |
| 161 |
} |
| 162 |
|
| 163 |
String |
| 164 |
Name::english_name(int nameid) const |
| 165 |
{ |
| 166 |
const_iterator end = this->end(); |
| 167 |
const_iterator it = std::find_if(begin(), end, PlatformPred(nameid, P_MICROSOFT, E_MS_UNICODE_BMP, L_MS_ENGLISH_AMERICAN)); |
| 168 |
if (it == end) |
| 169 |
it = std::find_if(begin(), end, PlatformPred(nameid, P_MICROSOFT, E_MS_UNICODE_FULL, L_MS_ENGLISH_AMERICAN)); |
| 170 |
if (it == end) |
| 171 |
it = std::find_if(begin(), end, PlatformPred(nameid, P_MACINTOSH, E_MAC_ROMAN, 0)); |
| 172 |
return utf8_name(it); |
| 173 |
} |
| 174 |
|
| 175 |
bool |
| 176 |
Name::version_chaincontext_reverse_backtrack() const |
| 177 |
{ |
| 178 |
String vstr = name(std::find_if(begin(), end(), PlatformPred(N_VERSION, 1, 0, 0))); |
| 179 |
const char *v = vstr.begin(), *endv = vstr.end(); |
| 180 |
if (v + 20 <= endv) { |
| 181 |
if (v[0] != 'O' || v[1] != 'T' || v[2] != 'F' || v[3] == ';') |
| 182 |
goto try_core; |
| 183 |
for (v += 4; v < endv && *v != ';'; v++) |
| 184 |
/* do nothing */; |
| 185 |
if (v + 3 >= endv || v[1] != 'P' || v[2] != 'S' || v[3] == ';') |
| 186 |
goto try_core; |
| 187 |
for (v += 4; v < endv && *v != ';'; v++) |
| 188 |
/* do nothing */; |
| 189 |
if (v + 11 >= endv || memcmp(v + 1, "Core 1.0.", 9) != 0 |
| 190 |
|| (v[10] != '2' && v[10] != '3') |
| 191 |
|| (v[11] < '0' || v[11] > '9')) |
| 192 |
goto try_core; |
| 193 |
return true; |
| 194 |
} |
| 195 |
try_core: |
| 196 |
v = vstr.begin(); |
| 197 |
if (v + 16 > endv |
| 198 |
|| v[0] != 'C' || v[1] != 'o' || v[2] != 'r' || v[3] != 'e') |
| 199 |
return false; |
| 200 |
for (v += 4; v < endv && *v != ';'; v++) |
| 201 |
/* do nothing */; |
| 202 |
if (v + 12 > endv || memcmp(v, ";makeotf.lib", 12) != 0) |
| 203 |
return false; |
| 204 |
return true; |
| 205 |
} |
| 206 |
|
| 207 |
} // namespace Efont::OpenType |
| 208 |
} // namespace Efont |