| 1 |
/* This is dvipdfmx, an eXtended version of dvipdfm by Mark A. Wicks. |
| 2 |
|
| 3 |
Copyright (C) 2002-2020 by Jin-Hwan Cho and Shunsaku Hirata, |
| 4 |
the dvipdfmx project team. |
| 5 |
|
| 6 |
This program is free software; you can redistribute it and/or modify |
| 7 |
it under the terms of the GNU General Public License as published by |
| 8 |
the Free Software Foundation; either version 2 of the License, or |
| 9 |
(at your option) any later version. |
| 10 |
|
| 11 |
This program is distributed in the hope that it will be useful, |
| 12 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 |
GNU General Public License for more details. |
| 15 |
|
| 16 |
You should have received a copy of the GNU General Public License |
| 17 |
along with this program; if not, write to the Free Software |
| 18 |
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. |
| 19 |
*/ |
| 20 |
|
| 21 |
/* |
| 22 |
* CFF Font Dictionary |
| 23 |
* |
| 24 |
* Adobe Technical Note #5176 "The Compact Font Format Specification" |
| 25 |
*/ |
| 26 |
|
| 27 |
#ifdef HAVE_CONFIG_H |
| 28 |
#include <config.h> |
| 29 |
#endif |
| 30 |
|
| 31 |
#include <math.h> |
| 32 |
#include <stdlib.h> |
| 33 |
#include <errno.h> |
| 34 |
|
| 35 |
#include "error.h" |
| 36 |
#include "mem.h" |
| 37 |
|
| 38 |
#ifndef CFF_DEBUG_STR |
| 39 |
#define CFF_DEBUG_STR "CFF" |
| 40 |
#define CFF_DEBUG 5 |
| 41 |
#endif |
| 42 |
|
| 43 |
/* work_buffer for get_real() */ |
| 44 |
#include "mfileio.h" |
| 45 |
|
| 46 |
#include "cff_types.h" |
| 47 |
#include "cff_limits.h" |
| 48 |
|
| 49 |
/* #include "cff_string.h" */ |
| 50 |
#include "cff_dict.h" |
| 51 |
/* cff_update_dict requires this. */ |
| 52 |
#include "cff.h" |
| 53 |
|
| 54 |
#define CFF_PARSE_OK 0 |
| 55 |
#define CFF_ERROR_PARSE_ERROR -1 |
| 56 |
#define CFF_ERROR_STACK_OVERFLOW -2 |
| 57 |
#define CFF_ERROR_STACK_UNDERFLOW -3 |
| 58 |
#define CFF_ERROR_STACK_RANGECHECK -4 |
| 59 |
|
| 60 |
#define DICT_ENTRY_MAX 16 |
| 61 |
cff_dict *cff_new_dict (void) |
| 62 |
{ |
| 63 |
cff_dict *dict; |
| 64 |
|
| 65 |
dict = NEW(1, cff_dict); |
| 66 |
dict->max = DICT_ENTRY_MAX; |
| 67 |
dict->count = 0; |
| 68 |
dict->entries = NEW(dict->max, cff_dict_entry); |
| 69 |
|
| 70 |
return dict; |
| 71 |
} |
| 72 |
|
| 73 |
void cff_release_dict (cff_dict *dict) |
| 74 |
{ |
| 75 |
if (dict) { |
| 76 |
if (dict->entries) { |
| 77 |
int i; |
| 78 |
for (i=0;i<dict->count;i++) { |
| 79 |
if ((dict->entries)[i].values) |
| 80 |
RELEASE((dict->entries)[i].values); |
| 81 |
} |
| 82 |
RELEASE(dict->entries); |
| 83 |
} |
| 84 |
RELEASE(dict); |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
/* |
| 89 |
* Operand stack: |
| 90 |
* only numbers are stored (as double) |
| 91 |
* |
| 92 |
* Operand types: |
| 93 |
* |
| 94 |
* number : double (integer or real) |
| 95 |
* boolean: stored as a number |
| 96 |
* SID : stored as a number |
| 97 |
* array : array of numbers |
| 98 |
* delta : array of numbers |
| 99 |
*/ |
| 100 |
|
| 101 |
#define CFF_DICT_STACK_LIMIT 64 |
| 102 |
static int stack_top = 0; |
| 103 |
static double arg_stack[CFF_DICT_STACK_LIMIT]; |
| 104 |
|
| 105 |
/* |
| 106 |
* CFF DICT encoding: |
| 107 |
* TODO: default values |
| 108 |
*/ |
| 109 |
|
| 110 |
#define CFF_LAST_DICT_OP1 22 |
| 111 |
#define CFF_LAST_DICT_OP2 39 |
| 112 |
#define CFF_LAST_DICT_OP (CFF_LAST_DICT_OP1 + CFF_LAST_DICT_OP2) |
| 113 |
|
| 114 |
static struct { |
| 115 |
const char *opname; |
| 116 |
int argtype; |
| 117 |
} dict_operator[CFF_LAST_DICT_OP] = { |
| 118 |
{"version", CFF_TYPE_SID}, |
| 119 |
{"Notice", CFF_TYPE_SID}, |
| 120 |
{"FullName", CFF_TYPE_SID}, |
| 121 |
{"FamilyName", CFF_TYPE_SID}, |
| 122 |
{"Weight", CFF_TYPE_SID}, |
| 123 |
{"FontBBox", CFF_TYPE_ARRAY}, |
| 124 |
{"BlueValues", CFF_TYPE_DELTA}, |
| 125 |
{"OtherBlues", CFF_TYPE_DELTA}, |
| 126 |
{"FamilyBlues", CFF_TYPE_DELTA}, |
| 127 |
{"FamilyOtherBlues", CFF_TYPE_DELTA}, |
| 128 |
{"StdHW", CFF_TYPE_NUMBER}, |
| 129 |
{"StdVW", CFF_TYPE_NUMBER}, |
| 130 |
{NULL, -1}, /* first byte of two-byte operator */ |
| 131 |
/* Top */ |
| 132 |
{"UniqueID", CFF_TYPE_NUMBER}, |
| 133 |
{"XUID", CFF_TYPE_ARRAY}, |
| 134 |
{"charset", CFF_TYPE_OFFSET}, |
| 135 |
{"Encoding", CFF_TYPE_OFFSET}, |
| 136 |
{"CharStrings", CFF_TYPE_OFFSET}, |
| 137 |
{"Private", CFF_TYPE_SZOFF}, /* two numbers (size and offset) */ |
| 138 |
/* Private */ |
| 139 |
{"Subrs", CFF_TYPE_OFFSET}, |
| 140 |
{"defaultWidthX", CFF_TYPE_NUMBER}, |
| 141 |
{"nominalWidthX", CFF_TYPE_NUMBER}, |
| 142 |
/* Operator 2 */ |
| 143 |
{"Copyright", CFF_TYPE_SID}, |
| 144 |
{"IsFixedPitch", CFF_TYPE_BOOLEAN}, |
| 145 |
{"ItalicAngle", CFF_TYPE_NUMBER}, |
| 146 |
{"UnderlinePosition", CFF_TYPE_NUMBER}, |
| 147 |
{"UnderlineThickness", CFF_TYPE_NUMBER}, |
| 148 |
{"PaintType", CFF_TYPE_NUMBER}, |
| 149 |
{"CharstringType", CFF_TYPE_NUMBER}, |
| 150 |
{"FontMatrix", CFF_TYPE_ARRAY}, |
| 151 |
{"StrokeWidth", CFF_TYPE_NUMBER}, |
| 152 |
{"BlueScale", CFF_TYPE_NUMBER}, |
| 153 |
{"BlueShift", CFF_TYPE_NUMBER}, |
| 154 |
{"BlueFuzz", CFF_TYPE_NUMBER}, |
| 155 |
{"StemSnapH", CFF_TYPE_DELTA}, |
| 156 |
{"StemSnapV", CFF_TYPE_DELTA}, |
| 157 |
{"ForceBold", CFF_TYPE_BOOLEAN}, |
| 158 |
{NULL, -1}, |
| 159 |
{NULL, -1}, |
| 160 |
{"LanguageGroup", CFF_TYPE_NUMBER}, |
| 161 |
{"ExpansionFactor", CFF_TYPE_NUMBER}, |
| 162 |
{"InitialRandomSeed", CFF_TYPE_NUMBER}, |
| 163 |
{"SyntheticBase", CFF_TYPE_NUMBER}, |
| 164 |
{"PostScript", CFF_TYPE_SID}, |
| 165 |
{"BaseFontName", CFF_TYPE_SID}, |
| 166 |
{"BaseFontBlend", CFF_TYPE_DELTA}, /* MMaster ? */ |
| 167 |
{NULL, -1}, |
| 168 |
{NULL, -1}, |
| 169 |
{NULL, -1}, |
| 170 |
{NULL, -1}, |
| 171 |
{NULL, -1}, |
| 172 |
{NULL, -1}, |
| 173 |
/* CID-Keyed font */ |
| 174 |
{"ROS", CFF_TYPE_ROS}, /* SID SID number */ |
| 175 |
{"CIDFontVersion", CFF_TYPE_NUMBER}, |
| 176 |
{"CIDFontRevision", CFF_TYPE_NUMBER}, |
| 177 |
{"CIDFontType", CFF_TYPE_NUMBER}, |
| 178 |
{"CIDCount", CFF_TYPE_NUMBER}, |
| 179 |
{"UIDBase", CFF_TYPE_NUMBER}, |
| 180 |
{"FDArray", CFF_TYPE_OFFSET}, |
| 181 |
{"FDSelect", CFF_TYPE_OFFSET}, |
| 182 |
{"FontName", CFF_TYPE_SID}, |
| 183 |
}; |
| 184 |
|
| 185 |
/* Parse DICT data */ |
| 186 |
static double get_integer (card8 **data, card8 *endptr, int *status) |
| 187 |
{ |
| 188 |
int result = 0; |
| 189 |
card8 b0, b1, b2; |
| 190 |
|
| 191 |
b0 = *(*data)++; |
| 192 |
if (b0 == 28 && *data < endptr - 2) { /* shortint */ |
| 193 |
b1 = *(*data)++; |
| 194 |
b2 = *(*data)++; |
| 195 |
result = b1*256+b2; |
| 196 |
if (result > 0x7fffL) |
| 197 |
result -= 0x10000L; |
| 198 |
} else if (b0 == 29 && *data < endptr - 4) { /* longint */ |
| 199 |
int i; |
| 200 |
result = *(*data)++; |
| 201 |
if (result > 0x7f) |
| 202 |
result -= 0x100; |
| 203 |
for (i=0;i<3;i++) { |
| 204 |
result = result*256+(**data); |
| 205 |
*data += 1; |
| 206 |
} |
| 207 |
} else if (b0 >= 32 && b0 <= 246) { /* int (1) */ |
| 208 |
result = b0 - 139; |
| 209 |
} else if (b0 >= 247 && b0 <= 250) { /* int (2) */ |
| 210 |
b1 = *(*data)++; |
| 211 |
result = (b0-247)*256+b1+108; |
| 212 |
} else if (b0 >= 251 && b0 <= 254) { |
| 213 |
b1 = *(*data)++; |
| 214 |
result = -(b0-251)*256-b1-108; |
| 215 |
} else { |
| 216 |
*status = CFF_ERROR_PARSE_ERROR; |
| 217 |
} |
| 218 |
|
| 219 |
return (double) result; |
| 220 |
} |
| 221 |
|
| 222 |
/* Simply uses strtod */ |
| 223 |
static double get_real(card8 **data, card8 *endptr, int *status) |
| 224 |
{ |
| 225 |
double result = 0.0; |
| 226 |
int nibble = 0, pos = 0; |
| 227 |
int len = 0, fail = 0; |
| 228 |
|
| 229 |
if (**data != 30 || *data >= endptr -1) { |
| 230 |
*status = CFF_ERROR_PARSE_ERROR; |
| 231 |
return 0.0; |
| 232 |
} |
| 233 |
|
| 234 |
*data += 1; /* skip first byte (30) */ |
| 235 |
|
| 236 |
pos = 0; |
| 237 |
while ((! fail) && len < WORK_BUFFER_SIZE - 2 && *data < endptr) { |
| 238 |
/* get nibble */ |
| 239 |
if (pos % 2) { |
| 240 |
nibble = **data & 0x0f; |
| 241 |
*data += 1; |
| 242 |
} else { |
| 243 |
nibble = (**data >> 4) & 0x0f; |
| 244 |
} |
| 245 |
if (nibble >= 0x00 && nibble <= 0x09) { |
| 246 |
work_buffer[len++] = nibble + '0'; |
| 247 |
} else if (nibble == 0x0a) { /* . */ |
| 248 |
work_buffer[len++] = '.'; |
| 249 |
} else if (nibble == 0x0b || nibble == 0x0c) { /* E, E- */ |
| 250 |
work_buffer[len++] = 'e'; |
| 251 |
if (nibble == 0x0c) |
| 252 |
work_buffer[len++] = '-'; |
| 253 |
} else if (nibble == 0x0e) { /* `-' */ |
| 254 |
work_buffer[len++] = '-'; |
| 255 |
} else if (nibble == 0x0d) { /* skip */ |
| 256 |
/* do nothing */ |
| 257 |
} else if (nibble == 0x0f) { /* end */ |
| 258 |
work_buffer[len++] = '\0'; |
| 259 |
if (((pos % 2) == 0) && (**data != 0xff)) { |
| 260 |
fail = 1; |
| 261 |
} |
| 262 |
break; |
| 263 |
} else { /* invalid */ |
| 264 |
fail = 1; |
| 265 |
} |
| 266 |
pos++; |
| 267 |
} |
| 268 |
|
| 269 |
/* returned values */ |
| 270 |
if (fail || nibble != 0x0f) { |
| 271 |
*status = CFF_ERROR_PARSE_ERROR; |
| 272 |
} else { |
| 273 |
char *s; |
| 274 |
result = strtod(work_buffer, &s); |
| 275 |
if (*s != 0 || errno == ERANGE) { |
| 276 |
*status = CFF_ERROR_PARSE_ERROR; |
| 277 |
} |
| 278 |
} |
| 279 |
|
| 280 |
return result; |
| 281 |
} |
| 282 |
|
| 283 |
/* operators */ |
| 284 |
static void add_dict (cff_dict *dict, |
| 285 |
card8 **data, card8 *endptr, int *status) |
| 286 |
{ |
| 287 |
int id, argtype; |
| 288 |
|
| 289 |
id = **data; |
| 290 |
if (id == 0x0c) { |
| 291 |
*data += 1; |
| 292 |
if (*data >= endptr || |
| 293 |
(id = **data + CFF_LAST_DICT_OP1) >= CFF_LAST_DICT_OP) { |
| 294 |
*status = CFF_ERROR_PARSE_ERROR; |
| 295 |
return; |
| 296 |
} |
| 297 |
} else if (id >= CFF_LAST_DICT_OP1) { |
| 298 |
*status = CFF_ERROR_PARSE_ERROR; |
| 299 |
return; |
| 300 |
} |
| 301 |
|
| 302 |
argtype = dict_operator[id].argtype; |
| 303 |
if (dict_operator[id].opname == NULL || argtype < 0) { |
| 304 |
/* YuppySC-Regular.otf from OS X for instance uses op id 37, simply ignore |
| 305 |
this dict instead of treat it as parsing error. */ |
| 306 |
return; |
| 307 |
} |
| 308 |
|
| 309 |
if (dict->count >= dict->max) { |
| 310 |
dict->max += DICT_ENTRY_MAX; |
| 311 |
dict->entries = RENEW(dict->entries, dict->max, cff_dict_entry); |
| 312 |
} |
| 313 |
|
| 314 |
(dict->entries)[dict->count].id = id; |
| 315 |
(dict->entries)[dict->count].key = dict_operator[id].opname; |
| 316 |
if (argtype == CFF_TYPE_NUMBER || |
| 317 |
argtype == CFF_TYPE_BOOLEAN || |
| 318 |
argtype == CFF_TYPE_SID || |
| 319 |
argtype == CFF_TYPE_OFFSET) { |
| 320 |
/* check for underflow here, as exactly one operand is expected */ |
| 321 |
if (stack_top < 1) { |
| 322 |
*status = CFF_ERROR_STACK_UNDERFLOW; |
| 323 |
return; |
| 324 |
} |
| 325 |
stack_top--; |
| 326 |
(dict->entries)[dict->count].count = 1; |
| 327 |
(dict->entries)[dict->count].values = NEW(1, double); |
| 328 |
(dict->entries)[dict->count].values[0] = arg_stack[stack_top]; |
| 329 |
dict->count += 1; |
| 330 |
} else { |
| 331 |
/* just ignore operator if there were no operands provided; |
| 332 |
don't treat this as underflow (e.g. StemSnapV in TemporaLGCUni-Italic.otf) */ |
| 333 |
if (stack_top > 0) |
| 334 |
{ |
| 335 |
(dict->entries)[dict->count].count = stack_top; |
| 336 |
(dict->entries)[dict->count].values = NEW(stack_top, double); |
| 337 |
while (stack_top > 0) { |
| 338 |
stack_top--; |
| 339 |
(dict->entries)[dict->count].values[stack_top] = arg_stack[stack_top]; |
| 340 |
} |
| 341 |
dict->count += 1; |
| 342 |
} |
| 343 |
} |
| 344 |
|
| 345 |
*data += 1; |
| 346 |
|
| 347 |
return; |
| 348 |
} |
| 349 |
|
| 350 |
/* |
| 351 |
* All operands are treated as number or array of numbers. |
| 352 |
* Private: two numbers, size and offset |
| 353 |
* ROS : three numbers, SID, SID, and a number |
| 354 |
*/ |
| 355 |
cff_dict *cff_dict_unpack (card8 *data, card8 *endptr) |
| 356 |
{ |
| 357 |
cff_dict *dict; |
| 358 |
int status = CFF_PARSE_OK; |
| 359 |
|
| 360 |
stack_top = 0; |
| 361 |
|
| 362 |
dict = cff_new_dict(); |
| 363 |
while (data < endptr && status == CFF_PARSE_OK) { |
| 364 |
if (*data < 22) { /* operator */ |
| 365 |
add_dict(dict, &data, endptr, &status); |
| 366 |
} else if (*data == 30) { /* real - First byte of a sequence (variable) */ |
| 367 |
if (stack_top < CFF_DICT_STACK_LIMIT) { |
| 368 |
arg_stack[stack_top] = get_real(&data, endptr, &status); |
| 369 |
stack_top++; |
| 370 |
} else { |
| 371 |
status = CFF_ERROR_STACK_OVERFLOW; |
| 372 |
} |
| 373 |
} else if (*data == 255 || (*data >= 22 && *data <= 27)) { /* reserved */ |
| 374 |
data++; |
| 375 |
} else { /* everything else are integer */ |
| 376 |
if (stack_top < CFF_DICT_STACK_LIMIT) { |
| 377 |
arg_stack[stack_top] = get_integer(&data, endptr, &status); |
| 378 |
stack_top++; |
| 379 |
} else { |
| 380 |
status = CFF_ERROR_STACK_OVERFLOW; |
| 381 |
} |
| 382 |
} |
| 383 |
} |
| 384 |
|
| 385 |
if (status != CFF_PARSE_OK) { |
| 386 |
ERROR("%s: Parsing CFF DICT failed. (error=%d)", CFF_DEBUG_STR, status); |
| 387 |
} else if (stack_top != 0) { |
| 388 |
WARN("%s: Garbage in CFF DICT data.", CFF_DEBUG_STR); |
| 389 |
stack_top = 0; |
| 390 |
} |
| 391 |
|
| 392 |
return dict; |
| 393 |
} |
| 394 |
|
| 395 |
/* Pack DICT data */ |
| 396 |
static int pack_integer (card8 *dest, int destlen, int value) |
| 397 |
{ |
| 398 |
int len = 0; |
| 399 |
|
| 400 |
if (value >= -107 && value <= 107) { |
| 401 |
if (destlen < 1) |
| 402 |
ERROR("%s: Buffer overflow.", CFF_DEBUG_STR); |
| 403 |
dest[0] = (value + 139) & 0xff; |
| 404 |
len = 1; |
| 405 |
} else if (value >= 108 && value <= 1131) { |
| 406 |
if (destlen < 2) |
| 407 |
ERROR("%s: Buffer overflow.", CFF_DEBUG_STR); |
| 408 |
value = 0xf700u + value - 108; |
| 409 |
dest[0] = (value >> 8) & 0xff; |
| 410 |
dest[1] = value & 0xff; |
| 411 |
len = 2; |
| 412 |
} else if (value >= -1131 && value <= -108) { |
| 413 |
if (destlen < 2) |
| 414 |
ERROR("%s: Buffer overflow.", CFF_DEBUG_STR); |
| 415 |
value = 0xfb00u - value - 108; |
| 416 |
dest[0] = (value >> 8) & 0xff; |
| 417 |
dest[1] = value & 0xff; |
| 418 |
len = 2; |
| 419 |
} else if (value >= -32768 && value <= 32767) { /* shortint */ |
| 420 |
if (destlen < 3) |
| 421 |
ERROR("%s: Buffer overflow.", CFF_DEBUG_STR); |
| 422 |
dest[0] = 28; |
| 423 |
dest[1] = (value >> 8) & 0xff; |
| 424 |
dest[2] = value & 0xff; |
| 425 |
len = 3; |
| 426 |
} else { /* longint */ |
| 427 |
if (destlen < 5) |
| 428 |
ERROR("%s: Buffer overflow.", CFF_DEBUG_STR); |
| 429 |
dest[0] = 29; |
| 430 |
dest[1] = (value >> 24) & 0xff; |
| 431 |
dest[2] = (value >> 16) & 0xff; |
| 432 |
dest[3] = (value >> 8) & 0xff; |
| 433 |
dest[4] = value & 0xff; |
| 434 |
len = 5; |
| 435 |
} |
| 436 |
|
| 437 |
return len; |
| 438 |
} |
| 439 |
|
| 440 |
static int pack_real (card8 *dest, int destlen, double value) |
| 441 |
{ |
| 442 |
int i = 0, pos = 2; |
| 443 |
char buffer[32]; |
| 444 |
|
| 445 |
if (destlen < 2) |
| 446 |
ERROR("%s: Buffer overflow.", CFF_DEBUG_STR); |
| 447 |
|
| 448 |
dest[0] = 30; |
| 449 |
|
| 450 |
if (value == 0.0) { |
| 451 |
dest[1] = 0x0f; |
| 452 |
return 2; |
| 453 |
} |
| 454 |
|
| 455 |
if (value < 0.0) { |
| 456 |
dest[1] = 0xe0; |
| 457 |
value *= -1.0; |
| 458 |
pos++; |
| 459 |
} |
| 460 |
|
| 461 |
/* To avoid the problem with Mac OS X 10.4 Quartz, |
| 462 |
* change the presion of the real numbers |
| 463 |
* on June 27, 2007 for musix20.pfb */ |
| 464 |
sprintf(buffer, "%.13g", value); |
| 465 |
|
| 466 |
for (i = 0; buffer[i] != '\0'; i++) { |
| 467 |
unsigned char ch = 0; |
| 468 |
if (buffer[i] == '.') { |
| 469 |
ch = 0x0a; |
| 470 |
} else if (buffer[i] >= '0' && buffer[i] <= '9') { |
| 471 |
ch = buffer[i] - '0'; |
| 472 |
} else if (buffer[i] == 'e') { |
| 473 |
ch = (buffer[++i] == '-' ? 0x0c : 0x0b); |
| 474 |
} else { |
| 475 |
ERROR("%s: Invalid character.", CFF_DEBUG_STR); |
| 476 |
} |
| 477 |
|
| 478 |
if (destlen < pos/2 + 1) |
| 479 |
ERROR("%s: Buffer overflow.", CFF_DEBUG_STR); |
| 480 |
|
| 481 |
if (pos % 2) { |
| 482 |
dest[pos/2] += ch; |
| 483 |
} else { |
| 484 |
dest[pos/2] = (ch << 4); |
| 485 |
} |
| 486 |
pos++; |
| 487 |
} |
| 488 |
|
| 489 |
if (pos % 2) { |
| 490 |
dest[pos/2] += 0x0f; |
| 491 |
pos++; |
| 492 |
} else { |
| 493 |
if (destlen < pos/2 + 1) |
| 494 |
ERROR("%s: Buffer overflow.", CFF_DEBUG_STR); |
| 495 |
dest[pos/2] = 0xff; |
| 496 |
pos += 2; |
| 497 |
} |
| 498 |
|
| 499 |
return pos/2; |
| 500 |
} |
| 501 |
|
| 502 |
static int cff_dict_put_number (double value, |
| 503 |
card8 *dest, int destlen, |
| 504 |
int type) |
| 505 |
{ |
| 506 |
int len = 0; |
| 507 |
double nearint; |
| 508 |
|
| 509 |
nearint = floor(value+0.5); |
| 510 |
/* set offset to longint */ |
| 511 |
if (type == CFF_TYPE_OFFSET) { |
| 512 |
int lvalue; |
| 513 |
|
| 514 |
lvalue = (int) value; |
| 515 |
if (destlen < 5) |
| 516 |
ERROR("%s: Buffer overflow.", CFF_DEBUG_STR); |
| 517 |
dest[0] = 29; |
| 518 |
dest[1] = (lvalue >> 24) & 0xff; |
| 519 |
dest[2] = (lvalue >> 16) & 0xff; |
| 520 |
dest[3] = (lvalue >> 8) & 0xff; |
| 521 |
dest[4] = lvalue & 0xff; |
| 522 |
len = 5; |
| 523 |
} else if (value > CFF_INT_MAX || value < CFF_INT_MIN || |
| 524 |
(fabs(value - nearint) > 1.0e-5)) { /* real */ |
| 525 |
len = pack_real(dest, destlen, value); |
| 526 |
} else { /* integer */ |
| 527 |
len = pack_integer(dest, destlen, (int) nearint); |
| 528 |
} |
| 529 |
|
| 530 |
return len; |
| 531 |
} |
| 532 |
|
| 533 |
static int |
| 534 |
put_dict_entry (cff_dict_entry *de, |
| 535 |
card8 *dest, int destlen) |
| 536 |
{ |
| 537 |
int len = 0; |
| 538 |
int i, type, id; |
| 539 |
|
| 540 |
if (de->count > 0) { |
| 541 |
id = de->id; |
| 542 |
if (dict_operator[id].argtype == CFF_TYPE_OFFSET || |
| 543 |
dict_operator[id].argtype == CFF_TYPE_SZOFF) { |
| 544 |
type = CFF_TYPE_OFFSET; |
| 545 |
} else { |
| 546 |
type = CFF_TYPE_NUMBER; |
| 547 |
} |
| 548 |
for (i = 0; i < de->count; i++) { |
| 549 |
len += cff_dict_put_number(de->values[i], |
| 550 |
dest+len, |
| 551 |
destlen-len, type); |
| 552 |
} |
| 553 |
if (id >= 0 && id < CFF_LAST_DICT_OP1) { |
| 554 |
if (len + 1 > destlen) |
| 555 |
ERROR("%s: Buffer overflow.", CFF_DEBUG_STR); |
| 556 |
dest[len++] = id; |
| 557 |
} else if (id >= 0 && id < CFF_LAST_DICT_OP) { |
| 558 |
if (len + 2 > destlen) |
| 559 |
ERROR("in cff_dict_pack(): Buffer overflow"); |
| 560 |
dest[len++] = 12; |
| 561 |
dest[len++] = id - CFF_LAST_DICT_OP1; |
| 562 |
} else { |
| 563 |
ERROR("%s: Invalid CFF DICT operator ID.", CFF_DEBUG_STR); |
| 564 |
} |
| 565 |
} |
| 566 |
|
| 567 |
return len; |
| 568 |
} |
| 569 |
|
| 570 |
int cff_dict_pack (cff_dict *dict, card8 *dest, int destlen) |
| 571 |
{ |
| 572 |
int len = 0; |
| 573 |
int i; |
| 574 |
|
| 575 |
for (i = 0; i < dict->count; i++) { |
| 576 |
if (!strcmp(dict->entries[i].key, "ROS")) { |
| 577 |
len += put_dict_entry(&dict->entries[i], dest, destlen); |
| 578 |
break; |
| 579 |
} |
| 580 |
} |
| 581 |
for (i = 0; i < dict->count; i++) { |
| 582 |
if (strcmp(dict->entries[i].key, "ROS")) { |
| 583 |
len += put_dict_entry(&dict->entries[i], dest+len, destlen-len); |
| 584 |
} |
| 585 |
} |
| 586 |
|
| 587 |
return len; |
| 588 |
} |
| 589 |
|
| 590 |
void cff_dict_add (cff_dict *dict, const char *key, int count) |
| 591 |
{ |
| 592 |
int id, i; |
| 593 |
|
| 594 |
for (id=0;id<CFF_LAST_DICT_OP;id++) { |
| 595 |
if (key && dict_operator[id].opname && |
| 596 |
strcmp(dict_operator[id].opname, key) == 0) |
| 597 |
break; |
| 598 |
} |
| 599 |
|
| 600 |
if (id == CFF_LAST_DICT_OP) |
| 601 |
ERROR("%s: Unknown CFF DICT operator.", CFF_DEBUG_STR); |
| 602 |
|
| 603 |
for (i=0;i<dict->count;i++) { |
| 604 |
if ((dict->entries)[i].id == id) { |
| 605 |
if ((dict->entries)[i].count != count) |
| 606 |
ERROR("%s: Inconsistent DICT argument number.", CFF_DEBUG_STR); |
| 607 |
return; |
| 608 |
} |
| 609 |
} |
| 610 |
|
| 611 |
if (dict->count + 1 >= dict->max) { |
| 612 |
dict->max += 8; |
| 613 |
dict->entries = RENEW(dict->entries, dict->max, cff_dict_entry); |
| 614 |
} |
| 615 |
|
| 616 |
(dict->entries)[dict->count].id = id; |
| 617 |
(dict->entries)[dict->count].key = dict_operator[id].opname; |
| 618 |
(dict->entries)[dict->count].count = count; |
| 619 |
if (count > 0) { |
| 620 |
(dict->entries)[dict->count].values = NEW(count, double); |
| 621 |
memset((dict->entries)[dict->count].values, |
| 622 |
0, sizeof(double)*count); |
| 623 |
} else { |
| 624 |
(dict->entries)[dict->count].values = NULL; |
| 625 |
} |
| 626 |
dict->count += 1; |
| 627 |
|
| 628 |
return; |
| 629 |
} |
| 630 |
|
| 631 |
void cff_dict_remove (cff_dict *dict, const char *key) |
| 632 |
{ |
| 633 |
int i; |
| 634 |
for (i = 0; i < dict->count; i++) { |
| 635 |
if (key && strcmp(key, (dict->entries)[i].key) == 0) { |
| 636 |
(dict->entries)[i].count = 0; |
| 637 |
if ((dict->entries)[i].values) |
| 638 |
RELEASE((dict->entries)[i].values); |
| 639 |
(dict->entries)[i].values = NULL; |
| 640 |
} |
| 641 |
} |
| 642 |
} |
| 643 |
|
| 644 |
int cff_dict_known (cff_dict *dict, const char *key) |
| 645 |
{ |
| 646 |
int i; |
| 647 |
|
| 648 |
for (i = 0; i < dict->count; i++) { |
| 649 |
if (key && strcmp(key, (dict->entries)[i].key) == 0 |
| 650 |
&& (dict->entries)[i].count > 0) |
| 651 |
return 1; |
| 652 |
} |
| 653 |
|
| 654 |
return 0; |
| 655 |
} |
| 656 |
|
| 657 |
double cff_dict_get (cff_dict *dict, const char *key, int idx) |
| 658 |
{ |
| 659 |
double value = 0.0; |
| 660 |
int i; |
| 661 |
|
| 662 |
ASSERT(key && dict); |
| 663 |
|
| 664 |
for (i = 0; i < dict->count; i++) { |
| 665 |
if (strcmp(key, (dict->entries)[i].key) == 0) { |
| 666 |
if ((dict->entries)[i].count > idx) |
| 667 |
value = (dict->entries)[i].values[idx]; |
| 668 |
else |
| 669 |
ERROR("%s: Invalid index number.", CFF_DEBUG_STR); |
| 670 |
break; |
| 671 |
} |
| 672 |
} |
| 673 |
|
| 674 |
if (i == dict->count) |
| 675 |
ERROR("%s: DICT entry \"%s\" not found.", CFF_DEBUG_STR, key); |
| 676 |
|
| 677 |
return value; |
| 678 |
} |
| 679 |
|
| 680 |
void cff_dict_set (cff_dict *dict, const char *key, int idx, double value) |
| 681 |
{ |
| 682 |
int i; |
| 683 |
|
| 684 |
ASSERT(dict && key); |
| 685 |
|
| 686 |
for (i = 0 ; i < dict->count; i++) { |
| 687 |
if (strcmp(key, (dict->entries)[i].key) == 0) { |
| 688 |
if ((dict->entries)[i].count > idx) |
| 689 |
(dict->entries)[i].values[idx] = value; |
| 690 |
else |
| 691 |
ERROR("%s: Invalid index number.", CFF_DEBUG_STR); |
| 692 |
break; |
| 693 |
} |
| 694 |
} |
| 695 |
|
| 696 |
if (i == dict->count) |
| 697 |
ERROR("%s: DICT entry \"%s\" not found.", CFF_DEBUG_STR, key); |
| 698 |
} |
| 699 |
|
| 700 |
void cff_dict_update (cff_dict *dict, cff_font *cff) |
| 701 |
{ |
| 702 |
int i; |
| 703 |
|
| 704 |
for (i = 0;i < dict->count; i++) { |
| 705 |
if ((dict->entries)[i].count > 0) { |
| 706 |
char *str; |
| 707 |
int id; |
| 708 |
|
| 709 |
id = (dict->entries)[i].id; |
| 710 |
if (dict_operator[id].argtype == CFF_TYPE_SID) { |
| 711 |
str = cff_get_string(cff, (dict->entries)[i].values[0]); |
| 712 |
(dict->entries)[i].values[0] = cff_add_string(cff, str, 1); |
| 713 |
RELEASE(str); |
| 714 |
} else if (dict_operator[id].argtype == CFF_TYPE_ROS) { |
| 715 |
str = cff_get_string(cff, (dict->entries)[i].values[0]); |
| 716 |
(dict->entries)[i].values[0] = cff_add_string(cff, str, 1); |
| 717 |
RELEASE(str); |
| 718 |
str = cff_get_string(cff, (dict->entries)[i].values[1]); |
| 719 |
(dict->entries)[i].values[1] = cff_add_string(cff, str, 1); |
| 720 |
RELEASE(str); |
| 721 |
} |
| 722 |
} |
| 723 |
} |
| 724 |
} |