/src/3rdparty/freetype/src/sfnt/ttsbit0.c

https://bitbucket.org/ultra_iter/qt-vtl · C · 969 lines · 695 code · 222 blank · 52 comment · 95 complexity · 6bf17f84492b71120d65976ea16355b0 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. static FT_Error
  363. tt_sbit_decoder_load_bit_aligned( TT_SBitDecoder decoder,
  364. FT_Byte* p,
  365. FT_Byte* limit,
  366. FT_Int x_pos,
  367. FT_Int y_pos )
  368. {
  369. FT_Error error = SFNT_Err_Ok;
  370. FT_Byte* line;
  371. FT_Int bit_height, bit_width, pitch, width, height, h, nbits;
  372. FT_Bitmap* bitmap;
  373. FT_UShort rval;
  374. if ( !decoder->bitmap_allocated )
  375. {
  376. error = tt_sbit_decoder_alloc_bitmap( decoder );
  377. if ( error )
  378. goto Exit;
  379. }
  380. /* check that we can write the glyph into the bitmap */
  381. bitmap = decoder->bitmap;
  382. bit_width = bitmap->width;
  383. bit_height = bitmap->rows;
  384. pitch = bitmap->pitch;
  385. line = bitmap->buffer;
  386. width = decoder->metrics->width;
  387. height = decoder->metrics->height;
  388. if ( x_pos < 0 || x_pos + width > bit_width ||
  389. y_pos < 0 || y_pos + height > bit_height )
  390. {
  391. error = SFNT_Err_Invalid_File_Format;
  392. goto Exit;
  393. }
  394. if ( p + ( ( width * height + 7 ) >> 3 ) > limit )
  395. {
  396. error = SFNT_Err_Invalid_File_Format;
  397. goto Exit;
  398. }
  399. /* now do the blit */
  400. line += y_pos * pitch + ( x_pos >> 3 );
  401. x_pos &= 7;
  402. /* the higher byte of `rval' is used as a buffer */
  403. rval = 0;
  404. nbits = 0;
  405. for ( h = height; h > 0; h--, line += pitch )
  406. {
  407. FT_Byte* write = line;
  408. FT_Int w = width;
  409. if ( x_pos )
  410. {
  411. w = ( width < 8 - x_pos ) ? width : 8 - x_pos;
  412. if ( h == height )
  413. {
  414. rval |= *p++;
  415. nbits += x_pos;
  416. }
  417. else if ( nbits < w )
  418. {
  419. rval |= *p++;
  420. nbits += 8 - w;
  421. }
  422. else
  423. {
  424. rval >>= 8;
  425. nbits -= w;
  426. }
  427. *write++ |= ( ( rval >> nbits ) & 0xFF ) &
  428. ( ~( 0xFF << w ) << ( 8 - w - x_pos ) );
  429. rval <<= 8;
  430. w = width - w;
  431. }
  432. for ( ; w >= 8; w -= 8 )
  433. {
  434. rval |= *p++;
  435. *write++ |= ( rval >> nbits ) & 0xFF;
  436. rval <<= 8;
  437. }
  438. if ( w > 0 )
  439. {
  440. if ( nbits < w )
  441. {
  442. rval |= *p++;
  443. *write |= ( ( rval >> nbits ) & 0xFF ) & ( 0xFF00U >> w );
  444. nbits += 8 - w;
  445. rval <<= 8;
  446. }
  447. else
  448. {
  449. *write |= ( ( rval >> nbits ) & 0xFF ) & ( 0xFF00U >> w );
  450. nbits -= w;
  451. }
  452. }
  453. }
  454. Exit:
  455. return error;
  456. }
  457. static FT_Error
  458. tt_sbit_decoder_load_compound( TT_SBitDecoder decoder,
  459. FT_Byte* p,
  460. FT_Byte* limit,
  461. FT_Int x_pos,
  462. FT_Int y_pos )
  463. {
  464. FT_Error error = SFNT_Err_Ok;
  465. FT_UInt num_components, nn;
  466. FT_Char horiBearingX = decoder->metrics->horiBearingX;
  467. FT_Char horiBearingY = decoder->metrics->horiBearingY;
  468. FT_Byte horiAdvance = decoder->metrics->horiAdvance;
  469. FT_Char vertBearingX = decoder->metrics->vertBearingX;
  470. FT_Char vertBearingY = decoder->metrics->vertBearingY;
  471. FT_Byte vertAdvance = decoder->metrics->vertAdvance;
  472. if ( p + 2 > limit )
  473. goto Fail;
  474. num_components = FT_NEXT_USHORT( p );
  475. if ( p + 4 * num_components > limit )
  476. goto Fail;
  477. if ( !decoder->bitmap_allocated )
  478. {
  479. error = tt_sbit_decoder_alloc_bitmap( decoder );
  480. if ( error )
  481. goto Exit;
  482. }
  483. for ( nn = 0; nn < num_components; nn++ )
  484. {
  485. FT_UInt gindex = FT_NEXT_USHORT( p );
  486. FT_Byte dx = FT_NEXT_BYTE( p );
  487. FT_Byte dy = FT_NEXT_BYTE( p );
  488. /* NB: a recursive call */
  489. error = tt_sbit_decoder_load_image( decoder, gindex,
  490. x_pos + dx, y_pos + dy );
  491. if ( error )
  492. break;
  493. }
  494. decoder->metrics->horiBearingX = horiBearingX;
  495. decoder->metrics->horiBearingY = horiBearingY;
  496. decoder->metrics->horiAdvance = horiAdvance;
  497. decoder->metrics->vertBearingX = vertBearingX;
  498. decoder->metrics->vertBearingY = vertBearingY;
  499. decoder->metrics->vertAdvance = vertAdvance;
  500. decoder->metrics->width = (FT_UInt)decoder->bitmap->width;
  501. decoder->metrics->height = (FT_UInt)decoder->bitmap->rows;
  502. Exit:
  503. return error;
  504. Fail:
  505. error = SFNT_Err_Invalid_File_Format;
  506. goto Exit;
  507. }
  508. static FT_Error
  509. tt_sbit_decoder_load_bitmap( TT_SBitDecoder decoder,
  510. FT_UInt glyph_format,
  511. FT_ULong glyph_start,
  512. FT_ULong glyph_size,
  513. FT_Int x_pos,
  514. FT_Int y_pos )
  515. {
  516. FT_Error error;
  517. FT_Stream stream = decoder->stream;
  518. FT_Byte* p;
  519. FT_Byte* p_limit;
  520. FT_Byte* data;
  521. /* seek into the EBDT table now */
  522. if ( glyph_start + glyph_size > decoder->ebdt_size )
  523. {
  524. error = SFNT_Err_Invalid_Argument;
  525. goto Exit;
  526. }
  527. if ( FT_STREAM_SEEK( decoder->ebdt_start + glyph_start ) ||
  528. FT_FRAME_EXTRACT( glyph_size, data ) )
  529. goto Exit;
  530. p = data;
  531. p_limit = p + glyph_size;
  532. /* read the data, depending on the glyph format */
  533. switch ( glyph_format )
  534. {
  535. case 1:
  536. case 2:
  537. case 8:
  538. error = tt_sbit_decoder_load_metrics( decoder, &p, p_limit, 0 );
  539. break;
  540. case 6:
  541. case 7:
  542. case 9:
  543. error = tt_sbit_decoder_load_metrics( decoder, &p, p_limit, 1 );
  544. break;
  545. default:
  546. error = SFNT_Err_Ok;
  547. }
  548. if ( error )
  549. goto Fail;
  550. {
  551. TT_SBitDecoder_LoadFunc loader;
  552. switch ( glyph_format )
  553. {
  554. case 1:
  555. case 6:
  556. loader = tt_sbit_decoder_load_byte_aligned;
  557. break;
  558. case 2:
  559. case 5:
  560. case 7:
  561. loader = tt_sbit_decoder_load_bit_aligned;
  562. break;
  563. case 8:
  564. if ( p + 1 > p_limit )
  565. goto Fail;
  566. p += 1; /* skip padding */
  567. /* fall-through */
  568. case 9:
  569. loader = tt_sbit_decoder_load_compound;
  570. break;
  571. default:
  572. goto Fail;
  573. }
  574. error = loader( decoder, p, p_limit, x_pos, y_pos );
  575. }
  576. Fail:
  577. FT_FRAME_RELEASE( data );
  578. Exit:
  579. return error;
  580. }
  581. static FT_Error
  582. tt_sbit_decoder_load_image( TT_SBitDecoder decoder,
  583. FT_UInt glyph_index,
  584. FT_Int x_pos,
  585. FT_Int y_pos )
  586. {
  587. /*
  588. * First, we find the correct strike range that applies to this
  589. * glyph index.
  590. */
  591. FT_Byte* p = decoder->eblc_base + decoder->strike_index_array;
  592. FT_Byte* p_limit = decoder->eblc_limit;
  593. FT_ULong num_ranges = decoder->strike_index_count;
  594. FT_UInt start, end, index_format, image_format;
  595. FT_ULong image_start = 0, image_end = 0, image_offset;
  596. for ( ; num_ranges > 0; num_ranges-- )
  597. {
  598. start = FT_NEXT_USHORT( p );
  599. end = FT_NEXT_USHORT( p );
  600. if ( glyph_index >= start && glyph_index <= end )
  601. goto FoundRange;
  602. p += 4; /* ignore index offset */
  603. }
  604. goto NoBitmap;
  605. FoundRange:
  606. image_offset = FT_NEXT_ULONG( p );
  607. /* overflow check */
  608. if ( decoder->eblc_base + decoder->strike_index_array + image_offset <
  609. decoder->eblc_base )
  610. goto Failure;
  611. p = decoder->eblc_base + decoder->strike_index_array + image_offset;
  612. if ( p + 8 > p_limit )
  613. goto NoBitmap;
  614. /* now find the glyph's location and extend within the ebdt table */
  615. index_format = FT_NEXT_USHORT( p );
  616. image_format = FT_NEXT_USHORT( p );
  617. image_offset = FT_NEXT_ULONG ( p );
  618. switch ( index_format )
  619. {
  620. case 1: /* 4-byte offsets relative to `image_offset' */
  621. {
  622. p += 4 * ( glyph_index - start );
  623. if ( p + 8 > p_limit )
  624. goto NoBitmap;
  625. image_start = FT_NEXT_ULONG( p );
  626. image_end = FT_NEXT_ULONG( p );
  627. if ( image_start == image_end ) /* missing glyph */
  628. goto NoBitmap;
  629. }
  630. break;
  631. case 2: /* big metrics, constant image size */
  632. {
  633. FT_ULong image_size;
  634. if ( p + 12 > p_limit )
  635. goto NoBitmap;
  636. image_size = FT_NEXT_ULONG( p );
  637. if ( tt_sbit_decoder_load_metrics( decoder, &p, p_limit, 1 ) )
  638. goto NoBitmap;
  639. image_start = image_size * ( glyph_index - start );
  640. image_end = image_start + image_size;
  641. }
  642. break;
  643. case 3: /* 2-byte offsets relative to 'image_offset' */
  644. {
  645. p += 2 * ( glyph_index - start );
  646. if ( p + 4 > p_limit )
  647. goto NoBitmap;
  648. image_start = FT_NEXT_USHORT( p );
  649. image_end = FT_NEXT_USHORT( p );
  650. if ( image_start == image_end ) /* missing glyph */
  651. goto NoBitmap;
  652. }
  653. break;
  654. case 4: /* sparse glyph array with (glyph,offset) pairs */
  655. {
  656. FT_ULong mm, num_glyphs;
  657. if ( p + 4 > p_limit )
  658. goto NoBitmap;
  659. num_glyphs = FT_NEXT_ULONG( p );
  660. /* overflow check */
  661. if ( p + ( num_glyphs + 1 ) * 4 < p )
  662. goto Failure;
  663. if ( p + ( num_glyphs + 1 ) * 4 > p_limit )
  664. goto NoBitmap;
  665. for ( mm = 0; mm < num_glyphs; mm++ )
  666. {
  667. FT_UInt gindex = FT_NEXT_USHORT( p );
  668. if ( gindex == glyph_index )
  669. {
  670. image_start = FT_NEXT_USHORT( p );
  671. p += 2;
  672. image_end = FT_PEEK_USHORT( p );
  673. break;
  674. }
  675. p += 2;
  676. }
  677. if ( mm >= num_glyphs )
  678. goto NoBitmap;
  679. }
  680. break;
  681. case 5: /* constant metrics with sparse glyph codes */
  682. {
  683. FT_ULong image_size, mm, num_glyphs;
  684. if ( p + 16 > p_limit )
  685. goto NoBitmap;
  686. image_size = FT_NEXT_ULONG( p );
  687. if ( tt_sbit_decoder_load_metrics( decoder, &p, p_limit, 1 ) )
  688. goto NoBitmap;
  689. num_glyphs = FT_NEXT_ULONG( p );
  690. /* overflow check */
  691. if ( p + 2 * num_glyphs < p )
  692. goto Failure;
  693. if ( p + 2 * num_glyphs > p_limit )
  694. goto NoBitmap;
  695. for ( mm = 0; mm < num_glyphs; mm++ )
  696. {
  697. FT_UInt gindex = FT_NEXT_USHORT( p );
  698. if ( gindex == glyph_index )
  699. break;
  700. }
  701. if ( mm >= num_glyphs )
  702. goto NoBitmap;
  703. image_start = image_size * mm;
  704. image_end = image_start + image_size;
  705. }
  706. break;
  707. default:
  708. goto NoBitmap;
  709. }
  710. if ( image_start > image_end )
  711. goto NoBitmap;
  712. image_end -= image_start;
  713. image_start = image_offset + image_start;
  714. return tt_sbit_decoder_load_bitmap( decoder,
  715. image_format,
  716. image_start,
  717. image_end,
  718. x_pos,
  719. y_pos );
  720. Failure:
  721. return SFNT_Err_Invalid_Table;
  722. NoBitmap:
  723. return SFNT_Err_Invalid_Argument;
  724. }
  725. FT_LOCAL( FT_Error )
  726. tt_face_load_sbit_image( TT_Face face,
  727. FT_ULong strike_index,
  728. FT_UInt glyph_index,
  729. FT_UInt load_flags,
  730. FT_Stream stream,
  731. FT_Bitmap *map,
  732. TT_SBit_MetricsRec *metrics )
  733. {
  734. TT_SBitDecoderRec decoder[1];
  735. FT_Error error;
  736. FT_UNUSED( load_flags );
  737. FT_UNUSED( stream );
  738. FT_UNUSED( map );
  739. error = tt_sbit_decoder_init( decoder, face, strike_index, metrics );
  740. if ( !error )
  741. {
  742. error = tt_sbit_decoder_load_image( decoder, glyph_index, 0, 0 );
  743. tt_sbit_decoder_done( decoder );
  744. }
  745. return error;
  746. }
  747. /* EOF */