| 1 |
/* |
| 2 |
* ftlib.c |
| 3 |
* |
| 4 |
* This file is part of the ttf2pk package. |
| 5 |
* |
| 6 |
* Copyright 1997-1999, 2000, 2002, 2003 by |
| 7 |
* Frederic Loyer <[email protected]> |
| 8 |
* Werner Lemberg <[email protected]> |
| 9 |
* |
| 10 |
* Copyright (C) 2012, 2013 by |
| 11 |
* Peter Breitenlohner <[email protected]> |
| 12 |
*/ |
| 13 |
|
| 14 |
#ifdef HAVE_CONFIG_H |
| 15 |
#include <config.h> |
| 16 |
#endif |
| 17 |
|
| 18 |
#include <stdio.h> |
| 19 |
#include <stdlib.h> |
| 20 |
#include <string.h> |
| 21 |
#include <math.h> |
| 22 |
|
| 23 |
#include <ft2build.h> |
| 24 |
#include FT_FREETYPE_H |
| 25 |
#include FT_TRUETYPE_TABLES_H |
| 26 |
#include FT_OPENTYPE_VALIDATE_H |
| 27 |
|
| 28 |
#include "ttf2tfm.h" |
| 29 |
#include "ttfenc.h" |
| 30 |
#include "errormsg.h" |
| 31 |
#include "newobj.h" |
| 32 |
#include "ftlib.h" |
| 33 |
|
| 34 |
#define Macintosh_platform 1 |
| 35 |
#define Macintosh_encoding 0 |
| 36 |
|
| 37 |
#define Microsoft_platform 3 |
| 38 |
#define Microsoft_Symbol_encoding 0 |
| 39 |
#define Microsoft_Unicode_encoding 1 |
| 40 |
|
| 41 |
FT_Library engine; |
| 42 |
FT_Face face; |
| 43 |
|
| 44 |
FT_Matrix matrix1, matrix2; |
| 45 |
|
| 46 |
int dpi, ptsize; |
| 47 |
|
| 48 |
Boolean has_gsub; |
| 49 |
|
| 50 |
TT_OS2 *os2; |
| 51 |
TT_Postscript *postscript; |
| 52 |
|
| 53 |
FT_Byte *GSUB_table; |
| 54 |
FT_ULong GSUB_length; |
| 55 |
FT_ULong GSUB_ptr; |
| 56 |
|
| 57 |
static inline void |
| 58 |
need (FT_ULong needed) |
| 59 |
{ |
| 60 |
if (GSUB_ptr + needed > GSUB_length) |
| 61 |
oops("GSUB: Unexpected end of table."); |
| 62 |
} |
| 63 |
|
| 64 |
static inline FT_Byte |
| 65 |
get_Byte(void) |
| 66 |
{ |
| 67 |
return GSUB_table[GSUB_ptr++]; |
| 68 |
} |
| 69 |
|
| 70 |
static inline signed char |
| 71 |
get_SByte(void) |
| 72 |
{ |
| 73 |
return (signed char) GSUB_table[GSUB_ptr++]; |
| 74 |
} |
| 75 |
|
| 76 |
static FT_UShort |
| 77 |
get_UShort(void) |
| 78 |
{ |
| 79 |
unsigned int cc; |
| 80 |
cc = get_Byte() << 8; |
| 81 |
cc |= get_Byte(); |
| 82 |
return (FT_UShort) cc; |
| 83 |
} |
| 84 |
|
| 85 |
static FT_Short |
| 86 |
get_Short(void) |
| 87 |
{ |
| 88 |
int cc; |
| 89 |
cc = get_SByte() << 8; |
| 90 |
cc |= get_Byte(); |
| 91 |
return (FT_Short) cc; |
| 92 |
} |
| 93 |
|
| 94 |
static FT_ULong |
| 95 |
get_ULong(void) |
| 96 |
{ |
| 97 |
unsigned int cc; |
| 98 |
cc = get_Byte() << 24; |
| 99 |
cc |= get_Byte() << 16; |
| 100 |
cc |= get_Byte() << 8; |
| 101 |
cc |= get_Byte(); |
| 102 |
return (FT_ULong) cc; |
| 103 |
} |
| 104 |
|
| 105 |
#if 0 /* unused */ |
| 106 |
static FT_Long |
| 107 |
get_Long(void) |
| 108 |
{ |
| 109 |
int cc; |
| 110 |
cc = get_SByte() << 24; |
| 111 |
cc |= get_Byte() << 16; |
| 112 |
cc |= get_Byte() << 8; |
| 113 |
cc |= get_Byte(); |
| 114 |
return (FT_Long) cc; |
| 115 |
} |
| 116 |
#endif |
| 117 |
|
| 118 |
typedef struct { |
| 119 |
FT_UShort start; |
| 120 |
FT_UShort end; |
| 121 |
FT_UShort start_Index; |
| 122 |
} Range_Record; |
| 123 |
|
| 124 |
typedef struct { |
| 125 |
FT_ULong ptr; |
| 126 |
FT_UShort format; |
| 127 |
FT_UShort count; |
| 128 |
union { |
| 129 |
FT_UShort *glyphs; /* format = 1 */ |
| 130 |
Range_Record *ranges; /* format = 2 */ |
| 131 |
} data; |
| 132 |
} Coverage_Item; |
| 133 |
|
| 134 |
static inline void |
| 135 |
fetch_Glyphs (FT_UShort count, FT_UShort *glyphs) |
| 136 |
{ |
| 137 |
int i; |
| 138 |
|
| 139 |
need(2 * count); |
| 140 |
for (i = 0; i < count; i++) |
| 141 |
glyphs[i] = get_UShort(); |
| 142 |
for (i = 1; i < count; i++) |
| 143 |
if (glyphs[i] <= glyphs[i - 1]) |
| 144 |
oops("Glyph[%d]=%d and Glyph[%d]=%d: out of order.", i - 1, glyphs[i - 1], i, glyphs[i]); |
| 145 |
} |
| 146 |
|
| 147 |
static inline FT_UShort |
| 148 |
fetch_Ranges (FT_UShort count, Range_Record *ranges) |
| 149 |
{ |
| 150 |
int i; |
| 151 |
FT_UShort ndx = 0; |
| 152 |
|
| 153 |
need(6 * count); |
| 154 |
for (i = 0; i < count; i++) |
| 155 |
{ |
| 156 |
ranges[i].start = get_UShort(); |
| 157 |
ranges[i].end = get_UShort(); |
| 158 |
if (ranges[i].start > ranges[i].end) |
| 159 |
oops("Range[%d]: bad range=%d..%d.", i, ranges[i].start, ranges[i].end); |
| 160 |
ranges[i].start_Index = get_UShort(); |
| 161 |
if (ranges[i].start_Index != ndx) |
| 162 |
oops("Range[%d]: bad start_Index=%d (should be %d).", i, ranges[i].start_Index, ndx); |
| 163 |
ndx += (ranges[i].end - ranges[i].start) + 1; |
| 164 |
} |
| 165 |
for (i = 1; i < count; i++) |
| 166 |
if (ranges[i].start <= ranges[i-1].end) |
| 167 |
oops("Range[%d]=%d..%d and Range[%d]=%d..%d: out of order.", |
| 168 |
i - 1, ranges[i - 1].start, ranges[i - 1].end, i, ranges[i].start, ranges[i].end); |
| 169 |
return ndx; |
| 170 |
} |
| 171 |
|
| 172 |
static inline FT_UShort |
| 173 |
fetch_Coverage (Coverage_Item *coverage) |
| 174 |
{ |
| 175 |
FT_UShort res = 0; |
| 176 |
GSUB_ptr = coverage->ptr; |
| 177 |
need(4); |
| 178 |
coverage->format = get_UShort(); |
| 179 |
coverage->count = res = get_UShort(); |
| 180 |
switch (coverage->format) |
| 181 |
{ |
| 182 |
case 1: |
| 183 |
coverage->data.glyphs = mymalloc (res * sizeof(FT_UShort)); |
| 184 |
#ifdef DEBUG |
| 185 |
warning("Coverage Format=1 with %d Glyphs at 0x%04x.", res, coverage->ptr); |
| 186 |
#endif |
| 187 |
fetch_Glyphs (res, coverage->data.glyphs); |
| 188 |
break; |
| 189 |
case 2: |
| 190 |
coverage->data.ranges = mymalloc (res * sizeof(Range_Record)); |
| 191 |
#ifdef DEBUG |
| 192 |
warning("Coverage Format=1 with %d Ranges at 0x%04x.", res, coverage->ptr); |
| 193 |
#endif |
| 194 |
res = fetch_Ranges (res, coverage->data.ranges); |
| 195 |
break; |
| 196 |
default: |
| 197 |
oops("Coverage at 0x%04x: bad Format=%d.", coverage->ptr, coverage->format); |
| 198 |
} |
| 199 |
return res; |
| 200 |
} |
| 201 |
|
| 202 |
/* |
| 203 |
* We only accept Single Substitutions (and Extensions) |
| 204 |
*/ |
| 205 |
|
| 206 |
typedef struct { |
| 207 |
FT_UShort count; |
| 208 |
FT_UShort *glyphs; |
| 209 |
} Glyph_Array; |
| 210 |
|
| 211 |
typedef struct { |
| 212 |
FT_ULong ptr; |
| 213 |
FT_UShort format; |
| 214 |
Coverage_Item *coverage; |
| 215 |
union { |
| 216 |
FT_Short *delta; /* format = 1 */ |
| 217 |
Glyph_Array *array; /* format = 2 */ |
| 218 |
} data; |
| 219 |
} Single_Item; |
| 220 |
|
| 221 |
typedef struct _Subst_Item { |
| 222 |
Single_Item *item; |
| 223 |
struct _Subst_Item *next; |
| 224 |
} Subst_Item; |
| 225 |
|
| 226 |
Subst_Item *Subst_list, *Subst_last; |
| 227 |
|
| 228 |
static inline void |
| 229 |
add_Single (Single_Item *item) |
| 230 |
{ |
| 231 |
Subst_Item *entry = mycalloc (1, sizeof(Subst_Item)); |
| 232 |
entry->item = item; |
| 233 |
if (Subst_last) |
| 234 |
Subst_last->next = entry; |
| 235 |
else |
| 236 |
Subst_list = entry; |
| 237 |
Subst_last = entry; |
| 238 |
has_gsub = True; |
| 239 |
} |
| 240 |
|
| 241 |
static inline void |
| 242 |
fetch_Single (Single_Item *item) |
| 243 |
{ |
| 244 |
int i; |
| 245 |
FT_UShort val; |
| 246 |
|
| 247 |
item->coverage = mycalloc (1, sizeof(Coverage_Item)); |
| 248 |
GSUB_ptr = item->ptr; |
| 249 |
need(6); |
| 250 |
item->format = get_UShort(); |
| 251 |
item->coverage->ptr = item->ptr + get_UShort(); |
| 252 |
switch (item->format) |
| 253 |
{ |
| 254 |
case 1: |
| 255 |
val = get_Short(); |
| 256 |
item->data.delta = mycalloc (1, sizeof(FT_UShort)); |
| 257 |
*item->data.delta = val; |
| 258 |
#ifdef DEBUG |
| 259 |
warning("Single Substitution Format=1 with Delta=%d at 0x%04x.", val, item->ptr); |
| 260 |
#endif |
| 261 |
fetch_Coverage (item->coverage); |
| 262 |
break; |
| 263 |
case 2: |
| 264 |
val = get_UShort(); |
| 265 |
need(2 * val); |
| 266 |
item->data.array = mycalloc (1, sizeof(Glyph_Array)); |
| 267 |
item->data.array->count = val; |
| 268 |
item->data.array->glyphs = mycalloc (val, sizeof(FT_UShort)); |
| 269 |
for (i = 0; i < val; i++) |
| 270 |
item->data.array->glyphs[i] = get_UShort(); |
| 271 |
#ifdef DEBUG |
| 272 |
warning("Single Substitution Format=2 with %d Glyphs at 0x%04x.", val, item->ptr); |
| 273 |
#endif |
| 274 |
val = fetch_Coverage (item->coverage); |
| 275 |
if (val != item->data.array->count) |
| 276 |
oops("Coverage at 0x%04x: covers %d glyphs (should be %d).", item->ptr, val, item->data.array->count); |
| 277 |
break; |
| 278 |
default: |
| 279 |
oops("Single Substitution at 0x%04x: bad Format=%d.", item->ptr, item->format); |
| 280 |
} |
| 281 |
|
| 282 |
add_Single (item); |
| 283 |
} |
| 284 |
|
| 285 |
typedef struct { |
| 286 |
FT_UShort count; |
| 287 |
Single_Item *list; |
| 288 |
} Lookup_Item; |
| 289 |
|
| 290 |
typedef struct { |
| 291 |
FT_ULong ptr; |
| 292 |
Lookup_Item *entry; |
| 293 |
} Lookup_Record; |
| 294 |
|
| 295 |
Lookup_Record *Lookup_List; |
| 296 |
FT_UShort Lookup_Count; |
| 297 |
|
| 298 |
static inline void |
| 299 |
fetch_Lookup (int i) |
| 300 |
{ |
| 301 |
FT_ULong Lookup_ptr = Lookup_List[i].ptr; |
| 302 |
|
| 303 |
if (Lookup_ptr && !Lookup_List[i].entry) |
| 304 |
{ |
| 305 |
Lookup_Item *entry = Lookup_List[i].entry = mycalloc (1, sizeof(Lookup_Item)); |
| 306 |
Boolean is_ext = False; |
| 307 |
FT_UShort val; |
| 308 |
int j; |
| 309 |
|
| 310 |
#ifdef DEBUG |
| 311 |
warning("Lookup[%d] at 0x%04x.", i, Lookup_ptr); |
| 312 |
#endif |
| 313 |
GSUB_ptr = Lookup_ptr; |
| 314 |
|
| 315 |
/* |
| 316 |
* Read Lookup Item |
| 317 |
*/ |
| 318 |
need(6); |
| 319 |
val = get_UShort(); |
| 320 |
if (val == 7) |
| 321 |
is_ext = True; |
| 322 |
else if (val != 1) |
| 323 |
oops("Lookup[%d] at 0x%04x: bad Type=%d.", i, Lookup_ptr, val); |
| 324 |
get_UShort(); /* Ignore Lookup Flag */ |
| 325 |
entry->count = get_UShort(); |
| 326 |
entry->list = mycalloc (entry->count, sizeof(Single_Item)); |
| 327 |
|
| 328 |
need(2 * entry->count); |
| 329 |
for (j = 0; j < entry->count; j++) |
| 330 |
entry->list[j].ptr = Lookup_ptr + get_UShort(); |
| 331 |
|
| 332 |
if (is_ext) |
| 333 |
for (j = 0; j < entry->count; j++) |
| 334 |
{ |
| 335 |
GSUB_ptr = entry->list[j].ptr; |
| 336 |
need(8); |
| 337 |
val = get_UShort(); |
| 338 |
if (val != 1) |
| 339 |
oops("Lookup[%d] Extension[%d] at 0x%04x: bad Format=%d.", i, j, entry->list[j].ptr, val); |
| 340 |
val = get_UShort(); |
| 341 |
if (val != 1) |
| 342 |
oops("Lookup[%d] Extension[%d] at 0x%04x: bad Type=%d.", i, j, entry->list[j].ptr, val); |
| 343 |
entry->list[j].ptr += get_ULong(); |
| 344 |
} |
| 345 |
|
| 346 |
for (j = 0; j < entry->count; j++) |
| 347 |
fetch_Single (&entry->list[j]); |
| 348 |
} |
| 349 |
} |
| 350 |
|
| 351 |
typedef struct { |
| 352 |
FT_UShort count; |
| 353 |
FT_UShort *list; |
| 354 |
} Feature_Item; |
| 355 |
|
| 356 |
typedef struct { |
| 357 |
FT_Tag tag; |
| 358 |
FT_ULong ptr; |
| 359 |
Feature_Item *entry; |
| 360 |
} Feature_Record; |
| 361 |
|
| 362 |
Feature_Record *Feature_List; |
| 363 |
FT_UShort Feature_Count; |
| 364 |
|
| 365 |
/* |
| 366 |
* We check for the `vert' or `vrt2' feature in Chinese, Japanese, and Korean |
| 367 |
*/ |
| 368 |
#define FEATURE_vert FT_MAKE_TAG('v', 'e', 'r', 't') |
| 369 |
#define FEATURE_vrt2 FT_MAKE_TAG('v', 'r', 't', '2') |
| 370 |
|
| 371 |
#define print_Tag(t) t >> 24, (t >> 16) & 0xff, (t >> 8) & 0xff, t & 0xff |
| 372 |
|
| 373 |
static inline void |
| 374 |
fetch_Feature (int i) |
| 375 |
{ |
| 376 |
FT_ULong Feature_ptr = Feature_List[i].ptr; |
| 377 |
|
| 378 |
if (Feature_ptr && !Feature_List[i].entry) |
| 379 |
{ |
| 380 |
Feature_Item *entry = Feature_List[i].entry = mycalloc (1, sizeof(Feature_Item)); |
| 381 |
FT_UShort val; |
| 382 |
int j; |
| 383 |
|
| 384 |
#ifdef DEBUG |
| 385 |
warning("Feature[%d] '%c%c%c%c' at 0x%04x.", i, print_Tag(Feature_List[i].tag), Feature_ptr); |
| 386 |
#endif |
| 387 |
GSUB_ptr = Feature_ptr; |
| 388 |
|
| 389 |
/* |
| 390 |
* Read Feature Item |
| 391 |
*/ |
| 392 |
need(4); |
| 393 |
val = get_UShort(); |
| 394 |
if (val) |
| 395 |
oops("Feature[%d] at 0x%04x: bad FeaturParams 0x%04x.", Feature_ptr, i, val); |
| 396 |
entry->count = get_UShort(); |
| 397 |
entry->list = mycalloc (entry->count, sizeof(FT_UShort)); |
| 398 |
|
| 399 |
need(2*entry->count); |
| 400 |
for (j = 0; j < entry->count; j++) { |
| 401 |
entry->list[j] = get_UShort(); |
| 402 |
if (entry->list[j] >= Lookup_Count) |
| 403 |
oops("Feature[%d] at 0x%04x: bad Lookup Index[%d]=%d.", i, Feature_ptr, j, entry->list[j]); |
| 404 |
} |
| 405 |
|
| 406 |
for (j = 0; j < entry->count; j++) |
| 407 |
fetch_Lookup (entry->list[j]); |
| 408 |
} |
| 409 |
} |
| 410 |
|
| 411 |
#define SCRIPT_kana FT_MAKE_TAG('k', 'a', 'n', 'a') |
| 412 |
#define SCRIPT_hani FT_MAKE_TAG('h', 'a', 'n', 'i') |
| 413 |
#define SCRIPT_hang FT_MAKE_TAG('h', 'a', 'n', 'g') |
| 414 |
|
| 415 |
#define LANGUAGE_JAN FT_MAKE_TAG('J', 'A', 'N', ' ') |
| 416 |
#define LANGUAGE_CHN FT_MAKE_TAG('C', 'H', 'N', ' ') |
| 417 |
#define LANGUAGE_KOR FT_MAKE_TAG('K', 'O', 'R', ' ') |
| 418 |
|
| 419 |
struct _Script_Lang |
| 420 |
{ |
| 421 |
FT_Tag script; |
| 422 |
FT_Tag language; |
| 423 |
}; |
| 424 |
|
| 425 |
static const struct _Script_Lang Script_Lang[] = { |
| 426 |
{ SCRIPT_kana, LANGUAGE_JAN }, |
| 427 |
{ SCRIPT_hani, LANGUAGE_CHN }, |
| 428 |
{ SCRIPT_hang, LANGUAGE_KOR } |
| 429 |
}; |
| 430 |
#define num_Script_Lang (sizeof(Script_Lang) / sizeof(Script_Lang[0])) |
| 431 |
|
| 432 |
typedef struct { |
| 433 |
FT_UShort req; |
| 434 |
FT_UShort count; |
| 435 |
FT_UShort *list; |
| 436 |
} LangSys_Item; |
| 437 |
|
| 438 |
typedef struct { |
| 439 |
FT_Tag tag; |
| 440 |
FT_ULong ptr; |
| 441 |
LangSys_Item *entry; |
| 442 |
} LangSys_Record; |
| 443 |
|
| 444 |
typedef struct { |
| 445 |
FT_ULong ptr; |
| 446 |
LangSys_Item *entry; |
| 447 |
FT_UShort count; |
| 448 |
LangSys_Record *list; |
| 449 |
} Script_Item; |
| 450 |
|
| 451 |
typedef struct { |
| 452 |
FT_Tag tag, ltag; |
| 453 |
FT_ULong ptr; |
| 454 |
Script_Item *entry; |
| 455 |
} Script_Record; |
| 456 |
|
| 457 |
Script_Record *Script_List; |
| 458 |
FT_UShort Script_Count; |
| 459 |
|
| 460 |
static inline void |
| 461 |
fetch_LangSys (FT_ULong ptr, LangSys_Item **entry) |
| 462 |
{ |
| 463 |
LangSys_Item * item; |
| 464 |
FT_UShort val; |
| 465 |
int i; |
| 466 |
|
| 467 |
*entry = item = mycalloc (1, sizeof(LangSys_Item)); |
| 468 |
GSUB_ptr = ptr; |
| 469 |
|
| 470 |
/* |
| 471 |
* Read LangSys Item |
| 472 |
*/ |
| 473 |
need(6); |
| 474 |
val = get_UShort(); |
| 475 |
if (val) |
| 476 |
oops("LangSys at 0x%04x: bad LookupOrder 0x%04x.", ptr, val); |
| 477 |
item->req = get_UShort(); |
| 478 |
if (item->req != 0xffff && item->req >= Feature_Count) |
| 479 |
oops("LangSys at 0x%04x: bad required Feature Index=%d.", ptr, item->req); |
| 480 |
item->count = get_UShort(); |
| 481 |
item->list = mycalloc (item->count, sizeof(FT_UShort)); |
| 482 |
|
| 483 |
need(2 * item->count); |
| 484 |
for (i = 0; i < item->count; i++) { |
| 485 |
item->list[i] = get_UShort(); |
| 486 |
if (item->list[i] >= Feature_Count) |
| 487 |
oops("LangSys at 0x%04x: bad Feature Index[%d]=%d.", ptr, i, item->list[i]); |
| 488 |
} |
| 489 |
|
| 490 |
if (item->req != 0xffff) |
| 491 |
fetch_Feature (item->req); |
| 492 |
for (i = 0; i < item->count; i++) |
| 493 |
fetch_Feature (item->list[i]); |
| 494 |
} |
| 495 |
|
| 496 |
static inline void |
| 497 |
fetch_Script (int i) |
| 498 |
{ |
| 499 |
FT_ULong Script_ptr = Script_List[i].ptr; |
| 500 |
|
| 501 |
if (Script_ptr) |
| 502 |
{ |
| 503 |
Script_Item *entry = Script_List[i].entry = mycalloc (1, sizeof(Script_Item)); |
| 504 |
int j; |
| 505 |
|
| 506 |
#ifdef DEBUG |
| 507 |
warning("Script[%d] '%c%c%c%c' at 0x%04x.", i, print_Tag(Script_List[i].tag), Script_ptr); |
| 508 |
#endif |
| 509 |
GSUB_ptr = Script_ptr; |
| 510 |
|
| 511 |
/* |
| 512 |
* Read Script Item |
| 513 |
*/ |
| 514 |
need(4); |
| 515 |
if ((entry->ptr = get_UShort())) |
| 516 |
entry->ptr += Script_ptr; |
| 517 |
entry->count = get_UShort(); |
| 518 |
entry->list = mycalloc (entry->count, sizeof(LangSys_Record)); |
| 519 |
|
| 520 |
need(6 * entry->count); |
| 521 |
for (j = 0; j < entry->count; j++) { |
| 522 |
FT_Tag tag = Script_List[i].tag; |
| 523 |
FT_ULong ptr = Script_ptr + get_UShort(); |
| 524 |
|
| 525 |
entry->list[j].tag = tag; |
| 526 |
if (tag == Script_List[i].ltag) |
| 527 |
entry->list[j].ptr = ptr; |
| 528 |
} |
| 529 |
|
| 530 |
/* |
| 531 |
* Read Default LangSys entriy |
| 532 |
*/ |
| 533 |
if (entry->ptr) { |
| 534 |
#ifdef DEBUG |
| 535 |
warning("Script[%d] Default LangSys at 0x%04x.", i, entry->ptr); |
| 536 |
#endif |
| 537 |
fetch_LangSys (entry->ptr, &entry->entry); |
| 538 |
} |
| 539 |
|
| 540 |
/* |
| 541 |
* Read language LangSys entries |
| 542 |
*/ |
| 543 |
for (j = 0; j < entry->count; j++) |
| 544 |
if (entry->list[j].ptr) { |
| 545 |
#ifdef DEBUG |
| 546 |
warning("Script[%d] LangSys[%d] '%c%c%c%c' at 0x%04x.", i, j, |
| 547 |
print_Tag(entry->list[j].tag), entry->list[j].ptr); |
| 548 |
#endif |
| 549 |
fetch_LangSys (entry->list[j].ptr, &entry->list[j].entry); |
| 550 |
} |
| 551 |
} |
| 552 |
} |
| 553 |
|
| 554 |
#define TTAG_GSUB FT_MAKE_TAG('G', 'S', 'U', 'B') |
| 555 |
|
| 556 |
static inline void |
| 557 |
fetch_GSUB (void) |
| 558 |
{ |
| 559 |
FT_Error error; |
| 560 |
int i, j; |
| 561 |
|
| 562 |
FT_ULong Script_List_ptr, Feature_List_ptr, Lookup_List_ptr; |
| 563 |
|
| 564 |
/* |
| 565 |
* Fetch GSUB table |
| 566 |
*/ |
| 567 |
if ((error = FT_Load_Sfnt_Table(face, TTAG_GSUB, 0, NULL, &GSUB_length))) |
| 568 |
{ |
| 569 |
#ifdef DEBUG |
| 570 |
warning("No GSUB data available for vertical glyph presentation forms."); |
| 571 |
#endif |
| 572 |
return; |
| 573 |
} |
| 574 |
GSUB_table = mymalloc(GSUB_length); |
| 575 |
if ((error = FT_Load_Sfnt_Table(face, TTAG_GSUB, 0, GSUB_table, &GSUB_length))) |
| 576 |
{ |
| 577 |
warning("Cannot load GSUB table (error code = 0x%x).", error); |
| 578 |
return; |
| 579 |
} |
| 580 |
|
| 581 |
/* |
| 582 |
* Read GSUB header |
| 583 |
*/ |
| 584 |
need(10); |
| 585 |
if (get_ULong() != 0x00010000) |
| 586 |
oops("GSUB: Bad version."); |
| 587 |
Script_List_ptr = get_UShort(); |
| 588 |
Feature_List_ptr = get_UShort(); |
| 589 |
Lookup_List_ptr = get_UShort(); |
| 590 |
|
| 591 |
/* |
| 592 |
* Read Script_List |
| 593 |
*/ |
| 594 |
GSUB_ptr = Script_List_ptr; |
| 595 |
need(2); |
| 596 |
Script_Count = get_UShort(); |
| 597 |
#ifdef DEBUG |
| 598 |
warning("GSUB: Script List with %d entries at 0x%04x.", Script_Count, Script_List_ptr); |
| 599 |
#endif |
| 600 |
Script_List = mycalloc (Script_Count, sizeof(Script_Record)); |
| 601 |
|
| 602 |
need (6 * Script_Count); |
| 603 |
for (i = 0; i < Script_Count; i++) |
| 604 |
{ |
| 605 |
FT_Tag tag = get_ULong(); |
| 606 |
FT_ULong ptr = Script_List_ptr + get_UShort(); |
| 607 |
for (j = 0; j < num_Script_Lang; j++) |
| 608 |
if (tag == Script_Lang[j].script) |
| 609 |
{ |
| 610 |
Script_List[i].tag = tag; |
| 611 |
Script_List[i].ltag = Script_Lang[j].language; |
| 612 |
Script_List[i].ptr = ptr; |
| 613 |
} |
| 614 |
} |
| 615 |
|
| 616 |
/* |
| 617 |
* Read Feature_List |
| 618 |
*/ |
| 619 |
GSUB_ptr = Feature_List_ptr; |
| 620 |
need(2); |
| 621 |
Feature_Count = get_UShort(); |
| 622 |
#ifdef DEBUG |
| 623 |
warning("GSUB: Feature List with %d entries at 0x%04x.", Feature_Count, Feature_List_ptr); |
| 624 |
#endif |
| 625 |
Feature_List = mycalloc (Feature_Count, sizeof(Feature_Record)); |
| 626 |
|
| 627 |
need (6 * Feature_Count); |
| 628 |
for (i = 0; i < Feature_Count; i++) |
| 629 |
{ |
| 630 |
FT_Tag tag = get_ULong(); |
| 631 |
FT_ULong ptr = Feature_List_ptr + get_UShort(); |
| 632 |
if (tag == FEATURE_vert || tag == FEATURE_vrt2) |
| 633 |
{ |
| 634 |
Feature_List[i].tag = tag; |
| 635 |
Feature_List[i].ptr = ptr; |
| 636 |
} |
| 637 |
} |
| 638 |
|
| 639 |
/* |
| 640 |
* Read Lookup_List |
| 641 |
*/ |
| 642 |
GSUB_ptr = Lookup_List_ptr; |
| 643 |
need(2); |
| 644 |
Lookup_Count = get_UShort(); |
| 645 |
#ifdef DEBUG |
| 646 |
warning("GSUB: Lookup List with %d entries at 0x%04x.", Lookup_Count, Lookup_List_ptr); |
| 647 |
#endif |
| 648 |
Lookup_List = mycalloc (Lookup_Count, sizeof(Lookup_Record)); |
| 649 |
|
| 650 |
need (2 * Lookup_Count); |
| 651 |
for (i = 0; i < Lookup_Count; i++) |
| 652 |
Lookup_List[i].ptr = Lookup_List_ptr + get_UShort(); |
| 653 |
|
| 654 |
/* |
| 655 |
* Read Script_List entries |
| 656 |
*/ |
| 657 |
for (i = 0; i < Script_Count && !has_gsub; i++) |
| 658 |
fetch_Script (i); |
| 659 |
} |
| 660 |
|
| 661 |
void |
| 662 |
FTopen (char *filename, Font *fnt, Boolean do_tfm, Boolean quiet) |
| 663 |
{ |
| 664 |
FT_Error error; |
| 665 |
int i; |
| 666 |
unsigned short num_cmap, cmap_plat=0, cmap_enc=0; |
| 667 |
|
| 668 |
if ((error = FT_Init_FreeType(&engine))) |
| 669 |
oops("Cannot initialize FreeType engine (error code = 0x%x).", error); |
| 670 |
|
| 671 |
/* |
| 672 |
* Load face. |
| 673 |
*/ |
| 674 |
if ((error = FT_New_Face(engine, filename, 0, &face))) |
| 675 |
oops("Cannot open `%s'.", filename); |
| 676 |
|
| 677 |
if (face->num_faces == 1) |
| 678 |
{ |
| 679 |
if (fnt->fontindex != 0) |
| 680 |
{ |
| 681 |
warning("This isn't a TrueType collection.\n" |
| 682 |
"Parameter `%s' is ignored.", do_tfm ? "-f" : "Fontindex"); |
| 683 |
fnt->fontindex = 0; |
| 684 |
} |
| 685 |
fnt->fontindexparam = NULL; |
| 686 |
} |
| 687 |
else |
| 688 |
{ |
| 689 |
if (fnt->fontindex != 0) |
| 690 |
{ |
| 691 |
/* |
| 692 |
* Now we try to open the proper font in a collection. |
| 693 |
*/ |
| 694 |
FT_Done_Face(face); |
| 695 |
if ((error = FT_New_Face(engine, filename, (FT_Long)fnt->fontindex, &face))) |
| 696 |
oops("Cannot open font %lu in TrueType Collection `%s'.", |
| 697 |
fnt->fontindex, filename); |
| 698 |
} |
| 699 |
} |
| 700 |
|
| 701 |
if (do_tfm) |
| 702 |
{ |
| 703 |
/* |
| 704 |
* Get the OS/2 table. |
| 705 |
*/ |
| 706 |
if ((os2 = FT_Get_Sfnt_Table(face, ft_sfnt_os2)) == NULL) |
| 707 |
oops("Cannot find OS/2 table for `%s'.", filename); |
| 708 |
|
| 709 |
/* |
| 710 |
* Get the Postscript table. |
| 711 |
*/ |
| 712 |
if ((postscript = FT_Get_Sfnt_Table(face, ft_sfnt_post)) == NULL) |
| 713 |
oops("Cannot find Postscript table for `%s'.", filename); |
| 714 |
} |
| 715 |
|
| 716 |
if ((error = FT_Set_Char_Size(face, ptsize * 64, ptsize * 64, dpi, dpi))) |
| 717 |
oops("Cannot set character size (error code = 0x%x).", error); |
| 718 |
|
| 719 |
matrix1.xx = (FT_Fixed)(floor(fnt->efactor * 1024) * (1<<16)/1024); |
| 720 |
matrix1.xy = (FT_Fixed)(floor(fnt->slant * 1024) * (1<<16)/1024); |
| 721 |
matrix1.yx = (FT_Fixed)0; |
| 722 |
matrix1.yy = (FT_Fixed)(1<<16); |
| 723 |
|
| 724 |
if (fnt->rotate) |
| 725 |
{ |
| 726 |
matrix2.xx = 0; |
| 727 |
matrix2.yx = 1L << 16; |
| 728 |
matrix2.xy = -matrix2.yx; |
| 729 |
matrix2.yy = matrix2.xx; |
| 730 |
} |
| 731 |
|
| 732 |
if (do_tfm) |
| 733 |
{ |
| 734 |
fnt->units_per_em = face->units_per_EM; |
| 735 |
fnt->fixedpitch = postscript->isFixedPitch; |
| 736 |
fnt->italicangle = postscript->italicAngle / 65536.0; |
| 737 |
fnt->xheight = os2->sxHeight * 1000 / fnt->units_per_em; |
| 738 |
} |
| 739 |
|
| 740 |
if (fnt->PSnames != Only) |
| 741 |
{ |
| 742 |
num_cmap = face->num_charmaps; |
| 743 |
for (i = 0; i < num_cmap; i++) |
| 744 |
{ |
| 745 |
cmap_plat=face->charmaps[i]->platform_id; |
| 746 |
cmap_enc=face->charmaps[i]->encoding_id; |
| 747 |
if (cmap_plat == fnt->pid && cmap_enc == fnt->eid) |
| 748 |
break; |
| 749 |
} |
| 750 |
if (i == num_cmap) |
| 751 |
{ |
| 752 |
fprintf(stderr, "%s: ERROR: Invalid platform and/or encoding ID.\n", |
| 753 |
progname); |
| 754 |
if (num_cmap == 1) |
| 755 |
fprintf(stderr, " The only valid PID/EID pair is"); |
| 756 |
else |
| 757 |
fprintf(stderr, " Valid PID/EID pairs are:\n"); |
| 758 |
for (i = 0; i < num_cmap; i++) |
| 759 |
{ |
| 760 |
cmap_plat=face->charmaps[i]->platform_id; |
| 761 |
cmap_enc=face->charmaps[i]->encoding_id; |
| 762 |
fprintf(stderr, " (%i,%i)\n", cmap_plat, cmap_enc); |
| 763 |
} |
| 764 |
fprintf(stderr, "\n"); |
| 765 |
exit(1); |
| 766 |
} |
| 767 |
|
| 768 |
if ((error = FT_Set_Charmap(face, face->charmaps[i]))) |
| 769 |
oops("Cannot load cmap (error code = 0x%x).", error); |
| 770 |
} |
| 771 |
|
| 772 |
if (fnt->PSnames) |
| 773 |
{ |
| 774 |
if (!FT_HAS_GLYPH_NAMES(face)) |
| 775 |
oops("This font does not support PS names."); |
| 776 |
} |
| 777 |
else if (cmap_plat == Microsoft_platform && |
| 778 |
cmap_enc == Microsoft_Unicode_encoding) |
| 779 |
set_encoding_scheme(encUnicode, fnt); |
| 780 |
else if (cmap_plat == Macintosh_platform && |
| 781 |
cmap_enc == Macintosh_encoding) |
| 782 |
set_encoding_scheme(encMac, fnt); |
| 783 |
else |
| 784 |
set_encoding_scheme(encFontSpecific, fnt); |
| 785 |
|
| 786 |
if (fnt->rotate) |
| 787 |
fetch_GSUB(); |
| 788 |
} |
| 789 |
|
| 790 |
static inline int |
| 791 |
find_ndx (int Num, Coverage_Item *coverage) |
| 792 |
{ |
| 793 |
int i; |
| 794 |
|
| 795 |
switch (coverage->format) |
| 796 |
{ |
| 797 |
case 1: |
| 798 |
for (i = 0; i < coverage->count; i++) |
| 799 |
{ |
| 800 |
FT_UShort glyph = coverage->data.glyphs[i]; |
| 801 |
if (Num < glyph) |
| 802 |
break; |
| 803 |
else if (Num == glyph) |
| 804 |
return i; |
| 805 |
} |
| 806 |
break; |
| 807 |
case 2: |
| 808 |
for (i = 0; i < coverage->count; i++) |
| 809 |
{ |
| 810 |
Range_Record *range = &coverage->data.ranges[i]; |
| 811 |
|
| 812 |
if (Num < range->start) |
| 813 |
break; |
| 814 |
else if (Num <= range->end) |
| 815 |
return (Num - range->start) + range->start_Index; |
| 816 |
} |
| 817 |
break; |
| 818 |
default: |
| 819 |
oops("Internal error: Invalid Coverage Format=%d.", coverage->format); |
| 820 |
} |
| 821 |
return -1; |
| 822 |
} |
| 823 |
|
| 824 |
int |
| 825 |
Get_Vert (int Num) |
| 826 |
{ |
| 827 |
Subst_Item *entry; |
| 828 |
|
| 829 |
#ifdef DEBUG |
| 830 |
warning("GSUB: Looking for vertical form of glyph %d.", Num); |
| 831 |
#endif |
| 832 |
for (entry = Subst_list; entry; entry = entry->next) |
| 833 |
{ |
| 834 |
Single_Item *item = entry->item; |
| 835 |
int ndx = find_ndx (Num, item->coverage); |
| 836 |
|
| 837 |
if (ndx >= 0) |
| 838 |
{ |
| 839 |
#ifdef DEBUG |
| 840 |
warning("Covered at position %d.", ndx); |
| 841 |
#endif |
| 842 |
switch (item->format) |
| 843 |
{ |
| 844 |
case 1: |
| 845 |
Num += *item->data.delta; |
| 846 |
break; |
| 847 |
case 2: |
| 848 |
Num = item->data.array->glyphs[ndx]; |
| 849 |
break; |
| 850 |
default: |
| 851 |
oops("Internal error: Invalid Single Format=%d.", item->format); |
| 852 |
} |
| 853 |
#ifdef DEBUG |
| 854 |
warning("Substituted by glyph %d.", Num); |
| 855 |
#endif |
| 856 |
} |
| 857 |
} |
| 858 |
return Num; |
| 859 |
} |