| 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 |
* TrueType GSUB support: (incomplete) |
| 23 |
*/ |
| 24 |
|
| 25 |
#ifdef HAVE_CONFIG_H |
| 26 |
#include <config.h> |
| 27 |
#endif |
| 28 |
|
| 29 |
#include "system.h" |
| 30 |
|
| 31 |
#include "error.h" |
| 32 |
#include "mem.h" |
| 33 |
#include "mfileio.h" |
| 34 |
#include "dpxconf.h" |
| 35 |
|
| 36 |
#include "sfnt.h" |
| 37 |
|
| 38 |
#include "otl_opt.h" |
| 39 |
#include "tt_gsub.h" |
| 40 |
|
| 41 |
#define VERBOSE_LEVEL_MIN 2 |
| 42 |
|
| 43 |
typedef USHORT Offset; |
| 44 |
typedef USHORT GlyphID; |
| 45 |
|
| 46 |
/* OpenType Common Layout Table */ |
| 47 |
/* Records */ |
| 48 |
struct clt_record |
| 49 |
{ |
| 50 |
char tag[5]; /* 4-byte identifier */ |
| 51 |
Offset offset; |
| 52 |
}; |
| 53 |
|
| 54 |
/* Ranges */ |
| 55 |
/* RangeRecord */ |
| 56 |
struct clt_range |
| 57 |
{ |
| 58 |
GlyphID Start; /* First GlyphID in the range */ |
| 59 |
GlyphID End; /* Last GlyphID in the range */ |
| 60 |
USHORT StartCoverageIndex; /* Converage Index of first GID */ |
| 61 |
}; |
| 62 |
|
| 63 |
static int |
| 64 |
clt_read_record (struct clt_record *rec, sfnt *sfont) |
| 65 |
{ |
| 66 |
int i; |
| 67 |
|
| 68 |
ASSERT(rec && sfont); |
| 69 |
|
| 70 |
for (i = 0; i < 4; i++) { |
| 71 |
rec->tag[i] = sfnt_get_char(sfont); |
| 72 |
} |
| 73 |
rec->tag[4] = '\0'; |
| 74 |
rec->offset = sfnt_get_ushort(sfont); |
| 75 |
|
| 76 |
return 6; |
| 77 |
} |
| 78 |
|
| 79 |
static int |
| 80 |
clt_read_range (struct clt_range *rec, sfnt *sfont) |
| 81 |
{ |
| 82 |
ASSERT(rec && sfont); |
| 83 |
|
| 84 |
rec->Start = sfnt_get_ushort(sfont); |
| 85 |
rec->End = sfnt_get_ushort(sfont); |
| 86 |
rec->StartCoverageIndex = sfnt_get_ushort(sfont); |
| 87 |
|
| 88 |
return 6; |
| 89 |
} |
| 90 |
|
| 91 |
/* |
| 92 |
List structure: |
| 93 |
ScriptRecord (records), FeatureRecord (records), Lookup (offsets) |
| 94 |
*/ |
| 95 |
|
| 96 |
struct clt_record_list |
| 97 |
{ |
| 98 |
USHORT count; |
| 99 |
struct clt_record *record; |
| 100 |
}; |
| 101 |
|
| 102 |
/* offset and index list, Offset is USHORT */ |
| 103 |
struct clt_number_list |
| 104 |
{ |
| 105 |
USHORT count; |
| 106 |
USHORT *value; |
| 107 |
}; |
| 108 |
|
| 109 |
static int |
| 110 |
clt_read_record_list (struct clt_record_list *list, sfnt *sfont) |
| 111 |
{ |
| 112 |
int len, i; |
| 113 |
|
| 114 |
ASSERT(list && sfont); |
| 115 |
|
| 116 |
list->count = sfnt_get_ushort(sfont); |
| 117 |
len = 2; |
| 118 |
|
| 119 |
if (list->count == 0) |
| 120 |
list->record = NULL; |
| 121 |
else { |
| 122 |
list->record = NEW(list->count, struct clt_record); |
| 123 |
for (i = 0; i < list->count; i++) { |
| 124 |
len += clt_read_record(&(list->record[i]), sfont); |
| 125 |
} |
| 126 |
} |
| 127 |
|
| 128 |
return len; |
| 129 |
} |
| 130 |
|
| 131 |
static void |
| 132 |
clt_release_record_list (struct clt_record_list *list) |
| 133 |
{ |
| 134 |
if (list) { |
| 135 |
if (list->record) |
| 136 |
RELEASE(list->record); |
| 137 |
list->record = NULL; |
| 138 |
list->count = 0; |
| 139 |
} |
| 140 |
} |
| 141 |
|
| 142 |
static int |
| 143 |
clt_read_number_list (struct clt_number_list *list, sfnt *sfont) |
| 144 |
{ |
| 145 |
int i; |
| 146 |
|
| 147 |
ASSERT(list && sfont); |
| 148 |
|
| 149 |
list->count = sfnt_get_ushort(sfont); |
| 150 |
|
| 151 |
if (list->count == 0) |
| 152 |
list->value = NULL; |
| 153 |
else { |
| 154 |
list->value = NEW(list->count, USHORT); |
| 155 |
for (i = 0; i < list->count; i++) { |
| 156 |
list->value[i] = sfnt_get_ushort(sfont); |
| 157 |
} |
| 158 |
} |
| 159 |
|
| 160 |
return (2 + 2 * list->count); |
| 161 |
} |
| 162 |
|
| 163 |
static void |
| 164 |
clt_release_number_list (struct clt_number_list *list) |
| 165 |
{ |
| 166 |
if (list) { |
| 167 |
if (list->value) |
| 168 |
RELEASE(list->value); |
| 169 |
list->value = NULL; |
| 170 |
list->count = 0; |
| 171 |
} |
| 172 |
} |
| 173 |
|
| 174 |
/* |
| 175 |
* Tables |
| 176 |
*/ |
| 177 |
|
| 178 |
|
| 179 |
/* Coverage Table: format 1 and format 2 */ |
| 180 |
struct clt_coverage |
| 181 |
{ |
| 182 |
USHORT format; /* Format identifier: 1 (list), 2 (range) */ |
| 183 |
USHORT count; /* Glyphs/Range Count */ |
| 184 |
GlyphID *list; /* Array of GlyphIDs - in numerical order */ |
| 185 |
struct clt_range *range; /* Array of glyph ranges |
| 186 |
* - ordered by Start GlyphID |
| 187 |
*/ |
| 188 |
}; |
| 189 |
|
| 190 |
/* GSUB - The Glyph Substitution Table */ |
| 191 |
struct otl_gsub_header |
| 192 |
{ |
| 193 |
Fixed version; /* 0x00010000 */ |
| 194 |
Offset ScriptList; /* offset */ |
| 195 |
Offset FeatureList; /* offset */ |
| 196 |
Offset LookupList; /* offset */ |
| 197 |
}; |
| 198 |
|
| 199 |
/* Single Substitution Format 1 */ |
| 200 |
struct otl_gsub_single1 |
| 201 |
{ |
| 202 |
SHORT DeltaGlyphID; /* Add to original GlyphID to get |
| 203 |
* substitute GlyphID */ |
| 204 |
struct clt_coverage coverage; /* Coverage table */ |
| 205 |
}; |
| 206 |
|
| 207 |
/* Single Substitution Format 2 */ |
| 208 |
struct otl_gsub_single2 |
| 209 |
{ |
| 210 |
USHORT GlyphCount; /* Number of GlyphIDs in the Substitute array */ |
| 211 |
GlyphID *Substitute; /* Array of substitute GlyphIDs |
| 212 |
* - ordered by Coverage Index */ |
| 213 |
struct clt_coverage coverage; /* Coverage table */ |
| 214 |
}; |
| 215 |
|
| 216 |
/* Alternate Subsutitution Format 1 */ |
| 217 |
struct otl_gsub_altset |
| 218 |
{ |
| 219 |
USHORT GlyphCount; /* Number of GlyphIDs in the Alternate array */ |
| 220 |
GlyphID *Alternate; /* Array of alternate GlyphIDs |
| 221 |
* - in arbitrary order |
| 222 |
*/ |
| 223 |
}; |
| 224 |
|
| 225 |
struct otl_gsub_alternate1 |
| 226 |
{ |
| 227 |
USHORT AlternateSetCount; |
| 228 |
struct otl_gsub_altset *AlternateSet; |
| 229 |
|
| 230 |
struct clt_coverage coverage; |
| 231 |
}; |
| 232 |
|
| 233 |
/* Faithfull */ |
| 234 |
struct otl_gsub_ligtab |
| 235 |
{ |
| 236 |
GlyphID LigGlyph; /* GlyphID of ligature glyph */ |
| 237 |
USHORT CompCount; |
| 238 |
GlyphID *Component; /* CompCount - 1 elements |
| 239 |
* First component excluded. |
| 240 |
* Ordered in writing direction... |
| 241 |
*/ |
| 242 |
}; |
| 243 |
|
| 244 |
struct otl_gsub_ligset |
| 245 |
{ |
| 246 |
USHORT LigatureCount; |
| 247 |
struct otl_gsub_ligtab *Ligature; |
| 248 |
}; |
| 249 |
|
| 250 |
struct otl_gsub_ligature1 |
| 251 |
{ |
| 252 |
USHORT LigSetCount; |
| 253 |
struct otl_gsub_ligset *LigatureSet; |
| 254 |
|
| 255 |
struct clt_coverage coverage; |
| 256 |
}; |
| 257 |
|
| 258 |
/* GSUB subtable (single) */ |
| 259 |
struct otl_gsub_subtab |
| 260 |
{ |
| 261 |
USHORT LookupType; /* FIXME */ |
| 262 |
|
| 263 |
USHORT SubstFormat; |
| 264 |
union { |
| 265 |
struct otl_gsub_single1 *single1; |
| 266 |
struct otl_gsub_single2 *single2; |
| 267 |
struct otl_gsub_alternate1 *alternate1; |
| 268 |
struct otl_gsub_ligature1 *ligature1; |
| 269 |
} table; |
| 270 |
}; |
| 271 |
|
| 272 |
/* Script Table */ |
| 273 |
struct clt_script_table |
| 274 |
{ |
| 275 |
Offset DefaultLangSys; |
| 276 |
struct clt_record_list LangSysRecord; |
| 277 |
}; |
| 278 |
|
| 279 |
static int |
| 280 |
clt_read_script_table (struct clt_script_table *tab, sfnt *sfont) |
| 281 |
{ |
| 282 |
int len; |
| 283 |
|
| 284 |
ASSERT(tab && sfont); |
| 285 |
|
| 286 |
tab->DefaultLangSys = sfnt_get_ushort(sfont); |
| 287 |
len = 2; |
| 288 |
len += clt_read_record_list(&tab->LangSysRecord, sfont); |
| 289 |
|
| 290 |
return len; |
| 291 |
} |
| 292 |
|
| 293 |
static void |
| 294 |
clt_release_script_table (struct clt_script_table *tab) |
| 295 |
{ |
| 296 |
if (tab) |
| 297 |
clt_release_record_list(&tab->LangSysRecord); |
| 298 |
} |
| 299 |
|
| 300 |
/* LangSys Table */ |
| 301 |
struct clt_langsys_table |
| 302 |
{ |
| 303 |
Offset LookupOrder; /* reserved */ |
| 304 |
USHORT ReqFeatureIndex; |
| 305 |
struct clt_number_list FeatureIndex; /* Array of indices into the |
| 306 |
* FeatureList in arbitary order. |
| 307 |
*/ |
| 308 |
}; |
| 309 |
|
| 310 |
static int |
| 311 |
clt_read_langsys_table (struct clt_langsys_table *tab, sfnt *sfont) |
| 312 |
{ |
| 313 |
int len; |
| 314 |
|
| 315 |
ASSERT(tab && sfont); |
| 316 |
|
| 317 |
tab->LookupOrder = sfnt_get_ushort(sfont); |
| 318 |
tab->ReqFeatureIndex = sfnt_get_ushort(sfont); |
| 319 |
len = 4; |
| 320 |
len += clt_read_number_list(&tab->FeatureIndex, sfont); |
| 321 |
|
| 322 |
return len; |
| 323 |
} |
| 324 |
|
| 325 |
static void |
| 326 |
clt_release_langsys_table (struct clt_langsys_table *tab) |
| 327 |
{ |
| 328 |
if (tab) |
| 329 |
clt_release_number_list(&tab->FeatureIndex); |
| 330 |
} |
| 331 |
|
| 332 |
|
| 333 |
/* Feature Table */ |
| 334 |
struct clt_feature_table |
| 335 |
{ |
| 336 |
Offset FeatureParams; |
| 337 |
struct clt_number_list LookupListIndex; /* LookupListIndex List */ |
| 338 |
}; |
| 339 |
|
| 340 |
static int |
| 341 |
clt_read_feature_table (struct clt_feature_table *tab, sfnt *sfont) |
| 342 |
{ |
| 343 |
int len; |
| 344 |
|
| 345 |
ASSERT(tab && sfont); |
| 346 |
|
| 347 |
tab->FeatureParams = sfnt_get_ushort(sfont); |
| 348 |
len = 2; |
| 349 |
len += clt_read_number_list(&tab->LookupListIndex, sfont); |
| 350 |
|
| 351 |
return len; |
| 352 |
} |
| 353 |
|
| 354 |
static void |
| 355 |
clt_release_feature_table (struct clt_feature_table *tab) |
| 356 |
{ |
| 357 |
if (tab) |
| 358 |
clt_release_number_list(&tab->LookupListIndex); |
| 359 |
} |
| 360 |
|
| 361 |
/* Lookup Table: |
| 362 |
* Currently, only single substitution is supported. |
| 363 |
* LookupFlag is ignored. |
| 364 |
*/ |
| 365 |
struct clt_lookup_table |
| 366 |
{ |
| 367 |
USHORT LookupType; /* Different enumerations for GSUB and GPOS */ |
| 368 |
USHORT LookupFlag; /* Lookup qualifiers */ |
| 369 |
struct clt_number_list SubTableList; /* offset */ |
| 370 |
/* offset is from beginning of Lookup table */ |
| 371 |
}; |
| 372 |
|
| 373 |
static int |
| 374 |
clt_read_lookup_table (struct clt_lookup_table *tab, sfnt *sfont) |
| 375 |
{ |
| 376 |
int len; |
| 377 |
|
| 378 |
ASSERT(tab && sfont); |
| 379 |
|
| 380 |
tab->LookupType = sfnt_get_ushort(sfont); |
| 381 |
tab->LookupFlag = sfnt_get_ushort(sfont); |
| 382 |
len = 4; |
| 383 |
len += clt_read_number_list(&tab->SubTableList, sfont); |
| 384 |
|
| 385 |
return len; |
| 386 |
} |
| 387 |
|
| 388 |
static void |
| 389 |
clt_release_lookup_table (struct clt_lookup_table *tab) |
| 390 |
{ |
| 391 |
if (tab) |
| 392 |
clt_release_number_list(&tab->SubTableList); |
| 393 |
} |
| 394 |
|
| 395 |
static int |
| 396 |
clt_read_coverage (struct clt_coverage *cov, sfnt *sfont) |
| 397 |
{ |
| 398 |
int len, i; |
| 399 |
|
| 400 |
ASSERT(cov && sfont); |
| 401 |
|
| 402 |
cov->format = sfnt_get_ushort(sfont); |
| 403 |
cov->count = sfnt_get_ushort(sfont); |
| 404 |
len = 4; |
| 405 |
|
| 406 |
switch (cov->format) { |
| 407 |
case 1: /* list */ |
| 408 |
if (cov->count == 0) |
| 409 |
cov->list = NULL; |
| 410 |
else { |
| 411 |
cov->list = NEW(cov->count, USHORT); |
| 412 |
for (i = 0; i < cov->count; i++) { |
| 413 |
cov->list[i] = sfnt_get_ushort(sfont); |
| 414 |
} |
| 415 |
} |
| 416 |
cov->range = NULL; |
| 417 |
len += 2 * cov->count; |
| 418 |
break; |
| 419 |
case 2: /* range */ |
| 420 |
if (cov->count == 0) |
| 421 |
cov->range = NULL; |
| 422 |
else { |
| 423 |
cov->range = NEW(cov->count, struct clt_range); |
| 424 |
for (i = 0; i < cov->count; i++) { |
| 425 |
len += clt_read_range(&(cov->range[i]), sfont); |
| 426 |
} |
| 427 |
} |
| 428 |
cov->list = NULL; |
| 429 |
break; |
| 430 |
default: |
| 431 |
ERROR("Unknown coverage format"); |
| 432 |
} |
| 433 |
|
| 434 |
return len; |
| 435 |
} |
| 436 |
|
| 437 |
static void |
| 438 |
clt_release_coverage (struct clt_coverage *cov) |
| 439 |
{ |
| 440 |
if (cov) { |
| 441 |
switch (cov->format) { |
| 442 |
case 1: /* list */ |
| 443 |
if (cov->list) |
| 444 |
RELEASE(cov->list); |
| 445 |
cov->list = NULL; |
| 446 |
break; |
| 447 |
case 2: /* range */ |
| 448 |
if (cov->range) |
| 449 |
RELEASE(cov->range); |
| 450 |
cov->range = NULL; |
| 451 |
break; |
| 452 |
default: |
| 453 |
ERROR("Unknown coverage format"); |
| 454 |
} |
| 455 |
} |
| 456 |
cov->count = 0; |
| 457 |
} |
| 458 |
|
| 459 |
/* returns -1 if not found */ |
| 460 |
static int |
| 461 |
clt_lookup_coverage (struct clt_coverage *cov, USHORT gid) |
| 462 |
{ |
| 463 |
int i; |
| 464 |
|
| 465 |
ASSERT(cov); |
| 466 |
|
| 467 |
switch (cov->format) { |
| 468 |
case 1: /* list */ |
| 469 |
for (i = 0; i < cov->count; i++) { |
| 470 |
if (cov->list[i] > gid) { |
| 471 |
break; |
| 472 |
} else if (cov->list[i] == gid) { |
| 473 |
return i; /* found */ |
| 474 |
} |
| 475 |
} |
| 476 |
break; |
| 477 |
case 2: /* range */ |
| 478 |
for (i = 0; i < cov->count; i++) { |
| 479 |
if (gid < cov->range[i].Start) { |
| 480 |
break; |
| 481 |
} else if (gid <= cov->range[i].End) { /* found */ |
| 482 |
return (cov->range[i].StartCoverageIndex + |
| 483 |
gid - cov->range[i].Start); |
| 484 |
} |
| 485 |
} |
| 486 |
break; |
| 487 |
default: |
| 488 |
ERROR("Unknown coverage format"); |
| 489 |
} |
| 490 |
|
| 491 |
return -1; /* not found */ |
| 492 |
} |
| 493 |
|
| 494 |
static int |
| 495 |
otl_gsub_read_single (struct otl_gsub_subtab *subtab, sfnt *sfont) |
| 496 |
{ |
| 497 |
int len; |
| 498 |
ULONG offset; /* not Offset which is USHORT */ |
| 499 |
Offset cov_offset; /* subtable offset, offset to Coverage table */ |
| 500 |
|
| 501 |
ASSERT(subtab && sfont); |
| 502 |
|
| 503 |
offset = tell_position(sfont->stream); |
| 504 |
|
| 505 |
subtab->LookupType = OTL_GSUB_TYPE_SINGLE; |
| 506 |
subtab->SubstFormat = sfnt_get_ushort(sfont); |
| 507 |
len = 2; |
| 508 |
|
| 509 |
if (subtab->SubstFormat == 1) { |
| 510 |
struct otl_gsub_single1 *data; |
| 511 |
|
| 512 |
subtab->table.single1 = data = NEW(1, struct otl_gsub_single1); |
| 513 |
cov_offset = sfnt_get_ushort(sfont); |
| 514 |
data->DeltaGlyphID = sfnt_get_short(sfont); |
| 515 |
len += 4; |
| 516 |
|
| 517 |
sfnt_seek_set(sfont, offset + cov_offset); |
| 518 |
len += clt_read_coverage(&data->coverage, sfont); |
| 519 |
|
| 520 |
} else if (subtab->SubstFormat == 2) { |
| 521 |
struct otl_gsub_single2 *data; |
| 522 |
USHORT count; |
| 523 |
|
| 524 |
subtab->table.single2 = data = NEW(1, struct otl_gsub_single2); |
| 525 |
cov_offset = sfnt_get_ushort(sfont); |
| 526 |
data->GlyphCount = sfnt_get_ushort(sfont); |
| 527 |
len += 4; |
| 528 |
|
| 529 |
if (data->GlyphCount == 0) |
| 530 |
data->Substitute = NULL; |
| 531 |
else { |
| 532 |
data->Substitute = NEW(data->GlyphCount, GlyphID); |
| 533 |
for (count = 0; count < data->GlyphCount; count++) { |
| 534 |
data->Substitute[count] = sfnt_get_ushort(sfont); |
| 535 |
} |
| 536 |
len += 2 * data->GlyphCount; |
| 537 |
} |
| 538 |
|
| 539 |
sfnt_seek_set(sfont, offset + cov_offset); |
| 540 |
len += clt_read_coverage(&data->coverage, sfont); |
| 541 |
|
| 542 |
} else { |
| 543 |
ERROR("unexpected SubstFormat"); |
| 544 |
} |
| 545 |
/* not implemented yet */ |
| 546 |
|
| 547 |
return len; |
| 548 |
} |
| 549 |
|
| 550 |
static int |
| 551 |
otl_gsub_read_alternate (struct otl_gsub_subtab *subtab, sfnt *sfont) |
| 552 |
{ |
| 553 |
int len; |
| 554 |
USHORT i, j; |
| 555 |
ULONG offset; /* not Offset which is USHORT */ |
| 556 |
Offset cov_offset; /* subtable offset, offset to Coverage table */ |
| 557 |
struct clt_number_list altset_offsets; |
| 558 |
struct otl_gsub_alternate1 *data; |
| 559 |
|
| 560 |
ASSERT(subtab && sfont); |
| 561 |
|
| 562 |
offset = tell_position(sfont->stream); |
| 563 |
|
| 564 |
subtab->LookupType = OTL_GSUB_TYPE_ALTERNATE; |
| 565 |
subtab->SubstFormat = sfnt_get_ushort(sfont); /* Must be 1 */ |
| 566 |
if (subtab->SubstFormat != 1) { |
| 567 |
WARN("Unknown GSUB SubstFormat for Alternate: %u", |
| 568 |
subtab->SubstFormat); |
| 569 |
return -1; |
| 570 |
} |
| 571 |
|
| 572 |
len = 2; |
| 573 |
subtab->table.alternate1 = |
| 574 |
data = NEW(1, struct otl_gsub_alternate1); |
| 575 |
|
| 576 |
cov_offset = sfnt_get_ushort(sfont); |
| 577 |
len += 2; |
| 578 |
len += clt_read_number_list(&altset_offsets, sfont); |
| 579 |
data->AlternateSetCount = altset_offsets.count; |
| 580 |
if (data->AlternateSetCount == 0) { |
| 581 |
data->AlternateSet = NULL; |
| 582 |
} else { |
| 583 |
data->AlternateSet = NEW(data->AlternateSetCount, |
| 584 |
struct otl_gsub_altset); |
| 585 |
for (i = 0; i < data->AlternateSetCount; i++) { |
| 586 |
struct otl_gsub_altset *altset; |
| 587 |
ULONG altset_offset; |
| 588 |
|
| 589 |
altset = &(data->AlternateSet[i]); |
| 590 |
|
| 591 |
altset_offset = offset + altset_offsets.value[i]; |
| 592 |
sfnt_seek_set(sfont, altset_offset); |
| 593 |
altset->GlyphCount = sfnt_get_ushort(sfont); |
| 594 |
len += 2; |
| 595 |
if (altset->GlyphCount == 0) { |
| 596 |
altset->Alternate = NULL; |
| 597 |
continue; |
| 598 |
} |
| 599 |
altset->Alternate = NEW(altset->GlyphCount, GlyphID); |
| 600 |
for (j = 0; j < altset->GlyphCount; j++) { |
| 601 |
altset->Alternate[j] = sfnt_get_ushort(sfont); |
| 602 |
len += 2; |
| 603 |
} |
| 604 |
} |
| 605 |
clt_release_number_list(&altset_offsets); |
| 606 |
} |
| 607 |
sfnt_seek_set(sfont, offset + cov_offset); |
| 608 |
len += clt_read_coverage(&data->coverage, sfont); |
| 609 |
|
| 610 |
return len; |
| 611 |
} |
| 612 |
|
| 613 |
static int |
| 614 |
otl_gsub_read_ligature (struct otl_gsub_subtab *subtab, sfnt *sfont) |
| 615 |
{ |
| 616 |
int len; |
| 617 |
USHORT i, j; |
| 618 |
ULONG offset; /* not Offset which is USHORT */ |
| 619 |
Offset cov_offset; /* subtable offset, offset to Coverage table */ |
| 620 |
struct clt_number_list ligset_offsets; |
| 621 |
struct otl_gsub_ligature1 *data; |
| 622 |
|
| 623 |
ASSERT(subtab && sfont); |
| 624 |
|
| 625 |
offset = tell_position(sfont->stream); |
| 626 |
|
| 627 |
subtab->LookupType = OTL_GSUB_TYPE_LIGATURE; |
| 628 |
subtab->SubstFormat = sfnt_get_ushort(sfont); /* Must be 1 */ |
| 629 |
if (subtab->SubstFormat != 1) { |
| 630 |
WARN("Unknown GSUB SubstFormat for Ligature: %u", |
| 631 |
subtab->SubstFormat); |
| 632 |
return -1; |
| 633 |
} |
| 634 |
|
| 635 |
len = 2; |
| 636 |
subtab->table.ligature1 = |
| 637 |
data = NEW(1, struct otl_gsub_ligature1); |
| 638 |
|
| 639 |
cov_offset = sfnt_get_ushort(sfont); |
| 640 |
len += 2; |
| 641 |
len += clt_read_number_list(&ligset_offsets, sfont); |
| 642 |
data->LigSetCount = ligset_offsets.count; |
| 643 |
if (data->LigSetCount == 0) { |
| 644 |
data->LigatureSet = NULL; |
| 645 |
} else { |
| 646 |
data->LigatureSet = NEW(data->LigSetCount, |
| 647 |
struct otl_gsub_ligset); |
| 648 |
for (i = 0; i < data->LigSetCount; i++) { |
| 649 |
struct clt_number_list ligset_tab; |
| 650 |
struct otl_gsub_ligset *ligset; |
| 651 |
ULONG ligset_offset; |
| 652 |
USHORT count; |
| 653 |
|
| 654 |
ligset = &(data->LigatureSet[i]); |
| 655 |
|
| 656 |
ligset_offset = offset + ligset_offsets.value[i]; |
| 657 |
sfnt_seek_set(sfont, ligset_offset); |
| 658 |
len += clt_read_number_list(&ligset_tab, sfont); |
| 659 |
|
| 660 |
ligset->LigatureCount = ligset_tab.count; |
| 661 |
if (ligset_tab.count == 0) { |
| 662 |
ligset->Ligature = NULL; |
| 663 |
continue; |
| 664 |
} |
| 665 |
ligset->Ligature = NEW(ligset_tab.count, |
| 666 |
struct otl_gsub_ligtab); |
| 667 |
for (j = 0; j < ligset_tab.count; j++) { |
| 668 |
sfnt_seek_set(sfont, ligset_offset + ligset_tab.value[j]); |
| 669 |
ligset->Ligature[j].LigGlyph = sfnt_get_ushort(sfont); |
| 670 |
ligset->Ligature[j].CompCount = sfnt_get_ushort(sfont); |
| 671 |
if (ligset->Ligature[j].CompCount == 0) { |
| 672 |
ligset->Ligature[j].Component = NULL; |
| 673 |
continue; |
| 674 |
} |
| 675 |
ligset->Ligature[j].Component = |
| 676 |
NEW(ligset->Ligature[j].CompCount - 1, GlyphID); |
| 677 |
for (count = 0; |
| 678 |
count < ligset->Ligature[j].CompCount - 1; count++) { |
| 679 |
ligset->Ligature[j].Component[count] = sfnt_get_ushort(sfont); |
| 680 |
} |
| 681 |
len += 4 + count * 2; |
| 682 |
} |
| 683 |
clt_release_number_list(&ligset_tab); |
| 684 |
} |
| 685 |
} |
| 686 |
clt_release_number_list(&ligset_offsets); |
| 687 |
|
| 688 |
sfnt_seek_set(sfont, offset + cov_offset); |
| 689 |
len += clt_read_coverage(&data->coverage, sfont); |
| 690 |
|
| 691 |
return len; |
| 692 |
} |
| 693 |
|
| 694 |
static void |
| 695 |
otl_gsub_release_single (struct otl_gsub_subtab *subtab) |
| 696 |
{ |
| 697 |
if (subtab) { |
| 698 |
switch((int) subtab->SubstFormat) { |
| 699 |
case 1: |
| 700 |
{ |
| 701 |
struct otl_gsub_single1 *data; |
| 702 |
|
| 703 |
data = subtab->table.single1; |
| 704 |
if (data) { |
| 705 |
clt_release_coverage(&data->coverage); |
| 706 |
RELEASE(data); |
| 707 |
} |
| 708 |
subtab->table.single1 = NULL; |
| 709 |
} |
| 710 |
break; |
| 711 |
case 2: |
| 712 |
{ |
| 713 |
struct otl_gsub_single2 *data; |
| 714 |
|
| 715 |
data = subtab->table.single2; |
| 716 |
if (data) { |
| 717 |
if (data->Substitute) |
| 718 |
RELEASE(data->Substitute); |
| 719 |
clt_release_coverage(&data->coverage); |
| 720 |
RELEASE(data); |
| 721 |
} |
| 722 |
subtab->table.single2 = NULL; |
| 723 |
} |
| 724 |
break; |
| 725 |
default: |
| 726 |
ERROR("Unknown format for single substitution"); |
| 727 |
} |
| 728 |
} |
| 729 |
} |
| 730 |
|
| 731 |
static void |
| 732 |
otl_gsub_release_ligature (struct otl_gsub_subtab *subtab) |
| 733 |
{ |
| 734 |
if (subtab) { |
| 735 |
struct otl_gsub_ligature1 *data; |
| 736 |
USHORT i, j; |
| 737 |
|
| 738 |
data = subtab->table.ligature1; |
| 739 |
if (data && data->LigatureSet) { |
| 740 |
for (i = 0; i < data->LigSetCount; i++) { |
| 741 |
struct otl_gsub_ligset *ligset; |
| 742 |
|
| 743 |
ligset = &(data->LigatureSet[i]); |
| 744 |
for (j = 0; |
| 745 |
j < ligset->LigatureCount; j++) { |
| 746 |
if (ligset->Ligature[j].Component) |
| 747 |
RELEASE(ligset->Ligature[j].Component); |
| 748 |
ligset->Ligature[j].Component = NULL; |
| 749 |
} |
| 750 |
RELEASE(ligset->Ligature); |
| 751 |
ligset->Ligature = NULL; |
| 752 |
} |
| 753 |
RELEASE(data->LigatureSet); |
| 754 |
} |
| 755 |
clt_release_coverage(&data->coverage); |
| 756 |
data->LigatureSet = NULL; |
| 757 |
RELEASE(data); |
| 758 |
subtab->table.ligature1 = NULL; |
| 759 |
} |
| 760 |
} |
| 761 |
|
| 762 |
static void |
| 763 |
otl_gsub_release_alternate (struct otl_gsub_subtab *subtab) |
| 764 |
{ |
| 765 |
if (subtab) { |
| 766 |
struct otl_gsub_alternate1 *data; |
| 767 |
USHORT i; |
| 768 |
|
| 769 |
data = subtab->table.alternate1; |
| 770 |
if (data && data->AlternateSet) { |
| 771 |
for (i = 0; i < data->AlternateSetCount; i++) { |
| 772 |
struct otl_gsub_altset *altset; |
| 773 |
|
| 774 |
altset = &(data->AlternateSet[i]); |
| 775 |
if (altset->Alternate) |
| 776 |
RELEASE(altset->Alternate); |
| 777 |
altset->Alternate = NULL; |
| 778 |
} |
| 779 |
RELEASE(data->AlternateSet); |
| 780 |
} |
| 781 |
clt_release_coverage(&data->coverage); |
| 782 |
data->AlternateSet = NULL; |
| 783 |
RELEASE(data); |
| 784 |
subtab->table.alternate1 = NULL; |
| 785 |
} |
| 786 |
} |
| 787 |
|
| 788 |
static int |
| 789 |
otl_gsub_read_header (struct otl_gsub_header *head, sfnt *sfont) |
| 790 |
{ |
| 791 |
ASSERT(head && sfont); |
| 792 |
|
| 793 |
head->version = sfnt_get_ulong (sfont); |
| 794 |
head->ScriptList = sfnt_get_ushort(sfont); |
| 795 |
head->FeatureList = sfnt_get_ushort(sfont); |
| 796 |
head->LookupList = sfnt_get_ushort(sfont); |
| 797 |
|
| 798 |
return 10; |
| 799 |
} |
| 800 |
|
| 801 |
/* |
| 802 |
* script -- langsys --> feature indices |
| 803 |
* | |
| 804 |
* +- langsys --> feature indices |
| 805 |
* |
| 806 |
* feature --> lookup indices |
| 807 |
*/ |
| 808 |
|
| 809 |
struct otl_gsub_tab |
| 810 |
{ |
| 811 |
char *script; |
| 812 |
char *language; |
| 813 |
char *feature; |
| 814 |
|
| 815 |
int num_subtables; |
| 816 |
struct otl_gsub_subtab *subtables; |
| 817 |
}; |
| 818 |
|
| 819 |
|
| 820 |
static int |
| 821 |
otl_gsub_read_feat (struct otl_gsub_tab *gsub, sfnt *sfont) |
| 822 |
{ |
| 823 |
int feat_idx, script_idx; |
| 824 |
ULONG gsub_offset, offset; |
| 825 |
struct otl_gsub_header head; |
| 826 |
struct otl_gsub_subtab *subtab = NULL; |
| 827 |
USHORT num_subtabs = 0; |
| 828 |
unsigned char feat_bits[8192]; |
| 829 |
struct clt_record_list feature_list; |
| 830 |
struct clt_record_list script_list; |
| 831 |
struct clt_number_list lookup_list ; |
| 832 |
otl_opt *script, *language, *feature; |
| 833 |
|
| 834 |
ASSERT(gsub && sfont); |
| 835 |
|
| 836 |
gsub_offset = sfnt_find_table_pos(sfont, "GSUB"); |
| 837 |
if (gsub_offset == 0) |
| 838 |
return -1; /* not found */ |
| 839 |
|
| 840 |
script = otl_new_opt(); |
| 841 |
otl_parse_optstring(script, gsub->script); |
| 842 |
language = otl_new_opt(); |
| 843 |
otl_parse_optstring(language, gsub->language); |
| 844 |
feature = otl_new_opt(); |
| 845 |
otl_parse_optstring(feature, gsub->feature); |
| 846 |
|
| 847 |
memset(feat_bits, 0, 8192); |
| 848 |
|
| 849 |
/* GSUB header */ |
| 850 |
sfnt_seek_set(sfont, gsub_offset); |
| 851 |
otl_gsub_read_header(&head, sfont); |
| 852 |
|
| 853 |
/* Script */ |
| 854 |
offset = gsub_offset + head.ScriptList; |
| 855 |
sfnt_seek_set(sfont, offset); |
| 856 |
clt_read_record_list(&script_list, sfont); |
| 857 |
|
| 858 |
#define SET_BIT(b,p) do {\ |
| 859 |
(b)[(p)/8] |= (1<<(7-((p) % 8)));\ |
| 860 |
} while (0) |
| 861 |
#define BIT_SET(b,p) (((b)[(p)/8]) & (1 << (7-((p)%8)))) |
| 862 |
|
| 863 |
for (script_idx = 0; |
| 864 |
script_idx < script_list.count; script_idx++) { |
| 865 |
if (otl_match_optrule(script, |
| 866 |
script_list.record[script_idx].tag)) { |
| 867 |
struct clt_script_table script_tab; |
| 868 |
int langsys_idx; |
| 869 |
|
| 870 |
offset = gsub_offset + |
| 871 |
head.ScriptList + script_list.record[script_idx].offset; |
| 872 |
sfnt_seek_set(sfont, offset); |
| 873 |
clt_read_script_table(&script_tab, sfont); |
| 874 |
|
| 875 |
if (otl_match_optrule(language, "dflt") && |
| 876 |
script_tab.DefaultLangSys != 0) { |
| 877 |
struct clt_langsys_table langsys_tab; |
| 878 |
|
| 879 |
if(dpx_conf.verbose_level > VERBOSE_LEVEL_MIN) { |
| 880 |
MESG("otl_gsub>> OTL script-language enabled: %c%c%c%c.dflt\n", |
| 881 |
script_list.record[script_idx].tag[0], |
| 882 |
script_list.record[script_idx].tag[1], |
| 883 |
script_list.record[script_idx].tag[2], |
| 884 |
script_list.record[script_idx].tag[3]); |
| 885 |
} |
| 886 |
|
| 887 |
sfnt_seek_set(sfont, offset + script_tab.DefaultLangSys); |
| 888 |
clt_read_langsys_table(&langsys_tab, sfont); |
| 889 |
if (otl_match_optrule(feature, "____") && /* _FIXME_ */ |
| 890 |
langsys_tab.ReqFeatureIndex != 0xFFFF) |
| 891 |
SET_BIT(feat_bits, langsys_tab.ReqFeatureIndex); |
| 892 |
for (feat_idx = 0; |
| 893 |
feat_idx < langsys_tab.FeatureIndex.count; |
| 894 |
feat_idx++) { |
| 895 |
SET_BIT(feat_bits, |
| 896 |
langsys_tab.FeatureIndex.value[feat_idx]); |
| 897 |
} |
| 898 |
clt_release_langsys_table(&langsys_tab); |
| 899 |
} |
| 900 |
for (langsys_idx = 0; |
| 901 |
langsys_idx < script_tab.LangSysRecord.count; |
| 902 |
langsys_idx++) { |
| 903 |
struct clt_record *langsys_rec; |
| 904 |
|
| 905 |
langsys_rec = &(script_tab.LangSysRecord.record[langsys_idx]); |
| 906 |
if (otl_match_optrule(language, langsys_rec->tag)) { |
| 907 |
struct clt_langsys_table langsys_tab; |
| 908 |
|
| 909 |
if(dpx_conf.verbose_level > VERBOSE_LEVEL_MIN) { |
| 910 |
MESG("otl_gsub>> OTL script-language enabled: %c%c%c%c.%c%c%c%c\n", |
| 911 |
script_list.record[script_idx].tag[0], |
| 912 |
script_list.record[script_idx].tag[1], |
| 913 |
script_list.record[script_idx].tag[2], |
| 914 |
script_list.record[script_idx].tag[3], |
| 915 |
langsys_rec->tag[0], langsys_rec->tag[1], |
| 916 |
langsys_rec->tag[2], langsys_rec->tag[3]); |
| 917 |
} |
| 918 |
|
| 919 |
sfnt_seek_set(sfont, offset + langsys_rec->offset); |
| 920 |
clt_read_langsys_table(&langsys_tab, sfont); |
| 921 |
if (otl_match_optrule(feature, "____") || /* _FIXME_ */ |
| 922 |
langsys_tab.ReqFeatureIndex != 0xFFFF) |
| 923 |
SET_BIT(feat_bits, langsys_tab.ReqFeatureIndex); |
| 924 |
for (feat_idx = 0; |
| 925 |
feat_idx < langsys_tab.FeatureIndex.count; |
| 926 |
feat_idx++) { |
| 927 |
SET_BIT(feat_bits, |
| 928 |
langsys_tab.FeatureIndex.value[feat_idx]); |
| 929 |
} |
| 930 |
clt_release_langsys_table(&langsys_tab); |
| 931 |
} |
| 932 |
} |
| 933 |
clt_release_script_table(&script_tab); |
| 934 |
} |
| 935 |
} |
| 936 |
|
| 937 |
/* Feature List */ |
| 938 |
offset = gsub_offset + head.FeatureList; |
| 939 |
sfnt_seek_set(sfont, offset); |
| 940 |
clt_read_record_list(&feature_list, sfont); |
| 941 |
|
| 942 |
/* Lookup List */ |
| 943 |
offset = gsub_offset + head.LookupList; |
| 944 |
sfnt_seek_set(sfont, offset); |
| 945 |
clt_read_number_list(&lookup_list, sfont); |
| 946 |
|
| 947 |
if(dpx_conf.verbose_level > VERBOSE_LEVEL_MIN) { |
| 948 |
MESG("otl_gsub>> Reading OTL feature(s):"); |
| 949 |
} |
| 950 |
|
| 951 |
for (feat_idx = 0; |
| 952 |
feat_idx < feature_list.count; feat_idx++) { |
| 953 |
if (BIT_SET(feat_bits, feat_idx) && |
| 954 |
(otl_match_optrule(feature, |
| 955 |
feature_list.record[feat_idx].tag))) { |
| 956 |
struct clt_feature_table feature_table; |
| 957 |
int i; |
| 958 |
|
| 959 |
if(dpx_conf.verbose_level > VERBOSE_LEVEL_MIN) { |
| 960 |
MESG(" %c%c%c%c", |
| 961 |
feature_list.record[feat_idx].tag[0], |
| 962 |
feature_list.record[feat_idx].tag[1], |
| 963 |
feature_list.record[feat_idx].tag[2], |
| 964 |
feature_list.record[feat_idx].tag[3]); |
| 965 |
} |
| 966 |
|
| 967 |
/* Feature Table */ |
| 968 |
offset = gsub_offset + |
| 969 |
head.FeatureList + feature_list.record[feat_idx].offset; |
| 970 |
|
| 971 |
sfnt_seek_set(sfont, offset); |
| 972 |
clt_read_feature_table(&feature_table, sfont); |
| 973 |
#if 0 |
| 974 |
if (feature_table.FeatureParams != 0) { |
| 975 |
ERROR("unrecognized FeatureParams"); |
| 976 |
} |
| 977 |
#endif |
| 978 |
/* Lookup table */ |
| 979 |
for (i = 0; i < feature_table.LookupListIndex.count; i++) { |
| 980 |
struct clt_lookup_table lookup_table; |
| 981 |
int ll_idx, st_idx, r, n_st; |
| 982 |
|
| 983 |
ll_idx = feature_table.LookupListIndex.value[i]; |
| 984 |
if (ll_idx >= lookup_list.count) |
| 985 |
ERROR("invalid Lookup index."); |
| 986 |
|
| 987 |
offset = gsub_offset + |
| 988 |
head.LookupList + (lookup_list.value)[ll_idx]; |
| 989 |
sfnt_seek_set(sfont, offset); |
| 990 |
clt_read_lookup_table(&lookup_table, sfont); |
| 991 |
|
| 992 |
if (lookup_table.LookupType != OTL_GSUB_TYPE_SINGLE && |
| 993 |
lookup_table.LookupType != OTL_GSUB_TYPE_ALTERNATE && |
| 994 |
lookup_table.LookupType != OTL_GSUB_TYPE_LIGATURE && |
| 995 |
lookup_table.LookupType != OTL_GSUB_TYPE_ESUBST) { |
| 996 |
if (dpx_conf.verbose_level > VERBOSE_LEVEL_MIN) |
| 997 |
WARN("Skipping unsupported GSUB subtable: LookupType=%d", lookup_table.LookupType); |
| 998 |
continue; |
| 999 |
} |
| 1000 |
|
| 1001 |
subtab = RENEW(subtab, |
| 1002 |
num_subtabs + lookup_table.SubTableList.count, |
| 1003 |
struct otl_gsub_subtab); |
| 1004 |
for (n_st = 0, st_idx = 0; |
| 1005 |
st_idx < lookup_table.SubTableList.count; st_idx++) { |
| 1006 |
|
| 1007 |
offset = gsub_offset + head.LookupList + |
| 1008 |
lookup_list.value[ll_idx] + |
| 1009 |
(lookup_table.SubTableList.value)[st_idx]; |
| 1010 |
|
| 1011 |
sfnt_seek_set(sfont, offset); |
| 1012 |
|
| 1013 |
switch ((int) lookup_table.LookupType) { |
| 1014 |
case OTL_GSUB_TYPE_SINGLE: |
| 1015 |
r = otl_gsub_read_single(&subtab[num_subtabs + n_st], |
| 1016 |
sfont); |
| 1017 |
if (r <= 0) |
| 1018 |
WARN("Reading GSUB subtable (single) failed..."); |
| 1019 |
else { |
| 1020 |
if(dpx_conf.verbose_level > VERBOSE_LEVEL_MIN) { |
| 1021 |
MESG("(single)"); |
| 1022 |
} |
| 1023 |
n_st++; |
| 1024 |
} |
| 1025 |
break; |
| 1026 |
|
| 1027 |
case OTL_GSUB_TYPE_ALTERNATE: |
| 1028 |
r = otl_gsub_read_alternate(&subtab[num_subtabs + n_st], |
| 1029 |
sfont); |
| 1030 |
if (r <= 0) |
| 1031 |
WARN("Reading GSUB subtable (alternate) failed..."); |
| 1032 |
else { |
| 1033 |
if(dpx_conf.verbose_level > VERBOSE_LEVEL_MIN) { |
| 1034 |
MESG("(alternate)"); |
| 1035 |
} |
| 1036 |
n_st++; |
| 1037 |
} |
| 1038 |
break; |
| 1039 |
|
| 1040 |
case OTL_GSUB_TYPE_LIGATURE: |
| 1041 |
r = otl_gsub_read_ligature(&subtab[num_subtabs + n_st], |
| 1042 |
sfont); |
| 1043 |
if (r <= 0) |
| 1044 |
WARN("Reading GSUB subtable (ligature) failed..."); |
| 1045 |
else { |
| 1046 |
if(dpx_conf.verbose_level > VERBOSE_LEVEL_MIN) { |
| 1047 |
MESG("(ligature)"); |
| 1048 |
} |
| 1049 |
n_st++; |
| 1050 |
} |
| 1051 |
break; |
| 1052 |
|
| 1053 |
case OTL_GSUB_TYPE_ESUBST: |
| 1054 |
{ |
| 1055 |
USHORT SubstFormat; |
| 1056 |
USHORT ExtensionLookupType; |
| 1057 |
ULONG ExtensionOffset; |
| 1058 |
|
| 1059 |
SubstFormat = sfnt_get_ushort(sfont); |
| 1060 |
if (SubstFormat != 1) |
| 1061 |
break; |
| 1062 |
ExtensionLookupType = sfnt_get_ushort(sfont); |
| 1063 |
ExtensionOffset = sfnt_get_ulong (sfont); |
| 1064 |
|
| 1065 |
sfnt_seek_set(sfont, offset + ExtensionOffset); |
| 1066 |
switch (ExtensionLookupType) { |
| 1067 |
case OTL_GSUB_TYPE_SINGLE: |
| 1068 |
r = otl_gsub_read_single(&subtab[num_subtabs + n_st], |
| 1069 |
sfont); |
| 1070 |
if (r <= 0) |
| 1071 |
WARN("Reading GSUB subtable (ext:single) failed..."); |
| 1072 |
else { |
| 1073 |
if(dpx_conf.verbose_level > VERBOSE_LEVEL_MIN) { |
| 1074 |
MESG("(ext:single)"); |
| 1075 |
} |
| 1076 |
n_st++; |
| 1077 |
} |
| 1078 |
break; |
| 1079 |
|
| 1080 |
case OTL_GSUB_TYPE_ALTERNATE: |
| 1081 |
r = otl_gsub_read_alternate(&subtab[num_subtabs + n_st], |
| 1082 |
sfont); |
| 1083 |
if (r <= 0) |
| 1084 |
WARN("Reading GSUB subtable (alternate) failed..."); |
| 1085 |
else { |
| 1086 |
if(dpx_conf.verbose_level > VERBOSE_LEVEL_MIN) { |
| 1087 |
MESG("(alternate)"); |
| 1088 |
} |
| 1089 |
n_st++; |
| 1090 |
} |
| 1091 |
break; |
| 1092 |
|
| 1093 |
case OTL_GSUB_TYPE_LIGATURE: |
| 1094 |
r = otl_gsub_read_ligature(&subtab[num_subtabs + n_st], |
| 1095 |
sfont); |
| 1096 |
if (r <= 0) |
| 1097 |
WARN("Reading GSUB subtable (ext:ligature) failed..."); |
| 1098 |
else { |
| 1099 |
if(dpx_conf.verbose_level > VERBOSE_LEVEL_MIN) { |
| 1100 |
MESG("(ext:ligature)"); |
| 1101 |
} |
| 1102 |
n_st++; |
| 1103 |
} |
| 1104 |
break; |
| 1105 |
|
| 1106 |
} |
| 1107 |
} |
| 1108 |
break; |
| 1109 |
|
| 1110 |
default: |
| 1111 |
break; |
| 1112 |
} |
| 1113 |
} |
| 1114 |
num_subtabs += n_st; /* lookup_table.SubTableList.count; */ |
| 1115 |
clt_release_lookup_table(&lookup_table); |
| 1116 |
} |
| 1117 |
clt_release_feature_table(&feature_table); |
| 1118 |
} |
| 1119 |
} |
| 1120 |
|
| 1121 |
if(dpx_conf.verbose_level > VERBOSE_LEVEL_MIN) { |
| 1122 |
MESG("\n"); |
| 1123 |
MESG("otl_gsub>> %ld subtable(s) read.\n", num_subtabs); |
| 1124 |
} |
| 1125 |
|
| 1126 |
clt_release_number_list(&lookup_list); |
| 1127 |
clt_release_record_list(&feature_list); |
| 1128 |
clt_release_record_list(&script_list); |
| 1129 |
|
| 1130 |
otl_release_opt(script); |
| 1131 |
otl_release_opt(language); |
| 1132 |
otl_release_opt(feature); |
| 1133 |
|
| 1134 |
if (subtab != NULL) { |
| 1135 |
gsub->num_subtables = num_subtabs; |
| 1136 |
gsub->subtables = subtab; |
| 1137 |
} else { |
| 1138 |
return -1; |
| 1139 |
} |
| 1140 |
|
| 1141 |
return 0; |
| 1142 |
} |
| 1143 |
|
| 1144 |
|
| 1145 |
static int |
| 1146 |
otl_gsub_apply_single (struct otl_gsub_subtab *subtab, USHORT *gid) |
| 1147 |
{ |
| 1148 |
int idx; |
| 1149 |
|
| 1150 |
ASSERT(subtab && gid); |
| 1151 |
|
| 1152 |
if (subtab->SubstFormat == 1) { |
| 1153 |
struct otl_gsub_single1 *data; |
| 1154 |
|
| 1155 |
data = (subtab->table).single1; |
| 1156 |
idx = clt_lookup_coverage(&data->coverage, *gid); |
| 1157 |
if (idx >= 0) { |
| 1158 |
*gid += data->DeltaGlyphID; |
| 1159 |
return 0; /* found */ |
| 1160 |
} |
| 1161 |
} else if (subtab->SubstFormat == 2) { |
| 1162 |
struct otl_gsub_single2 *data; |
| 1163 |
|
| 1164 |
data = (subtab->table).single2; |
| 1165 |
idx = clt_lookup_coverage(&data->coverage, *gid); |
| 1166 |
if (idx >= 0 && |
| 1167 |
idx < data->GlyphCount) { |
| 1168 |
*gid = (data->Substitute)[idx]; |
| 1169 |
return 0; /* found */ |
| 1170 |
} |
| 1171 |
} |
| 1172 |
|
| 1173 |
return -1; |
| 1174 |
} |
| 1175 |
|
| 1176 |
static int |
| 1177 |
otl_gsub_apply_alternate (struct otl_gsub_subtab *subtab, |
| 1178 |
USHORT alt_idx, USHORT *gid) |
| 1179 |
{ |
| 1180 |
int idx; |
| 1181 |
|
| 1182 |
ASSERT(subtab && gid); |
| 1183 |
|
| 1184 |
if (subtab->SubstFormat == 1) { |
| 1185 |
struct otl_gsub_alternate1 *data; |
| 1186 |
|
| 1187 |
data = subtab->table.alternate1; |
| 1188 |
idx = clt_lookup_coverage(&data->coverage, *gid); |
| 1189 |
if (idx < 0 || idx >= data->AlternateSetCount) |
| 1190 |
return -1; |
| 1191 |
else { |
| 1192 |
struct otl_gsub_altset *altset; |
| 1193 |
altset = &(data->AlternateSet[idx]); |
| 1194 |
if (alt_idx >= altset->GlyphCount) |
| 1195 |
return -1; |
| 1196 |
else { |
| 1197 |
*gid = altset->Alternate[alt_idx]; |
| 1198 |
return 0; |
| 1199 |
} |
| 1200 |
} |
| 1201 |
} |
| 1202 |
|
| 1203 |
return -1; |
| 1204 |
} |
| 1205 |
|
| 1206 |
/* NOTE: Ligature table is in preference order */ |
| 1207 |
static int |
| 1208 |
glyph_seq_cmp (GlyphID *glyph_seq0, USHORT n_glyphs0, |
| 1209 |
GlyphID *glyph_seq1, USHORT n_glyphs1) |
| 1210 |
{ |
| 1211 |
USHORT i; |
| 1212 |
|
| 1213 |
if (n_glyphs0 != n_glyphs1) |
| 1214 |
return n_glyphs0 - n_glyphs1; |
| 1215 |
|
| 1216 |
for (i = 0; i < n_glyphs0; i++) { |
| 1217 |
if (glyph_seq0[i] != glyph_seq1[i]) |
| 1218 |
return glyph_seq0[i] - glyph_seq1[i]; |
| 1219 |
} |
| 1220 |
|
| 1221 |
return 0; |
| 1222 |
} |
| 1223 |
|
| 1224 |
static int |
| 1225 |
otl_gsub_apply_ligature (struct otl_gsub_subtab *subtab, |
| 1226 |
USHORT *gid_in, USHORT num_gids, |
| 1227 |
USHORT *gid_out) |
| 1228 |
{ |
| 1229 |
int idx; |
| 1230 |
|
| 1231 |
ASSERT(subtab && gid_out); |
| 1232 |
|
| 1233 |
if (!gid_in || num_gids < 1) |
| 1234 |
return -1; |
| 1235 |
|
| 1236 |
if (subtab->SubstFormat == 1) { |
| 1237 |
struct otl_gsub_ligature1 *data; |
| 1238 |
|
| 1239 |
data = subtab->table.ligature1; |
| 1240 |
idx = clt_lookup_coverage(&data->coverage, gid_in[0]); |
| 1241 |
if (idx >= 0 && idx < data->LigSetCount) { |
| 1242 |
struct otl_gsub_ligset *ligset; |
| 1243 |
USHORT j; |
| 1244 |
|
| 1245 |
ligset = &(data->LigatureSet[idx]); |
| 1246 |
for (j = 0; j < ligset->LigatureCount; j++) { |
| 1247 |
if (!glyph_seq_cmp(&gid_in[1], (USHORT)(num_gids - 1), |
| 1248 |
ligset->Ligature[j].Component, |
| 1249 |
(USHORT)(ligset->Ligature[j].CompCount - 1))) { |
| 1250 |
*gid_out = ligset->Ligature[j].LigGlyph; |
| 1251 |
return 0; /* found */ |
| 1252 |
} |
| 1253 |
} |
| 1254 |
} |
| 1255 |
} |
| 1256 |
|
| 1257 |
return -1; |
| 1258 |
} |
| 1259 |
|
| 1260 |
#define GSUB_LIST_MAX 32 |
| 1261 |
struct gsub_entry { |
| 1262 |
int index; |
| 1263 |
struct gsub_entry *next; |
| 1264 |
}; |
| 1265 |
|
| 1266 |
struct otl_gsub |
| 1267 |
{ |
| 1268 |
int num_gsubs; |
| 1269 |
int select; |
| 1270 |
struct gsub_entry *first; |
| 1271 |
struct otl_gsub_tab gsubs[GSUB_LIST_MAX]; |
| 1272 |
}; |
| 1273 |
|
| 1274 |
otl_gsub * |
| 1275 |
otl_gsub_new (void) |
| 1276 |
{ |
| 1277 |
struct otl_gsub *gsub_list; |
| 1278 |
|
| 1279 |
gsub_list = NEW(1, struct otl_gsub); |
| 1280 |
gsub_list->num_gsubs = 0; |
| 1281 |
gsub_list->select = -1; |
| 1282 |
gsub_list->first = NULL; |
| 1283 |
|
| 1284 |
return (otl_gsub *) gsub_list; |
| 1285 |
} |
| 1286 |
|
| 1287 |
static void |
| 1288 |
clear_chain (otl_gsub *gsub_list) { |
| 1289 |
struct gsub_entry *entry, *next; |
| 1290 |
for (entry = gsub_list->first; entry != NULL; entry = next) { |
| 1291 |
next = entry->next; |
| 1292 |
RELEASE(entry); |
| 1293 |
} |
| 1294 |
gsub_list->first = NULL; |
| 1295 |
} |
| 1296 |
|
| 1297 |
int |
| 1298 |
otl_gsub_add_feat (otl_gsub *gsub_list, |
| 1299 |
const char *script, |
| 1300 |
const char *language, |
| 1301 |
const char *feature, |
| 1302 |
sfnt *sfont) |
| 1303 |
{ |
| 1304 |
int retval = -1; |
| 1305 |
int i; |
| 1306 |
struct otl_gsub_tab *gsub; |
| 1307 |
|
| 1308 |
if (gsub_list->num_gsubs > GSUB_LIST_MAX) { |
| 1309 |
ERROR("Too many GSUB features..."); |
| 1310 |
return -1; |
| 1311 |
} |
| 1312 |
for (i = 0; i < gsub_list->num_gsubs; i++) { |
| 1313 |
gsub = &(gsub_list->gsubs[i]); |
| 1314 |
if (!strcmp(script, gsub->script) && |
| 1315 |
!strcmp(language, gsub->language) && |
| 1316 |
!strcmp(feature, gsub->feature)) { |
| 1317 |
gsub_list->select = i; |
| 1318 |
return 0; |
| 1319 |
} |
| 1320 |
} |
| 1321 |
gsub = &gsub_list->gsubs[i]; |
| 1322 |
|
| 1323 |
gsub->script = NEW(strlen(script) +1, char); |
| 1324 |
strcpy(gsub->script, script); |
| 1325 |
gsub->language = NEW(strlen(language)+1, char); |
| 1326 |
strcpy(gsub->language, language); |
| 1327 |
gsub->feature = NEW(strlen(feature) +1, char); |
| 1328 |
strcpy(gsub->feature, feature); |
| 1329 |
|
| 1330 |
if(dpx_conf.verbose_level > VERBOSE_LEVEL_MIN) { |
| 1331 |
MESG("\n"); |
| 1332 |
MESG("otl_gsub>> Reading \"%s.%s.%s\"...\n", script, language, feature); |
| 1333 |
} |
| 1334 |
|
| 1335 |
retval = otl_gsub_read_feat(gsub, sfont); |
| 1336 |
if (retval >= 0) { |
| 1337 |
gsub_list->select = i; |
| 1338 |
gsub_list->num_gsubs++; |
| 1339 |
} else { |
| 1340 |
if(dpx_conf.verbose_level > VERBOSE_LEVEL_MIN) { |
| 1341 |
MESG("otl_gsub>> Failed\n"); |
| 1342 |
} |
| 1343 |
RELEASE(gsub->script); |
| 1344 |
RELEASE(gsub->language); |
| 1345 |
RELEASE(gsub->feature); |
| 1346 |
} |
| 1347 |
|
| 1348 |
return retval; |
| 1349 |
} |
| 1350 |
|
| 1351 |
static int |
| 1352 |
scan_otl_tag (const char *otl_tags, const char *endptr, |
| 1353 |
char *script, char *language, char *feature) |
| 1354 |
{ |
| 1355 |
const char *p, *period; |
| 1356 |
|
| 1357 |
ASSERT(script && language && feature); |
| 1358 |
|
| 1359 |
if (!otl_tags || otl_tags >= endptr) |
| 1360 |
return -1; |
| 1361 |
|
| 1362 |
memset(script, ' ', 4); script[4] = 0; |
| 1363 |
memset(language, ' ', 4); language[4] = 0; |
| 1364 |
memset(feature, ' ', 4); feature[4] = 0; |
| 1365 |
|
| 1366 |
/* First parse otl_tags variable */ |
| 1367 |
p = otl_tags; |
| 1368 |
period = strchr(p, '.'); |
| 1369 |
if (period && period < endptr) { |
| 1370 |
/* Format scrp.lang.feat */ |
| 1371 |
if (period < p + 5) { |
| 1372 |
strncpy(script, p, period - p); |
| 1373 |
} else { |
| 1374 |
WARN("Invalid OTL script tag found: %s", p); |
| 1375 |
return -1; |
| 1376 |
} |
| 1377 |
p = period + 1; |
| 1378 |
period = strchr(p, '.'); |
| 1379 |
if (period && period < endptr) { |
| 1380 |
/* Now lang part */ |
| 1381 |
if (period < p + 5) { |
| 1382 |
strncpy(language, p, period - p); |
| 1383 |
} else { |
| 1384 |
WARN("Invalid OTL lanuage tag found: %s", p); |
| 1385 |
return -1; |
| 1386 |
} |
| 1387 |
p = period + 1; |
| 1388 |
} |
| 1389 |
} else { |
| 1390 |
strcpy(script, "*"); |
| 1391 |
strcpy(language, "*"); |
| 1392 |
} |
| 1393 |
|
| 1394 |
/* Finally feature */ |
| 1395 |
if (p + 4 <= endptr) { |
| 1396 |
strncpy(feature, p, endptr - p); |
| 1397 |
p = endptr; |
| 1398 |
} else { |
| 1399 |
WARN("No valid OTL feature tag specified."); |
| 1400 |
return -1; |
| 1401 |
} |
| 1402 |
|
| 1403 |
return 0; |
| 1404 |
} |
| 1405 |
|
| 1406 |
void |
| 1407 |
otl_gsub_release (otl_gsub *gsub_list) |
| 1408 |
{ |
| 1409 |
struct otl_gsub_tab *gsub; |
| 1410 |
struct otl_gsub_subtab *subtab; |
| 1411 |
int i, j; |
| 1412 |
|
| 1413 |
if (!gsub_list) |
| 1414 |
return; |
| 1415 |
|
| 1416 |
for (i = 0; i < gsub_list->num_gsubs; i++) { |
| 1417 |
gsub = &(gsub_list->gsubs[i]); |
| 1418 |
|
| 1419 |
if (gsub->script) |
| 1420 |
RELEASE(gsub->script); |
| 1421 |
if (gsub->language) |
| 1422 |
RELEASE(gsub->language); |
| 1423 |
if (gsub->feature) |
| 1424 |
RELEASE(gsub->feature); |
| 1425 |
|
| 1426 |
for (j = 0; j < gsub->num_subtables; j++) { |
| 1427 |
subtab = &(gsub->subtables[j]); |
| 1428 |
switch ((int) subtab->LookupType) { |
| 1429 |
case OTL_GSUB_TYPE_SINGLE: |
| 1430 |
otl_gsub_release_single(subtab); |
| 1431 |
break; |
| 1432 |
case OTL_GSUB_TYPE_ALTERNATE: |
| 1433 |
otl_gsub_release_alternate(subtab); |
| 1434 |
break; |
| 1435 |
case OTL_GSUB_TYPE_LIGATURE: |
| 1436 |
otl_gsub_release_ligature(subtab); |
| 1437 |
break; |
| 1438 |
default: |
| 1439 |
ERROR("???"); |
| 1440 |
break; |
| 1441 |
} |
| 1442 |
} |
| 1443 |
RELEASE(gsub->subtables); |
| 1444 |
} |
| 1445 |
clear_chain(gsub_list); |
| 1446 |
RELEASE(gsub_list); |
| 1447 |
} |
| 1448 |
|
| 1449 |
int |
| 1450 |
otl_gsub_apply (otl_gsub *gsub_list, USHORT *gid) |
| 1451 |
{ |
| 1452 |
int retval = -1; |
| 1453 |
struct otl_gsub_tab *gsub; |
| 1454 |
struct otl_gsub_subtab *subtab; |
| 1455 |
int i, j; |
| 1456 |
|
| 1457 |
if (!gsub_list || !gid) |
| 1458 |
return retval; |
| 1459 |
|
| 1460 |
i = gsub_list->select; |
| 1461 |
if (i < 0 || i >= gsub_list->num_gsubs) { |
| 1462 |
ERROR("GSUB not selected..."); |
| 1463 |
return -1; |
| 1464 |
} |
| 1465 |
gsub = &(gsub_list->gsubs[i]); |
| 1466 |
|
| 1467 |
for (j = 0; |
| 1468 |
retval < 0 && j < gsub->num_subtables; j++) { |
| 1469 |
subtab = &(gsub->subtables[j]); |
| 1470 |
switch ((int) subtab->LookupType){ |
| 1471 |
case OTL_GSUB_TYPE_SINGLE: |
| 1472 |
retval = otl_gsub_apply_single(subtab, gid); |
| 1473 |
break; |
| 1474 |
default: |
| 1475 |
break; |
| 1476 |
} |
| 1477 |
} |
| 1478 |
|
| 1479 |
return retval; |
| 1480 |
} |
| 1481 |
|
| 1482 |
int |
| 1483 |
otl_gsub_apply_alt (otl_gsub *gsub_list, USHORT alt_idx, USHORT *gid) |
| 1484 |
{ |
| 1485 |
int retval = -1; |
| 1486 |
struct otl_gsub_tab *gsub; |
| 1487 |
struct otl_gsub_subtab *subtab; |
| 1488 |
int i, j; |
| 1489 |
|
| 1490 |
if (!gsub_list || !gid) |
| 1491 |
return retval; |
| 1492 |
|
| 1493 |
i = gsub_list->select; |
| 1494 |
if (i < 0 || i >= gsub_list->num_gsubs) { |
| 1495 |
ERROR("GSUB not selected..."); |
| 1496 |
return -1; |
| 1497 |
} |
| 1498 |
gsub = &(gsub_list->gsubs[i]); |
| 1499 |
|
| 1500 |
for (j = 0; |
| 1501 |
retval < 0 && j < gsub->num_subtables; j++) { |
| 1502 |
subtab = &(gsub->subtables[j]); |
| 1503 |
switch ((int) subtab->LookupType){ |
| 1504 |
case OTL_GSUB_TYPE_ALTERNATE: |
| 1505 |
retval = otl_gsub_apply_alternate(subtab, alt_idx, gid); |
| 1506 |
break; |
| 1507 |
default: |
| 1508 |
break; |
| 1509 |
} |
| 1510 |
} |
| 1511 |
|
| 1512 |
return retval; |
| 1513 |
} |
| 1514 |
|
| 1515 |
int |
| 1516 |
otl_gsub_apply_lig (otl_gsub *gsub_list, |
| 1517 |
USHORT *gid_in, USHORT num_gids, USHORT *gid_out) |
| 1518 |
{ |
| 1519 |
int retval = -1; |
| 1520 |
struct otl_gsub_tab *gsub; |
| 1521 |
struct otl_gsub_subtab *subtab; |
| 1522 |
int i, j; |
| 1523 |
|
| 1524 |
if (!gsub_list || !gid_out) |
| 1525 |
return retval; |
| 1526 |
|
| 1527 |
i = gsub_list->select; |
| 1528 |
if (i < 0 || i >= gsub_list->num_gsubs) { |
| 1529 |
ERROR("GSUB not selected..."); |
| 1530 |
return -1; |
| 1531 |
} |
| 1532 |
gsub = &(gsub_list->gsubs[i]); |
| 1533 |
|
| 1534 |
for (j = 0; |
| 1535 |
retval < 0 && j < gsub->num_subtables; j++) { |
| 1536 |
subtab = &(gsub->subtables[j]); |
| 1537 |
switch ((int) subtab->LookupType){ |
| 1538 |
case OTL_GSUB_TYPE_LIGATURE: |
| 1539 |
retval = otl_gsub_apply_ligature(subtab, |
| 1540 |
gid_in, num_gids, gid_out); |
| 1541 |
break; |
| 1542 |
default: |
| 1543 |
break; |
| 1544 |
} |
| 1545 |
} |
| 1546 |
|
| 1547 |
return retval; |
| 1548 |
} |
| 1549 |
|
| 1550 |
static int |
| 1551 |
gsub_find (otl_gsub *gsub_list, |
| 1552 |
const char *script, |
| 1553 |
const char *language, |
| 1554 |
const char *feature) |
| 1555 |
{ |
| 1556 |
int i; |
| 1557 |
struct otl_gsub_tab *gsub; |
| 1558 |
|
| 1559 |
for (i = 0; i < gsub_list->num_gsubs; i++) { |
| 1560 |
gsub = &(gsub_list->gsubs[i]); |
| 1561 |
if (!strcmp(gsub->script, script) && |
| 1562 |
!strcmp(gsub->language, language) && |
| 1563 |
!strcmp(gsub->feature, feature)) { |
| 1564 |
return i; |
| 1565 |
} |
| 1566 |
} |
| 1567 |
|
| 1568 |
return -1; |
| 1569 |
} |
| 1570 |
|
| 1571 |
int |
| 1572 |
otl_gsub_select (otl_gsub *gsub_list, |
| 1573 |
const char *script, |
| 1574 |
const char *language, |
| 1575 |
const char *feature) |
| 1576 |
{ |
| 1577 |
gsub_list->select = gsub_find(gsub_list, script, language, feature); |
| 1578 |
return gsub_list->select; |
| 1579 |
} |
| 1580 |
|
| 1581 |
int |
| 1582 |
otl_gsub_set_chain (otl_gsub *gsub_list, const char *otl_tags) { |
| 1583 |
struct gsub_entry *prev = NULL; |
| 1584 |
const char *p, *nextptr, *endptr; |
| 1585 |
char script[5], language[5], feature[5]; |
| 1586 |
int idx; |
| 1587 |
|
| 1588 |
clear_chain(gsub_list); |
| 1589 |
|
| 1590 |
endptr = otl_tags + strlen(otl_tags); |
| 1591 |
for (p = otl_tags; p < endptr; p = nextptr) { |
| 1592 |
nextptr = strchr(p, ':'); |
| 1593 |
if (!nextptr) |
| 1594 |
nextptr = endptr; |
| 1595 |
if (scan_otl_tag(p, nextptr, script, language, feature) >= 0) { |
| 1596 |
idx = gsub_find(gsub_list, script, language, feature); |
| 1597 |
if (idx >= 0 && idx <= gsub_list->num_gsubs) { |
| 1598 |
struct gsub_entry *entry; |
| 1599 |
entry = NEW(1, struct gsub_entry); |
| 1600 |
if (!gsub_list->first) |
| 1601 |
gsub_list->first = entry; |
| 1602 |
if (prev) |
| 1603 |
prev->next = entry; |
| 1604 |
entry->index = idx; |
| 1605 |
prev = entry; |
| 1606 |
} |
| 1607 |
} |
| 1608 |
nextptr++; |
| 1609 |
} |
| 1610 |
if (prev) |
| 1611 |
prev->next = NULL; |
| 1612 |
|
| 1613 |
return 0; |
| 1614 |
} |
| 1615 |
|
| 1616 |
int |
| 1617 |
otl_gsub_add_feat_list (otl_gsub *gsub_list, const char *otl_tags, sfnt *sfont) |
| 1618 |
{ |
| 1619 |
const char *p, *nextptr, *endptr; |
| 1620 |
char script[5], language[5], feature[5]; |
| 1621 |
int idx; |
| 1622 |
|
| 1623 |
if (!gsub_list || !otl_tags || !sfont) |
| 1624 |
return -1; |
| 1625 |
|
| 1626 |
clear_chain(gsub_list); |
| 1627 |
endptr = otl_tags + strlen(otl_tags); |
| 1628 |
for (p = otl_tags; p < endptr; p = nextptr) { |
| 1629 |
nextptr = strchr(p, ':'); |
| 1630 |
if (!nextptr) |
| 1631 |
nextptr = endptr; |
| 1632 |
if (scan_otl_tag(p, nextptr, script, language, feature) >= 0) { |
| 1633 |
idx = gsub_find(gsub_list, script, language, feature); |
| 1634 |
if (idx < 0) { |
| 1635 |
otl_gsub_add_feat(gsub_list, script, language, feature, sfont); |
| 1636 |
} |
| 1637 |
} |
| 1638 |
nextptr++; |
| 1639 |
} |
| 1640 |
|
| 1641 |
return 0; |
| 1642 |
} |
| 1643 |
|
| 1644 |
int |
| 1645 |
otl_gsub_apply_chain (otl_gsub *gsub_list, USHORT *gid) { |
| 1646 |
int retval = -1; |
| 1647 |
struct otl_gsub_tab *gsub; |
| 1648 |
struct otl_gsub_subtab *subtab; |
| 1649 |
struct gsub_entry *entry; |
| 1650 |
int i, idx; |
| 1651 |
|
| 1652 |
if (!gsub_list || !gid) |
| 1653 |
return retval; |
| 1654 |
|
| 1655 |
for (entry = gsub_list->first; |
| 1656 |
entry != NULL; entry = entry->next) { |
| 1657 |
idx = entry->index; |
| 1658 |
if (idx < 0 || idx >= gsub_list->num_gsubs) |
| 1659 |
continue; |
| 1660 |
gsub = &(gsub_list->gsubs[idx]); |
| 1661 |
for (i = 0, retval = -1; |
| 1662 |
retval < 0 && i < gsub->num_subtables; i++) { |
| 1663 |
subtab = &(gsub->subtables[i]); |
| 1664 |
switch ((int) subtab->LookupType){ |
| 1665 |
case OTL_GSUB_TYPE_SINGLE: |
| 1666 |
retval = otl_gsub_apply_single(subtab, gid); |
| 1667 |
break; |
| 1668 |
default: |
| 1669 |
break; |
| 1670 |
} |
| 1671 |
} |
| 1672 |
} |
| 1673 |
|
| 1674 |
return retval; |
| 1675 |
} |
| 1676 |
|
| 1677 |
#if 1 |
| 1678 |
#include "unicode.h" |
| 1679 |
|
| 1680 |
#ifndef is_used_char2 |
| 1681 |
#define is_used_char2(b,c) (((b)[(c)/8]) & (1 << (7-((c)%8)))) |
| 1682 |
#endif |
| 1683 |
|
| 1684 |
static int |
| 1685 |
add_glyph_if_valid (CMap *cmap, char *used_chars, |
| 1686 |
int32_t *map_base, int32_t *map_sub, USHORT num_glyphs, |
| 1687 |
uint16_t *GIDToCIDMap, USHORT gid, USHORT gid_sub) |
| 1688 |
{ |
| 1689 |
int count = 0; |
| 1690 |
unsigned char src[2], dst[4]; |
| 1691 |
unsigned char *p = dst, *endptr = dst + 4; |
| 1692 |
size_t len; |
| 1693 |
uint16_t cid_sub; |
| 1694 |
|
| 1695 |
if (gid_sub >= num_glyphs || gid >= num_glyphs) |
| 1696 |
return 0; |
| 1697 |
|
| 1698 |
cid_sub = GIDToCIDMap[gid_sub]; |
| 1699 |
if (is_used_char2(used_chars, cid_sub)) { |
| 1700 |
int32_t ch = map_base[gid]; |
| 1701 |
if (UC_is_valid(ch)) { |
| 1702 |
src[0] = (cid_sub >> 8) & 0xff; |
| 1703 |
src[1] = cid_sub & 0xff; |
| 1704 |
len = UC_UTF16BE_encode_char(ch, &p, endptr); |
| 1705 |
CMap_add_bfchar(cmap, src, 2, dst, len); |
| 1706 |
used_chars[cid_sub / 8] &= ~(1 << (7 - (cid_sub % 8))); |
| 1707 |
count = 1; |
| 1708 |
} else { |
| 1709 |
ch = map_sub[gid]; |
| 1710 |
if (UC_is_valid(ch)) { |
| 1711 |
src[0] = (cid_sub >> 8) & 0xff; |
| 1712 |
src[1] = cid_sub & 0xff; |
| 1713 |
len = UC_UTF16BE_encode_char(ch, &p, endptr); |
| 1714 |
CMap_add_bfchar(cmap, src, 2, dst, len); |
| 1715 |
used_chars[cid_sub / 8] &= ~(1 << (7 - (cid_sub % 8))); |
| 1716 |
count = 1; |
| 1717 |
} |
| 1718 |
} |
| 1719 |
} |
| 1720 |
return count; |
| 1721 |
} |
| 1722 |
|
| 1723 |
static int |
| 1724 |
add_ToUnicode_single (CMap *cmap, char *used_chars, |
| 1725 |
struct otl_gsub_subtab *subtab, |
| 1726 |
int32_t *map_base, int32_t *map_sub, USHORT num_glyphs, |
| 1727 |
uint16_t *GIDToCIDMap) |
| 1728 |
{ |
| 1729 |
int count = 0; |
| 1730 |
USHORT i, idx, gid; |
| 1731 |
USHORT gid_sub; |
| 1732 |
|
| 1733 |
ASSERT(subtab); |
| 1734 |
|
| 1735 |
if (subtab->SubstFormat == 1) { |
| 1736 |
struct otl_gsub_single1 *data; |
| 1737 |
struct clt_coverage *cov; |
| 1738 |
|
| 1739 |
data = (subtab->table).single1; |
| 1740 |
cov = &data->coverage; |
| 1741 |
switch (cov->format) { |
| 1742 |
case 1: /* list */ |
| 1743 |
for (idx = 0; idx < cov->count; idx++) { |
| 1744 |
gid = cov->list[idx]; |
| 1745 |
gid_sub = gid + data->DeltaGlyphID; |
| 1746 |
count += add_glyph_if_valid(cmap, used_chars, |
| 1747 |
map_base, map_sub, num_glyphs, |
| 1748 |
GIDToCIDMap, gid, gid_sub); |
| 1749 |
} |
| 1750 |
break; |
| 1751 |
case 2: /* range */ |
| 1752 |
for (i = 0; i < cov->count; i++) { |
| 1753 |
for (gid = cov->range[i].Start; |
| 1754 |
gid <= cov->range[i].End && gid < num_glyphs; gid++) { |
| 1755 |
idx = cov->range[i].StartCoverageIndex + gid - cov->range[i].Start; |
| 1756 |
gid_sub = gid + data->DeltaGlyphID; |
| 1757 |
count += add_glyph_if_valid(cmap, used_chars, |
| 1758 |
map_base, map_sub, num_glyphs, |
| 1759 |
GIDToCIDMap, gid, gid_sub); |
| 1760 |
} |
| 1761 |
} |
| 1762 |
break; |
| 1763 |
} |
| 1764 |
} else if (subtab->SubstFormat == 2) { |
| 1765 |
struct otl_gsub_single2 *data; |
| 1766 |
struct clt_coverage *cov; |
| 1767 |
|
| 1768 |
data = (subtab->table).single2; |
| 1769 |
cov = &data->coverage; |
| 1770 |
switch (cov->format) { |
| 1771 |
case 1: /* list */ |
| 1772 |
for (idx = 0; idx < cov->count; idx++) { |
| 1773 |
gid = cov->list[idx]; |
| 1774 |
if (idx >= 0 && idx < data->GlyphCount) { |
| 1775 |
gid_sub = (data->Substitute)[idx]; |
| 1776 |
count += add_glyph_if_valid(cmap, used_chars, |
| 1777 |
map_base, map_sub, num_glyphs, |
| 1778 |
GIDToCIDMap, gid, gid_sub); |
| 1779 |
} |
| 1780 |
} |
| 1781 |
break; |
| 1782 |
case 2: /* range */ |
| 1783 |
for (i = 0; i < cov->count; i++) { |
| 1784 |
for (gid = cov->range[i].Start; |
| 1785 |
gid <= cov->range[i].End && gid < num_glyphs; gid++) { |
| 1786 |
idx = cov->range[i].StartCoverageIndex + gid - cov->range[i].Start; |
| 1787 |
if (idx >= 0 && idx < data->GlyphCount) { |
| 1788 |
gid_sub = (data->Substitute)[idx]; |
| 1789 |
count += add_glyph_if_valid(cmap, used_chars, |
| 1790 |
map_base, map_sub, num_glyphs, |
| 1791 |
GIDToCIDMap, gid, gid_sub); |
| 1792 |
} |
| 1793 |
} |
| 1794 |
} |
| 1795 |
break; |
| 1796 |
} |
| 1797 |
} |
| 1798 |
|
| 1799 |
return count; |
| 1800 |
} |
| 1801 |
|
| 1802 |
static int32_t |
| 1803 |
add_alternate1_inverse_map (CMap *cmap, char *used_chars, |
| 1804 |
int32_t *map_base, int32_t *map_sub, USHORT num_glyphs, |
| 1805 |
uint16_t *GIDToCIDMap, USHORT gid, int idx, |
| 1806 |
struct otl_gsub_alternate1 *data) |
| 1807 |
{ |
| 1808 |
int32_t count = 0; |
| 1809 |
|
| 1810 |
if (idx >= 0 && idx < data->AlternateSetCount) { |
| 1811 |
struct otl_gsub_altset *altset; |
| 1812 |
USHORT i; |
| 1813 |
|
| 1814 |
altset = &(data->AlternateSet[idx]); |
| 1815 |
if (altset->GlyphCount == 0) |
| 1816 |
return count; |
| 1817 |
for (i = 0; i < altset->GlyphCount; i++) { |
| 1818 |
USHORT gid_alt = altset->Alternate[i]; |
| 1819 |
count += add_glyph_if_valid(cmap, used_chars, |
| 1820 |
map_base, map_sub, num_glyphs, |
| 1821 |
GIDToCIDMap, gid, gid_alt); |
| 1822 |
} |
| 1823 |
} |
| 1824 |
return count; |
| 1825 |
} |
| 1826 |
|
| 1827 |
static int32_t |
| 1828 |
add_ToUnicode_alternate (CMap *cmap, char *used_chars, |
| 1829 |
struct otl_gsub_subtab *subtab, |
| 1830 |
int32_t *map_base, int32_t *map_sub, USHORT num_glyphs, |
| 1831 |
uint16_t *GIDToCIDMap) |
| 1832 |
{ |
| 1833 |
int32_t count = 0; |
| 1834 |
USHORT i, gid, idx; |
| 1835 |
|
| 1836 |
ASSERT(subtab); |
| 1837 |
|
| 1838 |
if (subtab->SubstFormat == 1) { |
| 1839 |
struct otl_gsub_alternate1 *data; |
| 1840 |
struct clt_coverage *cov; |
| 1841 |
data = subtab->table.alternate1; |
| 1842 |
cov = &data->coverage; |
| 1843 |
switch (cov->format) { |
| 1844 |
case 1: /* list */ |
| 1845 |
for (idx = 0; idx < cov->count; idx++) { |
| 1846 |
gid = cov->list[idx]; |
| 1847 |
if (gid < num_glyphs) { |
| 1848 |
count += add_alternate1_inverse_map(cmap, used_chars, |
| 1849 |
map_base, map_sub, num_glyphs, |
| 1850 |
GIDToCIDMap, gid, idx, data); |
| 1851 |
} |
| 1852 |
} |
| 1853 |
break; |
| 1854 |
case 2: /* range */ |
| 1855 |
for (i = 0; i < cov->count; i++) { |
| 1856 |
for (gid = cov->range[i].Start; |
| 1857 |
gid <= cov->range[i].End && gid < num_glyphs; gid++) { |
| 1858 |
idx = cov->range[i].StartCoverageIndex + gid - cov->range[i].Start; |
| 1859 |
count += add_alternate1_inverse_map(cmap, used_chars, |
| 1860 |
map_base, map_sub, num_glyphs, |
| 1861 |
GIDToCIDMap, gid, idx, data); |
| 1862 |
} |
| 1863 |
} |
| 1864 |
break; |
| 1865 |
} |
| 1866 |
} |
| 1867 |
return count; |
| 1868 |
} |
| 1869 |
|
| 1870 |
static int32_t |
| 1871 |
add_ligature1_inverse_map (CMap *cmap, char *used_chars, |
| 1872 |
int32_t *map_base, int32_t *map_sub, USHORT num_glyphs, |
| 1873 |
uint16_t *GIDToCIDMap, USHORT gid_1, int idx, |
| 1874 |
struct otl_gsub_ligature1 *data) |
| 1875 |
{ |
| 1876 |
int32_t count = 0; |
| 1877 |
|
| 1878 |
if (idx >= 0 && idx < data->LigSetCount) { |
| 1879 |
struct otl_gsub_ligset *ligset; |
| 1880 |
USHORT i, j; |
| 1881 |
|
| 1882 |
ligset = &(data->LigatureSet[idx]); |
| 1883 |
for (j = 0; j < ligset->LigatureCount; j++) { |
| 1884 |
USHORT gid_sub = ligset->Ligature[j].LigGlyph; |
| 1885 |
if (gid_sub < num_glyphs) { |
| 1886 |
uint16_t cid = GIDToCIDMap[gid_sub]; |
| 1887 |
if (is_used_char2(used_chars, cid)) { |
| 1888 |
int32_t ch, *ucv; |
| 1889 |
USHORT comp_count = ligset->Ligature[j].CompCount; |
| 1890 |
int fail_count = 0; |
| 1891 |
|
| 1892 |
ucv = NEW(comp_count, int32_t); |
| 1893 |
ch = UC_is_valid(map_base[gid_1]) ? map_base[gid_1] : map_sub[gid_1]; |
| 1894 |
ucv[0] = ch; |
| 1895 |
fail_count += UC_is_valid(ch) ? 0 : 1; |
| 1896 |
for (i = 0; i < ligset->Ligature[j].CompCount - 1; i++) { |
| 1897 |
USHORT gid = ligset->Ligature[j].Component[i]; |
| 1898 |
if (gid < num_glyphs) { |
| 1899 |
ch = UC_is_valid(map_base[gid]) ? map_base[gid] : map_sub[gid]; |
| 1900 |
ucv[i+1] = ch; |
| 1901 |
fail_count += UC_is_valid(ch) ? 0 : 1; |
| 1902 |
} else { |
| 1903 |
fail_count += 1; |
| 1904 |
} |
| 1905 |
} |
| 1906 |
if (fail_count == 0) { |
| 1907 |
unsigned char src[2], *dst; |
| 1908 |
unsigned char *p, *endptr; |
| 1909 |
size_t len = 0; |
| 1910 |
|
| 1911 |
src[0] = (cid >> 8) & 0xff; |
| 1912 |
src[1] = cid & 0xff; |
| 1913 |
dst = NEW(comp_count*4, unsigned char); |
| 1914 |
p = dst; |
| 1915 |
endptr = dst + comp_count * 4; |
| 1916 |
for (i = 0; i < comp_count; i++) { |
| 1917 |
len += UC_UTF16BE_encode_char(ucv[i], &p, endptr); |
| 1918 |
} |
| 1919 |
CMap_add_bfchar(cmap, src, 2, dst, len); |
| 1920 |
used_chars[cid / 8] &= ~(1 << (7 - (cid % 8))); |
| 1921 |
count++; |
| 1922 |
RELEASE(dst); |
| 1923 |
} |
| 1924 |
RELEASE(ucv); |
| 1925 |
} |
| 1926 |
} |
| 1927 |
} |
| 1928 |
} |
| 1929 |
|
| 1930 |
return count; |
| 1931 |
} |
| 1932 |
|
| 1933 |
static int32_t |
| 1934 |
add_ToUnicode_ligature (CMap *cmap, char *used_chars, |
| 1935 |
struct otl_gsub_subtab *subtab, |
| 1936 |
int32_t *map_base, int32_t *map_sub, USHORT num_glyphs, |
| 1937 |
uint16_t *GIDToCIDMap) |
| 1938 |
{ |
| 1939 |
int32_t count = 0; |
| 1940 |
USHORT i, idx, gid; |
| 1941 |
|
| 1942 |
ASSERT(subtab); |
| 1943 |
|
| 1944 |
if (subtab->SubstFormat == 1) { |
| 1945 |
struct otl_gsub_ligature1 *data; |
| 1946 |
struct clt_coverage *cov; |
| 1947 |
|
| 1948 |
data = subtab->table.ligature1; |
| 1949 |
cov = &data->coverage; |
| 1950 |
switch (cov->format) { |
| 1951 |
case 1: /* list */ |
| 1952 |
for (idx = 0; idx < cov->count; idx++) { |
| 1953 |
gid = cov->list[idx]; |
| 1954 |
if (gid < num_glyphs) { |
| 1955 |
count += add_ligature1_inverse_map(cmap, used_chars, |
| 1956 |
map_base, map_sub, num_glyphs, |
| 1957 |
GIDToCIDMap, gid, idx, data); |
| 1958 |
} |
| 1959 |
} |
| 1960 |
break; |
| 1961 |
case 2: /* range */ |
| 1962 |
for (i = 0; i < cov->count; i++) { |
| 1963 |
for (gid = cov->range[i].Start; |
| 1964 |
gid <= cov->range[i].End && gid < num_glyphs; gid++) { |
| 1965 |
idx = cov->range[i].StartCoverageIndex + gid - cov->range[i].Start; |
| 1966 |
if (gid < num_glyphs) { |
| 1967 |
count += add_ligature1_inverse_map(cmap, used_chars, |
| 1968 |
map_base, map_sub, num_glyphs, |
| 1969 |
GIDToCIDMap, gid, idx, data); |
| 1970 |
} |
| 1971 |
} |
| 1972 |
} |
| 1973 |
break; |
| 1974 |
} |
| 1975 |
} |
| 1976 |
|
| 1977 |
return 0; |
| 1978 |
} |
| 1979 |
|
| 1980 |
int |
| 1981 |
otl_gsub_add_ToUnicode (CMap *cmap, char *used_chars, |
| 1982 |
int32_t *map_base, int32_t *map_sub, USHORT num_glyphs, |
| 1983 |
uint16_t *GIDToCIDMap, sfnt *sfont) |
| 1984 |
{ |
| 1985 |
int count = 0; |
| 1986 |
otl_gsub *gsub_list; |
| 1987 |
struct otl_gsub_tab *gsub; |
| 1988 |
struct otl_gsub_subtab *subtab; |
| 1989 |
int i, j; |
| 1990 |
|
| 1991 |
gsub_list = otl_gsub_new(); |
| 1992 |
otl_gsub_add_feat(gsub_list, "*", "*", "*", sfont); |
| 1993 |
|
| 1994 |
for (i = 0; i < gsub_list->num_gsubs; i++) { |
| 1995 |
gsub = &(gsub_list->gsubs[i]); |
| 1996 |
for (j = 0; j < gsub->num_subtables; j++) { |
| 1997 |
subtab = &(gsub->subtables[j]); |
| 1998 |
switch ((int) subtab->LookupType){ |
| 1999 |
case OTL_GSUB_TYPE_SINGLE: |
| 2000 |
count += add_ToUnicode_single(cmap, used_chars, subtab, |
| 2001 |
map_base, map_sub, num_glyphs, |
| 2002 |
GIDToCIDMap); |
| 2003 |
break; |
| 2004 |
case OTL_GSUB_TYPE_ALTERNATE: |
| 2005 |
count += add_ToUnicode_alternate(cmap, used_chars, subtab, |
| 2006 |
map_base, map_sub, num_glyphs, |
| 2007 |
GIDToCIDMap); |
| 2008 |
break; |
| 2009 |
case OTL_GSUB_TYPE_LIGATURE: |
| 2010 |
count += add_ToUnicode_ligature(cmap, used_chars, subtab, |
| 2011 |
map_base, map_sub, num_glyphs, |
| 2012 |
GIDToCIDMap); |
| 2013 |
break; |
| 2014 |
} |
| 2015 |
} |
| 2016 |
} |
| 2017 |
otl_gsub_release(gsub_list); |
| 2018 |
|
| 2019 |
return count; |
| 2020 |
} |
| 2021 |
#endif |