/src/compiler/android-ndk/jni/freetype/src/sfnt/ttsbit0.c

http://ftk.googlecode.com/ · C · 1011 lines · 697 code · 224 blank · 90 comment · 97 complexity · 2b988fa453b76cc0a37a9da0b90feafb MD5 · raw file

  1. /***************************************************************************/
  2. /* */
  3. /* ttsbit0.c */
  4. /* */
  5. /* TrueType and OpenType embedded bitmap support (body). */
  6. /* This is a heap-optimized version. */
  7. /* */
  8. /* Copyright 2005, 2006, 2007, 2008, 2009 by */
  9. /* David Turner, Robert Wilhelm, and Werner Lemberg. */
  10. /* */
  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. */
  16. /* */
  17. /***************************************************************************/
  18. /* This file is included by ttsbit.c */
  19. #include <ft2build.h>
  20. #include FT_INTERNAL_DEBUG_H
  21. #include FT_INTERNAL_STREAM_H
  22. #include FT_TRUETYPE_TAGS_H
  23. #include "ttsbit.h"
  24. #include "sferrors.h"
  25. /*************************************************************************/
  26. /* */
  27. /* The macro FT_COMPONENT is used in trace mode. It is an implicit */
  28. /* parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log */
  29. /* messages during execution. */
  30. /* */
  31. #undef FT_COMPONENT
  32. #define FT_COMPONENT trace_ttsbit
  33. FT_LOCAL_DEF( FT_Error )
  34. tt_face_load_eblc( TT_Face face,
  35. FT_Stream stream )
  36. {
  37. FT_Error error = SFNT_Err_Ok;
  38. FT_Fixed version;
  39. FT_ULong num_strikes, table_size;
  40. FT_Byte* p;
  41. FT_Byte* p_limit;
  42. FT_UInt count;
  43. face->sbit_num_strikes = 0;
  44. /* this table is optional */
  45. error = face->goto_table( face, TTAG_EBLC, stream, &table_size );
  46. if ( error )
  47. error = face->goto_table( face, TTAG_bloc, stream, &table_size );
  48. if ( error )
  49. goto Exit;
  50. if ( table_size < 8 )
  51. {
  52. FT_ERROR(( "tt_face_load_sbit_strikes: table too short\n" ));
  53. error = SFNT_Err_Invalid_File_Format;
  54. goto Exit;
  55. }
  56. if ( FT_FRAME_EXTRACT( table_size, face->sbit_table ) )
  57. goto Exit;
  58. face->sbit_table_size = table_size;
  59. p = face->sbit_table;
  60. p_limit = p + table_size;
  61. version = FT_NEXT_ULONG( p );
  62. num_strikes = FT_NEXT_ULONG( p );
  63. if ( version != 0x00020000UL || num_strikes >= 0x10000UL )
  64. {
  65. FT_ERROR(( "tt_face_load_sbit_strikes: invalid table version\n" ));
  66. error = SFNT_Err_Invalid_File_Format;
  67. goto Fail;
  68. }
  69. /*
  70. * Count the number of strikes available in the table. We are a bit
  71. * paranoid there and don't trust the data.
  72. */
  73. count = (FT_UInt)num_strikes;
  74. if ( 8 + 48UL * count > table_size )
  75. count = (FT_UInt)( ( p_limit - p ) / 48 );
  76. face->sbit_num_strikes = count;
  77. FT_TRACE3(( "sbit_num_strikes: %u\n", count ));
  78. Exit:
  79. return error;
  80. Fail:
  81. FT_FRAME_RELEASE( face->sbit_table );
  82. face->sbit_table_size = 0;
  83. goto Exit;
  84. }
  85. FT_LOCAL_DEF( void )
  86. tt_face_free_eblc( TT_Face face )
  87. {
  88. FT_Stream stream = face->root.stream;
  89. FT_FRAME_RELEASE( face->sbit_table );
  90. face->sbit_table_size = 0;
  91. face->sbit_num_strikes = 0;
  92. }
  93. FT_LOCAL_DEF( FT_Error )
  94. tt_face_set_sbit_strike( TT_Face face,
  95. FT_Size_Request req,
  96. FT_ULong* astrike_index )
  97. {
  98. return FT_Match_Size( (FT_Face)face, req, 0, astrike_index );
  99. }
  100. FT_LOCAL_DEF( FT_Error )
  101. tt_face_load_strike_metrics( TT_Face face,
  102. FT_ULong strike_index,
  103. FT_Size_Metrics* metrics )
  104. {
  105. FT_Byte* strike;
  106. if ( strike_index >= (FT_ULong)face->sbit_num_strikes )
  107. return SFNT_Err_Invalid_Argument;
  108. strike = face->sbit_table + 8 + strike_index * 48;
  109. metrics->x_ppem = (FT_UShort)strike[44];
  110. metrics->y_ppem = (FT_UShort)strike[45];
  111. metrics->ascender = (FT_Char)strike[16] << 6; /* hori.ascender */
  112. metrics->descender = (FT_Char)strike[17] << 6; /* hori.descender */
  113. metrics->height = metrics->ascender - metrics->descender;
  114. /* XXX: Is this correct? */
  115. metrics->max_advance = ( (FT_Char)strike[22] + /* min_origin_SB */
  116. strike[18] + /* max_width */
  117. (FT_Char)strike[23] /* min_advance_SB */
  118. ) << 6;
  119. return SFNT_Err_Ok;
  120. }
  121. typedef struct TT_SBitDecoderRec_
  122. {
  123. TT_Face face;
  124. FT_Stream stream;
  125. FT_Bitmap* bitmap;
  126. TT_SBit_Metrics metrics;
  127. FT_Bool metrics_loaded;
  128. FT_Bool bitmap_allocated;
  129. FT_Byte bit_depth;
  130. FT_ULong ebdt_start;
  131. FT_ULong ebdt_size;
  132. FT_ULong strike_index_array;
  133. FT_ULong strike_index_count;
  134. FT_Byte* eblc_base;
  135. FT_Byte* eblc_limit;
  136. } TT_SBitDecoderRec, *TT_SBitDecoder;
  137. static FT_Error
  138. tt_sbit_decoder_init( TT_SBitDecoder decoder,
  139. TT_Face face,
  140. FT_ULong strike_index,
  141. TT_SBit_MetricsRec* metrics )
  142. {
  143. FT_Error error;
  144. FT_Stream stream = face->root.stream;
  145. FT_ULong ebdt_size;
  146. error = face->goto_table( face, TTAG_EBDT, stream, &ebdt_size );
  147. if ( error )
  148. error = face->goto_table( face, TTAG_bdat, stream, &ebdt_size );
  149. if ( error )
  150. goto Exit;
  151. decoder->face = face;
  152. decoder->stream = stream;
  153. decoder->bitmap = &face->root.glyph->bitmap;
  154. decoder->metrics = metrics;
  155. decoder->metrics_loaded = 0;
  156. decoder->bitmap_allocated = 0;
  157. decoder->ebdt_start = FT_STREAM_POS();
  158. decoder->ebdt_size = ebdt_size;
  159. decoder->eblc_base = face->sbit_table;
  160. decoder->eblc_limit = face->sbit_table + face->sbit_table_size;
  161. /* now find the strike corresponding to the index */
  162. {
  163. FT_Byte* p;
  164. if ( 8 + 48 * strike_index + 3 * 4 + 34 + 1 > face->sbit_table_size )
  165. {
  166. error = SFNT_Err_Invalid_File_Format;
  167. goto Exit;
  168. }
  169. p = decoder->eblc_base + 8 + 48 * strike_index;
  170. decoder->strike_index_array = FT_NEXT_ULONG( p );
  171. p += 4;
  172. decoder->strike_index_count = FT_NEXT_ULONG( p );
  173. p += 34;
  174. decoder->bit_depth = *p;
  175. if ( decoder->strike_index_array > face->sbit_table_size ||
  176. decoder->strike_index_array + 8 * decoder->strike_index_count >
  177. face->sbit_table_size )
  178. error = SFNT_Err_Invalid_File_Format;
  179. }
  180. Exit:
  181. return error;
  182. }
  183. static void
  184. tt_sbit_decoder_done( TT_SBitDecoder decoder )
  185. {
  186. FT_UNUSED( decoder );
  187. }
  188. static FT_Error
  189. tt_sbit_decoder_alloc_bitmap( TT_SBitDecoder decoder )
  190. {
  191. FT_Error error = SFNT_Err_Ok;
  192. FT_UInt width, height;
  193. FT_Bitmap* map = decoder->bitmap;
  194. FT_Long size;
  195. if ( !decoder->metrics_loaded )
  196. {
  197. error = SFNT_Err_Invalid_Argument;
  198. goto Exit;
  199. }
  200. width = decoder->metrics->width;
  201. height = decoder->metrics->height;
  202. map->width = (int)width;
  203. map->rows = (int)height;
  204. switch ( decoder->bit_depth )
  205. {
  206. case 1:
  207. map->pixel_mode = FT_PIXEL_MODE_MONO;
  208. map->pitch = ( map->width + 7 ) >> 3;
  209. break;
  210. case 2:
  211. map->pixel_mode = FT_PIXEL_MODE_GRAY2;
  212. map->pitch = ( map->width + 3 ) >> 2;
  213. break;
  214. case 4:
  215. map->pixel_mode = FT_PIXEL_MODE_GRAY4;
  216. map->pitch = ( map->width + 1 ) >> 1;
  217. break;
  218. case 8:
  219. map->pixel_mode = FT_PIXEL_MODE_GRAY;
  220. map->pitch = map->width;
  221. break;
  222. default:
  223. error = SFNT_Err_Invalid_File_Format;
  224. goto Exit;
  225. }
  226. size = map->rows * map->pitch;
  227. /* check that there is no empty image */
  228. if ( size == 0 )
  229. goto Exit; /* exit successfully! */
  230. error = ft_glyphslot_alloc_bitmap( decoder->face->root.glyph, size );
  231. if ( error )
  232. goto Exit;
  233. decoder->bitmap_allocated = 1;
  234. Exit:
  235. return error;
  236. }
  237. static FT_Error
  238. tt_sbit_decoder_load_metrics( TT_SBitDecoder decoder,
  239. FT_Byte* *pp,
  240. FT_Byte* limit,
  241. FT_Bool big )
  242. {
  243. FT_Byte* p = *pp;
  244. TT_SBit_Metrics metrics = decoder->metrics;
  245. if ( p + 5 > limit )
  246. goto Fail;
  247. metrics->height = p[0];
  248. metrics->width = p[1];
  249. metrics->horiBearingX = (FT_Char)p[2];
  250. metrics->horiBearingY = (FT_Char)p[3];
  251. metrics->horiAdvance = p[4];
  252. p += 5;
  253. if ( big )
  254. {
  255. if ( p + 3 > limit )
  256. goto Fail;
  257. metrics->vertBearingX = (FT_Char)p[0];
  258. metrics->vertBearingY = (FT_Char)p[1];
  259. metrics->vertAdvance = p[2];
  260. p += 3;
  261. }
  262. decoder->metrics_loaded = 1;
  263. *pp = p;
  264. return SFNT_Err_Ok;
  265. Fail:
  266. return SFNT_Err_Invalid_Argument;
  267. }
  268. /* forward declaration */
  269. static FT_Error
  270. tt_sbit_decoder_load_image( TT_SBitDecoder decoder,
  271. FT_UInt glyph_index,
  272. FT_Int x_pos,
  273. FT_Int y_pos );
  274. typedef FT_Error (*TT_SBitDecoder_LoadFunc)( TT_SBitDecoder decoder,
  275. FT_Byte* p,
  276. FT_Byte* plimit,
  277. FT_Int x_pos,
  278. FT_Int y_pos );
  279. static FT_Error
  280. tt_sbit_decoder_load_byte_aligned( TT_SBitDecoder decoder,
  281. FT_Byte* p,
  282. FT_Byte* limit,
  283. FT_Int x_pos,
  284. FT_Int y_pos )
  285. {
  286. FT_Error error = SFNT_Err_Ok;
  287. FT_Byte* line;
  288. FT_Int bit_height, bit_width, pitch, width, height, h;
  289. FT_Bitmap* bitmap;
  290. if ( !decoder->bitmap_allocated )
  291. {
  292. error = tt_sbit_decoder_alloc_bitmap( decoder );
  293. if ( error )
  294. goto Exit;
  295. }
  296. /* check that we can write the glyph into the bitmap */
  297. bitmap = decoder->bitmap;
  298. bit_width = bitmap->width;
  299. bit_height = bitmap->rows;
  300. pitch = bitmap->pitch;
  301. line = bitmap->buffer;
  302. width = decoder->metrics->width;
  303. height = decoder->metrics->height;
  304. if ( x_pos < 0 || x_pos + width > bit_width ||
  305. y_pos < 0 || y_pos + height > bit_height )
  306. {
  307. error = SFNT_Err_Invalid_File_Format;
  308. goto Exit;
  309. }
  310. if ( p + ( ( width + 7 ) >> 3 ) * height > limit )
  311. {
  312. error = SFNT_Err_Invalid_File_Format;
  313. goto Exit;
  314. }
  315. /* now do the blit */
  316. line += y_pos * pitch + ( x_pos >> 3 );
  317. x_pos &= 7;
  318. if ( x_pos == 0 ) /* the easy one */
  319. {
  320. for ( h = height; h > 0; h--, line += pitch )
  321. {
  322. FT_Byte* write = line;
  323. FT_Int w;
  324. for ( w = width; w >= 8; w -= 8 )
  325. {
  326. write[0] = (FT_Byte)( write[0] | *p++ );
  327. write += 1;
  328. }
  329. if ( w > 0 )
  330. write[0] = (FT_Byte)( write[0] | ( *p++ & ( 0xFF00U >> w ) ) );
  331. }
  332. }
  333. else /* x_pos > 0 */
  334. {
  335. for ( h = height; h > 0; h--, line += pitch )
  336. {
  337. FT_Byte* write = line;
  338. FT_Int w;
  339. FT_UInt wval = 0;
  340. for ( w = width; w >= 8; w -= 8 )
  341. {
  342. wval = (FT_UInt)( wval | *p++ );
  343. write[0] = (FT_Byte)( write[0] | ( wval >> x_pos ) );
  344. write += 1;
  345. wval <<= 8;
  346. }
  347. if ( w > 0 )
  348. wval = (FT_UInt)( wval | ( *p++ & ( 0xFF00U >> w ) ) );
  349. /* all bits read and there are `x_pos + w' bits to be written */
  350. write[0] = (FT_Byte)( write[0] | ( wval >> x_pos ) );
  351. if ( x_pos + w > 8 )
  352. {
  353. write++;
  354. wval <<= 8;
  355. write[0] = (FT_Byte)( write[0] | ( wval >> x_pos ) );
  356. }
  357. }
  358. }
  359. Exit:
  360. return error;
  361. }
  362. /*
  363. * Load a bit-aligned bitmap (with pointer `p') into a line-aligned bitmap
  364. * (with pointer `write'). In the example below, the width is 3 pixel,
  365. * and `x_pos' is 1 pixel.
  366. *
  367. * p p+1
  368. * | | |
  369. * | 7 6 5 4 3 2 1 0 | 7 6 5 4 3 2 1 0 |...
  370. * | | |
  371. * +-------+ +-------+ +-------+ ...
  372. * . . .
  373. * . . .
  374. * v . .
  375. * +-------+ . .
  376. * | | .
  377. * | 7 6 5 4 3 2 1 0 | .
  378. * | | .
  379. * write . .
  380. * . .
  381. * v .
  382. * +-------+ .
  383. * | |
  384. * | 7 6 5 4 3 2 1 0 |
  385. * | |
  386. * write+1 .
  387. * .
  388. * v
  389. * +-------+
  390. * | |
  391. * | 7 6 5 4 3 2 1 0 |
  392. * | |
  393. * write+2
  394. *
  395. */
  396. static FT_Error
  397. tt_sbit_decoder_load_bit_aligned( TT_SBitDecoder decoder,
  398. FT_Byte* p,
  399. FT_Byte* limit,
  400. FT_Int x_pos,
  401. FT_Int y_pos )
  402. {
  403. FT_Error error = SFNT_Err_Ok;
  404. FT_Byte* line;
  405. FT_Int bit_height, bit_width, pitch, width, height, h, nbits;
  406. FT_Bitmap* bitmap;
  407. FT_UShort rval;
  408. if ( !decoder->bitmap_allocated )
  409. {
  410. error = tt_sbit_decoder_alloc_bitmap( decoder );
  411. if ( error )
  412. goto Exit;
  413. }
  414. /* check that we can write the glyph into the bitmap */
  415. bitmap = decoder->bitmap;
  416. bit_width = bitmap->width;
  417. bit_height = bitmap->rows;
  418. pitch = bitmap->pitch;
  419. line = bitmap->buffer;
  420. width = decoder->metrics->width;
  421. height = decoder->metrics->height;
  422. if ( x_pos < 0 || x_pos + width > bit_width ||
  423. y_pos < 0 || y_pos + height > bit_height )
  424. {
  425. error = SFNT_Err_Invalid_File_Format;
  426. goto Exit;
  427. }
  428. if ( p + ( ( width * height + 7 ) >> 3 ) > limit )
  429. {
  430. error = SFNT_Err_Invalid_File_Format;
  431. goto Exit;
  432. }
  433. /* now do the blit */
  434. /* adjust `line' to point to the first byte of the bitmap */
  435. line += y_pos * pitch + ( x_pos >> 3 );
  436. x_pos &= 7;
  437. /* the higher byte of `rval' is used as a buffer */
  438. rval = 0;
  439. nbits = 0;
  440. for ( h = height; h > 0; h--, line += pitch )
  441. {
  442. FT_Byte* write = line;
  443. FT_Int w = width;
  444. /* handle initial byte (in target bitmap) specially if necessary */
  445. if ( x_pos )
  446. {
  447. w = ( width < 8 - x_pos ) ? width : 8 - x_pos;
  448. if ( h == height )
  449. {
  450. rval = *p++;
  451. nbits = x_pos;
  452. }
  453. else if ( nbits < w )
  454. {
  455. if ( p < limit )
  456. rval |= *p++;
  457. nbits += 8 - w;
  458. }
  459. else
  460. {
  461. rval >>= 8;
  462. nbits -= w;
  463. }
  464. *write++ |= ( ( rval >> nbits ) & 0xFF ) &
  465. ( ~( 0xFF << w ) << ( 8 - w - x_pos ) );
  466. rval <<= 8;
  467. w = width - w;
  468. }
  469. /* handle medial bytes */
  470. for ( ; w >= 8; w -= 8 )
  471. {
  472. rval |= *p++;
  473. *write++ |= ( rval >> nbits ) & 0xFF;
  474. rval <<= 8;
  475. }
  476. /* handle final byte if necessary */
  477. if ( w > 0 )
  478. {
  479. if ( nbits < w )
  480. {
  481. if ( p < limit )
  482. rval |= *p++;
  483. *write |= ( ( rval >> nbits ) & 0xFF ) & ( 0xFF00U >> w );
  484. nbits += 8 - w;
  485. rval <<= 8;
  486. }
  487. else
  488. {
  489. *write |= ( ( rval >> nbits ) & 0xFF ) & ( 0xFF00U >> w );
  490. nbits -= w;
  491. }
  492. }
  493. }
  494. Exit:
  495. return error;
  496. }
  497. static FT_Error
  498. tt_sbit_decoder_load_compound( TT_SBitDecoder decoder,
  499. FT_Byte* p,
  500. FT_Byte* limit,
  501. FT_Int x_pos,
  502. FT_Int y_pos )
  503. {
  504. FT_Error error = SFNT_Err_Ok;
  505. FT_UInt num_components, nn;
  506. FT_Char horiBearingX = decoder->metrics->horiBearingX;
  507. FT_Char horiBearingY = decoder->metrics->horiBearingY;
  508. FT_Byte horiAdvance = decoder->metrics->horiAdvance;
  509. FT_Char vertBearingX = decoder->metrics->vertBearingX;
  510. FT_Char vertBearingY = decoder->metrics->vertBearingY;
  511. FT_Byte vertAdvance = decoder->metrics->vertAdvance;
  512. if ( p + 2 > limit )
  513. goto Fail;
  514. num_components = FT_NEXT_USHORT( p );
  515. if ( p + 4 * num_components > limit )
  516. goto Fail;
  517. if ( !decoder->bitmap_allocated )
  518. {
  519. error = tt_sbit_decoder_alloc_bitmap( decoder );
  520. if ( error )
  521. goto Exit;
  522. }
  523. for ( nn = 0; nn < num_components; nn++ )
  524. {
  525. FT_UInt gindex = FT_NEXT_USHORT( p );
  526. FT_Byte dx = FT_NEXT_BYTE( p );
  527. FT_Byte dy = FT_NEXT_BYTE( p );
  528. /* NB: a recursive call */
  529. error = tt_sbit_decoder_load_image( decoder, gindex,
  530. x_pos + dx, y_pos + dy );
  531. if ( error )
  532. break;
  533. }
  534. decoder->metrics->horiBearingX = horiBearingX;
  535. decoder->metrics->horiBearingY = horiBearingY;
  536. decoder->metrics->horiAdvance = horiAdvance;
  537. decoder->metrics->vertBearingX = vertBearingX;
  538. decoder->metrics->vertBearingY = vertBearingY;
  539. decoder->metrics->vertAdvance = vertAdvance;
  540. decoder->metrics->width = (FT_UInt)decoder->bitmap->width;
  541. decoder->metrics->height = (FT_UInt)decoder->bitmap->rows;
  542. Exit:
  543. return error;
  544. Fail:
  545. error = SFNT_Err_Invalid_File_Format;
  546. goto Exit;
  547. }
  548. static FT_Error
  549. tt_sbit_decoder_load_bitmap( TT_SBitDecoder decoder,
  550. FT_UInt glyph_format,
  551. FT_ULong glyph_start,
  552. FT_ULong glyph_size,
  553. FT_Int x_pos,
  554. FT_Int y_pos )
  555. {
  556. FT_Error error;
  557. FT_Stream stream = decoder->stream;
  558. FT_Byte* p;
  559. FT_Byte* p_limit;
  560. FT_Byte* data;
  561. /* seek into the EBDT table now */
  562. if ( glyph_start + glyph_size > decoder->ebdt_size )
  563. {
  564. error = SFNT_Err_Invalid_Argument;
  565. goto Exit;
  566. }
  567. if ( FT_STREAM_SEEK( decoder->ebdt_start + glyph_start ) ||
  568. FT_FRAME_EXTRACT( glyph_size, data ) )
  569. goto Exit;
  570. p = data;
  571. p_limit = p + glyph_size;
  572. /* read the data, depending on the glyph format */
  573. switch ( glyph_format )
  574. {
  575. case 1:
  576. case 2:
  577. case 8:
  578. error = tt_sbit_decoder_load_metrics( decoder, &p, p_limit, 0 );
  579. break;
  580. case 6:
  581. case 7:
  582. case 9:
  583. error = tt_sbit_decoder_load_metrics( decoder, &p, p_limit, 1 );
  584. break;
  585. default:
  586. error = SFNT_Err_Ok;
  587. }
  588. if ( error )
  589. goto Fail;
  590. {
  591. TT_SBitDecoder_LoadFunc loader;
  592. switch ( glyph_format )
  593. {
  594. case 1:
  595. case 6:
  596. loader = tt_sbit_decoder_load_byte_aligned;
  597. break;
  598. case 2:
  599. case 5:
  600. case 7:
  601. loader = tt_sbit_decoder_load_bit_aligned;
  602. break;
  603. case 8:
  604. if ( p + 1 > p_limit )
  605. goto Fail;
  606. p += 1; /* skip padding */
  607. /* fall-through */
  608. case 9:
  609. loader = tt_sbit_decoder_load_compound;
  610. break;
  611. default:
  612. goto Fail;
  613. }
  614. error = loader( decoder, p, p_limit, x_pos, y_pos );
  615. }
  616. Fail:
  617. FT_FRAME_RELEASE( data );
  618. Exit:
  619. return error;
  620. }
  621. static FT_Error
  622. tt_sbit_decoder_load_image( TT_SBitDecoder decoder,
  623. FT_UInt glyph_index,
  624. FT_Int x_pos,
  625. FT_Int y_pos )
  626. {
  627. /*
  628. * First, we find the correct strike range that applies to this
  629. * glyph index.
  630. */
  631. FT_Byte* p = decoder->eblc_base + decoder->strike_index_array;
  632. FT_Byte* p_limit = decoder->eblc_limit;
  633. FT_ULong num_ranges = decoder->strike_index_count;
  634. FT_UInt start, end, index_format, image_format;
  635. FT_ULong image_start = 0, image_end = 0, image_offset;
  636. for ( ; num_ranges > 0; num_ranges-- )
  637. {
  638. start = FT_NEXT_USHORT( p );
  639. end = FT_NEXT_USHORT( p );
  640. if ( glyph_index >= start && glyph_index <= end )
  641. goto FoundRange;
  642. p += 4; /* ignore index offset */
  643. }
  644. goto NoBitmap;
  645. FoundRange:
  646. image_offset = FT_NEXT_ULONG( p );
  647. /* overflow check */
  648. if ( decoder->eblc_base + decoder->strike_index_array + image_offset <
  649. decoder->eblc_base )
  650. goto Failure;
  651. p = decoder->eblc_base + decoder->strike_index_array + image_offset;
  652. if ( p + 8 > p_limit )
  653. goto NoBitmap;
  654. /* now find the glyph's location and extend within the ebdt table */
  655. index_format = FT_NEXT_USHORT( p );
  656. image_format = FT_NEXT_USHORT( p );
  657. image_offset = FT_NEXT_ULONG ( p );
  658. switch ( index_format )
  659. {
  660. case 1: /* 4-byte offsets relative to `image_offset' */
  661. {
  662. p += 4 * ( glyph_index - start );
  663. if ( p + 8 > p_limit )
  664. goto NoBitmap;
  665. image_start = FT_NEXT_ULONG( p );
  666. image_end = FT_NEXT_ULONG( p );
  667. if ( image_start == image_end ) /* missing glyph */
  668. goto NoBitmap;
  669. }
  670. break;
  671. case 2: /* big metrics, constant image size */
  672. {
  673. FT_ULong image_size;
  674. if ( p + 12 > p_limit )
  675. goto NoBitmap;
  676. image_size = FT_NEXT_ULONG( p );
  677. if ( tt_sbit_decoder_load_metrics( decoder, &p, p_limit, 1 ) )
  678. goto NoBitmap;
  679. image_start = image_size * ( glyph_index - start );
  680. image_end = image_start + image_size;
  681. }
  682. break;
  683. case 3: /* 2-byte offsets relative to 'image_offset' */
  684. {
  685. p += 2 * ( glyph_index - start );
  686. if ( p + 4 > p_limit )
  687. goto NoBitmap;
  688. image_start = FT_NEXT_USHORT( p );
  689. image_end = FT_NEXT_USHORT( p );
  690. if ( image_start == image_end ) /* missing glyph */
  691. goto NoBitmap;
  692. }
  693. break;
  694. case 4: /* sparse glyph array with (glyph,offset) pairs */
  695. {
  696. FT_ULong mm, num_glyphs;
  697. if ( p + 4 > p_limit )
  698. goto NoBitmap;
  699. num_glyphs = FT_NEXT_ULONG( p );
  700. /* overflow check */
  701. if ( p + ( num_glyphs + 1 ) * 4 < p )
  702. goto Failure;
  703. if ( p + ( num_glyphs + 1 ) * 4 > p_limit )
  704. goto NoBitmap;
  705. for ( mm = 0; mm < num_glyphs; mm++ )
  706. {
  707. FT_UInt gindex = FT_NEXT_USHORT( p );
  708. if ( gindex == glyph_index )
  709. {
  710. image_start = FT_NEXT_USHORT( p );
  711. p += 2;
  712. image_end = FT_PEEK_USHORT( p );
  713. break;
  714. }
  715. p += 2;
  716. }
  717. if ( mm >= num_glyphs )
  718. goto NoBitmap;
  719. }
  720. break;
  721. case 5: /* constant metrics with sparse glyph codes */
  722. {
  723. FT_ULong image_size, mm, num_glyphs;
  724. if ( p + 16 > p_limit )
  725. goto NoBitmap;
  726. image_size = FT_NEXT_ULONG( p );
  727. if ( tt_sbit_decoder_load_metrics( decoder, &p, p_limit, 1 ) )
  728. goto NoBitmap;
  729. num_glyphs = FT_NEXT_ULONG( p );
  730. /* overflow check */
  731. if ( p + 2 * num_glyphs < p )
  732. goto Failure;
  733. if ( p + 2 * num_glyphs > p_limit )
  734. goto NoBitmap;
  735. for ( mm = 0; mm < num_glyphs; mm++ )
  736. {
  737. FT_UInt gindex = FT_NEXT_USHORT( p );
  738. if ( gindex == glyph_index )
  739. break;
  740. }
  741. if ( mm >= num_glyphs )
  742. goto NoBitmap;
  743. image_start = image_size * mm;
  744. image_end = image_start + image_size;
  745. }
  746. break;
  747. default:
  748. goto NoBitmap;
  749. }
  750. if ( image_start > image_end )
  751. goto NoBitmap;
  752. image_end -= image_start;
  753. image_start = image_offset + image_start;
  754. return tt_sbit_decoder_load_bitmap( decoder,
  755. image_format,
  756. image_start,
  757. image_end,
  758. x_pos,
  759. y_pos );
  760. Failure:
  761. return SFNT_Err_Invalid_Table;
  762. NoBitmap:
  763. return SFNT_Err_Invalid_Argument;
  764. }
  765. FT_LOCAL( FT_Error )
  766. tt_face_load_sbit_image( TT_Face face,
  767. FT_ULong strike_index,
  768. FT_UInt glyph_index,
  769. FT_UInt load_flags,
  770. FT_Stream stream,
  771. FT_Bitmap *map,
  772. TT_SBit_MetricsRec *metrics )
  773. {
  774. TT_SBitDecoderRec decoder[1];
  775. FT_Error error;
  776. FT_UNUSED( load_flags );
  777. FT_UNUSED( stream );
  778. FT_UNUSED( map );
  779. error = tt_sbit_decoder_init( decoder, face, strike_index, metrics );
  780. if ( !error )
  781. {
  782. error = tt_sbit_decoder_load_image( decoder, glyph_index, 0, 0 );
  783. tt_sbit_decoder_done( decoder );
  784. }
  785. return error;
  786. }
  787. /* EOF */