1 /***************************************************************************/
5 /* TrueType character mapping table (cmap) support (body). */
7 /* Copyright 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010 by */
8 /* David Turner, Robert Wilhelm, and Werner Lemberg. */
10 /* This file is part of the FreeType project, and may only be used, */
11 /* modified, and distributed under the terms of the FreeType project */
12 /* license, LICENSE.TXT. By continuing to use, modify, or distribute */
13 /* this file you indicate that you have read the license and */
14 /* understand and accept it fully. */
16 /***************************************************************************/
20 #include FT_INTERNAL_DEBUG_H
22 #include "sferrors.h" /* must come before FT_INTERNAL_VALIDATE_H */
24 #include FT_INTERNAL_VALIDATE_H
25 #include FT_INTERNAL_STREAM_H
31 /*************************************************************************/
33 /* The macro FT_COMPONENT is used in trace mode. It is an implicit */
34 /* parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log */
35 /* messages during execution. */
38 #define FT_COMPONENT trace_ttcmap
41 #define TT_PEEK_SHORT FT_PEEK_SHORT
42 #define TT_PEEK_USHORT FT_PEEK_USHORT
43 #define TT_PEEK_UINT24 FT_PEEK_UOFF3
44 #define TT_PEEK_LONG FT_PEEK_LONG
45 #define TT_PEEK_ULONG FT_PEEK_ULONG
47 #define TT_NEXT_SHORT FT_NEXT_SHORT
48 #define TT_NEXT_USHORT FT_NEXT_USHORT
49 #define TT_NEXT_UINT24 FT_NEXT_UOFF3
50 #define TT_NEXT_LONG FT_NEXT_LONG
51 #define TT_NEXT_ULONG FT_NEXT_ULONG
54 FT_CALLBACK_DEF( FT_Error )
55 tt_cmap_init( TT_CMap cmap,
63 /*************************************************************************/
64 /*************************************************************************/
66 /***** FORMAT 0 *****/
68 /*************************************************************************/
69 /*************************************************************************/
71 /*************************************************************************/
76 /* NAME OFFSET TYPE DESCRIPTION */
78 /* format 0 USHORT must be 0 */
79 /* length 2 USHORT table length in bytes */
80 /* language 4 USHORT Mac language code */
81 /* glyph_ids 6 BYTE[256] array of glyph indices */
85 #ifdef TT_CONFIG_CMAP_FORMAT_0
87 FT_CALLBACK_DEF( FT_Error )
88 tt_cmap0_validate( FT_Byte* table,
91 FT_Byte* p = table + 2;
92 FT_UInt length = TT_NEXT_USHORT( p );
95 if ( table + length > valid->limit || length < 262 )
98 /* check glyph indices whenever necessary */
99 if ( valid->level >= FT_VALIDATE_TIGHT )
105 for ( n = 0; n < 256; n++ )
108 if ( idx >= TT_VALID_GLYPH_COUNT( valid ) )
117 FT_CALLBACK_DEF( FT_UInt )
118 tt_cmap0_char_index( TT_CMap cmap,
119 FT_UInt32 char_code )
121 FT_Byte* table = cmap->data;
124 return char_code < 256 ? table[6 + char_code] : 0;
128 FT_CALLBACK_DEF( FT_UInt32 )
129 tt_cmap0_char_next( TT_CMap cmap,
130 FT_UInt32 *pchar_code )
132 FT_Byte* table = cmap->data;
133 FT_UInt32 charcode = *pchar_code;
134 FT_UInt32 result = 0;
138 table += 6; /* go to glyph IDs */
139 while ( ++charcode < 256 )
141 gindex = table[charcode];
149 *pchar_code = result;
154 FT_CALLBACK_DEF( FT_Error )
155 tt_cmap0_get_info( TT_CMap cmap,
156 TT_CMapInfo *cmap_info )
158 FT_Byte* p = cmap->data + 4;
161 cmap_info->format = 0;
162 cmap_info->language = (FT_ULong)TT_PEEK_USHORT( p );
168 FT_DEFINE_TT_CMAP(tt_cmap0_class_rec,
169 sizeof ( TT_CMapRec ),
171 (FT_CMap_InitFunc) tt_cmap_init,
172 (FT_CMap_DoneFunc) NULL,
173 (FT_CMap_CharIndexFunc)tt_cmap0_char_index,
174 (FT_CMap_CharNextFunc) tt_cmap0_char_next,
176 NULL, NULL, NULL, NULL, NULL
179 (TT_CMap_ValidateFunc) tt_cmap0_validate,
180 (TT_CMap_Info_GetFunc) tt_cmap0_get_info
183 #endif /* TT_CONFIG_CMAP_FORMAT_0 */
186 /*************************************************************************/
187 /*************************************************************************/
189 /***** FORMAT 2 *****/
191 /***** This is used for certain CJK encodings that encode text in a *****/
192 /***** mixed 8/16 bits encoding along the following lines: *****/
194 /***** * Certain byte values correspond to an 8-bit character code *****/
195 /***** (typically in the range 0..127 for ASCII compatibility). *****/
197 /***** * Certain byte values signal the first byte of a 2-byte *****/
198 /***** character code (but these values are also valid as the *****/
199 /***** second byte of a 2-byte character). *****/
201 /***** The following charmap lookup and iteration functions all *****/
202 /***** assume that the value "charcode" correspond to following: *****/
204 /***** - For one byte characters, "charcode" is simply the *****/
205 /***** character code. *****/
207 /***** - For two byte characters, "charcode" is the 2-byte *****/
208 /***** character code in big endian format. More exactly: *****/
210 /***** (charcode >> 8) is the first byte value *****/
211 /***** (charcode & 0xFF) is the second byte value *****/
213 /***** Note that not all values of "charcode" are valid according *****/
214 /***** to these rules, and the function moderately check the *****/
215 /***** arguments. *****/
217 /*************************************************************************/
218 /*************************************************************************/
220 /*************************************************************************/
225 /* NAME OFFSET TYPE DESCRIPTION */
227 /* format 0 USHORT must be 2 */
228 /* length 2 USHORT table length in bytes */
229 /* language 4 USHORT Mac language code */
230 /* keys 6 USHORT[256] sub-header keys */
231 /* subs 518 SUBHEAD[NSUBS] sub-headers array */
232 /* glyph_ids 518+NSUB*8 USHORT[] glyph ID array */
234 /* The `keys' table is used to map charcode high-bytes to sub-headers. */
235 /* The value of `NSUBS' is the number of sub-headers defined in the */
236 /* table and is computed by finding the maximum of the `keys' table. */
238 /* Note that for any n, `keys[n]' is a byte offset within the `subs' */
239 /* table, i.e., it is the corresponding sub-header index multiplied */
242 /* Each sub-header has the following format: */
244 /* NAME OFFSET TYPE DESCRIPTION */
246 /* first 0 USHORT first valid low-byte */
247 /* count 2 USHORT number of valid low-bytes */
248 /* delta 4 SHORT see below */
249 /* offset 6 USHORT see below */
251 /* A sub-header defines, for each high-byte, the range of valid */
252 /* low-bytes within the charmap. Note that the range defined by `first' */
253 /* and `count' must be completely included in the interval [0..255] */
254 /* according to the specification. */
256 /* If a character code is contained within a given sub-header, then */
257 /* mapping it to a glyph index is done as follows: */
259 /* * The value of `offset' is read. This is a _byte_ distance from the */
260 /* location of the `offset' field itself into a slice of the */
261 /* `glyph_ids' table. Let's call it `slice' (it is a USHORT[] too). */
263 /* * The value `slice[char.lo - first]' is read. If it is 0, there is */
264 /* no glyph for the charcode. Otherwise, the value of `delta' is */
265 /* added to it (modulo 65536) to form a new glyph index. */
267 /* It is up to the validation routine to check that all offsets fall */
268 /* within the glyph IDs table (and not within the `subs' table itself or */
269 /* outside of the CMap). */
272 #ifdef TT_CONFIG_CMAP_FORMAT_2
274 FT_CALLBACK_DEF( FT_Error )
275 tt_cmap2_validate( FT_Byte* table,
278 FT_Byte* p = table + 2; /* skip format */
279 FT_UInt length = TT_PEEK_USHORT( p );
281 FT_Byte* keys; /* keys table */
282 FT_Byte* subs; /* sub-headers */
283 FT_Byte* glyph_ids; /* glyph ID array */
286 if ( table + length > valid->limit || length < 6 + 512 )
287 FT_INVALID_TOO_SHORT;
291 /* parse keys to compute sub-headers count */
294 for ( n = 0; n < 256; n++ )
296 FT_UInt idx = TT_NEXT_USHORT( p );
299 /* value must be multiple of 8 */
300 if ( valid->level >= FT_VALIDATE_PARANOID && ( idx & 7 ) != 0 )
305 if ( idx > max_subs )
309 FT_ASSERT( p == table + 518 );
312 glyph_ids = subs + (max_subs + 1) * 8;
313 if ( glyph_ids > valid->limit )
314 FT_INVALID_TOO_SHORT;
316 /* parse sub-headers */
317 for ( n = 0; n <= max_subs; n++ )
319 FT_UInt first_code, code_count, offset;
324 first_code = TT_NEXT_USHORT( p );
325 code_count = TT_NEXT_USHORT( p );
326 delta = TT_NEXT_SHORT( p );
327 offset = TT_NEXT_USHORT( p );
329 /* many Dynalab fonts have empty sub-headers */
330 if ( code_count == 0 )
333 /* check range within 0..255 */
334 if ( valid->level >= FT_VALIDATE_PARANOID )
336 if ( first_code >= 256 || first_code + code_count > 256 )
343 ids = p - 2 + offset;
344 if ( ids < glyph_ids || ids + code_count*2 > table + length )
347 /* check glyph IDs */
348 if ( valid->level >= FT_VALIDATE_TIGHT )
350 FT_Byte* limit = p + code_count * 2;
356 idx = TT_NEXT_USHORT( p );
359 idx = ( idx + delta ) & 0xFFFFU;
360 if ( idx >= TT_VALID_GLYPH_COUNT( valid ) )
372 /* return sub header corresponding to a given character code */
373 /* NULL on invalid charcode */
375 tt_cmap2_get_subheader( FT_Byte* table,
376 FT_UInt32 char_code )
378 FT_Byte* result = NULL;
381 if ( char_code < 0x10000UL )
383 FT_UInt char_lo = (FT_UInt)( char_code & 0xFF );
384 FT_UInt char_hi = (FT_UInt)( char_code >> 8 );
385 FT_Byte* p = table + 6; /* keys table */
386 FT_Byte* subs = table + 518; /* subheaders table */
392 /* an 8-bit character code -- we use subHeader 0 in this case */
393 /* to test whether the character code is in the charmap */
395 sub = subs; /* jump to first sub-header */
397 /* check that the sub-header for this byte is 0, which */
398 /* indicates that it is really a valid one-byte value */
399 /* Otherwise, return 0 */
402 if ( TT_PEEK_USHORT( p ) != 0 )
407 /* a 16-bit character code */
409 /* jump to key entry */
411 /* jump to sub-header */
412 sub = subs + ( FT_PAD_FLOOR( TT_PEEK_USHORT( p ), 8 ) );
414 /* check that the high byte isn't a valid one-byte value */
425 FT_CALLBACK_DEF( FT_UInt )
426 tt_cmap2_char_index( TT_CMap cmap,
427 FT_UInt32 char_code )
429 FT_Byte* table = cmap->data;
434 subheader = tt_cmap2_get_subheader( table, char_code );
437 FT_Byte* p = subheader;
438 FT_UInt idx = (FT_UInt)(char_code & 0xFF);
439 FT_UInt start, count;
444 start = TT_NEXT_USHORT( p );
445 count = TT_NEXT_USHORT( p );
446 delta = TT_NEXT_SHORT ( p );
447 offset = TT_PEEK_USHORT( p );
450 if ( idx < count && offset != 0 )
452 p += offset + 2 * idx;
453 idx = TT_PEEK_USHORT( p );
456 result = (FT_UInt)( idx + delta ) & 0xFFFFU;
463 FT_CALLBACK_DEF( FT_UInt32 )
464 tt_cmap2_char_next( TT_CMap cmap,
465 FT_UInt32 *pcharcode )
467 FT_Byte* table = cmap->data;
469 FT_UInt32 result = 0;
470 FT_UInt32 charcode = *pcharcode + 1;
474 while ( charcode < 0x10000UL )
476 subheader = tt_cmap2_get_subheader( table, charcode );
479 FT_Byte* p = subheader;
480 FT_UInt start = TT_NEXT_USHORT( p );
481 FT_UInt count = TT_NEXT_USHORT( p );
482 FT_Int delta = TT_NEXT_SHORT ( p );
483 FT_UInt offset = TT_PEEK_USHORT( p );
484 FT_UInt char_lo = (FT_UInt)( charcode & 0xFF );
491 if ( char_lo < start )
497 pos = (FT_UInt)( char_lo - start );
499 p += offset + pos * 2;
500 charcode = FT_PAD_FLOOR( charcode, 256 ) + char_lo;
502 for ( ; pos < count; pos++, charcode++ )
504 idx = TT_NEXT_USHORT( p );
508 gindex = ( idx + delta ) & 0xFFFFU;
518 /* jump to next sub-header, i.e. higher byte value */
520 charcode = FT_PAD_FLOOR( charcode, 256 ) + 256;
530 FT_CALLBACK_DEF( FT_Error )
531 tt_cmap2_get_info( TT_CMap cmap,
532 TT_CMapInfo *cmap_info )
534 FT_Byte* p = cmap->data + 4;
537 cmap_info->format = 2;
538 cmap_info->language = (FT_ULong)TT_PEEK_USHORT( p );
544 FT_DEFINE_TT_CMAP(tt_cmap2_class_rec,
545 sizeof ( TT_CMapRec ),
547 (FT_CMap_InitFunc) tt_cmap_init,
548 (FT_CMap_DoneFunc) NULL,
549 (FT_CMap_CharIndexFunc)tt_cmap2_char_index,
550 (FT_CMap_CharNextFunc) tt_cmap2_char_next,
552 NULL, NULL, NULL, NULL, NULL
555 (TT_CMap_ValidateFunc) tt_cmap2_validate,
556 (TT_CMap_Info_GetFunc) tt_cmap2_get_info
559 #endif /* TT_CONFIG_CMAP_FORMAT_2 */
562 /*************************************************************************/
563 /*************************************************************************/
565 /***** FORMAT 4 *****/
567 /*************************************************************************/
568 /*************************************************************************/
570 /*************************************************************************/
575 /* NAME OFFSET TYPE DESCRIPTION */
577 /* format 0 USHORT must be 4 */
578 /* length 2 USHORT table length */
580 /* language 4 USHORT Mac language code */
582 /* segCountX2 6 USHORT 2*NUM_SEGS */
583 /* searchRange 8 USHORT 2*(1 << LOG_SEGS) */
584 /* entrySelector 10 USHORT LOG_SEGS */
585 /* rangeShift 12 USHORT segCountX2 - */
588 /* endCount 14 USHORT[NUM_SEGS] end charcode for */
589 /* each segment; last */
592 /* pad 14+NUM_SEGS*2 USHORT padding */
594 /* startCount 16+NUM_SEGS*2 USHORT[NUM_SEGS] first charcode for */
597 /* idDelta 16+NUM_SEGS*4 SHORT[NUM_SEGS] delta for each */
599 /* idOffset 16+NUM_SEGS*6 SHORT[NUM_SEGS] range offset for */
600 /* each segment; can be */
603 /* glyphIds 16+NUM_SEGS*8 USHORT[] array of glyph ID */
606 /* Character codes are modelled by a series of ordered (increasing) */
607 /* intervals called segments. Each segment has start and end codes, */
608 /* provided by the `startCount' and `endCount' arrays. Segments must */
609 /* not overlap, and the last segment should always contain the value */
610 /* 0xFFFF for `endCount'. */
612 /* The fields `searchRange', `entrySelector' and `rangeShift' are better */
613 /* ignored (they are traces of over-engineering in the TrueType */
614 /* specification). */
616 /* Each segment also has a signed `delta', as well as an optional offset */
617 /* within the `glyphIds' table. */
619 /* If a segment's idOffset is 0, the glyph index corresponding to any */
620 /* charcode within the segment is obtained by adding the value of */
621 /* `idDelta' directly to the charcode, modulo 65536. */
623 /* Otherwise, a glyph index is taken from the glyph IDs sub-array for */
624 /* the segment, and the value of `idDelta' is added to it. */
627 /* Finally, note that a lot of fonts contain an invalid last segment, */
628 /* where `start' and `end' are correctly set to 0xFFFF but both `delta' */
629 /* and `offset' are incorrect (e.g., `opens___.ttf' which comes with */
630 /* OpenOffice.org). We need special code to deal with them correctly. */
633 #ifdef TT_CONFIG_CMAP_FORMAT_4
635 typedef struct TT_CMap4Rec_
638 FT_UInt32 cur_charcode; /* current charcode */
639 FT_UInt cur_gindex; /* current glyph index */
648 } TT_CMap4Rec, *TT_CMap4;
651 FT_CALLBACK_DEF( FT_Error )
652 tt_cmap4_init( TT_CMap4 cmap,
658 cmap->cmap.data = table;
661 cmap->num_ranges = FT_PEEK_USHORT( p ) >> 1;
662 cmap->cur_charcode = (FT_UInt32)0xFFFFFFFFUL;
663 cmap->cur_gindex = 0;
670 tt_cmap4_set_range( TT_CMap4 cmap,
671 FT_UInt range_index )
673 FT_Byte* table = cmap->cmap.data;
675 FT_UInt num_ranges = cmap->num_ranges;
678 while ( range_index < num_ranges )
683 p = table + 14 + range_index * 2;
684 cmap->cur_end = FT_PEEK_USHORT( p );
686 p += 2 + num_ranges * 2;
687 cmap->cur_start = FT_PEEK_USHORT( p );
690 cmap->cur_delta = FT_PEEK_SHORT( p );
693 offset = FT_PEEK_USHORT( p );
695 /* some fonts have an incorrect last segment; */
696 /* we have to catch it */
697 if ( range_index >= num_ranges - 1 &&
698 cmap->cur_start == 0xFFFFU &&
699 cmap->cur_end == 0xFFFFU )
701 TT_Face face = (TT_Face)cmap->cmap.cmap.charmap.face;
702 FT_Byte* limit = face->cmap_table + face->cmap_size;
705 if ( offset && p + offset + 2 > limit )
712 if ( offset != 0xFFFFU )
714 cmap->cur_values = offset ? p + offset : NULL;
715 cmap->cur_range = range_index;
719 /* we skip empty segments */
727 /* search the index of the charcode next to cmap->cur_charcode; */
728 /* caller should call tt_cmap4_set_range with proper range */
729 /* before calling this function */
732 tt_cmap4_next( TT_CMap4 cmap )
737 if ( cmap->cur_charcode >= 0xFFFFUL )
740 charcode = (FT_UInt)cmap->cur_charcode + 1;
742 if ( charcode < cmap->cur_start )
743 charcode = cmap->cur_start;
747 FT_Byte* values = cmap->cur_values;
748 FT_UInt end = cmap->cur_end;
749 FT_Int delta = cmap->cur_delta;
752 if ( charcode <= end )
756 FT_Byte* p = values + 2 * ( charcode - cmap->cur_start );
761 FT_UInt gindex = FT_NEXT_USHORT( p );
766 gindex = (FT_UInt)( ( gindex + delta ) & 0xFFFFU );
769 cmap->cur_charcode = charcode;
770 cmap->cur_gindex = gindex;
774 } while ( ++charcode <= end );
780 FT_UInt gindex = (FT_UInt)( ( charcode + delta ) & 0xFFFFU );
785 cmap->cur_charcode = charcode;
786 cmap->cur_gindex = gindex;
789 } while ( ++charcode <= end );
793 /* we need to find another range */
794 if ( tt_cmap4_set_range( cmap, cmap->cur_range + 1 ) < 0 )
797 if ( charcode < cmap->cur_start )
798 charcode = cmap->cur_start;
802 cmap->cur_charcode = (FT_UInt32)0xFFFFFFFFUL;
803 cmap->cur_gindex = 0;
807 FT_CALLBACK_DEF( FT_Error )
808 tt_cmap4_validate( FT_Byte* table,
811 FT_Byte* p = table + 2; /* skip format */
812 FT_UInt length = TT_NEXT_USHORT( p );
813 FT_Byte *ends, *starts, *offsets, *deltas, *glyph_ids;
815 FT_Error error = SFNT_Err_Ok;
819 FT_INVALID_TOO_SHORT;
821 /* in certain fonts, the `length' field is invalid and goes */
822 /* out of bound. We try to correct this here... */
823 if ( table + length > valid->limit )
825 if ( valid->level >= FT_VALIDATE_TIGHT )
826 FT_INVALID_TOO_SHORT;
828 length = (FT_UInt)( valid->limit - table );
832 num_segs = TT_NEXT_USHORT( p ); /* read segCountX2 */
834 if ( valid->level >= FT_VALIDATE_PARANOID )
836 /* check that we have an even value here */
843 if ( length < 16 + num_segs * 2 * 4 )
844 FT_INVALID_TOO_SHORT;
846 /* check the search parameters - even though we never use them */
848 if ( valid->level >= FT_VALIDATE_PARANOID )
850 /* check the values of `searchRange', `entrySelector', `rangeShift' */
851 FT_UInt search_range = TT_NEXT_USHORT( p );
852 FT_UInt entry_selector = TT_NEXT_USHORT( p );
853 FT_UInt range_shift = TT_NEXT_USHORT( p );
856 if ( ( search_range | range_shift ) & 1 ) /* must be even values */
862 /* `search range' is the greatest power of 2 that is <= num_segs */
864 if ( search_range > num_segs ||
865 search_range * 2 < num_segs ||
866 search_range + range_shift != num_segs ||
867 search_range != ( 1U << entry_selector ) )
872 starts = table + 16 + num_segs * 2;
873 deltas = starts + num_segs * 2;
874 offsets = deltas + num_segs * 2;
875 glyph_ids = offsets + num_segs * 2;
877 /* check last segment; its end count value must be 0xFFFF */
878 if ( valid->level >= FT_VALIDATE_PARANOID )
880 p = ends + ( num_segs - 1 ) * 2;
881 if ( TT_PEEK_USHORT( p ) != 0xFFFFU )
886 FT_UInt start, end, offset, n;
887 FT_UInt last_start = 0, last_end = 0;
889 FT_Byte* p_start = starts;
890 FT_Byte* p_end = ends;
891 FT_Byte* p_delta = deltas;
892 FT_Byte* p_offset = offsets;
895 for ( n = 0; n < num_segs; n++ )
898 start = TT_NEXT_USHORT( p_start );
899 end = TT_NEXT_USHORT( p_end );
900 delta = TT_NEXT_SHORT( p_delta );
901 offset = TT_NEXT_USHORT( p_offset );
906 /* this test should be performed at default validation level; */
907 /* unfortunately, some popular Asian fonts have overlapping */
908 /* ranges in their charmaps */
910 if ( start <= last_end && n > 0 )
912 if ( valid->level >= FT_VALIDATE_TIGHT )
916 /* allow overlapping segments, provided their start points */
917 /* and end points, respectively, are in ascending order */
919 if ( last_start > start || last_end > end )
920 error |= TT_CMAP_FLAG_UNSORTED;
922 error |= TT_CMAP_FLAG_OVERLAPPING;
926 if ( offset && offset != 0xFFFFU )
928 p += offset; /* start of glyph ID array */
930 /* check that we point within the glyph IDs table only */
931 if ( valid->level >= FT_VALIDATE_TIGHT )
933 if ( p < glyph_ids ||
934 p + ( end - start + 1 ) * 2 > table + length )
937 /* Some fonts handle the last segment incorrectly. In */
938 /* theory, 0xFFFF might point to an ordinary glyph -- */
939 /* a cmap 4 is versatile and could be used for any */
940 /* encoding, not only Unicode. However, reality shows */
941 /* that far too many fonts are sloppy and incorrectly */
942 /* set all fields but `start' and `end' for the last */
943 /* segment if it contains only a single character. */
945 /* We thus omit the test here, delaying it to the */
946 /* routines which actually access the cmap. */
947 else if ( n != num_segs - 1 ||
948 !( start == 0xFFFFU && end == 0xFFFFU ) )
950 if ( p < glyph_ids ||
951 p + ( end - start + 1 ) * 2 > valid->limit )
955 /* check glyph indices within the segment range */
956 if ( valid->level >= FT_VALIDATE_TIGHT )
961 for ( i = start; i < end; i++ )
963 idx = FT_NEXT_USHORT( p );
966 idx = (FT_UInt)( idx + delta ) & 0xFFFFU;
968 if ( idx >= TT_VALID_GLYPH_COUNT( valid ) )
974 else if ( offset == 0xFFFFU )
976 /* some fonts (erroneously?) use a range offset of 0xFFFF */
977 /* to mean missing glyph in cmap table */
979 if ( valid->level >= FT_VALIDATE_PARANOID ||
981 !( start == 0xFFFFU && end == 0xFFFFU ) )
995 tt_cmap4_char_map_linear( TT_CMap cmap,
996 FT_UInt32* pcharcode,
999 FT_UInt num_segs2, start, end, offset;
1001 FT_UInt i, num_segs;
1002 FT_UInt32 charcode = *pcharcode;
1008 num_segs2 = FT_PAD_FLOOR( TT_PEEK_USHORT( p ), 2 );
1010 num_segs = num_segs2 >> 1;
1019 for ( ; charcode <= 0xFFFFU; charcode++ )
1024 p = cmap->data + 14; /* ends table */
1025 q = cmap->data + 16 + num_segs2; /* starts table */
1027 for ( i = 0; i < num_segs; i++ )
1029 end = TT_NEXT_USHORT( p );
1030 start = TT_NEXT_USHORT( q );
1032 if ( charcode >= start && charcode <= end )
1034 p = q - 2 + num_segs2;
1035 delta = TT_PEEK_SHORT( p );
1037 offset = TT_PEEK_USHORT( p );
1039 /* some fonts have an incorrect last segment; */
1040 /* we have to catch it */
1041 if ( i >= num_segs - 1 &&
1042 start == 0xFFFFU && end == 0xFFFFU )
1044 TT_Face face = (TT_Face)cmap->cmap.charmap.face;
1045 FT_Byte* limit = face->cmap_table + face->cmap_size;
1048 if ( offset && p + offset + 2 > limit )
1055 if ( offset == 0xFFFFU )
1060 p += offset + ( charcode - start ) * 2;
1061 gindex = TT_PEEK_USHORT( p );
1063 gindex = (FT_UInt)( gindex + delta ) & 0xFFFFU;
1066 gindex = (FT_UInt)( charcode + delta ) & 0xFFFFU;
1072 if ( !next || gindex )
1076 if ( next && gindex )
1077 *pcharcode = charcode;
1084 tt_cmap4_char_map_binary( TT_CMap cmap,
1085 FT_UInt32* pcharcode,
1088 FT_UInt num_segs2, start, end, offset;
1090 FT_UInt max, min, mid, num_segs;
1091 FT_UInt charcode = (FT_UInt)*pcharcode;
1097 num_segs2 = FT_PAD_FLOOR( TT_PEEK_USHORT( p ), 2 );
1102 num_segs = num_segs2 >> 1;
1104 /* make compiler happy */
1117 mid = ( min + max ) >> 1;
1118 p = cmap->data + 14 + mid * 2;
1119 end = TT_PEEK_USHORT( p );
1121 start = TT_PEEK_USHORT( p );
1123 if ( charcode < start )
1125 else if ( charcode > end )
1130 delta = TT_PEEK_SHORT( p );
1132 offset = TT_PEEK_USHORT( p );
1134 /* some fonts have an incorrect last segment; */
1135 /* we have to catch it */
1136 if ( mid >= num_segs - 1 &&
1137 start == 0xFFFFU && end == 0xFFFFU )
1139 TT_Face face = (TT_Face)cmap->cmap.charmap.face;
1140 FT_Byte* limit = face->cmap_table + face->cmap_size;
1143 if ( offset && p + offset + 2 > limit )
1150 /* search the first segment containing `charcode' */
1151 if ( cmap->flags & TT_CMAP_FLAG_OVERLAPPING )
1156 /* call the current segment `max' */
1159 if ( offset == 0xFFFFU )
1162 /* search in segments before the current segment */
1163 for ( i = max ; i > 0; i-- )
1170 p = cmap->data + 14 + ( i - 1 ) * 2;
1171 prev_end = TT_PEEK_USHORT( p );
1173 if ( charcode > prev_end )
1181 start = TT_PEEK_USHORT( p );
1183 delta = TT_PEEK_SHORT( p );
1185 offset = TT_PEEK_USHORT( p );
1187 if ( offset != 0xFFFFU )
1192 if ( mid == max + 1 )
1196 p = cmap->data + 14 + max * 2;
1197 end = TT_PEEK_USHORT( p );
1199 start = TT_PEEK_USHORT( p );
1201 delta = TT_PEEK_SHORT( p );
1203 offset = TT_PEEK_USHORT( p );
1208 /* search in segments after the current segment */
1209 for ( i = max + 1; i < num_segs; i++ )
1211 FT_UInt next_end, next_start;
1214 p = cmap->data + 14 + i * 2;
1215 next_end = TT_PEEK_USHORT( p );
1217 next_start = TT_PEEK_USHORT( p );
1219 if ( charcode < next_start )
1225 delta = TT_PEEK_SHORT( p );
1227 offset = TT_PEEK_USHORT( p );
1229 if ( offset != 0xFFFFU )
1243 /* end, start, delta, and offset are for the i'th segment */
1246 p = cmap->data + 14 + mid * 2;
1247 end = TT_PEEK_USHORT( p );
1249 start = TT_PEEK_USHORT( p );
1251 delta = TT_PEEK_SHORT( p );
1253 offset = TT_PEEK_USHORT( p );
1258 if ( offset == 0xFFFFU )
1264 p += offset + ( charcode - start ) * 2;
1265 gindex = TT_PEEK_USHORT( p );
1267 gindex = (FT_UInt)( gindex + delta ) & 0xFFFFU;
1270 gindex = (FT_UInt)( charcode + delta ) & 0xFFFFU;
1278 TT_CMap4 cmap4 = (TT_CMap4)cmap;
1281 /* if `charcode' is not in any segment, then `mid' is */
1282 /* the segment nearest to `charcode' */
1285 if ( charcode > end )
1288 if ( mid == num_segs )
1292 if ( tt_cmap4_set_range( cmap4, mid ) )
1295 *pcharcode = charcode;
1299 cmap4->cur_charcode = charcode;
1302 cmap4->cur_gindex = gindex;
1305 cmap4->cur_charcode = charcode;
1306 tt_cmap4_next( cmap4 );
1307 gindex = cmap4->cur_gindex;
1311 *pcharcode = cmap4->cur_charcode;
1319 FT_CALLBACK_DEF( FT_UInt )
1320 tt_cmap4_char_index( TT_CMap cmap,
1321 FT_UInt32 char_code )
1323 if ( char_code >= 0x10000UL )
1326 if ( cmap->flags & TT_CMAP_FLAG_UNSORTED )
1327 return tt_cmap4_char_map_linear( cmap, &char_code, 0 );
1329 return tt_cmap4_char_map_binary( cmap, &char_code, 0 );
1333 FT_CALLBACK_DEF( FT_UInt32 )
1334 tt_cmap4_char_next( TT_CMap cmap,
1335 FT_UInt32 *pchar_code )
1340 if ( *pchar_code >= 0xFFFFU )
1343 if ( cmap->flags & TT_CMAP_FLAG_UNSORTED )
1344 gindex = tt_cmap4_char_map_linear( cmap, pchar_code, 1 );
1347 TT_CMap4 cmap4 = (TT_CMap4)cmap;
1350 /* no need to search */
1351 if ( *pchar_code == cmap4->cur_charcode )
1353 tt_cmap4_next( cmap4 );
1354 gindex = cmap4->cur_gindex;
1356 *pchar_code = cmap4->cur_charcode;
1359 gindex = tt_cmap4_char_map_binary( cmap, pchar_code, 1 );
1366 FT_CALLBACK_DEF( FT_Error )
1367 tt_cmap4_get_info( TT_CMap cmap,
1368 TT_CMapInfo *cmap_info )
1370 FT_Byte* p = cmap->data + 4;
1373 cmap_info->format = 4;
1374 cmap_info->language = (FT_ULong)TT_PEEK_USHORT( p );
1380 FT_DEFINE_TT_CMAP(tt_cmap4_class_rec,
1381 sizeof ( TT_CMap4Rec ),
1382 (FT_CMap_InitFunc) tt_cmap4_init,
1383 (FT_CMap_DoneFunc) NULL,
1384 (FT_CMap_CharIndexFunc)tt_cmap4_char_index,
1385 (FT_CMap_CharNextFunc) tt_cmap4_char_next,
1387 NULL, NULL, NULL, NULL, NULL
1390 (TT_CMap_ValidateFunc) tt_cmap4_validate,
1391 (TT_CMap_Info_GetFunc) tt_cmap4_get_info
1394 #endif /* TT_CONFIG_CMAP_FORMAT_4 */
1397 /*************************************************************************/
1398 /*************************************************************************/
1400 /***** FORMAT 6 *****/
1402 /*************************************************************************/
1403 /*************************************************************************/
1405 /*************************************************************************/
1407 /* TABLE OVERVIEW */
1408 /* -------------- */
1410 /* NAME OFFSET TYPE DESCRIPTION */
1412 /* format 0 USHORT must be 4 */
1413 /* length 2 USHORT table length in bytes */
1414 /* language 4 USHORT Mac language code */
1416 /* first 6 USHORT first segment code */
1417 /* count 8 USHORT segment size in chars */
1418 /* glyphIds 10 USHORT[count] glyph IDs */
1420 /* A very simplified segment mapping. */
1423 #ifdef TT_CONFIG_CMAP_FORMAT_6
1425 FT_CALLBACK_DEF( FT_Error )
1426 tt_cmap6_validate( FT_Byte* table,
1427 FT_Validator valid )
1430 FT_UInt length, count;
1433 if ( table + 10 > valid->limit )
1434 FT_INVALID_TOO_SHORT;
1437 length = TT_NEXT_USHORT( p );
1439 p = table + 8; /* skip language and start index */
1440 count = TT_NEXT_USHORT( p );
1442 if ( table + length > valid->limit || length < 10 + count * 2 )
1443 FT_INVALID_TOO_SHORT;
1445 /* check glyph indices */
1446 if ( valid->level >= FT_VALIDATE_TIGHT )
1451 for ( ; count > 0; count-- )
1453 gindex = TT_NEXT_USHORT( p );
1454 if ( gindex >= TT_VALID_GLYPH_COUNT( valid ) )
1455 FT_INVALID_GLYPH_ID;
1463 FT_CALLBACK_DEF( FT_UInt )
1464 tt_cmap6_char_index( TT_CMap cmap,
1465 FT_UInt32 char_code )
1467 FT_Byte* table = cmap->data;
1469 FT_Byte* p = table + 6;
1470 FT_UInt start = TT_NEXT_USHORT( p );
1471 FT_UInt count = TT_NEXT_USHORT( p );
1472 FT_UInt idx = (FT_UInt)( char_code - start );
1478 result = TT_PEEK_USHORT( p );
1484 FT_CALLBACK_DEF( FT_UInt32 )
1485 tt_cmap6_char_next( TT_CMap cmap,
1486 FT_UInt32 *pchar_code )
1488 FT_Byte* table = cmap->data;
1489 FT_UInt32 result = 0;
1490 FT_UInt32 char_code = *pchar_code + 1;
1493 FT_Byte* p = table + 6;
1494 FT_UInt start = TT_NEXT_USHORT( p );
1495 FT_UInt count = TT_NEXT_USHORT( p );
1499 if ( char_code >= 0x10000UL )
1502 if ( char_code < start )
1505 idx = (FT_UInt)( char_code - start );
1508 for ( ; idx < count; idx++ )
1510 gindex = TT_NEXT_USHORT( p );
1520 *pchar_code = result;
1525 FT_CALLBACK_DEF( FT_Error )
1526 tt_cmap6_get_info( TT_CMap cmap,
1527 TT_CMapInfo *cmap_info )
1529 FT_Byte* p = cmap->data + 4;
1532 cmap_info->format = 6;
1533 cmap_info->language = (FT_ULong)TT_PEEK_USHORT( p );
1539 FT_DEFINE_TT_CMAP(tt_cmap6_class_rec,
1540 sizeof ( TT_CMapRec ),
1542 (FT_CMap_InitFunc) tt_cmap_init,
1543 (FT_CMap_DoneFunc) NULL,
1544 (FT_CMap_CharIndexFunc)tt_cmap6_char_index,
1545 (FT_CMap_CharNextFunc) tt_cmap6_char_next,
1547 NULL, NULL, NULL, NULL, NULL
1550 (TT_CMap_ValidateFunc) tt_cmap6_validate,
1551 (TT_CMap_Info_GetFunc) tt_cmap6_get_info
1554 #endif /* TT_CONFIG_CMAP_FORMAT_6 */
1557 /*************************************************************************/
1558 /*************************************************************************/
1560 /***** FORMAT 8 *****/
1562 /***** It is hard to completely understand what the OpenType spec *****/
1563 /***** says about this format, but here is my conclusion. *****/
1565 /***** The purpose of this format is to easily map UTF-16 text to *****/
1566 /***** glyph indices. Basically, the `char_code' must be in one of *****/
1567 /***** the following formats: *****/
1569 /***** - A 16-bit value that isn't part of the Unicode Surrogates *****/
1570 /***** Area (i.e. U+D800-U+DFFF). *****/
1572 /***** - A 32-bit value, made of two surrogate values, i.e.. if *****/
1573 /***** `char_code = (char_hi << 16) | char_lo', then both *****/
1574 /***** `char_hi' and `char_lo' must be in the Surrogates Area. *****/
1577 /***** The `is32' table embedded in the charmap indicates whether a *****/
1578 /***** given 16-bit value is in the surrogates area or not. *****/
1580 /***** So, for any given `char_code', we can assert the following: *****/
1582 /***** If `char_hi == 0' then we must have `is32[char_lo] == 0'. *****/
1584 /***** If `char_hi != 0' then we must have both *****/
1585 /***** `is32[char_hi] != 0' and `is32[char_lo] != 0'. *****/
1587 /*************************************************************************/
1588 /*************************************************************************/
1590 /*************************************************************************/
1592 /* TABLE OVERVIEW */
1593 /* -------------- */
1595 /* NAME OFFSET TYPE DESCRIPTION */
1597 /* format 0 USHORT must be 8 */
1598 /* reserved 2 USHORT reserved */
1599 /* length 4 ULONG length in bytes */
1600 /* language 8 ULONG Mac language code */
1601 /* is32 12 BYTE[8192] 32-bitness bitmap */
1602 /* count 8204 ULONG number of groups */
1604 /* This header is followed by `count' groups of the following format: */
1606 /* start 0 ULONG first charcode */
1607 /* end 4 ULONG last charcode */
1608 /* startId 8 ULONG start glyph ID for the group */
1611 #ifdef TT_CONFIG_CMAP_FORMAT_8
1613 FT_CALLBACK_DEF( FT_Error )
1614 tt_cmap8_validate( FT_Byte* table,
1615 FT_Validator valid )
1617 FT_Byte* p = table + 4;
1620 FT_UInt32 num_groups;
1623 if ( table + 16 + 8192 > valid->limit )
1624 FT_INVALID_TOO_SHORT;
1626 length = TT_NEXT_ULONG( p );
1627 if ( length > (FT_UInt32)( valid->limit - table ) || length < 8192 + 16 )
1628 FT_INVALID_TOO_SHORT;
1631 p = is32 + 8192; /* skip `is32' array */
1632 num_groups = TT_NEXT_ULONG( p );
1634 if ( p + num_groups * 12 > valid->limit )
1635 FT_INVALID_TOO_SHORT;
1637 /* check groups, they must be in increasing order */
1639 FT_UInt32 n, start, end, start_id, count, last = 0;
1642 for ( n = 0; n < num_groups; n++ )
1647 start = TT_NEXT_ULONG( p );
1648 end = TT_NEXT_ULONG( p );
1649 start_id = TT_NEXT_ULONG( p );
1654 if ( n > 0 && start <= last )
1657 if ( valid->level >= FT_VALIDATE_TIGHT )
1659 if ( start_id + end - start >= TT_VALID_GLYPH_COUNT( valid ) )
1660 FT_INVALID_GLYPH_ID;
1662 count = (FT_UInt32)( end - start + 1 );
1664 if ( start & ~0xFFFFU )
1666 /* start_hi != 0; check that is32[i] is 1 for each i in */
1667 /* the `hi' and `lo' of the range [start..end] */
1668 for ( ; count > 0; count--, start++ )
1670 hi = (FT_UInt)( start >> 16 );
1671 lo = (FT_UInt)( start & 0xFFFFU );
1673 if ( (is32[hi >> 3] & ( 0x80 >> ( hi & 7 ) ) ) == 0 )
1676 if ( (is32[lo >> 3] & ( 0x80 >> ( lo & 7 ) ) ) == 0 )
1682 /* start_hi == 0; check that is32[i] is 0 for each i in */
1683 /* the range [start..end] */
1685 /* end_hi cannot be != 0! */
1686 if ( end & ~0xFFFFU )
1689 for ( ; count > 0; count--, start++ )
1691 lo = (FT_UInt)( start & 0xFFFFU );
1693 if ( (is32[lo >> 3] & ( 0x80 >> ( lo & 7 ) ) ) != 0 )
1707 FT_CALLBACK_DEF( FT_UInt )
1708 tt_cmap8_char_index( TT_CMap cmap,
1709 FT_UInt32 char_code )
1711 FT_Byte* table = cmap->data;
1713 FT_Byte* p = table + 8204;
1714 FT_UInt32 num_groups = TT_NEXT_ULONG( p );
1715 FT_UInt32 start, end, start_id;
1718 for ( ; num_groups > 0; num_groups-- )
1720 start = TT_NEXT_ULONG( p );
1721 end = TT_NEXT_ULONG( p );
1722 start_id = TT_NEXT_ULONG( p );
1724 if ( char_code < start )
1727 if ( char_code <= end )
1729 result = (FT_UInt)( start_id + char_code - start );
1737 FT_CALLBACK_DEF( FT_UInt32 )
1738 tt_cmap8_char_next( TT_CMap cmap,
1739 FT_UInt32 *pchar_code )
1741 FT_UInt32 result = 0;
1742 FT_UInt32 char_code = *pchar_code + 1;
1744 FT_Byte* table = cmap->data;
1745 FT_Byte* p = table + 8204;
1746 FT_UInt32 num_groups = TT_NEXT_ULONG( p );
1747 FT_UInt32 start, end, start_id;
1752 for ( ; num_groups > 0; num_groups-- )
1754 start = TT_NEXT_ULONG( p );
1755 end = TT_NEXT_ULONG( p );
1756 start_id = TT_NEXT_ULONG( p );
1758 if ( char_code < start )
1761 if ( char_code <= end )
1763 gindex = (FT_UInt)( char_code - start + start_id );
1773 *pchar_code = result;
1778 FT_CALLBACK_DEF( FT_Error )
1779 tt_cmap8_get_info( TT_CMap cmap,
1780 TT_CMapInfo *cmap_info )
1782 FT_Byte* p = cmap->data + 8;
1785 cmap_info->format = 8;
1786 cmap_info->language = (FT_ULong)TT_PEEK_ULONG( p );
1792 FT_DEFINE_TT_CMAP(tt_cmap8_class_rec,
1793 sizeof ( TT_CMapRec ),
1795 (FT_CMap_InitFunc) tt_cmap_init,
1796 (FT_CMap_DoneFunc) NULL,
1797 (FT_CMap_CharIndexFunc)tt_cmap8_char_index,
1798 (FT_CMap_CharNextFunc) tt_cmap8_char_next,
1800 NULL, NULL, NULL, NULL, NULL
1803 (TT_CMap_ValidateFunc) tt_cmap8_validate,
1804 (TT_CMap_Info_GetFunc) tt_cmap8_get_info
1807 #endif /* TT_CONFIG_CMAP_FORMAT_8 */
1810 /*************************************************************************/
1811 /*************************************************************************/
1813 /***** FORMAT 10 *****/
1815 /*************************************************************************/
1816 /*************************************************************************/
1818 /*************************************************************************/
1820 /* TABLE OVERVIEW */
1821 /* -------------- */
1823 /* NAME OFFSET TYPE DESCRIPTION */
1825 /* format 0 USHORT must be 10 */
1826 /* reserved 2 USHORT reserved */
1827 /* length 4 ULONG length in bytes */
1828 /* language 8 ULONG Mac language code */
1830 /* start 12 ULONG first char in range */
1831 /* count 16 ULONG number of chars in range */
1832 /* glyphIds 20 USHORT[count] glyph indices covered */
1835 #ifdef TT_CONFIG_CMAP_FORMAT_10
1837 FT_CALLBACK_DEF( FT_Error )
1838 tt_cmap10_validate( FT_Byte* table,
1839 FT_Validator valid )
1841 FT_Byte* p = table + 4;
1842 FT_ULong length, count;
1845 if ( table + 20 > valid->limit )
1846 FT_INVALID_TOO_SHORT;
1848 length = TT_NEXT_ULONG( p );
1850 count = TT_NEXT_ULONG( p );
1852 if ( length > (FT_ULong)( valid->limit - table ) ||
1853 length < 20 + count * 2 )
1854 FT_INVALID_TOO_SHORT;
1856 /* check glyph indices */
1857 if ( valid->level >= FT_VALIDATE_TIGHT )
1862 for ( ; count > 0; count-- )
1864 gindex = TT_NEXT_USHORT( p );
1865 if ( gindex >= TT_VALID_GLYPH_COUNT( valid ) )
1866 FT_INVALID_GLYPH_ID;
1874 FT_CALLBACK_DEF( FT_UInt )
1875 tt_cmap10_char_index( TT_CMap cmap,
1876 FT_UInt32 char_code )
1878 FT_Byte* table = cmap->data;
1880 FT_Byte* p = table + 12;
1881 FT_UInt32 start = TT_NEXT_ULONG( p );
1882 FT_UInt32 count = TT_NEXT_ULONG( p );
1883 FT_UInt32 idx = (FT_ULong)( char_code - start );
1889 result = TT_PEEK_USHORT( p );
1895 FT_CALLBACK_DEF( FT_UInt32 )
1896 tt_cmap10_char_next( TT_CMap cmap,
1897 FT_UInt32 *pchar_code )
1899 FT_Byte* table = cmap->data;
1900 FT_UInt32 char_code = *pchar_code + 1;
1902 FT_Byte* p = table + 12;
1903 FT_UInt32 start = TT_NEXT_ULONG( p );
1904 FT_UInt32 count = TT_NEXT_ULONG( p );
1908 if ( char_code < start )
1911 idx = (FT_UInt32)( char_code - start );
1914 for ( ; idx < count; idx++ )
1916 gindex = TT_NEXT_USHORT( p );
1922 *pchar_code = char_code;
1927 FT_CALLBACK_DEF( FT_Error )
1928 tt_cmap10_get_info( TT_CMap cmap,
1929 TT_CMapInfo *cmap_info )
1931 FT_Byte* p = cmap->data + 8;
1934 cmap_info->format = 10;
1935 cmap_info->language = (FT_ULong)TT_PEEK_ULONG( p );
1941 FT_DEFINE_TT_CMAP(tt_cmap10_class_rec,
1942 sizeof ( TT_CMapRec ),
1944 (FT_CMap_InitFunc) tt_cmap_init,
1945 (FT_CMap_DoneFunc) NULL,
1946 (FT_CMap_CharIndexFunc)tt_cmap10_char_index,
1947 (FT_CMap_CharNextFunc) tt_cmap10_char_next,
1949 NULL, NULL, NULL, NULL, NULL
1952 (TT_CMap_ValidateFunc) tt_cmap10_validate,
1953 (TT_CMap_Info_GetFunc) tt_cmap10_get_info
1956 #endif /* TT_CONFIG_CMAP_FORMAT_10 */
1959 /*************************************************************************/
1960 /*************************************************************************/
1962 /***** FORMAT 12 *****/
1964 /*************************************************************************/
1965 /*************************************************************************/
1967 /*************************************************************************/
1969 /* TABLE OVERVIEW */
1970 /* -------------- */
1972 /* NAME OFFSET TYPE DESCRIPTION */
1974 /* format 0 USHORT must be 12 */
1975 /* reserved 2 USHORT reserved */
1976 /* length 4 ULONG length in bytes */
1977 /* language 8 ULONG Mac language code */
1978 /* count 12 ULONG number of groups */
1981 /* This header is followed by `count' groups of the following format: */
1983 /* start 0 ULONG first charcode */
1984 /* end 4 ULONG last charcode */
1985 /* startId 8 ULONG start glyph ID for the group */
1988 #ifdef TT_CONFIG_CMAP_FORMAT_12
1990 typedef struct TT_CMap12Rec_
1994 FT_ULong cur_charcode;
1997 FT_ULong num_groups;
1999 } TT_CMap12Rec, *TT_CMap12;
2002 FT_CALLBACK_DEF( FT_Error )
2003 tt_cmap12_init( TT_CMap12 cmap,
2006 cmap->cmap.data = table;
2009 cmap->num_groups = FT_PEEK_ULONG( table );
2017 FT_CALLBACK_DEF( FT_Error )
2018 tt_cmap12_validate( FT_Byte* table,
2019 FT_Validator valid )
2023 FT_ULong num_groups;
2026 if ( table + 16 > valid->limit )
2027 FT_INVALID_TOO_SHORT;
2030 length = TT_NEXT_ULONG( p );
2033 num_groups = TT_NEXT_ULONG( p );
2035 if ( length > (FT_ULong)( valid->limit - table ) ||
2036 length < 16 + 12 * num_groups )
2037 FT_INVALID_TOO_SHORT;
2039 /* check groups, they must be in increasing order */
2041 FT_ULong n, start, end, start_id, last = 0;
2044 for ( n = 0; n < num_groups; n++ )
2046 start = TT_NEXT_ULONG( p );
2047 end = TT_NEXT_ULONG( p );
2048 start_id = TT_NEXT_ULONG( p );
2053 if ( n > 0 && start <= last )
2056 if ( valid->level >= FT_VALIDATE_TIGHT )
2058 if ( start_id + end - start >= TT_VALID_GLYPH_COUNT( valid ) )
2059 FT_INVALID_GLYPH_ID;
2070 /* search the index of the charcode next to cmap->cur_charcode */
2071 /* cmap->cur_group should be set up properly by caller */
2074 tt_cmap12_next( TT_CMap12 cmap )
2077 FT_ULong start, end, start_id, char_code;
2082 if ( cmap->cur_charcode >= 0xFFFFFFFFUL )
2085 char_code = cmap->cur_charcode + 1;
2087 n = cmap->cur_group;
2089 for ( n = cmap->cur_group; n < cmap->num_groups; n++ )
2091 p = cmap->cmap.data + 16 + 12 * n;
2092 start = TT_NEXT_ULONG( p );
2093 end = TT_NEXT_ULONG( p );
2094 start_id = TT_PEEK_ULONG( p );
2096 if ( char_code < start )
2099 for ( ; char_code <= end; char_code++ )
2101 gindex = (FT_UInt)( start_id + char_code - start );
2105 cmap->cur_charcode = char_code;;
2106 cmap->cur_gindex = gindex;
2107 cmap->cur_group = n;
2120 tt_cmap12_char_map_binary( TT_CMap cmap,
2121 FT_UInt32* pchar_code,
2125 FT_Byte* p = cmap->data + 12;
2126 FT_UInt32 num_groups = TT_PEEK_ULONG( p );
2127 FT_UInt32 char_code = *pchar_code;
2128 FT_UInt32 start, end, start_id;
2129 FT_UInt32 max, min, mid;
2135 /* make compiler happy */
2148 mid = ( min + max ) >> 1;
2149 p = cmap->data + 16 + 12 * mid;
2151 start = TT_NEXT_ULONG( p );
2152 end = TT_NEXT_ULONG( p );
2154 if ( char_code < start )
2156 else if ( char_code > end )
2160 start_id = TT_PEEK_ULONG( p );
2161 gindex = (FT_UInt)( start_id + char_code - start );
2169 TT_CMap12 cmap12 = (TT_CMap12)cmap;
2172 /* if `char_code' is not in any group, then `mid' is */
2173 /* the group nearest to `char_code' */
2176 if ( char_code > end )
2179 if ( mid == num_groups )
2184 cmap12->cur_charcode = char_code;
2185 cmap12->cur_group = mid;
2189 tt_cmap12_next( cmap12 );
2191 if ( cmap12->valid )
2192 gindex = cmap12->cur_gindex;
2195 cmap12->cur_gindex = gindex;
2198 *pchar_code = cmap12->cur_charcode;
2205 FT_CALLBACK_DEF( FT_UInt )
2206 tt_cmap12_char_index( TT_CMap cmap,
2207 FT_UInt32 char_code )
2209 return tt_cmap12_char_map_binary( cmap, &char_code, 0 );
2213 FT_CALLBACK_DEF( FT_UInt32 )
2214 tt_cmap12_char_next( TT_CMap cmap,
2215 FT_UInt32 *pchar_code )
2217 TT_CMap12 cmap12 = (TT_CMap12)cmap;
2221 if ( cmap12->cur_charcode >= 0xFFFFFFFFUL )
2224 /* no need to search */
2225 if ( cmap12->valid && cmap12->cur_charcode == *pchar_code )
2227 tt_cmap12_next( cmap12 );
2228 if ( cmap12->valid )
2230 gindex = cmap12->cur_gindex;
2232 /* XXX: check cur_charcode overflow is expected */
2234 *pchar_code = (FT_UInt32)cmap12->cur_charcode;
2240 gindex = tt_cmap12_char_map_binary( cmap, pchar_code, 1 );
2242 /* XXX: check gindex overflow is expected */
2243 return (FT_UInt32)gindex;
2247 FT_CALLBACK_DEF( FT_Error )
2248 tt_cmap12_get_info( TT_CMap cmap,
2249 TT_CMapInfo *cmap_info )
2251 FT_Byte* p = cmap->data + 8;
2254 cmap_info->format = 12;
2255 cmap_info->language = (FT_ULong)TT_PEEK_ULONG( p );
2261 FT_DEFINE_TT_CMAP(tt_cmap12_class_rec,
2262 sizeof ( TT_CMap12Rec ),
2264 (FT_CMap_InitFunc) tt_cmap12_init,
2265 (FT_CMap_DoneFunc) NULL,
2266 (FT_CMap_CharIndexFunc)tt_cmap12_char_index,
2267 (FT_CMap_CharNextFunc) tt_cmap12_char_next,
2269 NULL, NULL, NULL, NULL, NULL
2272 (TT_CMap_ValidateFunc) tt_cmap12_validate,
2273 (TT_CMap_Info_GetFunc) tt_cmap12_get_info
2276 #endif /* TT_CONFIG_CMAP_FORMAT_12 */
2279 /*************************************************************************/
2280 /*************************************************************************/
2282 /***** FORMAT 13 *****/
2284 /*************************************************************************/
2285 /*************************************************************************/
2287 /*************************************************************************/
2289 /* TABLE OVERVIEW */
2290 /* -------------- */
2292 /* NAME OFFSET TYPE DESCRIPTION */
2294 /* format 0 USHORT must be 13 */
2295 /* reserved 2 USHORT reserved */
2296 /* length 4 ULONG length in bytes */
2297 /* language 8 ULONG Mac language code */
2298 /* count 12 ULONG number of groups */
2301 /* This header is followed by `count' groups of the following format: */
2303 /* start 0 ULONG first charcode */
2304 /* end 4 ULONG last charcode */
2305 /* glyphId 8 ULONG glyph ID for the whole group */
2308 #ifdef TT_CONFIG_CMAP_FORMAT_13
2310 typedef struct TT_CMap13Rec_
2314 FT_ULong cur_charcode;
2317 FT_ULong num_groups;
2319 } TT_CMap13Rec, *TT_CMap13;
2322 FT_CALLBACK_DEF( FT_Error )
2323 tt_cmap13_init( TT_CMap13 cmap,
2326 cmap->cmap.data = table;
2329 cmap->num_groups = FT_PEEK_ULONG( table );
2337 FT_CALLBACK_DEF( FT_Error )
2338 tt_cmap13_validate( FT_Byte* table,
2339 FT_Validator valid )
2343 FT_ULong num_groups;
2346 if ( table + 16 > valid->limit )
2347 FT_INVALID_TOO_SHORT;
2350 length = TT_NEXT_ULONG( p );
2353 num_groups = TT_NEXT_ULONG( p );
2355 if ( length > (FT_ULong)( valid->limit - table ) ||
2356 length < 16 + 12 * num_groups )
2357 FT_INVALID_TOO_SHORT;
2359 /* check groups, they must be in increasing order */
2361 FT_ULong n, start, end, glyph_id, last = 0;
2364 for ( n = 0; n < num_groups; n++ )
2366 start = TT_NEXT_ULONG( p );
2367 end = TT_NEXT_ULONG( p );
2368 glyph_id = TT_NEXT_ULONG( p );
2373 if ( n > 0 && start <= last )
2376 if ( valid->level >= FT_VALIDATE_TIGHT )
2378 if ( glyph_id >= TT_VALID_GLYPH_COUNT( valid ) )
2379 FT_INVALID_GLYPH_ID;
2390 /* search the index of the charcode next to cmap->cur_charcode */
2391 /* cmap->cur_group should be set up properly by caller */
2394 tt_cmap13_next( TT_CMap13 cmap )
2397 FT_ULong start, end, glyph_id, char_code;
2402 if ( cmap->cur_charcode >= 0xFFFFFFFFUL )
2405 char_code = cmap->cur_charcode + 1;
2407 n = cmap->cur_group;
2409 for ( n = cmap->cur_group; n < cmap->num_groups; n++ )
2411 p = cmap->cmap.data + 16 + 12 * n;
2412 start = TT_NEXT_ULONG( p );
2413 end = TT_NEXT_ULONG( p );
2414 glyph_id = TT_PEEK_ULONG( p );
2416 if ( char_code < start )
2419 if ( char_code <= end )
2421 gindex = (FT_UInt)glyph_id;
2425 cmap->cur_charcode = char_code;;
2426 cmap->cur_gindex = gindex;
2427 cmap->cur_group = n;
2440 tt_cmap13_char_map_binary( TT_CMap cmap,
2441 FT_UInt32* pchar_code,
2445 FT_Byte* p = cmap->data + 12;
2446 FT_UInt32 num_groups = TT_PEEK_ULONG( p );
2447 FT_UInt32 char_code = *pchar_code;
2448 FT_UInt32 start, end;
2449 FT_UInt32 max, min, mid;
2455 /* make compiler happy */
2468 mid = ( min + max ) >> 1;
2469 p = cmap->data + 16 + 12 * mid;
2471 start = TT_NEXT_ULONG( p );
2472 end = TT_NEXT_ULONG( p );
2474 if ( char_code < start )
2476 else if ( char_code > end )
2480 gindex = (FT_UInt)TT_PEEK_ULONG( p );
2488 TT_CMap13 cmap13 = (TT_CMap13)cmap;
2491 /* if `char_code' is not in any group, then `mid' is */
2492 /* the group nearest to `char_code' */
2495 if ( char_code > end )
2498 if ( mid == num_groups )
2503 cmap13->cur_charcode = char_code;
2504 cmap13->cur_group = mid;
2508 tt_cmap13_next( cmap13 );
2510 if ( cmap13->valid )
2511 gindex = cmap13->cur_gindex;
2514 cmap13->cur_gindex = gindex;
2517 *pchar_code = cmap13->cur_charcode;
2524 FT_CALLBACK_DEF( FT_UInt )
2525 tt_cmap13_char_index( TT_CMap cmap,
2526 FT_UInt32 char_code )
2528 return tt_cmap13_char_map_binary( cmap, &char_code, 0 );
2532 FT_CALLBACK_DEF( FT_UInt32 )
2533 tt_cmap13_char_next( TT_CMap cmap,
2534 FT_UInt32 *pchar_code )
2536 TT_CMap13 cmap13 = (TT_CMap13)cmap;
2540 if ( cmap13->cur_charcode >= 0xFFFFFFFFUL )
2543 /* no need to search */
2544 if ( cmap13->valid && cmap13->cur_charcode == *pchar_code )
2546 tt_cmap13_next( cmap13 );
2547 if ( cmap13->valid )
2549 gindex = cmap13->cur_gindex;
2551 *pchar_code = cmap13->cur_charcode;
2557 gindex = tt_cmap13_char_map_binary( cmap, pchar_code, 1 );
2563 FT_CALLBACK_DEF( FT_Error )
2564 tt_cmap13_get_info( TT_CMap cmap,
2565 TT_CMapInfo *cmap_info )
2567 FT_Byte* p = cmap->data + 8;
2570 cmap_info->format = 13;
2571 cmap_info->language = (FT_ULong)TT_PEEK_ULONG( p );
2577 FT_DEFINE_TT_CMAP(tt_cmap13_class_rec,
2578 sizeof ( TT_CMap13Rec ),
2580 (FT_CMap_InitFunc) tt_cmap13_init,
2581 (FT_CMap_DoneFunc) NULL,
2582 (FT_CMap_CharIndexFunc)tt_cmap13_char_index,
2583 (FT_CMap_CharNextFunc) tt_cmap13_char_next,
2585 NULL, NULL, NULL, NULL, NULL
2588 (TT_CMap_ValidateFunc) tt_cmap13_validate,
2589 (TT_CMap_Info_GetFunc) tt_cmap13_get_info
2592 #endif /* TT_CONFIG_CMAP_FORMAT_13 */
2595 /*************************************************************************/
2596 /*************************************************************************/
2598 /***** FORMAT 14 *****/
2600 /*************************************************************************/
2601 /*************************************************************************/
2603 /*************************************************************************/
2605 /* TABLE OVERVIEW */
2606 /* -------------- */
2608 /* NAME OFFSET TYPE DESCRIPTION */
2610 /* format 0 USHORT must be 14 */
2611 /* length 2 ULONG table length in bytes */
2612 /* numSelector 6 ULONG number of variation sel. records */
2614 /* Followed by numSelector records, each of which looks like */
2616 /* varSelector 0 UINT24 Unicode codepoint of sel. */
2617 /* defaultOff 3 ULONG offset to a default UVS table */
2618 /* describing any variants to be found in */
2619 /* the normal Unicode subtable. */
2620 /* nonDefOff 7 ULONG offset to a non-default UVS table */
2621 /* describing any variants not in the */
2622 /* standard cmap, with GIDs here */
2623 /* (either offset may be 0 NULL) */
2625 /* Selectors are sorted by code point. */
2627 /* A default Unicode Variation Selector (UVS) subtable is just a list of */
2628 /* ranges of code points which are to be found in the standard cmap. No */
2629 /* glyph IDs (GIDs) here. */
2631 /* numRanges 0 ULONG number of ranges following */
2633 /* A range looks like */
2635 /* uniStart 0 UINT24 code point of the first character in */
2637 /* additionalCnt 3 UBYTE count of additional characters in this */
2638 /* range (zero means a range of a single */
2641 /* Ranges are sorted by `uniStart'. */
2643 /* A non-default Unicode Variation Selector (UVS) subtable is a list of */
2644 /* mappings from codepoint to GID. */
2646 /* numMappings 0 ULONG number of mappings */
2648 /* A range looks like */
2650 /* uniStart 0 UINT24 code point of the first character in */
2652 /* GID 3 USHORT and its GID */
2654 /* Ranges are sorted by `uniStart'. */
2656 #ifdef TT_CONFIG_CMAP_FORMAT_14
2658 typedef struct TT_CMap14Rec_
2661 FT_ULong num_selectors;
2663 /* This array is used to store the results of various
2664 * cmap 14 query functions. The data is overwritten
2665 * on each call to these functions.
2667 FT_UInt32 max_results;
2671 } TT_CMap14Rec, *TT_CMap14;
2674 FT_CALLBACK_DEF( void )
2675 tt_cmap14_done( TT_CMap14 cmap )
2677 FT_Memory memory = cmap->memory;
2680 cmap->max_results = 0;
2681 if ( memory != NULL && cmap->results != NULL )
2682 FT_FREE( cmap->results );
2687 tt_cmap14_ensure( TT_CMap14 cmap,
2688 FT_UInt32 num_results,
2691 FT_UInt32 old_max = cmap->max_results;
2692 FT_Error error = SFNT_Err_Ok;
2695 if ( num_results > cmap->max_results )
2697 cmap->memory = memory;
2699 if ( FT_QRENEW_ARRAY( cmap->results, old_max, num_results ) )
2702 cmap->max_results = num_results;
2709 FT_CALLBACK_DEF( FT_Error )
2710 tt_cmap14_init( TT_CMap14 cmap,
2713 cmap->cmap.data = table;
2716 cmap->num_selectors = FT_PEEK_ULONG( table );
2717 cmap->max_results = 0;
2718 cmap->results = NULL;
2724 FT_CALLBACK_DEF( FT_Error )
2725 tt_cmap14_validate( FT_Byte* table,
2726 FT_Validator valid )
2728 FT_Byte* p = table + 2;
2729 FT_ULong length = TT_NEXT_ULONG( p );
2730 FT_ULong num_selectors = TT_NEXT_ULONG( p );
2733 if ( length > (FT_ULong)( valid->limit - table ) ||
2734 length < 10 + 11 * num_selectors )
2735 FT_INVALID_TOO_SHORT;
2737 /* check selectors, they must be in increasing order */
2739 /* we start lastVarSel at 1 because a variant selector value of 0
2742 FT_ULong n, lastVarSel = 1;
2745 for ( n = 0; n < num_selectors; n++ )
2747 FT_ULong varSel = TT_NEXT_UINT24( p );
2748 FT_ULong defOff = TT_NEXT_ULONG( p );
2749 FT_ULong nondefOff = TT_NEXT_ULONG( p );
2752 if ( defOff >= length || nondefOff >= length )
2753 FT_INVALID_TOO_SHORT;
2755 if ( varSel < lastVarSel )
2758 lastVarSel = varSel + 1;
2760 /* check the default table (these glyphs should be reached */
2761 /* through the normal Unicode cmap, no GIDs, just check order) */
2764 FT_Byte* defp = table + defOff;
2765 FT_ULong numRanges = TT_NEXT_ULONG( defp );
2767 FT_ULong lastBase = 0;
2770 if ( defp + numRanges * 4 > valid->limit )
2771 FT_INVALID_TOO_SHORT;
2773 for ( i = 0; i < numRanges; ++i )
2775 FT_ULong base = TT_NEXT_UINT24( defp );
2776 FT_ULong cnt = FT_NEXT_BYTE( defp );
2779 if ( base + cnt >= 0x110000UL ) /* end of Unicode */
2782 if ( base < lastBase )
2785 lastBase = base + cnt + 1U;
2789 /* and the non-default table (these glyphs are specified here) */
2790 if ( nondefOff != 0 )
2792 FT_Byte* ndp = table + nondefOff;
2793 FT_ULong numMappings = TT_NEXT_ULONG( ndp );
2794 FT_ULong i, lastUni = 0;
2797 if ( numMappings * 4 > (FT_ULong)( valid->limit - ndp ) )
2798 FT_INVALID_TOO_SHORT;
2800 for ( i = 0; i < numMappings; ++i )
2802 FT_ULong uni = TT_NEXT_UINT24( ndp );
2803 FT_ULong gid = TT_NEXT_USHORT( ndp );
2806 if ( uni >= 0x110000UL ) /* end of Unicode */
2809 if ( uni < lastUni )
2814 if ( valid->level >= FT_VALIDATE_TIGHT &&
2815 gid >= TT_VALID_GLYPH_COUNT( valid ) )
2816 FT_INVALID_GLYPH_ID;
2826 FT_CALLBACK_DEF( FT_UInt )
2827 tt_cmap14_char_index( TT_CMap cmap,
2828 FT_UInt32 char_code )
2831 FT_UNUSED( char_code );
2833 /* This can't happen */
2838 FT_CALLBACK_DEF( FT_UInt32 )
2839 tt_cmap14_char_next( TT_CMap cmap,
2840 FT_UInt32 *pchar_code )
2844 /* This can't happen */
2850 FT_CALLBACK_DEF( FT_Error )
2851 tt_cmap14_get_info( TT_CMap cmap,
2852 TT_CMapInfo *cmap_info )
2856 cmap_info->format = 14;
2857 /* subtable 14 does not define a language field */
2858 cmap_info->language = 0xFFFFFFFFUL;
2865 tt_cmap14_char_map_def_binary( FT_Byte *base,
2866 FT_UInt32 char_code )
2868 FT_UInt32 numRanges = TT_PEEK_ULONG( base );
2880 FT_UInt32 mid = ( min + max ) >> 1;
2881 FT_Byte* p = base + 4 * mid;
2882 FT_ULong start = TT_NEXT_UINT24( p );
2883 FT_UInt cnt = FT_NEXT_BYTE( p );
2886 if ( char_code < start )
2888 else if ( char_code > start+cnt )
2899 tt_cmap14_char_map_nondef_binary( FT_Byte *base,
2900 FT_UInt32 char_code )
2902 FT_UInt32 numMappings = TT_PEEK_ULONG( base );
2914 FT_UInt32 mid = ( min + max ) >> 1;
2915 FT_Byte* p = base + 5 * mid;
2916 FT_UInt32 uni = (FT_UInt32)TT_NEXT_UINT24( p );
2919 if ( char_code < uni )
2921 else if ( char_code > uni )
2924 return TT_PEEK_USHORT( p );
2932 tt_cmap14_find_variant( FT_Byte *base,
2933 FT_UInt32 variantCode )
2935 FT_UInt32 numVar = TT_PEEK_ULONG( base );
2947 FT_UInt32 mid = ( min + max ) >> 1;
2948 FT_Byte* p = base + 11 * mid;
2949 FT_ULong varSel = TT_NEXT_UINT24( p );
2952 if ( variantCode < varSel )
2954 else if ( variantCode > varSel )
2964 FT_CALLBACK_DEF( FT_UInt )
2965 tt_cmap14_char_var_index( TT_CMap cmap,
2968 FT_UInt32 variantSelector)
2970 FT_Byte* p = tt_cmap14_find_variant( cmap->data + 6, variantSelector );
2978 defOff = TT_NEXT_ULONG( p );
2979 nondefOff = TT_PEEK_ULONG( p );
2982 tt_cmap14_char_map_def_binary( cmap->data + defOff, charcode ) )
2984 /* This is the default variant of this charcode. GID not stored */
2985 /* here; stored in the normal Unicode charmap instead. */
2986 return ucmap->cmap.clazz->char_index( &ucmap->cmap, charcode );
2989 if ( nondefOff != 0 )
2990 return tt_cmap14_char_map_nondef_binary( cmap->data + nondefOff,
2997 FT_CALLBACK_DEF( FT_Int )
2998 tt_cmap14_char_var_isdefault( TT_CMap cmap,
3000 FT_UInt32 variantSelector )
3002 FT_Byte* p = tt_cmap14_find_variant( cmap->data + 6, variantSelector );
3010 defOff = TT_NEXT_ULONG( p );
3011 nondefOff = TT_NEXT_ULONG( p );
3014 tt_cmap14_char_map_def_binary( cmap->data + defOff, charcode ) )
3017 if ( nondefOff != 0 &&
3018 tt_cmap14_char_map_nondef_binary( cmap->data + nondefOff,
3026 FT_CALLBACK_DEF( FT_UInt32* )
3027 tt_cmap14_variants( TT_CMap cmap,
3030 TT_CMap14 cmap14 = (TT_CMap14)cmap;
3031 FT_UInt32 count = cmap14->num_selectors;
3032 FT_Byte* p = cmap->data + 10;
3037 if ( tt_cmap14_ensure( cmap14, ( count + 1 ), memory ) )
3040 result = cmap14->results;
3041 for ( i = 0; i < count; ++i )
3043 result[i] = (FT_UInt32)TT_NEXT_UINT24( p );
3052 FT_CALLBACK_DEF( FT_UInt32 * )
3053 tt_cmap14_char_variants( TT_CMap cmap,
3055 FT_UInt32 charCode )
3057 TT_CMap14 cmap14 = (TT_CMap14) cmap;
3058 FT_UInt32 count = cmap14->num_selectors;
3059 FT_Byte* p = cmap->data + 10;
3063 if ( tt_cmap14_ensure( cmap14, ( count + 1 ), memory ) )
3066 for ( q = cmap14->results; count > 0; --count )
3068 FT_UInt32 varSel = TT_NEXT_UINT24( p );
3069 FT_ULong defOff = TT_NEXT_ULONG( p );
3070 FT_ULong nondefOff = TT_NEXT_ULONG( p );
3073 if ( ( defOff != 0 &&
3074 tt_cmap14_char_map_def_binary( cmap->data + defOff,
3077 tt_cmap14_char_map_nondef_binary( cmap->data + nondefOff,
3086 return cmap14->results;
3091 tt_cmap14_def_char_count( FT_Byte *p )
3093 FT_UInt32 numRanges = (FT_UInt32)TT_NEXT_ULONG( p );
3097 p += 3; /* point to the first `cnt' field */
3098 for ( ; numRanges > 0; numRanges-- )
3109 tt_cmap14_get_def_chars( TT_CMap cmap,
3113 TT_CMap14 cmap14 = (TT_CMap14) cmap;
3114 FT_UInt32 numRanges;
3119 cnt = tt_cmap14_def_char_count( p );
3120 numRanges = (FT_UInt32)TT_NEXT_ULONG( p );
3122 if ( tt_cmap14_ensure( cmap14, ( cnt + 1 ), memory ) )
3125 for ( q = cmap14->results; numRanges > 0; --numRanges )
3127 FT_UInt32 uni = (FT_UInt32)TT_NEXT_UINT24( p );
3130 cnt = FT_NEXT_BYTE( p ) + 1;
3136 } while ( --cnt != 0 );
3140 return cmap14->results;
3145 tt_cmap14_get_nondef_chars( TT_CMap cmap,
3149 TT_CMap14 cmap14 = (TT_CMap14) cmap;
3150 FT_UInt32 numMappings;
3155 numMappings = (FT_UInt32)TT_NEXT_ULONG( p );
3157 if ( tt_cmap14_ensure( cmap14, ( numMappings + 1 ), memory ) )
3160 ret = cmap14->results;
3161 for ( i = 0; i < numMappings; ++i )
3163 ret[i] = (FT_UInt32)TT_NEXT_UINT24( p );
3172 FT_CALLBACK_DEF( FT_UInt32 * )
3173 tt_cmap14_variant_chars( TT_CMap cmap,
3175 FT_UInt32 variantSelector )
3177 FT_Byte *p = tt_cmap14_find_variant( cmap->data + 6,
3188 defOff = TT_NEXT_ULONG( p );
3189 nondefOff = TT_NEXT_ULONG( p );
3191 if ( defOff == 0 && nondefOff == 0 )
3195 return tt_cmap14_get_nondef_chars( cmap, cmap->data + nondefOff,
3197 else if ( nondefOff == 0 )
3198 return tt_cmap14_get_def_chars( cmap, cmap->data + defOff,
3202 /* Both a default and a non-default glyph set? That's probably not */
3203 /* good font design, but the spec allows for it... */
3204 TT_CMap14 cmap14 = (TT_CMap14) cmap;
3205 FT_UInt32 numRanges;
3206 FT_UInt32 numMappings;
3214 p = cmap->data + nondefOff;
3215 dp = cmap->data + defOff;
3217 numMappings = (FT_UInt32)TT_NEXT_ULONG( p );
3218 dcnt = tt_cmap14_def_char_count( dp );
3219 numRanges = (FT_UInt32)TT_NEXT_ULONG( dp );
3221 if ( numMappings == 0 )
3222 return tt_cmap14_get_def_chars( cmap, cmap->data + defOff,
3225 return tt_cmap14_get_nondef_chars( cmap, cmap->data + nondefOff,
3228 if ( tt_cmap14_ensure( cmap14, ( dcnt + numMappings + 1 ), memory ) )
3231 ret = cmap14->results;
3232 duni = (FT_UInt32)TT_NEXT_UINT24( dp );
3233 dcnt = FT_NEXT_BYTE( dp );
3235 nuni = (FT_UInt32)TT_NEXT_UINT24( p );
3242 if ( nuni > duni + dcnt )
3244 for ( k = 0; k <= dcnt; ++k )
3245 ret[i++] = duni + k;
3249 if ( di > numRanges )
3252 duni = (FT_UInt32)TT_NEXT_UINT24( dp );
3253 dcnt = FT_NEXT_BYTE( dp );
3259 /* If it is within the default range then ignore it -- */
3260 /* that should not have happened */
3262 if ( ni > numMappings )
3265 nuni = (FT_UInt32)TT_NEXT_UINT24( p );
3270 if ( ni <= numMappings )
3272 /* If we get here then we have run out of all default ranges. */
3273 /* We have read one non-default mapping which we haven't stored */
3274 /* and there may be others that need to be read. */
3276 while ( ni < numMappings )
3278 ret[i++] = (FT_UInt32)TT_NEXT_UINT24( p );
3283 else if ( di <= numRanges )
3285 /* If we get here then we have run out of all non-default */
3286 /* mappings. We have read one default range which we haven't */
3287 /* stored and there may be others that need to be read. */
3288 for ( k = 0; k <= dcnt; ++k )
3289 ret[i++] = duni + k;
3291 while ( di < numRanges )
3293 duni = (FT_UInt32)TT_NEXT_UINT24( dp );
3294 dcnt = FT_NEXT_BYTE( dp );
3296 for ( k = 0; k <= dcnt; ++k )
3297 ret[i++] = duni + k;
3309 FT_DEFINE_TT_CMAP(tt_cmap14_class_rec,
3310 sizeof ( TT_CMap14Rec ),
3312 (FT_CMap_InitFunc) tt_cmap14_init,
3313 (FT_CMap_DoneFunc) tt_cmap14_done,
3314 (FT_CMap_CharIndexFunc)tt_cmap14_char_index,
3315 (FT_CMap_CharNextFunc) tt_cmap14_char_next,
3317 /* Format 14 extension functions */
3318 (FT_CMap_CharVarIndexFunc) tt_cmap14_char_var_index,
3319 (FT_CMap_CharVarIsDefaultFunc)tt_cmap14_char_var_isdefault,
3320 (FT_CMap_VariantListFunc) tt_cmap14_variants,
3321 (FT_CMap_CharVariantListFunc) tt_cmap14_char_variants,
3322 (FT_CMap_VariantCharListFunc) tt_cmap14_variant_chars
3325 (TT_CMap_ValidateFunc)tt_cmap14_validate,
3326 (TT_CMap_Info_GetFunc)tt_cmap14_get_info
3329 #endif /* TT_CONFIG_CMAP_FORMAT_14 */
3332 #ifndef FT_CONFIG_OPTION_PIC
3334 static const TT_CMap_Class tt_cmap_classes[] =
3336 #define TTCMAPCITEM(a) &a,
3337 #include "ttcmapc.h"
3341 #else /*FT_CONFIG_OPTION_PIC*/
3343 void FT_Destroy_Class_tt_cmap_classes(FT_Library library, TT_CMap_Class* clazz)
3345 FT_Memory memory = library->memory;
3350 FT_Error FT_Create_Class_tt_cmap_classes(FT_Library library, TT_CMap_Class** output_class)
3352 TT_CMap_Class* clazz;
3353 TT_CMap_ClassRec* recs;
3355 FT_Memory memory = library->memory;
3358 #define TTCMAPCITEM(a) i++;
3359 #include "ttcmapc.h"
3361 /* allocate enough space for both the pointers +terminator and the class instances */
3362 if ( FT_ALLOC( clazz, sizeof(*clazz)*(i+1)+sizeof(TT_CMap_ClassRec)*i ) )
3365 /* the location of the class instances follows the array of pointers */
3366 recs = (TT_CMap_ClassRec*) (((char*)clazz)+(sizeof(*clazz)*(i+1)));
3370 #define TTCMAPCITEM(a) \
3371 FT_Init_Class_##a(&recs[i]); \
3372 clazz[i] = &recs[i]; \
3374 #include "ttcmapc.h"
3378 *output_class = clazz;
3382 #endif /*FT_CONFIG_OPTION_PIC*/
3385 /* parse the `cmap' table and build the corresponding TT_CMap objects */
3386 /* in the current face */
3388 FT_LOCAL_DEF( FT_Error )
3389 tt_face_build_cmaps( TT_Face face )
3391 FT_Byte* table = face->cmap_table;
3392 FT_Byte* limit = table + face->cmap_size;
3393 FT_UInt volatile num_cmaps;
3394 FT_Byte* volatile p = table;
3395 FT_Library library = FT_FACE_LIBRARY( face );
3397 FT_UNUSED( library );
3400 if ( !p || p + 4 > limit )
3401 return SFNT_Err_Invalid_Table;
3403 /* only recognize format 0 */
3404 if ( TT_NEXT_USHORT( p ) != 0 )
3407 FT_ERROR(( "tt_face_build_cmaps:"
3408 " unsupported `cmap' table format = %d\n",
3409 TT_PEEK_USHORT( p ) ));
3410 return SFNT_Err_Invalid_Table;
3413 num_cmaps = TT_NEXT_USHORT( p );
3414 #ifdef FT_MAX_CHARMAP_CACHEABLE
3415 if ( num_cmaps > FT_MAX_CHARMAP_CACHEABLE )
3416 FT_ERROR(( "tt_face_build_cmaps: too many cmap subtables(%d) "
3417 "subtable#%d and later are loaded but cannot be searched\n",
3418 num_cmaps, FT_MAX_CHARMAP_CACHEABLE + 1 ));
3421 for ( ; num_cmaps > 0 && p + 8 <= limit; num_cmaps-- )
3423 FT_CharMapRec charmap;
3427 charmap.platform_id = TT_NEXT_USHORT( p );
3428 charmap.encoding_id = TT_NEXT_USHORT( p );
3429 charmap.face = FT_FACE( face );
3430 charmap.encoding = FT_ENCODING_NONE; /* will be filled later */
3431 offset = TT_NEXT_ULONG( p );
3433 if ( offset && offset <= face->cmap_size - 2 )
3435 FT_Byte* volatile cmap = table + offset;
3436 volatile FT_UInt format = TT_PEEK_USHORT( cmap );
3437 const TT_CMap_Class* volatile pclazz = FT_TT_CMAP_CLASSES_GET;
3438 TT_CMap_Class volatile clazz;
3441 for ( ; *pclazz; pclazz++ )
3444 if ( clazz->format == format )
3446 volatile TT_ValidatorRec valid;
3447 volatile FT_Error error = SFNT_Err_Ok;
3450 ft_validator_init( FT_VALIDATOR( &valid ), cmap, limit,
3451 FT_VALIDATE_DEFAULT );
3453 valid.num_glyphs = (FT_UInt)face->max_profile.numGlyphs;
3456 *((ft_jmp_buf*)&FT_VALIDATOR( &valid )->jump_buffer) ) == 0 )
3458 /* validate this cmap sub-table */
3459 error = clazz->validate( cmap, FT_VALIDATOR( &valid ) );
3462 if ( valid.validator.error == 0 )
3467 /* It might make sense to store the single variation selector */
3468 /* cmap somewhere special. But it would have to be in the */
3469 /* public FT_FaceRec, and we can't change that. */
3471 if ( !FT_CMap_New( (FT_CMap_Class)clazz,
3472 cmap, &charmap, &ttcmap ) )
3474 /* it is simpler to directly set `flags' than adding */
3475 /* a parameter to FT_CMap_New */
3476 ((TT_CMap)ttcmap)->flags = (FT_Int)error;
3481 FT_TRACE0(( "tt_face_build_cmaps:"
3482 " broken cmap sub-table ignored\n" ));
3488 if ( *pclazz == NULL )
3490 FT_TRACE0(( "tt_face_build_cmaps:"
3491 " unsupported cmap sub-table ignored\n" ));
3500 FT_LOCAL( FT_Error )
3501 tt_get_cmap_info( FT_CharMap charmap,
3502 TT_CMapInfo *cmap_info )
3504 FT_CMap cmap = (FT_CMap)charmap;
3505 TT_CMap_Class clazz = (TT_CMap_Class)cmap->clazz;
3508 return clazz->get_cmap_info( charmap, cmap_info );