1 /***************************************************************************/
5 /* TrueType and OpenType embedded bitmap support (body). */
7 /* Copyright 1996-2001, 2002 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
21 #include FT_INTERNAL_STREAM_H
22 #include FT_TRUETYPE_TAGS_H
28 /*************************************************************************/
30 /* The macro FT_COMPONENT is used in trace mode. It is an implicit */
31 /* parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log */
32 /* messages during execution. */
35 #define FT_COMPONENT trace_ttsbit
38 /*************************************************************************/
44 /* Blits a bitmap from an input stream into a given target. Supports */
45 /* x and y offsets as well as byte padded lines. */
48 /* target :: The target bitmap/pixmap. */
50 /* source :: The input packed bitmap data. */
52 /* line_bits :: The number of bits per line. */
54 /* byte_padded :: A flag which is true if lines are byte-padded. */
56 /* x_offset :: The horizontal offset. */
58 /* y_offset :: The vertical offset. */
61 /* IMPORTANT: The x and y offsets are relative to the top corner of */
62 /* the target bitmap (unlike the normal TrueType */
63 /* convention). A positive y offset indicates a downwards */
67 blit_sbit( FT_Bitmap* target,
82 /* first of all, compute starting write position */
83 line_incr = target->pitch;
84 line_buff = target->buffer;
87 line_buff -= line_incr * ( target->rows - 1 );
89 line_buff += ( x_offset >> 3 ) + y_offset * line_incr;
91 /***********************************************************************/
93 /* We use the extra-classic `accumulator' trick to extract the bits */
94 /* from the source byte stream. */
96 /* Namely, the variable `acc' is a 16-bit accumulator containing the */
97 /* last `loaded' bits from the input stream. The bits are shifted to */
98 /* the upmost position in `acc'. */
100 /***********************************************************************/
102 acc = 0; /* clear accumulator */
103 loaded = 0; /* no bits were loaded */
105 for ( height = target->rows; height > 0; height-- )
107 FT_Byte* cur = line_buff; /* current write cursor */
108 FT_Int count = line_bits; /* # of bits to extract per line */
109 FT_Byte shift = (FT_Byte)( x_offset & 7 ); /* current write shift */
110 FT_Byte space = (FT_Byte)( 8 - shift );
113 /* first of all, read individual source bytes */
123 /* ensure that there are at least 8 bits in the accumulator */
126 acc |= (FT_UShort)((FT_UShort)*source++ << ( 8 - loaded ));
130 /* now write one byte */
131 val = (FT_Byte)( acc >> 8 );
134 cur[0] |= (FT_Byte)( val >> shift );
135 cur[1] |= (FT_Byte)( val << space );
141 acc <<= 8; /* remove bits from accumulator */
145 } while ( count >= 0 );
148 /* restore `count' to correct value */
152 /* now write remaining bits (count < 8) */
158 /* ensure that there are at least `count' bits in the accumulator */
159 if ( (FT_Int)loaded < count )
161 acc |= (FT_UShort)((FT_UShort)*source++ << ( 8 - loaded ));
165 /* now write remaining bits */
166 val = (FT_Byte)( ( (FT_Byte)( acc >> 8 ) ) & ~( 0xFF >> count ) );
167 cur[0] |= (FT_Byte)( val >> shift );
170 cur[1] |= (FT_Byte)( val << space );
176 /* now, skip to next line */
180 loaded = 0; /* clear accumulator on byte-padded lines */
183 line_buff += line_incr;
188 const FT_Frame_Field sbit_metrics_fields[] =
191 #define FT_STRUCTURE TT_SBit_MetricsRec
194 FT_FRAME_BYTE( height ),
195 FT_FRAME_BYTE( width ),
197 FT_FRAME_CHAR( horiBearingX ),
198 FT_FRAME_CHAR( horiBearingY ),
199 FT_FRAME_BYTE( horiAdvance ),
201 FT_FRAME_CHAR( vertBearingX ),
202 FT_FRAME_CHAR( vertBearingY ),
203 FT_FRAME_BYTE( vertAdvance ),
208 /*************************************************************************/
211 /* TT_Load_SBit_Const_Metrics */
214 /* Loads the metrics for `EBLC' index tables format 2 and 5. */
217 /* range :: The target range. */
219 /* stream :: The input stream. */
222 /* FreeType error code. 0 means success. */
225 Load_SBit_Const_Metrics( TT_SBit_Range range,
231 if ( FT_READ_ULONG( range->image_size ) )
234 return FT_STREAM_READ_FIELDS( sbit_metrics_fields, &range->metrics );
238 /*************************************************************************/
241 /* TT_Load_SBit_Range_Codes */
244 /* Loads the range codes for `EBLC' index tables format 4 and 5. */
247 /* range :: The target range. */
249 /* stream :: The input stream. */
251 /* load_offsets :: A flag whether to load the glyph offset table. */
254 /* FreeType error code. 0 means success. */
257 Load_SBit_Range_Codes( TT_SBit_Range range,
259 FT_Bool load_offsets )
262 FT_ULong count, n, size;
263 FT_Memory memory = stream->memory;
266 if ( FT_READ_ULONG( count ) )
269 range->num_glyphs = count;
271 /* Allocate glyph offsets table if needed */
274 if ( FT_NEW_ARRAY( range->glyph_offsets, count ) )
282 /* Allocate glyph codes table and access frame */
283 if ( FT_NEW_ARRAY ( range->glyph_codes, count ) ||
284 FT_FRAME_ENTER( size ) )
287 for ( n = 0; n < count; n++ )
289 range->glyph_codes[n] = FT_GET_USHORT();
292 range->glyph_offsets[n] = (FT_ULong)range->image_offset +
303 /*************************************************************************/
306 /* TT_Load_SBit_Range */
309 /* Loads a given `EBLC' index/range table. */
312 /* range :: The target range. */
314 /* stream :: The input stream. */
317 /* FreeType error code. 0 means success. */
320 Load_SBit_Range( TT_SBit_Range range,
324 FT_Memory memory = stream->memory;
327 switch( range->index_format )
329 case 1: /* variable metrics with 4-byte offsets */
330 case 3: /* variable metrics with 2-byte offsets */
332 FT_ULong num_glyphs, n;
334 FT_Bool large = FT_BOOL( range->index_format == 1 );
337 num_glyphs = range->last_glyph - range->first_glyph + 1L;
338 range->num_glyphs = num_glyphs;
339 num_glyphs++; /* XXX: BEWARE - see spec */
341 size_elem = large ? 4 : 2;
343 if ( FT_NEW_ARRAY( range->glyph_offsets, num_glyphs ) ||
344 FT_FRAME_ENTER( num_glyphs * size_elem ) )
347 for ( n = 0; n < num_glyphs; n++ )
348 range->glyph_offsets[n] = (FT_ULong)( range->image_offset +
349 ( large ? FT_GET_ULONG()
350 : FT_GET_USHORT() ) );
355 case 2: /* all glyphs have identical metrics */
356 error = Load_SBit_Const_Metrics( range, stream );
360 error = Load_SBit_Range_Codes( range, stream, 1 );
364 error = Load_SBit_Const_Metrics( range, stream ) ||
365 Load_SBit_Range_Codes( range, stream, 0 );
369 error = SFNT_Err_Invalid_File_Format;
377 /*************************************************************************/
380 /* tt_face_load_sbit_strikes */
383 /* Loads the table of embedded bitmap sizes for this face. */
386 /* face :: The target face object. */
388 /* stream :: The input stream. */
391 /* FreeType error code. 0 means success. */
393 FT_LOCAL_DEF( FT_Error )
394 tt_face_load_sbit_strikes( TT_Face face,
398 FT_Memory memory = stream->memory;
400 FT_ULong num_strikes;
403 const FT_Frame_Field sbit_line_metrics_fields[] =
406 #define FT_STRUCTURE TT_SBit_LineMetricsRec
408 /* no FT_FRAME_START */
409 FT_FRAME_CHAR( ascender ),
410 FT_FRAME_CHAR( descender ),
411 FT_FRAME_BYTE( max_width ),
413 FT_FRAME_CHAR( caret_slope_numerator ),
414 FT_FRAME_CHAR( caret_slope_denominator ),
415 FT_FRAME_CHAR( caret_offset ),
417 FT_FRAME_CHAR( min_origin_SB ),
418 FT_FRAME_CHAR( min_advance_SB ),
419 FT_FRAME_CHAR( max_before_BL ),
420 FT_FRAME_CHAR( min_after_BL ),
421 FT_FRAME_CHAR( pads[0] ),
422 FT_FRAME_CHAR( pads[1] ),
426 const FT_Frame_Field strike_start_fields[] =
429 #define FT_STRUCTURE TT_SBit_StrikeRec
431 /* no FT_FRAME_START */
432 FT_FRAME_ULONG( ranges_offset ),
434 FT_FRAME_ULONG( num_ranges ),
435 FT_FRAME_ULONG( color_ref ),
439 const FT_Frame_Field strike_end_fields[] =
441 /* no FT_FRAME_START */
442 FT_FRAME_USHORT( start_glyph ),
443 FT_FRAME_USHORT( end_glyph ),
444 FT_FRAME_BYTE ( x_ppem ),
445 FT_FRAME_BYTE ( y_ppem ),
446 FT_FRAME_BYTE ( bit_depth ),
447 FT_FRAME_CHAR ( flags ),
452 face->num_sbit_strikes = 0;
454 /* this table is optional */
455 error = face->goto_table( face, TTAG_EBLC, stream, 0 );
457 error = face->goto_table( face, TTAG_bloc, stream, 0 );
461 table_base = FT_STREAM_POS();
462 if ( FT_FRAME_ENTER( 8L ) )
465 version = FT_GET_LONG();
466 num_strikes = FT_GET_ULONG();
470 /* check version number and strike count */
471 if ( version != 0x00020000L ||
472 num_strikes >= 0x10000L )
474 FT_ERROR(( "tt_face_load_sbit_strikes: invalid table version!\n" ));
475 error = SFNT_Err_Invalid_File_Format;
480 /* allocate the strikes table */
481 if ( FT_NEW_ARRAY( face->sbit_strikes, num_strikes ) )
484 face->num_sbit_strikes = num_strikes;
486 /* now read each strike table separately */
488 TT_SBit_Strike strike = face->sbit_strikes;
489 FT_ULong count = num_strikes;
492 if ( FT_FRAME_ENTER( 48L * num_strikes ) )
497 if ( FT_STREAM_READ_FIELDS( strike_start_fields, strike ) ||
498 FT_STREAM_READ_FIELDS( sbit_line_metrics_fields, &strike->hori ) ||
499 FT_STREAM_READ_FIELDS( sbit_line_metrics_fields, &strike->vert ) ||
500 FT_STREAM_READ_FIELDS( strike_end_fields, strike ) )
510 /* allocate the index ranges for each strike table */
512 TT_SBit_Strike strike = face->sbit_strikes;
513 FT_ULong count = num_strikes;
519 FT_ULong count2 = strike->num_ranges;
522 if ( FT_NEW_ARRAY( strike->sbit_ranges, strike->num_ranges ) )
525 /* read each range */
526 if ( FT_STREAM_SEEK( table_base + strike->ranges_offset ) ||
527 FT_FRAME_ENTER( strike->num_ranges * 8L ) )
530 range = strike->sbit_ranges;
533 range->first_glyph = FT_GET_USHORT();
534 range->last_glyph = FT_GET_USHORT();
535 range->table_offset = table_base + strike->ranges_offset +
543 /* Now, read each index table */
544 count2 = strike->num_ranges;
545 range = strike->sbit_ranges;
548 /* Read the header */
549 if ( FT_STREAM_SEEK( range->table_offset ) ||
550 FT_FRAME_ENTER( 8L ) )
553 range->index_format = FT_GET_USHORT();
554 range->image_format = FT_GET_USHORT();
555 range->image_offset = FT_GET_ULONG();
559 error = Load_SBit_Range( range, stream );
577 /*************************************************************************/
580 /* tt_face_free_sbit_strikes */
583 /* Releases the embedded bitmap tables. */
586 /* face :: The target face object. */
589 tt_face_free_sbit_strikes( TT_Face face )
591 FT_Memory memory = face->root.memory;
592 TT_SBit_Strike strike = face->sbit_strikes;
593 TT_SBit_Strike strike_limit = strike + face->num_sbit_strikes;
598 for ( ; strike < strike_limit; strike++ )
600 TT_SBit_Range range = strike->sbit_ranges;
601 TT_SBit_Range range_limit = range + strike->num_ranges;
606 for ( ; range < range_limit; range++ )
608 /* release the glyph offsets and codes tables */
609 /* where appropriate */
610 FT_FREE( range->glyph_offsets );
611 FT_FREE( range->glyph_codes );
614 FT_FREE( strike->sbit_ranges );
615 strike->num_ranges = 0;
617 FT_FREE( face->sbit_strikes );
619 face->num_sbit_strikes = 0;
623 FT_LOCAL_DEF( FT_Error )
624 tt_face_set_sbit_strike( TT_Face face,
627 FT_ULong *astrike_index )
632 if ( x_ppem < 0 || x_ppem > 255 ||
633 y_ppem < 1 || y_ppem > 255 )
634 return SFNT_Err_Invalid_PPem;
636 for ( i = 0; i < face->num_sbit_strikes; i++ )
638 if ( ( face->sbit_strikes[i].y_ppem == y_ppem ) &&
640 ( face->sbit_strikes[i].x_ppem == x_ppem ) ) )
647 return SFNT_Err_Invalid_PPem;
651 /*************************************************************************/
654 /* find_sbit_range */
657 /* Scans a given strike's ranges and return, for a given glyph */
658 /* index, the corresponding sbit range, and `EBDT' offset. */
661 /* glyph_index :: The glyph index. */
663 /* strike :: The source/current sbit strike. */
666 /* arange :: The sbit range containing the glyph index. */
668 /* aglyph_offset :: The offset of the glyph data in `EBDT' table. */
671 /* FreeType error code. 0 means the glyph index was found. */
674 find_sbit_range( FT_UInt glyph_index,
675 TT_SBit_Strike strike,
676 TT_SBit_Range *arange,
677 FT_ULong *aglyph_offset )
679 TT_SBit_RangeRec *range, *range_limit;
682 /* check whether the glyph index is within this strike's */
684 if ( glyph_index < (FT_UInt)strike->start_glyph ||
685 glyph_index > (FT_UInt)strike->end_glyph )
688 /* scan all ranges in strike */
689 range = strike->sbit_ranges;
690 range_limit = range + strike->num_ranges;
694 for ( ; range < range_limit; range++ )
696 if ( glyph_index >= (FT_UInt)range->first_glyph &&
697 glyph_index <= (FT_UInt)range->last_glyph )
699 FT_UShort delta = (FT_UShort)( glyph_index - range->first_glyph );
702 switch ( range->index_format )
706 *aglyph_offset = range->glyph_offsets[delta];
710 *aglyph_offset = range->image_offset +
711 range->image_size * delta;
720 for ( n = 0; n < range->num_glyphs; n++ )
722 if ( (FT_UInt)range->glyph_codes[n] == glyph_index )
724 if ( range->index_format == 4 )
725 *aglyph_offset = range->glyph_offsets[n];
727 *aglyph_offset = range->image_offset +
728 n * range->image_size;
740 /* return successfully! */
750 return SFNT_Err_Invalid_Argument;
754 /*************************************************************************/
757 /* find_sbit_image */
760 /* Checks whether an embedded bitmap (an `sbit') exists for a given */
761 /* glyph, at a given strike. */
764 /* face :: The target face object. */
766 /* glyph_index :: The glyph index. */
768 /* strike_index :: The current strike index. */
771 /* arange :: The SBit range containing the glyph index. */
773 /* astrike :: The SBit strike containing the glyph index. */
775 /* aglyph_offset :: The offset of the glyph data in `EBDT' table. */
778 /* FreeType error code. 0 means success. Returns */
779 /* SFNT_Err_Invalid_Argument if no sbit exists for the requested */
783 find_sbit_image( TT_Face face,
785 FT_ULong strike_index,
786 TT_SBit_Range *arange,
787 TT_SBit_Strike *astrike,
788 FT_ULong *aglyph_offset )
791 TT_SBit_Strike strike;
794 if ( !face->sbit_strikes ||
795 ( face->num_sbit_strikes <= strike_index ) )
798 strike = &face->sbit_strikes[strike_index];
800 error = find_sbit_range( glyph_index, strike,
801 arange, aglyph_offset );
810 /* no embedded bitmap for this glyph in face */
815 return SFNT_Err_Invalid_Argument;
819 /*************************************************************************/
822 /* load_sbit_metrics */
825 /* Gets the big metrics for a given SBit. */
828 /* stream :: The input stream. */
830 /* range :: The SBit range containing the glyph. */
833 /* big_metrics :: A big SBit metrics structure for the glyph. */
836 /* FreeType error code. 0 means success. */
839 /* The stream cursor must be positioned at the glyph's offset within */
840 /* the `EBDT' table before the call. */
842 /* If the image format uses variable metrics, the stream cursor is */
843 /* positioned just after the metrics header in the `EBDT' table on */
847 load_sbit_metrics( FT_Stream stream,
849 TT_SBit_Metrics metrics )
851 FT_Error error = SFNT_Err_Ok;
854 switch ( range->image_format )
859 /* variable small metrics */
861 TT_SBit_SmallMetricsRec smetrics;
863 const FT_Frame_Field sbit_small_metrics_fields[] =
866 #define FT_STRUCTURE TT_SBit_SmallMetricsRec
869 FT_FRAME_BYTE( height ),
870 FT_FRAME_BYTE( width ),
871 FT_FRAME_CHAR( bearingX ),
872 FT_FRAME_CHAR( bearingY ),
873 FT_FRAME_BYTE( advance ),
878 /* read small metrics */
879 if ( FT_STREAM_READ_FIELDS( sbit_small_metrics_fields, &smetrics ) )
882 /* convert it to a big metrics */
883 metrics->height = smetrics.height;
884 metrics->width = smetrics.width;
885 metrics->horiBearingX = smetrics.bearingX;
886 metrics->horiBearingY = smetrics.bearingY;
887 metrics->horiAdvance = smetrics.advance;
889 /* these metrics are made up at a higher level when */
891 metrics->vertBearingX = 0;
892 metrics->vertBearingY = 0;
893 metrics->vertAdvance = 0;
900 /* variable big metrics */
901 if ( FT_STREAM_READ_FIELDS( sbit_metrics_fields, metrics ) )
906 default: /* constant metrics */
907 if ( range->index_format == 2 || range->index_format == 5 )
908 *metrics = range->metrics;
910 return SFNT_Err_Invalid_File_Format;
918 /*************************************************************************/
924 /* Crops a bitmap to its tightest bounding box, and adjusts its */
928 /* map :: The bitmap. */
930 /* metrics :: The corresponding metrics structure. */
933 crop_bitmap( FT_Bitmap* map,
934 TT_SBit_Metrics metrics )
936 /***********************************************************************/
938 /* In this situation, some bounding boxes of embedded bitmaps are too */
939 /* large. We need to crop it to a reasonable size. */
945 /* | * | ------> | * | */
949 /* --------- ----- */
951 /***********************************************************************/
958 /***********************************************************************/
960 /* first of all, check the top-most lines of the bitmap, and remove */
961 /* them if they're empty. */
964 line = (FT_Byte*)map->buffer;
966 line_len = map->pitch;
969 for ( count = 0; count < rows; count++ )
972 FT_Byte* limit = line + line_len;
975 for ( ; cur < limit; cur++ )
979 /* the current line was empty - skip to next one */
984 /* check that we have at least one filled line */
988 /* now, crop the empty upper lines */
991 line = (FT_Byte*)map->buffer;
993 FT_MEM_MOVE( line, line + count * line_len,
994 ( rows - count ) * line_len );
996 metrics->height = (FT_Byte)( metrics->height - count );
997 metrics->horiBearingY = (FT_Char)( metrics->horiBearingY - count );
998 metrics->vertBearingY = (FT_Char)( metrics->vertBearingY - count );
1005 /***********************************************************************/
1007 /* second, crop the lower lines */
1010 line = (FT_Byte*)map->buffer + ( rows - 1 ) * line_len;
1012 for ( count = 0; count < rows; count++ )
1014 FT_Byte* cur = line;
1015 FT_Byte* limit = line + line_len;
1018 for ( ; cur < limit; cur++ )
1022 /* the current line was empty - skip to previous one */
1029 metrics->height = (FT_Byte)( metrics->height - count );
1035 /***********************************************************************/
1037 /* third, get rid of the space on the left side of the glyph */
1044 line = (FT_Byte*)map->buffer;
1045 limit = line + rows * line_len;
1047 for ( ; line < limit; line += line_len )
1048 if ( line[0] & 0x80 )
1051 /* shift the whole glyph one pixel to the left */
1052 line = (FT_Byte*)map->buffer;
1053 limit = line + rows * line_len;
1055 for ( ; line < limit; line += line_len )
1057 FT_Int n, width = map->width;
1059 FT_Byte* cur = line;
1062 old = (FT_Byte)(cur[0] << 1);
1063 for ( n = 8; n < width; n += 8 )
1069 cur[0] = (FT_Byte)( old | ( val >> 7 ) );
1070 old = (FT_Byte)( val << 1 );
1077 metrics->horiBearingX++;
1078 metrics->vertBearingX++;
1081 } while ( map->width > 0 );
1085 /***********************************************************************/
1087 /* finally, crop the bitmap width to get rid of the space on the right */
1088 /* side of the glyph. */
1092 FT_Int right = map->width - 1;
1097 line = (FT_Byte*)map->buffer + ( right >> 3 );
1098 limit = line + rows * line_len;
1099 mask = (FT_Byte)( 0x80 >> ( right & 7 ) );
1101 for ( ; line < limit; line += line_len )
1102 if ( line[0] & mask )
1105 /* crop the whole glyph to the right */
1109 } while ( map->width > 0 );
1112 /* all right, the bitmap was cropped */
1119 map->pixel_mode = FT_PIXEL_MODE_MONO;
1124 Load_SBit_Single( FT_Bitmap* map,
1128 FT_UShort image_format,
1129 TT_SBit_Metrics metrics,
1135 /* check that the source bitmap fits into the target pixmap */
1136 if ( x_offset < 0 || x_offset + metrics->width > map->width ||
1137 y_offset < 0 || y_offset + metrics->height > map->rows )
1139 error = SFNT_Err_Invalid_Argument;
1145 FT_Int glyph_width = metrics->width;
1146 FT_Int glyph_height = metrics->height;
1148 FT_Int line_bits = pix_bits * glyph_width;
1149 FT_Bool pad_bytes = 0;
1152 /* compute size of glyph image */
1153 switch ( image_format )
1155 case 1: /* byte-padded formats */
1164 line_length = ( glyph_width + 7 ) >> 3;
1167 line_length = ( glyph_width + 3 ) >> 2;
1170 line_length = ( glyph_width + 1 ) >> 1;
1173 line_length = glyph_width;
1176 glyph_size = glyph_height * line_length;
1184 line_bits = glyph_width * pix_bits;
1185 glyph_size = ( glyph_height * line_bits + 7 ) >> 3;
1188 default: /* invalid format */
1189 return SFNT_Err_Invalid_File_Format;
1192 /* Now read data and draw glyph into target pixmap */
1193 if ( FT_FRAME_ENTER( glyph_size ) )
1196 /* don't forget to multiply `x_offset' by `map->pix_bits' as */
1197 /* the sbit blitter doesn't make a difference between pixmap */
1199 blit_sbit( map, (FT_Byte*)stream->cursor, line_bits, pad_bytes,
1200 x_offset * pix_bits, y_offset );
1211 Load_SBit_Image( TT_SBit_Strike strike,
1212 TT_SBit_Range range,
1214 FT_ULong glyph_offset,
1219 TT_SBit_Metrics metrics,
1222 FT_Memory memory = stream->memory;
1223 FT_Bitmap* map = &slot->bitmap;
1227 /* place stream at beginning of glyph data and read metrics */
1228 if ( FT_STREAM_SEEK( ebdt_pos + glyph_offset ) )
1231 error = load_sbit_metrics( stream, range, metrics );
1235 /* this function is recursive. At the top-level call, we */
1236 /* compute the dimensions of the higher-level glyph to */
1237 /* allocate the final pixmap buffer */
1243 map->width = metrics->width;
1244 map->rows = metrics->height;
1246 switch ( strike->bit_depth )
1249 map->pixel_mode = FT_PIXEL_MODE_MONO;
1250 map->pitch = ( map->width + 7 ) >> 3;
1254 map->pixel_mode = FT_PIXEL_MODE_GRAY2;
1255 map->pitch = ( map->width + 3 ) >> 2;
1259 map->pixel_mode = FT_PIXEL_MODE_GRAY4;
1260 map->pitch = ( map->width + 1 ) >> 1;
1264 map->pixel_mode = FT_PIXEL_MODE_GRAY;
1265 map->pitch = map->width;
1269 return SFNT_Err_Invalid_File_Format;
1272 size = map->rows * map->pitch;
1274 /* check that there is no empty image */
1276 goto Exit; /* exit successfully! */
1278 error = ft_glyphslot_alloc_bitmap( slot, size );
1283 switch ( range->image_format )
1285 case 1: /* single sbit image - load it */
1290 return Load_SBit_Single( map, x_offset, y_offset, strike->bit_depth,
1291 range->image_format, metrics, stream );
1293 case 8: /* compound format */
1294 FT_Stream_Skip( stream, 1L );
1300 default: /* invalid image format */
1301 return SFNT_Err_Invalid_File_Format;
1304 /* All right, we have a compound format. First of all, read */
1305 /* the array of elements. */
1307 TT_SBit_Component components;
1308 TT_SBit_Component comp;
1309 FT_UShort num_components, count;
1312 if ( FT_READ_USHORT( num_components ) ||
1313 FT_NEW_ARRAY( components, num_components ) )
1316 count = num_components;
1318 if ( FT_FRAME_ENTER( 4L * num_components ) )
1321 for ( comp = components; count > 0; count--, comp++ )
1323 comp->glyph_code = FT_GET_USHORT();
1324 comp->x_offset = FT_GET_CHAR();
1325 comp->y_offset = FT_GET_CHAR();
1330 /* Now recursively load each element glyph */
1331 count = num_components;
1333 for ( ; count > 0; count--, comp++ )
1335 TT_SBit_Range elem_range;
1336 TT_SBit_MetricsRec elem_metrics;
1337 FT_ULong elem_offset;
1340 /* find the range for this element */
1341 error = find_sbit_range( comp->glyph_code,
1348 /* now load the element, recursively */
1349 error = Load_SBit_Image( strike,
1354 x_offset + comp->x_offset,
1355 y_offset + comp->y_offset,
1364 FT_FREE( components );
1372 /*************************************************************************/
1375 /* tt_face_load_sbit_image */
1378 /* Loads a given glyph sbit image from the font resource. This also */
1379 /* returns its metrics. */
1382 /* face :: The target face object. */
1384 /* strike_index :: The current strike index. */
1386 /* glyph_index :: The current glyph index. */
1388 /* load_flags :: The glyph load flags (the code checks for the flag */
1389 /* FT_LOAD_CROP_BITMAP). */
1391 /* stream :: The input stream. */
1394 /* map :: The target pixmap. */
1396 /* metrics :: A big sbit metrics structure for the glyph image. */
1399 /* FreeType error code. 0 means success. Returns an error if no */
1400 /* glyph sbit exists for the index. */
1403 /* The `map.buffer' field is always freed before the glyph is loaded. */
1405 FT_LOCAL_DEF( FT_Error )
1406 tt_face_load_sbit_image( TT_Face face,
1407 FT_ULong strike_index,
1408 FT_UInt glyph_index,
1412 TT_SBit_MetricsRec *metrics )
1415 FT_ULong ebdt_pos, glyph_offset;
1417 TT_SBit_Strike strike;
1418 TT_SBit_Range range;
1421 /* Check whether there is a glyph sbit for the current index */
1422 error = find_sbit_image( face, glyph_index, strike_index,
1423 &range, &strike, &glyph_offset );
1427 /* now, find the location of the `EBDT' table in */
1429 error = face->goto_table( face, TTAG_EBDT, stream, 0 );
1431 error = face->goto_table( face, TTAG_bdat, stream, 0 );
1435 ebdt_pos = FT_STREAM_POS();
1437 error = Load_SBit_Image( strike, range, ebdt_pos, glyph_offset,
1438 face->root.glyph, 0, 0, stream, metrics, 0 );
1442 /* setup vertical metrics if needed */
1443 if ( strike->flags & 1 )
1445 /* in case of a horizontal strike only */
1449 advance = strike->hori.ascender - strike->hori.descender;
1451 /* some heuristic values */
1453 metrics->vertBearingX = (FT_Char)(-metrics->width / 2 );
1454 metrics->vertBearingY = (FT_Char)( advance / 10 );
1455 metrics->vertAdvance = (FT_Char)( advance * 12 / 10 );
1458 /* Crop the bitmap now, unless specified otherwise */
1459 if ( load_flags & FT_LOAD_CROP_BITMAP )
1460 crop_bitmap( map, metrics );