/modules/freetype2/src/pshinter/pshrec.c

http://github.com/zpao/v8monkey · C · 1224 lines · 828 code · 260 blank · 136 comment · 109 complexity · 90e4b2dedf2ad92e4001c4c6ade52d3b MD5 · raw file

  1. /***************************************************************************/
  2. /* */
  3. /* pshrec.c */
  4. /* */
  5. /* FreeType PostScript hints recorder (body). */
  6. /* */
  7. /* Copyright 2001, 2002, 2003, 2004, 2007, 2009 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 <ft2build.h>
  18. #include FT_FREETYPE_H
  19. #include FT_INTERNAL_OBJECTS_H
  20. #include FT_INTERNAL_DEBUG_H
  21. #include FT_INTERNAL_CALC_H
  22. #include "pshrec.h"
  23. #include "pshalgo.h"
  24. #include "pshnterr.h"
  25. #undef FT_COMPONENT
  26. #define FT_COMPONENT trace_pshrec
  27. #ifdef DEBUG_HINTER
  28. PS_Hints ps_debug_hints = 0;
  29. int ps_debug_no_horz_hints = 0;
  30. int ps_debug_no_vert_hints = 0;
  31. #endif
  32. /*************************************************************************/
  33. /*************************************************************************/
  34. /***** *****/
  35. /***** PS_HINT MANAGEMENT *****/
  36. /***** *****/
  37. /*************************************************************************/
  38. /*************************************************************************/
  39. /* destroy hints table */
  40. static void
  41. ps_hint_table_done( PS_Hint_Table table,
  42. FT_Memory memory )
  43. {
  44. FT_FREE( table->hints );
  45. table->num_hints = 0;
  46. table->max_hints = 0;
  47. }
  48. /* ensure that a table can contain "count" elements */
  49. static FT_Error
  50. ps_hint_table_ensure( PS_Hint_Table table,
  51. FT_UInt count,
  52. FT_Memory memory )
  53. {
  54. FT_UInt old_max = table->max_hints;
  55. FT_UInt new_max = count;
  56. FT_Error error = PSH_Err_Ok;
  57. if ( new_max > old_max )
  58. {
  59. /* try to grow the table */
  60. new_max = FT_PAD_CEIL( new_max, 8 );
  61. if ( !FT_RENEW_ARRAY( table->hints, old_max, new_max ) )
  62. table->max_hints = new_max;
  63. }
  64. return error;
  65. }
  66. static FT_Error
  67. ps_hint_table_alloc( PS_Hint_Table table,
  68. FT_Memory memory,
  69. PS_Hint *ahint )
  70. {
  71. FT_Error error = PSH_Err_Ok;
  72. FT_UInt count;
  73. PS_Hint hint = 0;
  74. count = table->num_hints;
  75. count++;
  76. if ( count >= table->max_hints )
  77. {
  78. error = ps_hint_table_ensure( table, count, memory );
  79. if ( error )
  80. goto Exit;
  81. }
  82. hint = table->hints + count - 1;
  83. hint->pos = 0;
  84. hint->len = 0;
  85. hint->flags = 0;
  86. table->num_hints = count;
  87. Exit:
  88. *ahint = hint;
  89. return error;
  90. }
  91. /*************************************************************************/
  92. /*************************************************************************/
  93. /***** *****/
  94. /***** PS_MASK MANAGEMENT *****/
  95. /***** *****/
  96. /*************************************************************************/
  97. /*************************************************************************/
  98. /* destroy mask */
  99. static void
  100. ps_mask_done( PS_Mask mask,
  101. FT_Memory memory )
  102. {
  103. FT_FREE( mask->bytes );
  104. mask->num_bits = 0;
  105. mask->max_bits = 0;
  106. mask->end_point = 0;
  107. }
  108. /* ensure that a mask can contain "count" bits */
  109. static FT_Error
  110. ps_mask_ensure( PS_Mask mask,
  111. FT_UInt count,
  112. FT_Memory memory )
  113. {
  114. FT_UInt old_max = ( mask->max_bits + 7 ) >> 3;
  115. FT_UInt new_max = ( count + 7 ) >> 3;
  116. FT_Error error = PSH_Err_Ok;
  117. if ( new_max > old_max )
  118. {
  119. new_max = FT_PAD_CEIL( new_max, 8 );
  120. if ( !FT_RENEW_ARRAY( mask->bytes, old_max, new_max ) )
  121. mask->max_bits = new_max * 8;
  122. }
  123. return error;
  124. }
  125. /* test a bit value in a given mask */
  126. static FT_Int
  127. ps_mask_test_bit( PS_Mask mask,
  128. FT_Int idx )
  129. {
  130. if ( (FT_UInt)idx >= mask->num_bits )
  131. return 0;
  132. return mask->bytes[idx >> 3] & ( 0x80 >> ( idx & 7 ) );
  133. }
  134. /* clear a given bit */
  135. static void
  136. ps_mask_clear_bit( PS_Mask mask,
  137. FT_Int idx )
  138. {
  139. FT_Byte* p;
  140. if ( (FT_UInt)idx >= mask->num_bits )
  141. return;
  142. p = mask->bytes + ( idx >> 3 );
  143. p[0] = (FT_Byte)( p[0] & ~( 0x80 >> ( idx & 7 ) ) );
  144. }
  145. /* set a given bit, possibly grow the mask */
  146. static FT_Error
  147. ps_mask_set_bit( PS_Mask mask,
  148. FT_Int idx,
  149. FT_Memory memory )
  150. {
  151. FT_Error error = PSH_Err_Ok;
  152. FT_Byte* p;
  153. if ( idx < 0 )
  154. goto Exit;
  155. if ( (FT_UInt)idx >= mask->num_bits )
  156. {
  157. error = ps_mask_ensure( mask, idx + 1, memory );
  158. if ( error )
  159. goto Exit;
  160. mask->num_bits = idx + 1;
  161. }
  162. p = mask->bytes + ( idx >> 3 );
  163. p[0] = (FT_Byte)( p[0] | ( 0x80 >> ( idx & 7 ) ) );
  164. Exit:
  165. return error;
  166. }
  167. /* destroy mask table */
  168. static void
  169. ps_mask_table_done( PS_Mask_Table table,
  170. FT_Memory memory )
  171. {
  172. FT_UInt count = table->max_masks;
  173. PS_Mask mask = table->masks;
  174. for ( ; count > 0; count--, mask++ )
  175. ps_mask_done( mask, memory );
  176. FT_FREE( table->masks );
  177. table->num_masks = 0;
  178. table->max_masks = 0;
  179. }
  180. /* ensure that a mask table can contain "count" masks */
  181. static FT_Error
  182. ps_mask_table_ensure( PS_Mask_Table table,
  183. FT_UInt count,
  184. FT_Memory memory )
  185. {
  186. FT_UInt old_max = table->max_masks;
  187. FT_UInt new_max = count;
  188. FT_Error error = PSH_Err_Ok;
  189. if ( new_max > old_max )
  190. {
  191. new_max = FT_PAD_CEIL( new_max, 8 );
  192. if ( !FT_RENEW_ARRAY( table->masks, old_max, new_max ) )
  193. table->max_masks = new_max;
  194. }
  195. return error;
  196. }
  197. /* allocate a new mask in a table */
  198. static FT_Error
  199. ps_mask_table_alloc( PS_Mask_Table table,
  200. FT_Memory memory,
  201. PS_Mask *amask )
  202. {
  203. FT_UInt count;
  204. FT_Error error = PSH_Err_Ok;
  205. PS_Mask mask = 0;
  206. count = table->num_masks;
  207. count++;
  208. if ( count > table->max_masks )
  209. {
  210. error = ps_mask_table_ensure( table, count, memory );
  211. if ( error )
  212. goto Exit;
  213. }
  214. mask = table->masks + count - 1;
  215. mask->num_bits = 0;
  216. mask->end_point = 0;
  217. table->num_masks = count;
  218. Exit:
  219. *amask = mask;
  220. return error;
  221. }
  222. /* return last hint mask in a table, create one if the table is empty */
  223. static FT_Error
  224. ps_mask_table_last( PS_Mask_Table table,
  225. FT_Memory memory,
  226. PS_Mask *amask )
  227. {
  228. FT_Error error = PSH_Err_Ok;
  229. FT_UInt count;
  230. PS_Mask mask;
  231. count = table->num_masks;
  232. if ( count == 0 )
  233. {
  234. error = ps_mask_table_alloc( table, memory, &mask );
  235. if ( error )
  236. goto Exit;
  237. }
  238. else
  239. mask = table->masks + count - 1;
  240. Exit:
  241. *amask = mask;
  242. return error;
  243. }
  244. /* set a new mask to a given bit range */
  245. static FT_Error
  246. ps_mask_table_set_bits( PS_Mask_Table table,
  247. const FT_Byte* source,
  248. FT_UInt bit_pos,
  249. FT_UInt bit_count,
  250. FT_Memory memory )
  251. {
  252. FT_Error error = PSH_Err_Ok;
  253. PS_Mask mask;
  254. error = ps_mask_table_last( table, memory, &mask );
  255. if ( error )
  256. goto Exit;
  257. error = ps_mask_ensure( mask, bit_count, memory );
  258. if ( error )
  259. goto Exit;
  260. mask->num_bits = bit_count;
  261. /* now, copy bits */
  262. {
  263. FT_Byte* read = (FT_Byte*)source + ( bit_pos >> 3 );
  264. FT_Int rmask = 0x80 >> ( bit_pos & 7 );
  265. FT_Byte* write = mask->bytes;
  266. FT_Int wmask = 0x80;
  267. FT_Int val;
  268. for ( ; bit_count > 0; bit_count-- )
  269. {
  270. val = write[0] & ~wmask;
  271. if ( read[0] & rmask )
  272. val |= wmask;
  273. write[0] = (FT_Byte)val;
  274. rmask >>= 1;
  275. if ( rmask == 0 )
  276. {
  277. read++;
  278. rmask = 0x80;
  279. }
  280. wmask >>= 1;
  281. if ( wmask == 0 )
  282. {
  283. write++;
  284. wmask = 0x80;
  285. }
  286. }
  287. }
  288. Exit:
  289. return error;
  290. }
  291. /* test whether two masks in a table intersect */
  292. static FT_Int
  293. ps_mask_table_test_intersect( PS_Mask_Table table,
  294. FT_Int index1,
  295. FT_Int index2 )
  296. {
  297. PS_Mask mask1 = table->masks + index1;
  298. PS_Mask mask2 = table->masks + index2;
  299. FT_Byte* p1 = mask1->bytes;
  300. FT_Byte* p2 = mask2->bytes;
  301. FT_UInt count1 = mask1->num_bits;
  302. FT_UInt count2 = mask2->num_bits;
  303. FT_UInt count;
  304. count = ( count1 <= count2 ) ? count1 : count2;
  305. for ( ; count >= 8; count -= 8 )
  306. {
  307. if ( p1[0] & p2[0] )
  308. return 1;
  309. p1++;
  310. p2++;
  311. }
  312. if ( count == 0 )
  313. return 0;
  314. return ( p1[0] & p2[0] ) & ~( 0xFF >> count );
  315. }
  316. /* merge two masks, used by ps_mask_table_merge_all */
  317. static FT_Error
  318. ps_mask_table_merge( PS_Mask_Table table,
  319. FT_Int index1,
  320. FT_Int index2,
  321. FT_Memory memory )
  322. {
  323. FT_UInt temp;
  324. FT_Error error = PSH_Err_Ok;
  325. /* swap index1 and index2 so that index1 < index2 */
  326. if ( index1 > index2 )
  327. {
  328. temp = index1;
  329. index1 = index2;
  330. index2 = temp;
  331. }
  332. if ( index1 < index2 && index1 >= 0 && index2 < (FT_Int)table->num_masks )
  333. {
  334. /* we need to merge the bitsets of index1 and index2 with a */
  335. /* simple union */
  336. PS_Mask mask1 = table->masks + index1;
  337. PS_Mask mask2 = table->masks + index2;
  338. FT_UInt count1 = mask1->num_bits;
  339. FT_UInt count2 = mask2->num_bits;
  340. FT_Int delta;
  341. if ( count2 > 0 )
  342. {
  343. FT_UInt pos;
  344. FT_Byte* read;
  345. FT_Byte* write;
  346. /* if "count2" is greater than "count1", we need to grow the */
  347. /* first bitset, and clear the highest bits */
  348. if ( count2 > count1 )
  349. {
  350. error = ps_mask_ensure( mask1, count2, memory );
  351. if ( error )
  352. goto Exit;
  353. for ( pos = count1; pos < count2; pos++ )
  354. ps_mask_clear_bit( mask1, pos );
  355. }
  356. /* merge (unite) the bitsets */
  357. read = mask2->bytes;
  358. write = mask1->bytes;
  359. pos = (FT_UInt)( ( count2 + 7 ) >> 3 );
  360. for ( ; pos > 0; pos-- )
  361. {
  362. write[0] = (FT_Byte)( write[0] | read[0] );
  363. write++;
  364. read++;
  365. }
  366. }
  367. /* Now, remove "mask2" from the list. We need to keep the masks */
  368. /* sorted in order of importance, so move table elements. */
  369. mask2->num_bits = 0;
  370. mask2->end_point = 0;
  371. delta = table->num_masks - 1 - index2; /* number of masks to move */
  372. if ( delta > 0 )
  373. {
  374. /* move to end of table for reuse */
  375. PS_MaskRec dummy = *mask2;
  376. ft_memmove( mask2, mask2 + 1, delta * sizeof ( PS_MaskRec ) );
  377. mask2[delta] = dummy;
  378. }
  379. table->num_masks--;
  380. }
  381. else
  382. FT_TRACE0(( "ps_mask_table_merge: ignoring invalid indices (%d,%d)\n",
  383. index1, index2 ));
  384. Exit:
  385. return error;
  386. }
  387. /* Try to merge all masks in a given table. This is used to merge */
  388. /* all counter masks into independent counter "paths". */
  389. /* */
  390. static FT_Error
  391. ps_mask_table_merge_all( PS_Mask_Table table,
  392. FT_Memory memory )
  393. {
  394. FT_Int index1, index2;
  395. FT_Error error = PSH_Err_Ok;
  396. for ( index1 = table->num_masks - 1; index1 > 0; index1-- )
  397. {
  398. for ( index2 = index1 - 1; index2 >= 0; index2-- )
  399. {
  400. if ( ps_mask_table_test_intersect( table, index1, index2 ) )
  401. {
  402. error = ps_mask_table_merge( table, index2, index1, memory );
  403. if ( error )
  404. goto Exit;
  405. break;
  406. }
  407. }
  408. }
  409. Exit:
  410. return error;
  411. }
  412. /*************************************************************************/
  413. /*************************************************************************/
  414. /***** *****/
  415. /***** PS_DIMENSION MANAGEMENT *****/
  416. /***** *****/
  417. /*************************************************************************/
  418. /*************************************************************************/
  419. /* finalize a given dimension */
  420. static void
  421. ps_dimension_done( PS_Dimension dimension,
  422. FT_Memory memory )
  423. {
  424. ps_mask_table_done( &dimension->counters, memory );
  425. ps_mask_table_done( &dimension->masks, memory );
  426. ps_hint_table_done( &dimension->hints, memory );
  427. }
  428. /* initialize a given dimension */
  429. static void
  430. ps_dimension_init( PS_Dimension dimension )
  431. {
  432. dimension->hints.num_hints = 0;
  433. dimension->masks.num_masks = 0;
  434. dimension->counters.num_masks = 0;
  435. }
  436. #if 0
  437. /* set a bit at a given index in the current hint mask */
  438. static FT_Error
  439. ps_dimension_set_mask_bit( PS_Dimension dim,
  440. FT_UInt idx,
  441. FT_Memory memory )
  442. {
  443. PS_Mask mask;
  444. FT_Error error = PSH_Err_Ok;
  445. /* get last hint mask */
  446. error = ps_mask_table_last( &dim->masks, memory, &mask );
  447. if ( error )
  448. goto Exit;
  449. error = ps_mask_set_bit( mask, idx, memory );
  450. Exit:
  451. return error;
  452. }
  453. #endif
  454. /* set the end point in a mask, called from "End" & "Reset" methods */
  455. static void
  456. ps_dimension_end_mask( PS_Dimension dim,
  457. FT_UInt end_point )
  458. {
  459. FT_UInt count = dim->masks.num_masks;
  460. PS_Mask mask;
  461. if ( count > 0 )
  462. {
  463. mask = dim->masks.masks + count - 1;
  464. mask->end_point = end_point;
  465. }
  466. }
  467. /* set the end point in the current mask, then create a new empty one */
  468. /* (called by "Reset" method) */
  469. static FT_Error
  470. ps_dimension_reset_mask( PS_Dimension dim,
  471. FT_UInt end_point,
  472. FT_Memory memory )
  473. {
  474. PS_Mask mask;
  475. /* end current mask */
  476. ps_dimension_end_mask( dim, end_point );
  477. /* allocate new one */
  478. return ps_mask_table_alloc( &dim->masks, memory, &mask );
  479. }
  480. /* set a new mask, called from the "T2Stem" method */
  481. static FT_Error
  482. ps_dimension_set_mask_bits( PS_Dimension dim,
  483. const FT_Byte* source,
  484. FT_UInt source_pos,
  485. FT_UInt source_bits,
  486. FT_UInt end_point,
  487. FT_Memory memory )
  488. {
  489. FT_Error error = PSH_Err_Ok;
  490. /* reset current mask, if any */
  491. error = ps_dimension_reset_mask( dim, end_point, memory );
  492. if ( error )
  493. goto Exit;
  494. /* set bits in new mask */
  495. error = ps_mask_table_set_bits( &dim->masks, source,
  496. source_pos, source_bits, memory );
  497. Exit:
  498. return error;
  499. }
  500. /* add a new single stem (called from "T1Stem" method) */
  501. static FT_Error
  502. ps_dimension_add_t1stem( PS_Dimension dim,
  503. FT_Int pos,
  504. FT_Int len,
  505. FT_Memory memory,
  506. FT_Int *aindex )
  507. {
  508. FT_Error error = PSH_Err_Ok;
  509. FT_UInt flags = 0;
  510. /* detect ghost stem */
  511. if ( len < 0 )
  512. {
  513. flags |= PS_HINT_FLAG_GHOST;
  514. if ( len == -21 )
  515. {
  516. flags |= PS_HINT_FLAG_BOTTOM;
  517. pos += len;
  518. }
  519. len = 0;
  520. }
  521. if ( aindex )
  522. *aindex = -1;
  523. /* now, lookup stem in the current hints table */
  524. {
  525. PS_Mask mask;
  526. FT_UInt idx;
  527. FT_UInt max = dim->hints.num_hints;
  528. PS_Hint hint = dim->hints.hints;
  529. for ( idx = 0; idx < max; idx++, hint++ )
  530. {
  531. if ( hint->pos == pos && hint->len == len )
  532. break;
  533. }
  534. /* we need to create a new hint in the table */
  535. if ( idx >= max )
  536. {
  537. error = ps_hint_table_alloc( &dim->hints, memory, &hint );
  538. if ( error )
  539. goto Exit;
  540. hint->pos = pos;
  541. hint->len = len;
  542. hint->flags = flags;
  543. }
  544. /* now, store the hint in the current mask */
  545. error = ps_mask_table_last( &dim->masks, memory, &mask );
  546. if ( error )
  547. goto Exit;
  548. error = ps_mask_set_bit( mask, idx, memory );
  549. if ( error )
  550. goto Exit;
  551. if ( aindex )
  552. *aindex = (FT_Int)idx;
  553. }
  554. Exit:
  555. return error;
  556. }
  557. /* add a "hstem3/vstem3" counter to our dimension table */
  558. static FT_Error
  559. ps_dimension_add_counter( PS_Dimension dim,
  560. FT_Int hint1,
  561. FT_Int hint2,
  562. FT_Int hint3,
  563. FT_Memory memory )
  564. {
  565. FT_Error error = PSH_Err_Ok;
  566. FT_UInt count = dim->counters.num_masks;
  567. PS_Mask counter = dim->counters.masks;
  568. /* try to find an existing counter mask that already uses */
  569. /* one of these stems here */
  570. for ( ; count > 0; count--, counter++ )
  571. {
  572. if ( ps_mask_test_bit( counter, hint1 ) ||
  573. ps_mask_test_bit( counter, hint2 ) ||
  574. ps_mask_test_bit( counter, hint3 ) )
  575. break;
  576. }
  577. /* create a new counter when needed */
  578. if ( count == 0 )
  579. {
  580. error = ps_mask_table_alloc( &dim->counters, memory, &counter );
  581. if ( error )
  582. goto Exit;
  583. }
  584. /* now, set the bits for our hints in the counter mask */
  585. error = ps_mask_set_bit( counter, hint1, memory );
  586. if ( error )
  587. goto Exit;
  588. error = ps_mask_set_bit( counter, hint2, memory );
  589. if ( error )
  590. goto Exit;
  591. error = ps_mask_set_bit( counter, hint3, memory );
  592. if ( error )
  593. goto Exit;
  594. Exit:
  595. return error;
  596. }
  597. /* end of recording session for a given dimension */
  598. static FT_Error
  599. ps_dimension_end( PS_Dimension dim,
  600. FT_UInt end_point,
  601. FT_Memory memory )
  602. {
  603. /* end hint mask table */
  604. ps_dimension_end_mask( dim, end_point );
  605. /* merge all counter masks into independent "paths" */
  606. return ps_mask_table_merge_all( &dim->counters, memory );
  607. }
  608. /*************************************************************************/
  609. /*************************************************************************/
  610. /***** *****/
  611. /***** PS_RECORDER MANAGEMENT *****/
  612. /***** *****/
  613. /*************************************************************************/
  614. /*************************************************************************/
  615. /* destroy hints */
  616. FT_LOCAL( void )
  617. ps_hints_done( PS_Hints hints )
  618. {
  619. FT_Memory memory = hints->memory;
  620. ps_dimension_done( &hints->dimension[0], memory );
  621. ps_dimension_done( &hints->dimension[1], memory );
  622. hints->error = PSH_Err_Ok;
  623. hints->memory = 0;
  624. }
  625. FT_LOCAL( FT_Error )
  626. ps_hints_init( PS_Hints hints,
  627. FT_Memory memory )
  628. {
  629. FT_MEM_ZERO( hints, sizeof ( *hints ) );
  630. hints->memory = memory;
  631. return PSH_Err_Ok;
  632. }
  633. /* initialize a hints for a new session */
  634. static void
  635. ps_hints_open( PS_Hints hints,
  636. PS_Hint_Type hint_type )
  637. {
  638. switch ( hint_type )
  639. {
  640. case PS_HINT_TYPE_1:
  641. case PS_HINT_TYPE_2:
  642. hints->error = PSH_Err_Ok;
  643. hints->hint_type = hint_type;
  644. ps_dimension_init( &hints->dimension[0] );
  645. ps_dimension_init( &hints->dimension[1] );
  646. break;
  647. default:
  648. hints->error = PSH_Err_Invalid_Argument;
  649. hints->hint_type = hint_type;
  650. FT_TRACE0(( "ps_hints_open: invalid charstring type\n" ));
  651. break;
  652. }
  653. }
  654. /* add one or more stems to the current hints table */
  655. static void
  656. ps_hints_stem( PS_Hints hints,
  657. FT_Int dimension,
  658. FT_UInt count,
  659. FT_Long* stems )
  660. {
  661. if ( !hints->error )
  662. {
  663. /* limit "dimension" to 0..1 */
  664. if ( dimension < 0 || dimension > 1 )
  665. {
  666. FT_TRACE0(( "ps_hints_stem: invalid dimension (%d) used\n",
  667. dimension ));
  668. dimension = ( dimension != 0 );
  669. }
  670. /* record the stems in the current hints/masks table */
  671. switch ( hints->hint_type )
  672. {
  673. case PS_HINT_TYPE_1: /* Type 1 "hstem" or "vstem" operator */
  674. case PS_HINT_TYPE_2: /* Type 2 "hstem" or "vstem" operator */
  675. {
  676. PS_Dimension dim = &hints->dimension[dimension];
  677. for ( ; count > 0; count--, stems += 2 )
  678. {
  679. FT_Error error;
  680. FT_Memory memory = hints->memory;
  681. error = ps_dimension_add_t1stem(
  682. dim, (FT_Int)stems[0], (FT_Int)stems[1],
  683. memory, NULL );
  684. if ( error )
  685. {
  686. FT_ERROR(( "ps_hints_stem: could not add stem"
  687. " (%d,%d) to hints table\n", stems[0], stems[1] ));
  688. hints->error = error;
  689. return;
  690. }
  691. }
  692. break;
  693. }
  694. default:
  695. FT_TRACE0(( "ps_hints_stem: called with invalid hint type (%d)\n",
  696. hints->hint_type ));
  697. break;
  698. }
  699. }
  700. }
  701. /* add one Type1 counter stem to the current hints table */
  702. static void
  703. ps_hints_t1stem3( PS_Hints hints,
  704. FT_Int dimension,
  705. FT_Fixed* stems )
  706. {
  707. FT_Error error = PSH_Err_Ok;
  708. if ( !hints->error )
  709. {
  710. PS_Dimension dim;
  711. FT_Memory memory = hints->memory;
  712. FT_Int count;
  713. FT_Int idx[3];
  714. /* limit "dimension" to 0..1 */
  715. if ( dimension < 0 || dimension > 1 )
  716. {
  717. FT_TRACE0(( "ps_hints_t1stem3: invalid dimension (%d) used\n",
  718. dimension ));
  719. dimension = ( dimension != 0 );
  720. }
  721. dim = &hints->dimension[dimension];
  722. /* there must be 6 elements in the 'stem' array */
  723. if ( hints->hint_type == PS_HINT_TYPE_1 )
  724. {
  725. /* add the three stems to our hints/masks table */
  726. for ( count = 0; count < 3; count++, stems += 2 )
  727. {
  728. error = ps_dimension_add_t1stem( dim,
  729. (FT_Int)FIXED_TO_INT( stems[0] ),
  730. (FT_Int)FIXED_TO_INT( stems[1] ),
  731. memory, &idx[count] );
  732. if ( error )
  733. goto Fail;
  734. }
  735. /* now, add the hints to the counters table */
  736. error = ps_dimension_add_counter( dim, idx[0], idx[1], idx[2],
  737. memory );
  738. if ( error )
  739. goto Fail;
  740. }
  741. else
  742. {
  743. FT_ERROR(( "ps_hints_t1stem3: called with invalid hint type\n" ));
  744. error = PSH_Err_Invalid_Argument;
  745. goto Fail;
  746. }
  747. }
  748. return;
  749. Fail:
  750. FT_ERROR(( "ps_hints_t1stem3: could not add counter stems to table\n" ));
  751. hints->error = error;
  752. }
  753. /* reset hints (only with Type 1 hints) */
  754. static void
  755. ps_hints_t1reset( PS_Hints hints,
  756. FT_UInt end_point )
  757. {
  758. FT_Error error = PSH_Err_Ok;
  759. if ( !hints->error )
  760. {
  761. FT_Memory memory = hints->memory;
  762. if ( hints->hint_type == PS_HINT_TYPE_1 )
  763. {
  764. error = ps_dimension_reset_mask( &hints->dimension[0],
  765. end_point, memory );
  766. if ( error )
  767. goto Fail;
  768. error = ps_dimension_reset_mask( &hints->dimension[1],
  769. end_point, memory );
  770. if ( error )
  771. goto Fail;
  772. }
  773. else
  774. {
  775. /* invalid hint type */
  776. error = PSH_Err_Invalid_Argument;
  777. goto Fail;
  778. }
  779. }
  780. return;
  781. Fail:
  782. hints->error = error;
  783. }
  784. /* Type2 "hintmask" operator, add a new hintmask to each direction */
  785. static void
  786. ps_hints_t2mask( PS_Hints hints,
  787. FT_UInt end_point,
  788. FT_UInt bit_count,
  789. const FT_Byte* bytes )
  790. {
  791. FT_Error error;
  792. if ( !hints->error )
  793. {
  794. PS_Dimension dim = hints->dimension;
  795. FT_Memory memory = hints->memory;
  796. FT_UInt count1 = dim[0].hints.num_hints;
  797. FT_UInt count2 = dim[1].hints.num_hints;
  798. /* check bit count; must be equal to current total hint count */
  799. if ( bit_count != count1 + count2 )
  800. {
  801. FT_TRACE0(( "ps_hints_t2mask:"
  802. " called with invalid bitcount %d (instead of %d)\n",
  803. bit_count, count1 + count2 ));
  804. /* simply ignore the operator */
  805. return;
  806. }
  807. /* set-up new horizontal and vertical hint mask now */
  808. error = ps_dimension_set_mask_bits( &dim[0], bytes, count2, count1,
  809. end_point, memory );
  810. if ( error )
  811. goto Fail;
  812. error = ps_dimension_set_mask_bits( &dim[1], bytes, 0, count2,
  813. end_point, memory );
  814. if ( error )
  815. goto Fail;
  816. }
  817. return;
  818. Fail:
  819. hints->error = error;
  820. }
  821. static void
  822. ps_hints_t2counter( PS_Hints hints,
  823. FT_UInt bit_count,
  824. const FT_Byte* bytes )
  825. {
  826. FT_Error error;
  827. if ( !hints->error )
  828. {
  829. PS_Dimension dim = hints->dimension;
  830. FT_Memory memory = hints->memory;
  831. FT_UInt count1 = dim[0].hints.num_hints;
  832. FT_UInt count2 = dim[1].hints.num_hints;
  833. /* check bit count, must be equal to current total hint count */
  834. if ( bit_count != count1 + count2 )
  835. {
  836. FT_TRACE0(( "ps_hints_t2counter:"
  837. " called with invalid bitcount %d (instead of %d)\n",
  838. bit_count, count1 + count2 ));
  839. /* simply ignore the operator */
  840. return;
  841. }
  842. /* set-up new horizontal and vertical hint mask now */
  843. error = ps_dimension_set_mask_bits( &dim[0], bytes, 0, count1,
  844. 0, memory );
  845. if ( error )
  846. goto Fail;
  847. error = ps_dimension_set_mask_bits( &dim[1], bytes, count1, count2,
  848. 0, memory );
  849. if ( error )
  850. goto Fail;
  851. }
  852. return;
  853. Fail:
  854. hints->error = error;
  855. }
  856. /* end recording session */
  857. static FT_Error
  858. ps_hints_close( PS_Hints hints,
  859. FT_UInt end_point )
  860. {
  861. FT_Error error;
  862. error = hints->error;
  863. if ( !error )
  864. {
  865. FT_Memory memory = hints->memory;
  866. PS_Dimension dim = hints->dimension;
  867. error = ps_dimension_end( &dim[0], end_point, memory );
  868. if ( !error )
  869. {
  870. error = ps_dimension_end( &dim[1], end_point, memory );
  871. }
  872. }
  873. #ifdef DEBUG_HINTER
  874. if ( !error )
  875. ps_debug_hints = hints;
  876. #endif
  877. return error;
  878. }
  879. /*************************************************************************/
  880. /*************************************************************************/
  881. /***** *****/
  882. /***** TYPE 1 HINTS RECORDING INTERFACE *****/
  883. /***** *****/
  884. /*************************************************************************/
  885. /*************************************************************************/
  886. static void
  887. t1_hints_open( T1_Hints hints )
  888. {
  889. ps_hints_open( (PS_Hints)hints, PS_HINT_TYPE_1 );
  890. }
  891. static void
  892. t1_hints_stem( T1_Hints hints,
  893. FT_Int dimension,
  894. FT_Fixed* coords )
  895. {
  896. FT_Pos stems[2];
  897. stems[0] = FIXED_TO_INT( coords[0] );
  898. stems[1] = FIXED_TO_INT( coords[1] );
  899. ps_hints_stem( (PS_Hints)hints, dimension, 1, stems );
  900. }
  901. FT_LOCAL_DEF( void )
  902. t1_hints_funcs_init( T1_Hints_FuncsRec* funcs )
  903. {
  904. FT_MEM_ZERO( (char*)funcs, sizeof ( *funcs ) );
  905. funcs->open = (T1_Hints_OpenFunc) t1_hints_open;
  906. funcs->close = (T1_Hints_CloseFunc) ps_hints_close;
  907. funcs->stem = (T1_Hints_SetStemFunc) t1_hints_stem;
  908. funcs->stem3 = (T1_Hints_SetStem3Func)ps_hints_t1stem3;
  909. funcs->reset = (T1_Hints_ResetFunc) ps_hints_t1reset;
  910. funcs->apply = (T1_Hints_ApplyFunc) ps_hints_apply;
  911. }
  912. /*************************************************************************/
  913. /*************************************************************************/
  914. /***** *****/
  915. /***** TYPE 2 HINTS RECORDING INTERFACE *****/
  916. /***** *****/
  917. /*************************************************************************/
  918. /*************************************************************************/
  919. static void
  920. t2_hints_open( T2_Hints hints )
  921. {
  922. ps_hints_open( (PS_Hints)hints, PS_HINT_TYPE_2 );
  923. }
  924. static void
  925. t2_hints_stems( T2_Hints hints,
  926. FT_Int dimension,
  927. FT_Int count,
  928. FT_Fixed* coords )
  929. {
  930. FT_Pos stems[32], y, n;
  931. FT_Int total = count;
  932. y = 0;
  933. while ( total > 0 )
  934. {
  935. /* determine number of stems to write */
  936. count = total;
  937. if ( count > 16 )
  938. count = 16;
  939. /* compute integer stem positions in font units */
  940. for ( n = 0; n < count * 2; n++ )
  941. {
  942. y += coords[n];
  943. stems[n] = FIXED_TO_INT( y );
  944. }
  945. /* compute lengths */
  946. for ( n = 0; n < count * 2; n += 2 )
  947. stems[n + 1] = stems[n + 1] - stems[n];
  948. /* add them to the current dimension */
  949. ps_hints_stem( (PS_Hints)hints, dimension, count, stems );
  950. total -= count;
  951. }
  952. }
  953. FT_LOCAL_DEF( void )
  954. t2_hints_funcs_init( T2_Hints_FuncsRec* funcs )
  955. {
  956. FT_MEM_ZERO( funcs, sizeof ( *funcs ) );
  957. funcs->open = (T2_Hints_OpenFunc) t2_hints_open;
  958. funcs->close = (T2_Hints_CloseFunc) ps_hints_close;
  959. funcs->stems = (T2_Hints_StemsFunc) t2_hints_stems;
  960. funcs->hintmask= (T2_Hints_MaskFunc) ps_hints_t2mask;
  961. funcs->counter = (T2_Hints_CounterFunc)ps_hints_t2counter;
  962. funcs->apply = (T2_Hints_ApplyFunc) ps_hints_apply;
  963. }
  964. /* END */