1 /***************************************************************************/
5 /* TrueType and OpenType embedded bitmap support (body). */
7 /* Copyright 2005-2018 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
29 #ifdef TT_CONFIG_OPTION_EMBEDDED_BITMAPS
39 /*************************************************************************/
41 /* The macro FT_COMPONENT is used in trace mode. It is an implicit */
42 /* parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log */
43 /* messages during execution. */
46 #define FT_COMPONENT trace_ttsbit
49 FT_LOCAL_DEF( FT_Error )
50 tt_face_load_sbit( TT_Face face,
58 face->sbit_table = NULL;
59 face->sbit_table_size = 0;
60 face->sbit_table_type = TT_SBIT_TABLE_TYPE_NONE;
61 face->sbit_num_strikes = 0;
63 error = face->goto_table( face, TTAG_CBLC, stream, &table_size );
65 face->sbit_table_type = TT_SBIT_TABLE_TYPE_CBLC;
68 error = face->goto_table( face, TTAG_EBLC, stream, &table_size );
70 error = face->goto_table( face, TTAG_bloc, stream, &table_size );
72 face->sbit_table_type = TT_SBIT_TABLE_TYPE_EBLC;
77 error = face->goto_table( face, TTAG_sbix, stream, &table_size );
79 face->sbit_table_type = TT_SBIT_TABLE_TYPE_SBIX;
86 FT_ERROR(( "tt_face_load_sbit_strikes: table too short\n" ));
87 error = FT_THROW( Invalid_File_Format );
91 table_start = FT_STREAM_POS();
93 switch ( (FT_UInt)face->sbit_table_type )
95 case TT_SBIT_TABLE_TYPE_EBLC:
96 case TT_SBIT_TABLE_TYPE_CBLC:
100 FT_ULong num_strikes;
104 if ( FT_FRAME_EXTRACT( table_size, face->sbit_table ) )
107 face->sbit_table_size = table_size;
109 p = face->sbit_table;
111 version = FT_NEXT_LONG( p );
112 num_strikes = FT_NEXT_ULONG( p );
114 /* there's at least one font (FZShuSong-Z01, version 3) */
115 /* that uses the wrong byte order for the `version' field */
116 if ( ( (FT_ULong)version & 0xFFFF0000UL ) != 0x00020000UL &&
117 ( (FT_ULong)version & 0x0000FFFFUL ) != 0x00000200UL &&
118 ( (FT_ULong)version & 0xFFFF0000UL ) != 0x00030000UL &&
119 ( (FT_ULong)version & 0x0000FFFFUL ) != 0x00000300UL )
121 error = FT_THROW( Unknown_File_Format );
125 if ( num_strikes >= 0x10000UL )
127 error = FT_THROW( Invalid_File_Format );
132 * Count the number of strikes available in the table. We are a bit
133 * paranoid there and don't trust the data.
135 count = (FT_UInt)num_strikes;
136 if ( 8 + 48UL * count > table_size )
137 count = (FT_UInt)( ( table_size - 8 ) / 48 );
139 face->sbit_num_strikes = count;
143 case TT_SBIT_TABLE_TYPE_SBIX:
147 FT_ULong num_strikes;
151 if ( FT_FRAME_ENTER( 8 ) )
154 version = FT_GET_USHORT();
155 flags = FT_GET_USHORT();
156 num_strikes = FT_GET_ULONG();
162 error = FT_THROW( Unknown_File_Format );
166 /* Bit 0 must always be `1'. */
167 /* Bit 1 controls the overlay of bitmaps with outlines. */
168 /* All other bits should be zero. */
169 if ( !( flags == 1 || flags == 3 ) ||
170 num_strikes >= 0x10000UL )
172 error = FT_THROW( Invalid_File_Format );
176 /* we currently don't support bit 1; however, it is better to */
177 /* draw at least something... */
179 FT_TRACE1(( "tt_face_load_sbit_strikes:"
180 " sbix overlay not supported yet\n"
182 " expect bad rendering results\n" ));
185 * Count the number of strikes available in the table. We are a bit
186 * paranoid there and don't trust the data.
188 count = (FT_UInt)num_strikes;
189 if ( 8 + 4UL * count > table_size )
190 count = (FT_UInt)( ( table_size - 8 ) / 4 );
192 if ( FT_STREAM_SEEK( FT_STREAM_POS() - 8 ) )
195 face->sbit_table_size = 8 + count * 4;
196 if ( FT_FRAME_EXTRACT( face->sbit_table_size, face->sbit_table ) )
199 face->sbit_num_strikes = count;
204 /* we ignore unknown table formats */
205 error = FT_THROW( Unknown_File_Format );
210 FT_TRACE3(( "tt_face_load_sbit_strikes: found %u strikes\n",
211 face->sbit_num_strikes ));
213 face->ebdt_start = 0;
216 if ( face->sbit_table_type == TT_SBIT_TABLE_TYPE_SBIX )
218 /* the `sbix' table is self-contained; */
219 /* it has no associated data table */
220 face->ebdt_start = table_start;
221 face->ebdt_size = table_size;
223 else if ( face->sbit_table_type != TT_SBIT_TABLE_TYPE_NONE )
228 error = face->goto_table( face, TTAG_CBDT, stream, &ebdt_size );
230 error = face->goto_table( face, TTAG_EBDT, stream, &ebdt_size );
232 error = face->goto_table( face, TTAG_bdat, stream, &ebdt_size );
236 face->ebdt_start = FT_STREAM_POS();
237 face->ebdt_size = ebdt_size;
241 if ( !face->ebdt_size )
243 FT_TRACE2(( "tt_face_load_sbit_strikes:"
244 " no embedded bitmap data table found;\n"
246 " resetting number of strikes to zero\n" ));
247 face->sbit_num_strikes = 0;
255 if ( face->sbit_table )
256 FT_FRAME_RELEASE( face->sbit_table );
257 face->sbit_table_size = 0;
258 face->sbit_table_type = TT_SBIT_TABLE_TYPE_NONE;
266 tt_face_free_sbit( TT_Face face )
268 FT_Stream stream = face->root.stream;
271 FT_FRAME_RELEASE( face->sbit_table );
272 face->sbit_table_size = 0;
273 face->sbit_table_type = TT_SBIT_TABLE_TYPE_NONE;
274 face->sbit_num_strikes = 0;
278 FT_LOCAL_DEF( FT_Error )
279 tt_face_set_sbit_strike( TT_Face face,
281 FT_ULong* astrike_index )
283 return FT_Match_Size( (FT_Face)face, req, 0, astrike_index );
287 FT_LOCAL_DEF( FT_Error )
288 tt_face_load_strike_metrics( TT_Face face,
289 FT_ULong strike_index,
290 FT_Size_Metrics* metrics )
292 /* we have to test for the existence of `sbit_strike_map' */
293 /* because the function gets also used at the very beginning */
294 /* to construct `sbit_strike_map' itself */
295 if ( face->sbit_strike_map )
297 if ( strike_index >= (FT_ULong)face->root.num_fixed_sizes )
298 return FT_THROW( Invalid_Argument );
300 /* map to real index */
301 strike_index = face->sbit_strike_map[strike_index];
305 if ( strike_index >= (FT_ULong)face->sbit_num_strikes )
306 return FT_THROW( Invalid_Argument );
309 switch ( (FT_UInt)face->sbit_table_type )
311 case TT_SBIT_TABLE_TYPE_EBLC:
312 case TT_SBIT_TABLE_TYPE_CBLC:
315 FT_Char max_before_bl;
316 FT_Char min_after_bl;
319 strike = face->sbit_table + 8 + strike_index * 48;
321 metrics->x_ppem = (FT_UShort)strike[44];
322 metrics->y_ppem = (FT_UShort)strike[45];
324 metrics->ascender = (FT_Char)strike[16] * 64; /* hori.ascender */
325 metrics->descender = (FT_Char)strike[17] * 64; /* hori.descender */
327 /* Due to fuzzy wording in the EBLC documentation, we find both */
328 /* positive and negative values for `descender'. Additionally, */
329 /* many fonts have both `ascender' and `descender' set to zero */
330 /* (which is definitely wrong). MS Windows simply ignores all */
331 /* those values... For these reasons we apply some heuristics */
332 /* to get a reasonable, non-zero value for the height. */
334 max_before_bl = (FT_Char)strike[24];
335 min_after_bl = (FT_Char)strike[25];
337 if ( metrics->descender > 0 )
339 /* compare sign of descender with `min_after_bl' */
340 if ( min_after_bl < 0 )
341 metrics->descender = -metrics->descender;
344 else if ( metrics->descender == 0 )
346 if ( metrics->ascender == 0 )
348 FT_TRACE2(( "tt_face_load_strike_metrics:"
349 " sanitizing invalid ascender and descender\n"
351 " values for strike %d (%dppem, %dppem)\n",
353 metrics->x_ppem, metrics->y_ppem ));
355 /* sanitize buggy ascender and descender values */
356 if ( max_before_bl || min_after_bl )
358 metrics->ascender = max_before_bl * 64;
359 metrics->descender = min_after_bl * 64;
363 metrics->ascender = metrics->y_ppem * 64;
364 metrics->descender = 0;
371 ; /* if we have a negative descender, simply use it */
374 metrics->height = metrics->ascender - metrics->descender;
375 if ( metrics->height == 0 )
377 FT_TRACE2(( "tt_face_load_strike_metrics:"
378 " sanitizing invalid height value\n"
380 " for strike (%d, %d)\n",
381 metrics->x_ppem, metrics->y_ppem ));
382 metrics->height = metrics->y_ppem * 64;
383 metrics->descender = metrics->ascender - metrics->height;
386 /* Is this correct? */
387 metrics->max_advance = ( (FT_Char)strike[22] + /* min_origin_SB */
388 strike[18] + /* max_width */
389 (FT_Char)strike[23] /* min_advance_SB */
392 /* set the scale values (in 16.16 units) so advances */
393 /* from the hmtx and vmtx table are scaled correctly */
394 metrics->x_scale = FT_MulDiv( metrics->x_ppem,
396 face->header.Units_Per_EM );
397 metrics->y_scale = FT_MulDiv( metrics->y_ppem,
399 face->header.Units_Per_EM );
404 case TT_SBIT_TABLE_TYPE_SBIX:
406 FT_Stream stream = face->root.stream;
408 FT_UShort upem, ppem, resolution;
410 FT_Pos ppem_; /* to reduce casts */
416 p = face->sbit_table + 8 + 4 * strike_index;
417 offset = FT_NEXT_ULONG( p );
419 if ( offset + 4 > face->ebdt_size )
420 return FT_THROW( Invalid_File_Format );
422 if ( FT_STREAM_SEEK( face->ebdt_start + offset ) ||
423 FT_FRAME_ENTER( 4 ) )
426 ppem = FT_GET_USHORT();
427 resolution = FT_GET_USHORT();
429 FT_UNUSED( resolution ); /* What to do with this? */
433 upem = face->header.Units_Per_EM;
434 hori = &face->horizontal;
436 metrics->x_ppem = ppem;
437 metrics->y_ppem = ppem;
439 ppem_ = (FT_Pos)ppem;
442 FT_MulDiv( hori->Ascender, ppem_ * 64, upem );
444 FT_MulDiv( hori->Descender, ppem_ * 64, upem );
446 FT_MulDiv( hori->Ascender - hori->Descender + hori->Line_Gap,
448 metrics->max_advance =
449 FT_MulDiv( hori->advance_Width_Max, ppem_ * 64, upem );
451 /* set the scale values (in 16.16 units) so advances */
452 /* from the hmtx and vmtx table are scaled correctly */
453 metrics->x_scale = FT_MulDiv( metrics->x_ppem,
455 face->header.Units_Per_EM );
456 metrics->y_scale = FT_MulDiv( metrics->y_ppem,
458 face->header.Units_Per_EM );
464 return FT_THROW( Unknown_File_Format );
469 typedef struct TT_SBitDecoderRec_
474 TT_SBit_Metrics metrics;
475 FT_Bool metrics_loaded;
476 FT_Bool bitmap_allocated;
482 FT_ULong strike_index_array;
483 FT_ULong strike_index_count;
487 } TT_SBitDecoderRec, *TT_SBitDecoder;
491 tt_sbit_decoder_init( TT_SBitDecoder decoder,
493 FT_ULong strike_index,
494 TT_SBit_MetricsRec* metrics )
496 FT_Error error = FT_ERR( Table_Missing );
497 FT_Stream stream = face->root.stream;
500 strike_index = face->sbit_strike_map[strike_index];
502 if ( !face->ebdt_size )
504 if ( FT_STREAM_SEEK( face->ebdt_start ) )
507 decoder->face = face;
508 decoder->stream = stream;
509 decoder->bitmap = &face->root.glyph->bitmap;
510 decoder->metrics = metrics;
512 decoder->metrics_loaded = 0;
513 decoder->bitmap_allocated = 0;
515 decoder->ebdt_start = face->ebdt_start;
516 decoder->ebdt_size = face->ebdt_size;
518 decoder->eblc_base = face->sbit_table;
519 decoder->eblc_limit = face->sbit_table + face->sbit_table_size;
521 /* now find the strike corresponding to the index */
526 if ( 8 + 48 * strike_index + 3 * 4 + 34 + 1 > face->sbit_table_size )
528 error = FT_THROW( Invalid_File_Format );
532 p = decoder->eblc_base + 8 + 48 * strike_index;
534 decoder->strike_index_array = FT_NEXT_ULONG( p );
536 decoder->strike_index_count = FT_NEXT_ULONG( p );
538 decoder->bit_depth = *p;
540 /* decoder->strike_index_array + */
541 /* 8 * decoder->strike_index_count > face->sbit_table_size ? */
542 if ( decoder->strike_index_array > face->sbit_table_size ||
543 decoder->strike_index_count >
544 ( face->sbit_table_size - decoder->strike_index_array ) / 8 )
545 error = FT_THROW( Invalid_File_Format );
554 tt_sbit_decoder_done( TT_SBitDecoder decoder )
556 FT_UNUSED( decoder );
561 tt_sbit_decoder_alloc_bitmap( TT_SBitDecoder decoder,
562 FT_Bool metrics_only )
564 FT_Error error = FT_Err_Ok;
565 FT_UInt width, height;
566 FT_Bitmap* map = decoder->bitmap;
570 if ( !decoder->metrics_loaded )
572 error = FT_THROW( Invalid_Argument );
576 width = decoder->metrics->width;
577 height = decoder->metrics->height;
582 switch ( decoder->bit_depth )
585 map->pixel_mode = FT_PIXEL_MODE_MONO;
586 map->pitch = (int)( ( map->width + 7 ) >> 3 );
591 map->pixel_mode = FT_PIXEL_MODE_GRAY2;
592 map->pitch = (int)( ( map->width + 3 ) >> 2 );
597 map->pixel_mode = FT_PIXEL_MODE_GRAY4;
598 map->pitch = (int)( ( map->width + 1 ) >> 1 );
603 map->pixel_mode = FT_PIXEL_MODE_GRAY;
604 map->pitch = (int)( map->width );
605 map->num_grays = 256;
609 map->pixel_mode = FT_PIXEL_MODE_BGRA;
610 map->pitch = (int)( map->width * 4 );
611 map->num_grays = 256;
615 error = FT_THROW( Invalid_File_Format );
619 size = map->rows * (FT_ULong)map->pitch;
621 /* check that there is no empty image */
623 goto Exit; /* exit successfully! */
626 goto Exit; /* only metrics are requested */
628 error = ft_glyphslot_alloc_bitmap( decoder->face->root.glyph, size );
632 decoder->bitmap_allocated = 1;
640 tt_sbit_decoder_load_metrics( TT_SBitDecoder decoder,
646 TT_SBit_Metrics metrics = decoder->metrics;
652 metrics->height = p[0];
653 metrics->width = p[1];
654 metrics->horiBearingX = (FT_Char)p[2];
655 metrics->horiBearingY = (FT_Char)p[3];
656 metrics->horiAdvance = p[4];
664 metrics->vertBearingX = (FT_Char)p[0];
665 metrics->vertBearingY = (FT_Char)p[1];
666 metrics->vertAdvance = p[2];
672 /* avoid uninitialized data in case there is no vertical info -- */
673 metrics->vertBearingX = 0;
674 metrics->vertBearingY = 0;
675 metrics->vertAdvance = 0;
678 decoder->metrics_loaded = 1;
683 FT_TRACE1(( "tt_sbit_decoder_load_metrics: broken table\n" ));
684 return FT_THROW( Invalid_Argument );
688 /* forward declaration */
690 tt_sbit_decoder_load_image( TT_SBitDecoder decoder,
694 FT_UInt recurse_count,
695 FT_Bool metrics_only );
697 typedef FT_Error (*TT_SBitDecoder_LoadFunc)(
698 TT_SBitDecoder decoder,
703 FT_UInt recurse_count );
707 tt_sbit_decoder_load_byte_aligned( TT_SBitDecoder decoder,
712 FT_UInt recurse_count )
714 FT_Error error = FT_Err_Ok;
716 FT_Int pitch, width, height, line_bits, h;
717 FT_UInt bit_height, bit_width;
720 FT_UNUSED( recurse_count );
723 /* check that we can write the glyph into the bitmap */
724 bitmap = decoder->bitmap;
725 bit_width = bitmap->width;
726 bit_height = bitmap->rows;
727 pitch = bitmap->pitch;
728 line = bitmap->buffer;
730 width = decoder->metrics->width;
731 height = decoder->metrics->height;
733 line_bits = width * decoder->bit_depth;
735 if ( x_pos < 0 || (FT_UInt)( x_pos + width ) > bit_width ||
736 y_pos < 0 || (FT_UInt)( y_pos + height ) > bit_height )
738 FT_TRACE1(( "tt_sbit_decoder_load_byte_aligned:"
739 " invalid bitmap dimensions\n" ));
740 error = FT_THROW( Invalid_File_Format );
744 if ( p + ( ( line_bits + 7 ) >> 3 ) * height > limit )
746 FT_TRACE1(( "tt_sbit_decoder_load_byte_aligned: broken bitmap\n" ));
747 error = FT_THROW( Invalid_File_Format );
751 /* now do the blit */
752 line += y_pos * pitch + ( x_pos >> 3 );
755 if ( x_pos == 0 ) /* the easy one */
757 for ( h = height; h > 0; h--, line += pitch )
759 FT_Byte* pwrite = line;
763 for ( w = line_bits; w >= 8; w -= 8 )
765 pwrite[0] = (FT_Byte)( pwrite[0] | *p++ );
770 pwrite[0] = (FT_Byte)( pwrite[0] | ( *p++ & ( 0xFF00U >> w ) ) );
775 for ( h = height; h > 0; h--, line += pitch )
777 FT_Byte* pwrite = line;
782 for ( w = line_bits; w >= 8; w -= 8 )
784 wval = (FT_UInt)( wval | *p++ );
785 pwrite[0] = (FT_Byte)( pwrite[0] | ( wval >> x_pos ) );
791 wval = (FT_UInt)( wval | ( *p++ & ( 0xFF00U >> w ) ) );
793 /* all bits read and there are `x_pos + w' bits to be written */
795 pwrite[0] = (FT_Byte)( pwrite[0] | ( wval >> x_pos ) );
801 pwrite[0] = (FT_Byte)( pwrite[0] | ( wval >> x_pos ) );
808 FT_TRACE3(( "tt_sbit_decoder_load_byte_aligned: loaded\n" ));
814 * Load a bit-aligned bitmap (with pointer `p') into a line-aligned bitmap
815 * (with pointer `pwrite'). In the example below, the width is 3 pixel,
816 * and `x_pos' is 1 pixel.
820 * | 7 6 5 4 3 2 1 0 | 7 6 5 4 3 2 1 0 |...
822 * +-------+ +-------+ +-------+ ...
828 * | 7 6 5 4 3 2 1 0 | .
835 * | 7 6 5 4 3 2 1 0 |
842 * | 7 6 5 4 3 2 1 0 |
849 tt_sbit_decoder_load_bit_aligned( TT_SBitDecoder decoder,
854 FT_UInt recurse_count )
856 FT_Error error = FT_Err_Ok;
858 FT_Int pitch, width, height, line_bits, h, nbits;
859 FT_UInt bit_height, bit_width;
863 FT_UNUSED( recurse_count );
866 /* check that we can write the glyph into the bitmap */
867 bitmap = decoder->bitmap;
868 bit_width = bitmap->width;
869 bit_height = bitmap->rows;
870 pitch = bitmap->pitch;
871 line = bitmap->buffer;
873 width = decoder->metrics->width;
874 height = decoder->metrics->height;
876 line_bits = width * decoder->bit_depth;
878 if ( x_pos < 0 || (FT_UInt)( x_pos + width ) > bit_width ||
879 y_pos < 0 || (FT_UInt)( y_pos + height ) > bit_height )
881 FT_TRACE1(( "tt_sbit_decoder_load_bit_aligned:"
882 " invalid bitmap dimensions\n" ));
883 error = FT_THROW( Invalid_File_Format );
887 if ( p + ( ( line_bits * height + 7 ) >> 3 ) > limit )
889 FT_TRACE1(( "tt_sbit_decoder_load_bit_aligned: broken bitmap\n" ));
890 error = FT_THROW( Invalid_File_Format );
894 if ( !line_bits || !height )
900 /* now do the blit */
902 /* adjust `line' to point to the first byte of the bitmap */
903 line += y_pos * pitch + ( x_pos >> 3 );
906 /* the higher byte of `rval' is used as a buffer */
910 for ( h = height; h > 0; h--, line += pitch )
912 FT_Byte* pwrite = line;
913 FT_Int w = line_bits;
916 /* handle initial byte (in target bitmap) specially if necessary */
919 w = ( line_bits < 8 - x_pos ) ? line_bits : 8 - x_pos;
926 else if ( nbits < w )
938 *pwrite++ |= ( ( rval >> nbits ) & 0xFF ) &
939 ( ~( 0xFFU << w ) << ( 8 - w - x_pos ) );
945 /* handle medial bytes */
946 for ( ; w >= 8; w -= 8 )
949 *pwrite++ |= ( rval >> nbits ) & 0xFF;
954 /* handle final byte if necessary */
961 *pwrite |= ( ( rval >> nbits ) & 0xFF ) & ( 0xFF00U >> w );
968 *pwrite |= ( ( rval >> nbits ) & 0xFF ) & ( 0xFF00U >> w );
976 FT_TRACE3(( "tt_sbit_decoder_load_bit_aligned: loaded\n" ));
982 tt_sbit_decoder_load_compound( TT_SBitDecoder decoder,
987 FT_UInt recurse_count )
989 FT_Error error = FT_Err_Ok;
990 FT_UInt num_components, nn;
992 FT_Char horiBearingX = (FT_Char)decoder->metrics->horiBearingX;
993 FT_Char horiBearingY = (FT_Char)decoder->metrics->horiBearingY;
994 FT_Byte horiAdvance = (FT_Byte)decoder->metrics->horiAdvance;
995 FT_Char vertBearingX = (FT_Char)decoder->metrics->vertBearingX;
996 FT_Char vertBearingY = (FT_Char)decoder->metrics->vertBearingY;
997 FT_Byte vertAdvance = (FT_Byte)decoder->metrics->vertAdvance;
1000 if ( p + 2 > limit )
1003 num_components = FT_NEXT_USHORT( p );
1004 if ( p + 4 * num_components > limit )
1006 FT_TRACE1(( "tt_sbit_decoder_load_compound: broken table\n" ));
1010 FT_TRACE3(( "tt_sbit_decoder_load_compound: loading %d component%s\n",
1012 num_components == 1 ? "" : "s" ));
1014 for ( nn = 0; nn < num_components; nn++ )
1016 FT_UInt gindex = FT_NEXT_USHORT( p );
1017 FT_Byte dx = FT_NEXT_BYTE( p );
1018 FT_Byte dy = FT_NEXT_BYTE( p );
1021 /* NB: a recursive call */
1022 error = tt_sbit_decoder_load_image( decoder,
1027 /* request full bitmap image */
1033 FT_TRACE3(( "tt_sbit_decoder_load_compound: done\n" ));
1035 decoder->metrics->horiBearingX = horiBearingX;
1036 decoder->metrics->horiBearingY = horiBearingY;
1037 decoder->metrics->horiAdvance = horiAdvance;
1038 decoder->metrics->vertBearingX = vertBearingX;
1039 decoder->metrics->vertBearingY = vertBearingY;
1040 decoder->metrics->vertAdvance = vertAdvance;
1041 decoder->metrics->width = (FT_Byte)decoder->bitmap->width;
1042 decoder->metrics->height = (FT_Byte)decoder->bitmap->rows;
1048 error = FT_THROW( Invalid_File_Format );
1053 #ifdef FT_CONFIG_OPTION_USE_PNG
1056 tt_sbit_decoder_load_png( TT_SBitDecoder decoder,
1061 FT_UInt recurse_count )
1063 FT_Error error = FT_Err_Ok;
1066 FT_UNUSED( recurse_count );
1069 if ( limit - p < 4 )
1071 FT_TRACE1(( "tt_sbit_decoder_load_png: broken bitmap\n" ));
1072 error = FT_THROW( Invalid_File_Format );
1076 png_len = FT_NEXT_ULONG( p );
1077 if ( (FT_ULong)( limit - p ) < png_len )
1079 FT_TRACE1(( "tt_sbit_decoder_load_png: broken bitmap\n" ));
1080 error = FT_THROW( Invalid_File_Format );
1084 error = Load_SBit_Png( decoder->face->root.glyph,
1089 decoder->stream->memory,
1097 FT_TRACE3(( "tt_sbit_decoder_load_png: loaded\n" ));
1101 #endif /* FT_CONFIG_OPTION_USE_PNG */
1105 tt_sbit_decoder_load_bitmap( TT_SBitDecoder decoder,
1106 FT_UInt glyph_format,
1107 FT_ULong glyph_start,
1108 FT_ULong glyph_size,
1111 FT_UInt recurse_count,
1112 FT_Bool metrics_only )
1115 FT_Stream stream = decoder->stream;
1121 /* seek into the EBDT table now */
1123 glyph_start + glyph_size > decoder->ebdt_size )
1125 error = FT_THROW( Invalid_Argument );
1129 if ( FT_STREAM_SEEK( decoder->ebdt_start + glyph_start ) ||
1130 FT_FRAME_EXTRACT( glyph_size, data ) )
1134 p_limit = p + glyph_size;
1136 /* read the data, depending on the glyph format */
1137 switch ( glyph_format )
1143 error = tt_sbit_decoder_load_metrics( decoder, &p, p_limit, 0 );
1150 error = tt_sbit_decoder_load_metrics( decoder, &p, p_limit, 1 );
1161 TT_SBitDecoder_LoadFunc loader;
1164 switch ( glyph_format )
1168 loader = tt_sbit_decoder_load_byte_aligned;
1174 /* Don't trust `glyph_format'. For example, Apple's main Korean */
1175 /* system font, `AppleMyungJo.ttf' (version 7.0d2e6), uses glyph */
1176 /* format 7, but the data is format 6. We check whether we have */
1177 /* an excessive number of bytes in the image: If it is equal to */
1178 /* the value for a byte-aligned glyph, use the other loading */
1181 /* Note that for some (width,height) combinations, where the */
1182 /* width is not a multiple of 8, the sizes for bit- and */
1183 /* byte-aligned data are equal, for example (7,7) or (15,6). We */
1184 /* then prefer what `glyph_format' specifies. */
1186 FT_UInt width = decoder->metrics->width;
1187 FT_UInt height = decoder->metrics->height;
1189 FT_UInt bit_size = ( width * height + 7 ) >> 3;
1190 FT_UInt byte_size = height * ( ( width + 7 ) >> 3 );
1193 if ( bit_size < byte_size &&
1194 byte_size == (FT_UInt)( p_limit - p ) )
1195 loader = tt_sbit_decoder_load_byte_aligned;
1197 loader = tt_sbit_decoder_load_bit_aligned;
1202 loader = tt_sbit_decoder_load_bit_aligned;
1206 if ( p + 1 > p_limit )
1209 p += 1; /* skip padding */
1213 loader = tt_sbit_decoder_load_compound;
1216 case 17: /* small metrics, PNG image data */
1217 case 18: /* big metrics, PNG image data */
1218 case 19: /* metrics in EBLC, PNG image data */
1219 #ifdef FT_CONFIG_OPTION_USE_PNG
1220 loader = tt_sbit_decoder_load_png;
1223 error = FT_THROW( Unimplemented_Feature );
1225 #endif /* FT_CONFIG_OPTION_USE_PNG */
1228 error = FT_THROW( Invalid_Table );
1232 if ( !decoder->bitmap_allocated )
1234 error = tt_sbit_decoder_alloc_bitmap( decoder, metrics_only );
1241 goto Fail; /* this is not an error */
1243 error = loader( decoder, p, p_limit, x_pos, y_pos, recurse_count );
1247 FT_FRAME_RELEASE( data );
1255 tt_sbit_decoder_load_image( TT_SBitDecoder decoder,
1256 FT_UInt glyph_index,
1259 FT_UInt recurse_count,
1260 FT_Bool metrics_only )
1262 FT_Byte* p = decoder->eblc_base + decoder->strike_index_array;
1263 FT_Byte* p_limit = decoder->eblc_limit;
1264 FT_ULong num_ranges = decoder->strike_index_count;
1265 FT_UInt start, end, index_format, image_format;
1266 FT_ULong image_start = 0, image_end = 0, image_offset;
1269 /* arbitrary recursion limit */
1270 if ( recurse_count > 100 )
1272 FT_TRACE4(( "tt_sbit_decoder_load_image:"
1273 " recursion depth exceeded\n" ));
1278 /* First, we find the correct strike range that applies to this */
1280 for ( ; num_ranges > 0; num_ranges-- )
1282 start = FT_NEXT_USHORT( p );
1283 end = FT_NEXT_USHORT( p );
1285 if ( glyph_index >= start && glyph_index <= end )
1288 p += 4; /* ignore index offset */
1293 image_offset = FT_NEXT_ULONG( p );
1295 /* overflow check */
1296 p = decoder->eblc_base + decoder->strike_index_array;
1297 if ( image_offset > (FT_ULong)( p_limit - p ) )
1301 if ( p + 8 > p_limit )
1304 /* now find the glyph's location and extend within the ebdt table */
1305 index_format = FT_NEXT_USHORT( p );
1306 image_format = FT_NEXT_USHORT( p );
1307 image_offset = FT_NEXT_ULONG ( p );
1309 switch ( index_format )
1311 case 1: /* 4-byte offsets relative to `image_offset' */
1312 p += 4 * ( glyph_index - start );
1313 if ( p + 8 > p_limit )
1316 image_start = FT_NEXT_ULONG( p );
1317 image_end = FT_NEXT_ULONG( p );
1319 if ( image_start == image_end ) /* missing glyph */
1323 case 2: /* big metrics, constant image size */
1325 FT_ULong image_size;
1328 if ( p + 12 > p_limit )
1331 image_size = FT_NEXT_ULONG( p );
1333 if ( tt_sbit_decoder_load_metrics( decoder, &p, p_limit, 1 ) )
1336 image_start = image_size * ( glyph_index - start );
1337 image_end = image_start + image_size;
1341 case 3: /* 2-byte offsets relative to 'image_offset' */
1342 p += 2 * ( glyph_index - start );
1343 if ( p + 4 > p_limit )
1346 image_start = FT_NEXT_USHORT( p );
1347 image_end = FT_NEXT_USHORT( p );
1349 if ( image_start == image_end ) /* missing glyph */
1353 case 4: /* sparse glyph array with (glyph,offset) pairs */
1355 FT_ULong mm, num_glyphs;
1358 if ( p + 4 > p_limit )
1361 num_glyphs = FT_NEXT_ULONG( p );
1363 /* overflow check for p + ( num_glyphs + 1 ) * 4 */
1364 if ( p + 4 > p_limit ||
1365 num_glyphs > (FT_ULong)( ( ( p_limit - p ) >> 2 ) - 1 ) )
1368 for ( mm = 0; mm < num_glyphs; mm++ )
1370 FT_UInt gindex = FT_NEXT_USHORT( p );
1373 if ( gindex == glyph_index )
1375 image_start = FT_NEXT_USHORT( p );
1377 image_end = FT_PEEK_USHORT( p );
1383 if ( mm >= num_glyphs )
1388 case 5: /* constant metrics with sparse glyph codes */
1391 FT_ULong image_size, mm, num_glyphs;
1394 if ( p + 16 > p_limit )
1397 image_size = FT_NEXT_ULONG( p );
1399 if ( tt_sbit_decoder_load_metrics( decoder, &p, p_limit, 1 ) )
1402 num_glyphs = FT_NEXT_ULONG( p );
1404 /* overflow check for p + 2 * num_glyphs */
1405 if ( num_glyphs > (FT_ULong)( ( p_limit - p ) >> 1 ) )
1408 for ( mm = 0; mm < num_glyphs; mm++ )
1410 FT_UInt gindex = FT_NEXT_USHORT( p );
1413 if ( gindex == glyph_index )
1417 if ( mm >= num_glyphs )
1420 image_start = image_size * mm;
1421 image_end = image_start + image_size;
1429 if ( image_start > image_end )
1432 image_end -= image_start;
1433 image_start = image_offset + image_start;
1435 FT_TRACE3(( "tt_sbit_decoder_load_image:"
1436 " found sbit (format %d) for glyph index %d\n",
1437 image_format, glyph_index ));
1439 return tt_sbit_decoder_load_bitmap( decoder,
1449 return FT_THROW( Invalid_Table );
1452 if ( recurse_count )
1454 FT_TRACE4(( "tt_sbit_decoder_load_image:"
1455 " missing subglyph sbit with glyph index %d\n",
1457 return FT_THROW( Invalid_Composite );
1460 FT_TRACE4(( "tt_sbit_decoder_load_image:"
1461 " no sbit found for glyph index %d\n", glyph_index ));
1462 return FT_THROW( Missing_Bitmap );
1467 tt_face_load_sbix_image( TT_Face face,
1468 FT_ULong strike_index,
1469 FT_UInt glyph_index,
1472 TT_SBit_MetricsRec *metrics,
1473 FT_Bool metrics_only )
1475 FT_UInt strike_offset, glyph_start, glyph_end;
1476 FT_Int originOffsetX, originOffsetY;
1478 FT_Int recurse_depth = 0;
1484 #ifndef FT_CONFIG_OPTION_USE_PNG
1485 FT_UNUSED( metrics_only );
1489 strike_index = face->sbit_strike_map[strike_index];
1492 metrics->height = 0;
1494 p = face->sbit_table + 8 + 4 * strike_index;
1495 strike_offset = FT_NEXT_ULONG( p );
1498 if ( glyph_index > (FT_UInt)face->root.num_glyphs )
1499 return FT_THROW( Invalid_Argument );
1501 if ( strike_offset >= face->ebdt_size ||
1502 face->ebdt_size - strike_offset < 4 + glyph_index * 4 + 8 )
1503 return FT_THROW( Invalid_File_Format );
1505 if ( FT_STREAM_SEEK( face->ebdt_start +
1507 glyph_index * 4 ) ||
1508 FT_FRAME_ENTER( 8 ) )
1511 glyph_start = FT_GET_ULONG();
1512 glyph_end = FT_GET_ULONG();
1516 if ( glyph_start == glyph_end )
1517 return FT_THROW( Missing_Bitmap );
1518 if ( glyph_start > glyph_end ||
1519 glyph_end - glyph_start < 8 ||
1520 face->ebdt_size - strike_offset < glyph_end )
1521 return FT_THROW( Invalid_File_Format );
1523 if ( FT_STREAM_SEEK( face->ebdt_start + strike_offset + glyph_start ) ||
1524 FT_FRAME_ENTER( glyph_end - glyph_start ) )
1527 originOffsetX = FT_GET_SHORT();
1528 originOffsetY = FT_GET_SHORT();
1530 graphicType = FT_GET_TAG4();
1532 switch ( graphicType )
1534 case FT_MAKE_TAG( 'd', 'u', 'p', 'e' ):
1535 if ( recurse_depth < 4 )
1537 glyph_index = FT_GET_USHORT();
1542 error = FT_THROW( Invalid_File_Format );
1545 case FT_MAKE_TAG( 'p', 'n', 'g', ' ' ):
1546 #ifdef FT_CONFIG_OPTION_USE_PNG
1547 error = Load_SBit_Png( face->root.glyph,
1554 glyph_end - glyph_start - 8,
1558 error = FT_THROW( Unimplemented_Feature );
1562 case FT_MAKE_TAG( 'j', 'p', 'g', ' ' ):
1563 case FT_MAKE_TAG( 't', 'i', 'f', 'f' ):
1564 case FT_MAKE_TAG( 'r', 'g', 'b', 'l' ): /* used on iOS 7.1 */
1565 error = FT_THROW( Unknown_File_Format );
1569 error = FT_THROW( Unimplemented_Feature );
1581 tt_face_get_metrics( face, FALSE, glyph_index, &abearing, &aadvance );
1583 metrics->horiBearingX = (FT_Short)originOffsetX;
1584 metrics->horiBearingY = (FT_Short)( -originOffsetY + metrics->height );
1585 metrics->horiAdvance = (FT_UShort)( aadvance *
1586 face->root.size->metrics.x_ppem /
1587 face->header.Units_Per_EM );
1593 FT_LOCAL( FT_Error )
1594 tt_face_load_sbit_image( TT_Face face,
1595 FT_ULong strike_index,
1596 FT_UInt glyph_index,
1600 TT_SBit_MetricsRec *metrics )
1602 FT_Error error = FT_Err_Ok;
1605 switch ( (FT_UInt)face->sbit_table_type )
1607 case TT_SBIT_TABLE_TYPE_EBLC:
1608 case TT_SBIT_TABLE_TYPE_CBLC:
1610 TT_SBitDecoderRec decoder[1];
1613 error = tt_sbit_decoder_init( decoder, face, strike_index, metrics );
1616 error = tt_sbit_decoder_load_image(
1622 ( load_flags & FT_LOAD_BITMAP_METRICS_ONLY ) != 0 );
1623 tt_sbit_decoder_done( decoder );
1628 case TT_SBIT_TABLE_TYPE_SBIX:
1629 error = tt_face_load_sbix_image(
1636 ( load_flags & FT_LOAD_BITMAP_METRICS_ONLY ) != 0 );
1640 error = FT_THROW( Unknown_File_Format );
1644 /* Flatten color bitmaps if color was not requested. */
1646 !( load_flags & FT_LOAD_COLOR ) &&
1647 !( load_flags & FT_LOAD_BITMAP_METRICS_ONLY ) &&
1648 map->pixel_mode == FT_PIXEL_MODE_BGRA )
1651 FT_Library library = face->root.glyph->library;
1654 FT_Bitmap_Init( &new_map );
1656 /* Convert to 8bit grayscale. */
1657 error = FT_Bitmap_Convert( library, map, &new_map, 1 );
1659 FT_Bitmap_Done( library, &new_map );
1662 map->pixel_mode = new_map.pixel_mode;
1663 map->pitch = new_map.pitch;
1664 map->num_grays = new_map.num_grays;
1666 ft_glyphslot_set_bitmap( face->root.glyph, new_map.buffer );
1667 face->root.glyph->internal->flags |= FT_GLYPH_OWN_BITMAP;
1674 #else /* !TT_CONFIG_OPTION_EMBEDDED_BITMAPS */
1676 /* ANSI C doesn't like empty source files */
1677 typedef int _tt_sbit_dummy;
1679 #endif /* !TT_CONFIG_OPTION_EMBEDDED_BITMAPS */