| 1 |
// -*- related-file-name: "../include/efont/otfpost.hh" -*- |
| 2 |
|
| 3 |
/* otfpost.{cc,hh} -- OpenType post table |
| 4 |
* |
| 5 |
* Copyright (c) 2006-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/otfpost.hh> |
| 20 |
#include <lcdf/error.hh> |
| 21 |
#include <errno.h> |
| 22 |
#include <stdlib.h> |
| 23 |
#include <string.h> |
| 24 |
#include <efont/otfdata.hh> // for ntohl() |
| 25 |
|
| 26 |
#define USHORT_AT(d) (Data::u16_aligned(d)) |
| 27 |
#define SHORT_AT(d) (Data::s16_aligned(d)) |
| 28 |
#define ULONG_AT(d) (Data::u32_aligned(d)) |
| 29 |
#define LONG_AT(d) (Data::s32_aligned(d)) |
| 30 |
#define FIXED_AT(d) (Data::fixed_aligned(d)) |
| 31 |
|
| 32 |
namespace Efont { namespace OpenType { |
| 33 |
|
| 34 |
static const char * const mac_names[] = { |
| 35 |
".notdef", ".null", "nonmarkingreturn", "space", // 0-3 |
| 36 |
"exclam", "quotedbl", "numbersign", "dollar", // 4-7 |
| 37 |
"percent", "ampersand", "quotesingle", "parenleft", // 8-11 |
| 38 |
"parenright", "asterisk", "plus", "comma", // 12-15 |
| 39 |
"hyphen", "period", "slash", "zero", // 16-19 |
| 40 |
"one", "two", "three", "four", // 20-23 |
| 41 |
"five", "six", "seven", "eight", // 24-27 |
| 42 |
"nine", "colon", "semicolon", "less", // 28-31 |
| 43 |
"equal", "greater", "question", "at", // 32-35 |
| 44 |
"A", "B", "C", "D", // 36-39 |
| 45 |
"E", "F", "G", "H", // 40-43 |
| 46 |
"I", "J", "K", "L", // 44-47 |
| 47 |
"M", "N", "O", "P", // 48-51 |
| 48 |
"Q", "R", "S", "T", // 52-55 |
| 49 |
"U", "V", "W", "X", // 56-59 |
| 50 |
"Y", "Z", "bracketleft", "backslash", // 60-63 |
| 51 |
"bracketright", "asciicircum", "underscore", "grave", // 64-67 |
| 52 |
"a", "b", "c", "d", // 68-71 |
| 53 |
"e", "f", "g", "h", // 72-75 |
| 54 |
"i", "j", "k", "l", // 76-79 |
| 55 |
"m", "n", "o", "p", // 80-83 |
| 56 |
"q", "r", "s", "t", // 84-87 |
| 57 |
"u", "v", "w", "x", // 88-91 |
| 58 |
"y", "z", "braceleft", "bar", // 92-95 |
| 59 |
"braceright", "asciitilde", "Adieresis", "Aring", // 96-99 |
| 60 |
"Ccedilla", "Eacute", "Ntilde", "Odieresis", // 100-103 |
| 61 |
"Udieresis", "aacute", "agrave", "acircumflex", // 104-107 |
| 62 |
"adieresis", "atilde", "aring", "ccedilla", // 108-111 |
| 63 |
"eacute", "egrave", "ecircumflex", "edieresis", // 112-115 |
| 64 |
"iacute", "igrave", "icircumflex", "idieresis", // 116-119 |
| 65 |
"ntilde", "oacute", "ograve", "ocircumflex", // 120-123 |
| 66 |
"odieresis", "otilde", "uacute", "ugrave", // 124-127 |
| 67 |
"ucircumflex", "udieresis", "dagger", "degree", // 128-131 |
| 68 |
"cent", "sterling", "section", "bullet", // 132-135 |
| 69 |
"paragraph", "germandbls", "registered", "copyright", // 136-139 |
| 70 |
"trademark", "acute", "dieresis", "notequal", // 140-143 |
| 71 |
"AE", "Oslash", "infinity", "plusminus", // 144-147 |
| 72 |
"lessequal", "greaterequal", "yen", "mu", // 148-151 |
| 73 |
"partialdiff", "summation", "product", "pi", // 152-155 |
| 74 |
"integral", "ordfeminine", "ordmasculine", "Omega", // 156-159 |
| 75 |
"ae", "oslash", "questiondown", "exclamdown", // 160-163 |
| 76 |
"logicalnot", "radical", "florin", "approxequal", // 164-167 |
| 77 |
"Delta", "guillemotleft", "guillemotright", "ellipsis", // 168-171 |
| 78 |
"nonbreakingspace", "Agrave", "Atilde", "Otilde", // 172-175 |
| 79 |
"OE", "oe", "endash", "emdash", // 176-179 |
| 80 |
"quotedblleft", "quotedblright", "quoteleft", "quoteright", // 180-183 |
| 81 |
"divide", "lozenge", "ydieresis", "Ydieresis", // 184-187 |
| 82 |
"fraction", "currency", "guilsinglleft", "guilsinglright", // 188-191 |
| 83 |
"fi", "fl", "daggerdbl", "periodcentered", // 192-195 |
| 84 |
"quotesinglbase", "quotedblbase", "perthousand", "Acircumflex", // 196-199 |
| 85 |
"Ecircumflex", "Aacute", "Edieresis", "Egrave", // 200-203 |
| 86 |
"Iacute", "Icircumflex", "Idieresis", "Igrave", // 204-207 |
| 87 |
"Oacute", "Ocircumflex", "apple", "Ograve", // 208-211 |
| 88 |
"Uacute", "Ucircumflex", "Ugrave", "dotlessi", // 212-215 |
| 89 |
"circumflex", "tilde", "macron", "breve", // 216-219 |
| 90 |
"dotaccent", "ring", "cedilla", "hungarumlaut", // 220-223 |
| 91 |
"ogonek", "caron", "Lslash", "lslash", // 224-227 |
| 92 |
"Scaron", "scaron", "Zcaron", "zcaron", // 228-231 |
| 93 |
"brokenbar", "Eth", "eth", "Yacute", // 232-235 |
| 94 |
"yacute", "Thorn", "thorn", "minus", // 236-239 |
| 95 |
"multiply", "onesuperior", "twosuperior", "threesuperior", // 240-243 |
| 96 |
"onehalf", "onequarter", "threequarters", "franc", // 244-247 |
| 97 |
"Gbreve", "gbreve", "Idotaccent", "Scedilla", // 248-251 |
| 98 |
"scedilla", "Cacute", "cacute", "Ccaron", // 252-255 |
| 99 |
"ccaron", "dcroat" // 256-257 |
| 100 |
}; |
| 101 |
|
| 102 |
|
| 103 |
Post::Post(const String &s, ErrorHandler *errh) |
| 104 |
: _str(s), _version(0) |
| 105 |
{ |
| 106 |
_str.align_long(); |
| 107 |
_error = parse_header(errh ? errh : ErrorHandler::silent_handler()); |
| 108 |
} |
| 109 |
|
| 110 |
int |
| 111 |
Post::parse_header(ErrorHandler *errh) |
| 112 |
{ |
| 113 |
// HEADER FORMAT: |
| 114 |
// 0 FIXED version |
| 115 |
// 4 FIXED italicAngle |
| 116 |
// 8 FWORD underlinePosition |
| 117 |
// 10 FWORD underlineThickness |
| 118 |
// 12 ULONG isFixedPitch |
| 119 |
// 16 ULONG minMemType42 |
| 120 |
// 20 ULONG maxMemType42 |
| 121 |
// 24 ULONG minMemType1 |
| 122 |
// 28 ULONG maxMemType1 |
| 123 |
int len = _str.length(); |
| 124 |
const uint8_t *data = _str.udata(); |
| 125 |
if (HEADER_SIZE > len) |
| 126 |
return errh->error("OTF post table too small"), -EFAULT; |
| 127 |
_version = USHORT_AT(data); // ignore minor version number |
| 128 |
// except that version 2.5 isn't compatible |
| 129 |
if (_version < 1 || _version > 3 |
| 130 |
|| (_version == 2 && USHORT_AT(data + 2) == 0x5000)) |
| 131 |
return errh->error("bad post version number"), -ERANGE; |
| 132 |
if (_version == 2) { |
| 133 |
// VERSION 2.0 GLYPH NAMES FORMAT: |
| 134 |
// 32 USHORT numberOfGlyphs |
| 135 |
// 34 USHORT glyphNameIndex[mumberOfGlyphs] |
| 136 |
// CHAR names[...] |
| 137 |
if (HEADER_SIZE + 2 > len |
| 138 |
|| ((_nglyphs = USHORT_AT(data + HEADER_SIZE)), |
| 139 |
HEADER_SIZE + 2 + 2 * _nglyphs > len)) |
| 140 |
return errh->error("OTF post table too small for glyph map"), -EFAULT; |
| 141 |
int pos = HEADER_SIZE + 2 + 2 * _nglyphs; |
| 142 |
while (pos < len && pos + data[pos] < len) { |
| 143 |
_extend_glyph_names.push_back(pos); |
| 144 |
pos += 1 + data[pos]; |
| 145 |
} |
| 146 |
const uint8_t *gni = data + HEADER_SIZE + 2; |
| 147 |
for (int i = 0, g; i < _nglyphs; ++i, gni += 2) |
| 148 |
if ((g = USHORT_AT(gni)) >= _extend_glyph_names.size() + N_MAC_GLYPHS) |
| 149 |
return errh->error("bad glyph name index in post"); |
| 150 |
} else if (_version == 1) |
| 151 |
_nglyphs = N_MAC_GLYPHS; |
| 152 |
else |
| 153 |
_nglyphs = -1; |
| 154 |
|
| 155 |
return 0; |
| 156 |
} |
| 157 |
|
| 158 |
double |
| 159 |
Post::italic_angle() const |
| 160 |
{ |
| 161 |
if (error() < 0) |
| 162 |
return -1; |
| 163 |
return FIXED_AT(_str.udata() + 4); |
| 164 |
} |
| 165 |
|
| 166 |
bool |
| 167 |
Post::is_fixed_pitch() const |
| 168 |
{ |
| 169 |
if (error() < 0) |
| 170 |
return false; |
| 171 |
return ULONG_AT(_str.udata() + 12) != 0; |
| 172 |
} |
| 173 |
|
| 174 |
bool |
| 175 |
Post::glyph_names(Vector<PermString> &gnames) const |
| 176 |
{ |
| 177 |
gnames.clear(); |
| 178 |
if (error() < 0) |
| 179 |
return false; |
| 180 |
if (_version == 1) { |
| 181 |
for (int i = 0; i < N_MAC_GLYPHS; i++) |
| 182 |
gnames.push_back(mac_names[i]); |
| 183 |
return true; |
| 184 |
} else if (_version == 2) { |
| 185 |
const uint8_t *data = _str.udata(); |
| 186 |
const uint8_t *gni = data + HEADER_SIZE + 2; |
| 187 |
for (int i = 0; i < _nglyphs; i++, gni += 2) { |
| 188 |
int g = USHORT_AT(gni); |
| 189 |
if (g < N_MAC_GLYPHS) |
| 190 |
gnames.push_back(mac_names[g]); |
| 191 |
else { |
| 192 |
const uint8_t *n = data + _extend_glyph_names[g - N_MAC_GLYPHS]; |
| 193 |
gnames.push_back(PermString((const char *) n + 1, *n)); |
| 194 |
} |
| 195 |
} |
| 196 |
return true; |
| 197 |
} else |
| 198 |
return false; |
| 199 |
} |
| 200 |
|
| 201 |
}} |