1 /***************************************************************************/
5 /* TrueType character mapping table (cmap) support (body). */
7 /* Copyright 2002-2010, 2012, 2013 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
);
170 sizeof ( TT_CMapRec
),
172 (FT_CMap_InitFunc
) tt_cmap_init
,
173 (FT_CMap_DoneFunc
) NULL
,
174 (FT_CMap_CharIndexFunc
)tt_cmap0_char_index
,
175 (FT_CMap_CharNextFunc
) tt_cmap0_char_next
,
184 (TT_CMap_ValidateFunc
)tt_cmap0_validate
,
185 (TT_CMap_Info_GetFunc
)tt_cmap0_get_info
)
187 #endif /* TT_CONFIG_CMAP_FORMAT_0 */
190 /*************************************************************************/
191 /*************************************************************************/
193 /***** FORMAT 2 *****/
195 /***** This is used for certain CJK encodings that encode text in a *****/
196 /***** mixed 8/16 bits encoding along the following lines: *****/
198 /***** * Certain byte values correspond to an 8-bit character code *****/
199 /***** (typically in the range 0..127 for ASCII compatibility). *****/
201 /***** * Certain byte values signal the first byte of a 2-byte *****/
202 /***** character code (but these values are also valid as the *****/
203 /***** second byte of a 2-byte character). *****/
205 /***** The following charmap lookup and iteration functions all *****/
206 /***** assume that the value "charcode" correspond to following: *****/
208 /***** - For one byte characters, "charcode" is simply the *****/
209 /***** character code. *****/
211 /***** - For two byte characters, "charcode" is the 2-byte *****/
212 /***** character code in big endian format. More exactly: *****/
214 /***** (charcode >> 8) is the first byte value *****/
215 /***** (charcode & 0xFF) is the second byte value *****/
217 /***** Note that not all values of "charcode" are valid according *****/
218 /***** to these rules, and the function moderately check the *****/
219 /***** arguments. *****/
221 /*************************************************************************/
222 /*************************************************************************/
224 /*************************************************************************/
229 /* NAME OFFSET TYPE DESCRIPTION */
231 /* format 0 USHORT must be 2 */
232 /* length 2 USHORT table length in bytes */
233 /* language 4 USHORT Mac language code */
234 /* keys 6 USHORT[256] sub-header keys */
235 /* subs 518 SUBHEAD[NSUBS] sub-headers array */
236 /* glyph_ids 518+NSUB*8 USHORT[] glyph ID array */
238 /* The `keys' table is used to map charcode high-bytes to sub-headers. */
239 /* The value of `NSUBS' is the number of sub-headers defined in the */
240 /* table and is computed by finding the maximum of the `keys' table. */
242 /* Note that for any n, `keys[n]' is a byte offset within the `subs' */
243 /* table, i.e., it is the corresponding sub-header index multiplied */
246 /* Each sub-header has the following format: */
248 /* NAME OFFSET TYPE DESCRIPTION */
250 /* first 0 USHORT first valid low-byte */
251 /* count 2 USHORT number of valid low-bytes */
252 /* delta 4 SHORT see below */
253 /* offset 6 USHORT see below */
255 /* A sub-header defines, for each high-byte, the range of valid */
256 /* low-bytes within the charmap. Note that the range defined by `first' */
257 /* and `count' must be completely included in the interval [0..255] */
258 /* according to the specification. */
260 /* If a character code is contained within a given sub-header, then */
261 /* mapping it to a glyph index is done as follows: */
263 /* * The value of `offset' is read. This is a _byte_ distance from the */
264 /* location of the `offset' field itself into a slice of the */
265 /* `glyph_ids' table. Let's call it `slice' (it is a USHORT[] too). */
267 /* * The value `slice[char.lo - first]' is read. If it is 0, there is */
268 /* no glyph for the charcode. Otherwise, the value of `delta' is */
269 /* added to it (modulo 65536) to form a new glyph index. */
271 /* It is up to the validation routine to check that all offsets fall */
272 /* within the glyph IDs table (and not within the `subs' table itself or */
273 /* outside of the CMap). */
276 #ifdef TT_CONFIG_CMAP_FORMAT_2
278 FT_CALLBACK_DEF( FT_Error
)
279 tt_cmap2_validate( FT_Byte
* table
,
282 FT_Byte
* p
= table
+ 2; /* skip format */
283 FT_UInt length
= TT_PEEK_USHORT( p
);
285 FT_Byte
* keys
; /* keys table */
286 FT_Byte
* subs
; /* sub-headers */
287 FT_Byte
* glyph_ids
; /* glyph ID array */
290 if ( table
+ length
> valid
->limit
|| length
< 6 + 512 )
291 FT_INVALID_TOO_SHORT
;
295 /* parse keys to compute sub-headers count */
298 for ( n
= 0; n
< 256; n
++ )
300 FT_UInt idx
= TT_NEXT_USHORT( p
);
303 /* value must be multiple of 8 */
304 if ( valid
->level
>= FT_VALIDATE_PARANOID
&& ( idx
& 7 ) != 0 )
309 if ( idx
> max_subs
)
313 FT_ASSERT( p
== table
+ 518 );
316 glyph_ids
= subs
+ (max_subs
+ 1) * 8;
317 if ( glyph_ids
> valid
->limit
)
318 FT_INVALID_TOO_SHORT
;
320 /* parse sub-headers */
321 for ( n
= 0; n
<= max_subs
; n
++ )
323 FT_UInt first_code
, code_count
, offset
;
327 first_code
= TT_NEXT_USHORT( p
);
328 code_count
= TT_NEXT_USHORT( p
);
329 delta
= TT_NEXT_SHORT( p
);
330 offset
= TT_NEXT_USHORT( p
);
332 /* many Dynalab fonts have empty sub-headers */
333 if ( code_count
== 0 )
336 /* check range within 0..255 */
337 if ( valid
->level
>= FT_VALIDATE_PARANOID
)
339 if ( first_code
>= 256 || first_code
+ code_count
> 256 )
349 ids
= p
- 2 + offset
;
350 if ( ids
< glyph_ids
|| ids
+ code_count
*2 > table
+ length
)
353 /* check glyph IDs */
354 if ( valid
->level
>= FT_VALIDATE_TIGHT
)
356 FT_Byte
* limit
= p
+ code_count
* 2;
362 idx
= TT_NEXT_USHORT( p
);
365 idx
= ( idx
+ delta
) & 0xFFFFU
;
366 if ( idx
>= TT_VALID_GLYPH_COUNT( valid
) )
378 /* return sub header corresponding to a given character code */
379 /* NULL on invalid charcode */
381 tt_cmap2_get_subheader( FT_Byte
* table
,
382 FT_UInt32 char_code
)
384 FT_Byte
* result
= NULL
;
387 if ( char_code
< 0x10000UL
)
389 FT_UInt char_lo
= (FT_UInt
)( char_code
& 0xFF );
390 FT_UInt char_hi
= (FT_UInt
)( char_code
>> 8 );
391 FT_Byte
* p
= table
+ 6; /* keys table */
392 FT_Byte
* subs
= table
+ 518; /* subheaders table */
398 /* an 8-bit character code -- we use subHeader 0 in this case */
399 /* to test whether the character code is in the charmap */
401 sub
= subs
; /* jump to first sub-header */
403 /* check that the sub-header for this byte is 0, which */
404 /* indicates that it is really a valid one-byte value */
405 /* Otherwise, return 0 */
408 if ( TT_PEEK_USHORT( p
) != 0 )
413 /* a 16-bit character code */
415 /* jump to key entry */
417 /* jump to sub-header */
418 sub
= subs
+ ( FT_PAD_FLOOR( TT_PEEK_USHORT( p
), 8 ) );
420 /* check that the high byte isn't a valid one-byte value */
431 FT_CALLBACK_DEF( FT_UInt
)
432 tt_cmap2_char_index( TT_CMap cmap
,
433 FT_UInt32 char_code
)
435 FT_Byte
* table
= cmap
->data
;
440 subheader
= tt_cmap2_get_subheader( table
, char_code
);
443 FT_Byte
* p
= subheader
;
444 FT_UInt idx
= (FT_UInt
)(char_code
& 0xFF);
445 FT_UInt start
, count
;
450 start
= TT_NEXT_USHORT( p
);
451 count
= TT_NEXT_USHORT( p
);
452 delta
= TT_NEXT_SHORT ( p
);
453 offset
= TT_PEEK_USHORT( p
);
456 if ( idx
< count
&& offset
!= 0 )
458 p
+= offset
+ 2 * idx
;
459 idx
= TT_PEEK_USHORT( p
);
462 result
= (FT_UInt
)( idx
+ delta
) & 0xFFFFU
;
469 FT_CALLBACK_DEF( FT_UInt32
)
470 tt_cmap2_char_next( TT_CMap cmap
,
471 FT_UInt32
*pcharcode
)
473 FT_Byte
* table
= cmap
->data
;
475 FT_UInt32 result
= 0;
476 FT_UInt32 charcode
= *pcharcode
+ 1;
480 while ( charcode
< 0x10000UL
)
482 subheader
= tt_cmap2_get_subheader( table
, charcode
);
485 FT_Byte
* p
= subheader
;
486 FT_UInt start
= TT_NEXT_USHORT( p
);
487 FT_UInt count
= TT_NEXT_USHORT( p
);
488 FT_Int delta
= TT_NEXT_SHORT ( p
);
489 FT_UInt offset
= TT_PEEK_USHORT( p
);
490 FT_UInt char_lo
= (FT_UInt
)( charcode
& 0xFF );
497 if ( char_lo
< start
)
503 pos
= (FT_UInt
)( char_lo
- start
);
505 p
+= offset
+ pos
* 2;
506 charcode
= FT_PAD_FLOOR( charcode
, 256 ) + char_lo
;
508 for ( ; pos
< count
; pos
++, charcode
++ )
510 idx
= TT_NEXT_USHORT( p
);
514 gindex
= ( idx
+ delta
) & 0xFFFFU
;
524 /* jump to next sub-header, i.e. higher byte value */
526 charcode
= FT_PAD_FLOOR( charcode
, 256 ) + 256;
536 FT_CALLBACK_DEF( FT_Error
)
537 tt_cmap2_get_info( TT_CMap cmap
,
538 TT_CMapInfo
*cmap_info
)
540 FT_Byte
* p
= cmap
->data
+ 4;
543 cmap_info
->format
= 2;
544 cmap_info
->language
= (FT_ULong
)TT_PEEK_USHORT( p
);
552 sizeof ( TT_CMapRec
),
554 (FT_CMap_InitFunc
) tt_cmap_init
,
555 (FT_CMap_DoneFunc
) NULL
,
556 (FT_CMap_CharIndexFunc
)tt_cmap2_char_index
,
557 (FT_CMap_CharNextFunc
) tt_cmap2_char_next
,
566 (TT_CMap_ValidateFunc
)tt_cmap2_validate
,
567 (TT_CMap_Info_GetFunc
)tt_cmap2_get_info
)
569 #endif /* TT_CONFIG_CMAP_FORMAT_2 */
572 /*************************************************************************/
573 /*************************************************************************/
575 /***** FORMAT 4 *****/
577 /*************************************************************************/
578 /*************************************************************************/
580 /*************************************************************************/
585 /* NAME OFFSET TYPE DESCRIPTION */
587 /* format 0 USHORT must be 4 */
588 /* length 2 USHORT table length */
590 /* language 4 USHORT Mac language code */
592 /* segCountX2 6 USHORT 2*NUM_SEGS */
593 /* searchRange 8 USHORT 2*(1 << LOG_SEGS) */
594 /* entrySelector 10 USHORT LOG_SEGS */
595 /* rangeShift 12 USHORT segCountX2 - */
598 /* endCount 14 USHORT[NUM_SEGS] end charcode for */
599 /* each segment; last */
602 /* pad 14+NUM_SEGS*2 USHORT padding */
604 /* startCount 16+NUM_SEGS*2 USHORT[NUM_SEGS] first charcode for */
607 /* idDelta 16+NUM_SEGS*4 SHORT[NUM_SEGS] delta for each */
609 /* idOffset 16+NUM_SEGS*6 SHORT[NUM_SEGS] range offset for */
610 /* each segment; can be */
613 /* glyphIds 16+NUM_SEGS*8 USHORT[] array of glyph ID */
616 /* Character codes are modelled by a series of ordered (increasing) */
617 /* intervals called segments. Each segment has start and end codes, */
618 /* provided by the `startCount' and `endCount' arrays. Segments must */
619 /* not overlap, and the last segment should always contain the value */
620 /* 0xFFFF for `endCount'. */
622 /* The fields `searchRange', `entrySelector' and `rangeShift' are better */
623 /* ignored (they are traces of over-engineering in the TrueType */
624 /* specification). */
626 /* Each segment also has a signed `delta', as well as an optional offset */
627 /* within the `glyphIds' table. */
629 /* If a segment's idOffset is 0, the glyph index corresponding to any */
630 /* charcode within the segment is obtained by adding the value of */
631 /* `idDelta' directly to the charcode, modulo 65536. */
633 /* Otherwise, a glyph index is taken from the glyph IDs sub-array for */
634 /* the segment, and the value of `idDelta' is added to it. */
637 /* Finally, note that a lot of fonts contain an invalid last segment, */
638 /* where `start' and `end' are correctly set to 0xFFFF but both `delta' */
639 /* and `offset' are incorrect (e.g., `opens___.ttf' which comes with */
640 /* OpenOffice.org). We need special code to deal with them correctly. */
643 #ifdef TT_CONFIG_CMAP_FORMAT_4
645 typedef struct TT_CMap4Rec_
648 FT_UInt32 cur_charcode
; /* current charcode */
649 FT_UInt cur_gindex
; /* current glyph index */
658 } TT_CMap4Rec
, *TT_CMap4
;
661 FT_CALLBACK_DEF( FT_Error
)
662 tt_cmap4_init( TT_CMap4 cmap
,
668 cmap
->cmap
.data
= table
;
671 cmap
->num_ranges
= FT_PEEK_USHORT( p
) >> 1;
672 cmap
->cur_charcode
= (FT_UInt32
)0xFFFFFFFFUL
;
673 cmap
->cur_gindex
= 0;
680 tt_cmap4_set_range( TT_CMap4 cmap
,
681 FT_UInt range_index
)
683 FT_Byte
* table
= cmap
->cmap
.data
;
685 FT_UInt num_ranges
= cmap
->num_ranges
;
688 while ( range_index
< num_ranges
)
693 p
= table
+ 14 + range_index
* 2;
694 cmap
->cur_end
= FT_PEEK_USHORT( p
);
696 p
+= 2 + num_ranges
* 2;
697 cmap
->cur_start
= FT_PEEK_USHORT( p
);
700 cmap
->cur_delta
= FT_PEEK_SHORT( p
);
703 offset
= FT_PEEK_USHORT( p
);
705 /* some fonts have an incorrect last segment; */
706 /* we have to catch it */
707 if ( range_index
>= num_ranges
- 1 &&
708 cmap
->cur_start
== 0xFFFFU
&&
709 cmap
->cur_end
== 0xFFFFU
)
711 TT_Face face
= (TT_Face
)cmap
->cmap
.cmap
.charmap
.face
;
712 FT_Byte
* limit
= face
->cmap_table
+ face
->cmap_size
;
715 if ( offset
&& p
+ offset
+ 2 > limit
)
722 if ( offset
!= 0xFFFFU
)
724 cmap
->cur_values
= offset
? p
+ offset
: NULL
;
725 cmap
->cur_range
= range_index
;
729 /* we skip empty segments */
737 /* search the index of the charcode next to cmap->cur_charcode; */
738 /* caller should call tt_cmap4_set_range with proper range */
739 /* before calling this function */
742 tt_cmap4_next( TT_CMap4 cmap
)
747 if ( cmap
->cur_charcode
>= 0xFFFFUL
)
750 charcode
= (FT_UInt
)cmap
->cur_charcode
+ 1;
752 if ( charcode
< cmap
->cur_start
)
753 charcode
= cmap
->cur_start
;
757 FT_Byte
* values
= cmap
->cur_values
;
758 FT_UInt end
= cmap
->cur_end
;
759 FT_Int delta
= cmap
->cur_delta
;
762 if ( charcode
<= end
)
766 FT_Byte
* p
= values
+ 2 * ( charcode
- cmap
->cur_start
);
771 FT_UInt gindex
= FT_NEXT_USHORT( p
);
776 gindex
= (FT_UInt
)( ( gindex
+ delta
) & 0xFFFFU
);
779 cmap
->cur_charcode
= charcode
;
780 cmap
->cur_gindex
= gindex
;
784 } while ( ++charcode
<= end
);
790 FT_UInt gindex
= (FT_UInt
)( ( charcode
+ delta
) & 0xFFFFU
);
795 cmap
->cur_charcode
= charcode
;
796 cmap
->cur_gindex
= gindex
;
799 } while ( ++charcode
<= end
);
803 /* we need to find another range */
804 if ( tt_cmap4_set_range( cmap
, cmap
->cur_range
+ 1 ) < 0 )
807 if ( charcode
< cmap
->cur_start
)
808 charcode
= cmap
->cur_start
;
812 cmap
->cur_charcode
= (FT_UInt32
)0xFFFFFFFFUL
;
813 cmap
->cur_gindex
= 0;
817 FT_CALLBACK_DEF( FT_Error
)
818 tt_cmap4_validate( FT_Byte
* table
,
821 FT_Byte
* p
= table
+ 2; /* skip format */
822 FT_UInt length
= TT_NEXT_USHORT( p
);
823 FT_Byte
*ends
, *starts
, *offsets
, *deltas
, *glyph_ids
;
825 FT_Error error
= FT_Err_Ok
;
829 FT_INVALID_TOO_SHORT
;
831 /* in certain fonts, the `length' field is invalid and goes */
832 /* out of bound. We try to correct this here... */
833 if ( table
+ length
> valid
->limit
)
835 if ( valid
->level
>= FT_VALIDATE_TIGHT
)
836 FT_INVALID_TOO_SHORT
;
838 length
= (FT_UInt
)( valid
->limit
- table
);
842 num_segs
= TT_NEXT_USHORT( p
); /* read segCountX2 */
844 if ( valid
->level
>= FT_VALIDATE_PARANOID
)
846 /* check that we have an even value here */
853 if ( length
< 16 + num_segs
* 2 * 4 )
854 FT_INVALID_TOO_SHORT
;
856 /* check the search parameters - even though we never use them */
858 if ( valid
->level
>= FT_VALIDATE_PARANOID
)
860 /* check the values of `searchRange', `entrySelector', `rangeShift' */
861 FT_UInt search_range
= TT_NEXT_USHORT( p
);
862 FT_UInt entry_selector
= TT_NEXT_USHORT( p
);
863 FT_UInt range_shift
= TT_NEXT_USHORT( p
);
866 if ( ( search_range
| range_shift
) & 1 ) /* must be even values */
872 /* `search range' is the greatest power of 2 that is <= num_segs */
874 if ( search_range
> num_segs
||
875 search_range
* 2 < num_segs
||
876 search_range
+ range_shift
!= num_segs
||
877 search_range
!= ( 1U << entry_selector
) )
882 starts
= table
+ 16 + num_segs
* 2;
883 deltas
= starts
+ num_segs
* 2;
884 offsets
= deltas
+ num_segs
* 2;
885 glyph_ids
= offsets
+ num_segs
* 2;
887 /* check last segment; its end count value must be 0xFFFF */
888 if ( valid
->level
>= FT_VALIDATE_PARANOID
)
890 p
= ends
+ ( num_segs
- 1 ) * 2;
891 if ( TT_PEEK_USHORT( p
) != 0xFFFFU
)
896 FT_UInt start
, end
, offset
, n
;
897 FT_UInt last_start
= 0, last_end
= 0;
899 FT_Byte
* p_start
= starts
;
900 FT_Byte
* p_end
= ends
;
901 FT_Byte
* p_delta
= deltas
;
902 FT_Byte
* p_offset
= offsets
;
905 for ( n
= 0; n
< num_segs
; n
++ )
908 start
= TT_NEXT_USHORT( p_start
);
909 end
= TT_NEXT_USHORT( p_end
);
910 delta
= TT_NEXT_SHORT( p_delta
);
911 offset
= TT_NEXT_USHORT( p_offset
);
916 /* this test should be performed at default validation level; */
917 /* unfortunately, some popular Asian fonts have overlapping */
918 /* ranges in their charmaps */
920 if ( start
<= last_end
&& n
> 0 )
922 if ( valid
->level
>= FT_VALIDATE_TIGHT
)
926 /* allow overlapping segments, provided their start points */
927 /* and end points, respectively, are in ascending order */
929 if ( last_start
> start
|| last_end
> end
)
930 error
|= TT_CMAP_FLAG_UNSORTED
;
932 error
|= TT_CMAP_FLAG_OVERLAPPING
;
936 if ( offset
&& offset
!= 0xFFFFU
)
938 p
+= offset
; /* start of glyph ID array */
940 /* check that we point within the glyph IDs table only */
941 if ( valid
->level
>= FT_VALIDATE_TIGHT
)
943 if ( p
< glyph_ids
||
944 p
+ ( end
- start
+ 1 ) * 2 > table
+ length
)
947 /* Some fonts handle the last segment incorrectly. In */
948 /* theory, 0xFFFF might point to an ordinary glyph -- */
949 /* a cmap 4 is versatile and could be used for any */
950 /* encoding, not only Unicode. However, reality shows */
951 /* that far too many fonts are sloppy and incorrectly */
952 /* set all fields but `start' and `end' for the last */
953 /* segment if it contains only a single character. */
955 /* We thus omit the test here, delaying it to the */
956 /* routines which actually access the cmap. */
957 else if ( n
!= num_segs
- 1 ||
958 !( start
== 0xFFFFU
&& end
== 0xFFFFU
) )
960 if ( p
< glyph_ids
||
961 p
+ ( end
- start
+ 1 ) * 2 > valid
->limit
)
965 /* check glyph indices within the segment range */
966 if ( valid
->level
>= FT_VALIDATE_TIGHT
)
971 for ( i
= start
; i
< end
; i
++ )
973 idx
= FT_NEXT_USHORT( p
);
976 idx
= (FT_UInt
)( idx
+ delta
) & 0xFFFFU
;
978 if ( idx
>= TT_VALID_GLYPH_COUNT( valid
) )
984 else if ( offset
== 0xFFFFU
)
986 /* some fonts (erroneously?) use a range offset of 0xFFFF */
987 /* to mean missing glyph in cmap table */
989 if ( valid
->level
>= FT_VALIDATE_PARANOID
||
991 !( start
== 0xFFFFU
&& end
== 0xFFFFU
) )
1005 tt_cmap4_char_map_linear( TT_CMap cmap
,
1006 FT_UInt32
* pcharcode
,
1009 FT_UInt num_segs2
, start
, end
, offset
;
1011 FT_UInt i
, num_segs
;
1012 FT_UInt32 charcode
= *pcharcode
;
1018 num_segs2
= FT_PAD_FLOOR( TT_PEEK_USHORT( p
), 2 );
1020 num_segs
= num_segs2
>> 1;
1029 for ( ; charcode
<= 0xFFFFU
; charcode
++ )
1034 p
= cmap
->data
+ 14; /* ends table */
1035 q
= cmap
->data
+ 16 + num_segs2
; /* starts table */
1037 for ( i
= 0; i
< num_segs
; i
++ )
1039 end
= TT_NEXT_USHORT( p
);
1040 start
= TT_NEXT_USHORT( q
);
1042 if ( charcode
>= start
&& charcode
<= end
)
1044 p
= q
- 2 + num_segs2
;
1045 delta
= TT_PEEK_SHORT( p
);
1047 offset
= TT_PEEK_USHORT( p
);
1049 /* some fonts have an incorrect last segment; */
1050 /* we have to catch it */
1051 if ( i
>= num_segs
- 1 &&
1052 start
== 0xFFFFU
&& end
== 0xFFFFU
)
1054 TT_Face face
= (TT_Face
)cmap
->cmap
.charmap
.face
;
1055 FT_Byte
* limit
= face
->cmap_table
+ face
->cmap_size
;
1058 if ( offset
&& p
+ offset
+ 2 > limit
)
1065 if ( offset
== 0xFFFFU
)
1070 p
+= offset
+ ( charcode
- start
) * 2;
1071 gindex
= TT_PEEK_USHORT( p
);
1073 gindex
= (FT_UInt
)( gindex
+ delta
) & 0xFFFFU
;
1076 gindex
= (FT_UInt
)( charcode
+ delta
) & 0xFFFFU
;
1082 if ( !next
|| gindex
)
1086 if ( next
&& gindex
)
1087 *pcharcode
= charcode
;
1094 tt_cmap4_char_map_binary( TT_CMap cmap
,
1095 FT_UInt32
* pcharcode
,
1098 FT_UInt num_segs2
, start
, end
, offset
;
1100 FT_UInt max
, min
, mid
, num_segs
;
1101 FT_UInt charcode
= (FT_UInt
)*pcharcode
;
1107 num_segs2
= FT_PAD_FLOOR( TT_PEEK_USHORT( p
), 2 );
1112 num_segs
= num_segs2
>> 1;
1114 /* make compiler happy */
1127 mid
= ( min
+ max
) >> 1;
1128 p
= cmap
->data
+ 14 + mid
* 2;
1129 end
= TT_PEEK_USHORT( p
);
1131 start
= TT_PEEK_USHORT( p
);
1133 if ( charcode
< start
)
1135 else if ( charcode
> end
)
1140 delta
= TT_PEEK_SHORT( p
);
1142 offset
= TT_PEEK_USHORT( p
);
1144 /* some fonts have an incorrect last segment; */
1145 /* we have to catch it */
1146 if ( mid
>= num_segs
- 1 &&
1147 start
== 0xFFFFU
&& end
== 0xFFFFU
)
1149 TT_Face face
= (TT_Face
)cmap
->cmap
.charmap
.face
;
1150 FT_Byte
* limit
= face
->cmap_table
+ face
->cmap_size
;
1153 if ( offset
&& p
+ offset
+ 2 > limit
)
1160 /* search the first segment containing `charcode' */
1161 if ( cmap
->flags
& TT_CMAP_FLAG_OVERLAPPING
)
1166 /* call the current segment `max' */
1169 if ( offset
== 0xFFFFU
)
1172 /* search in segments before the current segment */
1173 for ( i
= max
; i
> 0; i
-- )
1180 p
= cmap
->data
+ 14 + ( i
- 1 ) * 2;
1181 prev_end
= TT_PEEK_USHORT( p
);
1183 if ( charcode
> prev_end
)
1191 start
= TT_PEEK_USHORT( p
);
1193 delta
= TT_PEEK_SHORT( p
);
1195 offset
= TT_PEEK_USHORT( p
);
1197 if ( offset
!= 0xFFFFU
)
1202 if ( mid
== max
+ 1 )
1206 p
= cmap
->data
+ 14 + max
* 2;
1207 end
= TT_PEEK_USHORT( p
);
1209 start
= TT_PEEK_USHORT( p
);
1211 delta
= TT_PEEK_SHORT( p
);
1213 offset
= TT_PEEK_USHORT( p
);
1218 /* search in segments after the current segment */
1219 for ( i
= max
+ 1; i
< num_segs
; i
++ )
1221 FT_UInt next_end
, next_start
;
1224 p
= cmap
->data
+ 14 + i
* 2;
1225 next_end
= TT_PEEK_USHORT( p
);
1227 next_start
= TT_PEEK_USHORT( p
);
1229 if ( charcode
< next_start
)
1235 delta
= TT_PEEK_SHORT( p
);
1237 offset
= TT_PEEK_USHORT( p
);
1239 if ( offset
!= 0xFFFFU
)
1253 /* end, start, delta, and offset are for the i'th segment */
1256 p
= cmap
->data
+ 14 + mid
* 2;
1257 end
= TT_PEEK_USHORT( p
);
1259 start
= TT_PEEK_USHORT( p
);
1261 delta
= TT_PEEK_SHORT( p
);
1263 offset
= TT_PEEK_USHORT( p
);
1268 if ( offset
== 0xFFFFU
)
1274 p
+= offset
+ ( charcode
- start
) * 2;
1275 gindex
= TT_PEEK_USHORT( p
);
1277 gindex
= (FT_UInt
)( gindex
+ delta
) & 0xFFFFU
;
1280 gindex
= (FT_UInt
)( charcode
+ delta
) & 0xFFFFU
;
1288 TT_CMap4 cmap4
= (TT_CMap4
)cmap
;
1291 /* if `charcode' is not in any segment, then `mid' is */
1292 /* the segment nearest to `charcode' */
1295 if ( charcode
> end
)
1298 if ( mid
== num_segs
)
1302 if ( tt_cmap4_set_range( cmap4
, mid
) )
1305 *pcharcode
= charcode
;
1309 cmap4
->cur_charcode
= charcode
;
1312 cmap4
->cur_gindex
= gindex
;
1315 cmap4
->cur_charcode
= charcode
;
1316 tt_cmap4_next( cmap4
);
1317 gindex
= cmap4
->cur_gindex
;
1321 *pcharcode
= cmap4
->cur_charcode
;
1329 FT_CALLBACK_DEF( FT_UInt
)
1330 tt_cmap4_char_index( TT_CMap cmap
,
1331 FT_UInt32 char_code
)
1333 if ( char_code
>= 0x10000UL
)
1336 if ( cmap
->flags
& TT_CMAP_FLAG_UNSORTED
)
1337 return tt_cmap4_char_map_linear( cmap
, &char_code
, 0 );
1339 return tt_cmap4_char_map_binary( cmap
, &char_code
, 0 );
1343 FT_CALLBACK_DEF( FT_UInt32
)
1344 tt_cmap4_char_next( TT_CMap cmap
,
1345 FT_UInt32
*pchar_code
)
1350 if ( *pchar_code
>= 0xFFFFU
)
1353 if ( cmap
->flags
& TT_CMAP_FLAG_UNSORTED
)
1354 gindex
= tt_cmap4_char_map_linear( cmap
, pchar_code
, 1 );
1357 TT_CMap4 cmap4
= (TT_CMap4
)cmap
;
1360 /* no need to search */
1361 if ( *pchar_code
== cmap4
->cur_charcode
)
1363 tt_cmap4_next( cmap4
);
1364 gindex
= cmap4
->cur_gindex
;
1366 *pchar_code
= cmap4
->cur_charcode
;
1369 gindex
= tt_cmap4_char_map_binary( cmap
, pchar_code
, 1 );
1376 FT_CALLBACK_DEF( FT_Error
)
1377 tt_cmap4_get_info( TT_CMap cmap
,
1378 TT_CMapInfo
*cmap_info
)
1380 FT_Byte
* p
= cmap
->data
+ 4;
1383 cmap_info
->format
= 4;
1384 cmap_info
->language
= (FT_ULong
)TT_PEEK_USHORT( p
);
1392 sizeof ( TT_CMap4Rec
),
1393 (FT_CMap_InitFunc
) tt_cmap4_init
,
1394 (FT_CMap_DoneFunc
) NULL
,
1395 (FT_CMap_CharIndexFunc
)tt_cmap4_char_index
,
1396 (FT_CMap_CharNextFunc
) tt_cmap4_char_next
,
1405 (TT_CMap_ValidateFunc
)tt_cmap4_validate
,
1406 (TT_CMap_Info_GetFunc
)tt_cmap4_get_info
)
1408 #endif /* TT_CONFIG_CMAP_FORMAT_4 */
1411 /*************************************************************************/
1412 /*************************************************************************/
1414 /***** FORMAT 6 *****/
1416 /*************************************************************************/
1417 /*************************************************************************/
1419 /*************************************************************************/
1421 /* TABLE OVERVIEW */
1422 /* -------------- */
1424 /* NAME OFFSET TYPE DESCRIPTION */
1426 /* format 0 USHORT must be 4 */
1427 /* length 2 USHORT table length in bytes */
1428 /* language 4 USHORT Mac language code */
1430 /* first 6 USHORT first segment code */
1431 /* count 8 USHORT segment size in chars */
1432 /* glyphIds 10 USHORT[count] glyph IDs */
1434 /* A very simplified segment mapping. */
1437 #ifdef TT_CONFIG_CMAP_FORMAT_6
1439 FT_CALLBACK_DEF( FT_Error
)
1440 tt_cmap6_validate( FT_Byte
* table
,
1441 FT_Validator valid
)
1444 FT_UInt length
, count
;
1447 if ( table
+ 10 > valid
->limit
)
1448 FT_INVALID_TOO_SHORT
;
1451 length
= TT_NEXT_USHORT( p
);
1453 p
= table
+ 8; /* skip language and start index */
1454 count
= TT_NEXT_USHORT( p
);
1456 if ( table
+ length
> valid
->limit
|| length
< 10 + count
* 2 )
1457 FT_INVALID_TOO_SHORT
;
1459 /* check glyph indices */
1460 if ( valid
->level
>= FT_VALIDATE_TIGHT
)
1465 for ( ; count
> 0; count
-- )
1467 gindex
= TT_NEXT_USHORT( p
);
1468 if ( gindex
>= TT_VALID_GLYPH_COUNT( valid
) )
1469 FT_INVALID_GLYPH_ID
;
1477 FT_CALLBACK_DEF( FT_UInt
)
1478 tt_cmap6_char_index( TT_CMap cmap
,
1479 FT_UInt32 char_code
)
1481 FT_Byte
* table
= cmap
->data
;
1483 FT_Byte
* p
= table
+ 6;
1484 FT_UInt start
= TT_NEXT_USHORT( p
);
1485 FT_UInt count
= TT_NEXT_USHORT( p
);
1486 FT_UInt idx
= (FT_UInt
)( char_code
- start
);
1492 result
= TT_PEEK_USHORT( p
);
1498 FT_CALLBACK_DEF( FT_UInt32
)
1499 tt_cmap6_char_next( TT_CMap cmap
,
1500 FT_UInt32
*pchar_code
)
1502 FT_Byte
* table
= cmap
->data
;
1503 FT_UInt32 result
= 0;
1504 FT_UInt32 char_code
= *pchar_code
+ 1;
1507 FT_Byte
* p
= table
+ 6;
1508 FT_UInt start
= TT_NEXT_USHORT( p
);
1509 FT_UInt count
= TT_NEXT_USHORT( p
);
1513 if ( char_code
>= 0x10000UL
)
1516 if ( char_code
< start
)
1519 idx
= (FT_UInt
)( char_code
- start
);
1522 for ( ; idx
< count
; idx
++ )
1524 gindex
= TT_NEXT_USHORT( p
);
1534 *pchar_code
= result
;
1539 FT_CALLBACK_DEF( FT_Error
)
1540 tt_cmap6_get_info( TT_CMap cmap
,
1541 TT_CMapInfo
*cmap_info
)
1543 FT_Byte
* p
= cmap
->data
+ 4;
1546 cmap_info
->format
= 6;
1547 cmap_info
->language
= (FT_ULong
)TT_PEEK_USHORT( p
);
1555 sizeof ( TT_CMapRec
),
1557 (FT_CMap_InitFunc
) tt_cmap_init
,
1558 (FT_CMap_DoneFunc
) NULL
,
1559 (FT_CMap_CharIndexFunc
)tt_cmap6_char_index
,
1560 (FT_CMap_CharNextFunc
) tt_cmap6_char_next
,
1569 (TT_CMap_ValidateFunc
)tt_cmap6_validate
,
1570 (TT_CMap_Info_GetFunc
)tt_cmap6_get_info
)
1572 #endif /* TT_CONFIG_CMAP_FORMAT_6 */
1575 /*************************************************************************/
1576 /*************************************************************************/
1578 /***** FORMAT 8 *****/
1580 /***** It is hard to completely understand what the OpenType spec *****/
1581 /***** says about this format, but here is my conclusion. *****/
1583 /***** The purpose of this format is to easily map UTF-16 text to *****/
1584 /***** glyph indices. Basically, the `char_code' must be in one of *****/
1585 /***** the following formats: *****/
1587 /***** - A 16-bit value that isn't part of the Unicode Surrogates *****/
1588 /***** Area (i.e. U+D800-U+DFFF). *****/
1590 /***** - A 32-bit value, made of two surrogate values, i.e.. if *****/
1591 /***** `char_code = (char_hi << 16) | char_lo', then both *****/
1592 /***** `char_hi' and `char_lo' must be in the Surrogates Area. *****/
1595 /***** The `is32' table embedded in the charmap indicates whether a *****/
1596 /***** given 16-bit value is in the surrogates area or not. *****/
1598 /***** So, for any given `char_code', we can assert the following: *****/
1600 /***** If `char_hi == 0' then we must have `is32[char_lo] == 0'. *****/
1602 /***** If `char_hi != 0' then we must have both *****/
1603 /***** `is32[char_hi] != 0' and `is32[char_lo] != 0'. *****/
1605 /*************************************************************************/
1606 /*************************************************************************/
1608 /*************************************************************************/
1610 /* TABLE OVERVIEW */
1611 /* -------------- */
1613 /* NAME OFFSET TYPE DESCRIPTION */
1615 /* format 0 USHORT must be 8 */
1616 /* reserved 2 USHORT reserved */
1617 /* length 4 ULONG length in bytes */
1618 /* language 8 ULONG Mac language code */
1619 /* is32 12 BYTE[8192] 32-bitness bitmap */
1620 /* count 8204 ULONG number of groups */
1622 /* This header is followed by `count' groups of the following format: */
1624 /* start 0 ULONG first charcode */
1625 /* end 4 ULONG last charcode */
1626 /* startId 8 ULONG start glyph ID for the group */
1629 #ifdef TT_CONFIG_CMAP_FORMAT_8
1631 FT_CALLBACK_DEF( FT_Error
)
1632 tt_cmap8_validate( FT_Byte
* table
,
1633 FT_Validator valid
)
1635 FT_Byte
* p
= table
+ 4;
1638 FT_UInt32 num_groups
;
1641 if ( table
+ 16 + 8192 > valid
->limit
)
1642 FT_INVALID_TOO_SHORT
;
1644 length
= TT_NEXT_ULONG( p
);
1645 if ( length
> (FT_UInt32
)( valid
->limit
- table
) || length
< 8192 + 16 )
1646 FT_INVALID_TOO_SHORT
;
1649 p
= is32
+ 8192; /* skip `is32' array */
1650 num_groups
= TT_NEXT_ULONG( p
);
1652 if ( p
+ num_groups
* 12 > valid
->limit
)
1653 FT_INVALID_TOO_SHORT
;
1655 /* check groups, they must be in increasing order */
1657 FT_UInt32 n
, start
, end
, start_id
, count
, last
= 0;
1660 for ( n
= 0; n
< num_groups
; n
++ )
1665 start
= TT_NEXT_ULONG( p
);
1666 end
= TT_NEXT_ULONG( p
);
1667 start_id
= TT_NEXT_ULONG( p
);
1672 if ( n
> 0 && start
<= last
)
1675 if ( valid
->level
>= FT_VALIDATE_TIGHT
)
1677 if ( start_id
+ end
- start
>= TT_VALID_GLYPH_COUNT( valid
) )
1678 FT_INVALID_GLYPH_ID
;
1680 count
= (FT_UInt32
)( end
- start
+ 1 );
1682 if ( start
& ~0xFFFFU
)
1684 /* start_hi != 0; check that is32[i] is 1 for each i in */
1685 /* the `hi' and `lo' of the range [start..end] */
1686 for ( ; count
> 0; count
--, start
++ )
1688 hi
= (FT_UInt
)( start
>> 16 );
1689 lo
= (FT_UInt
)( start
& 0xFFFFU
);
1691 if ( (is32
[hi
>> 3] & ( 0x80 >> ( hi
& 7 ) ) ) == 0 )
1694 if ( (is32
[lo
>> 3] & ( 0x80 >> ( lo
& 7 ) ) ) == 0 )
1700 /* start_hi == 0; check that is32[i] is 0 for each i in */
1701 /* the range [start..end] */
1703 /* end_hi cannot be != 0! */
1704 if ( end
& ~0xFFFFU
)
1707 for ( ; count
> 0; count
--, start
++ )
1709 lo
= (FT_UInt
)( start
& 0xFFFFU
);
1711 if ( (is32
[lo
>> 3] & ( 0x80 >> ( lo
& 7 ) ) ) != 0 )
1725 FT_CALLBACK_DEF( FT_UInt
)
1726 tt_cmap8_char_index( TT_CMap cmap
,
1727 FT_UInt32 char_code
)
1729 FT_Byte
* table
= cmap
->data
;
1731 FT_Byte
* p
= table
+ 8204;
1732 FT_UInt32 num_groups
= TT_NEXT_ULONG( p
);
1733 FT_UInt32 start
, end
, start_id
;
1736 for ( ; num_groups
> 0; num_groups
-- )
1738 start
= TT_NEXT_ULONG( p
);
1739 end
= TT_NEXT_ULONG( p
);
1740 start_id
= TT_NEXT_ULONG( p
);
1742 if ( char_code
< start
)
1745 if ( char_code
<= end
)
1747 result
= (FT_UInt
)( start_id
+ char_code
- start
);
1755 FT_CALLBACK_DEF( FT_UInt32
)
1756 tt_cmap8_char_next( TT_CMap cmap
,
1757 FT_UInt32
*pchar_code
)
1759 FT_UInt32 result
= 0;
1760 FT_UInt32 char_code
= *pchar_code
+ 1;
1762 FT_Byte
* table
= cmap
->data
;
1763 FT_Byte
* p
= table
+ 8204;
1764 FT_UInt32 num_groups
= TT_NEXT_ULONG( p
);
1765 FT_UInt32 start
, end
, start_id
;
1770 for ( ; num_groups
> 0; num_groups
-- )
1772 start
= TT_NEXT_ULONG( p
);
1773 end
= TT_NEXT_ULONG( p
);
1774 start_id
= TT_NEXT_ULONG( p
);
1776 if ( char_code
< start
)
1779 if ( char_code
<= end
)
1781 gindex
= (FT_UInt
)( char_code
- start
+ start_id
);
1791 *pchar_code
= result
;
1796 FT_CALLBACK_DEF( FT_Error
)
1797 tt_cmap8_get_info( TT_CMap cmap
,
1798 TT_CMapInfo
*cmap_info
)
1800 FT_Byte
* p
= cmap
->data
+ 8;
1803 cmap_info
->format
= 8;
1804 cmap_info
->language
= (FT_ULong
)TT_PEEK_ULONG( p
);
1812 sizeof ( TT_CMapRec
),
1814 (FT_CMap_InitFunc
) tt_cmap_init
,
1815 (FT_CMap_DoneFunc
) NULL
,
1816 (FT_CMap_CharIndexFunc
)tt_cmap8_char_index
,
1817 (FT_CMap_CharNextFunc
) tt_cmap8_char_next
,
1826 (TT_CMap_ValidateFunc
)tt_cmap8_validate
,
1827 (TT_CMap_Info_GetFunc
)tt_cmap8_get_info
)
1829 #endif /* TT_CONFIG_CMAP_FORMAT_8 */
1832 /*************************************************************************/
1833 /*************************************************************************/
1835 /***** FORMAT 10 *****/
1837 /*************************************************************************/
1838 /*************************************************************************/
1840 /*************************************************************************/
1842 /* TABLE OVERVIEW */
1843 /* -------------- */
1845 /* NAME OFFSET TYPE DESCRIPTION */
1847 /* format 0 USHORT must be 10 */
1848 /* reserved 2 USHORT reserved */
1849 /* length 4 ULONG length in bytes */
1850 /* language 8 ULONG Mac language code */
1852 /* start 12 ULONG first char in range */
1853 /* count 16 ULONG number of chars in range */
1854 /* glyphIds 20 USHORT[count] glyph indices covered */
1857 #ifdef TT_CONFIG_CMAP_FORMAT_10
1859 FT_CALLBACK_DEF( FT_Error
)
1860 tt_cmap10_validate( FT_Byte
* table
,
1861 FT_Validator valid
)
1863 FT_Byte
* p
= table
+ 4;
1864 FT_ULong length
, count
;
1867 if ( table
+ 20 > valid
->limit
)
1868 FT_INVALID_TOO_SHORT
;
1870 length
= TT_NEXT_ULONG( p
);
1872 count
= TT_NEXT_ULONG( p
);
1874 if ( length
> (FT_ULong
)( valid
->limit
- table
) ||
1875 length
< 20 + count
* 2 )
1876 FT_INVALID_TOO_SHORT
;
1878 /* check glyph indices */
1879 if ( valid
->level
>= FT_VALIDATE_TIGHT
)
1884 for ( ; count
> 0; count
-- )
1886 gindex
= TT_NEXT_USHORT( p
);
1887 if ( gindex
>= TT_VALID_GLYPH_COUNT( valid
) )
1888 FT_INVALID_GLYPH_ID
;
1896 FT_CALLBACK_DEF( FT_UInt
)
1897 tt_cmap10_char_index( TT_CMap cmap
,
1898 FT_UInt32 char_code
)
1900 FT_Byte
* table
= cmap
->data
;
1902 FT_Byte
* p
= table
+ 12;
1903 FT_UInt32 start
= TT_NEXT_ULONG( p
);
1904 FT_UInt32 count
= TT_NEXT_ULONG( p
);
1905 FT_UInt32 idx
= (FT_ULong
)( char_code
- start
);
1911 result
= TT_PEEK_USHORT( p
);
1917 FT_CALLBACK_DEF( FT_UInt32
)
1918 tt_cmap10_char_next( TT_CMap cmap
,
1919 FT_UInt32
*pchar_code
)
1921 FT_Byte
* table
= cmap
->data
;
1922 FT_UInt32 char_code
= *pchar_code
+ 1;
1924 FT_Byte
* p
= table
+ 12;
1925 FT_UInt32 start
= TT_NEXT_ULONG( p
);
1926 FT_UInt32 count
= TT_NEXT_ULONG( p
);
1930 if ( char_code
< start
)
1933 idx
= (FT_UInt32
)( char_code
- start
);
1936 for ( ; idx
< count
; idx
++ )
1938 gindex
= TT_NEXT_USHORT( p
);
1944 *pchar_code
= char_code
;
1949 FT_CALLBACK_DEF( FT_Error
)
1950 tt_cmap10_get_info( TT_CMap cmap
,
1951 TT_CMapInfo
*cmap_info
)
1953 FT_Byte
* p
= cmap
->data
+ 8;
1956 cmap_info
->format
= 10;
1957 cmap_info
->language
= (FT_ULong
)TT_PEEK_ULONG( p
);
1964 tt_cmap10_class_rec
,
1965 sizeof ( TT_CMapRec
),
1967 (FT_CMap_InitFunc
) tt_cmap_init
,
1968 (FT_CMap_DoneFunc
) NULL
,
1969 (FT_CMap_CharIndexFunc
)tt_cmap10_char_index
,
1970 (FT_CMap_CharNextFunc
) tt_cmap10_char_next
,
1979 (TT_CMap_ValidateFunc
)tt_cmap10_validate
,
1980 (TT_CMap_Info_GetFunc
)tt_cmap10_get_info
)
1982 #endif /* TT_CONFIG_CMAP_FORMAT_10 */
1985 /*************************************************************************/
1986 /*************************************************************************/
1988 /***** FORMAT 12 *****/
1990 /*************************************************************************/
1991 /*************************************************************************/
1993 /*************************************************************************/
1995 /* TABLE OVERVIEW */
1996 /* -------------- */
1998 /* NAME OFFSET TYPE DESCRIPTION */
2000 /* format 0 USHORT must be 12 */
2001 /* reserved 2 USHORT reserved */
2002 /* length 4 ULONG length in bytes */
2003 /* language 8 ULONG Mac language code */
2004 /* count 12 ULONG number of groups */
2007 /* This header is followed by `count' groups of the following format: */
2009 /* start 0 ULONG first charcode */
2010 /* end 4 ULONG last charcode */
2011 /* startId 8 ULONG start glyph ID for the group */
2014 #ifdef TT_CONFIG_CMAP_FORMAT_12
2016 typedef struct TT_CMap12Rec_
2020 FT_ULong cur_charcode
;
2023 FT_ULong num_groups
;
2025 } TT_CMap12Rec
, *TT_CMap12
;
2028 FT_CALLBACK_DEF( FT_Error
)
2029 tt_cmap12_init( TT_CMap12 cmap
,
2032 cmap
->cmap
.data
= table
;
2035 cmap
->num_groups
= FT_PEEK_ULONG( table
);
2043 FT_CALLBACK_DEF( FT_Error
)
2044 tt_cmap12_validate( FT_Byte
* table
,
2045 FT_Validator valid
)
2049 FT_ULong num_groups
;
2052 if ( table
+ 16 > valid
->limit
)
2053 FT_INVALID_TOO_SHORT
;
2056 length
= TT_NEXT_ULONG( p
);
2059 num_groups
= TT_NEXT_ULONG( p
);
2061 if ( length
> (FT_ULong
)( valid
->limit
- table
) ||
2062 length
< 16 + 12 * num_groups
)
2063 FT_INVALID_TOO_SHORT
;
2065 /* check groups, they must be in increasing order */
2067 FT_ULong n
, start
, end
, start_id
, last
= 0;
2070 for ( n
= 0; n
< num_groups
; n
++ )
2072 start
= TT_NEXT_ULONG( p
);
2073 end
= TT_NEXT_ULONG( p
);
2074 start_id
= TT_NEXT_ULONG( p
);
2079 if ( n
> 0 && start
<= last
)
2082 if ( valid
->level
>= FT_VALIDATE_TIGHT
)
2084 if ( start_id
+ end
- start
>= TT_VALID_GLYPH_COUNT( valid
) )
2085 FT_INVALID_GLYPH_ID
;
2096 /* search the index of the charcode next to cmap->cur_charcode */
2097 /* cmap->cur_group should be set up properly by caller */
2100 tt_cmap12_next( TT_CMap12 cmap
)
2103 FT_ULong start
, end
, start_id
, char_code
;
2108 if ( cmap
->cur_charcode
>= 0xFFFFFFFFUL
)
2111 char_code
= cmap
->cur_charcode
+ 1;
2113 n
= cmap
->cur_group
;
2115 for ( n
= cmap
->cur_group
; n
< cmap
->num_groups
; n
++ )
2117 p
= cmap
->cmap
.data
+ 16 + 12 * n
;
2118 start
= TT_NEXT_ULONG( p
);
2119 end
= TT_NEXT_ULONG( p
);
2120 start_id
= TT_PEEK_ULONG( p
);
2122 if ( char_code
< start
)
2125 for ( ; char_code
<= end
; char_code
++ )
2127 gindex
= (FT_UInt
)( start_id
+ char_code
- start
);
2131 cmap
->cur_charcode
= char_code
;;
2132 cmap
->cur_gindex
= gindex
;
2133 cmap
->cur_group
= n
;
2146 tt_cmap12_char_map_binary( TT_CMap cmap
,
2147 FT_UInt32
* pchar_code
,
2151 FT_Byte
* p
= cmap
->data
+ 12;
2152 FT_UInt32 num_groups
= TT_PEEK_ULONG( p
);
2153 FT_UInt32 char_code
= *pchar_code
;
2154 FT_UInt32 start
, end
, start_id
;
2155 FT_UInt32 max
, min
, mid
;
2161 /* make compiler happy */
2174 mid
= ( min
+ max
) >> 1;
2175 p
= cmap
->data
+ 16 + 12 * mid
;
2177 start
= TT_NEXT_ULONG( p
);
2178 end
= TT_NEXT_ULONG( p
);
2180 if ( char_code
< start
)
2182 else if ( char_code
> end
)
2186 start_id
= TT_PEEK_ULONG( p
);
2187 gindex
= (FT_UInt
)( start_id
+ char_code
- start
);
2195 TT_CMap12 cmap12
= (TT_CMap12
)cmap
;
2198 /* if `char_code' is not in any group, then `mid' is */
2199 /* the group nearest to `char_code' */
2202 if ( char_code
> end
)
2205 if ( mid
== num_groups
)
2210 cmap12
->cur_charcode
= char_code
;
2211 cmap12
->cur_group
= mid
;
2215 tt_cmap12_next( cmap12
);
2217 if ( cmap12
->valid
)
2218 gindex
= cmap12
->cur_gindex
;
2221 cmap12
->cur_gindex
= gindex
;
2224 *pchar_code
= cmap12
->cur_charcode
;
2231 FT_CALLBACK_DEF( FT_UInt
)
2232 tt_cmap12_char_index( TT_CMap cmap
,
2233 FT_UInt32 char_code
)
2235 return tt_cmap12_char_map_binary( cmap
, &char_code
, 0 );
2239 FT_CALLBACK_DEF( FT_UInt32
)
2240 tt_cmap12_char_next( TT_CMap cmap
,
2241 FT_UInt32
*pchar_code
)
2243 TT_CMap12 cmap12
= (TT_CMap12
)cmap
;
2247 if ( cmap12
->cur_charcode
>= 0xFFFFFFFFUL
)
2250 /* no need to search */
2251 if ( cmap12
->valid
&& cmap12
->cur_charcode
== *pchar_code
)
2253 tt_cmap12_next( cmap12
);
2254 if ( cmap12
->valid
)
2256 gindex
= cmap12
->cur_gindex
;
2258 /* XXX: check cur_charcode overflow is expected */
2260 *pchar_code
= (FT_UInt32
)cmap12
->cur_charcode
;
2266 gindex
= tt_cmap12_char_map_binary( cmap
, pchar_code
, 1 );
2268 /* XXX: check gindex overflow is expected */
2269 return (FT_UInt32
)gindex
;
2273 FT_CALLBACK_DEF( FT_Error
)
2274 tt_cmap12_get_info( TT_CMap cmap
,
2275 TT_CMapInfo
*cmap_info
)
2277 FT_Byte
* p
= cmap
->data
+ 8;
2280 cmap_info
->format
= 12;
2281 cmap_info
->language
= (FT_ULong
)TT_PEEK_ULONG( p
);
2288 tt_cmap12_class_rec
,
2289 sizeof ( TT_CMap12Rec
),
2291 (FT_CMap_InitFunc
) tt_cmap12_init
,
2292 (FT_CMap_DoneFunc
) NULL
,
2293 (FT_CMap_CharIndexFunc
)tt_cmap12_char_index
,
2294 (FT_CMap_CharNextFunc
) tt_cmap12_char_next
,
2303 (TT_CMap_ValidateFunc
)tt_cmap12_validate
,
2304 (TT_CMap_Info_GetFunc
)tt_cmap12_get_info
)
2306 #endif /* TT_CONFIG_CMAP_FORMAT_12 */
2309 /*************************************************************************/
2310 /*************************************************************************/
2312 /***** FORMAT 13 *****/
2314 /*************************************************************************/
2315 /*************************************************************************/
2317 /*************************************************************************/
2319 /* TABLE OVERVIEW */
2320 /* -------------- */
2322 /* NAME OFFSET TYPE DESCRIPTION */
2324 /* format 0 USHORT must be 13 */
2325 /* reserved 2 USHORT reserved */
2326 /* length 4 ULONG length in bytes */
2327 /* language 8 ULONG Mac language code */
2328 /* count 12 ULONG number of groups */
2331 /* This header is followed by `count' groups of the following format: */
2333 /* start 0 ULONG first charcode */
2334 /* end 4 ULONG last charcode */
2335 /* glyphId 8 ULONG glyph ID for the whole group */
2338 #ifdef TT_CONFIG_CMAP_FORMAT_13
2340 typedef struct TT_CMap13Rec_
2344 FT_ULong cur_charcode
;
2347 FT_ULong num_groups
;
2349 } TT_CMap13Rec
, *TT_CMap13
;
2352 FT_CALLBACK_DEF( FT_Error
)
2353 tt_cmap13_init( TT_CMap13 cmap
,
2356 cmap
->cmap
.data
= table
;
2359 cmap
->num_groups
= FT_PEEK_ULONG( table
);
2367 FT_CALLBACK_DEF( FT_Error
)
2368 tt_cmap13_validate( FT_Byte
* table
,
2369 FT_Validator valid
)
2373 FT_ULong num_groups
;
2376 if ( table
+ 16 > valid
->limit
)
2377 FT_INVALID_TOO_SHORT
;
2380 length
= TT_NEXT_ULONG( p
);
2383 num_groups
= TT_NEXT_ULONG( p
);
2385 if ( length
> (FT_ULong
)( valid
->limit
- table
) ||
2386 length
< 16 + 12 * num_groups
)
2387 FT_INVALID_TOO_SHORT
;
2389 /* check groups, they must be in increasing order */
2391 FT_ULong n
, start
, end
, glyph_id
, last
= 0;
2394 for ( n
= 0; n
< num_groups
; n
++ )
2396 start
= TT_NEXT_ULONG( p
);
2397 end
= TT_NEXT_ULONG( p
);
2398 glyph_id
= TT_NEXT_ULONG( p
);
2403 if ( n
> 0 && start
<= last
)
2406 if ( valid
->level
>= FT_VALIDATE_TIGHT
)
2408 if ( glyph_id
>= TT_VALID_GLYPH_COUNT( valid
) )
2409 FT_INVALID_GLYPH_ID
;
2420 /* search the index of the charcode next to cmap->cur_charcode */
2421 /* cmap->cur_group should be set up properly by caller */
2424 tt_cmap13_next( TT_CMap13 cmap
)
2427 FT_ULong start
, end
, glyph_id
, char_code
;
2432 if ( cmap
->cur_charcode
>= 0xFFFFFFFFUL
)
2435 char_code
= cmap
->cur_charcode
+ 1;
2437 n
= cmap
->cur_group
;
2439 for ( n
= cmap
->cur_group
; n
< cmap
->num_groups
; n
++ )
2441 p
= cmap
->cmap
.data
+ 16 + 12 * n
;
2442 start
= TT_NEXT_ULONG( p
);
2443 end
= TT_NEXT_ULONG( p
);
2444 glyph_id
= TT_PEEK_ULONG( p
);
2446 if ( char_code
< start
)
2449 if ( char_code
<= end
)
2451 gindex
= (FT_UInt
)glyph_id
;
2455 cmap
->cur_charcode
= char_code
;;
2456 cmap
->cur_gindex
= gindex
;
2457 cmap
->cur_group
= n
;
2470 tt_cmap13_char_map_binary( TT_CMap cmap
,
2471 FT_UInt32
* pchar_code
,
2475 FT_Byte
* p
= cmap
->data
+ 12;
2476 FT_UInt32 num_groups
= TT_PEEK_ULONG( p
);
2477 FT_UInt32 char_code
= *pchar_code
;
2478 FT_UInt32 start
, end
;
2479 FT_UInt32 max
, min
, mid
;
2485 /* make compiler happy */
2498 mid
= ( min
+ max
) >> 1;
2499 p
= cmap
->data
+ 16 + 12 * mid
;
2501 start
= TT_NEXT_ULONG( p
);
2502 end
= TT_NEXT_ULONG( p
);
2504 if ( char_code
< start
)
2506 else if ( char_code
> end
)
2510 gindex
= (FT_UInt
)TT_PEEK_ULONG( p
);
2518 TT_CMap13 cmap13
= (TT_CMap13
)cmap
;
2521 /* if `char_code' is not in any group, then `mid' is */
2522 /* the group nearest to `char_code' */
2524 if ( char_code
> end
)
2527 if ( mid
== num_groups
)
2532 cmap13
->cur_charcode
= char_code
;
2533 cmap13
->cur_group
= mid
;
2537 tt_cmap13_next( cmap13
);
2539 if ( cmap13
->valid
)
2540 gindex
= cmap13
->cur_gindex
;
2543 cmap13
->cur_gindex
= gindex
;
2546 *pchar_code
= cmap13
->cur_charcode
;
2553 FT_CALLBACK_DEF( FT_UInt
)
2554 tt_cmap13_char_index( TT_CMap cmap
,
2555 FT_UInt32 char_code
)
2557 return tt_cmap13_char_map_binary( cmap
, &char_code
, 0 );
2561 FT_CALLBACK_DEF( FT_UInt32
)
2562 tt_cmap13_char_next( TT_CMap cmap
,
2563 FT_UInt32
*pchar_code
)
2565 TT_CMap13 cmap13
= (TT_CMap13
)cmap
;
2569 if ( cmap13
->cur_charcode
>= 0xFFFFFFFFUL
)
2572 /* no need to search */
2573 if ( cmap13
->valid
&& cmap13
->cur_charcode
== *pchar_code
)
2575 tt_cmap13_next( cmap13
);
2576 if ( cmap13
->valid
)
2578 gindex
= cmap13
->cur_gindex
;
2580 *pchar_code
= cmap13
->cur_charcode
;
2586 gindex
= tt_cmap13_char_map_binary( cmap
, pchar_code
, 1 );
2592 FT_CALLBACK_DEF( FT_Error
)
2593 tt_cmap13_get_info( TT_CMap cmap
,
2594 TT_CMapInfo
*cmap_info
)
2596 FT_Byte
* p
= cmap
->data
+ 8;
2599 cmap_info
->format
= 13;
2600 cmap_info
->language
= (FT_ULong
)TT_PEEK_ULONG( p
);
2607 tt_cmap13_class_rec
,
2608 sizeof ( TT_CMap13Rec
),
2610 (FT_CMap_InitFunc
) tt_cmap13_init
,
2611 (FT_CMap_DoneFunc
) NULL
,
2612 (FT_CMap_CharIndexFunc
)tt_cmap13_char_index
,
2613 (FT_CMap_CharNextFunc
) tt_cmap13_char_next
,
2622 (TT_CMap_ValidateFunc
)tt_cmap13_validate
,
2623 (TT_CMap_Info_GetFunc
)tt_cmap13_get_info
)
2625 #endif /* TT_CONFIG_CMAP_FORMAT_13 */
2628 /*************************************************************************/
2629 /*************************************************************************/
2631 /***** FORMAT 14 *****/
2633 /*************************************************************************/
2634 /*************************************************************************/
2636 /*************************************************************************/
2638 /* TABLE OVERVIEW */
2639 /* -------------- */
2641 /* NAME OFFSET TYPE DESCRIPTION */
2643 /* format 0 USHORT must be 14 */
2644 /* length 2 ULONG table length in bytes */
2645 /* numSelector 6 ULONG number of variation sel. records */
2647 /* Followed by numSelector records, each of which looks like */
2649 /* varSelector 0 UINT24 Unicode codepoint of sel. */
2650 /* defaultOff 3 ULONG offset to a default UVS table */
2651 /* describing any variants to be found in */
2652 /* the normal Unicode subtable. */
2653 /* nonDefOff 7 ULONG offset to a non-default UVS table */
2654 /* describing any variants not in the */
2655 /* standard cmap, with GIDs here */
2656 /* (either offset may be 0 NULL) */
2658 /* Selectors are sorted by code point. */
2660 /* A default Unicode Variation Selector (UVS) subtable is just a list of */
2661 /* ranges of code points which are to be found in the standard cmap. No */
2662 /* glyph IDs (GIDs) here. */
2664 /* numRanges 0 ULONG number of ranges following */
2666 /* A range looks like */
2668 /* uniStart 0 UINT24 code point of the first character in */
2670 /* additionalCnt 3 UBYTE count of additional characters in this */
2671 /* range (zero means a range of a single */
2674 /* Ranges are sorted by `uniStart'. */
2676 /* A non-default Unicode Variation Selector (UVS) subtable is a list of */
2677 /* mappings from codepoint to GID. */
2679 /* numMappings 0 ULONG number of mappings */
2681 /* A range looks like */
2683 /* uniStart 0 UINT24 code point of the first character in */
2685 /* GID 3 USHORT and its GID */
2687 /* Ranges are sorted by `uniStart'. */
2689 #ifdef TT_CONFIG_CMAP_FORMAT_14
2691 typedef struct TT_CMap14Rec_
2694 FT_ULong num_selectors
;
2696 /* This array is used to store the results of various
2697 * cmap 14 query functions. The data is overwritten
2698 * on each call to these functions.
2700 FT_UInt32 max_results
;
2704 } TT_CMap14Rec
, *TT_CMap14
;
2707 FT_CALLBACK_DEF( void )
2708 tt_cmap14_done( TT_CMap14 cmap
)
2710 FT_Memory memory
= cmap
->memory
;
2713 cmap
->max_results
= 0;
2714 if ( memory
!= NULL
&& cmap
->results
!= NULL
)
2715 FT_FREE( cmap
->results
);
2720 tt_cmap14_ensure( TT_CMap14 cmap
,
2721 FT_UInt32 num_results
,
2724 FT_UInt32 old_max
= cmap
->max_results
;
2725 FT_Error error
= FT_Err_Ok
;
2728 if ( num_results
> cmap
->max_results
)
2730 cmap
->memory
= memory
;
2732 if ( FT_QRENEW_ARRAY( cmap
->results
, old_max
, num_results
) )
2735 cmap
->max_results
= num_results
;
2742 FT_CALLBACK_DEF( FT_Error
)
2743 tt_cmap14_init( TT_CMap14 cmap
,
2746 cmap
->cmap
.data
= table
;
2749 cmap
->num_selectors
= FT_PEEK_ULONG( table
);
2750 cmap
->max_results
= 0;
2751 cmap
->results
= NULL
;
2757 FT_CALLBACK_DEF( FT_Error
)
2758 tt_cmap14_validate( FT_Byte
* table
,
2759 FT_Validator valid
)
2761 FT_Byte
* p
= table
+ 2;
2762 FT_ULong length
= TT_NEXT_ULONG( p
);
2763 FT_ULong num_selectors
= TT_NEXT_ULONG( p
);
2766 if ( length
> (FT_ULong
)( valid
->limit
- table
) ||
2767 length
< 10 + 11 * num_selectors
)
2768 FT_INVALID_TOO_SHORT
;
2770 /* check selectors, they must be in increasing order */
2772 /* we start lastVarSel at 1 because a variant selector value of 0
2775 FT_ULong n
, lastVarSel
= 1;
2778 for ( n
= 0; n
< num_selectors
; n
++ )
2780 FT_ULong varSel
= TT_NEXT_UINT24( p
);
2781 FT_ULong defOff
= TT_NEXT_ULONG( p
);
2782 FT_ULong nondefOff
= TT_NEXT_ULONG( p
);
2785 if ( defOff
>= length
|| nondefOff
>= length
)
2786 FT_INVALID_TOO_SHORT
;
2788 if ( varSel
< lastVarSel
)
2791 lastVarSel
= varSel
+ 1;
2793 /* check the default table (these glyphs should be reached */
2794 /* through the normal Unicode cmap, no GIDs, just check order) */
2797 FT_Byte
* defp
= table
+ defOff
;
2798 FT_ULong numRanges
= TT_NEXT_ULONG( defp
);
2800 FT_ULong lastBase
= 0;
2803 if ( defp
+ numRanges
* 4 > valid
->limit
)
2804 FT_INVALID_TOO_SHORT
;
2806 for ( i
= 0; i
< numRanges
; ++i
)
2808 FT_ULong base
= TT_NEXT_UINT24( defp
);
2809 FT_ULong cnt
= FT_NEXT_BYTE( defp
);
2812 if ( base
+ cnt
>= 0x110000UL
) /* end of Unicode */
2815 if ( base
< lastBase
)
2818 lastBase
= base
+ cnt
+ 1U;
2822 /* and the non-default table (these glyphs are specified here) */
2823 if ( nondefOff
!= 0 )
2825 FT_Byte
* ndp
= table
+ nondefOff
;
2826 FT_ULong numMappings
= TT_NEXT_ULONG( ndp
);
2827 FT_ULong i
, lastUni
= 0;
2830 if ( numMappings
* 4 > (FT_ULong
)( valid
->limit
- ndp
) )
2831 FT_INVALID_TOO_SHORT
;
2833 for ( i
= 0; i
< numMappings
; ++i
)
2835 FT_ULong uni
= TT_NEXT_UINT24( ndp
);
2836 FT_ULong gid
= TT_NEXT_USHORT( ndp
);
2839 if ( uni
>= 0x110000UL
) /* end of Unicode */
2842 if ( uni
< lastUni
)
2847 if ( valid
->level
>= FT_VALIDATE_TIGHT
&&
2848 gid
>= TT_VALID_GLYPH_COUNT( valid
) )
2849 FT_INVALID_GLYPH_ID
;
2859 FT_CALLBACK_DEF( FT_UInt
)
2860 tt_cmap14_char_index( TT_CMap cmap
,
2861 FT_UInt32 char_code
)
2864 FT_UNUSED( char_code
);
2866 /* This can't happen */
2871 FT_CALLBACK_DEF( FT_UInt32
)
2872 tt_cmap14_char_next( TT_CMap cmap
,
2873 FT_UInt32
*pchar_code
)
2877 /* This can't happen */
2883 FT_CALLBACK_DEF( FT_Error
)
2884 tt_cmap14_get_info( TT_CMap cmap
,
2885 TT_CMapInfo
*cmap_info
)
2889 cmap_info
->format
= 14;
2890 /* subtable 14 does not define a language field */
2891 cmap_info
->language
= 0xFFFFFFFFUL
;
2898 tt_cmap14_char_map_def_binary( FT_Byte
*base
,
2899 FT_UInt32 char_code
)
2901 FT_UInt32 numRanges
= TT_PEEK_ULONG( base
);
2913 FT_UInt32 mid
= ( min
+ max
) >> 1;
2914 FT_Byte
* p
= base
+ 4 * mid
;
2915 FT_ULong start
= TT_NEXT_UINT24( p
);
2916 FT_UInt cnt
= FT_NEXT_BYTE( p
);
2919 if ( char_code
< start
)
2921 else if ( char_code
> start
+cnt
)
2932 tt_cmap14_char_map_nondef_binary( FT_Byte
*base
,
2933 FT_UInt32 char_code
)
2935 FT_UInt32 numMappings
= TT_PEEK_ULONG( base
);
2947 FT_UInt32 mid
= ( min
+ max
) >> 1;
2948 FT_Byte
* p
= base
+ 5 * mid
;
2949 FT_UInt32 uni
= (FT_UInt32
)TT_NEXT_UINT24( p
);
2952 if ( char_code
< uni
)
2954 else if ( char_code
> uni
)
2957 return TT_PEEK_USHORT( p
);
2965 tt_cmap14_find_variant( FT_Byte
*base
,
2966 FT_UInt32 variantCode
)
2968 FT_UInt32 numVar
= TT_PEEK_ULONG( base
);
2980 FT_UInt32 mid
= ( min
+ max
) >> 1;
2981 FT_Byte
* p
= base
+ 11 * mid
;
2982 FT_ULong varSel
= TT_NEXT_UINT24( p
);
2985 if ( variantCode
< varSel
)
2987 else if ( variantCode
> varSel
)
2997 FT_CALLBACK_DEF( FT_UInt
)
2998 tt_cmap14_char_var_index( TT_CMap cmap
,
3001 FT_UInt32 variantSelector
)
3003 FT_Byte
* p
= tt_cmap14_find_variant( cmap
->data
+ 6, variantSelector
);
3011 defOff
= TT_NEXT_ULONG( p
);
3012 nondefOff
= TT_PEEK_ULONG( p
);
3015 tt_cmap14_char_map_def_binary( cmap
->data
+ defOff
, charcode
) )
3017 /* This is the default variant of this charcode. GID not stored */
3018 /* here; stored in the normal Unicode charmap instead. */
3019 return ucmap
->cmap
.clazz
->char_index( &ucmap
->cmap
, charcode
);
3022 if ( nondefOff
!= 0 )
3023 return tt_cmap14_char_map_nondef_binary( cmap
->data
+ nondefOff
,
3030 FT_CALLBACK_DEF( FT_Int
)
3031 tt_cmap14_char_var_isdefault( TT_CMap cmap
,
3033 FT_UInt32 variantSelector
)
3035 FT_Byte
* p
= tt_cmap14_find_variant( cmap
->data
+ 6, variantSelector
);
3043 defOff
= TT_NEXT_ULONG( p
);
3044 nondefOff
= TT_NEXT_ULONG( p
);
3047 tt_cmap14_char_map_def_binary( cmap
->data
+ defOff
, charcode
) )
3050 if ( nondefOff
!= 0 &&
3051 tt_cmap14_char_map_nondef_binary( cmap
->data
+ nondefOff
,
3059 FT_CALLBACK_DEF( FT_UInt32
* )
3060 tt_cmap14_variants( TT_CMap cmap
,
3063 TT_CMap14 cmap14
= (TT_CMap14
)cmap
;
3064 FT_UInt32 count
= cmap14
->num_selectors
;
3065 FT_Byte
* p
= cmap
->data
+ 10;
3070 if ( tt_cmap14_ensure( cmap14
, ( count
+ 1 ), memory
) )
3073 result
= cmap14
->results
;
3074 for ( i
= 0; i
< count
; ++i
)
3076 result
[i
] = (FT_UInt32
)TT_NEXT_UINT24( p
);
3085 FT_CALLBACK_DEF( FT_UInt32
* )
3086 tt_cmap14_char_variants( TT_CMap cmap
,
3088 FT_UInt32 charCode
)
3090 TT_CMap14 cmap14
= (TT_CMap14
) cmap
;
3091 FT_UInt32 count
= cmap14
->num_selectors
;
3092 FT_Byte
* p
= cmap
->data
+ 10;
3096 if ( tt_cmap14_ensure( cmap14
, ( count
+ 1 ), memory
) )
3099 for ( q
= cmap14
->results
; count
> 0; --count
)
3101 FT_UInt32 varSel
= TT_NEXT_UINT24( p
);
3102 FT_ULong defOff
= TT_NEXT_ULONG( p
);
3103 FT_ULong nondefOff
= TT_NEXT_ULONG( p
);
3106 if ( ( defOff
!= 0 &&
3107 tt_cmap14_char_map_def_binary( cmap
->data
+ defOff
,
3110 tt_cmap14_char_map_nondef_binary( cmap
->data
+ nondefOff
,
3119 return cmap14
->results
;
3124 tt_cmap14_def_char_count( FT_Byte
*p
)
3126 FT_UInt32 numRanges
= (FT_UInt32
)TT_NEXT_ULONG( p
);
3130 p
+= 3; /* point to the first `cnt' field */
3131 for ( ; numRanges
> 0; numRanges
-- )
3142 tt_cmap14_get_def_chars( TT_CMap cmap
,
3146 TT_CMap14 cmap14
= (TT_CMap14
) cmap
;
3147 FT_UInt32 numRanges
;
3152 cnt
= tt_cmap14_def_char_count( p
);
3153 numRanges
= (FT_UInt32
)TT_NEXT_ULONG( p
);
3155 if ( tt_cmap14_ensure( cmap14
, ( cnt
+ 1 ), memory
) )
3158 for ( q
= cmap14
->results
; numRanges
> 0; --numRanges
)
3160 FT_UInt32 uni
= (FT_UInt32
)TT_NEXT_UINT24( p
);
3163 cnt
= FT_NEXT_BYTE( p
) + 1;
3170 } while ( --cnt
!= 0 );
3174 return cmap14
->results
;
3179 tt_cmap14_get_nondef_chars( TT_CMap cmap
,
3183 TT_CMap14 cmap14
= (TT_CMap14
) cmap
;
3184 FT_UInt32 numMappings
;
3189 numMappings
= (FT_UInt32
)TT_NEXT_ULONG( p
);
3191 if ( tt_cmap14_ensure( cmap14
, ( numMappings
+ 1 ), memory
) )
3194 ret
= cmap14
->results
;
3195 for ( i
= 0; i
< numMappings
; ++i
)
3197 ret
[i
] = (FT_UInt32
)TT_NEXT_UINT24( p
);
3206 FT_CALLBACK_DEF( FT_UInt32
* )
3207 tt_cmap14_variant_chars( TT_CMap cmap
,
3209 FT_UInt32 variantSelector
)
3211 FT_Byte
*p
= tt_cmap14_find_variant( cmap
->data
+ 6,
3221 defOff
= TT_NEXT_ULONG( p
);
3222 nondefOff
= TT_NEXT_ULONG( p
);
3224 if ( defOff
== 0 && nondefOff
== 0 )
3228 return tt_cmap14_get_nondef_chars( cmap
, cmap
->data
+ nondefOff
,
3230 else if ( nondefOff
== 0 )
3231 return tt_cmap14_get_def_chars( cmap
, cmap
->data
+ defOff
,
3235 /* Both a default and a non-default glyph set? That's probably not */
3236 /* good font design, but the spec allows for it... */
3237 TT_CMap14 cmap14
= (TT_CMap14
) cmap
;
3238 FT_UInt32 numRanges
;
3239 FT_UInt32 numMappings
;
3249 p
= cmap
->data
+ nondefOff
;
3250 dp
= cmap
->data
+ defOff
;
3252 numMappings
= (FT_UInt32
)TT_NEXT_ULONG( p
);
3253 dcnt
= tt_cmap14_def_char_count( dp
);
3254 numRanges
= (FT_UInt32
)TT_NEXT_ULONG( dp
);
3256 if ( numMappings
== 0 )
3257 return tt_cmap14_get_def_chars( cmap
, cmap
->data
+ defOff
,
3260 return tt_cmap14_get_nondef_chars( cmap
, cmap
->data
+ nondefOff
,
3263 if ( tt_cmap14_ensure( cmap14
, ( dcnt
+ numMappings
+ 1 ), memory
) )
3266 ret
= cmap14
->results
;
3267 duni
= (FT_UInt32
)TT_NEXT_UINT24( dp
);
3268 dcnt
= FT_NEXT_BYTE( dp
);
3270 nuni
= (FT_UInt32
)TT_NEXT_UINT24( p
);
3277 if ( nuni
> duni
+ dcnt
)
3279 for ( k
= 0; k
<= dcnt
; ++k
)
3280 ret
[i
++] = duni
+ k
;
3284 if ( di
> numRanges
)
3287 duni
= (FT_UInt32
)TT_NEXT_UINT24( dp
);
3288 dcnt
= FT_NEXT_BYTE( dp
);
3294 /* If it is within the default range then ignore it -- */
3295 /* that should not have happened */
3297 if ( ni
> numMappings
)
3300 nuni
= (FT_UInt32
)TT_NEXT_UINT24( p
);
3305 if ( ni
<= numMappings
)
3307 /* If we get here then we have run out of all default ranges. */
3308 /* We have read one non-default mapping which we haven't stored */
3309 /* and there may be others that need to be read. */
3311 while ( ni
< numMappings
)
3313 ret
[i
++] = (FT_UInt32
)TT_NEXT_UINT24( p
);
3318 else if ( di
<= numRanges
)
3320 /* If we get here then we have run out of all non-default */
3321 /* mappings. We have read one default range which we haven't */
3322 /* stored and there may be others that need to be read. */
3323 for ( k
= 0; k
<= dcnt
; ++k
)
3324 ret
[i
++] = duni
+ k
;
3326 while ( di
< numRanges
)
3328 duni
= (FT_UInt32
)TT_NEXT_UINT24( dp
);
3329 dcnt
= FT_NEXT_BYTE( dp
);
3331 for ( k
= 0; k
<= dcnt
; ++k
)
3332 ret
[i
++] = duni
+ k
;
3345 tt_cmap14_class_rec
,
3346 sizeof ( TT_CMap14Rec
),
3348 (FT_CMap_InitFunc
) tt_cmap14_init
,
3349 (FT_CMap_DoneFunc
) tt_cmap14_done
,
3350 (FT_CMap_CharIndexFunc
)tt_cmap14_char_index
,
3351 (FT_CMap_CharNextFunc
) tt_cmap14_char_next
,
3353 /* Format 14 extension functions */
3354 (FT_CMap_CharVarIndexFunc
) tt_cmap14_char_var_index
,
3355 (FT_CMap_CharVarIsDefaultFunc
)tt_cmap14_char_var_isdefault
,
3356 (FT_CMap_VariantListFunc
) tt_cmap14_variants
,
3357 (FT_CMap_CharVariantListFunc
) tt_cmap14_char_variants
,
3358 (FT_CMap_VariantCharListFunc
) tt_cmap14_variant_chars
,
3361 (TT_CMap_ValidateFunc
)tt_cmap14_validate
,
3362 (TT_CMap_Info_GetFunc
)tt_cmap14_get_info
)
3364 #endif /* TT_CONFIG_CMAP_FORMAT_14 */
3367 #ifndef FT_CONFIG_OPTION_PIC
3369 static const TT_CMap_Class tt_cmap_classes
[] =
3371 #define TTCMAPCITEM( a ) &a,
3372 #include "ttcmapc.h"
3376 #else /*FT_CONFIG_OPTION_PIC*/
3379 FT_Destroy_Class_tt_cmap_classes( FT_Library library
,
3380 TT_CMap_Class
* clazz
)
3382 FT_Memory memory
= library
->memory
;
3391 FT_Create_Class_tt_cmap_classes( FT_Library library
,
3392 TT_CMap_Class
** output_class
)
3394 TT_CMap_Class
* clazz
= NULL
;
3395 TT_CMap_ClassRec
* recs
;
3397 FT_Memory memory
= library
->memory
;
3402 #define TTCMAPCITEM( a ) i++;
3403 #include "ttcmapc.h"
3405 /* allocate enough space for both the pointers */
3406 /* plus terminator and the class instances */
3407 if ( FT_ALLOC( clazz
, sizeof ( *clazz
) * ( i
+ 1 ) +
3408 sizeof ( TT_CMap_ClassRec
) * i
) )
3411 /* the location of the class instances follows the array of pointers */
3412 recs
= (TT_CMap_ClassRec
*)( (char*)clazz
+
3413 sizeof ( *clazz
) * ( i
+ 1 ) );
3417 #define TTCMAPCITEM( a ) \
3418 FT_Init_Class_ ## a( &recs[i] ); \
3419 clazz[i] = &recs[i]; \
3421 #include "ttcmapc.h"
3425 *output_class
= clazz
;
3429 #endif /*FT_CONFIG_OPTION_PIC*/
3432 /* parse the `cmap' table and build the corresponding TT_CMap objects */
3433 /* in the current face */
3435 FT_LOCAL_DEF( FT_Error
)
3436 tt_face_build_cmaps( TT_Face face
)
3438 FT_Byte
* table
= face
->cmap_table
;
3439 FT_Byte
* limit
= table
+ face
->cmap_size
;
3440 FT_UInt
volatile num_cmaps
;
3441 FT_Byte
* volatile p
= table
;
3442 FT_Library library
= FT_FACE_LIBRARY( face
);
3444 FT_UNUSED( library
);
3447 if ( !p
|| p
+ 4 > limit
)
3448 return FT_THROW( Invalid_Table
);
3450 /* only recognize format 0 */
3451 if ( TT_NEXT_USHORT( p
) != 0 )
3454 FT_ERROR(( "tt_face_build_cmaps:"
3455 " unsupported `cmap' table format = %d\n",
3456 TT_PEEK_USHORT( p
) ));
3457 return FT_THROW( Invalid_Table
);
3460 num_cmaps
= TT_NEXT_USHORT( p
);
3462 #ifdef FT_MAX_CHARMAP_CACHEABLE
3463 if ( num_cmaps
> FT_MAX_CHARMAP_CACHEABLE
)
3464 FT_ERROR(( "tt_face_build_cmaps: too many cmap subtables (%d)\n"
3465 " subtable #%d and higher are loaded"
3466 " but cannot be searched\n",
3467 num_cmaps
, FT_MAX_CHARMAP_CACHEABLE
+ 1 ));
3470 for ( ; num_cmaps
> 0 && p
+ 8 <= limit
; num_cmaps
-- )
3472 FT_CharMapRec charmap
;
3476 charmap
.platform_id
= TT_NEXT_USHORT( p
);
3477 charmap
.encoding_id
= TT_NEXT_USHORT( p
);
3478 charmap
.face
= FT_FACE( face
);
3479 charmap
.encoding
= FT_ENCODING_NONE
; /* will be filled later */
3480 offset
= TT_NEXT_ULONG( p
);
3482 if ( offset
&& offset
<= face
->cmap_size
- 2 )
3484 FT_Byte
* volatile cmap
= table
+ offset
;
3485 volatile FT_UInt format
= TT_PEEK_USHORT( cmap
);
3486 const TT_CMap_Class
* volatile pclazz
= TT_CMAP_CLASSES_GET
;
3487 TT_CMap_Class
volatile clazz
;
3490 for ( ; *pclazz
; pclazz
++ )
3493 if ( clazz
->format
== format
)
3495 volatile TT_ValidatorRec valid
;
3496 volatile FT_Error error
= FT_Err_Ok
;
3499 ft_validator_init( FT_VALIDATOR( &valid
), cmap
, limit
,
3500 FT_VALIDATE_DEFAULT
);
3502 valid
.num_glyphs
= (FT_UInt
)face
->max_profile
.numGlyphs
;
3504 if ( ft_setjmp( FT_VALIDATOR( &valid
)->jump_buffer
) == 0 )
3506 /* validate this cmap sub-table */
3507 error
= clazz
->validate( cmap
, FT_VALIDATOR( &valid
) );
3510 if ( valid
.validator
.error
== 0 )
3515 /* It might make sense to store the single variation */
3516 /* selector cmap somewhere special. But it would have to be */
3517 /* in the public FT_FaceRec, and we can't change that. */
3519 if ( !FT_CMap_New( (FT_CMap_Class
)clazz
,
3520 cmap
, &charmap
, &ttcmap
) )
3522 /* it is simpler to directly set `flags' than adding */
3523 /* a parameter to FT_CMap_New */
3524 ((TT_CMap
)ttcmap
)->flags
= (FT_Int
)error
;
3529 FT_TRACE0(( "tt_face_build_cmaps:"
3530 " broken cmap sub-table ignored\n" ));
3536 if ( *pclazz
== NULL
)
3538 FT_TRACE0(( "tt_face_build_cmaps:"
3539 " unsupported cmap sub-table ignored\n" ));
3548 FT_LOCAL( FT_Error
)
3549 tt_get_cmap_info( FT_CharMap charmap
,
3550 TT_CMapInfo
*cmap_info
)
3552 FT_CMap cmap
= (FT_CMap
)charmap
;
3553 TT_CMap_Class clazz
= (TT_CMap_Class
)cmap
->clazz
;
3556 return clazz
->get_cmap_info( charmap
, cmap_info
);