-
Notifications
You must be signed in to change notification settings - Fork 444
/
ft2vert.c
562 lines (496 loc) · 16.6 KB
/
ft2vert.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
/*
* "ft2vert.c"
*
* Converter to vertical glyph ID by handling GSUB vrt2/vert feature
* requires FreeType-2.1.10 or latter
*
* (C) 2005 Nobuyuki TSUCHIMURA
*
* using such Lookup
* ScriptTag == 'kana'
* DefaultLangSys or LangSysTag == 'JAN '
* FeatureTag == 'vrt2' or 'vert'
*
* [reference]
* http://partners.adobe.com/public/developer/opentype/index_table_formats1.html
* http://partners.adobe.com/public/developer/opentype/index_table_formats.html
* http://partners.adobe.com/public/developer/opentype/index_tag9.html#vrt2
*/
//#include "xdvi-config.h"
//#include "xdvi.h"
//#ifdef USE_ZEIT
//#include <windows.h>
#include <ft2build.h>
#include FT_FREETYPE_H
#include FT_OPENTYPE_VALIDATE_H
#include <stdio.h>
#include <stdlib.h>
#include "ft2vert.h"
const struct ivs_otft_desc {
int baseChar;
int otft_index;
} ivs_otft[] = {
#define OTFT_DEF_BEGIN(index) /**/
#define OTFT_DEF(baseChar, otft_index) { baseChar, otft_index },
#define OTFT_DEF_END(count) /**/
#include "ivs_otft.h"
#undef OTFT_DEF_BEGIN
#undef OTFT_DEF
#undef OTFT_DEF_END
};
const int ivs_otft_index[] = {
#define OTFT_DEF_BEGIN(index) index,
#define OTFT_DEF(baseChar, otft_index) /**/
#define OTFT_DEF_END(count) /**/
#include "ivs_otft.h"
#undef OTFT_DEF_BEGIN
#undef OTFT_DEF
#undef OTFT_DEF_END
};
const int ivs_otft_count[] = {
#define OTFT_DEF_BEGIN(index) /**/
#define OTFT_DEF(baseChar, otft_index) /**/
#define OTFT_DEF_END(count) count,
#include "ivs_otft.h"
#undef OTFT_DEF_BEGIN
#undef OTFT_DEF
#undef OTFT_DEF_END
};
#define TAG_KANA FT_MAKE_TAG('k', 'a', 'n', 'a')
#define TAG_HANI FT_MAKE_TAG('h', 'a', 'n', 'i')
#define TAG_JAN FT_MAKE_TAG('J', 'A', 'N', ' ')
#define TAG_VERT FT_MAKE_TAG('v', 'e', 'r', 't')
#define TAG_VRT2 FT_MAKE_TAG('v', 'r', 't', '2')
#define TAG_JP78 FT_MAKE_TAG('j', 'p', '7', '8')
#define TAG_JP90 FT_MAKE_TAG('j', 'p', '9', '0')
#define TAG_JP04 FT_MAKE_TAG('j', 'p', '0', '4')
#define VERT_LOOKUP_INDEX 0
#define JP78_LOOKUP_INDEX 1
#define JP90_LOOKUP_INDEX 2
#define JP04_LOOKUP_INDEX 3
#define MALLOC(ptr, size) ptr = malloc(sizeof((ptr)[0]) * (size))
#define BYTE2(p) ((p) += 2, (int)(p)[-2] << 8 | (p)[-1])
#define BYTE4(p) ((p) += 4, (int)(p)[-4] << 24 | (int)(p)[-3] << 16 | \
(int)(p)[-2] << 8 | (p)[-1])
struct ft2vert_st {
struct Lookup_st {
int SubTableCount;
struct SubTable_st {
struct SingleSubst_st {
FT_UInt SubstFormat;
FT_UInt DeltaGlyphID; /* SubstFormat == 1 */
int GlyphCount; /* SubstFormat == 2 */
FT_UInt *Substitute; /* SubstFormat == 2 */
} SingleSubst;
struct Coverage_st {
FT_UInt CoverageFormat;
int GlyphCount; /* CoverageFormat == 1 */
FT_UInt *GlyphArray; /* CoverageFormat == 1 */
int RangeCount; /* CoverageFormat == 2 */
struct RangeRecord_st
*RangeRecord; /* CoverageFormat == 2 */
} Coverage;
} *SubTable;
} Lookup[4];
FT_Bytes GSUB_table;
FT_Bytes kanaFeature, haniFeature;
FT_Bytes vertLookup, vrt2Lookup;
FT_Bytes jp78Lookup, jp90Lookup, jp04Lookup;
FT_UInt32* variantSelectors;
};
struct RangeRecord_st {
FT_UInt Start;
FT_UInt End;
};
int isInIndex(FT_Bytes s, int index) {
int i, count;
if (s == NULL) return 0;
count = BYTE2(s);
for (i = 0; i < count; i++) {
if (index == BYTE2(s)) return 1;
}
return 0;
}
/********** Debug ***************/
#ifdef DEBUG
static FT_Bytes gsub_top;
void print_offset(char *message, const FT_Bytes ptr) {
printf("%s offset = %x\n", message, ptr - gsub_top);
}
char *tag_to_string(FT_Tag tag) {
static char str[5];
str[0] = tag >> 24;
str[1] = tag >> 16;
str[2] = tag >> 8;
str[3] = tag;
return str;
}
void hex_dump(const FT_Bytes top) {
int i, j;
FT_Bytes s = top;
for (j = 0; j < 100; j++) {
printf("%04x : ", j*8);
for (i = 0; i < 8; i++) {
printf("%02x ", s[i+j*8]);
}
printf("\n");
}
}
#endif /* DEBUG */
/********** Lookup part ***************/
void scan_Coverage(struct ft2vert_st *ret, const FT_Bytes top, const int l) {
int i;
FT_Bytes s = top;
struct Coverage_st *t;
t = &ret->Lookup[l].SubTable[ret->Lookup[l].SubTableCount].Coverage;
t->CoverageFormat = BYTE2(s);
switch (t->CoverageFormat) {
case 1:
t->GlyphCount = BYTE2(s);
MALLOC(t->GlyphArray, t->GlyphCount);
memset(t->GlyphArray, 0, sizeof(t->GlyphArray[0]) * t->GlyphCount);
for (i = 0; i < t->GlyphCount; i++) {
t->GlyphArray[i] = BYTE2(s);
}
break;
case 2:
t->RangeCount = BYTE2(s);
MALLOC(t->RangeRecord, t->RangeCount);
memset(t->RangeRecord, 0, sizeof(t->RangeRecord[0]) * t->RangeCount);
for (i = 0; i < t->RangeCount; i++) {
t->RangeRecord[i].Start = BYTE2(s);
t->RangeRecord[i].End = BYTE2(s);
s += 2; /* drop StartCoverageIndex */
}
break;
default:
#ifdef _DEBUG
fprintf(stderr, "scan_Coverage: unknown CoverageFormat (%d).",
t->CoverageFormat);
#endif
return;
}
ret->Lookup[l].SubTableCount++;
}
void scan_SubTable(struct ft2vert_st *ret, const FT_Bytes top, const int l) {
int i;
FT_Bytes s = top;
FT_Offset Coverage;
struct SingleSubst_st *t;
t = &ret->Lookup[l].SubTable[ret->Lookup[l].SubTableCount].SingleSubst;
t->SubstFormat = BYTE2(s);
Coverage = BYTE2(s);
scan_Coverage(ret, top + Coverage, l);
switch (t->SubstFormat) {
case 1: /* SingleSubstFormat1 */
t->DeltaGlyphID = BYTE2(s);
break;
case 2: /* SingleSubstFormat2 */
t->GlyphCount = BYTE2(s);
MALLOC(t->Substitute, t->GlyphCount);
memset(t->Substitute, 0, sizeof(t->Substitute[0]) * t->GlyphCount);
for (i = 0; i < t->GlyphCount; i++) {
t->Substitute[i] = BYTE2(s);
}
break;
#ifdef _DEBUG
default:
fprintf(stderr, "scan_SubTable: unknown SubstFormat (%d).",
t->SubstFormat);
#endif
}
}
void scan_Lookup(struct ft2vert_st *ret, const FT_Bytes top, const int l) {
int i;
FT_Bytes s = top;
FT_UShort LookupType;
FT_UShort LookupFlag;
FT_UShort SubTableCount;
FT_UShort SubTable;
LookupType = BYTE2(s);
LookupFlag = BYTE2(s);
SubTableCount = BYTE2(s);
SubTable = BYTE2(s);
MALLOC(ret->Lookup[l].SubTable, SubTableCount);
memset(ret->Lookup[l].SubTable, 0, sizeof(ret->Lookup[l].SubTable[0]) * SubTableCount);
for (i = 0; i < SubTableCount; i++) {
scan_SubTable(ret, top + SubTable, l);
}
if (ret->Lookup[l].SubTableCount != SubTableCount) {
#ifdef _DEBUG
fprintf(stderr, "warning (scan_Lookup): "
"SubTableCount (=%d) is not expected (=%d).\n",
ret->Lookup[l].SubTableCount, SubTableCount);
#endif
}
}
void scan_LookupList(struct ft2vert_st *ret, const FT_Bytes top) {
int i;
FT_Bytes s = top;
int LookupCount;
LookupCount = BYTE2(s);
for (i = 0; i < LookupCount; i++) {
FT_Bytes t = top + BYTE2(s);
if (isInIndex(ret->vertLookup, i)) {
scan_Lookup(ret, t, VERT_LOOKUP_INDEX);
} else if (isInIndex(ret->jp78Lookup, i)) {
scan_Lookup(ret, t, JP78_LOOKUP_INDEX);
} else if (isInIndex(ret->jp90Lookup, i)) {
scan_Lookup(ret, t, JP90_LOOKUP_INDEX);
} else if (isInIndex(ret->jp04Lookup, i)) {
scan_Lookup(ret, t, JP04_LOOKUP_INDEX);
}
}
}
/********** Feature part ****************/
void scan_FeatureList(struct ft2vert_st *ret, const FT_Bytes top) {
int i;
FT_Bytes s = top;
int FeatureCount;
FeatureCount = BYTE2(s);
for (i = 0; i < FeatureCount; i++) {
FT_Tag FeatureTag = BYTE4(s);
FT_Offset Feature = BYTE2(s);
if (isInIndex(ret->kanaFeature, i)) {
switch (FeatureTag) {
case TAG_VERT:
ret->vertLookup = top + Feature + 2;
break;
case TAG_VRT2:
ret->vrt2Lookup = top + Feature + 2;
break;
}
} else if (isInIndex(ret->haniFeature, i)) {
switch (FeatureTag) {
case TAG_JP78:
ret->jp78Lookup = top + Feature + 2;
break;
case TAG_JP90:
ret->jp90Lookup = top + Feature + 2;
break;
case TAG_JP04:
ret->jp04Lookup = top + Feature + 2;
break;
}
}
}
}
/********** Script part ****************/
void scan_LangSys(struct ft2vert_st *ret, const FT_Bytes top, const FT_Tag ScriptTag) {
if (ScriptTag == TAG_KANA && ret->kanaFeature == NULL) ret->kanaFeature = top + 4;
if (ScriptTag == TAG_HANI && ret->haniFeature == NULL) ret->haniFeature = top + 4;
}
void scan_Script(struct ft2vert_st *ret, const FT_Bytes top, const FT_Tag ScriptTag) {
int i;
FT_Bytes s = top;
FT_Offset DefaultLangSys;
int LangSysCount;
DefaultLangSys = BYTE2(s);
if (DefaultLangSys != 0) {
scan_LangSys(ret, top + DefaultLangSys, ScriptTag);
}
LangSysCount = BYTE2(s);
for (i = 0; i < LangSysCount; i++) {
FT_Tag LangSysTag = BYTE4(s);
FT_Bytes t = top + BYTE2(s);
if (LangSysTag == TAG_JAN) {
scan_LangSys(ret, t, ScriptTag);
}
}
}
void scan_ScriptList(struct ft2vert_st *ret, const FT_Bytes top) {
int i;
FT_Bytes s = top;
int ScriptCount;
ScriptCount = BYTE2(s);
for (i = 0; i < ScriptCount; i++) {
FT_Tag ScriptTag = BYTE4(s);
FT_Bytes t = top + BYTE2(s);
if (ScriptTag == TAG_KANA || ScriptTag == TAG_HANI) {
scan_Script(ret, t, ScriptTag);
}
}
}
/********** header part *****************/
void scan_GSUB_Header(struct ft2vert_st *ret, const FT_Bytes top) {
FT_Bytes s = top;
FT_Fixed Version;
FT_Offset ScriptList;
FT_Offset FeatureList;
FT_Offset LookupList;
#ifdef DEBUG
gsub_top = top;
#endif /* DEBUG */
Version = BYTE4(s);
ScriptList = BYTE2(s);
FeatureList = BYTE2(s);
LookupList = BYTE2(s);
if (Version != 0x00010000) {
#ifdef _DEBUG
fprintf(stderr, "warning: GSUB Version (=%.1f) is not 1.0\n",
(double)Version / 0x10000);
#endif
}
scan_ScriptList(ret, top + ScriptList);
scan_FeatureList(ret, top + FeatureList);
/* vrt2 has higher priority over vert */
if (ret->vrt2Lookup != NULL) ret->vertLookup = ret->vrt2Lookup;
scan_LookupList(ret, top + LookupList);
}
struct ft2vert_st *ft2vert_init(FT_Face face) {
struct ft2vert_st *ret;
int ft_error, i;
FT_Bytes base = NULL;
FT_Bytes gdef = NULL;
FT_Bytes gpos = NULL;
FT_Bytes jstf = NULL;
MALLOC(ret, 1);
memset(ret, 0, sizeof(ret[0]));
for (i = 0; i < sizeof(ret->Lookup) / sizeof(ret->Lookup[0]); i++) {
ret->Lookup[i].SubTableCount = 0;
}
ret->vertLookup = NULL;
ret->vrt2Lookup = NULL;
ret->jp78Lookup = NULL;
ret->jp90Lookup = NULL;
ret->jp04Lookup = NULL;
ret->kanaFeature = NULL;
ret->haniFeature = NULL;
ret->GSUB_table = NULL;
ret->variantSelectors = FT_Face_GetVariantSelectors(face);
ft_error = FT_OpenType_Validate(
face, FT_VALIDATE_GSUB,
&base, &gdef, &gpos, &ret->GSUB_table, &jstf);
if (ft_error == FT_Err_Unimplemented_Feature) {
#ifdef _DEBUG
fprintf(stderr, "warning: FT_OpenType_Validate is disabled. "
"Replace FreeType2 with otvalid-enabled version.\n");
#endif
return ret;
} else if (ft_error != 0 || ret->GSUB_table == 0) {
#ifdef _DEBUG
fprintf(stderr, "warning: %s has no GSUB table.\n",
face->family_name);
#endif
return ret;
}
scan_GSUB_Header(ret, ret->GSUB_table);
if (ret->Lookup[VERT_LOOKUP_INDEX].SubTableCount == 0) {
#ifdef _DEBUG
fprintf(stderr, "warning: %s has no vrt2/vert feature.\n",
face->family_name);
#endif
}
// free((void*)GSUB_table);
return ret;
}
void ft2vert_final(FT_Face face, struct ft2vert_st *vert){
int i, j;
for (i = 0; i < sizeof(vert->Lookup) / sizeof(vert->Lookup[0]); i++) {
for (j = 0; j < vert->Lookup[i].SubTableCount; j++) {
free(vert->Lookup[i].SubTable[j].SingleSubst.Substitute);
free(vert->Lookup[i].SubTable[j].Coverage.GlyphArray);
free(vert->Lookup[i].SubTable[j].Coverage.RangeRecord);
}
free(vert->Lookup[i].SubTable);
}
FT_OpenType_Free(face, vert->GSUB_table);
free(vert);
// FT_Bytes kanaFeature;
// FT_Bytes haniFeature;
// FT_Bytes vertLookup;
// FT_Bytes vrt2Lookup;
// FT_Bytes jp78Lookup;
// FT_Bytes jp90Lookup;
// FT_Bytes jp04Lookup;
// FT_UInt32* variantSelectors;
}
/********** converting part *****************/
static FT_UInt get_vert_nth_gid(struct SubTable_st *t, FT_UInt gid, int n) {
switch (t->SingleSubst.SubstFormat) {
case 1:
return gid + t->SingleSubst.DeltaGlyphID;
case 2:
return t->SingleSubst.Substitute[n];
}
#ifdef _DEBUG
fprintf(stderr, "get_vert_nth_gid: internal error");
#endif
return 0;
}
FT_UInt ft2gsub_get_gid(const struct ft2vert_st *ft2vert, const FT_UInt gid, const int l) {
int i, k;
int j = 0; /* StartCoverageIndex */
for (k = 0; k < ft2vert->Lookup[l].SubTableCount; k++) {
struct SubTable_st *t = &ft2vert->Lookup[l].SubTable[k];
switch (t->Coverage.CoverageFormat) {
case 1:
for (i = 0; i < t->Coverage.GlyphCount; i++) {
if (t->Coverage.GlyphArray[i] == gid) {
return get_vert_nth_gid(t, gid, i);
}
}
break;
case 2:
for (i = 0; i < t->Coverage.RangeCount; i++) {
struct RangeRecord_st *r = &t->Coverage.RangeRecord[i];
if (r->Start <= gid && gid <= r->End) {
return get_vert_nth_gid(t, gid, gid - r->Start + j);
}
j += r->End - r->Start + 1;
}
break;
#ifdef _DEBUG
default:
fprintf(stderr, "ft2vert_get_gid: internal error");
#endif
}
}
return 0;
}
FT_UInt ft2vert_get_gid(const struct ft2vert_st *ft2vert, const FT_UInt gid) {
FT_UInt tmp = ft2gsub_get_gid(ft2vert, gid, VERT_LOOKUP_INDEX);
return tmp ? tmp : gid;
}
int glyphs_comp(const void *_a, const void *_b) {
const struct ivs_otft_desc *a = (const struct ivs_otft_desc *)_a;
const struct ivs_otft_desc *b = (const struct ivs_otft_desc *)_b;
if (a->baseChar < b->baseChar)
return -1;
else if (a->baseChar > b->baseChar)
return 1;
return 0;
}
FT_UInt ft2_subst_uvs(const FT_Face face, const FT_UInt gid, const FT_UInt vsindex, const FT_UInt baseChar)
{
FT_UInt newglyph;
const struct ivs_otft_desc key = { baseChar }, *found;
struct ft2vert_st *ft2vert = (struct ft2vert_st *)face->generic.data;
if (!ft2vert)
return 0;
// 存在するならUVS Subtableから探す
if (ft2vert->variantSelectors)
return FT_Face_GetCharVariantIndex(face, baseChar, 0xE0100 + vsindex);
// GSUBテーブルのOpenType featureによりシミュレートする
if (vsindex >= sizeof ivs_otft_index / sizeof ivs_otft_index[0])
return 0;
found = (const struct ivs_otft_desc *)bsearch(&key, ivs_otft + ivs_otft_index[vsindex], ivs_otft_count[vsindex], sizeof(struct ivs_otft_desc), glyphs_comp);
if (!found)
return 0;
// シミュレートできるfeatureが見つかったので置換を試みる。
newglyph = ft2gsub_get_gid(ft2vert, gid, found->otft_index);
// 置換に成功したらそれを返す
if (newglyph)
return newglyph;
// フォントがGSUBテーブルに置換定義を持っていない。
// 'jp04'を持っているが'jp90'を持っていないときはJIS90フォントとみなし、
// 'jp90'を持っているが'jp04'を持っていないときはJIS2004フォントとみなす。
// JIS90フォントに'jp90'を要求された場合とJIS2004フォント'jp04'を要求された場合は
// デフォルト字形が要求された字形であるとみなしてそのまま返す。
if (ft2vert->jp04Lookup && !ft2vert->jp90Lookup && found->otft_index == JP90_LOOKUP_INDEX
|| ft2vert->jp90Lookup && !ft2vert->jp04Lookup && found->otft_index == JP04_LOOKUP_INDEX)
return gid;
// どちらでもなければフォントは要求された字形を持っていないとみなす。
return 0;
}
//#endif /* USE_ZEIT */