PageRenderTime 59ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/modules/freetype2/src/pshinter/pshalgo.c

http://github.com/zpao/v8monkey
C | 2306 lines | 1557 code | 513 blank | 236 comment | 356 complexity | 950e909d4d9c4e6b855fd40d270e8083 MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception, LGPL-3.0, AGPL-1.0, LGPL-2.1, BSD-3-Clause, GPL-2.0, JSON, Apache-2.0, 0BSD
  1. /***************************************************************************/
  2. /* */
  3. /* pshalgo.c */
  4. /* */
  5. /* PostScript hinting algorithm (body). */
  6. /* */
  7. /* Copyright 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010 */
  8. /* 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. #include <ft2build.h>
  19. #include FT_INTERNAL_OBJECTS_H
  20. #include FT_INTERNAL_DEBUG_H
  21. #include FT_INTERNAL_CALC_H
  22. #include "pshalgo.h"
  23. #include "pshnterr.h"
  24. #undef FT_COMPONENT
  25. #define FT_COMPONENT trace_pshalgo2
  26. #ifdef DEBUG_HINTER
  27. PSH_Hint_Table ps_debug_hint_table = 0;
  28. PSH_HintFunc ps_debug_hint_func = 0;
  29. PSH_Glyph ps_debug_glyph = 0;
  30. #endif
  31. #define COMPUTE_INFLEXS /* compute inflection points to optimize `S' */
  32. /* and similar glyphs */
  33. #define STRONGER /* slightly increase the contrast of smooth */
  34. /* hinting */
  35. /*************************************************************************/
  36. /*************************************************************************/
  37. /***** *****/
  38. /***** BASIC HINTS RECORDINGS *****/
  39. /***** *****/
  40. /*************************************************************************/
  41. /*************************************************************************/
  42. /* return true if two stem hints overlap */
  43. static FT_Int
  44. psh_hint_overlap( PSH_Hint hint1,
  45. PSH_Hint hint2 )
  46. {
  47. return hint1->org_pos + hint1->org_len >= hint2->org_pos &&
  48. hint2->org_pos + hint2->org_len >= hint1->org_pos;
  49. }
  50. /* destroy hints table */
  51. static void
  52. psh_hint_table_done( PSH_Hint_Table table,
  53. FT_Memory memory )
  54. {
  55. FT_FREE( table->zones );
  56. table->num_zones = 0;
  57. table->zone = 0;
  58. FT_FREE( table->sort );
  59. FT_FREE( table->hints );
  60. table->num_hints = 0;
  61. table->max_hints = 0;
  62. table->sort_global = 0;
  63. }
  64. /* deactivate all hints in a table */
  65. static void
  66. psh_hint_table_deactivate( PSH_Hint_Table table )
  67. {
  68. FT_UInt count = table->max_hints;
  69. PSH_Hint hint = table->hints;
  70. for ( ; count > 0; count--, hint++ )
  71. {
  72. psh_hint_deactivate( hint );
  73. hint->order = -1;
  74. }
  75. }
  76. /* internal function to record a new hint */
  77. static void
  78. psh_hint_table_record( PSH_Hint_Table table,
  79. FT_UInt idx )
  80. {
  81. PSH_Hint hint = table->hints + idx;
  82. if ( idx >= table->max_hints )
  83. {
  84. FT_TRACE0(( "psh_hint_table_record: invalid hint index %d\n", idx ));
  85. return;
  86. }
  87. /* ignore active hints */
  88. if ( psh_hint_is_active( hint ) )
  89. return;
  90. psh_hint_activate( hint );
  91. /* now scan the current active hint set to check */
  92. /* whether `hint' overlaps with another hint */
  93. {
  94. PSH_Hint* sorted = table->sort_global;
  95. FT_UInt count = table->num_hints;
  96. PSH_Hint hint2;
  97. hint->parent = 0;
  98. for ( ; count > 0; count--, sorted++ )
  99. {
  100. hint2 = sorted[0];
  101. if ( psh_hint_overlap( hint, hint2 ) )
  102. {
  103. hint->parent = hint2;
  104. break;
  105. }
  106. }
  107. }
  108. if ( table->num_hints < table->max_hints )
  109. table->sort_global[table->num_hints++] = hint;
  110. else
  111. FT_TRACE0(( "psh_hint_table_record: too many sorted hints! BUG!\n" ));
  112. }
  113. static void
  114. psh_hint_table_record_mask( PSH_Hint_Table table,
  115. PS_Mask hint_mask )
  116. {
  117. FT_Int mask = 0, val = 0;
  118. FT_Byte* cursor = hint_mask->bytes;
  119. FT_UInt idx, limit;
  120. limit = hint_mask->num_bits;
  121. for ( idx = 0; idx < limit; idx++ )
  122. {
  123. if ( mask == 0 )
  124. {
  125. val = *cursor++;
  126. mask = 0x80;
  127. }
  128. if ( val & mask )
  129. psh_hint_table_record( table, idx );
  130. mask >>= 1;
  131. }
  132. }
  133. /* create hints table */
  134. static FT_Error
  135. psh_hint_table_init( PSH_Hint_Table table,
  136. PS_Hint_Table hints,
  137. PS_Mask_Table hint_masks,
  138. PS_Mask_Table counter_masks,
  139. FT_Memory memory )
  140. {
  141. FT_UInt count;
  142. FT_Error error;
  143. FT_UNUSED( counter_masks );
  144. count = hints->num_hints;
  145. /* allocate our tables */
  146. if ( FT_NEW_ARRAY( table->sort, 2 * count ) ||
  147. FT_NEW_ARRAY( table->hints, count ) ||
  148. FT_NEW_ARRAY( table->zones, 2 * count + 1 ) )
  149. goto Exit;
  150. table->max_hints = count;
  151. table->sort_global = table->sort + count;
  152. table->num_hints = 0;
  153. table->num_zones = 0;
  154. table->zone = 0;
  155. /* initialize the `table->hints' array */
  156. {
  157. PSH_Hint write = table->hints;
  158. PS_Hint read = hints->hints;
  159. for ( ; count > 0; count--, write++, read++ )
  160. {
  161. write->org_pos = read->pos;
  162. write->org_len = read->len;
  163. write->flags = read->flags;
  164. }
  165. }
  166. /* we now need to determine the initial `parent' stems; first */
  167. /* activate the hints that are given by the initial hint masks */
  168. if ( hint_masks )
  169. {
  170. PS_Mask mask = hint_masks->masks;
  171. count = hint_masks->num_masks;
  172. table->hint_masks = hint_masks;
  173. for ( ; count > 0; count--, mask++ )
  174. psh_hint_table_record_mask( table, mask );
  175. }
  176. /* finally, do a linear parse in case some hints were left alone */
  177. if ( table->num_hints != table->max_hints )
  178. {
  179. FT_UInt idx;
  180. FT_TRACE0(( "psh_hint_table_init: missing/incorrect hint masks\n" ));
  181. count = table->max_hints;
  182. for ( idx = 0; idx < count; idx++ )
  183. psh_hint_table_record( table, idx );
  184. }
  185. Exit:
  186. return error;
  187. }
  188. static void
  189. psh_hint_table_activate_mask( PSH_Hint_Table table,
  190. PS_Mask hint_mask )
  191. {
  192. FT_Int mask = 0, val = 0;
  193. FT_Byte* cursor = hint_mask->bytes;
  194. FT_UInt idx, limit, count;
  195. limit = hint_mask->num_bits;
  196. count = 0;
  197. psh_hint_table_deactivate( table );
  198. for ( idx = 0; idx < limit; idx++ )
  199. {
  200. if ( mask == 0 )
  201. {
  202. val = *cursor++;
  203. mask = 0x80;
  204. }
  205. if ( val & mask )
  206. {
  207. PSH_Hint hint = &table->hints[idx];
  208. if ( !psh_hint_is_active( hint ) )
  209. {
  210. FT_UInt count2;
  211. #if 0
  212. PSH_Hint* sort = table->sort;
  213. PSH_Hint hint2;
  214. for ( count2 = count; count2 > 0; count2--, sort++ )
  215. {
  216. hint2 = sort[0];
  217. if ( psh_hint_overlap( hint, hint2 ) )
  218. FT_TRACE0(( "psh_hint_table_activate_mask:"
  219. " found overlapping hints\n" ))
  220. }
  221. #else
  222. count2 = 0;
  223. #endif
  224. if ( count2 == 0 )
  225. {
  226. psh_hint_activate( hint );
  227. if ( count < table->max_hints )
  228. table->sort[count++] = hint;
  229. else
  230. FT_TRACE0(( "psh_hint_tableactivate_mask:"
  231. " too many active hints\n" ));
  232. }
  233. }
  234. }
  235. mask >>= 1;
  236. }
  237. table->num_hints = count;
  238. /* now, sort the hints; they are guaranteed to not overlap */
  239. /* so we can compare their "org_pos" field directly */
  240. {
  241. FT_Int i1, i2;
  242. PSH_Hint hint1, hint2;
  243. PSH_Hint* sort = table->sort;
  244. /* a simple bubble sort will do, since in 99% of cases, the hints */
  245. /* will be already sorted -- and the sort will be linear */
  246. for ( i1 = 1; i1 < (FT_Int)count; i1++ )
  247. {
  248. hint1 = sort[i1];
  249. for ( i2 = i1 - 1; i2 >= 0; i2-- )
  250. {
  251. hint2 = sort[i2];
  252. if ( hint2->org_pos < hint1->org_pos )
  253. break;
  254. sort[i2 + 1] = hint2;
  255. sort[i2] = hint1;
  256. }
  257. }
  258. }
  259. }
  260. /*************************************************************************/
  261. /*************************************************************************/
  262. /***** *****/
  263. /***** HINTS GRID-FITTING AND OPTIMIZATION *****/
  264. /***** *****/
  265. /*************************************************************************/
  266. /*************************************************************************/
  267. #if 1
  268. static FT_Pos
  269. psh_dimension_quantize_len( PSH_Dimension dim,
  270. FT_Pos len,
  271. FT_Bool do_snapping )
  272. {
  273. if ( len <= 64 )
  274. len = 64;
  275. else
  276. {
  277. FT_Pos delta = len - dim->stdw.widths[0].cur;
  278. if ( delta < 0 )
  279. delta = -delta;
  280. if ( delta < 40 )
  281. {
  282. len = dim->stdw.widths[0].cur;
  283. if ( len < 48 )
  284. len = 48;
  285. }
  286. if ( len < 3 * 64 )
  287. {
  288. delta = ( len & 63 );
  289. len &= -64;
  290. if ( delta < 10 )
  291. len += delta;
  292. else if ( delta < 32 )
  293. len += 10;
  294. else if ( delta < 54 )
  295. len += 54;
  296. else
  297. len += delta;
  298. }
  299. else
  300. len = FT_PIX_ROUND( len );
  301. }
  302. if ( do_snapping )
  303. len = FT_PIX_ROUND( len );
  304. return len;
  305. }
  306. #endif /* 0 */
  307. #ifdef DEBUG_HINTER
  308. static void
  309. ps_simple_scale( PSH_Hint_Table table,
  310. FT_Fixed scale,
  311. FT_Fixed delta,
  312. FT_Int dimension )
  313. {
  314. PSH_Hint hint;
  315. FT_UInt count;
  316. for ( count = 0; count < table->max_hints; count++ )
  317. {
  318. hint = table->hints + count;
  319. hint->cur_pos = FT_MulFix( hint->org_pos, scale ) + delta;
  320. hint->cur_len = FT_MulFix( hint->org_len, scale );
  321. if ( ps_debug_hint_func )
  322. ps_debug_hint_func( hint, dimension );
  323. }
  324. }
  325. #endif /* DEBUG_HINTER */
  326. static FT_Fixed
  327. psh_hint_snap_stem_side_delta( FT_Fixed pos,
  328. FT_Fixed len )
  329. {
  330. FT_Fixed delta1 = FT_PIX_ROUND( pos ) - pos;
  331. FT_Fixed delta2 = FT_PIX_ROUND( pos + len ) - pos - len;
  332. if ( FT_ABS( delta1 ) <= FT_ABS( delta2 ) )
  333. return delta1;
  334. else
  335. return delta2;
  336. }
  337. static void
  338. psh_hint_align( PSH_Hint hint,
  339. PSH_Globals globals,
  340. FT_Int dimension,
  341. PSH_Glyph glyph )
  342. {
  343. PSH_Dimension dim = &globals->dimension[dimension];
  344. FT_Fixed scale = dim->scale_mult;
  345. FT_Fixed delta = dim->scale_delta;
  346. if ( !psh_hint_is_fitted( hint ) )
  347. {
  348. FT_Pos pos = FT_MulFix( hint->org_pos, scale ) + delta;
  349. FT_Pos len = FT_MulFix( hint->org_len, scale );
  350. FT_Int do_snapping;
  351. FT_Pos fit_len;
  352. PSH_AlignmentRec align;
  353. /* ignore stem alignments when requested through the hint flags */
  354. if ( ( dimension == 0 && !glyph->do_horz_hints ) ||
  355. ( dimension == 1 && !glyph->do_vert_hints ) )
  356. {
  357. hint->cur_pos = pos;
  358. hint->cur_len = len;
  359. psh_hint_set_fitted( hint );
  360. return;
  361. }
  362. /* perform stem snapping when requested - this is necessary
  363. * for monochrome and LCD hinting modes only
  364. */
  365. do_snapping = ( dimension == 0 && glyph->do_horz_snapping ) ||
  366. ( dimension == 1 && glyph->do_vert_snapping );
  367. hint->cur_len = fit_len = len;
  368. /* check blue zones for horizontal stems */
  369. align.align = PSH_BLUE_ALIGN_NONE;
  370. align.align_bot = align.align_top = 0;
  371. if ( dimension == 1 )
  372. psh_blues_snap_stem( &globals->blues,
  373. hint->org_pos + hint->org_len,
  374. hint->org_pos,
  375. &align );
  376. switch ( align.align )
  377. {
  378. case PSH_BLUE_ALIGN_TOP:
  379. /* the top of the stem is aligned against a blue zone */
  380. hint->cur_pos = align.align_top - fit_len;
  381. break;
  382. case PSH_BLUE_ALIGN_BOT:
  383. /* the bottom of the stem is aligned against a blue zone */
  384. hint->cur_pos = align.align_bot;
  385. break;
  386. case PSH_BLUE_ALIGN_TOP | PSH_BLUE_ALIGN_BOT:
  387. /* both edges of the stem are aligned against blue zones */
  388. hint->cur_pos = align.align_bot;
  389. hint->cur_len = align.align_top - align.align_bot;
  390. break;
  391. default:
  392. {
  393. PSH_Hint parent = hint->parent;
  394. if ( parent )
  395. {
  396. FT_Pos par_org_center, par_cur_center;
  397. FT_Pos cur_org_center, cur_delta;
  398. /* ensure that parent is already fitted */
  399. if ( !psh_hint_is_fitted( parent ) )
  400. psh_hint_align( parent, globals, dimension, glyph );
  401. /* keep original relation between hints, this is, use the */
  402. /* scaled distance between the centers of the hints to */
  403. /* compute the new position */
  404. par_org_center = parent->org_pos + ( parent->org_len >> 1 );
  405. par_cur_center = parent->cur_pos + ( parent->cur_len >> 1 );
  406. cur_org_center = hint->org_pos + ( hint->org_len >> 1 );
  407. cur_delta = FT_MulFix( cur_org_center - par_org_center, scale );
  408. pos = par_cur_center + cur_delta - ( len >> 1 );
  409. }
  410. hint->cur_pos = pos;
  411. hint->cur_len = fit_len;
  412. /* Stem adjustment tries to snap stem widths to standard
  413. * ones. This is important to prevent unpleasant rounding
  414. * artefacts.
  415. */
  416. if ( glyph->do_stem_adjust )
  417. {
  418. if ( len <= 64 )
  419. {
  420. /* the stem is less than one pixel; we will center it
  421. * around the nearest pixel center
  422. */
  423. if ( len >= 32 )
  424. {
  425. /* This is a special case where we also widen the stem
  426. * and align it to the pixel grid.
  427. *
  428. * stem_center = pos + (len/2)
  429. * nearest_pixel_center = FT_ROUND(stem_center-32)+32
  430. * new_pos = nearest_pixel_center-32
  431. * = FT_ROUND(stem_center-32)
  432. * = FT_FLOOR(stem_center-32+32)
  433. * = FT_FLOOR(stem_center)
  434. * new_len = 64
  435. */
  436. pos = FT_PIX_FLOOR( pos + ( len >> 1 ) );
  437. len = 64;
  438. }
  439. else if ( len > 0 )
  440. {
  441. /* This is a very small stem; we simply align it to the
  442. * pixel grid, trying to find the minimal displacement.
  443. *
  444. * left = pos
  445. * right = pos + len
  446. * left_nearest_edge = ROUND(pos)
  447. * right_nearest_edge = ROUND(right)
  448. *
  449. * if ( ABS(left_nearest_edge - left) <=
  450. * ABS(right_nearest_edge - right) )
  451. * new_pos = left
  452. * else
  453. * new_pos = right
  454. */
  455. FT_Pos left_nearest = FT_PIX_ROUND( pos );
  456. FT_Pos right_nearest = FT_PIX_ROUND( pos + len );
  457. FT_Pos left_disp = left_nearest - pos;
  458. FT_Pos right_disp = right_nearest - ( pos + len );
  459. if ( left_disp < 0 )
  460. left_disp = -left_disp;
  461. if ( right_disp < 0 )
  462. right_disp = -right_disp;
  463. if ( left_disp <= right_disp )
  464. pos = left_nearest;
  465. else
  466. pos = right_nearest;
  467. }
  468. else
  469. {
  470. /* this is a ghost stem; we simply round it */
  471. pos = FT_PIX_ROUND( pos );
  472. }
  473. }
  474. else
  475. {
  476. len = psh_dimension_quantize_len( dim, len, 0 );
  477. }
  478. }
  479. /* now that we have a good hinted stem width, try to position */
  480. /* the stem along a pixel grid integer coordinate */
  481. hint->cur_pos = pos + psh_hint_snap_stem_side_delta( pos, len );
  482. hint->cur_len = len;
  483. }
  484. }
  485. if ( do_snapping )
  486. {
  487. pos = hint->cur_pos;
  488. len = hint->cur_len;
  489. if ( len < 64 )
  490. len = 64;
  491. else
  492. len = FT_PIX_ROUND( len );
  493. switch ( align.align )
  494. {
  495. case PSH_BLUE_ALIGN_TOP:
  496. hint->cur_pos = align.align_top - len;
  497. hint->cur_len = len;
  498. break;
  499. case PSH_BLUE_ALIGN_BOT:
  500. hint->cur_len = len;
  501. break;
  502. case PSH_BLUE_ALIGN_BOT | PSH_BLUE_ALIGN_TOP:
  503. /* don't touch */
  504. break;
  505. default:
  506. hint->cur_len = len;
  507. if ( len & 64 )
  508. pos = FT_PIX_FLOOR( pos + ( len >> 1 ) ) + 32;
  509. else
  510. pos = FT_PIX_ROUND( pos + ( len >> 1 ) );
  511. hint->cur_pos = pos - ( len >> 1 );
  512. hint->cur_len = len;
  513. }
  514. }
  515. psh_hint_set_fitted( hint );
  516. #ifdef DEBUG_HINTER
  517. if ( ps_debug_hint_func )
  518. ps_debug_hint_func( hint, dimension );
  519. #endif
  520. }
  521. }
  522. #if 0 /* not used for now, experimental */
  523. /*
  524. * A variant to perform "light" hinting (i.e. FT_RENDER_MODE_LIGHT)
  525. * of stems
  526. */
  527. static void
  528. psh_hint_align_light( PSH_Hint hint,
  529. PSH_Globals globals,
  530. FT_Int dimension,
  531. PSH_Glyph glyph )
  532. {
  533. PSH_Dimension dim = &globals->dimension[dimension];
  534. FT_Fixed scale = dim->scale_mult;
  535. FT_Fixed delta = dim->scale_delta;
  536. if ( !psh_hint_is_fitted( hint ) )
  537. {
  538. FT_Pos pos = FT_MulFix( hint->org_pos, scale ) + delta;
  539. FT_Pos len = FT_MulFix( hint->org_len, scale );
  540. FT_Pos fit_len;
  541. PSH_AlignmentRec align;
  542. /* ignore stem alignments when requested through the hint flags */
  543. if ( ( dimension == 0 && !glyph->do_horz_hints ) ||
  544. ( dimension == 1 && !glyph->do_vert_hints ) )
  545. {
  546. hint->cur_pos = pos;
  547. hint->cur_len = len;
  548. psh_hint_set_fitted( hint );
  549. return;
  550. }
  551. fit_len = len;
  552. hint->cur_len = fit_len;
  553. /* check blue zones for horizontal stems */
  554. align.align = PSH_BLUE_ALIGN_NONE;
  555. align.align_bot = align.align_top = 0;
  556. if ( dimension == 1 )
  557. psh_blues_snap_stem( &globals->blues,
  558. hint->org_pos + hint->org_len,
  559. hint->org_pos,
  560. &align );
  561. switch ( align.align )
  562. {
  563. case PSH_BLUE_ALIGN_TOP:
  564. /* the top of the stem is aligned against a blue zone */
  565. hint->cur_pos = align.align_top - fit_len;
  566. break;
  567. case PSH_BLUE_ALIGN_BOT:
  568. /* the bottom of the stem is aligned against a blue zone */
  569. hint->cur_pos = align.align_bot;
  570. break;
  571. case PSH_BLUE_ALIGN_TOP | PSH_BLUE_ALIGN_BOT:
  572. /* both edges of the stem are aligned against blue zones */
  573. hint->cur_pos = align.align_bot;
  574. hint->cur_len = align.align_top - align.align_bot;
  575. break;
  576. default:
  577. {
  578. PSH_Hint parent = hint->parent;
  579. if ( parent )
  580. {
  581. FT_Pos par_org_center, par_cur_center;
  582. FT_Pos cur_org_center, cur_delta;
  583. /* ensure that parent is already fitted */
  584. if ( !psh_hint_is_fitted( parent ) )
  585. psh_hint_align_light( parent, globals, dimension, glyph );
  586. par_org_center = parent->org_pos + ( parent->org_len / 2 );
  587. par_cur_center = parent->cur_pos + ( parent->cur_len / 2 );
  588. cur_org_center = hint->org_pos + ( hint->org_len / 2 );
  589. cur_delta = FT_MulFix( cur_org_center - par_org_center, scale );
  590. pos = par_cur_center + cur_delta - ( len >> 1 );
  591. }
  592. /* Stems less than one pixel wide are easy -- we want to
  593. * make them as dark as possible, so they must fall within
  594. * one pixel. If the stem is split between two pixels
  595. * then snap the edge that is nearer to the pixel boundary
  596. * to the pixel boundary.
  597. */
  598. if ( len <= 64 )
  599. {
  600. if ( ( pos + len + 63 ) / 64 != pos / 64 + 1 )
  601. pos += psh_hint_snap_stem_side_delta ( pos, len );
  602. }
  603. /* Position stems other to minimize the amount of mid-grays.
  604. * There are, in general, two positions that do this,
  605. * illustrated as A) and B) below.
  606. *
  607. * + + + +
  608. *
  609. * A) |--------------------------------|
  610. * B) |--------------------------------|
  611. * C) |--------------------------------|
  612. *
  613. * Position A) (split the excess stem equally) should be better
  614. * for stems of width N + f where f < 0.5.
  615. *
  616. * Position B) (split the deficiency equally) should be better
  617. * for stems of width N + f where f > 0.5.
  618. *
  619. * It turns out though that minimizing the total number of lit
  620. * pixels is also important, so position C), with one edge
  621. * aligned with a pixel boundary is actually preferable
  622. * to A). There are also more possibile positions for C) than
  623. * for A) or B), so it involves less distortion of the overall
  624. * character shape.
  625. */
  626. else /* len > 64 */
  627. {
  628. FT_Fixed frac_len = len & 63;
  629. FT_Fixed center = pos + ( len >> 1 );
  630. FT_Fixed delta_a, delta_b;
  631. if ( ( len / 64 ) & 1 )
  632. {
  633. delta_a = FT_PIX_FLOOR( center ) + 32 - center;
  634. delta_b = FT_PIX_ROUND( center ) - center;
  635. }
  636. else
  637. {
  638. delta_a = FT_PIX_ROUND( center ) - center;
  639. delta_b = FT_PIX_FLOOR( center ) + 32 - center;
  640. }
  641. /* We choose between B) and C) above based on the amount
  642. * of fractinal stem width; for small amounts, choose
  643. * C) always, for large amounts, B) always, and inbetween,
  644. * pick whichever one involves less stem movement.
  645. */
  646. if ( frac_len < 32 )
  647. {
  648. pos += psh_hint_snap_stem_side_delta ( pos, len );
  649. }
  650. else if ( frac_len < 48 )
  651. {
  652. FT_Fixed side_delta = psh_hint_snap_stem_side_delta ( pos,
  653. len );
  654. if ( FT_ABS( side_delta ) < FT_ABS( delta_b ) )
  655. pos += side_delta;
  656. else
  657. pos += delta_b;
  658. }
  659. else
  660. {
  661. pos += delta_b;
  662. }
  663. }
  664. hint->cur_pos = pos;
  665. }
  666. } /* switch */
  667. psh_hint_set_fitted( hint );
  668. #ifdef DEBUG_HINTER
  669. if ( ps_debug_hint_func )
  670. ps_debug_hint_func( hint, dimension );
  671. #endif
  672. }
  673. }
  674. #endif /* 0 */
  675. static void
  676. psh_hint_table_align_hints( PSH_Hint_Table table,
  677. PSH_Globals globals,
  678. FT_Int dimension,
  679. PSH_Glyph glyph )
  680. {
  681. PSH_Hint hint;
  682. FT_UInt count;
  683. #ifdef DEBUG_HINTER
  684. PSH_Dimension dim = &globals->dimension[dimension];
  685. FT_Fixed scale = dim->scale_mult;
  686. FT_Fixed delta = dim->scale_delta;
  687. if ( ps_debug_no_vert_hints && dimension == 0 )
  688. {
  689. ps_simple_scale( table, scale, delta, dimension );
  690. return;
  691. }
  692. if ( ps_debug_no_horz_hints && dimension == 1 )
  693. {
  694. ps_simple_scale( table, scale, delta, dimension );
  695. return;
  696. }
  697. #endif /* DEBUG_HINTER*/
  698. hint = table->hints;
  699. count = table->max_hints;
  700. for ( ; count > 0; count--, hint++ )
  701. psh_hint_align( hint, globals, dimension, glyph );
  702. }
  703. /*************************************************************************/
  704. /*************************************************************************/
  705. /***** *****/
  706. /***** POINTS INTERPOLATION ROUTINES *****/
  707. /***** *****/
  708. /*************************************************************************/
  709. /*************************************************************************/
  710. #define PSH_ZONE_MIN -3200000L
  711. #define PSH_ZONE_MAX +3200000L
  712. #define xxDEBUG_ZONES
  713. #ifdef DEBUG_ZONES
  714. #include FT_CONFIG_STANDARD_LIBRARY_H
  715. static void
  716. psh_print_zone( PSH_Zone zone )
  717. {
  718. printf( "zone [scale,delta,min,max] = [%.3f,%.3f,%d,%d]\n",
  719. zone->scale / 65536.0,
  720. zone->delta / 64.0,
  721. zone->min,
  722. zone->max );
  723. }
  724. #else
  725. #define psh_print_zone( x ) do { } while ( 0 )
  726. #endif /* DEBUG_ZONES */
  727. /*************************************************************************/
  728. /*************************************************************************/
  729. /***** *****/
  730. /***** HINTER GLYPH MANAGEMENT *****/
  731. /***** *****/
  732. /*************************************************************************/
  733. /*************************************************************************/
  734. #if 1
  735. #define psh_corner_is_flat ft_corner_is_flat
  736. #define psh_corner_orientation ft_corner_orientation
  737. #else
  738. FT_LOCAL_DEF( FT_Int )
  739. psh_corner_is_flat( FT_Pos x_in,
  740. FT_Pos y_in,
  741. FT_Pos x_out,
  742. FT_Pos y_out )
  743. {
  744. FT_Pos ax = x_in;
  745. FT_Pos ay = y_in;
  746. FT_Pos d_in, d_out, d_corner;
  747. if ( ax < 0 )
  748. ax = -ax;
  749. if ( ay < 0 )
  750. ay = -ay;
  751. d_in = ax + ay;
  752. ax = x_out;
  753. if ( ax < 0 )
  754. ax = -ax;
  755. ay = y_out;
  756. if ( ay < 0 )
  757. ay = -ay;
  758. d_out = ax + ay;
  759. ax = x_out + x_in;
  760. if ( ax < 0 )
  761. ax = -ax;
  762. ay = y_out + y_in;
  763. if ( ay < 0 )
  764. ay = -ay;
  765. d_corner = ax + ay;
  766. return ( d_in + d_out - d_corner ) < ( d_corner >> 4 );
  767. }
  768. static FT_Int
  769. psh_corner_orientation( FT_Pos in_x,
  770. FT_Pos in_y,
  771. FT_Pos out_x,
  772. FT_Pos out_y )
  773. {
  774. FT_Int result;
  775. /* deal with the trivial cases quickly */
  776. if ( in_y == 0 )
  777. {
  778. if ( in_x >= 0 )
  779. result = out_y;
  780. else
  781. result = -out_y;
  782. }
  783. else if ( in_x == 0 )
  784. {
  785. if ( in_y >= 0 )
  786. result = -out_x;
  787. else
  788. result = out_x;
  789. }
  790. else if ( out_y == 0 )
  791. {
  792. if ( out_x >= 0 )
  793. result = in_y;
  794. else
  795. result = -in_y;
  796. }
  797. else if ( out_x == 0 )
  798. {
  799. if ( out_y >= 0 )
  800. result = -in_x;
  801. else
  802. result = in_x;
  803. }
  804. else /* general case */
  805. {
  806. long long delta = (long long)in_x * out_y - (long long)in_y * out_x;
  807. if ( delta == 0 )
  808. result = 0;
  809. else
  810. result = 1 - 2 * ( delta < 0 );
  811. }
  812. return result;
  813. }
  814. #endif /* !1 */
  815. #ifdef COMPUTE_INFLEXS
  816. /* compute all inflex points in a given glyph */
  817. static void
  818. psh_glyph_compute_inflections( PSH_Glyph glyph )
  819. {
  820. FT_UInt n;
  821. for ( n = 0; n < glyph->num_contours; n++ )
  822. {
  823. PSH_Point first, start, end, before, after;
  824. FT_Pos in_x, in_y, out_x, out_y;
  825. FT_Int orient_prev, orient_cur;
  826. FT_Int finished = 0;
  827. /* we need at least 4 points to create an inflection point */
  828. if ( glyph->contours[n].count < 4 )
  829. continue;
  830. /* compute first segment in contour */
  831. first = glyph->contours[n].start;
  832. start = end = first;
  833. do
  834. {
  835. end = end->next;
  836. if ( end == first )
  837. goto Skip;
  838. in_x = end->org_u - start->org_u;
  839. in_y = end->org_v - start->org_v;
  840. } while ( in_x == 0 && in_y == 0 );
  841. /* extend the segment start whenever possible */
  842. before = start;
  843. do
  844. {
  845. do
  846. {
  847. start = before;
  848. before = before->prev;
  849. if ( before == first )
  850. goto Skip;
  851. out_x = start->org_u - before->org_u;
  852. out_y = start->org_v - before->org_v;
  853. } while ( out_x == 0 && out_y == 0 );
  854. orient_prev = psh_corner_orientation( in_x, in_y, out_x, out_y );
  855. } while ( orient_prev == 0 );
  856. first = start;
  857. in_x = out_x;
  858. in_y = out_y;
  859. /* now, process all segments in the contour */
  860. do
  861. {
  862. /* first, extend current segment's end whenever possible */
  863. after = end;
  864. do
  865. {
  866. do
  867. {
  868. end = after;
  869. after = after->next;
  870. if ( after == first )
  871. finished = 1;
  872. out_x = after->org_u - end->org_u;
  873. out_y = after->org_v - end->org_v;
  874. } while ( out_x == 0 && out_y == 0 );
  875. orient_cur = psh_corner_orientation( in_x, in_y, out_x, out_y );
  876. } while ( orient_cur == 0 );
  877. if ( ( orient_cur ^ orient_prev ) < 0 )
  878. {
  879. do
  880. {
  881. psh_point_set_inflex( start );
  882. start = start->next;
  883. }
  884. while ( start != end );
  885. psh_point_set_inflex( start );
  886. }
  887. start = end;
  888. end = after;
  889. orient_prev = orient_cur;
  890. in_x = out_x;
  891. in_y = out_y;
  892. } while ( !finished );
  893. Skip:
  894. ;
  895. }
  896. }
  897. #endif /* COMPUTE_INFLEXS */
  898. static void
  899. psh_glyph_done( PSH_Glyph glyph )
  900. {
  901. FT_Memory memory = glyph->memory;
  902. psh_hint_table_done( &glyph->hint_tables[1], memory );
  903. psh_hint_table_done( &glyph->hint_tables[0], memory );
  904. FT_FREE( glyph->points );
  905. FT_FREE( glyph->contours );
  906. glyph->num_points = 0;
  907. glyph->num_contours = 0;
  908. glyph->memory = 0;
  909. }
  910. static int
  911. psh_compute_dir( FT_Pos dx,
  912. FT_Pos dy )
  913. {
  914. FT_Pos ax, ay;
  915. int result = PSH_DIR_NONE;
  916. ax = ( dx >= 0 ) ? dx : -dx;
  917. ay = ( dy >= 0 ) ? dy : -dy;
  918. if ( ay * 12 < ax )
  919. {
  920. /* |dy| <<< |dx| means a near-horizontal segment */
  921. result = ( dx >= 0 ) ? PSH_DIR_RIGHT : PSH_DIR_LEFT;
  922. }
  923. else if ( ax * 12 < ay )
  924. {
  925. /* |dx| <<< |dy| means a near-vertical segment */
  926. result = ( dy >= 0 ) ? PSH_DIR_UP : PSH_DIR_DOWN;
  927. }
  928. return result;
  929. }
  930. /* load outline point coordinates into hinter glyph */
  931. static void
  932. psh_glyph_load_points( PSH_Glyph glyph,
  933. FT_Int dimension )
  934. {
  935. FT_Vector* vec = glyph->outline->points;
  936. PSH_Point point = glyph->points;
  937. FT_UInt count = glyph->num_points;
  938. for ( ; count > 0; count--, point++, vec++ )
  939. {
  940. point->flags2 = 0;
  941. point->hint = NULL;
  942. if ( dimension == 0 )
  943. {
  944. point->org_u = vec->x;
  945. point->org_v = vec->y;
  946. }
  947. else
  948. {
  949. point->org_u = vec->y;
  950. point->org_v = vec->x;
  951. }
  952. #ifdef DEBUG_HINTER
  953. point->org_x = vec->x;
  954. point->org_y = vec->y;
  955. #endif
  956. }
  957. }
  958. /* save hinted point coordinates back to outline */
  959. static void
  960. psh_glyph_save_points( PSH_Glyph glyph,
  961. FT_Int dimension )
  962. {
  963. FT_UInt n;
  964. PSH_Point point = glyph->points;
  965. FT_Vector* vec = glyph->outline->points;
  966. char* tags = glyph->outline->tags;
  967. for ( n = 0; n < glyph->num_points; n++ )
  968. {
  969. if ( dimension == 0 )
  970. vec[n].x = point->cur_u;
  971. else
  972. vec[n].y = point->cur_u;
  973. if ( psh_point_is_strong( point ) )
  974. tags[n] |= (char)( ( dimension == 0 ) ? 32 : 64 );
  975. #ifdef DEBUG_HINTER
  976. if ( dimension == 0 )
  977. {
  978. point->cur_x = point->cur_u;
  979. point->flags_x = point->flags2 | point->flags;
  980. }
  981. else
  982. {
  983. point->cur_y = point->cur_u;
  984. point->flags_y = point->flags2 | point->flags;
  985. }
  986. #endif
  987. point++;
  988. }
  989. }
  990. static FT_Error
  991. psh_glyph_init( PSH_Glyph glyph,
  992. FT_Outline* outline,
  993. PS_Hints ps_hints,
  994. PSH_Globals globals )
  995. {
  996. FT_Error error;
  997. FT_Memory memory;
  998. /* clear all fields */
  999. FT_MEM_ZERO( glyph, sizeof ( *glyph ) );
  1000. memory = glyph->memory = globals->memory;
  1001. /* allocate and setup points + contours arrays */
  1002. if ( FT_NEW_ARRAY( glyph->points, outline->n_points ) ||
  1003. FT_NEW_ARRAY( glyph->contours, outline->n_contours ) )
  1004. goto Exit;
  1005. glyph->num_points = outline->n_points;
  1006. glyph->num_contours = outline->n_contours;
  1007. {
  1008. FT_UInt first = 0, next, n;
  1009. PSH_Point points = glyph->points;
  1010. PSH_Contour contour = glyph->contours;
  1011. for ( n = 0; n < glyph->num_contours; n++ )
  1012. {
  1013. FT_Int count;
  1014. PSH_Point point;
  1015. next = outline->contours[n] + 1;
  1016. count = next - first;
  1017. contour->start = points + first;
  1018. contour->count = (FT_UInt)count;
  1019. if ( count > 0 )
  1020. {
  1021. point = points + first;
  1022. point->prev = points + next - 1;
  1023. point->contour = contour;
  1024. for ( ; count > 1; count-- )
  1025. {
  1026. point[0].next = point + 1;
  1027. point[1].prev = point;
  1028. point++;
  1029. point->contour = contour;
  1030. }
  1031. point->next = points + first;
  1032. }
  1033. contour++;
  1034. first = next;
  1035. }
  1036. }
  1037. {
  1038. PSH_Point points = glyph->points;
  1039. PSH_Point point = points;
  1040. FT_Vector* vec = outline->points;
  1041. FT_UInt n;
  1042. for ( n = 0; n < glyph->num_points; n++, point++ )
  1043. {
  1044. FT_Int n_prev = (FT_Int)( point->prev - points );
  1045. FT_Int n_next = (FT_Int)( point->next - points );
  1046. FT_Pos dxi, dyi, dxo, dyo;
  1047. if ( !( outline->tags[n] & FT_CURVE_TAG_ON ) )
  1048. point->flags = PSH_POINT_OFF;
  1049. dxi = vec[n].x - vec[n_prev].x;
  1050. dyi = vec[n].y - vec[n_prev].y;
  1051. point->dir_in = (FT_Char)psh_compute_dir( dxi, dyi );
  1052. dxo = vec[n_next].x - vec[n].x;
  1053. dyo = vec[n_next].y - vec[n].y;
  1054. point->dir_out = (FT_Char)psh_compute_dir( dxo, dyo );
  1055. /* detect smooth points */
  1056. if ( point->flags & PSH_POINT_OFF )
  1057. point->flags |= PSH_POINT_SMOOTH;
  1058. else if ( point->dir_in == point->dir_out )
  1059. {
  1060. if ( point->dir_out != PSH_DIR_NONE ||
  1061. psh_corner_is_flat( dxi, dyi, dxo, dyo ) )
  1062. point->flags |= PSH_POINT_SMOOTH;
  1063. }
  1064. }
  1065. }
  1066. glyph->outline = outline;
  1067. glyph->globals = globals;
  1068. #ifdef COMPUTE_INFLEXS
  1069. psh_glyph_load_points( glyph, 0 );
  1070. psh_glyph_compute_inflections( glyph );
  1071. #endif /* COMPUTE_INFLEXS */
  1072. /* now deal with hints tables */
  1073. error = psh_hint_table_init( &glyph->hint_tables [0],
  1074. &ps_hints->dimension[0].hints,
  1075. &ps_hints->dimension[0].masks,
  1076. &ps_hints->dimension[0].counters,
  1077. memory );
  1078. if ( error )
  1079. goto Exit;
  1080. error = psh_hint_table_init( &glyph->hint_tables [1],
  1081. &ps_hints->dimension[1].hints,
  1082. &ps_hints->dimension[1].masks,
  1083. &ps_hints->dimension[1].counters,
  1084. memory );
  1085. if ( error )
  1086. goto Exit;
  1087. Exit:
  1088. return error;
  1089. }
  1090. /* compute all extrema in a glyph for a given dimension */
  1091. static void
  1092. psh_glyph_compute_extrema( PSH_Glyph glyph )
  1093. {
  1094. FT_UInt n;
  1095. /* first of all, compute all local extrema */
  1096. for ( n = 0; n < glyph->num_contours; n++ )
  1097. {
  1098. PSH_Point first = glyph->contours[n].start;
  1099. PSH_Point point, before, after;
  1100. if ( glyph->contours[n].count == 0 )
  1101. continue;
  1102. point = first;
  1103. before = point;
  1104. after = point;
  1105. do
  1106. {
  1107. before = before->prev;
  1108. if ( before == first )
  1109. goto Skip;
  1110. } while ( before->org_u == point->org_u );
  1111. first = point = before->next;
  1112. for (;;)
  1113. {
  1114. after = point;
  1115. do
  1116. {
  1117. after = after->next;
  1118. if ( after == first )
  1119. goto Next;
  1120. } while ( after->org_u == point->org_u );
  1121. if ( before->org_u < point->org_u )
  1122. {
  1123. if ( after->org_u < point->org_u )
  1124. {
  1125. /* local maximum */
  1126. goto Extremum;
  1127. }
  1128. }
  1129. else /* before->org_u > point->org_u */
  1130. {
  1131. if ( after->org_u > point->org_u )
  1132. {
  1133. /* local minimum */
  1134. Extremum:
  1135. do
  1136. {
  1137. psh_point_set_extremum( point );
  1138. point = point->next;
  1139. } while ( point != after );
  1140. }
  1141. }
  1142. before = after->prev;
  1143. point = after;
  1144. } /* for */
  1145. Next:
  1146. ;
  1147. }
  1148. /* for each extremum, determine its direction along the */
  1149. /* orthogonal axis */
  1150. for ( n = 0; n < glyph->num_points; n++ )
  1151. {
  1152. PSH_Point point, before, after;
  1153. point = &glyph->points[n];
  1154. before = point;
  1155. after = point;
  1156. if ( psh_point_is_extremum( point ) )
  1157. {
  1158. do
  1159. {
  1160. before = before->prev;
  1161. if ( before == point )
  1162. goto Skip;
  1163. } while ( before->org_v == point->org_v );
  1164. do
  1165. {
  1166. after = after->next;
  1167. if ( after == point )
  1168. goto Skip;
  1169. } while ( after->org_v == point->org_v );
  1170. }
  1171. if ( before->org_v < point->org_v &&
  1172. after->org_v > point->org_v )
  1173. {
  1174. psh_point_set_positive( point );
  1175. }
  1176. else if ( before->org_v > point->org_v &&
  1177. after->org_v < point->org_v )
  1178. {
  1179. psh_point_set_negative( point );
  1180. }
  1181. Skip:
  1182. ;
  1183. }
  1184. }
  1185. /* major_dir is the direction for points on the bottom/left of the stem; */
  1186. /* Points on the top/right of the stem will have a direction of */
  1187. /* -major_dir. */
  1188. static void
  1189. psh_hint_table_find_strong_points( PSH_Hint_Table table,
  1190. PSH_Point point,
  1191. FT_UInt count,
  1192. FT_Int threshold,
  1193. FT_Int major_dir )
  1194. {
  1195. PSH_Hint* sort = table->sort;
  1196. FT_UInt num_hints = table->num_hints;
  1197. for ( ; count > 0; count--, point++ )
  1198. {
  1199. FT_Int point_dir = 0;
  1200. FT_Pos org_u = point->org_u;
  1201. if ( psh_point_is_strong( point ) )
  1202. continue;
  1203. if ( PSH_DIR_COMPARE( point->dir_in, major_dir ) )
  1204. point_dir = point->dir_in;
  1205. else if ( PSH_DIR_COMPARE( point->dir_out, major_dir ) )
  1206. point_dir = point->dir_out;
  1207. if ( point_dir )
  1208. {
  1209. if ( point_dir == major_dir )
  1210. {
  1211. FT_UInt nn;
  1212. for ( nn = 0; nn < num_hints; nn++ )
  1213. {
  1214. PSH_Hint hint = sort[nn];
  1215. FT_Pos d = org_u - hint->org_pos;
  1216. if ( d < threshold && -d < threshold )
  1217. {
  1218. psh_point_set_strong( point );
  1219. point->flags2 |= PSH_POINT_EDGE_MIN;
  1220. point->hint = hint;
  1221. break;
  1222. }
  1223. }
  1224. }
  1225. else if ( point_dir == -major_dir )
  1226. {
  1227. FT_UInt nn;
  1228. for ( nn = 0; nn < num_hints; nn++ )
  1229. {
  1230. PSH_Hint hint = sort[nn];
  1231. FT_Pos d = org_u - hint->org_pos - hint->org_len;
  1232. if ( d < threshold && -d < threshold )
  1233. {
  1234. psh_point_set_strong( point );
  1235. point->flags2 |= PSH_POINT_EDGE_MAX;
  1236. point->hint = hint;
  1237. break;
  1238. }
  1239. }
  1240. }
  1241. }
  1242. #if 1
  1243. else if ( psh_point_is_extremum( point ) )
  1244. {
  1245. /* treat extrema as special cases for stem edge alignment */
  1246. FT_UInt nn, min_flag, max_flag;
  1247. if ( major_dir == PSH_DIR_HORIZONTAL )
  1248. {
  1249. min_flag = PSH_POINT_POSITIVE;
  1250. max_flag = PSH_POINT_NEGATIVE;
  1251. }
  1252. else
  1253. {
  1254. min_flag = PSH_POINT_NEGATIVE;
  1255. max_flag = PSH_POINT_POSITIVE;
  1256. }
  1257. if ( point->flags2 & min_flag )
  1258. {
  1259. for ( nn = 0; nn < num_hints; nn++ )
  1260. {
  1261. PSH_Hint hint = sort[nn];
  1262. FT_Pos d = org_u - hint->org_pos;
  1263. if ( d < threshold && -d < threshold )
  1264. {
  1265. point->flags2 |= PSH_POINT_EDGE_MIN;
  1266. point->hint = hint;
  1267. psh_point_set_strong( point );
  1268. break;
  1269. }
  1270. }
  1271. }
  1272. else if ( point->flags2 & max_flag )
  1273. {
  1274. for ( nn = 0; nn < num_hints; nn++ )
  1275. {
  1276. PSH_Hint hint = sort[nn];
  1277. FT_Pos d = org_u - hint->org_pos - hint->org_len;
  1278. if ( d < threshold && -d < threshold )
  1279. {
  1280. point->flags2 |= PSH_POINT_EDGE_MAX;
  1281. point->hint = hint;
  1282. psh_point_set_strong( point );
  1283. break;
  1284. }
  1285. }
  1286. }
  1287. if ( point->hint == NULL )
  1288. {
  1289. for ( nn = 0; nn < num_hints; nn++ )
  1290. {
  1291. PSH_Hint hint = sort[nn];
  1292. if ( org_u >= hint->org_pos &&
  1293. org_u <= hint->org_pos + hint->org_len )
  1294. {
  1295. point->hint = hint;
  1296. break;
  1297. }
  1298. }
  1299. }
  1300. }
  1301. #endif /* 1 */
  1302. }
  1303. }
  1304. /* the accepted shift for strong points in fractional pixels */
  1305. #define PSH_STRONG_THRESHOLD 32
  1306. /* the maximum shift value in font units */
  1307. #define PSH_STRONG_THRESHOLD_MAXIMUM 30
  1308. /* find strong points in a glyph */
  1309. static void
  1310. psh_glyph_find_strong_points( PSH_Glyph glyph,
  1311. FT_Int dimension )
  1312. {
  1313. /* a point is `strong' if it is located on a stem edge and */
  1314. /* has an `in' or `out' tangent parallel to the hint's direction */
  1315. PSH_Hint_Table table = &glyph->hint_tables[dimension];
  1316. PS_Mask mask = table->hint_masks->masks;
  1317. FT_UInt num_masks = table->hint_masks->num_masks;
  1318. FT_UInt first = 0;
  1319. FT_Int major_dir = dimension == 0 ? PSH_DIR_VERTICAL
  1320. : PSH_DIR_HORIZONTAL;
  1321. PSH_Dimension dim = &glyph->globals->dimension[dimension];
  1322. FT_Fixed scale = dim->scale_mult;
  1323. FT_Int threshold;
  1324. threshold = (FT_Int)FT_DivFix( PSH_STRONG_THRESHOLD, scale );
  1325. if ( threshold > PSH_STRONG_THRESHOLD_MAXIMUM )
  1326. threshold = PSH_STRONG_THRESHOLD_MAXIMUM;
  1327. /* process secondary hints to `selected' points */
  1328. if ( num_masks > 1 && glyph->num_points > 0 )
  1329. {
  1330. /* the `endchar' op can reduce the number of points */
  1331. first = mask->end_point > glyph->num_points
  1332. ? glyph->num_points
  1333. : mask->end_point;
  1334. mask++;
  1335. for ( ; num_masks > 1; num_masks--, mask++ )
  1336. {
  1337. FT_UInt next;
  1338. FT_Int count;
  1339. next = mask->end_point > glyph->num_points
  1340. ? glyph->num_points
  1341. : mask->end_point;
  1342. count = next - first;
  1343. if ( count > 0 )
  1344. {
  1345. PSH_Point point = glyph->points + first;
  1346. psh_hint_table_activate_mask( table, mask );
  1347. psh_hint_table_find_strong_points( table, point, count,
  1348. threshold, major_dir );
  1349. }
  1350. first = next;
  1351. }
  1352. }
  1353. /* process primary hints for all points */
  1354. if ( num_masks == 1 )
  1355. {
  1356. FT_UInt count = glyph->num_points;
  1357. PSH_Point point = glyph->points;
  1358. psh_hint_table_activate_mask( table, table->hint_masks->masks );
  1359. psh_hint_table_find_strong_points( table, point, count,
  1360. threshold, major_dir );
  1361. }
  1362. /* now, certain points may have been attached to a hint and */
  1363. /* not marked as strong; update their flags then */
  1364. {
  1365. FT_UInt count = glyph->num_points;
  1366. PSH_Point point = glyph->points;
  1367. for ( ; count > 0; count--, point++ )
  1368. if ( point->hint && !psh_point_is_strong( point ) )
  1369. psh_point_set_strong( point );
  1370. }
  1371. }
  1372. /* find points in a glyph which are in a blue zone and have `in' or */
  1373. /* `out' tangents parallel to the horizontal axis */
  1374. static void
  1375. psh_glyph_find_blue_points( PSH_Blues blues,
  1376. PSH_Glyph glyph )
  1377. {
  1378. PSH_Blue_Table table;
  1379. PSH_Blue_Zone zone;
  1380. FT_UInt glyph_count = glyph->num_points;
  1381. FT_UInt blue_count;
  1382. PSH_Point point = glyph->points;
  1383. for ( ; glyph_count > 0; glyph_count--, point++ )
  1384. {
  1385. FT_Pos y;
  1386. /* check tangents */
  1387. if ( !PSH_DIR_COMPARE( point->dir_in, PSH_DIR_HORIZONTAL ) &&
  1388. !PSH_DIR_COMPARE( point->dir_out, PSH_DIR_HORIZONTAL ) )
  1389. continue;
  1390. /* skip strong points */
  1391. if ( psh_point_is_strong( point ) )
  1392. continue;
  1393. y = point->org_u;
  1394. /* look up top zones */
  1395. table = &blues->normal_top;
  1396. blue_count = table->count;
  1397. zone = table->zones;
  1398. for ( ; blue_count > 0; blue_count--, zone++ )
  1399. {
  1400. FT_Pos delta = y - zone->org_bottom;
  1401. if ( delta < -blues->blue_fuzz )
  1402. break;
  1403. if ( y <= zone->org_top + blues->blue_fuzz )
  1404. if ( blues->no_overshoots || delta <= blues->blue_threshold )
  1405. {
  1406. point->cur_u = zone->cur_bottom;
  1407. psh_point_set_strong( point );
  1408. psh_point_set_fitted( point );
  1409. }
  1410. }
  1411. /* look up bottom zones */
  1412. table = &blues->normal_bottom;
  1413. blue_count = table->count;
  1414. zone = table->zones + blue_count - 1;
  1415. for ( ; blue_count > 0; blue_count--, zone-- )
  1416. {
  1417. FT_Pos delta = zone->org_top - y;
  1418. if ( delta < -blues->blue_fuzz )
  1419. break;
  1420. if ( y >= zone->org_bottom - blues->blue_fuzz )
  1421. if ( blues->no_overshoots || delta < blues->blue_threshold )
  1422. {
  1423. point->cur_u = zone->cur_top;
  1424. psh_point_set_strong( point );
  1425. psh_point_set_fitted( point );
  1426. }
  1427. }
  1428. }
  1429. }
  1430. /* interpolate strong points with the help of hinted coordinates */
  1431. static void
  1432. psh_glyph_interpolate_strong_points( PSH_Glyph glyph,
  1433. FT_Int dimension )
  1434. {
  1435. PSH_Dimension dim = &glyph->globals->dimension[dimension];
  1436. FT_Fixed scale = dim->scale_mult;
  1437. FT_UInt count = glyph->num_points;
  1438. PSH_Point point = glyph->points;
  1439. for ( ; count > 0; count--, point++ )
  1440. {
  1441. PSH_Hint hint = point->hint;
  1442. if ( hint )
  1443. {
  1444. FT_Pos delta;
  1445. if ( psh_point_is_edge_min( point ) )
  1446. point->cur_u = hint->cur_pos;
  1447. else if ( psh_point_is_edge_max( point ) )
  1448. point->cur_u = hint->cur_pos + hint->cur_len;
  1449. else
  1450. {
  1451. delta = point->org_u - hint->org_pos;
  1452. if ( delta <= 0 )
  1453. point->cur_u = hint->cur_pos + FT_MulFix( delta, scale );
  1454. else if ( delta >= hint->org_len )
  1455. point->cur_u = hint->cur_pos + hint->cur_len +
  1456. FT_MulFix( delta - hint->org_len, scale );
  1457. else /* hint->org_len > 0 */
  1458. point->cur_u = hint->cur_pos +
  1459. FT_MulDiv( delta, hint->cur_len,
  1460. hint->org_len );
  1461. }
  1462. psh_point_set_fitted( point );
  1463. }
  1464. }
  1465. }
  1466. #define PSH_MAX_STRONG_INTERNAL 16
  1467. static void
  1468. psh_glyph_interpolate_normal_points( PSH_Glyph glyph,
  1469. FT_Int dimension )
  1470. {
  1471. #if 1
  1472. /* first technique: a point is strong if it is a local extremum */
  1473. PSH_Dimension dim = &glyph->globals->dimension[dimension];
  1474. FT_Fixed scale = dim->scale_mult;
  1475. FT_Memory memory = glyph->memory;
  1476. PSH_Point* strongs = NULL;
  1477. PSH_Point strongs_0[PSH_MAX_STRONG_INTERNAL];
  1478. FT_UInt num_strongs = 0;
  1479. PSH_Point points = glyph->points;
  1480. PSH_Point points_end = points + glyph->num_points;
  1481. PSH_Point point;
  1482. /* first count the number of strong points */
  1483. for ( point = points; point < points_end; point++ )
  1484. {
  1485. if ( psh_point_is_strong( point ) )
  1486. num_strongs++;
  1487. }
  1488. if ( num_strongs == 0 ) /* nothing to do here */
  1489. return;
  1490. /* allocate an array to store a list of points, */
  1491. /* stored in increasing org_u order */
  1492. if ( num_strongs <= PSH_MAX_STRONG_INTERNAL )
  1493. strongs = strongs_0;
  1494. else
  1495. {
  1496. FT_Error error;
  1497. if ( FT_NEW_ARRAY( strongs, num_strongs ) )
  1498. return;
  1499. }
  1500. num_strongs = 0;
  1501. for ( point = points; point < points_end; point++ )
  1502. {
  1503. PSH_Point* insert;
  1504. if ( !psh_point_is_strong( point ) )
  1505. continue;
  1506. for ( insert = strongs + num_strongs; insert > strongs; insert-- )
  1507. {
  1508. if ( insert[-1]->org_u <= point->org_u )
  1509. break;
  1510. insert[0] = insert[-1];
  1511. }
  1512. insert[0] = point;
  1513. num_strongs++;
  1514. }
  1515. /* now try to interpolate all normal points */
  1516. for ( point = points; point < points_end; point++ )
  1517. {
  1518. if ( psh_point_is_strong( point ) )
  1519. continue;
  1520. /* sometimes, some local extrema are smooth points */
  1521. if ( psh_point_is_smooth( point ) )
  1522. {
  1523. if ( point->dir_in == PSH_DIR_NONE ||
  1524. point->dir_in != point->dir_out )
  1525. continue;
  1526. if ( !psh_point_is_extremum( point ) &&
  1527. !psh_point_is_inflex( point ) )
  1528. continue;
  1529. point->flags &= ~PSH_POINT_SMOOTH;
  1530. }
  1531. /* find best enclosing point coordinates then interpolate */
  1532. {
  1533. PSH_Point before, after;
  1534. FT_UInt nn;
  1535. for ( nn = 0; nn < num_strongs; nn++ )
  1536. if ( strongs[nn]->org_u > point->org_u )
  1537. break;
  1538. if ( nn == 0 ) /* point before the first strong point */
  1539. {
  1540. after = strongs[0];
  1541. point->cur_u = after->cur_u +
  1542. FT_MulFix( point->org_u - after->org_u,
  1543. scale );
  1544. }
  1545. else
  1546. {
  1547. before = strongs[nn - 1];
  1548. for ( nn = num_strongs; nn > 0; nn-- )
  1549. if ( strongs[nn - 1]->org_u < point->org_u )
  1550. break;
  1551. if ( nn == num_strongs ) /* point is after last strong point */
  1552. {
  1553. before = strongs[nn - 1];
  1554. point->cur_u = before->cur_u +
  1555. FT_MulFix( point->org_u - before->org_u,
  1556. scale );
  1557. }
  1558. else
  1559. {
  1560. FT_Pos u;
  1561. after = strongs[nn];
  1562. /* now interpolate point between before and after */
  1563. u = point->org_u;
  1564. if ( u == before->org_u )
  1565. point->cur_u = before->cur_u;
  1566. else if ( u == after->org_u )
  1567. point->cur_u = after->cur_u;
  1568. else
  1569. point->cur_u = before->cur_u +
  1570. FT_MulDiv( u - before->org_u,
  1571. after->cur_u - before->cur_u,
  1572. after->org_u - before->org_u );
  1573. }
  1574. }
  1575. psh_point_set_fitted( point );
  1576. }
  1577. }
  1578. if ( strongs != strongs_0 )
  1579. FT_FREE( strongs );
  1580. #endif /* 1 */
  1581. }
  1582. /* interpolate other points */
  1583. static void
  1584. psh_glyph_interpolate_other_points( PSH_Glyph glyph,
  1585. FT_Int dimension )
  1586. {
  1587. PSH_Dimension dim = &glyph->globals->dimension[dimension];
  1588. FT_Fixed scale = dim->scale_mult;
  1589. FT_Fixed delta = dim->scale_delta;
  1590. PSH_Contour contour = glyph->contours;
  1591. FT_UInt num_contours = glyph->num_contours;
  1592. for ( ; num_contours > 0; num_contours--, contour++ )
  1593. {
  1594. PSH_Point start = contour->start;
  1595. PSH_Point first, next, point;
  1596. FT_UInt fit_count;
  1597. /* count the number of strong points in this contour */
  1598. next = start + contour->count;
  1599. fit_count = 0;
  1600. first = 0;
  1601. for ( point = start; point < next; point++ )
  1602. if ( psh_point_is_fitted( point ) )
  1603. {
  1604. if ( !first )
  1605. first = point;
  1606. fit_count++;
  1607. }
  1608. /* if there are less than 2 fitted points in the contour, we */
  1609. /* simply scale and eventually translate the contour points */
  1610. if ( fit_count < 2 )
  1611. {
  1612. if ( fit_count == 1 )
  1613. delta = first->cur_u - FT_MulFix( first->org_u, scale );
  1614. for ( point = start; point < next; point++ )
  1615. if ( point != first )
  1616. point->cur_u = FT_MulFix( point->org_u, scale ) + delta;
  1617. goto Next_Contour;
  1618. }
  1619. /* there are more than 2 strong points in this contour; we */
  1620. /* need to interpolate weak points between them */
  1621. start = first;
  1622. do
  1623. {
  1624. point = first;
  1625. /* skip consecutive fitted points */
  1626. for (;;)
  1627. {
  1628. next = first->next;
  1629. if ( next == start )
  1630. goto Next_Contour;
  1631. if ( !psh_point_is_fitted( next ) )
  1632. break;
  1633. first = next;
  1634. }
  1635. /* find next fitted point after unfitted one */
  1636. for (;;)
  1637. {
  1638. next = next->next;
  1639. if ( psh_point_is_fitted( next ) )
  1640. break;
  1641. }
  1642. /* now interpolate between them */
  1643. {
  1644. FT_Pos org_a, org_ab, cur_a, cur_ab;
  1645. FT_Pos org_c, org_ac, cur_c;
  1646. FT_Fixed scale_ab;
  1647. if ( first->org_u <= next->org_u )
  1648. {
  1649. org_a = first->org_u;
  1650. cur_a = first->cur_u;
  1651. org_ab = next->org_u - org_a;
  1652. cur_ab = next->cur_u - cur_a;
  1653. }
  1654. else
  1655. {
  1656. org_a = next->org_u;
  1657. cur_a = next->cur_u;
  1658. org_ab = first->org_u - org_a;
  1659. cur_ab = first->cur_u - cur_a;
  1660. }
  1661. scale_ab = 0x10000L;
  1662. if ( org_ab > 0 )
  1663. scale_ab = FT_DivFix( cur_ab, org_ab );
  1664. point = first->next;
  1665. do
  1666. {
  1667. org_c = point->org_u;
  1668. org_ac = org_c - org_a;
  1669. if ( org_ac <= 0 )
  1670. {
  1671. /* on the left of the interpolation zone */
  1672. cur_c = cur_a + FT_MulFix( org_ac, scale );
  1673. }
  1674. else if ( org_ac >= org_ab )
  1675. {
  1676. /* on the right on the interpolation zone */
  1677. cur_c = cur_a + cur_ab + FT_MulFix( org_ac - org_ab, scale );
  1678. }
  1679. else
  1680. {
  1681. /* within the interpolation zone */
  1682. cur_c = cur_a + FT_MulFix( org_ac, scale_ab );
  1683. }
  1684. point->cur_u = cur_c;
  1685. point = point->next;
  1686. } while ( point != next );
  1687. }
  1688. /* keep going until all points in the contours have been processed */
  1689. first = next;
  1690. } while ( first != start );
  1691. Next_Contour:
  1692. ;
  1693. }
  1694. }
  1695. /*************************************************************************/
  1696. /*************************************************************************/
  1697. /***** *****/
  1698. /***** HIGH-LEVEL INTERFACE *****/
  1699. /***** *****/
  1700. /*************************************************************************/
  1701. /*************************************************************************/
  1702. FT_Error
  1703. ps_hints_apply( PS_Hints ps_hints,
  1704. FT_Outline* outline,
  1705. PSH_Globals globals,
  1706. FT_Render_Mode hint_mode )
  1707. {
  1708. PSH_GlyphRec glyphrec;
  1709. PSH_Glyph glyph = &glyphrec;
  1710. FT_Error error;
  1711. #ifdef DEBUG_HINTER
  1712. FT_Memory memory;
  1713. #endif
  1714. FT_Int dimension;
  1715. /* something to do? */
  1716. if ( outline->n_points == 0 || outline->n_contours == 0 )
  1717. return PSH_Err_Ok;
  1718. #ifdef DEBUG_HINTER
  1719. memory = globals->memory;
  1720. if ( ps_debug_glyph )
  1721. {
  1722. psh_glyph_done( ps_debug_glyph );
  1723. FT_FREE( ps_debug_glyph );
  1724. }
  1725. if ( FT_NEW( glyph ) )
  1726. return error;
  1727. ps_debug_glyph = glyph;
  1728. #endif /* DEBUG_HINTER */
  1729. error = psh_glyph_init( glyph, outline, ps_hints, globals );
  1730. if ( error )
  1731. goto Exit;
  1732. /* try to optimize the y_scale so that the top of non-capital letters
  1733. * is aligned on a pixel boundary whenever possible
  1734. */
  1735. {
  1736. PSH_Dimension dim_x = &glyph->globals->dimension[0];
  1737. PSH_Dimension dim_y = &glyph->globals->dimension[1];
  1738. FT_Fixed x_scale = dim_x->scale_mult;
  1739. FT_Fixed y_scale = dim_y->scale_mult;
  1740. FT_Fixed old_x_scale = x_scale;
  1741. FT_Fixed old_y_scale = y_scale;
  1742. FT_Fixed scaled;
  1743. FT_Fixed fitted;
  1744. FT_Bool rescale = FALSE;
  1745. scaled = FT_MulFix( globals->blues.normal_top.zones->org_ref, y_scale );
  1746. fitted = FT_PIX_ROUND( scaled );
  1747. if ( fitted != 0 && scaled != fitted )
  1748. {
  1749. rescale = TRUE;
  1750. y_scale = FT_MulDiv( y_scale, fitted, scaled );
  1751. if ( fitted < scaled )
  1752. x_scale -= x_scale / 50;
  1753. psh_globals_set_scale( glyph->globals, x_scale, y_scale, 0, 0 );
  1754. }
  1755. glyph->do_horz_hints = 1;
  1756. glyph->do_vert_hints = 1;
  1757. glyph->do_horz_snapping = FT_BOOL( hint_mode == FT_RENDER_MODE_MONO ||
  1758. hint_mode == FT_RENDER_MODE_LCD );
  1759. glyph->do_vert_snapping = FT_BOOL( hint_mode == FT_RENDER_MODE_MONO ||
  1760. hint_mode == FT_RENDER_MODE_LCD_V );
  1761. glyph->do_stem_adjust = FT_BOOL( hint_mode != FT_RENDER_MODE_LIGHT );
  1762. for ( dimension = 0; dimension < 2; dimension++ )
  1763. {
  1764. /* load outline coordinates into glyph */
  1765. psh_glyph_load_points( glyph, dimension );
  1766. /* compute local extrema */
  1767. psh_glyph_compute_extrema( glyph );
  1768. /* compute aligned stem/hints positions */
  1769. psh_hint_table_align_hints( &glyph->hint_tables[dimension],
  1770. glyph->globals,
  1771. dimension,
  1772. glyph );
  1773. /* find strong points, align them, then interpolate others */
  1774. psh_glyph_find_strong_points( glyph, dimension );
  1775. if ( dimension == 1 )
  1776. psh_glyph_find_blue_points( &globals->blues, glyph );
  1777. psh_glyph_interpolate_strong_points( glyph, dimension );
  1778. psh_glyph_interpolate_normal_points( glyph, dimension );
  1779. psh_glyph_interpolate_other_points( glyph, dimension );
  1780. /* save hinted coordinates back to outline */
  1781. psh_glyph_save_points( glyph, dimension );
  1782. if ( rescale )
  1783. psh_globals_set_scale( glyph->globals,
  1784. old_x_scale, old_y_scale, 0, 0 );
  1785. }
  1786. }
  1787. Exit:
  1788. #ifndef DEBUG_HINTER
  1789. psh_glyph_done( glyph );
  1790. #endif
  1791. return error;
  1792. }
  1793. /* END */