pshalgo2.c 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557
  1. /***************************************************************************/
  2. /* */
  3. /* pshalgo2.c */
  4. /* */
  5. /* PostScript hinting algorithm 2 (body). */
  6. /* */
  7. /* Copyright 2001, 2002 by */
  8. /* David Turner, Robert Wilhelm, and Werner Lemberg. */
  9. /* */
  10. /* This file is part of the FreeType project, and may only be used */
  11. /* modified and distributed under the terms of the FreeType project */
  12. /* license, LICENSE.TXT. By continuing to use, modify, or distribute */
  13. /* this file you indicate that you have read the license and */
  14. /* understand and accept it fully. */
  15. /* */
  16. /***************************************************************************/
  17. #include <ft2build.h>
  18. #include FT_INTERNAL_OBJECTS_H
  19. #include FT_INTERNAL_DEBUG_H
  20. #include "pshalgo2.h"
  21. #undef FT_COMPONENT
  22. #define FT_COMPONENT trace_pshalgo2
  23. #ifdef DEBUG_HINTER
  24. PSH2_Hint_Table ps2_debug_hint_table = 0;
  25. PSH2_HintFunc ps2_debug_hint_func = 0;
  26. PSH2_Glyph ps2_debug_glyph = 0;
  27. #endif
  28. /*************************************************************************/
  29. /*************************************************************************/
  30. /***** *****/
  31. /***** BASIC HINTS RECORDINGS *****/
  32. /***** *****/
  33. /*************************************************************************/
  34. /*************************************************************************/
  35. /* return true iff two stem hints overlap */
  36. static FT_Int
  37. psh2_hint_overlap( PSH2_Hint hint1,
  38. PSH2_Hint hint2 )
  39. {
  40. return ( hint1->org_pos + hint1->org_len >= hint2->org_pos &&
  41. hint2->org_pos + hint2->org_len >= hint1->org_pos );
  42. }
  43. /* destroy hints table */
  44. static void
  45. psh2_hint_table_done( PSH2_Hint_Table table,
  46. FT_Memory memory )
  47. {
  48. FT_FREE( table->zones );
  49. table->num_zones = 0;
  50. table->zone = 0;
  51. FT_FREE( table->sort );
  52. FT_FREE( table->hints );
  53. table->num_hints = 0;
  54. table->max_hints = 0;
  55. table->sort_global = 0;
  56. }
  57. /* deactivate all hints in a table */
  58. static void
  59. psh2_hint_table_deactivate( PSH2_Hint_Table table )
  60. {
  61. FT_UInt count = table->max_hints;
  62. PSH2_Hint hint = table->hints;
  63. for ( ; count > 0; count--, hint++ )
  64. {
  65. psh2_hint_deactivate( hint );
  66. hint->order = -1;
  67. }
  68. }
  69. /* internal function used to record a new hint */
  70. static void
  71. psh2_hint_table_record( PSH2_Hint_Table table,
  72. FT_UInt idx )
  73. {
  74. PSH2_Hint hint = table->hints + idx;
  75. if ( idx >= table->max_hints )
  76. {
  77. FT_ERROR(( "%s.activate: invalid hint index %d\n", idx ));
  78. return;
  79. }
  80. /* ignore active hints */
  81. if ( psh2_hint_is_active( hint ) )
  82. return;
  83. psh2_hint_activate( hint );
  84. /* now scan the current active hint set in order to determine */
  85. /* if we are overlapping with another segment */
  86. {
  87. PSH2_Hint* sorted = table->sort_global;
  88. FT_UInt count = table->num_hints;
  89. PSH2_Hint hint2;
  90. hint->parent = 0;
  91. for ( ; count > 0; count--, sorted++ )
  92. {
  93. hint2 = sorted[0];
  94. if ( psh2_hint_overlap( hint, hint2 ) )
  95. {
  96. hint->parent = hint2;
  97. break;
  98. }
  99. }
  100. }
  101. if ( table->num_hints < table->max_hints )
  102. table->sort_global[table->num_hints++] = hint;
  103. else
  104. FT_ERROR(( "%s.activate: too many sorted hints! BUG!\n",
  105. "ps.fitter" ));
  106. }
  107. static void
  108. psh2_hint_table_record_mask( PSH2_Hint_Table table,
  109. PS_Mask hint_mask )
  110. {
  111. FT_Int mask = 0, val = 0;
  112. FT_Byte* cursor = hint_mask->bytes;
  113. FT_UInt idx, limit;
  114. limit = hint_mask->num_bits;
  115. for ( idx = 0; idx < limit; idx++ )
  116. {
  117. if ( mask == 0 )
  118. {
  119. val = *cursor++;
  120. mask = 0x80;
  121. }
  122. if ( val & mask )
  123. psh2_hint_table_record( table, idx );
  124. mask >>= 1;
  125. }
  126. }
  127. /* create hints table */
  128. static FT_Error
  129. psh2_hint_table_init( PSH2_Hint_Table table,
  130. PS_Hint_Table hints,
  131. PS_Mask_Table hint_masks,
  132. PS_Mask_Table counter_masks,
  133. FT_Memory memory )
  134. {
  135. FT_UInt count = hints->num_hints;
  136. FT_Error error;
  137. FT_UNUSED( counter_masks );
  138. /* allocate our tables */
  139. if ( FT_NEW_ARRAY( table->sort, 2 * count ) ||
  140. FT_NEW_ARRAY( table->hints, count ) ||
  141. FT_NEW_ARRAY( table->zones, 2 * count + 1 ) )
  142. goto Exit;
  143. table->max_hints = count;
  144. table->sort_global = table->sort + count;
  145. table->num_hints = 0;
  146. table->num_zones = 0;
  147. table->zone = 0;
  148. /* now, initialize the "hints" array */
  149. {
  150. PSH2_Hint write = table->hints;
  151. PS_Hint read = hints->hints;
  152. for ( ; count > 0; count--, write++, read++ )
  153. {
  154. write->org_pos = read->pos;
  155. write->org_len = read->len;
  156. write->flags = read->flags;
  157. }
  158. }
  159. /* we now need to determine the initial "parent" stems; first */
  160. /* activate the hints that are given by the initial hint masks */
  161. if ( hint_masks )
  162. {
  163. FT_UInt Count = hint_masks->num_masks;
  164. PS_Mask Mask = hint_masks->masks;
  165. table->hint_masks = hint_masks;
  166. for ( ; Count > 0; Count--, Mask++ )
  167. psh2_hint_table_record_mask( table, Mask );
  168. }
  169. /* now, do a linear parse in case some hints were left alone */
  170. if ( table->num_hints != table->max_hints )
  171. {
  172. FT_UInt Index, Count;
  173. FT_ERROR(( "%s.init: missing/incorrect hint masks!\n" ));
  174. Count = table->max_hints;
  175. for ( Index = 0; Index < Count; Index++ )
  176. psh2_hint_table_record( table, Index );
  177. }
  178. Exit:
  179. return error;
  180. }
  181. static void
  182. psh2_hint_table_activate_mask( PSH2_Hint_Table table,
  183. PS_Mask hint_mask )
  184. {
  185. FT_Int mask = 0, val = 0;
  186. FT_Byte* cursor = hint_mask->bytes;
  187. FT_UInt idx, limit, count;
  188. limit = hint_mask->num_bits;
  189. count = 0;
  190. psh2_hint_table_deactivate( table );
  191. for ( idx = 0; idx < limit; idx++ )
  192. {
  193. if ( mask == 0 )
  194. {
  195. val = *cursor++;
  196. mask = 0x80;
  197. }
  198. if ( val & mask )
  199. {
  200. PSH2_Hint hint = &table->hints[idx];
  201. if ( !psh2_hint_is_active( hint ) )
  202. {
  203. FT_UInt count2;
  204. #if 0
  205. PSH2_Hint* sort = table->sort;
  206. PSH2_Hint hint2;
  207. for ( count2 = count; count2 > 0; count2--, sort++ )
  208. {
  209. hint2 = sort[0];
  210. if ( psh2_hint_overlap( hint, hint2 ) )
  211. FT_ERROR(( "%s.activate_mask: found overlapping hints\n",
  212. "psf.hint" ));
  213. }
  214. #else
  215. count2 = 0;
  216. #endif
  217. if ( count2 == 0 )
  218. {
  219. psh2_hint_activate( hint );
  220. if ( count < table->max_hints )
  221. table->sort[count++] = hint;
  222. else
  223. FT_ERROR(( "%s.activate_mask: too many active hints\n",
  224. "psf.hint" ));
  225. }
  226. }
  227. }
  228. mask >>= 1;
  229. }
  230. table->num_hints = count;
  231. /* now, sort the hints; they are guaranteed to not overlap */
  232. /* so we can compare their "org_pos" field directly */
  233. {
  234. FT_Int i1, i2;
  235. PSH2_Hint hint1, hint2;
  236. PSH2_Hint* sort = table->sort;
  237. /* a simple bubble sort will do, since in 99% of cases, the hints */
  238. /* will be already sorted -- and the sort will be linear */
  239. for ( i1 = 1; i1 < (FT_Int)count; i1++ )
  240. {
  241. hint1 = sort[i1];
  242. for ( i2 = i1 - 1; i2 >= 0; i2-- )
  243. {
  244. hint2 = sort[i2];
  245. if ( hint2->org_pos < hint1->org_pos )
  246. break;
  247. sort[i2 + 1] = hint2;
  248. sort[i2] = hint1;
  249. }
  250. }
  251. }
  252. }
  253. /*************************************************************************/
  254. /*************************************************************************/
  255. /***** *****/
  256. /***** HINTS GRID-FITTING AND OPTIMIZATION *****/
  257. /***** *****/
  258. /*************************************************************************/
  259. /*************************************************************************/
  260. #ifdef DEBUG_HINTER
  261. static void
  262. ps2_simple_scale( PSH2_Hint_Table table,
  263. FT_Fixed scale,
  264. FT_Fixed delta,
  265. FT_Int dimension )
  266. {
  267. PSH2_Hint hint;
  268. FT_UInt count;
  269. for ( count = 0; count < table->max_hints; count++ )
  270. {
  271. hint = table->hints + count;
  272. hint->cur_pos = FT_MulFix( hint->org_pos, scale ) + delta;
  273. hint->cur_len = FT_MulFix( hint->org_len, scale );
  274. if ( ps2_debug_hint_func )
  275. ps2_debug_hint_func( hint, dimension );
  276. }
  277. }
  278. #endif
  279. static void
  280. psh2_hint_align( PSH2_Hint hint,
  281. PSH_Globals globals,
  282. FT_Int dimension )
  283. {
  284. PSH_Dimension dim = &globals->dimension[dimension];
  285. FT_Fixed scale = dim->scale_mult;
  286. FT_Fixed delta = dim->scale_delta;
  287. if ( !psh2_hint_is_fitted(hint) )
  288. {
  289. FT_Pos pos = FT_MulFix( hint->org_pos, scale ) + delta;
  290. FT_Pos len = FT_MulFix( hint->org_len, scale );
  291. FT_Pos fit_center;
  292. FT_Pos fit_len;
  293. PSH_AlignmentRec align;
  294. /* compute fitted width/height */
  295. fit_len = 0;
  296. if ( hint->org_len )
  297. {
  298. fit_len = psh_dimension_snap_width( dim, hint->org_len );
  299. if ( fit_len < 64 )
  300. fit_len = 64;
  301. else
  302. fit_len = ( fit_len + 32 ) & -64;
  303. }
  304. hint->cur_len = fit_len;
  305. /* check blue zones for horizontal stems */
  306. align.align = PSH_BLUE_ALIGN_NONE;
  307. align.align_bot = align.align_top = 0;
  308. if ( dimension == 1 )
  309. psh_blues_snap_stem( &globals->blues,
  310. hint->org_pos + hint->org_len,
  311. hint->org_pos,
  312. &align );
  313. switch ( align.align )
  314. {
  315. case PSH_BLUE_ALIGN_TOP:
  316. /* the top of the stem is aligned against a blue zone */
  317. hint->cur_pos = align.align_top - fit_len;
  318. break;
  319. case PSH_BLUE_ALIGN_BOT:
  320. /* the bottom of the stem is aligned against a blue zone */
  321. hint->cur_pos = align.align_bot;
  322. break;
  323. case PSH_BLUE_ALIGN_TOP | PSH_BLUE_ALIGN_BOT:
  324. /* both edges of the stem are aligned against blue zones */
  325. hint->cur_pos = align.align_bot;
  326. hint->cur_len = align.align_top - align.align_bot;
  327. break;
  328. default:
  329. {
  330. PSH2_Hint parent = hint->parent;
  331. if ( parent )
  332. {
  333. FT_Pos par_org_center, par_cur_center;
  334. FT_Pos cur_org_center, cur_delta;
  335. /* ensure that parent is already fitted */
  336. if ( !psh2_hint_is_fitted( parent ) )
  337. psh2_hint_align( parent, globals, dimension );
  338. par_org_center = parent->org_pos + ( parent->org_len / 2);
  339. par_cur_center = parent->cur_pos + ( parent->cur_len / 2);
  340. cur_org_center = hint->org_pos + ( hint->org_len / 2);
  341. cur_delta = FT_MulFix( cur_org_center - par_org_center, scale );
  342. #if 0
  343. if ( cur_delta >= 0 )
  344. cur_delta = ( cur_delta + 16 ) & -64;
  345. else
  346. cur_delta = -( (-cur_delta + 16 ) & -64 );
  347. #endif
  348. pos = par_cur_center + cur_delta - ( len >> 1 );
  349. }
  350. /* normal processing */
  351. if ( ( fit_len / 64 ) & 1 )
  352. {
  353. /* odd number of pixels */
  354. fit_center = ( ( pos + ( len >> 1 ) ) & -64 ) + 32;
  355. }
  356. else
  357. {
  358. /* even number of pixels */
  359. fit_center = ( pos + ( len >> 1 ) + 32 ) & -64;
  360. }
  361. hint->cur_pos = fit_center - ( fit_len >> 1 );
  362. }
  363. }
  364. psh2_hint_set_fitted( hint );
  365. #ifdef DEBUG_HINTER
  366. if ( ps2_debug_hint_func )
  367. ps2_debug_hint_func( hint, dimension );
  368. #endif
  369. }
  370. }
  371. static void
  372. psh2_hint_table_align_hints( PSH2_Hint_Table table,
  373. PSH_Globals globals,
  374. FT_Int dimension )
  375. {
  376. PSH2_Hint hint;
  377. FT_UInt count;
  378. #ifdef DEBUG_HINTER
  379. PSH_Dimension dim = &globals->dimension[dimension];
  380. FT_Fixed scale = dim->scale_mult;
  381. FT_Fixed delta = dim->scale_delta;
  382. if ( ps_debug_no_vert_hints && dimension == 0 )
  383. {
  384. ps2_simple_scale( table, scale, delta, dimension );
  385. return;
  386. }
  387. if ( ps_debug_no_horz_hints && dimension == 1 )
  388. {
  389. ps2_simple_scale( table, scale, delta, dimension );
  390. return;
  391. }
  392. #endif
  393. hint = table->hints;
  394. count = table->max_hints;
  395. for ( ; count > 0; count--, hint++ )
  396. psh2_hint_align( hint, globals, dimension );
  397. }
  398. /*************************************************************************/
  399. /*************************************************************************/
  400. /***** *****/
  401. /***** POINTS INTERPOLATION ROUTINES *****/
  402. /***** *****/
  403. /*************************************************************************/
  404. /*************************************************************************/
  405. #define PSH2_ZONE_MIN -3200000
  406. #define PSH2_ZONE_MAX +3200000
  407. #define xxDEBUG_ZONES
  408. #ifdef DEBUG_ZONES
  409. #include <stdio.h>
  410. static void
  411. psh2_print_zone( PSH2_Zone zone )
  412. {
  413. printf( "zone [scale,delta,min,max] = [%.3f,%.3f,%d,%d]\n",
  414. zone->scale/65536.0,
  415. zone->delta/64.0,
  416. zone->min,
  417. zone->max );
  418. }
  419. #else
  420. #define psh2_print_zone( x ) do { } while ( 0 )
  421. #endif
  422. #if 0
  423. /* setup interpolation zones once the hints have been grid-fitted */
  424. /* by the optimizer */
  425. static void
  426. psh2_hint_table_setup_zones( PSH2_Hint_Table table,
  427. FT_Fixed scale,
  428. FT_Fixed delta )
  429. {
  430. FT_UInt count;
  431. PSH2_Zone zone;
  432. PSH2_Hint *sort, hint, hint2;
  433. zone = table->zones;
  434. /* special case, no hints defined */
  435. if ( table->num_hints == 0 )
  436. {
  437. zone->scale = scale;
  438. zone->delta = delta;
  439. zone->min = PSH2_ZONE_MIN;
  440. zone->max = PSH2_ZONE_MAX;
  441. table->num_zones = 1;
  442. table->zone = zone;
  443. return;
  444. }
  445. /* the first zone is before the first hint */
  446. /* x' = (x-x0)*s + x0' = x*s + ( x0' - x0*s ) */
  447. sort = table->sort;
  448. hint = sort[0];
  449. zone->scale = scale;
  450. zone->delta = hint->cur_pos - FT_MulFix( hint->org_pos, scale );
  451. zone->min = PSH2_ZONE_MIN;
  452. zone->max = hint->org_pos;
  453. psh2_print_zone( zone );
  454. zone++;
  455. for ( count = table->num_hints; count > 0; count-- )
  456. {
  457. FT_Fixed scale2;
  458. if ( hint->org_len > 0 )
  459. {
  460. /* setup a zone for inner-stem interpolation */
  461. /* (x' - x0') = (x - x0)*(x1'-x0')/(x1-x0) */
  462. /* x' = x*s2 + x0' - x0*s2 */
  463. scale2 = FT_DivFix( hint->cur_len, hint->org_len );
  464. zone->scale = scale2;
  465. zone->min = hint->org_pos;
  466. zone->max = hint->org_pos + hint->org_len;
  467. zone->delta = hint->cur_pos - FT_MulFix( zone->min, scale2 );
  468. psh2_print_zone( zone );
  469. zone++;
  470. }
  471. if ( count == 1 )
  472. break;
  473. sort++;
  474. hint2 = sort[0];
  475. /* setup zone for inter-stem interpolation */
  476. /* (x'-x1') = (x-x1)*(x2'-x1')/(x2-x1) */
  477. /* x' = x*s3 + x1' - x1*s3 */
  478. scale2 = FT_DivFix( hint2->cur_pos - (hint->cur_pos + hint->cur_len),
  479. hint2->org_pos - (hint->org_pos + hint->org_len) );
  480. zone->scale = scale2;
  481. zone->min = hint->org_pos + hint->org_len;
  482. zone->max = hint2->org_pos;
  483. zone->delta = hint->cur_pos + hint->cur_len -
  484. FT_MulFix( zone->min, scale2 );
  485. psh2_print_zone( zone );
  486. zone++;
  487. hint = hint2;
  488. }
  489. /* the last zone */
  490. zone->scale = scale;
  491. zone->min = hint->org_pos + hint->org_len;
  492. zone->max = PSH2_ZONE_MAX;
  493. zone->delta = hint->cur_pos + hint->cur_len -
  494. FT_MulFix( zone->min, scale );
  495. psh2_print_zone( zone );
  496. zone++;
  497. table->num_zones = zone - table->zones;
  498. table->zone = table->zones;
  499. }
  500. #endif
  501. #if 0
  502. /* tune a single coordinate with the current interpolation zones */
  503. static FT_Pos
  504. psh2_hint_table_tune_coord( PSH2_Hint_Table table,
  505. FT_Int coord )
  506. {
  507. PSH2_Zone zone;
  508. zone = table->zone;
  509. if ( coord < zone->min )
  510. {
  511. do
  512. {
  513. if ( zone == table->zones )
  514. break;
  515. zone--;
  516. } while ( coord < zone->min );
  517. table->zone = zone;
  518. }
  519. else if ( coord > zone->max )
  520. {
  521. do
  522. {
  523. if ( zone == table->zones + table->num_zones - 1 )
  524. break;
  525. zone++;
  526. } while ( coord > zone->max );
  527. table->zone = zone;
  528. }
  529. return FT_MulFix( coord, zone->scale ) + zone->delta;
  530. }
  531. #endif
  532. #if 0
  533. /* tune a given outline with current interpolation zones */
  534. /* the function only works in a single dimension.. */
  535. static void
  536. psh2_hint_table_tune_outline( PSH2_Hint_Table table,
  537. FT_Outline* outline,
  538. PSH_Globals globals,
  539. FT_Int dimension )
  540. {
  541. FT_UInt count, first, last;
  542. PS_Mask_Table hint_masks = table->hint_masks;
  543. PS_Mask mask;
  544. PSH_Dimension dim = &globals->dimension[dimension];
  545. FT_Fixed scale = dim->scale_mult;
  546. FT_Fixed delta = dim->scale_delta;
  547. if ( hint_masks && hint_masks->num_masks > 0 )
  548. {
  549. first = 0;
  550. mask = hint_masks->masks;
  551. count = hint_masks->num_masks;
  552. for ( ; count > 0; count--, mask++ )
  553. {
  554. last = mask->end_point;
  555. if ( last > first )
  556. {
  557. FT_Vector* vec;
  558. FT_Int count2;
  559. psh2_hint_table_activate_mask( table, mask );
  560. psh2_hint_table_optimize( table, globals, outline, dimension );
  561. psh2_hint_table_setup_zones( table, scale, delta );
  562. last = mask->end_point;
  563. vec = outline->points + first;
  564. count2 = last - first;
  565. for ( ; count2 > 0; count2--, vec++ )
  566. {
  567. FT_Pos x, *px;
  568. px = dimension ? &vec->y : &vec->x;
  569. x = *px;
  570. *px = psh2_hint_table_tune_coord( table, (FT_Int)x );
  571. }
  572. }
  573. first = last;
  574. }
  575. }
  576. else /* no hints in this glyph, simply scale the outline */
  577. {
  578. FT_Vector* vec;
  579. vec = outline->points;
  580. count = outline->n_points;
  581. if ( dimension == 0 )
  582. {
  583. for ( ; count > 0; count--, vec++ )
  584. vec->x = FT_MulFix( vec->x, scale ) + delta;
  585. }
  586. else
  587. {
  588. for ( ; count > 0; count--, vec++ )
  589. vec->y = FT_MulFix( vec->y, scale ) + delta;
  590. }
  591. }
  592. }
  593. #endif
  594. /*************************************************************************/
  595. /*************************************************************************/
  596. /***** *****/
  597. /***** HINTER GLYPH MANAGEMENT *****/
  598. /***** *****/
  599. /*************************************************************************/
  600. /*************************************************************************/
  601. static int
  602. psh2_point_is_extremum( PSH2_Point point )
  603. {
  604. PSH2_Point before = point;
  605. PSH2_Point after = point;
  606. FT_Pos d_before;
  607. FT_Pos d_after;
  608. do
  609. {
  610. before = before->prev;
  611. if ( before == point )
  612. return 0;
  613. d_before = before->org_u - point->org_u;
  614. } while ( d_before == 0 );
  615. do
  616. {
  617. after = after->next;
  618. if ( after == point )
  619. return 0;
  620. d_after = after->org_u - point->org_u;
  621. } while ( d_after == 0 );
  622. return ( ( d_before > 0 && d_after > 0 ) ||
  623. ( d_before < 0 && d_after < 0 ) );
  624. }
  625. static void
  626. psh2_glyph_done( PSH2_Glyph glyph )
  627. {
  628. FT_Memory memory = glyph->memory;
  629. psh2_hint_table_done( &glyph->hint_tables[1], memory );
  630. psh2_hint_table_done( &glyph->hint_tables[0], memory );
  631. FT_FREE( glyph->points );
  632. FT_FREE( glyph->contours );
  633. glyph->num_points = 0;
  634. glyph->num_contours = 0;
  635. glyph->memory = 0;
  636. }
  637. static int
  638. psh2_compute_dir( FT_Pos dx,
  639. FT_Pos dy )
  640. {
  641. FT_Pos ax, ay;
  642. int result = PSH2_DIR_NONE;
  643. ax = ( dx >= 0 ) ? dx : -dx;
  644. ay = ( dy >= 0 ) ? dy : -dy;
  645. if ( ay * 12 < ax )
  646. {
  647. /* |dy| <<< |dx| means a near-horizontal segment */
  648. result = ( dx >= 0 ) ? PSH2_DIR_RIGHT : PSH2_DIR_LEFT;
  649. }
  650. else if ( ax * 12 < ay )
  651. {
  652. /* |dx| <<< |dy| means a near-vertical segment */
  653. result = ( dy >= 0 ) ? PSH2_DIR_UP : PSH2_DIR_DOWN;
  654. }
  655. return result;
  656. }
  657. static FT_Error
  658. psh2_glyph_init( PSH2_Glyph glyph,
  659. FT_Outline* outline,
  660. PS_Hints ps_hints,
  661. PSH_Globals globals )
  662. {
  663. FT_Error error;
  664. FT_Memory memory;
  665. /* clear all fields */
  666. FT_MEM_ZERO( glyph, sizeof ( *glyph ) );
  667. memory = globals->memory;
  668. /* allocate and setup points + contours arrays */
  669. if ( FT_NEW_ARRAY( glyph->points, outline->n_points ) ||
  670. FT_NEW_ARRAY( glyph->contours, outline->n_contours ) )
  671. goto Exit;
  672. glyph->num_points = outline->n_points;
  673. glyph->num_contours = outline->n_contours;
  674. {
  675. FT_UInt first = 0, next, n;
  676. PSH2_Point points = glyph->points;
  677. PSH2_Contour contour = glyph->contours;
  678. for ( n = 0; n < glyph->num_contours; n++ )
  679. {
  680. FT_Int count;
  681. PSH2_Point point;
  682. next = outline->contours[n] + 1;
  683. count = next - first;
  684. contour->start = points + first;
  685. contour->count = (FT_UInt)count;
  686. if ( count > 0 )
  687. {
  688. point = points + first;
  689. point->prev = points + next - 1;
  690. point->contour = contour;
  691. for ( ; count > 1; count-- )
  692. {
  693. point[0].next = point + 1;
  694. point[1].prev = point;
  695. point++;
  696. point->contour = contour;
  697. }
  698. point->next = points + first;
  699. }
  700. contour++;
  701. first = next;
  702. }
  703. }
  704. {
  705. PSH2_Point points = glyph->points;
  706. PSH2_Point point = points;
  707. FT_Vector* vec = outline->points;
  708. FT_UInt n;
  709. for ( n = 0; n < glyph->num_points; n++, point++ )
  710. {
  711. FT_Int n_prev = point->prev - points;
  712. FT_Int n_next = point->next - points;
  713. FT_Pos dxi, dyi, dxo, dyo;
  714. if ( !( outline->tags[n] & FT_CURVE_TAG_ON ) )
  715. point->flags = PSH2_POINT_OFF;
  716. dxi = vec[n].x - vec[n_prev].x;
  717. dyi = vec[n].y - vec[n_prev].y;
  718. point->dir_in = (FT_Char)psh2_compute_dir( dxi, dyi );
  719. dxo = vec[n_next].x - vec[n].x;
  720. dyo = vec[n_next].y - vec[n].y;
  721. point->dir_out = (FT_Char)psh2_compute_dir( dxo, dyo );
  722. /* detect smooth points */
  723. if ( point->flags & PSH2_POINT_OFF )
  724. point->flags |= PSH2_POINT_SMOOTH;
  725. else if ( point->dir_in != PSH2_DIR_NONE ||
  726. point->dir_out != PSH2_DIR_NONE )
  727. {
  728. if ( point->dir_in == point->dir_out )
  729. point->flags |= PSH2_POINT_SMOOTH;
  730. }
  731. else
  732. {
  733. FT_Angle angle_in, angle_out, diff;
  734. angle_in = FT_Atan2( dxi, dyi );
  735. angle_out = FT_Atan2( dxo, dyo );
  736. diff = angle_in - angle_out;
  737. if ( diff < 0 )
  738. diff = -diff;
  739. if ( diff > FT_ANGLE_PI )
  740. diff = FT_ANGLE_2PI - diff;
  741. if ( diff < FT_ANGLE_PI / 16 )
  742. point->flags |= PSH2_POINT_SMOOTH;
  743. }
  744. }
  745. }
  746. glyph->memory = memory;
  747. glyph->outline = outline;
  748. glyph->globals = globals;
  749. /* now deal with hints tables */
  750. error = psh2_hint_table_init( &glyph->hint_tables [0],
  751. &ps_hints->dimension[0].hints,
  752. &ps_hints->dimension[0].masks,
  753. &ps_hints->dimension[0].counters,
  754. memory );
  755. if ( error )
  756. goto Exit;
  757. error = psh2_hint_table_init( &glyph->hint_tables [1],
  758. &ps_hints->dimension[1].hints,
  759. &ps_hints->dimension[1].masks,
  760. &ps_hints->dimension[1].counters,
  761. memory );
  762. if ( error )
  763. goto Exit;
  764. Exit:
  765. return error;
  766. }
  767. /* load outline point coordinates into hinter glyph */
  768. static void
  769. psh2_glyph_load_points( PSH2_Glyph glyph,
  770. FT_Int dimension )
  771. {
  772. FT_Vector* vec = glyph->outline->points;
  773. PSH2_Point point = glyph->points;
  774. FT_UInt count = glyph->num_points;
  775. for ( ; count > 0; count--, point++, vec++ )
  776. {
  777. point->flags &= PSH2_POINT_OFF | PSH2_POINT_SMOOTH;
  778. point->hint = 0;
  779. if ( dimension == 0 )
  780. point->org_u = vec->x;
  781. else
  782. point->org_u = vec->y;
  783. #ifdef DEBUG_HINTER
  784. point->org_x = vec->x;
  785. point->org_y = vec->y;
  786. #endif
  787. }
  788. }
  789. /* save hinted point coordinates back to outline */
  790. static void
  791. psh2_glyph_save_points( PSH2_Glyph glyph,
  792. FT_Int dimension )
  793. {
  794. FT_UInt n;
  795. PSH2_Point point = glyph->points;
  796. FT_Vector* vec = glyph->outline->points;
  797. char* tags = glyph->outline->tags;
  798. for ( n = 0; n < glyph->num_points; n++ )
  799. {
  800. if ( dimension == 0 )
  801. vec[n].x = point->cur_u;
  802. else
  803. vec[n].y = point->cur_u;
  804. if ( psh2_point_is_strong( point ) )
  805. tags[n] |= (char)( ( dimension == 0 ) ? 32 : 64 );
  806. #ifdef DEBUG_HINTER
  807. if ( dimension == 0 )
  808. {
  809. point->cur_x = point->cur_u;
  810. point->flags_x = point->flags;
  811. }
  812. else
  813. {
  814. point->cur_y = point->cur_u;
  815. point->flags_y = point->flags;
  816. }
  817. #endif
  818. point++;
  819. }
  820. }
  821. #define PSH2_STRONG_THRESHOLD 10
  822. static void
  823. psh2_hint_table_find_strong_point( PSH2_Hint_Table table,
  824. PSH2_Point point,
  825. FT_Int major_dir )
  826. {
  827. PSH2_Hint* sort = table->sort;
  828. FT_UInt num_hints = table->num_hints;
  829. for ( ; num_hints > 0; num_hints--, sort++ )
  830. {
  831. PSH2_Hint hint = sort[0];
  832. if ( ABS( point->dir_in ) == major_dir ||
  833. ABS( point->dir_out ) == major_dir )
  834. {
  835. FT_Pos d;
  836. d = point->org_u - hint->org_pos;
  837. if ( ABS( d ) < PSH2_STRONG_THRESHOLD )
  838. {
  839. Is_Strong:
  840. psh2_point_set_strong( point );
  841. point->hint = hint;
  842. break;
  843. }
  844. d -= hint->org_len;
  845. if ( ABS( d ) < PSH2_STRONG_THRESHOLD )
  846. goto Is_Strong;
  847. }
  848. #if 1
  849. if ( point->org_u >= hint->org_pos &&
  850. point->org_u <= hint->org_pos + hint->org_len &&
  851. psh2_point_is_extremum( point ) )
  852. {
  853. /* attach to hint, but don't mark as strong */
  854. point->hint = hint;
  855. break;
  856. }
  857. #endif
  858. }
  859. }
  860. /* find strong points in a glyph */
  861. static void
  862. psh2_glyph_find_strong_points( PSH2_Glyph glyph,
  863. FT_Int dimension )
  864. {
  865. /* a point is strong if it is located on a stem */
  866. /* edge and has an "in" or "out" tangent to the hint's direction */
  867. {
  868. PSH2_Hint_Table table = &glyph->hint_tables[dimension];
  869. PS_Mask mask = table->hint_masks->masks;
  870. FT_UInt num_masks = table->hint_masks->num_masks;
  871. FT_UInt first = 0;
  872. FT_Int major_dir = dimension == 0 ? PSH2_DIR_UP : PSH2_DIR_RIGHT;
  873. /* process secondary hints to "selected" points */
  874. if ( num_masks > 1 && glyph->num_points > 0 )
  875. {
  876. first = mask->end_point;
  877. mask++;
  878. for ( ; num_masks > 1; num_masks--, mask++ )
  879. {
  880. FT_UInt next;
  881. FT_Int count;
  882. next = mask->end_point;
  883. count = next - first;
  884. if ( count > 0 )
  885. {
  886. PSH2_Point point = glyph->points + first;
  887. psh2_hint_table_activate_mask( table, mask );
  888. for ( ; count > 0; count--, point++ )
  889. psh2_hint_table_find_strong_point( table, point, major_dir );
  890. }
  891. first = next;
  892. }
  893. }
  894. /* process primary hints for all points */
  895. if ( num_masks == 1 )
  896. {
  897. FT_UInt count = glyph->num_points;
  898. PSH2_Point point = glyph->points;
  899. psh2_hint_table_activate_mask( table, table->hint_masks->masks );
  900. for ( ; count > 0; count--, point++ )
  901. {
  902. if ( !psh2_point_is_strong( point ) )
  903. psh2_hint_table_find_strong_point( table, point, major_dir );
  904. }
  905. }
  906. /* now, certain points may have been attached to hint and */
  907. /* not marked as strong; update their flags then */
  908. {
  909. FT_UInt count = glyph->num_points;
  910. PSH2_Point point = glyph->points;
  911. for ( ; count > 0; count--, point++ )
  912. if ( point->hint && !psh2_point_is_strong( point ) )
  913. psh2_point_set_strong( point );
  914. }
  915. }
  916. }
  917. /* interpolate strong points with the help of hinted coordinates */
  918. static void
  919. psh2_glyph_interpolate_strong_points( PSH2_Glyph glyph,
  920. FT_Int dimension )
  921. {
  922. PSH_Dimension dim = &glyph->globals->dimension[dimension];
  923. FT_Fixed scale = dim->scale_mult;
  924. {
  925. FT_UInt count = glyph->num_points;
  926. PSH2_Point point = glyph->points;
  927. for ( ; count > 0; count--, point++ )
  928. {
  929. PSH2_Hint hint = point->hint;
  930. if ( hint )
  931. {
  932. FT_Pos delta;
  933. delta = point->org_u - hint->org_pos;
  934. if ( delta <= 0 )
  935. point->cur_u = hint->cur_pos + FT_MulFix( delta, scale );
  936. else if ( delta >= hint->org_len )
  937. point->cur_u = hint->cur_pos + hint->cur_len +
  938. FT_MulFix( delta - hint->org_len, scale );
  939. else if ( hint->org_len > 0 )
  940. point->cur_u = hint->cur_pos +
  941. FT_MulDiv( delta, hint->cur_len, hint->org_len );
  942. else
  943. point->cur_u = hint->cur_pos;
  944. psh2_point_set_fitted( point );
  945. }
  946. }
  947. }
  948. }
  949. static void
  950. psh2_glyph_interpolate_normal_points( PSH2_Glyph glyph,
  951. FT_Int dimension )
  952. {
  953. #if 1
  954. PSH_Dimension dim = &glyph->globals->dimension[dimension];
  955. FT_Fixed scale = dim->scale_mult;
  956. /* first technique: a point is strong if it is a local extrema */
  957. {
  958. FT_UInt count = glyph->num_points;
  959. PSH2_Point point = glyph->points;
  960. for ( ; count > 0; count--, point++ )
  961. {
  962. if ( psh2_point_is_strong( point ) )
  963. continue;
  964. /* sometimes, some local extremas are smooth points */
  965. if ( psh2_point_is_smooth( point ) )
  966. {
  967. if ( point->dir_in == PSH2_DIR_NONE ||
  968. point->dir_in != point->dir_out )
  969. continue;
  970. if ( !psh2_point_is_extremum( point ) )
  971. continue;
  972. point->flags &= ~PSH2_POINT_SMOOTH;
  973. }
  974. /* find best enclosing point coordinates */
  975. {
  976. PSH2_Point before = 0;
  977. PSH2_Point after = 0;
  978. FT_Pos diff_before = -32000;
  979. FT_Pos diff_after = 32000;
  980. FT_Pos u = point->org_u;
  981. FT_Int count2 = glyph->num_points;
  982. PSH2_Point cur = glyph->points;
  983. for ( ; count2 > 0; count2--, cur++ )
  984. {
  985. if ( psh2_point_is_strong( cur ) )
  986. {
  987. FT_Pos diff = cur->org_u - u;;
  988. if ( diff <= 0 )
  989. {
  990. if ( diff > diff_before )
  991. {
  992. diff_before = diff;
  993. before = cur;
  994. }
  995. }
  996. else if ( diff >= 0 )
  997. {
  998. if ( diff < diff_after )
  999. {
  1000. diff_after = diff;
  1001. after = cur;
  1002. }
  1003. }
  1004. }
  1005. }
  1006. if ( !before )
  1007. {
  1008. if ( !after )
  1009. continue;
  1010. /* we are before the first strong point coordinate; */
  1011. /* simply translate the point */
  1012. point->cur_u = after->cur_u +
  1013. FT_MulFix( point->org_u - after->org_u, scale );
  1014. }
  1015. else if ( !after )
  1016. {
  1017. /* we are after the last strong point coordinate; */
  1018. /* simply translate the point */
  1019. point->cur_u = before->cur_u +
  1020. FT_MulFix( point->org_u - before->org_u, scale );
  1021. }
  1022. else
  1023. {
  1024. if ( diff_before == 0 )
  1025. point->cur_u = before->cur_u;
  1026. else if ( diff_after == 0 )
  1027. point->cur_u = after->cur_u;
  1028. else
  1029. point->cur_u = before->cur_u +
  1030. FT_MulDiv( u - before->org_u,
  1031. after->cur_u - before->cur_u,
  1032. after->org_u - before->org_u );
  1033. }
  1034. psh2_point_set_fitted( point );
  1035. }
  1036. }
  1037. }
  1038. #endif
  1039. }
  1040. /* interpolate other points */
  1041. static void
  1042. psh2_glyph_interpolate_other_points( PSH2_Glyph glyph,
  1043. FT_Int dimension )
  1044. {
  1045. PSH_Dimension dim = &glyph->globals->dimension[dimension];
  1046. FT_Fixed scale = dim->scale_mult;
  1047. FT_Fixed delta = dim->scale_delta;
  1048. PSH2_Contour contour = glyph->contours;
  1049. FT_UInt num_contours = glyph->num_contours;
  1050. for ( ; num_contours > 0; num_contours--, contour++ )
  1051. {
  1052. PSH2_Point start = contour->start;
  1053. PSH2_Point first, next, point;
  1054. FT_UInt fit_count;
  1055. /* count the number of strong points in this contour */
  1056. next = start + contour->count;
  1057. fit_count = 0;
  1058. first = 0;
  1059. for ( point = start; point < next; point++ )
  1060. if ( psh2_point_is_fitted( point ) )
  1061. {
  1062. if ( !first )
  1063. first = point;
  1064. fit_count++;
  1065. }
  1066. /* if there are less than 2 fitted points in the contour, we */
  1067. /* simply scale and eventually translate the contour points */
  1068. if ( fit_count < 2 )
  1069. {
  1070. if ( fit_count == 1 )
  1071. delta = first->cur_u - FT_MulFix( first->org_u, scale );
  1072. for ( point = start; point < next; point++ )
  1073. if ( point != first )
  1074. point->cur_u = FT_MulFix( point->org_u, scale ) + delta;
  1075. goto Next_Contour;
  1076. }
  1077. /* there are more than 2 strong points in this contour; we */
  1078. /* need to interpolate weak points between them */
  1079. start = first;
  1080. do
  1081. {
  1082. point = first;
  1083. /* skip consecutive fitted points */
  1084. for (;;)
  1085. {
  1086. next = first->next;
  1087. if ( next == start )
  1088. goto Next_Contour;
  1089. if ( !psh2_point_is_fitted( next ) )
  1090. break;
  1091. first = next;
  1092. }
  1093. /* find next fitted point after unfitted one */
  1094. for (;;)
  1095. {
  1096. next = next->next;
  1097. if ( psh2_point_is_fitted( next ) )
  1098. break;
  1099. }
  1100. /* now interpolate between them */
  1101. {
  1102. FT_Pos org_a, org_ab, cur_a, cur_ab;
  1103. FT_Pos org_c, org_ac, cur_c;
  1104. FT_Fixed scale_ab;
  1105. if ( first->org_u <= next->org_u )
  1106. {
  1107. org_a = first->org_u;
  1108. cur_a = first->cur_u;
  1109. org_ab = next->org_u - org_a;
  1110. cur_ab = next->cur_u - cur_a;
  1111. }
  1112. else
  1113. {
  1114. org_a = next->org_u;
  1115. cur_a = next->cur_u;
  1116. org_ab = first->org_u - org_a;
  1117. cur_ab = first->cur_u - cur_a;
  1118. }
  1119. scale_ab = 0x10000L;
  1120. if ( org_ab > 0 )
  1121. scale_ab = FT_DivFix( cur_ab, org_ab );
  1122. point = first->next;
  1123. do
  1124. {
  1125. org_c = point->org_u;
  1126. org_ac = org_c - org_a;
  1127. if ( org_ac <= 0 )
  1128. {
  1129. /* on the left of the interpolation zone */
  1130. cur_c = cur_a + FT_MulFix( org_ac, scale );
  1131. }
  1132. else if ( org_ac >= org_ab )
  1133. {
  1134. /* on the right on the interpolation zone */
  1135. cur_c = cur_a + cur_ab + FT_MulFix( org_ac - org_ab, scale );
  1136. }
  1137. else
  1138. {
  1139. /* within the interpolation zone */
  1140. cur_c = cur_a + FT_MulFix( org_ac, scale_ab );
  1141. }
  1142. point->cur_u = cur_c;
  1143. point = point->next;
  1144. } while ( point != next );
  1145. }
  1146. /* keep going until all points in the contours have been processed */
  1147. first = next;
  1148. } while ( first != start );
  1149. Next_Contour:
  1150. ;
  1151. }
  1152. }
  1153. /*************************************************************************/
  1154. /*************************************************************************/
  1155. /***** *****/
  1156. /***** HIGH-LEVEL INTERFACE *****/
  1157. /***** *****/
  1158. /*************************************************************************/
  1159. /*************************************************************************/
  1160. FT_Error
  1161. ps2_hints_apply( PS_Hints ps_hints,
  1162. FT_Outline* outline,
  1163. PSH_Globals globals,
  1164. FT_Render_Mode hint_mode )
  1165. {
  1166. PSH2_GlyphRec glyphrec;
  1167. PSH2_Glyph glyph = &glyphrec;
  1168. FT_Error error;
  1169. #ifdef DEBUG_HINTER
  1170. FT_Memory memory;
  1171. #endif
  1172. FT_Int dimension;
  1173. FT_UNUSED( hint_mode );
  1174. #ifdef DEBUG_HINTER
  1175. memory = globals->memory;
  1176. if ( ps2_debug_glyph )
  1177. {
  1178. psh2_glyph_done( ps2_debug_glyph );
  1179. FT_FREE( ps2_debug_glyph );
  1180. }
  1181. if ( FT_NEW( glyph ) )
  1182. return error;
  1183. ps2_debug_glyph = glyph;
  1184. #endif
  1185. error = psh2_glyph_init( glyph, outline, ps_hints, globals );
  1186. if ( error )
  1187. goto Exit;
  1188. for ( dimension = 0; dimension < 2; dimension++ )
  1189. {
  1190. /* load outline coordinates into glyph */
  1191. psh2_glyph_load_points( glyph, dimension );
  1192. /* compute aligned stem/hints positions */
  1193. psh2_hint_table_align_hints( &glyph->hint_tables[dimension],
  1194. glyph->globals,
  1195. dimension );
  1196. /* find strong points, align them, then interpolate others */
  1197. psh2_glyph_find_strong_points( glyph, dimension );
  1198. psh2_glyph_interpolate_strong_points( glyph, dimension );
  1199. psh2_glyph_interpolate_normal_points( glyph, dimension );
  1200. psh2_glyph_interpolate_other_points( glyph, dimension );
  1201. /* save hinted coordinates back to outline */
  1202. psh2_glyph_save_points( glyph, dimension );
  1203. }
  1204. Exit:
  1205. #ifndef DEBUG_HINTER
  1206. psh2_glyph_done( glyph );
  1207. #endif
  1208. return error;
  1209. }
  1210. /* END */