| 1 |
// -*- related-file-name: "../include/efont/otf.hh" -*- |
| 2 |
|
| 3 |
/* otf.{cc,hh} -- OpenType font basics |
| 4 |
* |
| 5 |
* Copyright (c) 2002-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/otf.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 |
#include <efont/otfdata.hh> // for ntohl() |
| 27 |
#include <efont/otfname.hh> |
| 28 |
#include <efont/ttfhead.hh> |
| 29 |
|
| 30 |
namespace Efont { namespace OpenType { |
| 31 |
|
| 32 |
Vector<PermString> debug_glyph_names; |
| 33 |
|
| 34 |
Font::Font(const String& s, ErrorHandler* errh) |
| 35 |
: _str(s), _units_per_em(0) { |
| 36 |
_str.align(4); |
| 37 |
_error = parse_header(errh ? errh : ErrorHandler::silent_handler()); |
| 38 |
} |
| 39 |
|
| 40 |
int |
| 41 |
Font::parse_header(ErrorHandler *errh) |
| 42 |
{ |
| 43 |
// HEADER FORMAT: |
| 44 |
// Fixed sfnt version |
| 45 |
// USHORT numTables |
| 46 |
// USHORT searchRange |
| 47 |
// USHORT entrySelector |
| 48 |
// USHORT rangeShift |
| 49 |
int len = length(); |
| 50 |
const uint8_t *data = this->data(); |
| 51 |
if (HEADER_SIZE > len) |
| 52 |
return errh->error("OTF file corrupted (too small)"), -EFAULT; |
| 53 |
if ((data[0] != 'O' || data[1] != 'T' || data[2] != 'T' || data[3] != 'O') |
| 54 |
&& (data[0] != '\000' || data[1] != '\001')) |
| 55 |
return errh->error("not an OpenType font (bad magic number)"), -ERANGE; |
| 56 |
int ntables = Data::u16_aligned(data + 4); |
| 57 |
if (ntables == 0) |
| 58 |
return errh->error("OTF contains no tables"), -EINVAL; |
| 59 |
if (HEADER_SIZE + TABLE_DIR_ENTRY_SIZE * ntables > len) |
| 60 |
return errh->error("OTF table directory out of range"), -EFAULT; |
| 61 |
|
| 62 |
// TABLE DIRECTORY ENTRY FORMAT: |
| 63 |
// ULONG tag |
| 64 |
// ULONG checksum |
| 65 |
// ULONG offset |
| 66 |
// ULONG length |
| 67 |
uint32_t last_tag = 0U; |
| 68 |
for (int i = 0; i < ntables; i++) { |
| 69 |
int loc = HEADER_SIZE + TABLE_DIR_ENTRY_SIZE * i; |
| 70 |
uint32_t tag = Data::u32_aligned(data + loc); |
| 71 |
uint32_t offset = Data::u32_aligned(data + loc + 8); |
| 72 |
uint32_t length = Data::u32_aligned(data + loc + 12); |
| 73 |
if (tag <= last_tag) |
| 74 |
return errh->error("tags out of order"), -EINVAL; |
| 75 |
if (offset + length > (uint32_t) len) |
| 76 |
return errh->error("OTF data for %<%s%> out of range", Tag(tag).text().c_str()), -EFAULT; |
| 77 |
if (Tag::head_tag() == tag) { |
| 78 |
Head head(_str.substring(offset, length)); |
| 79 |
_units_per_em = head.units_per_em(); |
| 80 |
} |
| 81 |
last_tag = tag; |
| 82 |
} |
| 83 |
|
| 84 |
return 0; |
| 85 |
} |
| 86 |
|
| 87 |
bool |
| 88 |
Font::check_checksums(ErrorHandler *errh) const |
| 89 |
{ |
| 90 |
if (error() < 0) |
| 91 |
return false; |
| 92 |
int nt = ntables(); |
| 93 |
bool ok = true; |
| 94 |
for (int i = 0; i < nt; i++) { |
| 95 |
const uint8_t *entry = data() + HEADER_SIZE + TABLE_DIR_ENTRY_SIZE * i; |
| 96 |
String tbl = _str.substring(Data::u32_aligned(entry + 8), |
| 97 |
Data::u32_aligned(entry + 12)); |
| 98 |
uint32_t sum = checksum(tbl); |
| 99 |
if (Data::u32_aligned(entry) == 0x68656164 // 'head' |
| 100 |
&& tbl.length() >= 12) |
| 101 |
sum -= Data::u32_aligned(tbl.udata() + 8); |
| 102 |
if (sum != Data::u32_aligned(entry + 4)) { |
| 103 |
if (errh) |
| 104 |
errh->error("table %<%s%> checksum error: %x vs. %x", Tag(Data::u32_aligned(entry)).text().c_str(), sum, Data::u32_aligned(entry + 4)); |
| 105 |
ok = false; |
| 106 |
} |
| 107 |
} |
| 108 |
return ok; |
| 109 |
} |
| 110 |
|
| 111 |
int |
| 112 |
Font::ntables() const |
| 113 |
{ |
| 114 |
if (error() < 0) |
| 115 |
return 0; |
| 116 |
else |
| 117 |
return Data::u16_aligned(data() + 4); |
| 118 |
} |
| 119 |
|
| 120 |
String |
| 121 |
Font::table(Tag tag) const |
| 122 |
{ |
| 123 |
if (error() < 0) |
| 124 |
return String(); |
| 125 |
const uint8_t *entry = tag.table_entry(data() + HEADER_SIZE, Data::u16_aligned(data() + 4), TABLE_DIR_ENTRY_SIZE); |
| 126 |
if (entry) |
| 127 |
return _str.substring(Data::u32_aligned(entry + 8), Data::u32_aligned(entry + 12)); |
| 128 |
else |
| 129 |
return String(); |
| 130 |
} |
| 131 |
|
| 132 |
bool |
| 133 |
Font::has_table(Tag tag) const |
| 134 |
{ |
| 135 |
const uint8_t *entry = 0; |
| 136 |
if (error() >= 0) |
| 137 |
entry = tag.table_entry(data() + HEADER_SIZE, Data::u16_aligned(data() + 4), TABLE_DIR_ENTRY_SIZE); |
| 138 |
return entry != 0; |
| 139 |
} |
| 140 |
|
| 141 |
uint32_t |
| 142 |
Font::table_checksum(Tag tag) const |
| 143 |
{ |
| 144 |
if (error() < 0) |
| 145 |
return 0; |
| 146 |
const uint8_t *entry = tag.table_entry(data() + HEADER_SIZE, Data::u16_aligned(data() + 4), TABLE_DIR_ENTRY_SIZE); |
| 147 |
if (entry) |
| 148 |
return Data::u32_aligned(entry + 4); |
| 149 |
else |
| 150 |
return 0; |
| 151 |
} |
| 152 |
|
| 153 |
Tag |
| 154 |
Font::table_tag(int i) const |
| 155 |
{ |
| 156 |
if (error() < 0 || i < 0 || i >= ntables()) |
| 157 |
return Tag(); |
| 158 |
else |
| 159 |
return Tag(Data::u32_aligned(data() + HEADER_SIZE + TABLE_DIR_ENTRY_SIZE * i)); |
| 160 |
} |
| 161 |
|
| 162 |
uint32_t |
| 163 |
Font::checksum(const uint8_t *begin, const uint8_t *end) |
| 164 |
{ |
| 165 |
uint32_t sum = 0; |
| 166 |
if (reinterpret_cast<uintptr_t>(begin) % 4) |
| 167 |
for (; begin + 3 < end; begin += 4) |
| 168 |
sum += Data::u32(begin); |
| 169 |
else |
| 170 |
for (; begin + 3 < end; begin += 4) |
| 171 |
sum += Data::u32_aligned(begin); |
| 172 |
uint32_t leftover = 0; |
| 173 |
for (int i = 0; i < 4; i++) |
| 174 |
leftover = (leftover << 8) + (begin < end ? *begin++ : 0); |
| 175 |
return sum + leftover; |
| 176 |
} |
| 177 |
|
| 178 |
uint32_t |
| 179 |
Font::checksum(const String &s) |
| 180 |
{ |
| 181 |
return checksum(reinterpret_cast<const uint8_t *>(s.begin()), reinterpret_cast<const uint8_t *>(s.end())); |
| 182 |
} |
| 183 |
|
| 184 |
namespace { |
| 185 |
class TagCompar { public: |
| 186 |
TagCompar(const Vector<Tag> &tags) : _tags(tags) { } |
| 187 |
bool operator()(int a, int b) { return _tags[a] < _tags[b]; } |
| 188 |
const Vector<Tag> &_tags; |
| 189 |
}; |
| 190 |
} |
| 191 |
|
| 192 |
Font |
| 193 |
Font::make(bool truetype, const Vector<Tag>& tags, const Vector<String>& data) |
| 194 |
{ |
| 195 |
StringAccum sa; |
| 196 |
|
| 197 |
// create offset table |
| 198 |
{ |
| 199 |
union { |
| 200 |
uint8_t c[HEADER_SIZE]; |
| 201 |
uint16_t s[HEADER_SIZE / 2]; |
| 202 |
uint32_t l[HEADER_SIZE / 4]; |
| 203 |
} hdr; |
| 204 |
hdr.l[0] = (truetype ? htonl(0x00010000) : htonl(0x4F54544F)); |
| 205 |
hdr.s[2] = htons(tags.size()); |
| 206 |
int entrySelector; |
| 207 |
for (entrySelector = 0; (2 << entrySelector) <= tags.size(); entrySelector++) |
| 208 |
/* nada */; |
| 209 |
hdr.s[3] = htons((1 << entrySelector) * 16); |
| 210 |
hdr.s[4] = htons(entrySelector); |
| 211 |
hdr.s[5] = htons((tags.size() - (1 << entrySelector)) * 16); |
| 212 |
sa.append(&hdr.c[0], HEADER_SIZE); |
| 213 |
} |
| 214 |
|
| 215 |
// sort tags |
| 216 |
Vector<int> permut; |
| 217 |
for (int i = 0; i < tags.size(); i++) |
| 218 |
permut.push_back(i); |
| 219 |
std::sort(permut.begin(), permut.end(), TagCompar(tags)); |
| 220 |
|
| 221 |
// table listing |
| 222 |
uint32_t offset = HEADER_SIZE + TABLE_DIR_ENTRY_SIZE * tags.size(); |
| 223 |
for (int *tp = permut.begin(); tp < permut.end(); tp++) { |
| 224 |
union { |
| 225 |
uint8_t c[TABLE_DIR_ENTRY_SIZE]; |
| 226 |
uint32_t l[TABLE_DIR_ENTRY_SIZE / 4]; |
| 227 |
} tdir; |
| 228 |
tdir.l[0] = htonl(tags[*tp].value()); |
| 229 |
|
| 230 |
// discount current checksum adjustment in head table |
| 231 |
uint32_t sum = checksum(data[*tp]); |
| 232 |
if (tags[*tp] == Tag("head") && data[*tp].length() >= 12) |
| 233 |
sum -= Data::u32(data[*tp].udata() + 8); |
| 234 |
tdir.l[1] = htonl(sum); |
| 235 |
|
| 236 |
tdir.l[2] = htonl(offset); |
| 237 |
tdir.l[3] = htonl(data[*tp].length()); |
| 238 |
sa.append(&tdir.c[0], TABLE_DIR_ENTRY_SIZE); |
| 239 |
offset += (data[*tp].length() + 3) & ~3; |
| 240 |
} |
| 241 |
|
| 242 |
// actual tables |
| 243 |
for (int *tp = permut.begin(); tp < permut.end(); tp++) { |
| 244 |
sa << data[*tp]; |
| 245 |
while (sa.length() % 4) |
| 246 |
sa << '\0'; |
| 247 |
} |
| 248 |
|
| 249 |
// fix 'head' table |
| 250 |
for (int i = 0; i < tags.size(); i++) { |
| 251 |
unsigned char *thdr = sa.udata() + HEADER_SIZE + TABLE_DIR_ENTRY_SIZE * i; |
| 252 |
if (Data::u32(thdr) == 0x68656164 && Data::u32(thdr + 12) >= 12) { |
| 253 |
uint32_t offset = Data::u32(thdr + 8); |
| 254 |
char *head = sa.data() + offset; |
| 255 |
memset(head + 8, '\0', 4); |
| 256 |
uint32_t allsum = checksum(reinterpret_cast<uint8_t *>(sa.data()), reinterpret_cast<uint8_t *>(sa.data() + sa.length())); |
| 257 |
uint32_t *adj = reinterpret_cast<uint32_t *>(head + 8); |
| 258 |
*adj = htonl(0xB1B0AFBA - allsum); |
| 259 |
} |
| 260 |
} |
| 261 |
|
| 262 |
return Font(sa.take_string()); |
| 263 |
} |
| 264 |
|
| 265 |
|
| 266 |
/************************** |
| 267 |
* Tag * |
| 268 |
* * |
| 269 |
**************************/ |
| 270 |
|
| 271 |
Tag::Tag(const char *s) |
| 272 |
: _tag(0) |
| 273 |
{ |
| 274 |
if (!s) |
| 275 |
s = ""; |
| 276 |
for (int i = 0; i < 4; i++) |
| 277 |
if (*s == 0) |
| 278 |
_tag = (_tag << 8) | 0x20; |
| 279 |
else if (*s < 32 || *s > 126) { // don't care if s is signed |
| 280 |
_tag = 0; |
| 281 |
return; |
| 282 |
} else |
| 283 |
_tag = (_tag << 8) | *s++; |
| 284 |
if (*s) |
| 285 |
_tag = 0; |
| 286 |
} |
| 287 |
|
| 288 |
Tag::Tag(const String &s) |
| 289 |
: _tag(0) |
| 290 |
{ |
| 291 |
if (s.length() <= 4) { |
| 292 |
const char *ss = s.data(); |
| 293 |
for (int i = 0; i < s.length(); i++, ss++) |
| 294 |
if (*ss < 32 || *ss > 126) { |
| 295 |
_tag = 0; |
| 296 |
return; |
| 297 |
} else |
| 298 |
_tag = (_tag << 8) | *ss; |
| 299 |
for (int i = s.length(); i < 4; i++) |
| 300 |
_tag = (_tag << 8) | 0x20; |
| 301 |
} |
| 302 |
} |
| 303 |
|
| 304 |
bool |
| 305 |
Tag::valid() const |
| 306 |
{ |
| 307 |
uint32_t tag = _tag; |
| 308 |
for (int i = 0; i < 4; i++, tag >>= 8) |
| 309 |
if ((tag & 255) < 32 || (tag & 255) > 126) |
| 310 |
return false; |
| 311 |
return true; |
| 312 |
} |
| 313 |
|
| 314 |
String |
| 315 |
Tag::text() const |
| 316 |
{ |
| 317 |
StringAccum sa; |
| 318 |
uint32_t tag = _tag; |
| 319 |
for (int i = 0; i < 4; i++, tag = (tag << 8) | 0x20) |
| 320 |
if (tag != 0x20202020) { |
| 321 |
uint8_t c = (tag >> 24) & 255; |
| 322 |
if (c < 32 || c > 126) |
| 323 |
sa.snprintf(6, "\\%03o", c); |
| 324 |
else |
| 325 |
sa << c; |
| 326 |
} |
| 327 |
return sa.take_string(); |
| 328 |
} |
| 329 |
|
| 330 |
String |
| 331 |
Tag::langsys_text(Tag script, Tag langsys) |
| 332 |
{ |
| 333 |
if (!langsys.null()) |
| 334 |
return script.text() + "." + langsys.text(); |
| 335 |
else |
| 336 |
return script.text(); |
| 337 |
} |
| 338 |
|
| 339 |
const uint8_t * |
| 340 |
Tag::table_entry(const uint8_t *table, int n, int entry_size) const |
| 341 |
{ |
| 342 |
assert(((uintptr_t)table & 1) == 0); |
| 343 |
int l = 0, r = n; |
| 344 |
while (l < r) { |
| 345 |
int m = l + (r - l) / 2; |
| 346 |
const uint8_t *entry = table + m * entry_size; |
| 347 |
uint32_t m_tag = Data::u32_aligned16(entry); |
| 348 |
if (_tag < m_tag) |
| 349 |
r = m; |
| 350 |
else if (_tag == m_tag) |
| 351 |
return entry; |
| 352 |
else |
| 353 |
l = m + 1; |
| 354 |
} |
| 355 |
return 0; |
| 356 |
} |
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
/************************** |
| 361 |
* ScriptList * |
| 362 |
* * |
| 363 |
**************************/ |
| 364 |
|
| 365 |
int |
| 366 |
ScriptList::assign(const String &str, ErrorHandler *errh) |
| 367 |
{ |
| 368 |
_str = str; |
| 369 |
_str.align(4); |
| 370 |
int result = check_header(errh ? errh : ErrorHandler::silent_handler()); |
| 371 |
if (result < 0) |
| 372 |
_str = String(); |
| 373 |
return result; |
| 374 |
} |
| 375 |
|
| 376 |
int |
| 377 |
ScriptList::check_header(ErrorHandler *errh) |
| 378 |
{ |
| 379 |
// HEADER FORMAT: |
| 380 |
// USHORT scriptCount |
| 381 |
// 6bytes scriptRecord[] |
| 382 |
int scriptCount; |
| 383 |
if (_str.length() < SCRIPTLIST_HEADERSIZE |
| 384 |
|| (scriptCount = Data::u16_aligned(_str.udata()), |
| 385 |
_str.length() < SCRIPTLIST_HEADERSIZE + scriptCount*SCRIPT_RECSIZE)) |
| 386 |
return errh->error("OTF ScriptList too short"); |
| 387 |
|
| 388 |
// XXX check that scripts are sorted |
| 389 |
|
| 390 |
return 0; |
| 391 |
} |
| 392 |
|
| 393 |
int |
| 394 |
ScriptList::script_offset(Tag script) const |
| 395 |
{ |
| 396 |
if (_str.length() == 0) |
| 397 |
return -1; |
| 398 |
const uint8_t *data = _str.udata(); |
| 399 |
if (const uint8_t *entry = script.table_entry(data + SCRIPTLIST_HEADERSIZE, Data::u16_aligned(data), SCRIPT_RECSIZE)) |
| 400 |
return Data::u16_aligned(entry + 4); |
| 401 |
else |
| 402 |
return 0; |
| 403 |
} |
| 404 |
|
| 405 |
int |
| 406 |
ScriptList::check_script(Tag tag, int script_off, ErrorHandler *errh) const |
| 407 |
{ |
| 408 |
const uint8_t *data = _str.udata(); |
| 409 |
int langSysCount; |
| 410 |
if (_str.length() < script_off + SCRIPT_HEADERSIZE |
| 411 |
|| (langSysCount = Data::u16_aligned(data + script_off + 2), |
| 412 |
(_str.length() < script_off + SCRIPT_HEADERSIZE + langSysCount*LANGSYS_RECSIZE))) |
| 413 |
return (errh ? errh->error("OTF Script table for %<%s%> out of range", tag.text().c_str()) : -1); |
| 414 |
// XXX check that langsys are sorted |
| 415 |
return 0; |
| 416 |
} |
| 417 |
|
| 418 |
int |
| 419 |
ScriptList::langsys_offset(Tag script, Tag langsys, ErrorHandler *errh) const |
| 420 |
{ |
| 421 |
int script_off = script_offset(script); |
| 422 |
if (script_off == 0) { |
| 423 |
script = Tag("DFLT"); |
| 424 |
script_off = script_offset(script); |
| 425 |
} |
| 426 |
if (script_off <= 0) |
| 427 |
return script_off; |
| 428 |
|
| 429 |
// check script bounds |
| 430 |
if (check_script(script, script_off, errh) < 0) |
| 431 |
return -1; |
| 432 |
|
| 433 |
// search script table |
| 434 |
const uint8_t *data = _str.udata(); |
| 435 |
int langSysCount = Data::u16_aligned(data + script_off + 2); |
| 436 |
if (const uint8_t *entry = langsys.table_entry(data + script_off + SCRIPT_HEADERSIZE, langSysCount, LANGSYS_RECSIZE)) |
| 437 |
return script_off + Data::u16_aligned(entry + 4); |
| 438 |
|
| 439 |
// return default |
| 440 |
int defaultLangSys = Data::u16_aligned(data + script_off); |
| 441 |
if (defaultLangSys != 0) |
| 442 |
return script_off + defaultLangSys; |
| 443 |
else |
| 444 |
return 0; |
| 445 |
} |
| 446 |
|
| 447 |
int |
| 448 |
ScriptList::language_systems(Vector<Tag> &script, Vector<Tag> &langsys, ErrorHandler *errh) const |
| 449 |
{ |
| 450 |
script.clear(); |
| 451 |
langsys.clear(); |
| 452 |
|
| 453 |
const uint8_t *data = _str.udata(); |
| 454 |
int nscripts = Data::u16_aligned(data); |
| 455 |
for (int i = 0; i < nscripts; i++) { |
| 456 |
Tag script_tag(Data::u32_aligned16(data + SCRIPTLIST_HEADERSIZE + i*SCRIPT_RECSIZE)); |
| 457 |
int script_off = Data::u16_aligned(data + SCRIPTLIST_HEADERSIZE + i*SCRIPT_RECSIZE + 4); |
| 458 |
if (check_script(script_tag, script_off, errh) < 0) |
| 459 |
return -1; |
| 460 |
const uint8_t *script_table = data + script_off; |
| 461 |
if (Data::u16_aligned(script_table) != 0) // default LangSys |
| 462 |
script.push_back(script_tag), langsys.push_back(Tag()); |
| 463 |
int nlangsys = Data::u16_aligned(script_table + 2); |
| 464 |
for (int j = 0; j < nlangsys; j++) { |
| 465 |
Tag langsys_tag(Data::u32_aligned16(script_table + SCRIPT_HEADERSIZE + j*LANGSYS_RECSIZE)); |
| 466 |
script.push_back(script_tag), langsys.push_back(langsys_tag); |
| 467 |
} |
| 468 |
} |
| 469 |
|
| 470 |
return 0; |
| 471 |
} |
| 472 |
|
| 473 |
int |
| 474 |
ScriptList::features(Tag script, Tag langsys, int &required_fid, Vector<int> &fids, ErrorHandler *errh, bool clear_fids) const |
| 475 |
{ |
| 476 |
required_fid = -1; |
| 477 |
if (clear_fids) |
| 478 |
fids.clear(); |
| 479 |
|
| 480 |
int offset = langsys_offset(script, langsys); |
| 481 |
if (offset <= 0) |
| 482 |
return offset; |
| 483 |
|
| 484 |
// check langsys bounds |
| 485 |
const uint8_t *data = _str.udata(); |
| 486 |
int featureCount; |
| 487 |
if (_str.length() < offset + LANGSYS_HEADERSIZE |
| 488 |
|| (featureCount = Data::u16_aligned(data + offset + 4), |
| 489 |
(_str.length() < offset + LANGSYS_HEADERSIZE + featureCount*FEATURE_RECSIZE))) |
| 490 |
return (errh ? errh->error("OTF LangSys table for %<%s/%s%> out of range", script.text().c_str(), langsys.text().c_str()) : -1); |
| 491 |
|
| 492 |
// search langsys table |
| 493 |
int f = Data::u16_aligned(data + offset + 2); |
| 494 |
if (f != 0xFFFF) |
| 495 |
required_fid = f; |
| 496 |
data += offset + 6; |
| 497 |
for (int i = 0; i < featureCount; i++, data += FEATURE_RECSIZE) |
| 498 |
fids.push_back(Data::u16_aligned(data)); |
| 499 |
|
| 500 |
return 0; |
| 501 |
} |
| 502 |
|
| 503 |
|
| 504 |
|
| 505 |
/************************** |
| 506 |
* FeatureList * |
| 507 |
* * |
| 508 |
**************************/ |
| 509 |
|
| 510 |
int |
| 511 |
FeatureList::assign(const String &str, ErrorHandler *errh) |
| 512 |
{ |
| 513 |
_str = str; |
| 514 |
_str.align(2); |
| 515 |
int result = check_header(errh ? errh : ErrorHandler::silent_handler()); |
| 516 |
if (result < 0) |
| 517 |
_str = String(); |
| 518 |
return result; |
| 519 |
} |
| 520 |
|
| 521 |
int |
| 522 |
FeatureList::check_header(ErrorHandler *errh) |
| 523 |
{ |
| 524 |
int featureCount; |
| 525 |
if (_str.length() < FEATURELIST_HEADERSIZE |
| 526 |
|| (featureCount = Data::u16_aligned(_str.udata()), |
| 527 |
_str.length() < FEATURELIST_HEADERSIZE + featureCount*FEATURE_RECSIZE)) |
| 528 |
return errh->error("OTF FeatureList too short"); |
| 529 |
return 0; |
| 530 |
} |
| 531 |
|
| 532 |
Tag |
| 533 |
FeatureList::tag(int fid) const |
| 534 |
{ |
| 535 |
if (_str.length() == 0) |
| 536 |
return Tag(); |
| 537 |
const uint8_t *data = _str.udata(); |
| 538 |
int nfeatures = Data::u16_aligned(data); |
| 539 |
if (fid >= 0 && fid < nfeatures) |
| 540 |
return Tag(Data::u32_aligned16(data + FEATURELIST_HEADERSIZE + fid*FEATURE_RECSIZE)); |
| 541 |
else |
| 542 |
return Tag(); |
| 543 |
} |
| 544 |
|
| 545 |
String |
| 546 |
FeatureList::params(int fid, int length, ErrorHandler *errh, bool old_style_offset) const |
| 547 |
{ |
| 548 |
if (_str.length() == 0 || length < 0) |
| 549 |
return String(); |
| 550 |
if (errh == 0) |
| 551 |
errh = ErrorHandler::silent_handler(); |
| 552 |
|
| 553 |
const uint8_t *data = _str.udata(); |
| 554 |
int len = _str.length(); |
| 555 |
int nfeatures = Data::u16_aligned(data); |
| 556 |
if (fid < 0 || fid >= nfeatures) |
| 557 |
return errh->error("OTF feature ID %<%d%> out of range", fid), String(); |
| 558 |
int foff = Data::u16_aligned(data + FEATURELIST_HEADERSIZE + fid*FEATURE_RECSIZE + 4); |
| 559 |
if (len < foff + FEATURE_HEADERSIZE) |
| 560 |
return errh->error("OTF LookupList for feature ID %<%d%> too short", fid), String(); |
| 561 |
int poff = Data::u16_aligned(data + foff); |
| 562 |
if (poff == 0) |
| 563 |
return String(); |
| 564 |
if (!old_style_offset) |
| 565 |
poff += foff; |
| 566 |
if (len < poff + length) |
| 567 |
return errh->error("OTF feature parameters for feature ID %<%d%> out of range", fid), String(); |
| 568 |
else |
| 569 |
return _str.substring(poff, length); |
| 570 |
} |
| 571 |
|
| 572 |
String |
| 573 |
FeatureList::size_params(int fid, const Name &name, ErrorHandler *errh) const |
| 574 |
{ |
| 575 |
// implement 'size' checks from Read Roberts |
| 576 |
for (int i = 0; i < 2; i++) { |
| 577 |
String s = params(fid, 10, errh, i != 0); |
| 578 |
const uint8_t *data = s.udata(); |
| 579 |
// errh->message("trying %d %d %d %d %d\n", Data::u16_aligned(data), Data::u16_aligned(data + 2), Data::u16_aligned(data + 4), Data::u16_aligned(data + 6), Data::u16_aligned(data + 8)); |
| 580 |
if (Data::u16_aligned(data) == 0) // design size == 0 |
| 581 |
continue; |
| 582 |
if (Data::u16_aligned(data + 2) == 0 // subfamily ID == 0 |
| 583 |
&& Data::u16_aligned(data + 6) == 0 // range start == 0 |
| 584 |
&& Data::u16_aligned(data + 8) == 0 // range end == 0 |
| 585 |
&& Data::u16_aligned(data + 4) == 0) // menu name ID == 0 |
| 586 |
return s; |
| 587 |
if (Data::u16_aligned(data + 6) >= Data::u16_aligned(data + 8) // range start >= range end |
| 588 |
|| Data::u16_aligned(data + 4) < 256 // menu name ID < 256 |
| 589 |
|| Data::u16_aligned(data + 4) > 32767 // menu name ID > 32767 |
| 590 |
|| !name.english_name(Data::u16_aligned(data + 4))) // menu name ID is a name ID defined by the font |
| 591 |
continue; |
| 592 |
if (Data::u16_aligned(data) + 1 >= Data::u16_aligned(data + 6) // design size >= range start (with 1 dp grace) |
| 593 |
&& Data::u16_aligned(data) <= Data::u16_aligned(data + 8) + 1) // design size <= range end (with 1 dp grace) |
| 594 |
return s; |
| 595 |
else if (i == 1 // old-style feature |
| 596 |
&& Data::u16_aligned(data + 8) <= 1440 // range end <= 144 point |
| 597 |
&& Data::u16_aligned(data) <= 1440) { // design size <= 144 point |
| 598 |
// some old fonts define a bogus feature with design size |
| 599 |
// not in range (John Owens, Read Roberts) |
| 600 |
if (errh) |
| 601 |
errh->warning("invalid 'size' feature: design size not in range"); |
| 602 |
return s; |
| 603 |
} |
| 604 |
} |
| 605 |
if (errh) |
| 606 |
errh->error("no valid 'size' feature data in the 'size' feature"); |
| 607 |
return String(); |
| 608 |
} |
| 609 |
|
| 610 |
int |
| 611 |
FeatureList::find(Tag tag, const Vector<int> &fids) const |
| 612 |
{ |
| 613 |
if (fids.size() == 0 || _str.length() == 0) |
| 614 |
return -1; |
| 615 |
|
| 616 |
const uint8_t *data = _str.udata(); |
| 617 |
int nfeatures = Data::u16_aligned(data); |
| 618 |
for (const int *fidp = fids.begin(); fidp != fids.end(); fidp++) |
| 619 |
if (*fidp >= 0 && *fidp < nfeatures) { |
| 620 |
uint32_t ftag = Data::u32_aligned16(data + FEATURELIST_HEADERSIZE + (*fidp)*FEATURE_RECSIZE); |
| 621 |
if (ftag == tag.value()) |
| 622 |
return *fidp; |
| 623 |
} |
| 624 |
|
| 625 |
return -1; |
| 626 |
} |
| 627 |
|
| 628 |
void |
| 629 |
FeatureList::filter(Vector<int> &fids, const Vector<Tag> &sorted_ftags) const |
| 630 |
{ |
| 631 |
if (_str.length() == 0) |
| 632 |
fids.clear(); |
| 633 |
else { |
| 634 |
std::sort(fids.begin(), fids.end()); // sort fids |
| 635 |
|
| 636 |
int i = 0, j = 0; |
| 637 |
while (i < fids.size() && fids[i] < 0) |
| 638 |
fids[i++] = 0x7FFFFFFF; |
| 639 |
|
| 640 |
// XXX check that feature list is in alphabetical order |
| 641 |
|
| 642 |
const uint8_t *data = _str.udata(); |
| 643 |
int nfeatures = Data::u16_aligned(data); |
| 644 |
while (i < fids.size() && j < sorted_ftags.size() && fids[i] < nfeatures) { |
| 645 |
uint32_t ftag = Data::u32_aligned16(data + FEATURELIST_HEADERSIZE + fids[i]*FEATURE_RECSIZE); |
| 646 |
if (ftag < sorted_ftags[j].value()) { // not an interesting feature |
| 647 |
// replace featureID with a large number, remove later |
| 648 |
fids[i] = 0x7FFFFFFF; |
| 649 |
i++; |
| 650 |
} else if (ftag == sorted_ftags[j].value()) // interesting feature |
| 651 |
i++; |
| 652 |
else // an interesting feature is not available |
| 653 |
j++; |
| 654 |
} |
| 655 |
|
| 656 |
fids.resize(i); // remove remaining uninteresting features |
| 657 |
std::sort(fids.begin(), fids.end()); // resort, to move bad ones last |
| 658 |
while (fids.size() && fids.back() == 0x7FFFFFFF) |
| 659 |
fids.pop_back(); |
| 660 |
} |
| 661 |
} |
| 662 |
|
| 663 |
int |
| 664 |
FeatureList::lookups(int fid, Vector<int> &results, ErrorHandler *errh, bool clear_results) const |
| 665 |
{ |
| 666 |
if (clear_results) |
| 667 |
results.clear(); |
| 668 |
if (_str.length() == 0) |
| 669 |
return -1; |
| 670 |
if (errh == 0) |
| 671 |
errh = ErrorHandler::silent_handler(); |
| 672 |
|
| 673 |
const uint8_t *data = _str.udata(); |
| 674 |
int len = _str.length(); |
| 675 |
int nfeatures = Data::u16_aligned(data); |
| 676 |
if (fid < 0 || fid >= nfeatures) |
| 677 |
return errh->error("OTF feature ID %<%d%> out of range", fid); |
| 678 |
int foff = Data::u16_aligned(data + FEATURELIST_HEADERSIZE + fid*FEATURE_RECSIZE + 4); |
| 679 |
int lookupCount; |
| 680 |
if (len < foff + FEATURE_HEADERSIZE |
| 681 |
|| (lookupCount = Data::u16_aligned(data + foff + 2), |
| 682 |
len < foff + FEATURE_HEADERSIZE + lookupCount*LOOKUPLIST_RECSIZE)) |
| 683 |
return errh->error("OTF LookupList for feature ID %<%d%> too short", fid); |
| 684 |
const uint8_t *ldata = data + foff + FEATURE_HEADERSIZE; |
| 685 |
for (int j = 0; j < lookupCount; j++, ldata += LOOKUPLIST_RECSIZE) |
| 686 |
results.push_back(Data::u16_aligned(ldata)); |
| 687 |
|
| 688 |
return 0; |
| 689 |
} |
| 690 |
|
| 691 |
int |
| 692 |
FeatureList::lookups(const Vector<int> &fids, Vector<int> &results, ErrorHandler *errh) const |
| 693 |
{ |
| 694 |
results.clear(); |
| 695 |
if (_str.length() == 0) |
| 696 |
return -1; |
| 697 |
|
| 698 |
for (int i = 0; i < fids.size(); i++) |
| 699 |
if (lookups(fids[i], results, errh, false) < 0) |
| 700 |
return -1; |
| 701 |
|
| 702 |
// sort results and remove duplicates |
| 703 |
std::sort(results.begin(), results.end()); |
| 704 |
int *unique_result = std::unique(results.begin(), results.end()); |
| 705 |
results.resize(unique_result - results.begin()); |
| 706 |
return 0; |
| 707 |
} |
| 708 |
|
| 709 |
int |
| 710 |
FeatureList::lookups(const Vector<int> &required_fids, const Vector<int> &fids, const Vector<Tag> &sorted_ftags, Vector<int> &results, ErrorHandler *errh) const |
| 711 |
{ |
| 712 |
Vector<int> fidsx(fids); |
| 713 |
filter(fidsx, sorted_ftags); |
| 714 |
for (int i = 0; i < required_fids.size(); i++) |
| 715 |
fidsx.push_back(required_fids[i]); |
| 716 |
return lookups(fidsx, results, errh); |
| 717 |
} |
| 718 |
|
| 719 |
int |
| 720 |
FeatureList::lookups(int required_fid, const Vector<int> &fids, const Vector<Tag> &sorted_ftags, Vector<int> &results, ErrorHandler *errh) const |
| 721 |
{ |
| 722 |
Vector<int> fidsx(fids); |
| 723 |
filter(fidsx, sorted_ftags); |
| 724 |
if (required_fid >= 0) |
| 725 |
fidsx.push_back(required_fid); |
| 726 |
return lookups(fidsx, results, errh); |
| 727 |
} |
| 728 |
|
| 729 |
int |
| 730 |
FeatureList::lookups(const ScriptList &script_list, Tag script, Tag langsys, const Vector<Tag> &sorted_ftags, Vector<int> &results, ErrorHandler *errh) const |
| 731 |
{ |
| 732 |
int required_fid; |
| 733 |
Vector<int> fids; |
| 734 |
int result = script_list.features(script, langsys, required_fid, fids, errh); |
| 735 |
if (result >= 0) { |
| 736 |
filter(fids, sorted_ftags); |
| 737 |
if (required_fid >= 0) |
| 738 |
fids.push_back(required_fid); |
| 739 |
result = lookups(fids, results, errh); |
| 740 |
} |
| 741 |
return result; |
| 742 |
} |
| 743 |
|
| 744 |
|
| 745 |
/************************** |
| 746 |
* Coverage * |
| 747 |
* * |
| 748 |
**************************/ |
| 749 |
|
| 750 |
Coverage::Coverage() noexcept |
| 751 |
{ |
| 752 |
} |
| 753 |
|
| 754 |
Coverage::Coverage(Glyph first, Glyph last) noexcept |
| 755 |
{ |
| 756 |
if (first <= last) { |
| 757 |
_str = String("\000\002\000\001\000\000\000\000\000\000", 10); |
| 758 |
uint8_t *data = _str.mutable_udata(); |
| 759 |
data[4] = first >> 8; |
| 760 |
data[5] = first & 255; |
| 761 |
data[6] = last >> 8; |
| 762 |
data[7] = last & 255; |
| 763 |
_str.align(2); |
| 764 |
} |
| 765 |
} |
| 766 |
|
| 767 |
Coverage::Coverage(const Vector<bool> &gmap) noexcept |
| 768 |
{ |
| 769 |
int end = gmap.size(); |
| 770 |
while (end > 0 && !gmap[end - 1]) |
| 771 |
--end; |
| 772 |
if (end > 0) { |
| 773 |
_str = String::make_uninitialized(8 + end); |
| 774 |
_str.align(4); |
| 775 |
uint8_t *data = _str.mutable_udata(); |
| 776 |
memset(data, 0, 8 + end); |
| 777 |
data[1] = 3; |
| 778 |
|
| 779 |
uint32_t n = 0; |
| 780 |
const bool *it = gmap.begin(); |
| 781 |
data += 8; |
| 782 |
for (int i = 0; i < end; ++i, ++it, ++data) |
| 783 |
if (*it) { |
| 784 |
*data = 1; |
| 785 |
++n; |
| 786 |
} |
| 787 |
|
| 788 |
n = htonl(n); |
| 789 |
memcpy(_str.mutable_udata() + 4, &n, 4); |
| 790 |
} |
| 791 |
} |
| 792 |
|
| 793 |
Coverage::Coverage(const String &str, ErrorHandler *errh, bool do_check) noexcept |
| 794 |
: _str(str) |
| 795 |
{ |
| 796 |
_str.align(2); |
| 797 |
if (do_check) { |
| 798 |
if (check(errh ? errh : ErrorHandler::silent_handler()) < 0) |
| 799 |
_str = String(); |
| 800 |
} else { // check()'s shorten-string side effect |
| 801 |
const uint8_t *data = _str.udata(); |
| 802 |
int count = Data::u16_aligned(data + 2); |
| 803 |
if (data[1] == T_LIST) |
| 804 |
_str = _str.substring(0, HEADERSIZE + count*LIST_RECSIZE); |
| 805 |
else |
| 806 |
_str = _str.substring(0, HEADERSIZE + count*RANGES_RECSIZE); |
| 807 |
} |
| 808 |
} |
| 809 |
|
| 810 |
int |
| 811 |
Coverage::check(ErrorHandler *errh) |
| 812 |
// side effect: shorten string to cover coverage |
| 813 |
{ |
| 814 |
// HEADER FORMAT: |
| 815 |
// USHORT coverageFormat |
| 816 |
// USHORT glyphCount |
| 817 |
const uint8_t *data = _str.udata(); |
| 818 |
if (_str.length() < HEADERSIZE) |
| 819 |
return errh->error("OTF coverage table too small"); |
| 820 |
int coverageFormat = Data::u16_aligned(data); |
| 821 |
int count = Data::u16_aligned(data + 2); |
| 822 |
|
| 823 |
int len; |
| 824 |
switch (coverageFormat) { |
| 825 |
case T_LIST: |
| 826 |
len = HEADERSIZE + count*LIST_RECSIZE; |
| 827 |
if (_str.length() < len) |
| 828 |
return errh->error("OTF coverage table too short (format 1)"); |
| 829 |
// XXX don't check sorting |
| 830 |
break; |
| 831 |
case T_RANGES: |
| 832 |
len = HEADERSIZE + count*RANGES_RECSIZE; |
| 833 |
if (_str.length() < len) |
| 834 |
return errh->error("OTF coverage table too short (format 2)"); |
| 835 |
// XXX don't check sorting |
| 836 |
// XXX don't check startCoverageIndexes |
| 837 |
break; |
| 838 |
default: |
| 839 |
return errh->error("OTF coverage table has unknown format %d", coverageFormat); |
| 840 |
} |
| 841 |
|
| 842 |
_str = _str.substring(0, len); |
| 843 |
return 0; |
| 844 |
} |
| 845 |
|
| 846 |
int |
| 847 |
Coverage::size() const noexcept |
| 848 |
{ |
| 849 |
if (_str.length() == 0) |
| 850 |
return -1; |
| 851 |
const uint8_t *data = _str.udata(); |
| 852 |
if (data[1] == T_LIST) |
| 853 |
return (_str.length() - HEADERSIZE) / LIST_RECSIZE; |
| 854 |
else if (data[1] == T_RANGES) { |
| 855 |
data += _str.length() - RANGES_RECSIZE; |
| 856 |
return Data::u16_aligned(data + 4) + Data::u16_aligned(data + 2) - Data::u16_aligned(data) + 1; |
| 857 |
} else if (data[1] == T_X_BYTEMAP) |
| 858 |
return Data::u32_aligned(data + 4); |
| 859 |
else |
| 860 |
return -1; |
| 861 |
} |
| 862 |
|
| 863 |
int |
| 864 |
Coverage::coverage_index(Glyph g) const noexcept |
| 865 |
{ |
| 866 |
if (_str.length() == 0) |
| 867 |
return -1; |
| 868 |
|
| 869 |
const uint8_t *data = _str.udata(); |
| 870 |
int count = Data::u16_aligned(data + 2); |
| 871 |
if (data[1] == T_LIST) { |
| 872 |
int l = 0, r = count; |
| 873 |
data += HEADERSIZE; |
| 874 |
while (l < r) { |
| 875 |
int m = l + (r - l) / 2; |
| 876 |
int mval = Data::u16_aligned(data + m * LIST_RECSIZE); |
| 877 |
if (g < mval) |
| 878 |
r = m; |
| 879 |
else if (g == mval) |
| 880 |
return m; |
| 881 |
else |
| 882 |
l = m + 1; |
| 883 |
} |
| 884 |
return -1; |
| 885 |
} else if (data[1] == T_RANGES) { |
| 886 |
int l = 0, r = count; |
| 887 |
data += HEADERSIZE; |
| 888 |
while (l < r) { |
| 889 |
int m = l + (r - l) / 2; |
| 890 |
const uint8_t *rec = data + m * RANGES_RECSIZE; |
| 891 |
if (g < Data::u16_aligned(rec)) |
| 892 |
r = m; |
| 893 |
else if (g <= Data::u16_aligned(rec + 2)) |
| 894 |
return Data::u16_aligned(rec + 4) + g - Data::u16_aligned(rec); |
| 895 |
else |
| 896 |
l = m + 1; |
| 897 |
} |
| 898 |
return -1; |
| 899 |
} else if (data[1] == T_X_BYTEMAP) { |
| 900 |
if (g >= 0 && g < _str.length() - 8 && data[8 + g]) |
| 901 |
return g; |
| 902 |
else |
| 903 |
return -1; |
| 904 |
} else |
| 905 |
return -1; |
| 906 |
} |
| 907 |
|
| 908 |
Glyph |
| 909 |
Coverage::operator[](int cindex) const noexcept |
| 910 |
{ |
| 911 |
if (_str.length() == 0 || cindex < 0) |
| 912 |
return 0; |
| 913 |
|
| 914 |
const uint8_t *data = _str.udata(); |
| 915 |
int count = Data::u16_aligned(data + 2); |
| 916 |
if (data[1] == T_LIST) |
| 917 |
return (cindex < count ? Data::u16_aligned(data + cindex * LIST_RECSIZE) : 0); |
| 918 |
else if (data[1] == T_RANGES) { |
| 919 |
int l = 0, r = count; |
| 920 |
data += HEADERSIZE; |
| 921 |
while (l < r) { |
| 922 |
int m = l + (r - l) / 2; |
| 923 |
const uint8_t *rec = data + m * RANGES_RECSIZE; |
| 924 |
int start_cindex = Data::u16_aligned(rec + 4); |
| 925 |
if (cindex < start_cindex) |
| 926 |
r = m; |
| 927 |
else if (cindex < start_cindex + Data::u16_aligned(rec + 2) - Data::u16_aligned(rec)) |
| 928 |
return Data::u16_aligned(rec) + cindex - start_cindex; |
| 929 |
else |
| 930 |
l = m + 1; |
| 931 |
} |
| 932 |
return 0; |
| 933 |
} else |
| 934 |
return 0; |
| 935 |
} |
| 936 |
|
| 937 |
void |
| 938 |
Coverage::unparse(StringAccum &sa) const noexcept |
| 939 |
{ |
| 940 |
const uint8_t *data = _str.udata(); |
| 941 |
if (_str.length() == 0) |
| 942 |
sa << "@*#!"; |
| 943 |
else if (data[1] == T_LIST) { |
| 944 |
int count = Data::u16_aligned(data + 2); |
| 945 |
for (int i = 0; i < count; i++) { |
| 946 |
if (i) sa << ','; |
| 947 |
sa << Data::u16_aligned(data + HEADERSIZE + i*LIST_RECSIZE); |
| 948 |
} |
| 949 |
} else { |
| 950 |
for (int pos = HEADERSIZE; pos < _str.length(); pos += RANGES_RECSIZE) { |
| 951 |
Glyph start = Data::u16_aligned(data + pos); |
| 952 |
Glyph end = Data::u16_aligned(data + pos + 2); |
| 953 |
if (pos > HEADERSIZE) sa << ','; |
| 954 |
sa << start; |
| 955 |
if (end != start) sa << '.' << '.' << end; |
| 956 |
} |
| 957 |
} |
| 958 |
} |
| 959 |
|
| 960 |
String |
| 961 |
Coverage::unparse() const noexcept |
| 962 |
{ |
| 963 |
StringAccum sa; |
| 964 |
unparse(sa); |
| 965 |
return sa.take_string(); |
| 966 |
} |
| 967 |
|
| 968 |
Coverage |
| 969 |
operator&(const Coverage &a, const Coverage &b) |
| 970 |
{ |
| 971 |
StringAccum sa; |
| 972 |
sa << '\000' << '\001' << '\000' << '\000'; |
| 973 |
|
| 974 |
if (b.has_fast_covers()) { |
| 975 |
for (Coverage::iterator ai = a.begin(); ai; ++ai) |
| 976 |
if (b.covers(*ai)) { |
| 977 |
uint16_t x = *ai; |
| 978 |
sa << (char)(x >> 8) << (char)(x & 0xFF); |
| 979 |
} |
| 980 |
|
| 981 |
} else { |
| 982 |
Coverage::iterator ai = a.begin(), bi = b.begin(); |
| 983 |
while (ai && bi) { |
| 984 |
if (*ai < *bi) |
| 985 |
ai.forward_to(*bi); |
| 986 |
else if (*ai == *bi) { |
| 987 |
uint16_t x = *ai; |
| 988 |
sa << (char)(x >> 8) << (char)(x & 0xFF); |
| 989 |
ai++, bi++; |
| 990 |
} else |
| 991 |
bi.forward_to(*ai); |
| 992 |
} |
| 993 |
} |
| 994 |
|
| 995 |
int n = (sa.length() - 4) / 2; |
| 996 |
sa[2] = (n >> 8); |
| 997 |
sa[3] = (n & 0xFF); |
| 998 |
return Coverage(sa.take_string(), 0, false); |
| 999 |
} |
| 1000 |
|
| 1001 |
bool |
| 1002 |
operator<=(const Coverage &a, const Coverage &b) |
| 1003 |
{ |
| 1004 |
Coverage::iterator ai = a.begin(), bi = b.begin(); |
| 1005 |
while (ai && bi) { |
| 1006 |
if (*ai != *bi && !bi.forward_to(*ai)) |
| 1007 |
return false; |
| 1008 |
ai++, bi++; |
| 1009 |
} |
| 1010 |
return bi || !ai; |
| 1011 |
} |
| 1012 |
|
| 1013 |
|
| 1014 |
/************************ |
| 1015 |
* Coverage::iterator * |
| 1016 |
* * |
| 1017 |
************************/ |
| 1018 |
|
| 1019 |
Coverage::iterator::iterator(const String &str, bool is_end) |
| 1020 |
: _str(str), _value(0) |
| 1021 |
{ |
| 1022 |
// XXX assume _str has been checked |
| 1023 |
if (!_str.length()) { |
| 1024 |
_pos = 0; |
| 1025 |
return; |
| 1026 |
} |
| 1027 |
|
| 1028 |
// shrink _str to fit the coverage table |
| 1029 |
const uint8_t *data = _str.udata(); |
| 1030 |
int n = Data::u16_aligned(data + 2); |
| 1031 |
switch (Data::u16_aligned(data)) { |
| 1032 |
case T_LIST: |
| 1033 |
_str = _str.substring(0, HEADERSIZE + n*LIST_RECSIZE); |
| 1034 |
normal_pos_setting: |
| 1035 |
_pos = is_end ? _str.length() : HEADERSIZE; |
| 1036 |
_value = _pos >= _str.length() ? 0 : Data::u16_aligned(data + _pos); |
| 1037 |
break; |
| 1038 |
case T_RANGES: |
| 1039 |
_str = _str.substring(0, HEADERSIZE + n*RANGES_RECSIZE); |
| 1040 |
goto normal_pos_setting; |
| 1041 |
case T_X_BYTEMAP: |
| 1042 |
for (_pos = 8; _pos < _str.length() && !data[_pos]; ++_pos) |
| 1043 |
/* do nothing */; |
| 1044 |
_value = _pos >= _str.length() ? 0 : _pos - 8; |
| 1045 |
break; |
| 1046 |
default: |
| 1047 |
_str = String(); |
| 1048 |
_pos = 0; |
| 1049 |
_value = 0; |
| 1050 |
break; |
| 1051 |
} |
| 1052 |
} |
| 1053 |
|
| 1054 |
int |
| 1055 |
Coverage::iterator::coverage_index() const |
| 1056 |
{ |
| 1057 |
const uint8_t *data = _str.udata(); |
| 1058 |
assert(_pos < _str.length()); |
| 1059 |
if (data[1] == T_LIST) |
| 1060 |
return (_pos - HEADERSIZE) / LIST_RECSIZE; |
| 1061 |
else if (data[1] == T_RANGES) |
| 1062 |
return Data::u16_aligned(data + _pos + 4) + _value - Data::u16_aligned(data + _pos); |
| 1063 |
else |
| 1064 |
return _pos - 8; |
| 1065 |
} |
| 1066 |
|
| 1067 |
void |
| 1068 |
Coverage::iterator::operator++(int) |
| 1069 |
{ |
| 1070 |
const uint8_t *data = _str.udata(); |
| 1071 |
int len = _str.length(); |
| 1072 |
if (_pos >= len |
| 1073 |
|| (data[1] == T_RANGES && ++_value <= Data::u16_aligned(data + _pos + 2))) |
| 1074 |
return; |
| 1075 |
switch (data[1]) { |
| 1076 |
case T_LIST: |
| 1077 |
_pos += LIST_RECSIZE; |
| 1078 |
normal_pos_setting: |
| 1079 |
_value = _pos >= len ? 0 : Data::u16_aligned(data + _pos); |
| 1080 |
break; |
| 1081 |
case T_RANGES: |
| 1082 |
_pos += RANGES_RECSIZE; |
| 1083 |
goto normal_pos_setting; |
| 1084 |
case T_X_BYTEMAP: |
| 1085 |
for (++_pos; _pos < len && !data[_pos]; ++_pos) |
| 1086 |
/* do nothing */; |
| 1087 |
_value = _pos >= len ? 0 : _pos - 8; |
| 1088 |
break; |
| 1089 |
} |
| 1090 |
} |
| 1091 |
|
| 1092 |
bool |
| 1093 |
Coverage::iterator::forward_to(Glyph find) |
| 1094 |
{ |
| 1095 |
// XXX really should check that this works |
| 1096 |
|
| 1097 |
if (find <= _value) |
| 1098 |
return find == _value; |
| 1099 |
else if (_pos >= _str.length()) |
| 1100 |
return false; |
| 1101 |
|
| 1102 |
const uint8_t *data = _str.udata(); |
| 1103 |
if (data[1] == T_LIST) { |
| 1104 |
// check for "common" case: next element |
| 1105 |
_pos += LIST_RECSIZE; |
| 1106 |
if (_pos >= _str.length()) |
| 1107 |
return false; |
| 1108 |
else if (find <= Data::u16_aligned(data + _pos)) { |
| 1109 |
_value = Data::u16_aligned(data + _pos); |
| 1110 |
return find == _value; |
| 1111 |
} |
| 1112 |
|
| 1113 |
// otherwise, binary search over remaining area |
| 1114 |
int l = ((_pos - HEADERSIZE) / LIST_RECSIZE) + 1; |
| 1115 |
int r = (_str.length() - HEADERSIZE) / LIST_RECSIZE; |
| 1116 |
data += HEADERSIZE; |
| 1117 |
while (l < r) { |
| 1118 |
int m = l + (r - l) / 2; |
| 1119 |
Glyph g = Data::u16_aligned(data + m * LIST_RECSIZE); |
| 1120 |
if (find < g) |
| 1121 |
r = m; |
| 1122 |
else if (find == g) |
| 1123 |
l = r = m; |
| 1124 |
else |
| 1125 |
l = m + 1; |
| 1126 |
} |
| 1127 |
_pos = HEADERSIZE + l * LIST_RECSIZE; |
| 1128 |
_value = (_pos >= _str.length() ? 0 : Data::u16_aligned(data - HEADERSIZE + _pos)); |
| 1129 |
|
| 1130 |
} else if (data[1] == T_RANGES) { |
| 1131 |
// check for "common" case: this or next element |
| 1132 |
if (find <= Data::u16_aligned(data + _pos + 2)) { |
| 1133 |
assert(find >= Data::u16_aligned(data + _pos)); |
| 1134 |
_value = find; |
| 1135 |
return true; |
| 1136 |
} |
| 1137 |
_pos += RANGES_RECSIZE; |
| 1138 |
if (_pos >= _str.length()) |
| 1139 |
return false; |
| 1140 |
else if (find <= Data::u16_aligned(data + _pos + 2)) { |
| 1141 |
_value = (find >= Data::u16_aligned(data + _pos) ? find : Data::u16_aligned(data + _pos)); |
| 1142 |
return find == _value; |
| 1143 |
} |
| 1144 |
|
| 1145 |
// otherwise, binary search over remaining area |
| 1146 |
int l = ((_pos - HEADERSIZE) / RANGES_RECSIZE) + 1; |
| 1147 |
int r = (_str.length() - HEADERSIZE) / RANGES_RECSIZE; |
| 1148 |
data += HEADERSIZE; |
| 1149 |
while (l < r) { |
| 1150 |
int m = l + (r - l) / 2; |
| 1151 |
if (find < Data::u16_aligned(data + m * RANGES_RECSIZE)) |
| 1152 |
r = m; |
| 1153 |
else if (find <= Data::u16_aligned(data + m * RANGES_RECSIZE + 2)) { |
| 1154 |
_pos = HEADERSIZE + m * RANGES_RECSIZE; |
| 1155 |
_value = find; |
| 1156 |
return true; |
| 1157 |
} else |
| 1158 |
l = m + 1; |
| 1159 |
} |
| 1160 |
_pos = HEADERSIZE + l * LIST_RECSIZE; |
| 1161 |
_value = (_pos >= _str.length() ? 0 : Data::u16_aligned(data - HEADERSIZE + _pos)); |
| 1162 |
|
| 1163 |
} else if (data[1] == T_X_BYTEMAP) { |
| 1164 |
_pos = 8 + find; |
| 1165 |
while (_pos < _str.length() && !data[_pos]) |
| 1166 |
++_pos; |
| 1167 |
_pos = _pos >= _str.length() ? _str.length() : _pos; |
| 1168 |
_value = _pos >= _str.length() ? 0 : _pos - 8; |
| 1169 |
} |
| 1170 |
|
| 1171 |
return find == _value; |
| 1172 |
} |
| 1173 |
|
| 1174 |
|
| 1175 |
/************************** |
| 1176 |
* GlyphSet * |
| 1177 |
* * |
| 1178 |
**************************/ |
| 1179 |
|
| 1180 |
GlyphSet::GlyphSet() |
| 1181 |
{ |
| 1182 |
memset(_v, 0, sizeof(_v)); |
| 1183 |
} |
| 1184 |
|
| 1185 |
GlyphSet::GlyphSet(const GlyphSet &o) |
| 1186 |
{ |
| 1187 |
for (int i = 0; i < VLEN; i++) |
| 1188 |
if (o._v[i]) { |
| 1189 |
_v[i] = new uint32_t[VULEN]; |
| 1190 |
memcpy(_v[i], o._v[i], sizeof(uint32_t) * VULEN); |
| 1191 |
} else |
| 1192 |
_v[i] = 0; |
| 1193 |
} |
| 1194 |
|
| 1195 |
GlyphSet::~GlyphSet() |
| 1196 |
{ |
| 1197 |
for (int i = 0; i < VLEN; i++) |
| 1198 |
delete[] _v[i]; |
| 1199 |
} |
| 1200 |
|
| 1201 |
int |
| 1202 |
GlyphSet::change(Glyph g, bool value) |
| 1203 |
{ |
| 1204 |
if ((unsigned)g > MAXGLYPH) |
| 1205 |
return -1; |
| 1206 |
uint32_t *&u = _v[g >> SHIFT]; |
| 1207 |
if (!u) { |
| 1208 |
u = new uint32_t[VULEN]; |
| 1209 |
memset(u, 0, sizeof(uint32_t) * VULEN); |
| 1210 |
} |
| 1211 |
uint32_t mask = (1 << (g & 0x1F)); |
| 1212 |
if (value) |
| 1213 |
u[(g & MASK) >> 5] |= mask; |
| 1214 |
else |
| 1215 |
u[(g & MASK) >> 5] &= ~mask; |
| 1216 |
return 0; |
| 1217 |
} |
| 1218 |
|
| 1219 |
GlyphSet & |
| 1220 |
GlyphSet::operator=(const GlyphSet &o) |
| 1221 |
{ |
| 1222 |
if (&o != this) { |
| 1223 |
for (int i = 0; i < VLEN; i++) |
| 1224 |
if (o._v[i]) { |
| 1225 |
if (!_v[i]) |
| 1226 |
_v[i] = new uint32_t[VULEN]; |
| 1227 |
memcpy(_v[i], o._v[i], sizeof(uint32_t) * VULEN); |
| 1228 |
} else if (_v[i]) |
| 1229 |
memset(_v[i], 0, sizeof(uint32_t) * VULEN); |
| 1230 |
} |
| 1231 |
return *this; |
| 1232 |
} |
| 1233 |
|
| 1234 |
|
| 1235 |
/************************** |
| 1236 |
* ClassDef * |
| 1237 |
* * |
| 1238 |
**************************/ |
| 1239 |
|
| 1240 |
ClassDef::ClassDef(const String &str, ErrorHandler *errh) noexcept |
| 1241 |
: _str(str) |
| 1242 |
{ |
| 1243 |
_str.align(2); |
| 1244 |
if (check(errh ? errh : ErrorHandler::silent_handler()) < 0) |
| 1245 |
_str = String(); |
| 1246 |
} |
| 1247 |
|
| 1248 |
int |
| 1249 |
ClassDef::check(ErrorHandler *errh) |
| 1250 |
{ |
| 1251 |
// HEADER FORMAT: |
| 1252 |
// USHORT coverageFormat |
| 1253 |
// USHORT glyphCount |
| 1254 |
const uint8_t *data = _str.udata(); |
| 1255 |
if (_str.length() < 6) // NB: prevents empty format-2 tables |
| 1256 |
return errh->error("OTF class def table too small"); |
| 1257 |
int classFormat = Data::u16_aligned(data); |
| 1258 |
|
| 1259 |
int len; |
| 1260 |
if (classFormat == T_LIST) { |
| 1261 |
int count = Data::u16_aligned(data + 4); |
| 1262 |
len = LIST_HEADERSIZE + count*LIST_RECSIZE; |
| 1263 |
// XXX don't check sorting |
| 1264 |
} else if (classFormat == T_RANGES) { |
| 1265 |
int count = Data::u16_aligned(data + 2); |
| 1266 |
len = RANGES_HEADERSIZE + count*RANGES_RECSIZE; |
| 1267 |
// XXX don't check sorting |
| 1268 |
} else |
| 1269 |
return errh->error("OTF class def table has unknown format %d", classFormat); |
| 1270 |
|
| 1271 |
if (len > _str.length()) |
| 1272 |
return errh->error("OTF class def table too short"); |
| 1273 |
else { |
| 1274 |
_str = _str.substring(0, len); |
| 1275 |
return 0; |
| 1276 |
} |
| 1277 |
} |
| 1278 |
|
| 1279 |
int |
| 1280 |
ClassDef::lookup(Glyph g) const noexcept |
| 1281 |
{ |
| 1282 |
if (_str.length() == 0) |
| 1283 |
return -1; |
| 1284 |
|
| 1285 |
const uint8_t *data = _str.udata(); |
| 1286 |
int coverageFormat = Data::u16_aligned(data); |
| 1287 |
|
| 1288 |
if (coverageFormat == T_LIST) { |
| 1289 |
Glyph start = Data::u16_aligned(data + 2); |
| 1290 |
int count = Data::u16_aligned(data + 4); |
| 1291 |
if (g < start || g >= start + count) |
| 1292 |
return 0; |
| 1293 |
else |
| 1294 |
return Data::u16_aligned(data + LIST_HEADERSIZE + (g - start) * LIST_RECSIZE); |
| 1295 |
} else if (coverageFormat == T_RANGES) { |
| 1296 |
int l = 0, r = Data::u16_aligned(data + 2); |
| 1297 |
data += RANGES_HEADERSIZE; |
| 1298 |
while (l < r) { |
| 1299 |
int m = l + (r - l) / 2; |
| 1300 |
const uint8_t *rec = data + m * RANGES_RECSIZE; |
| 1301 |
if (g < Data::u16_aligned(rec)) |
| 1302 |
r = m; |
| 1303 |
else if (g <= Data::u16_aligned(rec + 2)) |
| 1304 |
return Data::u16_aligned(rec + 4); |
| 1305 |
else |
| 1306 |
l = m + 1; |
| 1307 |
} |
| 1308 |
return 0; |
| 1309 |
} else |
| 1310 |
return 0; |
| 1311 |
} |
| 1312 |
|
| 1313 |
void |
| 1314 |
ClassDef::unparse(StringAccum &sa) const noexcept |
| 1315 |
{ |
| 1316 |
const uint8_t *data = _str.udata(); |
| 1317 |
if (_str.length() == 0) |
| 1318 |
sa << "@*#!"; |
| 1319 |
else if (data[1] == T_LIST) { |
| 1320 |
Glyph start = Data::u16_aligned(data + 2); |
| 1321 |
int count = Data::u16_aligned(data + 4); |
| 1322 |
for (int i = 0; i < count; i++) { |
| 1323 |
if (i) sa << ','; |
| 1324 |
sa << start + i << '=' << Data::u16_aligned(data + LIST_HEADERSIZE + i*LIST_RECSIZE); |
| 1325 |
} |
| 1326 |
} else { |
| 1327 |
for (int pos = RANGES_HEADERSIZE; pos < _str.length(); pos += RANGES_RECSIZE) { |
| 1328 |
Glyph start = Data::u16_aligned(data + pos); |
| 1329 |
Glyph end = Data::u16_aligned(data + pos + 2); |
| 1330 |
if (pos > RANGES_HEADERSIZE) sa << ','; |
| 1331 |
sa << start; |
| 1332 |
if (end != start) sa << '.' << '.' << end; |
| 1333 |
sa << '=' << Data::u16_aligned(data + pos + 4); |
| 1334 |
} |
| 1335 |
} |
| 1336 |
} |
| 1337 |
|
| 1338 |
String |
| 1339 |
ClassDef::unparse() const noexcept |
| 1340 |
{ |
| 1341 |
StringAccum sa; |
| 1342 |
unparse(sa); |
| 1343 |
return sa.take_string(); |
| 1344 |
} |
| 1345 |
|
| 1346 |
|
| 1347 |
/****************************** |
| 1348 |
* ClassDef::class_iterator * |
| 1349 |
* * |
| 1350 |
******************************/ |
| 1351 |
|
| 1352 |
ClassDef::class_iterator::class_iterator(const String &str, int pos, int the_class, const Coverage::iterator &coviter) |
| 1353 |
: _str(str), _pos(pos), _class(the_class), _coviter(coviter) |
| 1354 |
{ |
| 1355 |
// XXX assume _str has been checked |
| 1356 |
|
| 1357 |
// cannot handle the_class == 0 when coviter doesn't exist |
| 1358 |
if (_class == 0 && !_coviter) |
| 1359 |
throw Error("cannot iterate over ClassDef class 0"); |
| 1360 |
|
| 1361 |
// shrink _str to fit the coverage table, and create a fake coverage |
| 1362 |
// iterator if necessary |
| 1363 |
const uint8_t *data = _str.udata(); |
| 1364 |
if (_str.length()) { |
| 1365 |
switch (Data::u16_aligned(data)) { |
| 1366 |
case T_LIST: { |
| 1367 |
Glyph start = Data::u16_aligned(data + 2); |
| 1368 |
int nglyphs = Data::u16_aligned(data + 4); |
| 1369 |
_str = _str.substring(0, LIST_HEADERSIZE + nglyphs*LIST_RECSIZE); |
| 1370 |
if (!_coviter) |
| 1371 |
_coviter = Coverage(start, start + nglyphs - 1).begin(); |
| 1372 |
if (_class) |
| 1373 |
_coviter.forward_to(start); |
| 1374 |
break; |
| 1375 |
} |
| 1376 |
case T_RANGES: { |
| 1377 |
Glyph start = Data::u16_aligned(data + RANGES_HEADERSIZE); |
| 1378 |
int nranges = Data::u16_aligned(data + 2); |
| 1379 |
_str = _str.substring(0, RANGES_HEADERSIZE + nranges*RANGES_RECSIZE); |
| 1380 |
if (!_coviter) { |
| 1381 |
Glyph end = Data::u16_aligned(data + RANGES_HEADERSIZE + 2 + (nranges - 1)*RANGES_RECSIZE); |
| 1382 |
_coviter = Coverage(start, end).begin(); |
| 1383 |
} |
| 1384 |
if (_class) |
| 1385 |
_coviter.forward_to(start); |
| 1386 |
break; |
| 1387 |
} |
| 1388 |
default: |
| 1389 |
_str = String(); |
| 1390 |
break; |
| 1391 |
} |
| 1392 |
} |
| 1393 |
|
| 1394 |
// move to the first relevant glyph |
| 1395 |
if (_pos >= _str.length()) |
| 1396 |
_pos = _str.length(); |
| 1397 |
else { |
| 1398 |
_pos = 0; |
| 1399 |
(*this)++; |
| 1400 |
} |
| 1401 |
} |
| 1402 |
|
| 1403 |
void |
| 1404 |
ClassDef::class_iterator::increment_class0() |
| 1405 |
{ |
| 1406 |
const uint8_t *data = _str.udata(); |
| 1407 |
int len = _str.length(); |
| 1408 |
bool is_list = (data[1] == T_LIST); |
| 1409 |
|
| 1410 |
if (_pos != 0) |
| 1411 |
_coviter++; |
| 1412 |
else |
| 1413 |
_pos = FIRST_POS; |
| 1414 |
|
| 1415 |
if (_pos == FIRST_POS && _coviter) { |
| 1416 |
if (*_coviter < Data::u16_aligned(data + (is_list ? 2 : RANGES_HEADERSIZE))) |
| 1417 |
return; |
| 1418 |
_pos = (is_list ? LIST_HEADERSIZE : RANGES_HEADERSIZE); |
| 1419 |
} |
| 1420 |
|
| 1421 |
while (_pos > 0 && _pos < len && _coviter) { |
| 1422 |
Glyph g = *_coviter; |
| 1423 |
if (is_list) { |
| 1424 |
_pos = LIST_HEADERSIZE + LIST_RECSIZE*(g - Data::u16_aligned(data + 2)); |
| 1425 |
if (_pos >= len) |
| 1426 |
break; |
| 1427 |
else if (Data::u16_aligned(data + _pos) == 0) // _class == 0 |
| 1428 |
return; |
| 1429 |
_coviter++; |
| 1430 |
} else { |
| 1431 |
if (g < Data::u16_aligned(data + _pos)) // in a zero range |
| 1432 |
return; |
| 1433 |
else if (g > Data::u16_aligned(data + _pos + 2)) |
| 1434 |
_pos += RANGES_RECSIZE; |
| 1435 |
else if (Data::u16_aligned(data + _pos + 4) == 0) // _class == 0 |
| 1436 |
return; |
| 1437 |
else |
| 1438 |
_coviter.forward_to(Data::u16_aligned(data + _pos + 2) + 1); |
| 1439 |
} |
| 1440 |
} |
| 1441 |
|
| 1442 |
if (_coviter) |
| 1443 |
_pos = LAST_POS; |
| 1444 |
else |
| 1445 |
_pos = len; |
| 1446 |
} |
| 1447 |
|
| 1448 |
void |
| 1449 |
ClassDef::class_iterator::operator++(int) |
| 1450 |
{ |
| 1451 |
if (_class == 0) { |
| 1452 |
increment_class0(); |
| 1453 |
return; |
| 1454 |
} |
| 1455 |
|
| 1456 |
const uint8_t *data = _str.udata(); |
| 1457 |
int len = _str.length(); |
| 1458 |
bool is_list = (data[1] == T_LIST); |
| 1459 |
|
| 1460 |
if (_pos != 0) |
| 1461 |
_coviter++; |
| 1462 |
else |
| 1463 |
_pos = (is_list ? LIST_HEADERSIZE : RANGES_HEADERSIZE); |
| 1464 |
|
| 1465 |
while (_pos < len && _coviter) { |
| 1466 |
Glyph g = *_coviter; |
| 1467 |
if (is_list) { |
| 1468 |
_pos = LIST_HEADERSIZE + LIST_RECSIZE*(g - Data::u16_aligned(data + 2)); |
| 1469 |
if (_pos >= len || Data::u16_aligned(data + _pos) == _class) |
| 1470 |
return; |
| 1471 |
_coviter++; |
| 1472 |
} else { |
| 1473 |
while (_pos < len && (g > Data::u16_aligned(data + _pos + 2) |
| 1474 |
|| Data::u16_aligned(data + _pos + 4) != _class)) |
| 1475 |
_pos += RANGES_RECSIZE; |
| 1476 |
// now, _pos >= len, or g <= rec.end && class == rec.class |
| 1477 |
if (_pos >= len || g >= Data::u16_aligned(data + _pos)) |
| 1478 |
return; |
| 1479 |
_coviter.forward_to(Data::u16_aligned(data + _pos)); |
| 1480 |
} |
| 1481 |
} |
| 1482 |
|
| 1483 |
_pos = len; |
| 1484 |
} |
| 1485 |
|
| 1486 |
|
| 1487 |
}} |
| 1488 |
|
| 1489 |
|
| 1490 |
// template instantiations |
| 1491 |
#include <lcdf/vector.cc> |