1 /***************************************************************************/
5 /* TrueType and OpenType embedded bitmap support (body). */
7 /* Copyright 1996-2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, */
9 /* David Turner, Robert Wilhelm, and Werner Lemberg. */
11 /* This file is part of the FreeType project, and may only be used, */
12 /* modified, and distributed under the terms of the FreeType project */
13 /* license, LICENSE.TXT. By continuing to use, modify, or distribute */
14 /* this file you indicate that you have read the license and */
15 /* understand and accept it fully. */
17 /***************************************************************************/
20 #include FT_INTERNAL_DEBUG_H
21 #include FT_INTERNAL_STREAM_H
22 #include FT_TRUETYPE_TAGS_H
25 * Alas, the memory-optimized sbit loader can't be used when implementing
26 * the `old internals' hack
28 #ifndef FT_CONFIG_OPTION_OLD_INTERNALS
32 #else /* FT_CONFIG_OPTION_OLD_INTERNALS */
35 #include FT_INTERNAL_DEBUG_H
36 #include FT_INTERNAL_STREAM_H
37 #include FT_TRUETYPE_TAGS_H
43 /*************************************************************************/
45 /* The macro FT_COMPONENT is used in trace mode. It is an implicit */
46 /* parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log */
47 /* messages during execution. */
50 #define FT_COMPONENT trace_ttsbit
53 /*************************************************************************/
59 /* Blits a bitmap from an input stream into a given target. Supports */
60 /* x and y offsets as well as byte padded lines. */
63 /* target :: The target bitmap/pixmap. */
65 /* source :: The input packed bitmap data. */
67 /* line_bits :: The number of bits per line. */
69 /* byte_padded :: A flag which is true if lines are byte-padded. */
71 /* x_offset :: The horizontal offset. */
73 /* y_offset :: The vertical offset. */
76 /* IMPORTANT: The x and y offsets are relative to the top corner of */
77 /* the target bitmap (unlike the normal TrueType */
78 /* convention). A positive y offset indicates a downwards */
82 blit_sbit( FT_Bitmap* target,
88 FT_Int source_height )
98 /* first of all, compute starting write position */
99 line_incr = target->pitch;
100 line_buff = target->buffer;
103 line_buff -= line_incr * ( target->rows - 1 );
105 line_buff += ( x_offset >> 3 ) + y_offset * line_incr;
107 /***********************************************************************/
109 /* We use the extra-classic `accumulator' trick to extract the bits */
110 /* from the source byte stream. */
112 /* Namely, the variable `acc' is a 16-bit accumulator containing the */
113 /* last `loaded' bits from the input stream. The bits are shifted to */
114 /* the upmost position in `acc'. */
116 /***********************************************************************/
118 acc = 0; /* clear accumulator */
119 loaded = 0; /* no bits were loaded */
121 for ( height = source_height; height > 0; height-- )
123 FT_Byte* cur = line_buff; /* current write cursor */
124 FT_Int count = line_bits; /* # of bits to extract per line */
125 FT_Byte shift = (FT_Byte)( x_offset & 7 ); /* current write shift */
126 FT_Byte space = (FT_Byte)( 8 - shift );
129 /* first of all, read individual source bytes */
139 /* ensure that there are at least 8 bits in the accumulator */
142 acc |= (FT_UShort)((FT_UShort)*source++ << ( 8 - loaded ));
146 /* now write one byte */
147 val = (FT_Byte)( acc >> 8 );
150 cur[0] |= (FT_Byte)( val >> shift );
151 cur[1] |= (FT_Byte)( val << space );
157 acc <<= 8; /* remove bits from accumulator */
161 } while ( count >= 0 );
164 /* restore `count' to correct value */
168 /* now write remaining bits (count < 8) */
174 /* ensure that there are at least `count' bits in the accumulator */
175 if ( (FT_Int)loaded < count )
177 acc |= (FT_UShort)((FT_UShort)*source++ << ( 8 - loaded ));
181 /* now write remaining bits */
182 val = (FT_Byte)( ( (FT_Byte)( acc >> 8 ) ) & ~( 0xFF >> count ) );
183 cur[0] |= (FT_Byte)( val >> shift );
186 cur[1] |= (FT_Byte)( val << space );
192 /* now, skip to next line */
196 loaded = 0; /* clear accumulator on byte-padded lines */
199 line_buff += line_incr;
204 static const FT_Frame_Field sbit_metrics_fields[] =
207 #define FT_STRUCTURE TT_SBit_MetricsRec
210 FT_FRAME_BYTE( height ),
211 FT_FRAME_BYTE( width ),
213 FT_FRAME_CHAR( horiBearingX ),
214 FT_FRAME_CHAR( horiBearingY ),
215 FT_FRAME_BYTE( horiAdvance ),
217 FT_FRAME_CHAR( vertBearingX ),
218 FT_FRAME_CHAR( vertBearingY ),
219 FT_FRAME_BYTE( vertAdvance ),
224 /*************************************************************************/
227 /* Load_SBit_Const_Metrics */
230 /* Loads the metrics for `EBLC' index tables format 2 and 5. */
233 /* range :: The target range. */
235 /* stream :: The input stream. */
238 /* FreeType error code. 0 means success. */
241 Load_SBit_Const_Metrics( TT_SBit_Range range,
247 if ( FT_READ_ULONG( range->image_size ) )
250 return FT_STREAM_READ_FIELDS( sbit_metrics_fields, &range->metrics );
254 /*************************************************************************/
257 /* Load_SBit_Range_Codes */
260 /* Loads the range codes for `EBLC' index tables format 4 and 5. */
263 /* range :: The target range. */
265 /* stream :: The input stream. */
267 /* load_offsets :: A flag whether to load the glyph offset table. */
270 /* FreeType error code. 0 means success. */
273 Load_SBit_Range_Codes( TT_SBit_Range range,
275 FT_Bool load_offsets )
278 FT_ULong count, n, size;
279 FT_Memory memory = stream->memory;
282 if ( FT_READ_ULONG( count ) )
285 range->num_glyphs = count;
287 /* Allocate glyph offsets table if needed */
290 if ( FT_NEW_ARRAY( range->glyph_offsets, count ) )
298 /* Allocate glyph codes table and access frame */
299 if ( FT_NEW_ARRAY ( range->glyph_codes, count ) ||
300 FT_FRAME_ENTER( size ) )
303 for ( n = 0; n < count; n++ )
305 range->glyph_codes[n] = FT_GET_USHORT();
308 range->glyph_offsets[n] = (FT_ULong)range->image_offset +
319 /*************************************************************************/
322 /* Load_SBit_Range */
325 /* Loads a given `EBLC' index/range table. */
328 /* range :: The target range. */
330 /* stream :: The input stream. */
333 /* FreeType error code. 0 means success. */
336 Load_SBit_Range( TT_SBit_Range range,
340 FT_Memory memory = stream->memory;
343 switch( range->index_format )
345 case 1: /* variable metrics with 4-byte offsets */
346 case 3: /* variable metrics with 2-byte offsets */
348 FT_ULong num_glyphs, n;
350 FT_Bool large = FT_BOOL( range->index_format == 1 );
354 if ( range->last_glyph < range->first_glyph )
356 error = SFNT_Err_Invalid_File_Format;
360 num_glyphs = range->last_glyph - range->first_glyph + 1L;
361 range->num_glyphs = num_glyphs;
362 num_glyphs++; /* XXX: BEWARE - see spec */
364 size_elem = large ? 4 : 2;
366 if ( FT_NEW_ARRAY( range->glyph_offsets, num_glyphs ) ||
367 FT_FRAME_ENTER( num_glyphs * size_elem ) )
370 for ( n = 0; n < num_glyphs; n++ )
371 range->glyph_offsets[n] = (FT_ULong)( range->image_offset +
372 ( large ? FT_GET_ULONG()
373 : FT_GET_USHORT() ) );
378 case 2: /* all glyphs have identical metrics */
379 error = Load_SBit_Const_Metrics( range, stream );
383 error = Load_SBit_Range_Codes( range, stream, 1 );
387 error = Load_SBit_Const_Metrics( range, stream );
389 error = Load_SBit_Range_Codes( range, stream, 0 );
393 error = SFNT_Err_Invalid_File_Format;
401 /*************************************************************************/
404 /* tt_face_load_eblc */
407 /* Loads the table of embedded bitmap sizes for this face. */
410 /* face :: The target face object. */
412 /* stream :: The input stream. */
415 /* FreeType error code. 0 means success. */
417 FT_LOCAL_DEF( FT_Error )
418 tt_face_load_eblc( TT_Face face,
421 FT_Error error = SFNT_Err_Ok;
422 FT_Memory memory = stream->memory;
424 FT_ULong num_strikes;
427 static const FT_Frame_Field sbit_line_metrics_fields[] =
430 #define FT_STRUCTURE TT_SBit_LineMetricsRec
432 /* no FT_FRAME_START */
433 FT_FRAME_CHAR( ascender ),
434 FT_FRAME_CHAR( descender ),
435 FT_FRAME_BYTE( max_width ),
437 FT_FRAME_CHAR( caret_slope_numerator ),
438 FT_FRAME_CHAR( caret_slope_denominator ),
439 FT_FRAME_CHAR( caret_offset ),
441 FT_FRAME_CHAR( min_origin_SB ),
442 FT_FRAME_CHAR( min_advance_SB ),
443 FT_FRAME_CHAR( max_before_BL ),
444 FT_FRAME_CHAR( min_after_BL ),
445 FT_FRAME_CHAR( pads[0] ),
446 FT_FRAME_CHAR( pads[1] ),
450 static const FT_Frame_Field strike_start_fields[] =
453 #define FT_STRUCTURE TT_SBit_StrikeRec
455 /* no FT_FRAME_START */
456 FT_FRAME_ULONG( ranges_offset ),
458 FT_FRAME_ULONG( num_ranges ),
459 FT_FRAME_ULONG( color_ref ),
463 static const FT_Frame_Field strike_end_fields[] =
465 /* no FT_FRAME_START */
466 FT_FRAME_USHORT( start_glyph ),
467 FT_FRAME_USHORT( end_glyph ),
468 FT_FRAME_BYTE ( x_ppem ),
469 FT_FRAME_BYTE ( y_ppem ),
470 FT_FRAME_BYTE ( bit_depth ),
471 FT_FRAME_CHAR ( flags ),
476 face->num_sbit_strikes = 0;
478 /* this table is optional */
479 error = face->goto_table( face, TTAG_EBLC, stream, 0 );
481 error = face->goto_table( face, TTAG_bloc, stream, 0 );
485 table_base = FT_STREAM_POS();
486 if ( FT_FRAME_ENTER( 8L ) )
489 version = FT_GET_LONG();
490 num_strikes = FT_GET_ULONG();
494 /* check version number and strike count */
495 if ( version != 0x00020000L ||
496 num_strikes >= 0x10000L )
498 FT_ERROR(( "tt_face_load_sbit_strikes: invalid table version\n" ));
499 error = SFNT_Err_Invalid_File_Format;
504 /* allocate the strikes table */
505 if ( FT_NEW_ARRAY( face->sbit_strikes, num_strikes ) )
508 face->num_sbit_strikes = num_strikes;
510 /* now read each strike table separately */
512 TT_SBit_Strike strike = face->sbit_strikes;
513 FT_ULong count = num_strikes;
516 if ( FT_FRAME_ENTER( 48L * num_strikes ) )
521 if ( FT_STREAM_READ_FIELDS( strike_start_fields, strike ) ||
522 FT_STREAM_READ_FIELDS( sbit_line_metrics_fields, &strike->hori ) ||
523 FT_STREAM_READ_FIELDS( sbit_line_metrics_fields, &strike->vert ) ||
524 FT_STREAM_READ_FIELDS( strike_end_fields, strike ) )
534 /* allocate the index ranges for each strike table */
536 TT_SBit_Strike strike = face->sbit_strikes;
537 FT_ULong count = num_strikes;
543 FT_ULong count2 = strike->num_ranges;
546 /* read each range */
547 if ( FT_STREAM_SEEK( table_base + strike->ranges_offset ) ||
548 FT_FRAME_ENTER( strike->num_ranges * 8L ) )
551 if ( FT_NEW_ARRAY( strike->sbit_ranges, strike->num_ranges ) )
554 range = strike->sbit_ranges;
557 range->first_glyph = FT_GET_USHORT();
558 range->last_glyph = FT_GET_USHORT();
559 range->table_offset = table_base + strike->ranges_offset +
567 /* Now, read each index table */
568 count2 = strike->num_ranges;
569 range = strike->sbit_ranges;
572 /* Read the header */
573 if ( FT_STREAM_SEEK( range->table_offset ) ||
574 FT_FRAME_ENTER( 8L ) )
577 range->index_format = FT_GET_USHORT();
578 range->image_format = FT_GET_USHORT();
579 range->image_offset = FT_GET_ULONG();
583 error = Load_SBit_Range( range, stream );
601 /*************************************************************************/
604 /* tt_face_free_eblc */
607 /* Releases the embedded bitmap tables. */
610 /* face :: The target face object. */
613 tt_face_free_eblc( TT_Face face )
615 FT_Memory memory = face->root.memory;
616 TT_SBit_Strike strike = face->sbit_strikes;
617 TT_SBit_Strike strike_limit = strike + face->num_sbit_strikes;
622 for ( ; strike < strike_limit; strike++ )
624 TT_SBit_Range range = strike->sbit_ranges;
625 TT_SBit_Range range_limit = range + strike->num_ranges;
630 for ( ; range < range_limit; range++ )
632 /* release the glyph offsets and codes tables */
633 /* where appropriate */
634 FT_FREE( range->glyph_offsets );
635 FT_FREE( range->glyph_codes );
638 FT_FREE( strike->sbit_ranges );
639 strike->num_ranges = 0;
641 FT_FREE( face->sbit_strikes );
643 face->num_sbit_strikes = 0;
647 FT_LOCAL_DEF( FT_Error )
648 tt_face_set_sbit_strike( TT_Face face,
650 FT_ULong* astrike_index )
652 return FT_Match_Size( (FT_Face)face, req, 0, astrike_index );
656 FT_LOCAL_DEF( FT_Error )
657 tt_face_load_strike_metrics( TT_Face face,
658 FT_ULong strike_index,
659 FT_Size_Metrics* metrics )
661 TT_SBit_Strike strike;
664 if ( strike_index >= face->num_sbit_strikes )
665 return SFNT_Err_Invalid_Argument;
667 strike = face->sbit_strikes + strike_index;
669 metrics->x_ppem = strike->x_ppem;
670 metrics->y_ppem = strike->y_ppem;
672 metrics->ascender = strike->hori.ascender << 6;
673 metrics->descender = strike->hori.descender << 6;
675 /* XXX: Is this correct? */
676 metrics->max_advance = ( strike->hori.min_origin_SB +
677 strike->hori.max_width +
678 strike->hori.min_advance_SB ) << 6;
680 metrics->height = metrics->ascender - metrics->descender;
686 /*************************************************************************/
689 /* find_sbit_range */
692 /* Scans a given strike's ranges and return, for a given glyph */
693 /* index, the corresponding sbit range, and `EBDT' offset. */
696 /* glyph_index :: The glyph index. */
698 /* strike :: The source/current sbit strike. */
701 /* arange :: The sbit range containing the glyph index. */
703 /* aglyph_offset :: The offset of the glyph data in `EBDT' table. */
706 /* FreeType error code. 0 means the glyph index was found. */
709 find_sbit_range( FT_UInt glyph_index,
710 TT_SBit_Strike strike,
711 TT_SBit_Range *arange,
712 FT_ULong *aglyph_offset )
714 TT_SBit_RangeRec *range, *range_limit;
717 /* check whether the glyph index is within this strike's */
719 if ( glyph_index < (FT_UInt)strike->start_glyph ||
720 glyph_index > (FT_UInt)strike->end_glyph )
723 /* scan all ranges in strike */
724 range = strike->sbit_ranges;
725 range_limit = range + strike->num_ranges;
729 for ( ; range < range_limit; range++ )
731 if ( glyph_index >= (FT_UInt)range->first_glyph &&
732 glyph_index <= (FT_UInt)range->last_glyph )
734 FT_UShort delta = (FT_UShort)( glyph_index - range->first_glyph );
737 switch ( range->index_format )
741 *aglyph_offset = range->glyph_offsets[delta];
745 *aglyph_offset = range->image_offset +
746 range->image_size * delta;
755 for ( n = 0; n < range->num_glyphs; n++ )
757 if ( (FT_UInt)range->glyph_codes[n] == glyph_index )
759 if ( range->index_format == 4 )
760 *aglyph_offset = range->glyph_offsets[n];
762 *aglyph_offset = range->image_offset +
763 n * range->image_size;
775 /* return successfully! */
785 return SFNT_Err_Invalid_Argument;
789 /*************************************************************************/
792 /* tt_find_sbit_image */
795 /* Checks whether an embedded bitmap (an `sbit') exists for a given */
796 /* glyph, at a given strike. */
799 /* face :: The target face object. */
801 /* glyph_index :: The glyph index. */
803 /* strike_index :: The current strike index. */
806 /* arange :: The SBit range containing the glyph index. */
808 /* astrike :: The SBit strike containing the glyph index. */
810 /* aglyph_offset :: The offset of the glyph data in `EBDT' table. */
813 /* FreeType error code. 0 means success. Returns */
814 /* SFNT_Err_Invalid_Argument if no sbit exists for the requested */
818 tt_find_sbit_image( TT_Face face,
820 FT_ULong strike_index,
821 TT_SBit_Range *arange,
822 TT_SBit_Strike *astrike,
823 FT_ULong *aglyph_offset )
826 TT_SBit_Strike strike;
829 if ( !face->sbit_strikes ||
830 ( face->num_sbit_strikes <= strike_index ) )
833 strike = &face->sbit_strikes[strike_index];
835 error = find_sbit_range( glyph_index, strike,
836 arange, aglyph_offset );
845 /* no embedded bitmap for this glyph in face */
850 return SFNT_Err_Invalid_Argument;
854 /*************************************************************************/
857 /* tt_load_sbit_metrics */
860 /* Gets the big metrics for a given SBit. */
863 /* stream :: The input stream. */
865 /* range :: The SBit range containing the glyph. */
868 /* big_metrics :: A big SBit metrics structure for the glyph. */
871 /* FreeType error code. 0 means success. */
874 /* The stream cursor must be positioned at the glyph's offset within */
875 /* the `EBDT' table before the call. */
877 /* If the image format uses variable metrics, the stream cursor is */
878 /* positioned just after the metrics header in the `EBDT' table on */
882 tt_load_sbit_metrics( FT_Stream stream,
884 TT_SBit_Metrics metrics )
886 FT_Error error = SFNT_Err_Ok;
889 switch ( range->image_format )
894 /* variable small metrics */
896 TT_SBit_SmallMetricsRec smetrics;
898 static const FT_Frame_Field sbit_small_metrics_fields[] =
901 #define FT_STRUCTURE TT_SBit_SmallMetricsRec
904 FT_FRAME_BYTE( height ),
905 FT_FRAME_BYTE( width ),
906 FT_FRAME_CHAR( bearingX ),
907 FT_FRAME_CHAR( bearingY ),
908 FT_FRAME_BYTE( advance ),
913 /* read small metrics */
914 if ( FT_STREAM_READ_FIELDS( sbit_small_metrics_fields, &smetrics ) )
917 /* convert it to a big metrics */
918 metrics->height = smetrics.height;
919 metrics->width = smetrics.width;
920 metrics->horiBearingX = smetrics.bearingX;
921 metrics->horiBearingY = smetrics.bearingY;
922 metrics->horiAdvance = smetrics.advance;
924 /* these metrics are made up at a higher level when */
926 metrics->vertBearingX = 0;
927 metrics->vertBearingY = 0;
928 metrics->vertAdvance = 0;
935 /* variable big metrics */
936 if ( FT_STREAM_READ_FIELDS( sbit_metrics_fields, metrics ) )
941 default: /* constant metrics */
942 if ( range->index_format == 2 || range->index_format == 5 )
943 *metrics = range->metrics;
945 return SFNT_Err_Invalid_File_Format;
953 /*************************************************************************/
959 /* Crops a bitmap to its tightest bounding box, and adjusts its */
963 /* map :: The bitmap. */
965 /* metrics :: The corresponding metrics structure. */
968 crop_bitmap( FT_Bitmap* map,
969 TT_SBit_Metrics metrics )
971 /***********************************************************************/
973 /* In this situation, some bounding boxes of embedded bitmaps are too */
974 /* large. We need to crop it to a reasonable size. */
980 /* | * | ------> | * | */
984 /* --------- ----- */
986 /***********************************************************************/
993 /***********************************************************************/
995 /* first of all, check the top-most lines of the bitmap, and remove */
996 /* them if they're empty. */
999 line = (FT_Byte*)map->buffer;
1001 line_len = map->pitch;
1004 for ( count = 0; count < rows; count++ )
1006 FT_Byte* cur = line;
1007 FT_Byte* limit = line + line_len;
1010 for ( ; cur < limit; cur++ )
1014 /* the current line was empty - skip to next one */
1019 /* check that we have at least one filled line */
1020 if ( count >= rows )
1023 /* now, crop the empty upper lines */
1026 line = (FT_Byte*)map->buffer;
1028 FT_MEM_MOVE( line, line + count * line_len,
1029 ( rows - count ) * line_len );
1031 metrics->height = (FT_Byte)( metrics->height - count );
1032 metrics->horiBearingY = (FT_Char)( metrics->horiBearingY - count );
1033 metrics->vertBearingY = (FT_Char)( metrics->vertBearingY - count );
1040 /***********************************************************************/
1042 /* second, crop the lower lines */
1045 line = (FT_Byte*)map->buffer + ( rows - 1 ) * line_len;
1047 for ( count = 0; count < rows; count++ )
1049 FT_Byte* cur = line;
1050 FT_Byte* limit = line + line_len;
1053 for ( ; cur < limit; cur++ )
1057 /* the current line was empty - skip to previous one */
1064 metrics->height = (FT_Byte)( metrics->height - count );
1070 /***********************************************************************/
1072 /* third, get rid of the space on the left side of the glyph */
1079 line = (FT_Byte*)map->buffer;
1080 limit = line + rows * line_len;
1082 for ( ; line < limit; line += line_len )
1083 if ( line[0] & 0x80 )
1086 /* shift the whole glyph one pixel to the left */
1087 line = (FT_Byte*)map->buffer;
1088 limit = line + rows * line_len;
1090 for ( ; line < limit; line += line_len )
1092 FT_Int n, width = map->width;
1094 FT_Byte* cur = line;
1097 old = (FT_Byte)(cur[0] << 1);
1098 for ( n = 8; n < width; n += 8 )
1104 cur[0] = (FT_Byte)( old | ( val >> 7 ) );
1105 old = (FT_Byte)( val << 1 );
1112 metrics->horiBearingX++;
1113 metrics->vertBearingX++;
1116 } while ( map->width > 0 );
1120 /***********************************************************************/
1122 /* finally, crop the bitmap width to get rid of the space on the right */
1123 /* side of the glyph. */
1127 FT_Int right = map->width - 1;
1132 line = (FT_Byte*)map->buffer + ( right >> 3 );
1133 limit = line + rows * line_len;
1134 mask = (FT_Byte)( 0x80 >> ( right & 7 ) );
1136 for ( ; line < limit; line += line_len )
1137 if ( line[0] & mask )
1140 /* crop the whole glyph to the right */
1144 } while ( map->width > 0 );
1147 /* all right, the bitmap was cropped */
1154 map->pixel_mode = FT_PIXEL_MODE_MONO;
1159 Load_SBit_Single( FT_Bitmap* map,
1163 FT_UShort image_format,
1164 TT_SBit_Metrics metrics,
1170 /* check that the source bitmap fits into the target pixmap */
1171 if ( x_offset < 0 || x_offset + metrics->width > map->width ||
1172 y_offset < 0 || y_offset + metrics->height > map->rows )
1174 error = SFNT_Err_Invalid_Argument;
1180 FT_Int glyph_width = metrics->width;
1181 FT_Int glyph_height = metrics->height;
1183 FT_Int line_bits = pix_bits * glyph_width;
1184 FT_Bool pad_bytes = 0;
1187 /* compute size of glyph image */
1188 switch ( image_format )
1190 case 1: /* byte-padded formats */
1199 line_length = ( glyph_width + 7 ) >> 3;
1202 line_length = ( glyph_width + 3 ) >> 2;
1205 line_length = ( glyph_width + 1 ) >> 1;
1208 line_length = glyph_width;
1211 glyph_size = glyph_height * line_length;
1219 line_bits = glyph_width * pix_bits;
1220 glyph_size = ( glyph_height * line_bits + 7 ) >> 3;
1223 default: /* invalid format */
1224 return SFNT_Err_Invalid_File_Format;
1227 /* Now read data and draw glyph into target pixmap */
1228 if ( FT_FRAME_ENTER( glyph_size ) )
1231 /* don't forget to multiply `x_offset' by `map->pix_bits' as */
1232 /* the sbit blitter doesn't make a difference between pixmap */
1234 blit_sbit( map, (FT_Byte*)stream->cursor, line_bits, pad_bytes,
1235 x_offset * pix_bits, y_offset, metrics->height );
1246 Load_SBit_Image( TT_SBit_Strike strike,
1247 TT_SBit_Range range,
1249 FT_ULong glyph_offset,
1254 TT_SBit_Metrics metrics,
1257 FT_Memory memory = stream->memory;
1258 FT_Bitmap* map = &slot->bitmap;
1262 /* place stream at beginning of glyph data and read metrics */
1263 if ( FT_STREAM_SEEK( ebdt_pos + glyph_offset ) )
1266 error = tt_load_sbit_metrics( stream, range, metrics );
1270 /* This function is recursive. At the top-level call, we */
1271 /* compute the dimensions of the higher-level glyph to */
1272 /* allocate the final pixmap buffer. */
1278 map->width = metrics->width;
1279 map->rows = metrics->height;
1281 switch ( strike->bit_depth )
1284 map->pixel_mode = FT_PIXEL_MODE_MONO;
1285 map->pitch = ( map->width + 7 ) >> 3;
1289 map->pixel_mode = FT_PIXEL_MODE_GRAY2;
1290 map->pitch = ( map->width + 3 ) >> 2;
1294 map->pixel_mode = FT_PIXEL_MODE_GRAY4;
1295 map->pitch = ( map->width + 1 ) >> 1;
1299 map->pixel_mode = FT_PIXEL_MODE_GRAY;
1300 map->pitch = map->width;
1304 return SFNT_Err_Invalid_File_Format;
1307 size = map->rows * map->pitch;
1309 /* check that there is no empty image */
1311 goto Exit; /* exit successfully! */
1313 error = ft_glyphslot_alloc_bitmap( slot, size );
1318 switch ( range->image_format )
1320 case 1: /* single sbit image - load it */
1325 return Load_SBit_Single( map, x_offset, y_offset, strike->bit_depth,
1326 range->image_format, metrics, stream );
1328 case 8: /* compound format */
1329 if ( FT_STREAM_SKIP( 1L ) )
1331 error = SFNT_Err_Invalid_Stream_Skip;
1339 default: /* invalid image format */
1340 return SFNT_Err_Invalid_File_Format;
1343 /* All right, we have a compound format. First of all, read */
1344 /* the array of elements. */
1346 TT_SBit_Component components = NULL;
1347 TT_SBit_Component comp;
1348 FT_UShort num_components, count;
1351 if ( FT_READ_USHORT( num_components ) ||
1352 FT_NEW_ARRAY( components, num_components ) )
1355 count = num_components;
1357 if ( FT_FRAME_ENTER( 4L * num_components ) )
1360 for ( comp = components; count > 0; count--, comp++ )
1362 comp->glyph_code = FT_GET_USHORT();
1363 comp->x_offset = FT_GET_CHAR();
1364 comp->y_offset = FT_GET_CHAR();
1369 /* Now recursively load each element glyph */
1370 count = num_components;
1372 for ( ; count > 0; count--, comp++ )
1374 TT_SBit_Range elem_range;
1375 TT_SBit_MetricsRec elem_metrics;
1376 FT_ULong elem_offset;
1379 /* find the range for this element */
1380 error = find_sbit_range( comp->glyph_code,
1387 /* now load the element, recursively */
1388 error = Load_SBit_Image( strike,
1393 x_offset + comp->x_offset,
1394 y_offset + comp->y_offset,
1403 FT_FREE( components );
1411 /*************************************************************************/
1414 /* tt_face_load_sbit_image */
1417 /* Loads a given glyph sbit image from the font resource. This also */
1418 /* returns its metrics. */
1421 /* face :: The target face object. */
1423 /* strike_index :: The current strike index. */
1425 /* glyph_index :: The current glyph index. */
1427 /* load_flags :: The glyph load flags (the code checks for the flag */
1428 /* FT_LOAD_CROP_BITMAP). */
1430 /* stream :: The input stream. */
1433 /* map :: The target pixmap. */
1435 /* metrics :: A big sbit metrics structure for the glyph image. */
1438 /* FreeType error code. 0 means success. Returns an error if no */
1439 /* glyph sbit exists for the index. */
1442 /* The `map.buffer' field is always freed before the glyph is loaded. */
1444 FT_LOCAL_DEF( FT_Error )
1445 tt_face_load_sbit_image( TT_Face face,
1446 FT_ULong strike_index,
1447 FT_UInt glyph_index,
1451 TT_SBit_MetricsRec *metrics )
1454 FT_ULong ebdt_pos, glyph_offset;
1456 TT_SBit_Strike strike;
1457 TT_SBit_Range range;
1460 /* Check whether there is a glyph sbit for the current index */
1461 error = tt_find_sbit_image( face, glyph_index, strike_index,
1462 &range, &strike, &glyph_offset );
1466 /* now, find the location of the `EBDT' table in */
1468 error = face->goto_table( face, TTAG_EBDT, stream, 0 );
1470 error = face->goto_table( face, TTAG_bdat, stream, 0 );
1474 ebdt_pos = FT_STREAM_POS();
1476 error = Load_SBit_Image( strike, range, ebdt_pos, glyph_offset,
1477 face->root.glyph, 0, 0, stream, metrics, 0 );
1481 /* setup vertical metrics if needed */
1482 if ( strike->flags & 1 )
1484 /* in case of a horizontal strike only */
1488 advance = strike->hori.ascender - strike->hori.descender;
1490 /* some heuristic values */
1492 metrics->vertBearingX = (FT_Char)(-metrics->width / 2 );
1493 metrics->vertBearingY = (FT_Char)( ( advance - metrics->height ) / 2 );
1494 metrics->vertAdvance = (FT_Char)( advance * 12 / 10 );
1497 /* Crop the bitmap now, unless specified otherwise */
1498 if ( load_flags & FT_LOAD_CROP_BITMAP )
1499 crop_bitmap( map, metrics );
1505 #endif /* FT_CONFIG_OPTION_OLD_INTERNALS */