-
Notifications
You must be signed in to change notification settings - Fork 207
/
ttcmap.c
3896 lines (3031 loc) · 107 KB
/
ttcmap.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
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/****************************************************************************
*
* ttcmap.c
*
* TrueType character mapping table (cmap) support (body).
*
* Copyright (C) 2002-2022 by
* David Turner, Robert Wilhelm, and Werner Lemberg.
*
* This file is part of the FreeType project, and may only be used,
* modified, and distributed under the terms of the FreeType project
* license, LICENSE.TXT. By continuing to use, modify, or distribute
* this file you indicate that you have read the license and
* understand and accept it fully.
*
*/
#include <freetype/internal/ftdebug.h>
#include "sferrors.h" /* must come before `ftvalid.h' */
#include <freetype/internal/ftvalid.h>
#include <freetype/internal/ftstream.h>
#include <freetype/internal/services/svpscmap.h>
#include "ttload.h"
#include "ttcmap.h"
#include "ttpost.h"
/**************************************************************************
*
* The macro FT_COMPONENT is used in trace mode. It is an implicit
* parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log
* messages during execution.
*/
#undef FT_COMPONENT
#define FT_COMPONENT ttcmap
#define TT_PEEK_SHORT FT_PEEK_SHORT
#define TT_PEEK_USHORT FT_PEEK_USHORT
#define TT_PEEK_UINT24 FT_PEEK_UOFF3
#define TT_PEEK_LONG FT_PEEK_LONG
#define TT_PEEK_ULONG FT_PEEK_ULONG
#define TT_NEXT_SHORT FT_NEXT_SHORT
#define TT_NEXT_USHORT FT_NEXT_USHORT
#define TT_NEXT_UINT24 FT_NEXT_UOFF3
#define TT_NEXT_LONG FT_NEXT_LONG
#define TT_NEXT_ULONG FT_NEXT_ULONG
/* Too large glyph index return values are caught in `FT_Get_Char_Index' */
/* and `FT_Get_Next_Char' (the latter calls the internal `next' function */
/* again in this case). To mark character code return values as invalid */
/* it is sufficient to set the corresponding glyph index return value to */
/* zero. */
FT_CALLBACK_DEF( FT_Error )
tt_cmap_init( TT_CMap cmap,
FT_Byte* table )
{
cmap->data = table;
return FT_Err_Ok;
}
/*************************************************************************/
/*************************************************************************/
/***** *****/
/***** FORMAT 0 *****/
/***** *****/
/*************************************************************************/
/*************************************************************************/
/**************************************************************************
*
* TABLE OVERVIEW
* --------------
*
* NAME OFFSET TYPE DESCRIPTION
*
* format 0 USHORT must be 0
* length 2 USHORT table length in bytes
* language 4 USHORT Mac language code
* glyph_ids 6 BYTE[256] array of glyph indices
* 262
*/
#ifdef TT_CONFIG_CMAP_FORMAT_0
FT_CALLBACK_DEF( FT_Error )
tt_cmap0_validate( FT_Byte* table,
FT_Validator valid )
{
FT_Byte* p;
FT_UInt length;
if ( table + 2 + 2 > valid->limit )
FT_INVALID_TOO_SHORT;
p = table + 2; /* skip format */
length = TT_NEXT_USHORT( p );
if ( table + length > valid->limit || length < 262 )
FT_INVALID_TOO_SHORT;
/* check glyph indices whenever necessary */
if ( valid->level >= FT_VALIDATE_TIGHT )
{
FT_UInt n, idx;
p = table + 6;
for ( n = 0; n < 256; n++ )
{
idx = *p++;
if ( idx >= TT_VALID_GLYPH_COUNT( valid ) )
FT_INVALID_GLYPH_ID;
}
}
return FT_Err_Ok;
}
FT_CALLBACK_DEF( FT_UInt )
tt_cmap0_char_index( TT_CMap cmap,
FT_UInt32 char_code )
{
FT_Byte* table = cmap->data;
return char_code < 256 ? table[6 + char_code] : 0;
}
FT_CALLBACK_DEF( FT_UInt32 )
tt_cmap0_char_next( TT_CMap cmap,
FT_UInt32 *pchar_code )
{
FT_Byte* table = cmap->data;
FT_UInt32 charcode = *pchar_code;
FT_UInt32 result = 0;
FT_UInt gindex = 0;
table += 6; /* go to glyph IDs */
while ( ++charcode < 256 )
{
gindex = table[charcode];
if ( gindex != 0 )
{
result = charcode;
break;
}
}
*pchar_code = result;
return gindex;
}
FT_CALLBACK_DEF( FT_Error )
tt_cmap0_get_info( TT_CMap cmap,
TT_CMapInfo *cmap_info )
{
FT_Byte* p = cmap->data + 4;
cmap_info->format = 0;
cmap_info->language = (FT_ULong)TT_PEEK_USHORT( p );
return FT_Err_Ok;
}
FT_DEFINE_TT_CMAP(
tt_cmap0_class_rec,
sizeof ( TT_CMapRec ),
(FT_CMap_InitFunc) tt_cmap_init, /* init */
(FT_CMap_DoneFunc) NULL, /* done */
(FT_CMap_CharIndexFunc)tt_cmap0_char_index, /* char_index */
(FT_CMap_CharNextFunc) tt_cmap0_char_next, /* char_next */
(FT_CMap_CharVarIndexFunc) NULL, /* char_var_index */
(FT_CMap_CharVarIsDefaultFunc)NULL, /* char_var_default */
(FT_CMap_VariantListFunc) NULL, /* variant_list */
(FT_CMap_CharVariantListFunc) NULL, /* charvariant_list */
(FT_CMap_VariantCharListFunc) NULL, /* variantchar_list */
0,
(TT_CMap_ValidateFunc)tt_cmap0_validate, /* validate */
(TT_CMap_Info_GetFunc)tt_cmap0_get_info /* get_cmap_info */
)
#endif /* TT_CONFIG_CMAP_FORMAT_0 */
/*************************************************************************/
/*************************************************************************/
/***** *****/
/***** FORMAT 2 *****/
/***** *****/
/***** This is used for certain CJK encodings that encode text in a *****/
/***** mixed 8/16 bits encoding along the following lines. *****/
/***** *****/
/***** * Certain byte values correspond to an 8-bit character code *****/
/***** (typically in the range 0..127 for ASCII compatibility). *****/
/***** *****/
/***** * Certain byte values signal the first byte of a 2-byte *****/
/***** character code (but these values are also valid as the *****/
/***** second byte of a 2-byte character). *****/
/***** *****/
/***** The following charmap lookup and iteration functions all *****/
/***** assume that the value `charcode' fulfills the following. *****/
/***** *****/
/***** - For one-byte characters, `charcode' is simply the *****/
/***** character code. *****/
/***** *****/
/***** - For two-byte characters, `charcode' is the 2-byte *****/
/***** character code in big endian format. More precisely: *****/
/***** *****/
/***** (charcode >> 8) is the first byte value *****/
/***** (charcode & 0xFF) is the second byte value *****/
/***** *****/
/***** Note that not all values of `charcode' are valid according *****/
/***** to these rules, and the function moderately checks the *****/
/***** arguments. *****/
/***** *****/
/*************************************************************************/
/*************************************************************************/
/**************************************************************************
*
* TABLE OVERVIEW
* --------------
*
* NAME OFFSET TYPE DESCRIPTION
*
* format 0 USHORT must be 2
* length 2 USHORT table length in bytes
* language 4 USHORT Mac language code
* keys 6 USHORT[256] sub-header keys
* subs 518 SUBHEAD[NSUBS] sub-headers array
* glyph_ids 518+NSUB*8 USHORT[] glyph ID array
*
* The `keys' table is used to map charcode high bytes to sub-headers.
* The value of `NSUBS' is the number of sub-headers defined in the
* table and is computed by finding the maximum of the `keys' table.
*
* Note that for any `n', `keys[n]' is a byte offset within the `subs'
* table, i.e., it is the corresponding sub-header index multiplied
* by 8.
*
* Each sub-header has the following format.
*
* NAME OFFSET TYPE DESCRIPTION
*
* first 0 USHORT first valid low-byte
* count 2 USHORT number of valid low-bytes
* delta 4 SHORT see below
* offset 6 USHORT see below
*
* A sub-header defines, for each high byte, the range of valid
* low bytes within the charmap. Note that the range defined by `first'
* and `count' must be completely included in the interval [0..255]
* according to the specification.
*
* If a character code is contained within a given sub-header, then
* mapping it to a glyph index is done as follows.
*
* - The value of `offset' is read. This is a _byte_ distance from the
* location of the `offset' field itself into a slice of the
* `glyph_ids' table. Let's call it `slice' (it is a USHORT[], too).
*
* - The value `slice[char.lo - first]' is read. If it is 0, there is
* no glyph for the charcode. Otherwise, the value of `delta' is
* added to it (modulo 65536) to form a new glyph index.
*
* It is up to the validation routine to check that all offsets fall
* within the glyph IDs table (and not within the `subs' table itself or
* outside of the CMap).
*/
#ifdef TT_CONFIG_CMAP_FORMAT_2
FT_CALLBACK_DEF( FT_Error )
tt_cmap2_validate( FT_Byte* table,
FT_Validator valid )
{
FT_Byte* p;
FT_UInt length;
FT_UInt n, max_subs;
FT_Byte* keys; /* keys table */
FT_Byte* subs; /* sub-headers */
FT_Byte* glyph_ids; /* glyph ID array */
if ( table + 2 + 2 > valid->limit )
FT_INVALID_TOO_SHORT;
p = table + 2; /* skip format */
length = TT_NEXT_USHORT( p );
if ( table + length > valid->limit || length < 6 + 512 )
FT_INVALID_TOO_SHORT;
keys = table + 6;
/* parse keys to compute sub-headers count */
p = keys;
max_subs = 0;
for ( n = 0; n < 256; n++ )
{
FT_UInt idx = TT_NEXT_USHORT( p );
/* value must be multiple of 8 */
if ( valid->level >= FT_VALIDATE_PARANOID && ( idx & 7 ) != 0 )
FT_INVALID_DATA;
idx >>= 3;
if ( idx > max_subs )
max_subs = idx;
}
FT_ASSERT( p == table + 518 );
subs = p;
glyph_ids = subs + ( max_subs + 1 ) * 8;
if ( glyph_ids > valid->limit )
FT_INVALID_TOO_SHORT;
/* parse sub-headers */
for ( n = 0; n <= max_subs; n++ )
{
FT_UInt first_code, code_count, offset;
FT_Int delta;
first_code = TT_NEXT_USHORT( p );
code_count = TT_NEXT_USHORT( p );
delta = TT_NEXT_SHORT( p );
offset = TT_NEXT_USHORT( p );
/* many Dynalab fonts have empty sub-headers */
if ( code_count == 0 )
continue;
/* check range within 0..255 */
if ( valid->level >= FT_VALIDATE_PARANOID )
{
if ( first_code >= 256 || code_count > 256 - first_code )
FT_INVALID_DATA;
}
/* check offset */
if ( offset != 0 )
{
FT_Byte* ids;
ids = p - 2 + offset;
if ( ids < glyph_ids || ids + code_count * 2 > table + length )
FT_INVALID_OFFSET;
/* check glyph IDs */
if ( valid->level >= FT_VALIDATE_TIGHT )
{
FT_Byte* limit = p + code_count * 2;
FT_UInt idx;
for ( ; p < limit; )
{
idx = TT_NEXT_USHORT( p );
if ( idx != 0 )
{
idx = (FT_UInt)( (FT_Int)idx + delta ) & 0xFFFFU;
if ( idx >= TT_VALID_GLYPH_COUNT( valid ) )
FT_INVALID_GLYPH_ID;
}
}
}
}
}
return FT_Err_Ok;
}
/* return sub header corresponding to a given character code */
/* NULL on invalid charcode */
static FT_Byte*
tt_cmap2_get_subheader( FT_Byte* table,
FT_UInt32 char_code )
{
FT_Byte* result = NULL;
if ( char_code < 0x10000UL )
{
FT_UInt char_lo = (FT_UInt)( char_code & 0xFF );
FT_UInt char_hi = (FT_UInt)( char_code >> 8 );
FT_Byte* p = table + 6; /* keys table */
FT_Byte* subs = table + 518; /* subheaders table */
FT_Byte* sub;
if ( char_hi == 0 )
{
/* an 8-bit character code -- we use subHeader 0 in this case */
/* to test whether the character code is in the charmap */
/* */
sub = subs; /* jump to first sub-header */
/* check that the sub-header for this byte is 0, which */
/* indicates that it is really a valid one-byte value; */
/* otherwise, return 0 */
/* */
p += char_lo * 2;
if ( TT_PEEK_USHORT( p ) != 0 )
goto Exit;
}
else
{
/* a 16-bit character code */
/* jump to key entry */
p += char_hi * 2;
/* jump to sub-header */
sub = subs + ( FT_PAD_FLOOR( TT_PEEK_USHORT( p ), 8 ) );
/* check that the high byte isn't a valid one-byte value */
if ( sub == subs )
goto Exit;
}
result = sub;
}
Exit:
return result;
}
FT_CALLBACK_DEF( FT_UInt )
tt_cmap2_char_index( TT_CMap cmap,
FT_UInt32 char_code )
{
FT_Byte* table = cmap->data;
FT_UInt result = 0;
FT_Byte* subheader;
subheader = tt_cmap2_get_subheader( table, char_code );
if ( subheader )
{
FT_Byte* p = subheader;
FT_UInt idx = (FT_UInt)( char_code & 0xFF );
FT_UInt start, count;
FT_Int delta;
FT_UInt offset;
start = TT_NEXT_USHORT( p );
count = TT_NEXT_USHORT( p );
delta = TT_NEXT_SHORT ( p );
offset = TT_PEEK_USHORT( p );
idx -= start;
if ( idx < count && offset != 0 )
{
p += offset + 2 * idx;
idx = TT_PEEK_USHORT( p );
if ( idx != 0 )
result = (FT_UInt)( (FT_Int)idx + delta ) & 0xFFFFU;
}
}
return result;
}
FT_CALLBACK_DEF( FT_UInt32 )
tt_cmap2_char_next( TT_CMap cmap,
FT_UInt32 *pcharcode )
{
FT_Byte* table = cmap->data;
FT_UInt gindex = 0;
FT_UInt32 result = 0;
FT_UInt32 charcode = *pcharcode + 1;
FT_Byte* subheader;
while ( charcode < 0x10000UL )
{
subheader = tt_cmap2_get_subheader( table, charcode );
if ( subheader )
{
FT_Byte* p = subheader;
FT_UInt start = TT_NEXT_USHORT( p );
FT_UInt count = TT_NEXT_USHORT( p );
FT_Int delta = TT_NEXT_SHORT ( p );
FT_UInt offset = TT_PEEK_USHORT( p );
FT_UInt char_lo = (FT_UInt)( charcode & 0xFF );
FT_UInt pos, idx;
if ( char_lo >= start + count && charcode <= 0xFF )
{
/* this happens only for a malformed cmap */
charcode = 0x100;
continue;
}
if ( offset == 0 )
{
if ( charcode == 0x100 )
goto Exit; /* this happens only for a malformed cmap */
goto Next_SubHeader;
}
if ( char_lo < start )
{
char_lo = start;
pos = 0;
}
else
pos = (FT_UInt)( char_lo - start );
p += offset + pos * 2;
charcode = FT_PAD_FLOOR( charcode, 256 ) + char_lo;
for ( ; pos < count; pos++, charcode++ )
{
idx = TT_NEXT_USHORT( p );
if ( idx != 0 )
{
gindex = (FT_UInt)( (FT_Int)idx + delta ) & 0xFFFFU;
if ( gindex != 0 )
{
result = charcode;
goto Exit;
}
}
}
/* if unsuccessful, avoid `charcode' leaving */
/* the current 256-character block */
if ( count )
charcode--;
}
/* If `charcode' is <= 0xFF, retry with `charcode + 1'. */
/* Otherwise jump to the next 256-character block and retry. */
Next_SubHeader:
if ( charcode <= 0xFF )
charcode++;
else
charcode = FT_PAD_FLOOR( charcode, 0x100 ) + 0x100;
}
Exit:
*pcharcode = result;
return gindex;
}
FT_CALLBACK_DEF( FT_Error )
tt_cmap2_get_info( TT_CMap cmap,
TT_CMapInfo *cmap_info )
{
FT_Byte* p = cmap->data + 4;
cmap_info->format = 2;
cmap_info->language = (FT_ULong)TT_PEEK_USHORT( p );
return FT_Err_Ok;
}
FT_DEFINE_TT_CMAP(
tt_cmap2_class_rec,
sizeof ( TT_CMapRec ),
(FT_CMap_InitFunc) tt_cmap_init, /* init */
(FT_CMap_DoneFunc) NULL, /* done */
(FT_CMap_CharIndexFunc)tt_cmap2_char_index, /* char_index */
(FT_CMap_CharNextFunc) tt_cmap2_char_next, /* char_next */
(FT_CMap_CharVarIndexFunc) NULL, /* char_var_index */
(FT_CMap_CharVarIsDefaultFunc)NULL, /* char_var_default */
(FT_CMap_VariantListFunc) NULL, /* variant_list */
(FT_CMap_CharVariantListFunc) NULL, /* charvariant_list */
(FT_CMap_VariantCharListFunc) NULL, /* variantchar_list */
2,
(TT_CMap_ValidateFunc)tt_cmap2_validate, /* validate */
(TT_CMap_Info_GetFunc)tt_cmap2_get_info /* get_cmap_info */
)
#endif /* TT_CONFIG_CMAP_FORMAT_2 */
/*************************************************************************/
/*************************************************************************/
/***** *****/
/***** FORMAT 4 *****/
/***** *****/
/*************************************************************************/
/*************************************************************************/
/**************************************************************************
*
* TABLE OVERVIEW
* --------------
*
* NAME OFFSET TYPE DESCRIPTION
*
* format 0 USHORT must be 4
* length 2 USHORT table length
* in bytes
* language 4 USHORT Mac language code
*
* segCountX2 6 USHORT 2*NUM_SEGS
* searchRange 8 USHORT 2*(1 << LOG_SEGS)
* entrySelector 10 USHORT LOG_SEGS
* rangeShift 12 USHORT segCountX2 -
* searchRange
*
* endCount 14 USHORT[NUM_SEGS] end charcode for
* each segment; last
* is 0xFFFF
*
* pad 14+NUM_SEGS*2 USHORT padding
*
* startCount 16+NUM_SEGS*2 USHORT[NUM_SEGS] first charcode for
* each segment
*
* idDelta 16+NUM_SEGS*4 SHORT[NUM_SEGS] delta for each
* segment
* idOffset 16+NUM_SEGS*6 SHORT[NUM_SEGS] range offset for
* each segment; can be
* zero
*
* glyphIds 16+NUM_SEGS*8 USHORT[] array of glyph ID
* ranges
*
* Character codes are modelled by a series of ordered (increasing)
* intervals called segments. Each segment has start and end codes,
* provided by the `startCount' and `endCount' arrays. Segments must
* not overlap, and the last segment should always contain the value
* 0xFFFF for `endCount'.
*
* The fields `searchRange', `entrySelector' and `rangeShift' are better
* ignored (they are traces of over-engineering in the TrueType
* specification).
*
* Each segment also has a signed `delta', as well as an optional offset
* within the `glyphIds' table.
*
* If a segment's idOffset is 0, the glyph index corresponding to any
* charcode within the segment is obtained by adding the value of
* `idDelta' directly to the charcode, modulo 65536.
*
* Otherwise, a glyph index is taken from the glyph IDs sub-array for
* the segment, and the value of `idDelta' is added to it.
*
*
* Finally, note that a lot of fonts contain an invalid last segment,
* where `start' and `end' are correctly set to 0xFFFF but both `delta'
* and `offset' are incorrect (e.g., `opens___.ttf' which comes with
* OpenOffice.org). We need special code to deal with them correctly.
*/
#ifdef TT_CONFIG_CMAP_FORMAT_4
typedef struct TT_CMap4Rec_
{
TT_CMapRec cmap;
FT_UInt32 cur_charcode; /* current charcode */
FT_UInt cur_gindex; /* current glyph index */
FT_UInt num_ranges;
FT_UInt cur_range;
FT_UInt cur_start;
FT_UInt cur_end;
FT_Int cur_delta;
FT_Byte* cur_values;
} TT_CMap4Rec, *TT_CMap4;
FT_CALLBACK_DEF( FT_Error )
tt_cmap4_init( TT_CMap4 cmap,
FT_Byte* table )
{
FT_Byte* p;
cmap->cmap.data = table;
p = table + 6;
cmap->num_ranges = FT_PEEK_USHORT( p ) >> 1;
cmap->cur_charcode = (FT_UInt32)0xFFFFFFFFUL;
cmap->cur_gindex = 0;
return FT_Err_Ok;
}
static FT_Int
tt_cmap4_set_range( TT_CMap4 cmap,
FT_UInt range_index )
{
FT_Byte* table = cmap->cmap.data;
FT_Byte* p;
FT_UInt num_ranges = cmap->num_ranges;
while ( range_index < num_ranges )
{
FT_UInt offset;
p = table + 14 + range_index * 2;
cmap->cur_end = FT_PEEK_USHORT( p );
p += 2 + num_ranges * 2;
cmap->cur_start = FT_PEEK_USHORT( p );
p += num_ranges * 2;
cmap->cur_delta = FT_PEEK_SHORT( p );
p += num_ranges * 2;
offset = FT_PEEK_USHORT( p );
/* some fonts have an incorrect last segment; */
/* we have to catch it */
if ( range_index >= num_ranges - 1 &&
cmap->cur_start == 0xFFFFU &&
cmap->cur_end == 0xFFFFU )
{
TT_Face face = (TT_Face)cmap->cmap.cmap.charmap.face;
FT_Byte* limit = face->cmap_table + face->cmap_size;
if ( offset && p + offset + 2 > limit )
{
cmap->cur_delta = 1;
offset = 0;
}
}
if ( offset != 0xFFFFU )
{
cmap->cur_values = offset ? p + offset : NULL;
cmap->cur_range = range_index;
return 0;
}
/* we skip empty segments */
range_index++;
}
return -1;
}
/* search the index of the charcode next to cmap->cur_charcode; */
/* caller should call tt_cmap4_set_range with proper range */
/* before calling this function */
/* */
static void
tt_cmap4_next( TT_CMap4 cmap )
{
TT_Face face = (TT_Face)cmap->cmap.cmap.charmap.face;
FT_Byte* limit = face->cmap_table + face->cmap_size;
FT_UInt charcode;
if ( cmap->cur_charcode >= 0xFFFFUL )
goto Fail;
charcode = (FT_UInt)cmap->cur_charcode + 1;
if ( charcode < cmap->cur_start )
charcode = cmap->cur_start;
for (;;)
{
FT_Byte* values = cmap->cur_values;
FT_UInt end = cmap->cur_end;
FT_Int delta = cmap->cur_delta;
if ( charcode <= end )
{
if ( values )
{
FT_Byte* p = values + 2 * ( charcode - cmap->cur_start );
/* if p > limit, the whole segment is invalid */
if ( p > limit )
goto Next_Segment;
do
{
FT_UInt gindex = FT_NEXT_USHORT( p );
if ( gindex )
{
gindex = (FT_UInt)( (FT_Int)gindex + delta ) & 0xFFFFU;
if ( gindex )
{
cmap->cur_charcode = charcode;
cmap->cur_gindex = gindex;
return;
}
}
} while ( ++charcode <= end );
}
else
{
do
{
FT_UInt gindex = (FT_UInt)( (FT_Int)charcode + delta ) & 0xFFFFU;
if ( gindex >= (FT_UInt)face->root.num_glyphs )
{
/* we have an invalid glyph index; if there is an overflow, */
/* we can adjust `charcode', otherwise the whole segment is */
/* invalid */
gindex = 0;
if ( (FT_Int)charcode + delta < 0 &&
(FT_Int)end + delta >= 0 )
charcode = (FT_UInt)( -delta );
else if ( (FT_Int)charcode + delta < 0x10000L &&
(FT_Int)end + delta >= 0x10000L )
charcode = (FT_UInt)( 0x10000L - delta );
else
goto Next_Segment;
}
if ( gindex )
{
cmap->cur_charcode = charcode;
cmap->cur_gindex = gindex;
return;
}
} while ( ++charcode <= end );
}
}
Next_Segment:
/* we need to find another range */
if ( tt_cmap4_set_range( cmap, cmap->cur_range + 1 ) < 0 )
break;
if ( charcode < cmap->cur_start )
charcode = cmap->cur_start;
}
Fail:
cmap->cur_charcode = (FT_UInt32)0xFFFFFFFFUL;
cmap->cur_gindex = 0;
}
FT_CALLBACK_DEF( FT_Error )
tt_cmap4_validate( FT_Byte* table,
FT_Validator valid )
{
FT_Byte* p;
FT_UInt length;
FT_Byte *ends, *starts, *offsets, *deltas, *glyph_ids;
FT_UInt num_segs;
FT_Error error = FT_Err_Ok;
if ( table + 2 + 2 > valid->limit )
FT_INVALID_TOO_SHORT;
p = table + 2; /* skip format */
length = TT_NEXT_USHORT( p );
/* in certain fonts, the `length' field is invalid and goes */
/* out of bound. We try to correct this here... */
if ( table + length > valid->limit )
{
if ( valid->level >= FT_VALIDATE_TIGHT )
FT_INVALID_TOO_SHORT;
length = (FT_UInt)( valid->limit - table );
}
/* it also happens that the `length' field is too small; */
/* this is easy to correct */
if ( length < (FT_UInt)( valid->limit - table ) )
{
if ( valid->level >= FT_VALIDATE_PARANOID )
FT_INVALID_DATA;
length = (FT_UInt)( valid->limit - table );
}
if ( length < 16 )
FT_INVALID_TOO_SHORT;
p = table + 6;
num_segs = TT_NEXT_USHORT( p ); /* read segCountX2 */
if ( valid->level >= FT_VALIDATE_PARANOID )
{
/* check that we have an even value here */
if ( num_segs & 1 )
FT_INVALID_DATA;
}
num_segs /= 2;
if ( length < 16 + num_segs * 2 * 4 )
FT_INVALID_TOO_SHORT;
/* check the search parameters - even though we never use them */
/* */
if ( valid->level >= FT_VALIDATE_PARANOID )
{
/* check the values of `searchRange', `entrySelector', `rangeShift' */
FT_UInt search_range = TT_NEXT_USHORT( p );
FT_UInt entry_selector = TT_NEXT_USHORT( p );
FT_UInt range_shift = TT_NEXT_USHORT( p );
if ( ( search_range | range_shift ) & 1 ) /* must be even values */
FT_INVALID_DATA;
search_range /= 2;
range_shift /= 2;
/* `search range' is the greatest power of 2 that is <= num_segs */
if ( search_range > num_segs ||
search_range * 2 < num_segs ||
search_range + range_shift != num_segs ||
search_range != ( 1U << entry_selector ) )
FT_INVALID_DATA;
}
ends = table + 14;
starts = table + 16 + num_segs * 2;
deltas = starts + num_segs * 2;
offsets = deltas + num_segs * 2;
glyph_ids = offsets + num_segs * 2;
/* check last segment; its end count value must be 0xFFFF */
if ( valid->level >= FT_VALIDATE_PARANOID )
{
p = ends + ( num_segs - 1 ) * 2;
if ( TT_PEEK_USHORT( p ) != 0xFFFFU )
FT_INVALID_DATA;
}
{
FT_UInt start, end, offset, n;
FT_UInt last_start = 0, last_end = 0;
FT_Int delta;
FT_Byte* p_start = starts;
FT_Byte* p_end = ends;
FT_Byte* p_delta = deltas;
FT_Byte* p_offset = offsets;
for ( n = 0; n < num_segs; n++ )
{
p = p_offset;
start = TT_NEXT_USHORT( p_start );
end = TT_NEXT_USHORT( p_end );