pfrsbit.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670
  1. /***************************************************************************/
  2. /* */
  3. /* pfrsbit.c */
  4. /* */
  5. /* FreeType PFR bitmap loader (body). */
  6. /* */
  7. /* Copyright 2002 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] = c;
  90. mask = 0x80;
  91. c = 0;
  92. cur ++;
  93. }
  94. }
  95. if ( mask != 0x80 )
  96. cur[0] = 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] = 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] = 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_Int count,
  223. FT_Byte 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 = 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 = 0;
  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_UInt decreasing,
  392. FT_Bitmap* target )
  393. {
  394. FT_Error error = 0;
  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 = FT_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 FT_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 = FT_Err_Invalid_Argument;
  479. goto Exit;
  480. }
  481. }
  482. /* get the bitmap metrics */
  483. {
  484. FT_Long xpos, ypos, advance;
  485. FT_UInt xsize, ysize, format;
  486. FT_Byte* p;
  487. advance = FT_MulDiv( size->root.metrics.x_ppem << 8,
  488. character->advance,
  489. phys->metrics_resolution );
  490. /* XXX: handle linearHoriAdvance correctly! */
  491. if ( FT_STREAM_SEEK( face->header.gps_section_offset + gps_offset ) ||
  492. FT_FRAME_ENTER( gps_size ) )
  493. goto Exit;
  494. p = stream->cursor;
  495. error = pfr_load_bitmap_metrics( &p, stream->limit,
  496. advance,
  497. &xpos, &ypos,
  498. &xsize, &ysize,
  499. &advance, &format );
  500. if ( !error )
  501. {
  502. glyph->root.format = FT_GLYPH_FORMAT_BITMAP;
  503. /* Set up glyph bitmap and metrics */
  504. glyph->root.bitmap.width = (FT_Int)xsize;
  505. glyph->root.bitmap.rows = (FT_Int)ysize;
  506. glyph->root.bitmap.pitch = (FT_Long)( xsize + 7 ) >> 3;
  507. glyph->root.bitmap.pixel_mode = FT_PIXEL_MODE_MONO;
  508. glyph->root.metrics.width = (FT_Long)xsize << 6;
  509. glyph->root.metrics.height = (FT_Long)ysize << 6;
  510. glyph->root.metrics.horiBearingX = xpos << 6;
  511. glyph->root.metrics.horiBearingY = ypos << 6;
  512. glyph->root.metrics.horiAdvance = ( ( advance >> 2 ) + 32 ) & -64;
  513. glyph->root.metrics.vertBearingX = - glyph->root.metrics.width >> 1;
  514. glyph->root.metrics.vertBearingY = 0;
  515. glyph->root.metrics.vertAdvance = size->root.metrics.height;
  516. glyph->root.bitmap_left = xpos;
  517. glyph->root.bitmap_top = ypos + ysize;
  518. /* Allocate and read bitmap data */
  519. {
  520. FT_Memory memory = face->root.memory;
  521. FT_Long len = glyph->root.bitmap.pitch * ysize;
  522. if ( !FT_ALLOC( glyph->root.bitmap.buffer, len ) )
  523. {
  524. error = pfr_load_bitmap_bits( p,
  525. stream->limit,
  526. format,
  527. face->header.color_flags & 2,
  528. &glyph->root.bitmap );
  529. }
  530. }
  531. }
  532. FT_FRAME_EXIT();
  533. }
  534. Exit:
  535. return error;
  536. }
  537. /* END */