| 1 |
// -*- related-file-name: "../include/efont/t1font.hh" -*- |
| 2 |
|
| 3 |
/* t1font.{cc,hh} -- Type 1 font |
| 4 |
* |
| 5 |
* Copyright (c) 1998-2023 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/t1font.hh> |
| 20 |
#include <efont/t1item.hh> |
| 21 |
#include <efont/t1rw.hh> |
| 22 |
#include <efont/t1mm.hh> |
| 23 |
#include <lcdf/error.hh> |
| 24 |
#include <string.h> |
| 25 |
#include <stdlib.h> |
| 26 |
#include <ctype.h> |
| 27 |
namespace Efont { |
| 28 |
|
| 29 |
static PermString::Initializer initializer; |
| 30 |
static PermString lenIV_str = "lenIV"; |
| 31 |
static PermString FontInfo_str = "FontInfo"; |
| 32 |
|
| 33 |
Type1Font::Type1Font(PermString name) |
| 34 |
: CharstringProgram(1000), |
| 35 |
_cached_defs(false), _built(true), _font_name(name), _glyph_map(-1), |
| 36 |
_encoding(0), _cached_mmspace(0), _mmspace(0), _synthetic_item(0) |
| 37 |
{ |
| 38 |
_dict = new HashMap<PermString, Type1Definition *>[dLast]; |
| 39 |
for (int i = 0; i < dLast; i++) { |
| 40 |
_index[i] = -1; |
| 41 |
_dict_deltas[i] = 0; |
| 42 |
_dict[i].set_default_value((Type1Definition *)0); |
| 43 |
} |
| 44 |
} |
| 45 |
|
| 46 |
Type1Font::Type1Font(Type1Reader &reader) |
| 47 |
: CharstringProgram(1000), |
| 48 |
_cached_defs(false), _built(false), _glyph_map(-1), _encoding(0), |
| 49 |
_cached_mmspace(0), _mmspace(0), _synthetic_item(0) |
| 50 |
{ |
| 51 |
_dict = new HashMap<PermString, Type1Definition *>[dLast]; |
| 52 |
for (int i = 0; i < dLast; i++) { |
| 53 |
_index[i] = -1; |
| 54 |
_dict_deltas[i] = 0; |
| 55 |
_dict[i].set_default_value((Type1Definition *)0); |
| 56 |
} |
| 57 |
read(reader); |
| 58 |
} |
| 59 |
|
| 60 |
Type1Font::~Type1Font() |
| 61 |
{ |
| 62 |
delete[] _dict; |
| 63 |
for (int i = 0; i < _items.size(); i++) |
| 64 |
delete _items[i]; |
| 65 |
delete _mmspace; |
| 66 |
for (int i = 0; i < _subrs.size(); i++) |
| 67 |
delete _subrs[i]; |
| 68 |
if (!_synthetic_item) |
| 69 |
for (int i = 0; i < _glyphs.size(); i++) |
| 70 |
delete _glyphs[i]; |
| 71 |
} |
| 72 |
|
| 73 |
void |
| 74 |
Type1Font::set_item(int i, Type1Item *it) |
| 75 |
{ |
| 76 |
delete _items[i]; |
| 77 |
_items[i] = it; |
| 78 |
} |
| 79 |
|
| 80 |
static const char * const dict_starters[] = { |
| 81 |
"0 dict dup begin", "/FontInfo 0 dict dup begin", "dup /Private 0 dict dup begin", "/Blend 0 dict dup begin", 0, 0 |
| 82 |
}; |
| 83 |
|
| 84 |
void |
| 85 |
Type1Font::add_definition(int dict, Type1Definition *t1d) |
| 86 |
{ |
| 87 |
if (_index[dict] < 0) { |
| 88 |
if (_built && dict_starters[dict]) |
| 89 |
add_item(new Type1CopyItem(dict_starters[dict])); |
| 90 |
_index[dict] = _items.size(); |
| 91 |
} |
| 92 |
add_item(t1d); |
| 93 |
set_dict(dict, t1d->name(), t1d); |
| 94 |
} |
| 95 |
|
| 96 |
void |
| 97 |
Type1Font::add_type1_encoding(Type1Encoding *e) |
| 98 |
{ |
| 99 |
if (_encoding) { |
| 100 |
for (Type1Item** t1i = _items.begin(); t1i < _items.end(); t1i++) |
| 101 |
if (*t1i == _encoding) { |
| 102 |
delete _encoding; |
| 103 |
*t1i = _encoding = e; |
| 104 |
return; |
| 105 |
} |
| 106 |
} |
| 107 |
_encoding = e; |
| 108 |
add_item(e); |
| 109 |
} |
| 110 |
|
| 111 |
void |
| 112 |
Type1Font::add_glyph(Type1Subr *s) |
| 113 |
{ |
| 114 |
int &g = _glyph_map.find_force(s->name(), _glyphs.size()); |
| 115 |
if (g == _glyphs.size()) |
| 116 |
_glyphs.push_back(s); |
| 117 |
else { |
| 118 |
delete _glyphs[g]; |
| 119 |
_glyphs[g] = s; |
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
int |
| 124 |
Type1Font::read(Type1Reader &reader) |
| 125 |
{ |
| 126 |
Dict cur_dict = dFont; |
| 127 |
int eexec_state = 0; |
| 128 |
bool have_subrs = false; |
| 129 |
bool have_charstrings = false; |
| 130 |
int lenIV = 4; |
| 131 |
Type1SubrGroupItem *cur_group = 0; |
| 132 |
int cur_group_count = 0; |
| 133 |
|
| 134 |
StringAccum accum; |
| 135 |
while (reader.next_line(accum)) { |
| 136 |
|
| 137 |
// check for NULL STRING |
| 138 |
int x_length = accum.length(); |
| 139 |
if (!x_length) |
| 140 |
continue; |
| 141 |
const char *x = accum.c_str(); // ensure we don't run off the string |
| 142 |
|
| 143 |
// check for CHARSTRINGS |
| 144 |
if (reader.was_charstring()) { |
| 145 |
Type1Subr *fcs = Type1Subr::make(x, x_length, reader.charstring_start(), reader.charstring_length(), lenIV); |
| 146 |
|
| 147 |
if (fcs->is_subr()) { |
| 148 |
if (fcs->subrno() >= _subrs.size()) |
| 149 |
_subrs.resize(fcs->subrno() + 30, (Type1Subr *)0); |
| 150 |
if (_subrs[fcs->subrno()]) // hybrid font program |
| 151 |
delete _subrs[fcs->subrno()]; |
| 152 |
_subrs[fcs->subrno()] = fcs; |
| 153 |
if (!have_subrs && _items.size()) { |
| 154 |
if (Type1CopyItem *item = _items.back()->cast_copy()) { |
| 155 |
cur_group = new Type1SubrGroupItem |
| 156 |
(this, true, item->value()); |
| 157 |
cur_group_count = 0; |
| 158 |
_items.back() = cur_group; |
| 159 |
delete item; |
| 160 |
} |
| 161 |
have_subrs = true; |
| 162 |
} |
| 163 |
|
| 164 |
} else { |
| 165 |
add_glyph(fcs); |
| 166 |
if (!have_charstrings && _items.size()) { |
| 167 |
if (Type1CopyItem *item = _items.back()->cast_copy()) { |
| 168 |
cur_group = new Type1SubrGroupItem |
| 169 |
(this, false, item->value()); |
| 170 |
cur_group_count = 0; |
| 171 |
_items.back() = cur_group; |
| 172 |
delete item; |
| 173 |
} |
| 174 |
have_charstrings = true; |
| 175 |
} |
| 176 |
} |
| 177 |
|
| 178 |
accum.clear(); |
| 179 |
continue; |
| 180 |
} |
| 181 |
|
| 182 |
// check for COMMENTS |
| 183 |
if (x[0] == '%') { |
| 184 |
add_item(new Type1CopyItem(accum.take_string())); |
| 185 |
continue; |
| 186 |
} |
| 187 |
|
| 188 |
// check for CHARSTRING START |
| 189 |
// 5/29/1999: beware of charstring start-like things that don't have |
| 190 |
// `readstring' in them! |
| 191 |
if (!_charstring_definer |
| 192 |
&& strstr(x, "string currentfile") != 0 |
| 193 |
&& strstr(x, "readstring") != 0) { |
| 194 |
const char *sb = x; |
| 195 |
while (*sb && *sb != '/') |
| 196 |
sb++; |
| 197 |
const char *se = sb + 1; |
| 198 |
while (*sb && *se && *se != ' ' && *se != '{') |
| 199 |
se++; |
| 200 |
if (*sb && *se) { |
| 201 |
_charstring_definer = permprintf(" %*s ", se - sb - 1, sb + 1); |
| 202 |
reader.set_charstring_definer(_charstring_definer); |
| 203 |
add_item(new Type1CopyItem(accum.take_string())); |
| 204 |
continue; |
| 205 |
} |
| 206 |
} |
| 207 |
|
| 208 |
// check for ENCODING |
| 209 |
if (!_encoding && strncmp(x, "/Encoding ", 10) == 0) { |
| 210 |
read_encoding(reader, x + 10); |
| 211 |
accum.clear(); |
| 212 |
continue; |
| 213 |
} |
| 214 |
|
| 215 |
// check for a DEFINITION |
| 216 |
if (x[0] == '/') { |
| 217 |
definition_succeed: |
| 218 |
Type1Definition *t1d = Type1Definition::make(accum, &reader); |
| 219 |
if (!t1d) |
| 220 |
goto definition_fail; |
| 221 |
if (t1d->name() == lenIV_str) |
| 222 |
t1d->value_int(lenIV); |
| 223 |
add_definition(cur_dict, t1d); |
| 224 |
accum.clear(); |
| 225 |
continue; |
| 226 |
} else if (x[0] == ' ') { |
| 227 |
const char *y; |
| 228 |
for (y = x; y[0] == ' '; y++) |
| 229 |
; |
| 230 |
if (y[0] == '/') |
| 231 |
goto definition_succeed; |
| 232 |
} |
| 233 |
|
| 234 |
definition_fail: |
| 235 |
|
| 236 |
// check for ZEROS special case |
| 237 |
if (eexec_state == 2) { |
| 238 |
// In eexec_state 2 (right after turning off eexec), the opening |
| 239 |
// part of the string will have some 0 bytes followed by '0's. |
| 240 |
// Change the 0 bytes into textual '0's. |
| 241 |
int zeros = 0; |
| 242 |
while (x[zeros] == 0 && x_length > 0) |
| 243 |
zeros++, x_length--; |
| 244 |
add_item(new Type1CopyItem(String::make_fill('0', zeros * 2 + x_length))); |
| 245 |
eexec_state = 3; |
| 246 |
accum.clear(); |
| 247 |
continue; |
| 248 |
} |
| 249 |
|
| 250 |
// check for MODIFIED FONT |
| 251 |
if (eexec_state == 1 && strstr(x, "FontDirectory") != 0 |
| 252 |
&& read_synthetic_font(reader, x, accum)) { |
| 253 |
accum.clear(); |
| 254 |
continue; |
| 255 |
} |
| 256 |
|
| 257 |
// check for END-OF-CHARSTRING-GROUP TEXT |
| 258 |
if (cur_group) { |
| 259 |
if (cur_group_count == 0 |
| 260 |
|| ((strstr(x, "end") != 0 || strstr(x, "put") != 0) |
| 261 |
&& strchr(x, '/') == 0)) { |
| 262 |
cur_group->add_end_text(x); |
| 263 |
cur_group_count++; |
| 264 |
accum.clear(); |
| 265 |
continue; |
| 266 |
} |
| 267 |
cur_group = 0; |
| 268 |
} |
| 269 |
|
| 270 |
// add COPY ITEM |
| 271 |
String s = accum.take_string(); |
| 272 |
add_item(new Type1CopyItem(s)); |
| 273 |
x = s.data(); |
| 274 |
|
| 275 |
if (eexec_state == 0 && strncmp(x, "currentfile eexec", 17) == 0 && (isspace((unsigned char) x[17]) || !x[17])) { |
| 276 |
// allow arbitrary whitespace after "currentfile eexec". |
| 277 |
// note: strlen("currentfile eexec") == 17 |
| 278 |
for (x += 17; isspace((unsigned char) *x); x++) |
| 279 |
/* nada */; |
| 280 |
reader.switch_eexec(true, (unsigned char *)x, (s.data() + s.length()) - x); |
| 281 |
set_item(nitems() - 1, new Type1EexecItem(true)); |
| 282 |
eexec_state = 1; |
| 283 |
} else if (eexec_state == 1 && strstr(x, "currentfile closefile") != 0) { |
| 284 |
reader.switch_eexec(false, 0, 0); |
| 285 |
add_item(new Type1EexecItem(false)); |
| 286 |
eexec_state = 2; |
| 287 |
} else if (strstr(x, "begin") != 0) { |
| 288 |
// 30.Sep.2002: NuevaMM's BlendFontInfo dict starts with a simple |
| 289 |
// "/FontInfo ... begin" inside a "/Blend ... begin". |
| 290 |
Dict was_dict = cur_dict; |
| 291 |
if (strstr(x, "/Private") != 0) |
| 292 |
cur_dict = dPrivate; |
| 293 |
else if (strstr(x, "/FontInfo") != 0) |
| 294 |
cur_dict = dFontInfo; |
| 295 |
else |
| 296 |
cur_dict = dFont; |
| 297 |
if (strstr(x, "/Blend") != 0) |
| 298 |
cur_dict = (Dict)(cur_dict + dBlend); |
| 299 |
else if (was_dict == dBlend && cur_dict == dFontInfo) |
| 300 |
cur_dict = (Dict)(cur_dict + dBlend); |
| 301 |
} else if (cur_dict == dFontInfo && strstr(x, "end") != 0) |
| 302 |
cur_dict = dFont; |
| 303 |
} |
| 304 |
|
| 305 |
// set dictionary deltas |
| 306 |
for (int i = dFI; i < dLast; i++) |
| 307 |
_dict_deltas[i] = get_dict_size(i) - _dict[i].size(); |
| 308 |
// borrow glyphs and glyph map from _synthetic_item |
| 309 |
if (!_glyphs.size() && _synthetic_item) { |
| 310 |
_glyphs = _synthetic_item->included_font()->_glyphs; |
| 311 |
_glyph_map = _synthetic_item->included_font()->_glyph_map; |
| 312 |
} |
| 313 |
|
| 314 |
return (ok() ? 0 : -1); |
| 315 |
} |
| 316 |
|
| 317 |
bool |
| 318 |
Type1Font::ok() const |
| 319 |
{ |
| 320 |
return font_name() && _glyphs.size() > 0; |
| 321 |
} |
| 322 |
|
| 323 |
static char *skip_comment_space(char *s) |
| 324 |
{ |
| 325 |
while (1) { |
| 326 |
if (isspace((unsigned char) *s)) |
| 327 |
++s; |
| 328 |
else if (*s == '%') { |
| 329 |
for (++s; *s != '\r' && *s != '\n' && *s != '\0'; ++s) |
| 330 |
/* nada */; |
| 331 |
} else |
| 332 |
return s; |
| 333 |
} |
| 334 |
} |
| 335 |
|
| 336 |
void |
| 337 |
Type1Font::read_encoding(Type1Reader &reader, const char *first_line) |
| 338 |
{ |
| 339 |
while (isspace((unsigned char) *first_line)) |
| 340 |
first_line++; |
| 341 |
if (strncmp(first_line, "StandardEncoding", 16) == 0) { |
| 342 |
add_type1_encoding(Type1Encoding::standard_encoding()); |
| 343 |
return; |
| 344 |
} |
| 345 |
|
| 346 |
add_type1_encoding(new Type1Encoding); |
| 347 |
|
| 348 |
bool got_any = false; |
| 349 |
StringAccum accum; |
| 350 |
|
| 351 |
while (reader.next_line(accum)) { |
| 352 |
|
| 353 |
// check for NULL STRING |
| 354 |
if (!accum.length()) |
| 355 |
continue; |
| 356 |
accum.append('\0'); // ensure we don't run off the string |
| 357 |
char *pos = accum.data(); |
| 358 |
|
| 359 |
// skip to first `dup' token |
| 360 |
if (!got_any) { |
| 361 |
if (!(pos = strstr(pos, "dup"))) { |
| 362 |
pos = accum.data(); |
| 363 |
goto check_done; |
| 364 |
} |
| 365 |
} |
| 366 |
|
| 367 |
// parse as many `dup INDEX */CHARNAME put' as there are in the line |
| 368 |
while (1) { |
| 369 |
// skip spaces, look for `dup ' |
| 370 |
while (isspace((unsigned char) pos[0])) |
| 371 |
++pos; |
| 372 |
if (pos[0] == '%') |
| 373 |
pos = skip_comment_space(pos); |
| 374 |
if (pos[0] != 'd' || pos[1] != 'u' || pos[2] != 'p' || !isspace((unsigned char) pos[3])) |
| 375 |
break; |
| 376 |
|
| 377 |
// look for `INDEX */' |
| 378 |
char *scan; |
| 379 |
int char_value = strtol(pos + 4, &scan, 10); |
| 380 |
if (scan[0] == '#' && char_value > 0 && char_value < 37 |
| 381 |
&& isalnum((unsigned char) scan[1])) |
| 382 |
char_value = strtol(scan + 1, &scan, char_value); |
| 383 |
while (isspace((unsigned char) scan[0])) |
| 384 |
scan++; |
| 385 |
if (char_value < 0 || char_value >= 256 || scan[0] != '/') |
| 386 |
break; |
| 387 |
|
| 388 |
// look for `CHARNAME put' |
| 389 |
scan++; |
| 390 |
char *name_pos = scan; |
| 391 |
while (!isspace((unsigned char) scan[0]) && scan[0] != '\0') |
| 392 |
++scan; |
| 393 |
char *name_end = scan; |
| 394 |
while (isspace((unsigned char) scan[0])) |
| 395 |
++scan; |
| 396 |
if (scan[0] != 'p' || scan[1] != 'u' || scan[2] != 't') |
| 397 |
break; |
| 398 |
|
| 399 |
_encoding->put(char_value, PermString(name_pos, name_end - name_pos)); |
| 400 |
got_any = true; |
| 401 |
pos = scan + 3; |
| 402 |
} |
| 403 |
|
| 404 |
check_done: |
| 405 |
// check for end of encoding section |
| 406 |
// if not over, add COPY ITEM for leftovers we didn't parse |
| 407 |
if ((strstr(pos, "readonly") != 0 || strstr(pos, "def") != 0) |
| 408 |
&& (got_any || strstr(pos, "for") == 0)) { |
| 409 |
_encoding->set_definer(String(pos)); |
| 410 |
return; |
| 411 |
} else if (got_any && *pos) |
| 412 |
add_item(new Type1CopyItem(String(pos))); |
| 413 |
|
| 414 |
accum.clear(); |
| 415 |
} |
| 416 |
} |
| 417 |
|
| 418 |
static bool |
| 419 |
read_synthetic_string(Type1Reader &reader, StringAccum &wrong_accum, |
| 420 |
const char *format, int *value) |
| 421 |
{ |
| 422 |
StringAccum accum; |
| 423 |
if (!reader.next_line(accum)) |
| 424 |
return false; |
| 425 |
wrong_accum << accum; |
| 426 |
accum.append('\0'); // ensure we don't run off the string |
| 427 |
int n = 0; |
| 428 |
if (value) |
| 429 |
sscanf(accum.data(), format, value, &n); |
| 430 |
else |
| 431 |
sscanf(accum.data(), format, &n); |
| 432 |
return (n != 0 && (isspace((unsigned char) accum[n]) || accum[n] == '\0')); |
| 433 |
} |
| 434 |
|
| 435 |
bool |
| 436 |
Type1Font::read_synthetic_font(Type1Reader &reader, const char *first_line, |
| 437 |
StringAccum &wrong_accum) |
| 438 |
{ |
| 439 |
// read font name |
| 440 |
PermString font_name; |
| 441 |
{ |
| 442 |
char *x = new char[strlen(first_line) + 1]; |
| 443 |
int n = 0; |
| 444 |
sscanf(first_line, "FontDirectory /%[^] \t\r\n[{}/] known {%n", x, &n); |
| 445 |
if (n && (isspace((unsigned char) first_line[n]) || first_line[n] == 0)) |
| 446 |
font_name = x; |
| 447 |
delete[] x; |
| 448 |
if (!font_name) |
| 449 |
return false; |
| 450 |
} |
| 451 |
|
| 452 |
// check UniqueID |
| 453 |
int unique_id; |
| 454 |
{ |
| 455 |
StringAccum accum; |
| 456 |
if (!reader.next_line(accum)) |
| 457 |
return false; |
| 458 |
wrong_accum << accum; |
| 459 |
accum.c_str(); // ensure we don't run off the string |
| 460 |
const char *y = accum.data(); |
| 461 |
if (*y != '/' || strncmp(y + 1, font_name.c_str(), font_name.length()) != 0) |
| 462 |
return false; |
| 463 |
int n = 0; |
| 464 |
sscanf(y + font_name.length() + 1, " findfont%n", &n); |
| 465 |
y = strstr(y, "/UniqueID get "); |
| 466 |
if (n == 0 || y == 0) |
| 467 |
return false; |
| 468 |
n = 0; |
| 469 |
sscanf(y + 14, "%d%n", &unique_id, &n); |
| 470 |
if (n == 0) |
| 471 |
return false; |
| 472 |
} |
| 473 |
|
| 474 |
// check lines that say how much text |
| 475 |
int multiplier; |
| 476 |
if (!read_synthetic_string(reader, wrong_accum, "save userdict /fbufstr %d string put%n", &multiplier)) |
| 477 |
return false; |
| 478 |
|
| 479 |
int multiplicand; |
| 480 |
if (!read_synthetic_string(reader, wrong_accum, "%d {currentfile fbufstr readstring { pop } { clear currentfile%n", &multiplicand)) |
| 481 |
return false; |
| 482 |
|
| 483 |
if (!read_synthetic_string(reader, wrong_accum, "closefile /fontdownload /unexpectedEOF /.error cvx exec } ifelse } repeat%n", 0)) |
| 484 |
return false; |
| 485 |
|
| 486 |
int extra; |
| 487 |
if (!read_synthetic_string(reader, wrong_accum, "currentfile %d string readstring { pop } { clear currentfile%n", &extra)) |
| 488 |
return false; |
| 489 |
|
| 490 |
if (!read_synthetic_string(reader, wrong_accum, "closefile /fontdownload /unexpectedEOF /.error cvx exec } ifelse%n", 0)) |
| 491 |
return false; |
| 492 |
|
| 493 |
if (!read_synthetic_string(reader, wrong_accum, "restore } if } if%n", 0)) |
| 494 |
return false; |
| 495 |
|
| 496 |
Type1SubsetReader subreader(&reader, multiplier*multiplicand + extra); |
| 497 |
Type1Font *synthetic = new Type1Font(subreader); |
| 498 |
if (!synthetic->ok()) |
| 499 |
delete synthetic; |
| 500 |
else { |
| 501 |
_synthetic_item = new Type1IncludedFont(synthetic, unique_id); |
| 502 |
add_item(_synthetic_item); |
| 503 |
} |
| 504 |
return true; |
| 505 |
} |
| 506 |
|
| 507 |
|
| 508 |
void |
| 509 |
Type1Font::undo_synthetic() |
| 510 |
{ |
| 511 |
// A synthetic font doesn't share arbitrary code with its base font; it |
| 512 |
// shares just the CharStrings dictionary, according to Adobe Type 1 Font |
| 513 |
// Format. We take advantage of this. |
| 514 |
|
| 515 |
if (!_synthetic_item) |
| 516 |
return; |
| 517 |
|
| 518 |
int mod_ii; |
| 519 |
for (mod_ii = nitems() - 1; mod_ii >= 0; mod_ii--) |
| 520 |
if (_items[mod_ii] == _synthetic_item) |
| 521 |
break; |
| 522 |
if (mod_ii < 0) |
| 523 |
return; |
| 524 |
|
| 525 |
// remove synthetic item and the reference to the included font |
| 526 |
_items[mod_ii] = new Type1NullItem; |
| 527 |
if (Type1CopyItem *copy = _items[mod_ii+1]->cast_copy()) |
| 528 |
if (copy->value().find_left("findfont") >= 0) { |
| 529 |
delete copy; |
| 530 |
_items[mod_ii+1] = new Type1NullItem; |
| 531 |
} |
| 532 |
|
| 533 |
Type1Font *f = _synthetic_item->included_font(); |
| 534 |
// its glyphs are already stored in our _glyphs array |
| 535 |
|
| 536 |
// copy SubrGroupItem from `f' into `this' |
| 537 |
Type1SubrGroupItem *oth_subrs = 0, *oth_glyphs = 0; |
| 538 |
for (int i = 0; i < f->nitems(); i++) |
| 539 |
if (Type1SubrGroupItem *subr_grp = f->_items[i]->cast_subr_group()) { |
| 540 |
if (subr_grp->is_subrs()) |
| 541 |
oth_subrs = subr_grp; |
| 542 |
else |
| 543 |
oth_glyphs = subr_grp; |
| 544 |
} |
| 545 |
|
| 546 |
assert(oth_glyphs != 0); |
| 547 |
|
| 548 |
for (int i = nitems() - 1; i >= 0; i--) |
| 549 |
if (Type1SubrGroupItem *subr_grp = _items[i]->cast_subr_group()) { |
| 550 |
assert(subr_grp->is_subrs()); |
| 551 |
if (oth_subrs) |
| 552 |
subr_grp->set_end_text(oth_subrs->end_text()); |
| 553 |
shift_indices(i + 1, 1); |
| 554 |
Type1SubrGroupItem *nsubr = new Type1SubrGroupItem(*oth_glyphs, this); |
| 555 |
_items[i + 1] = nsubr; |
| 556 |
break; |
| 557 |
} |
| 558 |
|
| 559 |
// delete included font |
| 560 |
f->_glyphs.clear(); // don't delete glyphs; we've stolen them |
| 561 |
delete _synthetic_item; |
| 562 |
_synthetic_item = 0; |
| 563 |
} |
| 564 |
|
| 565 |
|
| 566 |
Type1Charstring * |
| 567 |
Type1Font::subr(int e) const |
| 568 |
{ |
| 569 |
if (e >= 0 && e < _subrs.size() && _subrs[e]) |
| 570 |
return &_subrs[e]->t1cs(); |
| 571 |
else |
| 572 |
return 0; |
| 573 |
} |
| 574 |
|
| 575 |
PermString |
| 576 |
Type1Font::glyph_name(int i) const |
| 577 |
{ |
| 578 |
if (i >= 0 && i < _glyphs.size() && _glyphs[i]) |
| 579 |
return _glyphs[i]->name(); |
| 580 |
else |
| 581 |
return PermString(); |
| 582 |
} |
| 583 |
|
| 584 |
Type1Charstring * |
| 585 |
Type1Font::glyph(int i) const |
| 586 |
{ |
| 587 |
if (i >= 0 && i < _glyphs.size() && _glyphs[i]) |
| 588 |
return &_glyphs[i]->t1cs(); |
| 589 |
else |
| 590 |
return 0; |
| 591 |
} |
| 592 |
|
| 593 |
Type1Charstring * |
| 594 |
Type1Font::glyph(PermString name) const |
| 595 |
{ |
| 596 |
int i = _glyph_map[name]; |
| 597 |
if (i >= 0) |
| 598 |
return &_glyphs[i]->t1cs(); |
| 599 |
else |
| 600 |
return 0; |
| 601 |
} |
| 602 |
|
| 603 |
|
| 604 |
bool |
| 605 |
Type1Font::set_subr(int e, const Type1Charstring &t1cs, PermString definer) |
| 606 |
{ |
| 607 |
if (e < 0) |
| 608 |
return false; |
| 609 |
if (e >= _subrs.size()) |
| 610 |
_subrs.resize(e + 1, (Type1Subr *)0); |
| 611 |
|
| 612 |
if (!definer) { |
| 613 |
Type1Subr *pattern_subr = _subrs[e]; |
| 614 |
for (int i = 0; i < _subrs.size() && !pattern_subr; i++) |
| 615 |
pattern_subr = _subrs[i]; |
| 616 |
if (!pattern_subr) |
| 617 |
return false; |
| 618 |
definer = pattern_subr->definer(); |
| 619 |
} |
| 620 |
|
| 621 |
delete _subrs[e]; |
| 622 |
_subrs[e] = Type1Subr::make_subr(e, t1cs, definer); |
| 623 |
return true; |
| 624 |
} |
| 625 |
|
| 626 |
bool |
| 627 |
Type1Font::remove_subr(int e) |
| 628 |
{ |
| 629 |
if (e < 0 || e >= _subrs.size()) |
| 630 |
return false; |
| 631 |
delete _subrs[e]; |
| 632 |
_subrs[e] = 0; |
| 633 |
return true; |
| 634 |
} |
| 635 |
|
| 636 |
void |
| 637 |
Type1Font::fill_in_subrs() |
| 638 |
{ |
| 639 |
while (_subrs.size() && _subrs.back() == 0) |
| 640 |
_subrs.pop_back(); |
| 641 |
for (int i = 0; i < _subrs.size(); i++) |
| 642 |
if (!_subrs[i]) |
| 643 |
set_subr(i, Type1Charstring(String::make_stable("\013", 1))); |
| 644 |
} |
| 645 |
|
| 646 |
void |
| 647 |
Type1Font::renumber_subrs(const Vector<int> &renumbering) |
| 648 |
{ |
| 649 |
Vector<Type1Subr *> old_subrs; |
| 650 |
old_subrs.swap(_subrs); |
| 651 |
for (int i = 0; i < old_subrs.size() && i < renumbering.size(); i++) { |
| 652 |
int r = renumbering[i]; |
| 653 |
Type1Subr *s = old_subrs[i]; |
| 654 |
if (r >= 0 && s) |
| 655 |
set_subr(r, s->t1cs(), s->definer()); |
| 656 |
else |
| 657 |
delete s; |
| 658 |
} |
| 659 |
for (int i = renumbering.size(); i < old_subrs.size(); i++) |
| 660 |
delete old_subrs[i]; |
| 661 |
} |
| 662 |
|
| 663 |
|
| 664 |
void |
| 665 |
Type1Font::shift_indices(int move_index, int delta) |
| 666 |
{ |
| 667 |
if (delta > 0) { |
| 668 |
_items.resize(_items.size() + delta, (Type1Item *)0); |
| 669 |
memmove(&_items[move_index + delta], &_items[move_index], |
| 670 |
sizeof(Type1Item *) * (_items.size() - move_index - delta)); |
| 671 |
|
| 672 |
for (int i = dFont; i < dLast; i++) |
| 673 |
if (_index[i] > move_index) |
| 674 |
_index[i] += delta; |
| 675 |
|
| 676 |
} else { |
| 677 |
memmove(&_items[move_index], &_items[move_index - delta], |
| 678 |
sizeof(Type1Item *) * (_items.size() - (move_index - delta))); |
| 679 |
_items.resize(_items.size() + delta); |
| 680 |
|
| 681 |
for (int i = dFont; i < dLast; i++) |
| 682 |
if (_index[i] >= move_index) { |
| 683 |
if (_index[i] < move_index - delta) |
| 684 |
_index[i] = move_index; |
| 685 |
else |
| 686 |
_index[i] += delta; |
| 687 |
} |
| 688 |
} |
| 689 |
} |
| 690 |
|
| 691 |
Type1Definition * |
| 692 |
Type1Font::ensure(Dict dict, PermString name) |
| 693 |
{ |
| 694 |
assert(_index[dict] >= 0); |
| 695 |
Type1Definition *def = _dict[dict][name]; |
| 696 |
if (!def) { |
| 697 |
def = new Type1Definition(name, 0, "def"); |
| 698 |
int move_index = _index[dict]; |
| 699 |
shift_indices(move_index, 1); |
| 700 |
_items[move_index] = def; |
| 701 |
set_dict(dict, name, def); |
| 702 |
} |
| 703 |
return def; |
| 704 |
} |
| 705 |
|
| 706 |
void |
| 707 |
Type1Font::add_header_comment(const String &comment) |
| 708 |
{ |
| 709 |
int i; |
| 710 |
for (i = 0; i < _items.size(); i++) { |
| 711 |
Type1CopyItem *copy = _items[i]->cast_copy(); |
| 712 |
if (!copy || copy->value()[0] != '%') |
| 713 |
break; |
| 714 |
} |
| 715 |
shift_indices(i, 1); |
| 716 |
_items[i] = new Type1CopyItem(comment); |
| 717 |
} |
| 718 |
|
| 719 |
|
| 720 |
Type1Item * |
| 721 |
Type1Font::dict_size_item(int d) const |
| 722 |
{ |
| 723 |
switch (d) { |
| 724 |
|
| 725 |
case dF: |
| 726 |
if (_built && _index[d] > 0) |
| 727 |
return _items[_index[d] - 1]; |
| 728 |
break; |
| 729 |
|
| 730 |
case dFI: case dP: case dB: |
| 731 |
if (_index[d] > 0) |
| 732 |
return _items[_index[d] - 1]; |
| 733 |
break; |
| 734 |
|
| 735 |
case dBFI: |
| 736 |
if (Type1Item *t1i = b_dict("FontInfo")) |
| 737 |
return t1i; |
| 738 |
else if (_index[dBFI] > 0) |
| 739 |
return _items[_index[dBFI] - 1]; |
| 740 |
break; |
| 741 |
|
| 742 |
case dBP: |
| 743 |
if (Type1Item *t1i = b_dict("Private")) |
| 744 |
return t1i; |
| 745 |
else if (_index[dBP] > 0) |
| 746 |
return _items[_index[dBP] - 1]; |
| 747 |
break; |
| 748 |
|
| 749 |
} |
| 750 |
return 0; |
| 751 |
} |
| 752 |
|
| 753 |
int |
| 754 |
Type1Font::get_dict_size(int d) const |
| 755 |
{ |
| 756 |
Type1Item *item = dict_size_item(d); |
| 757 |
if (!item) |
| 758 |
/* nada */; |
| 759 |
else if (Type1Definition *t1d = item->cast_definition()) { |
| 760 |
int num; |
| 761 |
if (strstr(t1d->definer().c_str(), "dict") && t1d->value_int(num)) |
| 762 |
return num; |
| 763 |
} else if (Type1CopyItem *copy = item->cast_copy()) { |
| 764 |
String value = copy->value(); |
| 765 |
int pos = value.find_left(" dict"); |
| 766 |
if (pos >= 1 && isdigit((unsigned char) value[pos - 1])) { |
| 767 |
while (pos >= 1 && isdigit((unsigned char) value[pos - 1])) |
| 768 |
pos--; |
| 769 |
return strtol(value.data() + pos, 0, 10); |
| 770 |
} |
| 771 |
} |
| 772 |
return -1; |
| 773 |
} |
| 774 |
|
| 775 |
void |
| 776 |
Type1Font::set_dict_size(int d, int size) |
| 777 |
{ |
| 778 |
Type1Item *item = dict_size_item(d); |
| 779 |
if (!item) |
| 780 |
return; |
| 781 |
if (Type1Definition *t1d = item->cast_definition()) { |
| 782 |
int num; |
| 783 |
if (strstr(t1d->definer().c_str(), "dict") && t1d->value_int(num)) |
| 784 |
t1d->set_int(size); |
| 785 |
} else if (Type1CopyItem *copy = item->cast_copy()) { |
| 786 |
String value = copy->value(); |
| 787 |
int pos = value.find_left(" dict"); |
| 788 |
if (pos >= 1 && isdigit((unsigned char) value[pos - 1])) { |
| 789 |
int numpos = pos - 1; |
| 790 |
while (numpos >= 1 && isdigit((unsigned char) value[numpos - 1])) |
| 791 |
numpos--; |
| 792 |
StringAccum accum; |
| 793 |
accum << value.substring(0, numpos) << size << value.substring(pos); |
| 794 |
copy->set_value(accum.take_string()); |
| 795 |
} |
| 796 |
} |
| 797 |
} |
| 798 |
|
| 799 |
void |
| 800 |
Type1Font::write(Type1Writer &w) |
| 801 |
{ |
| 802 |
Type1Definition *lenIV_def = p_dict("lenIV"); |
| 803 |
int lenIV = 4; |
| 804 |
if (lenIV_def) |
| 805 |
lenIV_def->value_int(lenIV); |
| 806 |
w.set_charstring_start(_charstring_definer); |
| 807 |
w.set_lenIV(lenIV); |
| 808 |
|
| 809 |
// change dict sizes |
| 810 |
for (int i = dF; i < dLast; i++) |
| 811 |
set_dict_size(i, _dict[i].size() + _dict_deltas[i]); |
| 812 |
// XXX what if dict had nothing, but now has something? |
| 813 |
|
| 814 |
for (int i = 0; i < _items.size(); i++) |
| 815 |
_items[i]->gen(w); |
| 816 |
|
| 817 |
w.flush(); |
| 818 |
} |
| 819 |
|
| 820 |
void |
| 821 |
Type1Font::cache_defs() const |
| 822 |
{ |
| 823 |
Type1Definition *t1d = dict("FontName"); |
| 824 |
if (t1d) |
| 825 |
t1d->value_name(_font_name); |
| 826 |
_cached_defs = true; |
| 827 |
} |
| 828 |
|
| 829 |
MultipleMasterSpace * |
| 830 |
Type1Font::mmspace() const |
| 831 |
{ |
| 832 |
if (!_cached_mmspace) |
| 833 |
create_mmspace(); |
| 834 |
return _mmspace; |
| 835 |
} |
| 836 |
|
| 837 |
MultipleMasterSpace * |
| 838 |
Type1Font::create_mmspace(ErrorHandler *errh) const |
| 839 |
{ |
| 840 |
if (_cached_mmspace) |
| 841 |
return _mmspace; |
| 842 |
_cached_mmspace = 1; |
| 843 |
|
| 844 |
Type1Definition *t1d; |
| 845 |
|
| 846 |
Vector< Vector<double> > master_positions; |
| 847 |
t1d = fi_dict("BlendDesignPositions"); |
| 848 |
if (!t1d || !t1d->value_numvec_vec(master_positions)) |
| 849 |
return 0; |
| 850 |
|
| 851 |
int nmasters = master_positions.size(); |
| 852 |
if (nmasters <= 0) { |
| 853 |
if (errh) |
| 854 |
errh->error("bad BlendDesignPositions"); |
| 855 |
return 0; |
| 856 |
} |
| 857 |
int naxes = master_positions[0].size(); |
| 858 |
_mmspace = new MultipleMasterSpace(font_name(), naxes, nmasters); |
| 859 |
_mmspace->set_master_positions(master_positions); |
| 860 |
|
| 861 |
Vector< Vector<double> > normalize_in, normalize_out; |
| 862 |
t1d = fi_dict("BlendDesignMap"); |
| 863 |
if (t1d && t1d->value_normalize(normalize_in, normalize_out)) |
| 864 |
_mmspace->set_normalize(normalize_in, normalize_out); |
| 865 |
|
| 866 |
Vector<PermString> axis_types; |
| 867 |
t1d = fi_dict("BlendAxisTypes"); |
| 868 |
if (t1d && t1d->value_namevec(axis_types) && axis_types.size() == naxes) |
| 869 |
for (int a = 0; a < naxes; a++) |
| 870 |
_mmspace->set_axis_type(a, axis_types[a]); |
| 871 |
|
| 872 |
int ndv, cdv; |
| 873 |
Type1Charstring *cs; |
| 874 |
t1d = p_dict("NDV"); |
| 875 |
if (t1d && t1d->value_int(ndv) && (cs = subr(ndv))) |
| 876 |
_mmspace->set_ndv(*cs); |
| 877 |
t1d = p_dict("CDV"); |
| 878 |
if (t1d && t1d->value_int(cdv) && (cs = subr(cdv))) |
| 879 |
_mmspace->set_cdv(*cs); |
| 880 |
|
| 881 |
Vector<double> design_vector; |
| 882 |
t1d = dict("DesignVector"); |
| 883 |
if (t1d && t1d->value_numvec(design_vector)) |
| 884 |
_mmspace->set_design_vector(design_vector); |
| 885 |
|
| 886 |
Vector<double> weight_vector; |
| 887 |
t1d = dict("WeightVector"); |
| 888 |
if (t1d && t1d->value_numvec(weight_vector)) |
| 889 |
_mmspace->set_weight_vector(weight_vector); |
| 890 |
|
| 891 |
if (!_mmspace->check(errh)) { |
| 892 |
delete _mmspace; |
| 893 |
_mmspace = 0; |
| 894 |
} |
| 895 |
return _mmspace; |
| 896 |
} |
| 897 |
|
| 898 |
void |
| 899 |
Type1Font::uncache_defs() |
| 900 |
{ |
| 901 |
_cached_defs = false; |
| 902 |
} |
| 903 |
|
| 904 |
void |
| 905 |
Type1Font::font_matrix(double matrix[6]) const |
| 906 |
{ |
| 907 |
Vector<double> t1d_matrix; |
| 908 |
Type1Definition *t1d = dict("FontMatrix"); |
| 909 |
if (t1d && t1d->value_numvec(t1d_matrix) && t1d_matrix.size() == 6) |
| 910 |
memcpy(&matrix[0], &t1d_matrix[0], sizeof(double) * 6); |
| 911 |
else { |
| 912 |
matrix[0] = matrix[3] = 0.001; |
| 913 |
matrix[1] = matrix[2] = matrix[4] = matrix[5] = 0; |
| 914 |
} |
| 915 |
} |
| 916 |
|
| 917 |
} |