1 /***************************************************************************/
5 /* TrueType and OpenType embedded bitmap support (body). */
7 /* Copyright 2005-2009, 2013, 2014 by */
8 /* David Turner, Robert Wilhelm, and Werner Lemberg. */
10 /* Copyright 2013 by Google, Inc. */
11 /* Google Author(s): Behdad Esfahbod. */
13 /* This file is part of the FreeType project, and may only be used, */
14 /* modified, and distributed under the terms of the FreeType project */
15 /* license, LICENSE.TXT. By continuing to use, modify, or distribute */
16 /* this file you indicate that you have read the license and */
17 /* understand and accept it fully. */
19 /***************************************************************************/
23 #include FT_INTERNAL_DEBUG_H
24 #include FT_INTERNAL_STREAM_H
25 #include FT_TRUETYPE_TAGS_H
35 /*************************************************************************/
37 /* The macro FT_COMPONENT is used in trace mode. It is an implicit */
38 /* parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log */
39 /* messages during execution. */
42 #define FT_COMPONENT trace_ttsbit
45 FT_LOCAL_DEF( FT_Error )
46 tt_face_load_sbit( TT_Face face,
53 face->sbit_table = NULL;
54 face->sbit_table_size = 0;
55 face->sbit_table_type = TT_SBIT_TABLE_TYPE_NONE;
56 face->sbit_num_strikes = 0;
58 error = face->goto_table( face, TTAG_CBLC, stream, &table_size );
60 face->sbit_table_type = TT_SBIT_TABLE_TYPE_CBLC;
63 error = face->goto_table( face, TTAG_EBLC, stream, &table_size );
65 error = face->goto_table( face, TTAG_bloc, stream, &table_size );
67 face->sbit_table_type = TT_SBIT_TABLE_TYPE_EBLC;
72 error = face->goto_table( face, TTAG_sbix, stream, &table_size );
74 face->sbit_table_type = TT_SBIT_TABLE_TYPE_SBIX;
81 FT_ERROR(( "tt_face_load_sbit_strikes: table too short\n" ));
82 error = FT_THROW( Invalid_File_Format );
86 switch ( (FT_UInt)face->sbit_table_type )
88 case TT_SBIT_TABLE_TYPE_EBLC:
89 case TT_SBIT_TABLE_TYPE_CBLC:
97 if ( FT_FRAME_EXTRACT( table_size, face->sbit_table ) )
100 face->sbit_table_size = table_size;
102 p = face->sbit_table;
104 version = FT_NEXT_ULONG( p );
105 num_strikes = FT_NEXT_ULONG( p );
107 if ( ( version & 0xFFFF0000UL ) != 0x00020000UL )
109 error = FT_THROW( Unknown_File_Format );
113 if ( num_strikes >= 0x10000UL )
115 error = FT_THROW( Invalid_File_Format );
120 * Count the number of strikes available in the table. We are a bit
121 * paranoid there and don't trust the data.
123 count = (FT_UInt)num_strikes;
124 if ( 8 + 48UL * count > table_size )
125 count = (FT_UInt)( ( table_size - 8 ) / 48 );
127 face->sbit_num_strikes = count;
131 case TT_SBIT_TABLE_TYPE_SBIX:
135 FT_ULong num_strikes;
139 if ( FT_FRAME_ENTER( 8 ) )
142 version = FT_GET_USHORT();
143 flags = FT_GET_USHORT();
144 num_strikes = FT_GET_ULONG();
150 error = FT_THROW( Unknown_File_Format );
154 /* Bit 0 must always be `1'. */
155 /* Bit 1 controls the overlay of bitmaps with outlines. */
156 /* All other bits should be zero. */
157 if ( !( flags == 1 || flags == 3 ) ||
158 num_strikes >= 0x10000UL )
160 error = FT_THROW( Invalid_File_Format );
164 /* we currently don't support bit 1; however, it is better to */
165 /* draw at least something... */
167 FT_TRACE1(( "tt_face_load_sbit_strikes:"
168 " sbix overlay not supported yet\n"
170 " expect bad rendering results\n" ));
173 * Count the number of strikes available in the table. We are a bit
174 * paranoid there and don't trust the data.
176 count = (FT_UInt)num_strikes;
177 if ( 8 + 4UL * count > table_size )
178 count = (FT_UInt)( ( table_size - 8 ) / 4 );
180 if ( FT_STREAM_SEEK( FT_STREAM_POS() - 8 ) )
183 face->sbit_table_size = 8 + count * 4;
184 if ( FT_FRAME_EXTRACT( face->sbit_table_size, face->sbit_table ) )
187 face->sbit_num_strikes = count;
192 error = FT_THROW( Unknown_File_Format );
197 FT_TRACE3(( "sbit_num_strikes: %u\n", face->sbit_num_strikes ));
204 if ( face->sbit_table )
205 FT_FRAME_RELEASE( face->sbit_table );
206 face->sbit_table_size = 0;
207 face->sbit_table_type = TT_SBIT_TABLE_TYPE_NONE;
215 tt_face_free_sbit( TT_Face face )
217 FT_Stream stream = face->root.stream;
220 FT_FRAME_RELEASE( face->sbit_table );
221 face->sbit_table_size = 0;
222 face->sbit_table_type = TT_SBIT_TABLE_TYPE_NONE;
223 face->sbit_num_strikes = 0;
227 FT_LOCAL_DEF( FT_Error )
228 tt_face_set_sbit_strike( TT_Face face,
230 FT_ULong* astrike_index )
232 return FT_Match_Size( (FT_Face)face, req, 0, astrike_index );
236 FT_LOCAL_DEF( FT_Error )
237 tt_face_load_strike_metrics( TT_Face face,
238 FT_ULong strike_index,
239 FT_Size_Metrics* metrics )
241 if ( strike_index >= (FT_ULong)face->sbit_num_strikes )
242 return FT_THROW( Invalid_Argument );
244 switch ( (FT_UInt)face->sbit_table_type )
246 case TT_SBIT_TABLE_TYPE_EBLC:
247 case TT_SBIT_TABLE_TYPE_CBLC:
252 strike = face->sbit_table + 8 + strike_index * 48;
254 metrics->x_ppem = (FT_UShort)strike[44];
255 metrics->y_ppem = (FT_UShort)strike[45];
257 metrics->ascender = (FT_Char)strike[16] << 6; /* hori.ascender */
258 metrics->descender = (FT_Char)strike[17] << 6; /* hori.descender */
259 metrics->height = metrics->ascender - metrics->descender;
261 /* Is this correct? */
262 metrics->max_advance = ( (FT_Char)strike[22] + /* min_origin_SB */
263 strike[18] + /* max_width */
264 (FT_Char)strike[23] /* min_advance_SB */
269 case TT_SBIT_TABLE_TYPE_SBIX:
271 FT_Stream stream = face->root.stream;
272 FT_UInt offset, upem;
273 FT_UShort ppem, resolution;
281 p = face->sbit_table + 8 + 4 * strike_index;
282 offset = FT_NEXT_ULONG( p );
284 error = face->goto_table( face, TTAG_sbix, stream, &table_size );
288 if ( offset + 4 > table_size )
289 return FT_THROW( Invalid_File_Format );
291 if ( FT_STREAM_SEEK( FT_STREAM_POS() + offset ) ||
292 FT_FRAME_ENTER( 4 ) )
295 ppem = FT_GET_USHORT();
296 resolution = FT_GET_USHORT();
298 FT_UNUSED( resolution ); /* What to do with this? */
302 upem = face->header.Units_Per_EM;
303 hori = &face->horizontal;
305 metrics->x_ppem = ppem;
306 metrics->y_ppem = ppem;
308 metrics->ascender = ppem * hori->Ascender * 64 / upem;
309 metrics->descender = ppem * hori->Descender * 64 / upem;
310 metrics->height = ppem * ( hori->Ascender -
312 hori->Line_Gap ) * 64 / upem;
313 metrics->max_advance = ppem * hori->advance_Width_Max * 64 / upem;
319 return FT_THROW( Unknown_File_Format );
324 typedef struct TT_SBitDecoderRec_
329 TT_SBit_Metrics metrics;
330 FT_Bool metrics_loaded;
331 FT_Bool bitmap_allocated;
337 FT_ULong strike_index_array;
338 FT_ULong strike_index_count;
342 } TT_SBitDecoderRec, *TT_SBitDecoder;
346 tt_sbit_decoder_init( TT_SBitDecoder decoder,
348 FT_ULong strike_index,
349 TT_SBit_MetricsRec* metrics )
352 FT_Stream stream = face->root.stream;
356 error = face->goto_table( face, TTAG_CBDT, stream, &ebdt_size );
358 error = face->goto_table( face, TTAG_EBDT, stream, &ebdt_size );
360 error = face->goto_table( face, TTAG_bdat, stream, &ebdt_size );
364 decoder->face = face;
365 decoder->stream = stream;
366 decoder->bitmap = &face->root.glyph->bitmap;
367 decoder->metrics = metrics;
369 decoder->metrics_loaded = 0;
370 decoder->bitmap_allocated = 0;
372 decoder->ebdt_start = FT_STREAM_POS();
373 decoder->ebdt_size = ebdt_size;
375 decoder->eblc_base = face->sbit_table;
376 decoder->eblc_limit = face->sbit_table + face->sbit_table_size;
378 /* now find the strike corresponding to the index */
383 if ( 8 + 48 * strike_index + 3 * 4 + 34 + 1 > face->sbit_table_size )
385 error = FT_THROW( Invalid_File_Format );
389 p = decoder->eblc_base + 8 + 48 * strike_index;
391 decoder->strike_index_array = FT_NEXT_ULONG( p );
393 decoder->strike_index_count = FT_NEXT_ULONG( p );
395 decoder->bit_depth = *p;
397 /* decoder->strike_index_array + */
398 /* 8 * decoder->strike_index_count > face->sbit_table_size ? */
399 if ( decoder->strike_index_array > face->sbit_table_size ||
400 decoder->strike_index_count >
401 ( face->sbit_table_size - decoder->strike_index_array ) / 8 )
402 error = FT_THROW( Invalid_File_Format );
411 tt_sbit_decoder_done( TT_SBitDecoder decoder )
413 FT_UNUSED( decoder );
418 tt_sbit_decoder_alloc_bitmap( TT_SBitDecoder decoder )
420 FT_Error error = FT_Err_Ok;
421 FT_UInt width, height;
422 FT_Bitmap* map = decoder->bitmap;
426 if ( !decoder->metrics_loaded )
428 error = FT_THROW( Invalid_Argument );
432 width = decoder->metrics->width;
433 height = decoder->metrics->height;
435 map->width = (int)width;
436 map->rows = (int)height;
438 switch ( decoder->bit_depth )
441 map->pixel_mode = FT_PIXEL_MODE_MONO;
442 map->pitch = ( map->width + 7 ) >> 3;
447 map->pixel_mode = FT_PIXEL_MODE_GRAY2;
448 map->pitch = ( map->width + 3 ) >> 2;
453 map->pixel_mode = FT_PIXEL_MODE_GRAY4;
454 map->pitch = ( map->width + 1 ) >> 1;
459 map->pixel_mode = FT_PIXEL_MODE_GRAY;
460 map->pitch = map->width;
461 map->num_grays = 256;
465 map->pixel_mode = FT_PIXEL_MODE_BGRA;
466 map->pitch = map->width * 4;
467 map->num_grays = 256;
471 error = FT_THROW( Invalid_File_Format );
475 size = map->rows * map->pitch;
477 /* check that there is no empty image */
479 goto Exit; /* exit successfully! */
481 error = ft_glyphslot_alloc_bitmap( decoder->face->root.glyph, size );
485 decoder->bitmap_allocated = 1;
493 tt_sbit_decoder_load_metrics( TT_SBitDecoder decoder,
499 TT_SBit_Metrics metrics = decoder->metrics;
505 metrics->height = p[0];
506 metrics->width = p[1];
507 metrics->horiBearingX = (FT_Char)p[2];
508 metrics->horiBearingY = (FT_Char)p[3];
509 metrics->horiAdvance = p[4];
517 metrics->vertBearingX = (FT_Char)p[0];
518 metrics->vertBearingY = (FT_Char)p[1];
519 metrics->vertAdvance = p[2];
525 /* avoid uninitialized data in case there is no vertical info -- */
526 metrics->vertBearingX = 0;
527 metrics->vertBearingY = 0;
528 metrics->vertAdvance = 0;
531 decoder->metrics_loaded = 1;
536 FT_TRACE1(( "tt_sbit_decoder_load_metrics: broken table\n" ));
537 return FT_THROW( Invalid_Argument );
541 /* forward declaration */
543 tt_sbit_decoder_load_image( TT_SBitDecoder decoder,
548 typedef FT_Error (*TT_SBitDecoder_LoadFunc)( TT_SBitDecoder decoder,
556 tt_sbit_decoder_load_byte_aligned( TT_SBitDecoder decoder,
562 FT_Error error = FT_Err_Ok;
564 FT_Int bit_height, bit_width, pitch, width, height, line_bits, h;
568 /* check that we can write the glyph into the bitmap */
569 bitmap = decoder->bitmap;
570 bit_width = bitmap->width;
571 bit_height = bitmap->rows;
572 pitch = bitmap->pitch;
573 line = bitmap->buffer;
575 width = decoder->metrics->width;
576 height = decoder->metrics->height;
578 line_bits = width * decoder->bit_depth;
580 if ( x_pos < 0 || x_pos + width > bit_width ||
581 y_pos < 0 || y_pos + height > bit_height )
583 FT_TRACE1(( "tt_sbit_decoder_load_byte_aligned:"
584 " invalid bitmap dimensions\n" ));
585 error = FT_THROW( Invalid_File_Format );
589 if ( p + ( ( line_bits + 7 ) >> 3 ) * height > limit )
591 FT_TRACE1(( "tt_sbit_decoder_load_byte_aligned: broken bitmap\n" ));
592 error = FT_THROW( Invalid_File_Format );
596 /* now do the blit */
597 line += y_pos * pitch + ( x_pos >> 3 );
600 if ( x_pos == 0 ) /* the easy one */
602 for ( h = height; h > 0; h--, line += pitch )
604 FT_Byte* pwrite = line;
608 for ( w = line_bits; w >= 8; w -= 8 )
610 pwrite[0] = (FT_Byte)( pwrite[0] | *p++ );
615 pwrite[0] = (FT_Byte)( pwrite[0] | ( *p++ & ( 0xFF00U >> w ) ) );
620 for ( h = height; h > 0; h--, line += pitch )
622 FT_Byte* pwrite = line;
627 for ( w = line_bits; w >= 8; w -= 8 )
629 wval = (FT_UInt)( wval | *p++ );
630 pwrite[0] = (FT_Byte)( pwrite[0] | ( wval >> x_pos ) );
636 wval = (FT_UInt)( wval | ( *p++ & ( 0xFF00U >> w ) ) );
638 /* all bits read and there are `x_pos + w' bits to be written */
640 pwrite[0] = (FT_Byte)( pwrite[0] | ( wval >> x_pos ) );
646 pwrite[0] = (FT_Byte)( pwrite[0] | ( wval >> x_pos ) );
653 FT_TRACE3(( "tt_sbit_decoder_load_byte_aligned: loaded\n" ));
659 * Load a bit-aligned bitmap (with pointer `p') into a line-aligned bitmap
660 * (with pointer `pwrite'). In the example below, the width is 3 pixel,
661 * and `x_pos' is 1 pixel.
665 * | 7 6 5 4 3 2 1 0 | 7 6 5 4 3 2 1 0 |...
667 * +-------+ +-------+ +-------+ ...
673 * | 7 6 5 4 3 2 1 0 | .
680 * | 7 6 5 4 3 2 1 0 |
687 * | 7 6 5 4 3 2 1 0 |
694 tt_sbit_decoder_load_bit_aligned( TT_SBitDecoder decoder,
700 FT_Error error = FT_Err_Ok;
702 FT_Int bit_height, bit_width, pitch, width, height, line_bits, h, nbits;
707 /* check that we can write the glyph into the bitmap */
708 bitmap = decoder->bitmap;
709 bit_width = bitmap->width;
710 bit_height = bitmap->rows;
711 pitch = bitmap->pitch;
712 line = bitmap->buffer;
714 width = decoder->metrics->width;
715 height = decoder->metrics->height;
717 line_bits = width * decoder->bit_depth;
719 if ( x_pos < 0 || x_pos + width > bit_width ||
720 y_pos < 0 || y_pos + height > bit_height )
722 FT_TRACE1(( "tt_sbit_decoder_load_bit_aligned:"
723 " invalid bitmap dimensions\n" ));
724 error = FT_THROW( Invalid_File_Format );
728 if ( p + ( ( line_bits * height + 7 ) >> 3 ) > limit )
730 FT_TRACE1(( "tt_sbit_decoder_load_bit_aligned: broken bitmap\n" ));
731 error = FT_THROW( Invalid_File_Format );
735 /* now do the blit */
737 /* adjust `line' to point to the first byte of the bitmap */
738 line += y_pos * pitch + ( x_pos >> 3 );
741 /* the higher byte of `rval' is used as a buffer */
745 for ( h = height; h > 0; h--, line += pitch )
747 FT_Byte* pwrite = line;
748 FT_Int w = line_bits;
751 /* handle initial byte (in target bitmap) specially if necessary */
754 w = ( line_bits < 8 - x_pos ) ? line_bits : 8 - x_pos;
761 else if ( nbits < w )
773 *pwrite++ |= ( ( rval >> nbits ) & 0xFF ) &
774 ( ~( 0xFF << w ) << ( 8 - w - x_pos ) );
780 /* handle medial bytes */
781 for ( ; w >= 8; w -= 8 )
784 *pwrite++ |= ( rval >> nbits ) & 0xFF;
789 /* handle final byte if necessary */
796 *pwrite |= ( ( rval >> nbits ) & 0xFF ) & ( 0xFF00U >> w );
803 *pwrite |= ( ( rval >> nbits ) & 0xFF ) & ( 0xFF00U >> w );
811 FT_TRACE3(( "tt_sbit_decoder_load_bit_aligned: loaded\n" ));
817 tt_sbit_decoder_load_compound( TT_SBitDecoder decoder,
823 FT_Error error = FT_Err_Ok;
824 FT_UInt num_components, nn;
826 FT_Char horiBearingX = (FT_Char)decoder->metrics->horiBearingX;
827 FT_Char horiBearingY = (FT_Char)decoder->metrics->horiBearingY;
828 FT_Byte horiAdvance = (FT_Byte)decoder->metrics->horiAdvance;
829 FT_Char vertBearingX = (FT_Char)decoder->metrics->vertBearingX;
830 FT_Char vertBearingY = (FT_Char)decoder->metrics->vertBearingY;
831 FT_Byte vertAdvance = (FT_Byte)decoder->metrics->vertAdvance;
837 num_components = FT_NEXT_USHORT( p );
838 if ( p + 4 * num_components > limit )
840 FT_TRACE1(( "tt_sbit_decoder_load_compound: broken table\n" ));
844 FT_TRACE3(( "tt_sbit_decoder_load_compound: loading %d components\n",
847 for ( nn = 0; nn < num_components; nn++ )
849 FT_UInt gindex = FT_NEXT_USHORT( p );
850 FT_Byte dx = FT_NEXT_BYTE( p );
851 FT_Byte dy = FT_NEXT_BYTE( p );
854 /* NB: a recursive call */
855 error = tt_sbit_decoder_load_image( decoder, gindex,
856 x_pos + dx, y_pos + dy );
861 FT_TRACE3(( "tt_sbit_decoder_load_compound: done\n" ));
863 decoder->metrics->horiBearingX = horiBearingX;
864 decoder->metrics->horiBearingY = horiBearingY;
865 decoder->metrics->horiAdvance = horiAdvance;
866 decoder->metrics->vertBearingX = vertBearingX;
867 decoder->metrics->vertBearingY = vertBearingY;
868 decoder->metrics->vertAdvance = vertAdvance;
869 decoder->metrics->width = (FT_Byte)decoder->bitmap->width;
870 decoder->metrics->height = (FT_Byte)decoder->bitmap->rows;
876 error = FT_THROW( Invalid_File_Format );
881 #ifdef FT_CONFIG_OPTION_USE_PNG
884 tt_sbit_decoder_load_png( TT_SBitDecoder decoder,
890 FT_Error error = FT_Err_Ok;
896 FT_TRACE1(( "tt_sbit_decoder_load_png: broken bitmap\n" ));
897 error = FT_THROW( Invalid_File_Format );
901 png_len = FT_NEXT_ULONG( p );
902 if ( (FT_ULong)( limit - p ) < png_len )
904 FT_TRACE1(( "tt_sbit_decoder_load_png: broken bitmap\n" ));
905 error = FT_THROW( Invalid_File_Format );
909 error = Load_SBit_Png( decoder->face->root.glyph,
914 decoder->stream->memory,
921 FT_TRACE3(( "tt_sbit_decoder_load_png: loaded\n" ));
925 #endif /* FT_CONFIG_OPTION_USE_PNG */
929 tt_sbit_decoder_load_bitmap( TT_SBitDecoder decoder,
930 FT_UInt glyph_format,
931 FT_ULong glyph_start,
937 FT_Stream stream = decoder->stream;
943 /* seek into the EBDT table now */
944 if ( glyph_start + glyph_size > decoder->ebdt_size )
946 error = FT_THROW( Invalid_Argument );
950 if ( FT_STREAM_SEEK( decoder->ebdt_start + glyph_start ) ||
951 FT_FRAME_EXTRACT( glyph_size, data ) )
955 p_limit = p + glyph_size;
957 /* read the data, depending on the glyph format */
958 switch ( glyph_format )
964 error = tt_sbit_decoder_load_metrics( decoder, &p, p_limit, 0 );
971 error = tt_sbit_decoder_load_metrics( decoder, &p, p_limit, 1 );
982 TT_SBitDecoder_LoadFunc loader;
985 switch ( glyph_format )
989 loader = tt_sbit_decoder_load_byte_aligned;
995 /* Don't trust `glyph_format'. For example, Apple's main Korean */
996 /* system font, `AppleMyungJo.ttf' (version 7.0d2e6), uses glyph */
997 /* format 7, but the data is format 6. We check whether we have */
998 /* an excessive number of bytes in the image: If it is equal to */
999 /* the value for a byte-aligned glyph, use the other loading */
1002 /* Note that for some (width,height) combinations, where the */
1003 /* width is not a multiple of 8, the sizes for bit- and */
1004 /* byte-aligned data are equal, for example (7,7) or (15,6). We */
1005 /* then prefer what `glyph_format' specifies. */
1007 FT_UInt width = decoder->metrics->width;
1008 FT_UInt height = decoder->metrics->height;
1010 FT_UInt bit_size = ( width * height + 7 ) >> 3;
1011 FT_UInt byte_size = height * ( ( width + 7 ) >> 3 );
1014 if ( bit_size < byte_size &&
1015 byte_size == (FT_UInt)( p_limit - p ) )
1016 loader = tt_sbit_decoder_load_byte_aligned;
1018 loader = tt_sbit_decoder_load_bit_aligned;
1023 loader = tt_sbit_decoder_load_bit_aligned;
1027 if ( p + 1 > p_limit )
1030 p += 1; /* skip padding */
1034 loader = tt_sbit_decoder_load_compound;
1037 case 17: /* small metrics, PNG image data */
1038 case 18: /* big metrics, PNG image data */
1039 case 19: /* metrics in EBLC, PNG image data */
1040 #ifdef FT_CONFIG_OPTION_USE_PNG
1041 loader = tt_sbit_decoder_load_png;
1044 error = FT_THROW( Unimplemented_Feature );
1046 #endif /* FT_CONFIG_OPTION_USE_PNG */
1049 error = FT_THROW( Invalid_Table );
1053 if ( !decoder->bitmap_allocated )
1055 error = tt_sbit_decoder_alloc_bitmap( decoder );
1060 error = loader( decoder, p, p_limit, x_pos, y_pos );
1064 FT_FRAME_RELEASE( data );
1072 tt_sbit_decoder_load_image( TT_SBitDecoder decoder,
1073 FT_UInt glyph_index,
1078 * First, we find the correct strike range that applies to this
1082 FT_Byte* p = decoder->eblc_base + decoder->strike_index_array;
1083 FT_Byte* p_limit = decoder->eblc_limit;
1084 FT_ULong num_ranges = decoder->strike_index_count;
1085 FT_UInt start, end, index_format, image_format;
1086 FT_ULong image_start = 0, image_end = 0, image_offset;
1089 for ( ; num_ranges > 0; num_ranges-- )
1091 start = FT_NEXT_USHORT( p );
1092 end = FT_NEXT_USHORT( p );
1094 if ( glyph_index >= start && glyph_index <= end )
1097 p += 4; /* ignore index offset */
1102 image_offset = FT_NEXT_ULONG( p );
1104 /* overflow check */
1105 p = decoder->eblc_base + decoder->strike_index_array;
1106 if ( image_offset > (FT_ULong)( p_limit - p ) )
1110 if ( p + 8 > p_limit )
1113 /* now find the glyph's location and extend within the ebdt table */
1114 index_format = FT_NEXT_USHORT( p );
1115 image_format = FT_NEXT_USHORT( p );
1116 image_offset = FT_NEXT_ULONG ( p );
1118 switch ( index_format )
1120 case 1: /* 4-byte offsets relative to `image_offset' */
1121 p += 4 * ( glyph_index - start );
1122 if ( p + 8 > p_limit )
1125 image_start = FT_NEXT_ULONG( p );
1126 image_end = FT_NEXT_ULONG( p );
1128 if ( image_start == image_end ) /* missing glyph */
1132 case 2: /* big metrics, constant image size */
1134 FT_ULong image_size;
1137 if ( p + 12 > p_limit )
1140 image_size = FT_NEXT_ULONG( p );
1142 if ( tt_sbit_decoder_load_metrics( decoder, &p, p_limit, 1 ) )
1145 image_start = image_size * ( glyph_index - start );
1146 image_end = image_start + image_size;
1150 case 3: /* 2-byte offsets relative to 'image_offset' */
1151 p += 2 * ( glyph_index - start );
1152 if ( p + 4 > p_limit )
1155 image_start = FT_NEXT_USHORT( p );
1156 image_end = FT_NEXT_USHORT( p );
1158 if ( image_start == image_end ) /* missing glyph */
1162 case 4: /* sparse glyph array with (glyph,offset) pairs */
1164 FT_ULong mm, num_glyphs;
1167 if ( p + 4 > p_limit )
1170 num_glyphs = FT_NEXT_ULONG( p );
1172 /* overflow check for p + ( num_glyphs + 1 ) * 4 */
1173 if ( p + 4 > p_limit ||
1174 num_glyphs > (FT_ULong)( ( ( p_limit - p ) >> 2 ) - 1 ) )
1177 for ( mm = 0; mm < num_glyphs; mm++ )
1179 FT_UInt gindex = FT_NEXT_USHORT( p );
1182 if ( gindex == glyph_index )
1184 image_start = FT_NEXT_USHORT( p );
1186 image_end = FT_PEEK_USHORT( p );
1192 if ( mm >= num_glyphs )
1197 case 5: /* constant metrics with sparse glyph codes */
1200 FT_ULong image_size, mm, num_glyphs;
1203 if ( p + 16 > p_limit )
1206 image_size = FT_NEXT_ULONG( p );
1208 if ( tt_sbit_decoder_load_metrics( decoder, &p, p_limit, 1 ) )
1211 num_glyphs = FT_NEXT_ULONG( p );
1213 /* overflow check for p + 2 * num_glyphs */
1214 if ( num_glyphs > (FT_ULong)( ( p_limit - p ) >> 1 ) )
1217 for ( mm = 0; mm < num_glyphs; mm++ )
1219 FT_UInt gindex = FT_NEXT_USHORT( p );
1222 if ( gindex == glyph_index )
1226 if ( mm >= num_glyphs )
1229 image_start = image_size * mm;
1230 image_end = image_start + image_size;
1238 if ( image_start > image_end )
1241 image_end -= image_start;
1242 image_start = image_offset + image_start;
1244 FT_TRACE3(( "tt_sbit_decoder_load_image:"
1245 " found sbit (format %d) for glyph index %d\n",
1246 image_format, glyph_index ));
1248 return tt_sbit_decoder_load_bitmap( decoder,
1256 return FT_THROW( Invalid_Table );
1259 FT_TRACE4(( "tt_sbit_decoder_load_image:"
1260 " no sbit found for glyph index %d\n", glyph_index ));
1262 return FT_THROW( Invalid_Argument );
1267 tt_face_load_sbix_image( TT_Face face,
1268 FT_ULong strike_index,
1269 FT_UInt glyph_index,
1272 TT_SBit_MetricsRec *metrics )
1274 FT_UInt sbix_pos, strike_offset, glyph_start, glyph_end;
1275 FT_ULong table_size;
1276 FT_Int originOffsetX, originOffsetY;
1278 FT_Int recurse_depth = 0;
1287 metrics->height = 0;
1289 p = face->sbit_table + 8 + 4 * strike_index;
1290 strike_offset = FT_NEXT_ULONG( p );
1292 error = face->goto_table( face, TTAG_sbix, stream, &table_size );
1295 sbix_pos = FT_STREAM_POS();
1298 if ( glyph_index > (FT_UInt)face->root.num_glyphs )
1299 return FT_THROW( Invalid_Argument );
1301 if ( strike_offset >= table_size ||
1302 table_size - strike_offset < 4 + glyph_index * 4 + 8 )
1303 return FT_THROW( Invalid_File_Format );
1305 if ( FT_STREAM_SEEK( sbix_pos + strike_offset + 4 + glyph_index * 4 ) ||
1306 FT_FRAME_ENTER( 8 ) )
1309 glyph_start = FT_GET_ULONG();
1310 glyph_end = FT_GET_ULONG();
1314 if ( glyph_start == glyph_end )
1315 return FT_THROW( Invalid_Argument );
1316 if ( glyph_start > glyph_end ||
1317 glyph_end - glyph_start < 8 ||
1318 table_size - strike_offset < glyph_end )
1319 return FT_THROW( Invalid_File_Format );
1321 if ( FT_STREAM_SEEK( sbix_pos + strike_offset + glyph_start ) ||
1322 FT_FRAME_ENTER( glyph_end - glyph_start ) )
1325 originOffsetX = FT_GET_SHORT();
1326 originOffsetY = FT_GET_SHORT();
1328 graphicType = FT_GET_TAG4();
1330 switch ( graphicType )
1332 case FT_MAKE_TAG( 'd', 'u', 'p', 'e' ):
1333 if ( recurse_depth < 4 )
1335 glyph_index = FT_GET_USHORT();
1340 error = FT_THROW( Invalid_File_Format );
1343 case FT_MAKE_TAG( 'p', 'n', 'g', ' ' ):
1344 #ifdef FT_CONFIG_OPTION_USE_PNG
1345 error = Load_SBit_Png( face->root.glyph,
1352 glyph_end - glyph_start - 8,
1355 error = FT_THROW( Unimplemented_Feature );
1359 case FT_MAKE_TAG( 'j', 'p', 'g', ' ' ):
1360 case FT_MAKE_TAG( 't', 'i', 'f', 'f' ):
1361 case FT_MAKE_TAG( 'r', 'g', 'b', 'l' ): /* used on iOS 7.1 */
1362 error = FT_THROW( Unknown_File_Format );
1366 error = FT_THROW( Unimplemented_Feature );
1378 tt_face_get_metrics( face, FALSE, glyph_index, &abearing, &aadvance );
1380 metrics->horiBearingX = (FT_Short)originOffsetX;
1381 metrics->horiBearingY = (FT_Short)( -originOffsetY + metrics->height );
1382 metrics->horiAdvance = (FT_Short)( aadvance *
1383 face->root.size->metrics.x_ppem /
1384 face->header.Units_Per_EM );
1390 FT_LOCAL( FT_Error )
1391 tt_face_load_sbit_image( TT_Face face,
1392 FT_ULong strike_index,
1393 FT_UInt glyph_index,
1397 TT_SBit_MetricsRec *metrics )
1399 FT_Error error = FT_Err_Ok;
1402 switch ( (FT_UInt)face->sbit_table_type )
1404 case TT_SBIT_TABLE_TYPE_EBLC:
1405 case TT_SBIT_TABLE_TYPE_CBLC:
1407 TT_SBitDecoderRec decoder[1];
1410 error = tt_sbit_decoder_init( decoder, face, strike_index, metrics );
1413 error = tt_sbit_decoder_load_image( decoder,
1417 tt_sbit_decoder_done( decoder );
1422 case TT_SBIT_TABLE_TYPE_SBIX:
1423 error = tt_face_load_sbix_image( face,
1432 error = FT_THROW( Unknown_File_Format );
1436 /* Flatten color bitmaps if color was not requested. */
1438 !( load_flags & FT_LOAD_COLOR ) &&
1439 map->pixel_mode == FT_PIXEL_MODE_BGRA )
1442 FT_Library library = face->root.glyph->library;
1445 FT_Bitmap_New( &new_map );
1447 /* Convert to 8bit grayscale. */
1448 error = FT_Bitmap_Convert( library, map, &new_map, 1 );
1450 FT_Bitmap_Done( library, &new_map );
1453 map->pixel_mode = new_map.pixel_mode;
1454 map->pitch = new_map.pitch;
1455 map->num_grays = new_map.num_grays;
1457 ft_glyphslot_set_bitmap( face->root.glyph, new_map.buffer );
1458 face->root.glyph->internal->flags |= FT_GLYPH_OWN_BITMAP;