PageRenderTime 64ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 1ms

/src/compiler/android-ndk/jni/freetype/src/pshinter/pshalgo.c

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