1 /***************************************************************************/
5 /* TrueType and OpenType embedded bitmap support (body). */
7 /* Copyright 1996-2001 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)*source++ << ( 8 - loaded );
130 /* now write one byte */
131 val = (FT_Byte)( acc >> 8 );
134 cur[0] |= val >> shift;
135 cur[1] |= 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)*source++ << ( 8 - loaded );
165 /* now write remaining bits */
166 val = (FT_Byte)( ( (FT_Byte)( acc >> 8 ) ) & ~( 0xFF >> count ) );
167 cur[0] |= val >> shift;
170 cur[1] |= 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_Metrics
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 ( READ_ULong( range->image_size ) )
234 return 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 ( READ_ULong( count ) )
269 range->num_glyphs = count;
271 /* Allocate glyph offsets table if needed */
274 if ( ALLOC_ARRAY( range->glyph_offsets, count, FT_ULong ) )
282 /* Allocate glyph codes table and access frame */
283 if ( ALLOC_ARRAY ( range->glyph_codes, count, FT_UShort ) ||
284 ACCESS_Frame( size ) )
287 for ( n = 0; n < count; n++ )
289 range->glyph_codes[n] = 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 ( ALLOC_ARRAY( range->glyph_offsets,
344 num_glyphs, FT_ULong ) ||
345 ACCESS_Frame( num_glyphs * size_elem ) )
348 for ( n = 0; n < num_glyphs; n++ )
349 range->glyph_offsets[n] = (FT_ULong)( range->image_offset +
350 ( large ? GET_ULong()
356 case 2: /* all glyphs have identical metrics */
357 error = Load_SBit_Const_Metrics( range, stream );
361 error = Load_SBit_Range_Codes( range, stream, 1 );
365 error = Load_SBit_Const_Metrics( range, stream ) ||
366 Load_SBit_Range_Codes( range, stream, 0 );
370 error = SFNT_Err_Invalid_File_Format;
378 /*************************************************************************/
381 /* TT_Load_SBit_Strikes */
384 /* Loads the table of embedded bitmap sizes for this face. */
387 /* face :: The target face object. */
389 /* stream :: The input stream. */
392 /* FreeType error code. 0 means success. */
394 FT_LOCAL_DEF FT_Error
395 TT_Load_SBit_Strikes( TT_Face face,
399 FT_Memory memory = stream->memory;
401 FT_ULong num_strikes;
404 const FT_Frame_Field sbit_line_metrics_fields[] =
407 #define FT_STRUCTURE TT_SBit_Line_Metrics
409 /* no FT_FRAME_START */
410 FT_FRAME_CHAR( ascender ),
411 FT_FRAME_CHAR( descender ),
412 FT_FRAME_BYTE( max_width ),
414 FT_FRAME_CHAR( caret_slope_numerator ),
415 FT_FRAME_CHAR( caret_slope_denominator ),
416 FT_FRAME_CHAR( caret_offset ),
418 FT_FRAME_CHAR( min_origin_SB ),
419 FT_FRAME_CHAR( min_advance_SB ),
420 FT_FRAME_CHAR( max_before_BL ),
421 FT_FRAME_CHAR( min_after_BL ),
422 FT_FRAME_CHAR( pads[0] ),
423 FT_FRAME_CHAR( pads[1] ),
427 const FT_Frame_Field strike_start_fields[] =
430 #define FT_STRUCTURE TT_SBit_Strike
432 /* no FT_FRAME_START */
433 FT_FRAME_ULONG( ranges_offset ),
435 FT_FRAME_ULONG( num_ranges ),
436 FT_FRAME_ULONG( color_ref ),
440 const FT_Frame_Field strike_end_fields[] =
442 /* no FT_FRAME_START */
443 FT_FRAME_USHORT( start_glyph ),
444 FT_FRAME_USHORT( end_glyph ),
445 FT_FRAME_BYTE ( x_ppem ),
446 FT_FRAME_BYTE ( y_ppem ),
447 FT_FRAME_BYTE ( bit_depth ),
448 FT_FRAME_CHAR ( flags ),
453 face->num_sbit_strikes = 0;
455 /* this table is optional */
456 error = face->goto_table( face, TTAG_EBLC, stream, 0 );
458 error = face->goto_table( face, TTAG_bloc, stream, 0 );
462 table_base = FILE_Pos();
463 if ( ACCESS_Frame( 8L ) )
466 version = GET_Long();
467 num_strikes = GET_ULong();
471 /* check version number and strike count */
472 if ( version != 0x00020000L ||
473 num_strikes >= 0x10000L )
475 FT_ERROR(( "TT_Load_SBit_Strikes: invalid table version!\n" ));
476 error = SFNT_Err_Invalid_File_Format;
481 /* allocate the strikes table */
482 if ( ALLOC_ARRAY( face->sbit_strikes, num_strikes, TT_SBit_Strike ) )
485 face->num_sbit_strikes = num_strikes;
487 /* now read each strike table separately */
489 TT_SBit_Strike* strike = face->sbit_strikes;
490 FT_ULong count = num_strikes;
493 if ( ACCESS_Frame( 48L * num_strikes ) )
498 if ( READ_Fields( strike_start_fields, strike ) ||
499 READ_Fields( sbit_line_metrics_fields, &strike->hori ) ||
500 READ_Fields( sbit_line_metrics_fields, &strike->vert ) ||
501 READ_Fields( strike_end_fields, strike ) )
511 /* allocate the index ranges for each strike table */
513 TT_SBit_Strike* strike = face->sbit_strikes;
514 FT_ULong count = num_strikes;
519 TT_SBit_Range* range;
520 FT_ULong count2 = strike->num_ranges;
523 if ( ALLOC_ARRAY( strike->sbit_ranges,
528 /* read each range */
529 if ( FILE_Seek( table_base + strike->ranges_offset ) ||
530 ACCESS_Frame( strike->num_ranges * 8L ) )
533 range = strike->sbit_ranges;
536 range->first_glyph = GET_UShort();
537 range->last_glyph = GET_UShort();
538 range->table_offset = table_base + strike->ranges_offset
546 /* Now, read each index table */
547 count2 = strike->num_ranges;
548 range = strike->sbit_ranges;
551 /* Read the header */
552 if ( FILE_Seek( range->table_offset ) ||
556 range->index_format = GET_UShort();
557 range->image_format = GET_UShort();
558 range->image_offset = GET_ULong();
562 error = Load_SBit_Range( range, stream );
580 /*************************************************************************/
583 /* TT_Free_SBit_Strikes */
586 /* Releases the embedded bitmap tables. */
589 /* face :: The target face object. */
592 TT_Free_SBit_Strikes( TT_Face face )
594 FT_Memory memory = face->root.memory;
595 TT_SBit_Strike* strike = face->sbit_strikes;
596 TT_SBit_Strike* strike_limit = strike + face->num_sbit_strikes;
601 for ( ; strike < strike_limit; strike++ )
603 TT_SBit_Range* range = strike->sbit_ranges;
604 TT_SBit_Range* range_limit = range + strike->num_ranges;
609 for ( ; range < range_limit; range++ )
611 /* release the glyph offsets and codes tables */
612 /* where appropriate */
613 FREE( range->glyph_offsets );
614 FREE( range->glyph_codes );
617 FREE( strike->sbit_ranges );
618 strike->num_ranges = 0;
620 FREE( face->sbit_strikes );
622 face->num_sbit_strikes = 0;
626 FT_LOCAL_DEF FT_Error
627 TT_Set_SBit_Strike( TT_Face face,
630 FT_ULong *astrike_index )
635 if ( x_ppem < 0 || x_ppem > 255 ||
636 y_ppem < 1 || y_ppem > 255 )
637 return SFNT_Err_Invalid_PPem;
639 for ( i = 0; i < face->num_sbit_strikes; i++ )
641 if ( ( face->sbit_strikes[i].y_ppem == y_ppem ) &&
643 ( face->sbit_strikes[i].x_ppem == x_ppem ) ) )
650 return SFNT_Err_Invalid_PPem;
654 /*************************************************************************/
657 /* Find_SBit_Range */
660 /* Scans a given strike's ranges and return, for a given glyph */
661 /* index, the corresponding sbit range, and `EBDT' offset. */
664 /* glyph_index :: The glyph index. */
666 /* strike :: The source/current sbit strike. */
669 /* arange :: The sbit range containing the glyph index. */
671 /* aglyph_offset :: The offset of the glyph data in `EBDT' table. */
674 /* FreeType error code. 0 means the glyph index was found. */
677 Find_SBit_Range( FT_UInt glyph_index,
678 TT_SBit_Strike* strike,
679 TT_SBit_Range** arange,
680 FT_ULong* aglyph_offset )
682 TT_SBit_Range *range, *range_limit;
685 /* check whether the glyph index is within this strike's */
687 if ( glyph_index < (FT_UInt)strike->start_glyph ||
688 glyph_index > (FT_UInt)strike->end_glyph )
691 /* scan all ranges in strike */
692 range = strike->sbit_ranges;
693 range_limit = range + strike->num_ranges;
697 for ( ; range < range_limit; range++ )
699 if ( glyph_index >= (FT_UInt)range->first_glyph &&
700 glyph_index <= (FT_UInt)range->last_glyph )
702 FT_UShort delta = (FT_UShort)( glyph_index - range->first_glyph );
705 switch ( range->index_format )
709 *aglyph_offset = range->glyph_offsets[delta];
713 *aglyph_offset = range->image_offset +
714 range->image_size * delta;
723 for ( n = 0; n < range->num_glyphs; n++ )
725 if ( (FT_UInt)range->glyph_codes[n] == glyph_index )
727 if ( range->index_format == 4 )
728 *aglyph_offset = range->glyph_offsets[n];
730 *aglyph_offset = range->image_offset +
731 n * range->image_size;
743 /* return successfully! */
753 return SFNT_Err_Invalid_Argument;
757 /*************************************************************************/
760 /* Find_SBit_Image */
763 /* Checks whether an embedded bitmap (an `sbit') exists for a given */
764 /* glyph, at a given strike. */
767 /* face :: The target face object. */
769 /* glyph_index :: The glyph index. */
771 /* strike_index :: The current strike index. */
774 /* arange :: The SBit range containing the glyph index. */
776 /* astrike :: The SBit strike containing the glyph index. */
778 /* aglyph_offset :: The offset of the glyph data in `EBDT' table. */
781 /* FreeType error code. 0 means success. Returns */
782 /* SFNT_Err_Invalid_Argument if no sbit exists for the requested */
786 Find_SBit_Image( TT_Face face,
788 FT_ULong strike_index,
789 TT_SBit_Range* *arange,
790 TT_SBit_Strike* *astrike,
791 FT_ULong *aglyph_offset )
794 TT_SBit_Strike* strike;
797 if ( !face->sbit_strikes ||
798 ( face->num_sbit_strikes <= (FT_Int)strike_index ) )
801 strike = &face->sbit_strikes[strike_index];
803 error = Find_SBit_Range( glyph_index, strike,
804 arange, aglyph_offset );
813 /* no embedded bitmap for this glyph in face */
818 return SFNT_Err_Invalid_Argument;
822 /*************************************************************************/
825 /* Load_SBit_Metrics */
828 /* Gets the big metrics for a given SBit. */
831 /* stream :: The input stream. */
833 /* range :: The SBit range containing the glyph. */
836 /* big_metrics :: A big SBit metrics structure for the glyph. */
839 /* FreeType error code. 0 means success. */
842 /* The stream cursor must be positioned at the glyph's offset within */
843 /* the `EBDT' table before the call. */
845 /* If the image format uses variable metrics, the stream cursor is */
846 /* positioned just after the metrics header in the `EBDT' table on */
850 Load_SBit_Metrics( FT_Stream stream,
851 TT_SBit_Range* range,
852 TT_SBit_Metrics* metrics )
854 FT_Error error = SFNT_Err_Ok;
857 switch ( range->image_format )
862 /* variable small metrics */
864 TT_SBit_Small_Metrics smetrics;
866 const FT_Frame_Field sbit_small_metrics_fields[] =
869 #define FT_STRUCTURE TT_SBit_Small_Metrics
872 FT_FRAME_BYTE( height ),
873 FT_FRAME_BYTE( width ),
874 FT_FRAME_CHAR( bearingX ),
875 FT_FRAME_CHAR( bearingY ),
876 FT_FRAME_BYTE( advance ),
881 /* read small metrics */
882 if ( READ_Fields( sbit_small_metrics_fields, &smetrics ) )
885 /* convert it to a big metrics */
886 metrics->height = smetrics.height;
887 metrics->width = smetrics.width;
888 metrics->horiBearingX = smetrics.bearingX;
889 metrics->horiBearingY = smetrics.bearingY;
890 metrics->horiAdvance = smetrics.advance;
892 /* these metrics are made up at a higher level when */
894 metrics->vertBearingX = 0;
895 metrics->vertBearingY = 0;
896 metrics->vertAdvance = 0;
903 /* variable big metrics */
904 if ( READ_Fields( sbit_metrics_fields, metrics ) )
909 default: /* constant metrics */
910 if ( range->index_format == 2 || range->index_format == 5 )
911 *metrics = range->metrics;
913 return SFNT_Err_Invalid_File_Format;
921 /*************************************************************************/
927 /* Crops a bitmap to its tightest bounding box, and adjusts its */
931 /* map :: The bitmap. */
933 /* metrics :: The corresponding metrics structure. */
936 Crop_Bitmap( FT_Bitmap* map,
937 TT_SBit_Metrics* metrics )
939 /***********************************************************************/
941 /* In this situation, some bounding boxes of embedded bitmaps are too */
942 /* large. We need to crop it to a reasonable size. */
948 /* | * | ------> | * | */
952 /* --------- ----- */
954 /***********************************************************************/
961 /***********************************************************************/
963 /* first of all, check the top-most lines of the bitmap, and remove */
964 /* them if they're empty. */
967 line = (FT_Byte*)map->buffer;
969 line_len = map->pitch;
972 for ( count = 0; count < rows; count++ )
975 FT_Byte* limit = line + line_len;
978 for ( ; cur < limit; cur++ )
982 /* the current line was empty - skip to next one */
987 /* check that we have at least one filled line */
991 /* now, crop the empty upper lines */
994 line = (FT_Byte*)map->buffer;
996 MEM_Move( line, line + count * line_len,
997 ( rows - count ) * line_len );
999 metrics->height = (FT_Byte)( metrics->height - count );
1000 metrics->horiBearingY = (FT_Char)( metrics->horiBearingY - count );
1001 metrics->vertBearingY = (FT_Char)( metrics->vertBearingY - count );
1008 /***********************************************************************/
1010 /* second, crop the lower lines */
1013 line = (FT_Byte*)map->buffer + ( rows - 1 ) * line_len;
1015 for ( count = 0; count < rows; count++ )
1017 FT_Byte* cur = line;
1018 FT_Byte* limit = line + line_len;
1021 for ( ; cur < limit; cur++ )
1025 /* the current line was empty - skip to previous one */
1032 metrics->height = (FT_Byte)( metrics->height - count );
1038 /***********************************************************************/
1040 /* third, get rid of the space on the left side of the glyph */
1047 line = (FT_Byte*)map->buffer;
1048 limit = line + rows * line_len;
1050 for ( ; line < limit; line += line_len )
1051 if ( line[0] & 0x80 )
1054 /* shift the whole glyph one pixel to the left */
1055 line = (FT_Byte*)map->buffer;
1056 limit = line + rows * line_len;
1058 for ( ; line < limit; line += line_len )
1060 FT_Int n, width = map->width;
1062 FT_Byte* cur = line;
1065 old = (FT_Byte)(cur[0] << 1);
1066 for ( n = 8; n < width; n += 8 )
1072 cur[0] = (FT_Byte)( old | ( val >> 7 ) );
1073 old = (FT_Byte)( val << 1 );
1080 metrics->horiBearingX++;
1081 metrics->vertBearingX++;
1084 } while ( map->width > 0 );
1088 /***********************************************************************/
1090 /* finally, crop the bitmap width to get rid of the space on the right */
1091 /* side of the glyph. */
1095 FT_Int right = map->width - 1;
1100 line = (FT_Byte*)map->buffer + ( right >> 3 );
1101 limit = line + rows * line_len;
1102 mask = (FT_Byte)( 0x80 >> ( right & 7 ) );
1104 for ( ; line < limit; line += line_len )
1105 if ( line[0] & mask )
1108 /* crop the whole glyph to the right */
1112 } while ( map->width > 0 );
1115 /* all right, the bitmap was cropped */
1122 map->pixel_mode = ft_pixel_mode_mono;
1127 Load_SBit_Single( FT_Bitmap* map,
1131 FT_UShort image_format,
1132 TT_SBit_Metrics* metrics,
1138 /* check that the source bitmap fits into the target pixmap */
1139 if ( x_offset < 0 || x_offset + metrics->width > map->width ||
1140 y_offset < 0 || y_offset + metrics->height > map->rows )
1142 error = SFNT_Err_Invalid_Argument;
1148 FT_Int glyph_width = metrics->width;
1149 FT_Int glyph_height = metrics->height;
1151 FT_Int line_bits = pix_bits * glyph_width;
1152 FT_Bool pad_bytes = 0;
1155 /* compute size of glyph image */
1156 switch ( image_format )
1158 case 1: /* byte-padded formats */
1166 case 1: line_length = ( glyph_width + 7 ) >> 3; break;
1167 case 2: line_length = ( glyph_width + 3 ) >> 2; break;
1168 case 4: line_length = ( glyph_width + 1 ) >> 1; break;
1169 default: line_length = glyph_width;
1172 glyph_size = glyph_height * line_length;
1180 line_bits = glyph_width * pix_bits;
1181 glyph_size = ( glyph_height * line_bits + 7 ) >> 3;
1184 default: /* invalid format */
1185 return SFNT_Err_Invalid_File_Format;
1188 /* Now read data and draw glyph into target pixmap */
1189 if ( ACCESS_Frame( glyph_size ) )
1192 /* don't forget to multiply `x_offset' by `map->pix_bits' as */
1193 /* the sbit blitter doesn't make a difference between pixmap */
1195 blit_sbit( map, (FT_Byte*)stream->cursor, line_bits, pad_bytes,
1196 x_offset * pix_bits, y_offset );
1207 Load_SBit_Image( TT_SBit_Strike* strike,
1208 TT_SBit_Range* range,
1210 FT_ULong glyph_offset,
1215 TT_SBit_Metrics* metrics )
1217 FT_Memory memory = stream->memory;
1221 /* place stream at beginning of glyph data and read metrics */
1222 if ( FILE_Seek( ebdt_pos + glyph_offset ) )
1225 error = Load_SBit_Metrics( stream, range, metrics );
1229 /* this function is recursive. At the top-level call, the */
1230 /* field map.buffer is NULL. We thus begin by finding the */
1231 /* dimensions of the higher-level glyph to allocate the */
1232 /* final pixmap buffer */
1233 if ( map->buffer == 0 )
1238 map->width = metrics->width;
1239 map->rows = metrics->height;
1241 switch ( strike->bit_depth )
1244 map->pixel_mode = ft_pixel_mode_mono;
1245 map->pitch = ( map->width + 7 ) >> 3;
1249 map->pixel_mode = ft_pixel_mode_pal2;
1250 map->pitch = ( map->width + 3 ) >> 2;
1254 map->pixel_mode = ft_pixel_mode_pal4;
1255 map->pitch = ( map->width + 1 ) >> 1;
1259 map->pixel_mode = ft_pixel_mode_grays;
1260 map->pitch = map->width;
1264 return SFNT_Err_Invalid_File_Format;
1267 size = map->rows * map->pitch;
1269 /* check that there is no empty image */
1271 goto Exit; /* exit successfully! */
1273 if ( ALLOC( map->buffer, size ) )
1277 switch ( range->image_format )
1279 case 1: /* single sbit image - load it */
1284 return Load_SBit_Single( map, x_offset, y_offset, strike->bit_depth,
1285 range->image_format, metrics, stream );
1287 case 8: /* compound format */
1288 FT_Skip_Stream( stream, 1L );
1294 default: /* invalid image format */
1295 return SFNT_Err_Invalid_File_Format;
1298 /* All right, we have a compound format. First of all, read */
1299 /* the array of elements. */
1301 TT_SBit_Component* components;
1302 TT_SBit_Component* comp;
1303 FT_UShort num_components, count;
1306 if ( READ_UShort( num_components ) ||
1307 ALLOC_ARRAY( components, num_components, TT_SBit_Component ) )
1310 count = num_components;
1312 if ( ACCESS_Frame( 4L * num_components ) )
1315 for ( comp = components; count > 0; count--, comp++ )
1317 comp->glyph_code = GET_UShort();
1318 comp->x_offset = GET_Char();
1319 comp->y_offset = GET_Char();
1324 /* Now recursively load each element glyph */
1325 count = num_components;
1327 for ( ; count > 0; count--, comp++ )
1329 TT_SBit_Range* elem_range;
1330 TT_SBit_Metrics elem_metrics;
1331 FT_ULong elem_offset;
1334 /* find the range for this element */
1335 error = Find_SBit_Range( comp->glyph_code,
1342 /* now load the element, recursively */
1343 error = Load_SBit_Image( strike,
1348 x_offset + comp->x_offset,
1349 y_offset + comp->y_offset,
1365 /*************************************************************************/
1368 /* TT_Load_SBit_Image */
1371 /* Loads a given glyph sbit image from the font resource. This also */
1372 /* returns its metrics. */
1375 /* face :: The target face object. */
1377 /* strike_index :: The current strike index. */
1379 /* glyph_index :: The current glyph index. */
1381 /* load_flags :: The glyph load flags (the code checks for the flag */
1382 /* FT_LOAD_CROP_BITMAP). */
1384 /* stream :: The input stream. */
1387 /* map :: The target pixmap. */
1389 /* metrics :: A big sbit metrics structure for the glyph image. */
1392 /* FreeType error code. 0 means success. Returns an error if no */
1393 /* glyph sbit exists for the index. */
1396 /* The `map.buffer' field is always freed before the glyph is loaded. */
1398 FT_LOCAL_DEF FT_Error
1399 TT_Load_SBit_Image( TT_Face face,
1400 FT_ULong strike_index,
1401 FT_UInt glyph_index,
1405 TT_SBit_Metrics *metrics )
1408 FT_Memory memory = stream->memory;
1409 FT_ULong ebdt_pos, glyph_offset;
1411 TT_SBit_Strike* strike;
1412 TT_SBit_Range* range;
1415 /* Check whether there is a glyph sbit for the current index */
1416 error = Find_SBit_Image( face, glyph_index, strike_index,
1417 &range, &strike, &glyph_offset );
1421 /* now, find the location of the `EBDT' table in */
1423 error = face->goto_table( face, TTAG_EBDT, stream, 0 );
1425 error = face->goto_table( face, TTAG_bdat, stream, 0 );
1429 ebdt_pos = FILE_Pos();
1431 /* clear the bitmap & load the bitmap */
1432 if ( face->root.glyph->flags & ft_glyph_own_bitmap )
1433 FREE( map->buffer );
1435 map->rows = map->pitch = map->width = 0;
1437 error = Load_SBit_Image( strike, range, ebdt_pos, glyph_offset,
1438 map, 0, 0, stream, metrics );
1442 /* the glyph slot owns this bitmap buffer */
1443 face->root.glyph->flags |= ft_glyph_own_bitmap;
1445 /* setup vertical metrics if needed */
1446 if ( strike->flags & 1 )
1448 /* in case of a horizontal strike only */
1452 advance = strike->hori.ascender - strike->hori.descender;
1454 /* some heuristic values */
1456 metrics->vertBearingX = (FT_Char)(-metrics->width / 2 );
1457 metrics->vertBearingY = (FT_Char)( advance / 10 );
1458 metrics->vertAdvance = (FT_Char)( advance * 12 / 10 );
1461 /* Crop the bitmap now, unless specified otherwise */
1462 if ( load_flags & FT_LOAD_CROP_BITMAP )
1463 Crop_Bitmap( map, metrics );