/src/freetype/src/pfr/pfrsbit.c

https://bitbucket.org/cabalistic/ogredeps/ · C · 698 lines · 496 code · 137 blank · 65 comment · 71 complexity · 99a0e9b0bea3b526f9f56c1805eddc1c MD5 · raw file

  1. /***************************************************************************/
  2. /* */
  3. /* pfrsbit.c */
  4. /* */
  5. /* FreeType PFR bitmap loader (body). */
  6. /* */
  7. /* Copyright 2002, 2003, 2006, 2009, 2010 by */
  8. /* David Turner, Robert Wilhelm, and Werner Lemberg. */
  9. /* */
  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. */
  15. /* */
  16. /***************************************************************************/
  17. #include "pfrsbit.h"
  18. #include "pfrload.h"
  19. #include FT_INTERNAL_DEBUG_H
  20. #include FT_INTERNAL_STREAM_H
  21. #include "pfrerror.h"
  22. #undef FT_COMPONENT
  23. #define FT_COMPONENT trace_pfr
  24. /*************************************************************************/
  25. /*************************************************************************/
  26. /***** *****/
  27. /***** PFR BIT WRITER *****/
  28. /***** *****/
  29. /*************************************************************************/
  30. /*************************************************************************/
  31. typedef struct PFR_BitWriter_
  32. {
  33. FT_Byte* line; /* current line start */
  34. FT_Int pitch; /* line size in bytes */
  35. FT_Int width; /* width in pixels/bits */
  36. FT_Int rows; /* number of remaining rows to scan */
  37. FT_Int total; /* total number of bits to draw */
  38. } PFR_BitWriterRec, *PFR_BitWriter;
  39. static void
  40. pfr_bitwriter_init( PFR_BitWriter writer,
  41. FT_Bitmap* target,
  42. FT_Bool decreasing )
  43. {
  44. writer->line = target->buffer;
  45. writer->pitch = target->pitch;
  46. writer->width = target->width;
  47. writer->rows = target->rows;
  48. writer->total = writer->width * writer->rows;
  49. if ( !decreasing )
  50. {
  51. writer->line += writer->pitch * ( target->rows-1 );
  52. writer->pitch = -writer->pitch;
  53. }
  54. }
  55. static void
  56. pfr_bitwriter_decode_bytes( PFR_BitWriter writer,
  57. FT_Byte* p,
  58. FT_Byte* limit )
  59. {
  60. FT_Int n, reload;
  61. FT_Int left = writer->width;
  62. FT_Byte* cur = writer->line;
  63. FT_UInt mask = 0x80;
  64. FT_UInt val = 0;
  65. FT_UInt c = 0;
  66. n = (FT_Int)( limit - p ) * 8;
  67. if ( n > writer->total )
  68. n = writer->total;
  69. reload = n & 7;
  70. for ( ; n > 0; n-- )
  71. {
  72. if ( ( n & 7 ) == reload )
  73. val = *p++;
  74. if ( val & 0x80 )
  75. c |= mask;
  76. val <<= 1;
  77. mask >>= 1;
  78. if ( --left <= 0 )
  79. {
  80. cur[0] = (FT_Byte)c;
  81. left = writer->width;
  82. mask = 0x80;
  83. writer->line += writer->pitch;
  84. cur = writer->line;
  85. c = 0;
  86. }
  87. else if ( mask == 0 )
  88. {
  89. cur[0] = (FT_Byte)c;
  90. mask = 0x80;
  91. c = 0;
  92. cur ++;
  93. }
  94. }
  95. if ( mask != 0x80 )
  96. cur[0] = (FT_Byte)c;
  97. }
  98. static void
  99. pfr_bitwriter_decode_rle1( PFR_BitWriter writer,
  100. FT_Byte* p,
  101. FT_Byte* limit )
  102. {
  103. FT_Int n, phase, count, counts[2], reload;
  104. FT_Int left = writer->width;
  105. FT_Byte* cur = writer->line;
  106. FT_UInt mask = 0x80;
  107. FT_UInt c = 0;
  108. n = writer->total;
  109. phase = 1;
  110. counts[0] = 0;
  111. counts[1] = 0;
  112. count = 0;
  113. reload = 1;
  114. for ( ; n > 0; n-- )
  115. {
  116. if ( reload )
  117. {
  118. do
  119. {
  120. if ( phase )
  121. {
  122. FT_Int v;
  123. if ( p >= limit )
  124. break;
  125. v = *p++;
  126. counts[0] = v >> 4;
  127. counts[1] = v & 15;
  128. phase = 0;
  129. count = counts[0];
  130. }
  131. else
  132. {
  133. phase = 1;
  134. count = counts[1];
  135. }
  136. } while ( count == 0 );
  137. }
  138. if ( phase )
  139. c |= mask;
  140. mask >>= 1;
  141. if ( --left <= 0 )
  142. {
  143. cur[0] = (FT_Byte) c;
  144. left = writer->width;
  145. mask = 0x80;
  146. writer->line += writer->pitch;
  147. cur = writer->line;
  148. c = 0;
  149. }
  150. else if ( mask == 0 )
  151. {
  152. cur[0] = (FT_Byte)c;
  153. mask = 0x80;
  154. c = 0;
  155. cur ++;
  156. }
  157. reload = ( --count <= 0 );
  158. }
  159. if ( mask != 0x80 )
  160. cur[0] = (FT_Byte) c;
  161. }
  162. static void
  163. pfr_bitwriter_decode_rle2( PFR_BitWriter writer,
  164. FT_Byte* p,
  165. FT_Byte* limit )
  166. {
  167. FT_Int n, phase, count, reload;
  168. FT_Int left = writer->width;
  169. FT_Byte* cur = writer->line;
  170. FT_UInt mask = 0x80;
  171. FT_UInt c = 0;
  172. n = writer->total;
  173. phase = 1;
  174. count = 0;
  175. reload = 1;
  176. for ( ; n > 0; n-- )
  177. {
  178. if ( reload )
  179. {
  180. do
  181. {
  182. if ( p >= limit )
  183. break;
  184. count = *p++;
  185. phase = phase ^ 1;
  186. } while ( count == 0 );
  187. }
  188. if ( phase )
  189. c |= mask;
  190. mask >>= 1;
  191. if ( --left <= 0 )
  192. {
  193. cur[0] = (FT_Byte) c;
  194. c = 0;
  195. mask = 0x80;
  196. left = writer->width;
  197. writer->line += writer->pitch;
  198. cur = writer->line;
  199. }
  200. else if ( mask == 0 )
  201. {
  202. cur[0] = (FT_Byte)c;
  203. c = 0;
  204. mask = 0x80;
  205. cur ++;
  206. }
  207. reload = ( --count <= 0 );
  208. }
  209. if ( mask != 0x80 )
  210. cur[0] = (FT_Byte) c;
  211. }
  212. /*************************************************************************/
  213. /*************************************************************************/
  214. /***** *****/
  215. /***** BITMAP DATA DECODING *****/
  216. /***** *****/
  217. /*************************************************************************/
  218. /*************************************************************************/
  219. static void
  220. pfr_lookup_bitmap_data( FT_Byte* base,
  221. FT_Byte* limit,
  222. FT_UInt count,
  223. FT_UInt flags,
  224. FT_UInt char_code,
  225. FT_ULong* found_offset,
  226. FT_ULong* found_size )
  227. {
  228. FT_UInt left, right, char_len;
  229. FT_Bool two = FT_BOOL( flags & 1 );
  230. FT_Byte* buff;
  231. char_len = 4;
  232. if ( two ) char_len += 1;
  233. if ( flags & 2 ) char_len += 1;
  234. if ( flags & 4 ) char_len += 1;
  235. left = 0;
  236. right = count;
  237. while ( left < right )
  238. {
  239. FT_UInt middle, code;
  240. middle = ( left + right ) >> 1;
  241. buff = base + middle * char_len;
  242. /* check that we are not outside of the table -- */
  243. /* this is possible with broken fonts... */
  244. if ( buff + char_len > limit )
  245. goto Fail;
  246. if ( two )
  247. code = PFR_NEXT_USHORT( buff );
  248. else
  249. code = PFR_NEXT_BYTE( buff );
  250. if ( code == char_code )
  251. goto Found_It;
  252. if ( code < char_code )
  253. left = middle;
  254. else
  255. right = middle;
  256. }
  257. Fail:
  258. /* Not found */
  259. *found_size = 0;
  260. *found_offset = 0;
  261. return;
  262. Found_It:
  263. if ( flags & 2 )
  264. *found_size = PFR_NEXT_USHORT( buff );
  265. else
  266. *found_size = PFR_NEXT_BYTE( buff );
  267. if ( flags & 4 )
  268. *found_offset = PFR_NEXT_ULONG( buff );
  269. else
  270. *found_offset = PFR_NEXT_USHORT( buff );
  271. }
  272. /* load bitmap metrics. "*padvance" must be set to the default value */
  273. /* before calling this function... */
  274. /* */
  275. static FT_Error
  276. pfr_load_bitmap_metrics( FT_Byte** pdata,
  277. FT_Byte* limit,
  278. FT_Long scaled_advance,
  279. FT_Long *axpos,
  280. FT_Long *aypos,
  281. FT_UInt *axsize,
  282. FT_UInt *aysize,
  283. FT_Long *aadvance,
  284. FT_UInt *aformat )
  285. {
  286. FT_Error error = PFR_Err_Ok;
  287. FT_Byte flags;
  288. FT_Char b;
  289. FT_Byte* p = *pdata;
  290. FT_Long xpos, ypos, advance;
  291. FT_UInt xsize, ysize;
  292. PFR_CHECK( 1 );
  293. flags = PFR_NEXT_BYTE( p );
  294. xpos = 0;
  295. ypos = 0;
  296. xsize = 0;
  297. ysize = 0;
  298. advance = 0;
  299. switch ( flags & 3 )
  300. {
  301. case 0:
  302. PFR_CHECK( 1 );
  303. b = PFR_NEXT_INT8( p );
  304. xpos = b >> 4;
  305. ypos = ( (FT_Char)( b << 4 ) ) >> 4;
  306. break;
  307. case 1:
  308. PFR_CHECK( 2 );
  309. xpos = PFR_NEXT_INT8( p );
  310. ypos = PFR_NEXT_INT8( p );
  311. break;
  312. case 2:
  313. PFR_CHECK( 4 );
  314. xpos = PFR_NEXT_SHORT( p );
  315. ypos = PFR_NEXT_SHORT( p );
  316. break;
  317. case 3:
  318. PFR_CHECK( 6 );
  319. xpos = PFR_NEXT_LONG( p );
  320. ypos = PFR_NEXT_LONG( p );
  321. break;
  322. default:
  323. ;
  324. }
  325. flags >>= 2;
  326. switch ( flags & 3 )
  327. {
  328. case 0:
  329. /* blank image */
  330. xsize = 0;
  331. ysize = 0;
  332. break;
  333. case 1:
  334. PFR_CHECK( 1 );
  335. b = PFR_NEXT_BYTE( p );
  336. xsize = ( b >> 4 ) & 0xF;
  337. ysize = b & 0xF;
  338. break;
  339. case 2:
  340. PFR_CHECK( 2 );
  341. xsize = PFR_NEXT_BYTE( p );
  342. ysize = PFR_NEXT_BYTE( p );
  343. break;
  344. case 3:
  345. PFR_CHECK( 4 );
  346. xsize = PFR_NEXT_USHORT( p );
  347. ysize = PFR_NEXT_USHORT( p );
  348. break;
  349. default:
  350. ;
  351. }
  352. flags >>= 2;
  353. switch ( flags & 3 )
  354. {
  355. case 0:
  356. advance = scaled_advance;
  357. break;
  358. case 1:
  359. PFR_CHECK( 1 );
  360. advance = PFR_NEXT_INT8( p ) << 8;
  361. break;
  362. case 2:
  363. PFR_CHECK( 2 );
  364. advance = PFR_NEXT_SHORT( p );
  365. break;
  366. case 3:
  367. PFR_CHECK( 3 );
  368. advance = PFR_NEXT_LONG( p );
  369. break;
  370. default:
  371. ;
  372. }
  373. *axpos = xpos;
  374. *aypos = ypos;
  375. *axsize = xsize;
  376. *aysize = ysize;
  377. *aadvance = advance;
  378. *aformat = flags >> 2;
  379. *pdata = p;
  380. Exit:
  381. return error;
  382. Too_Short:
  383. error = PFR_Err_Invalid_Table;
  384. FT_ERROR(( "pfr_load_bitmap_metrics: invalid glyph data\n" ));
  385. goto Exit;
  386. }
  387. static FT_Error
  388. pfr_load_bitmap_bits( FT_Byte* p,
  389. FT_Byte* limit,
  390. FT_UInt format,
  391. FT_Bool decreasing,
  392. FT_Bitmap* target )
  393. {
  394. FT_Error error = PFR_Err_Ok;
  395. PFR_BitWriterRec writer;
  396. if ( target->rows > 0 && target->width > 0 )
  397. {
  398. pfr_bitwriter_init( &writer, target, decreasing );
  399. switch ( format )
  400. {
  401. case 0: /* packed bits */
  402. pfr_bitwriter_decode_bytes( &writer, p, limit );
  403. break;
  404. case 1: /* RLE1 */
  405. pfr_bitwriter_decode_rle1( &writer, p, limit );
  406. break;
  407. case 2: /* RLE2 */
  408. pfr_bitwriter_decode_rle2( &writer, p, limit );
  409. break;
  410. default:
  411. FT_ERROR(( "pfr_read_bitmap_data: invalid image type\n" ));
  412. error = PFR_Err_Invalid_File_Format;
  413. }
  414. }
  415. return error;
  416. }
  417. /*************************************************************************/
  418. /*************************************************************************/
  419. /***** *****/
  420. /***** BITMAP LOADING *****/
  421. /***** *****/
  422. /*************************************************************************/
  423. /*************************************************************************/
  424. FT_LOCAL( FT_Error )
  425. pfr_slot_load_bitmap( PFR_Slot glyph,
  426. PFR_Size size,
  427. FT_UInt glyph_index )
  428. {
  429. FT_Error error;
  430. PFR_Face face = (PFR_Face) glyph->root.face;
  431. FT_Stream stream = face->root.stream;
  432. PFR_PhyFont phys = &face->phy_font;
  433. FT_ULong gps_offset;
  434. FT_ULong gps_size;
  435. PFR_Char character;
  436. PFR_Strike strike;
  437. character = &phys->chars[glyph_index];
  438. /* Look-up a bitmap strike corresponding to the current */
  439. /* character dimensions */
  440. {
  441. FT_UInt n;
  442. strike = phys->strikes;
  443. for ( n = 0; n < phys->num_strikes; n++ )
  444. {
  445. if ( strike->x_ppm == (FT_UInt)size->root.metrics.x_ppem &&
  446. strike->y_ppm == (FT_UInt)size->root.metrics.y_ppem )
  447. {
  448. goto Found_Strike;
  449. }
  450. strike++;
  451. }
  452. /* couldn't find it */
  453. return PFR_Err_Invalid_Argument;
  454. }
  455. Found_Strike:
  456. /* Now lookup the glyph's position within the file */
  457. {
  458. FT_UInt char_len;
  459. char_len = 4;
  460. if ( strike->flags & 1 ) char_len += 1;
  461. if ( strike->flags & 2 ) char_len += 1;
  462. if ( strike->flags & 4 ) char_len += 1;
  463. /* Access data directly in the frame to speed lookups */
  464. if ( FT_STREAM_SEEK( phys->bct_offset + strike->bct_offset ) ||
  465. FT_FRAME_ENTER( char_len * strike->num_bitmaps ) )
  466. goto Exit;
  467. pfr_lookup_bitmap_data( stream->cursor,
  468. stream->limit,
  469. strike->num_bitmaps,
  470. strike->flags,
  471. character->char_code,
  472. &gps_offset,
  473. &gps_size );
  474. FT_FRAME_EXIT();
  475. if ( gps_size == 0 )
  476. {
  477. /* Could not find a bitmap program string for this glyph */
  478. error = PFR_Err_Invalid_Argument;
  479. goto Exit;
  480. }
  481. }
  482. /* get the bitmap metrics */
  483. {
  484. FT_Long xpos = 0, ypos = 0, advance = 0;
  485. FT_UInt xsize = 0, ysize = 0, format = 0;
  486. FT_Byte* p;
  487. /* compute linear advance */
  488. advance = character->advance;
  489. if ( phys->metrics_resolution != phys->outline_resolution )
  490. advance = FT_MulDiv( advance,
  491. phys->outline_resolution,
  492. phys->metrics_resolution );
  493. glyph->root.linearHoriAdvance = advance;
  494. /* compute default advance, i.e., scaled advance. This can be */
  495. /* overridden in the bitmap header of certain glyphs. */
  496. advance = FT_MulDiv( (FT_Fixed)size->root.metrics.x_ppem << 8,
  497. character->advance,
  498. phys->metrics_resolution );
  499. if ( FT_STREAM_SEEK( face->header.gps_section_offset + gps_offset ) ||
  500. FT_FRAME_ENTER( gps_size ) )
  501. goto Exit;
  502. p = stream->cursor;
  503. error = pfr_load_bitmap_metrics( &p, stream->limit,
  504. advance,
  505. &xpos, &ypos,
  506. &xsize, &ysize,
  507. &advance, &format );
  508. /*
  509. * XXX: on 16bit system, we return an error for huge bitmap
  510. * which causes a size truncation, because truncated
  511. * size properties makes bitmap glyph broken.
  512. */
  513. if ( xpos > FT_INT_MAX || ( ypos + ysize ) > FT_INT_MAX )
  514. {
  515. FT_TRACE1(( "pfr_slot_load_bitmap:" ));
  516. FT_TRACE1(( "huge bitmap glyph %dx%d over FT_GlyphSlot\n",
  517. xpos, ypos ));
  518. error = PFR_Err_Invalid_Pixel_Size;
  519. }
  520. if ( !error )
  521. {
  522. glyph->root.format = FT_GLYPH_FORMAT_BITMAP;
  523. /* Set up glyph bitmap and metrics */
  524. /* XXX: needs casts to fit FT_Bitmap.{width|rows|pitch} */
  525. glyph->root.bitmap.width = (FT_Int)xsize;
  526. glyph->root.bitmap.rows = (FT_Int)ysize;
  527. glyph->root.bitmap.pitch = (FT_Int)( xsize + 7 ) >> 3;
  528. glyph->root.bitmap.pixel_mode = FT_PIXEL_MODE_MONO;
  529. /* XXX: needs casts to fit FT_Glyph_Metrics.{width|height} */
  530. glyph->root.metrics.width = (FT_Pos)xsize << 6;
  531. glyph->root.metrics.height = (FT_Pos)ysize << 6;
  532. glyph->root.metrics.horiBearingX = xpos << 6;
  533. glyph->root.metrics.horiBearingY = ypos << 6;
  534. glyph->root.metrics.horiAdvance = FT_PIX_ROUND( ( advance >> 2 ) );
  535. glyph->root.metrics.vertBearingX = - glyph->root.metrics.width >> 1;
  536. glyph->root.metrics.vertBearingY = 0;
  537. glyph->root.metrics.vertAdvance = size->root.metrics.height;
  538. /* XXX: needs casts fit FT_GlyphSlotRec.bitmap_{left|top} */
  539. glyph->root.bitmap_left = (FT_Int)xpos;
  540. glyph->root.bitmap_top = (FT_Int)(ypos + ysize);
  541. /* Allocate and read bitmap data */
  542. {
  543. FT_ULong len = glyph->root.bitmap.pitch * ysize;
  544. error = ft_glyphslot_alloc_bitmap( &glyph->root, len );
  545. if ( !error )
  546. {
  547. error = pfr_load_bitmap_bits(
  548. p,
  549. stream->limit,
  550. format,
  551. FT_BOOL(face->header.color_flags & 2),
  552. &glyph->root.bitmap );
  553. }
  554. }
  555. }
  556. FT_FRAME_EXIT();
  557. }
  558. Exit:
  559. return error;
  560. }
  561. /* END */