PageRenderTime 62ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/src/freetype/src/autofit/aflatin.c

https://bitbucket.org/cabalistic/ogredeps/
C | 2359 lines | 1553 code | 501 blank | 305 comment | 375 complexity | bb350ae67fb1fb3ee0d2b4151adac33c MD5 | raw file
Possible License(s): LGPL-3.0, BSD-3-Clause, CPL-1.0, Unlicense, GPL-2.0, GPL-3.0, LGPL-2.0, MPL-2.0-no-copyleft-exception, BSD-2-Clause, LGPL-2.1
  1. /***************************************************************************/
  2. /* */
  3. /* aflatin.c */
  4. /* */
  5. /* Auto-fitter hinting routines for latin script (body). */
  6. /* */
  7. /* Copyright 2003-2011 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_ADVANCES_H
  19. #include FT_INTERNAL_DEBUG_H
  20. #include "aflatin.h"
  21. #include "aferrors.h"
  22. #ifdef AF_CONFIG_OPTION_USE_WARPER
  23. #include "afwarp.h"
  24. #endif
  25. /*************************************************************************/
  26. /* */
  27. /* The macro FT_COMPONENT is used in trace mode. It is an implicit */
  28. /* parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log */
  29. /* messages during execution. */
  30. /* */
  31. #undef FT_COMPONENT
  32. #define FT_COMPONENT trace_aflatin
  33. /*************************************************************************/
  34. /*************************************************************************/
  35. /***** *****/
  36. /***** L A T I N G L O B A L M E T R I C S *****/
  37. /***** *****/
  38. /*************************************************************************/
  39. /*************************************************************************/
  40. /* Find segments and links, compute all stem widths, and initialize */
  41. /* standard width and height for the glyph with given charcode. */
  42. FT_LOCAL_DEF( void )
  43. af_latin_metrics_init_widths( AF_LatinMetrics metrics,
  44. FT_Face face,
  45. FT_ULong charcode )
  46. {
  47. /* scan the array of segments in each direction */
  48. AF_GlyphHintsRec hints[1];
  49. af_glyph_hints_init( hints, face->memory );
  50. metrics->axis[AF_DIMENSION_HORZ].width_count = 0;
  51. metrics->axis[AF_DIMENSION_VERT].width_count = 0;
  52. {
  53. FT_Error error;
  54. FT_UInt glyph_index;
  55. int dim;
  56. AF_LatinMetricsRec dummy[1];
  57. AF_Scaler scaler = &dummy->root.scaler;
  58. glyph_index = FT_Get_Char_Index( face, charcode );
  59. if ( glyph_index == 0 )
  60. goto Exit;
  61. error = FT_Load_Glyph( face, glyph_index, FT_LOAD_NO_SCALE );
  62. if ( error || face->glyph->outline.n_points <= 0 )
  63. goto Exit;
  64. FT_ZERO( dummy );
  65. dummy->units_per_em = metrics->units_per_em;
  66. scaler->x_scale = 0x10000L;
  67. scaler->y_scale = 0x10000L;
  68. scaler->x_delta = 0;
  69. scaler->y_delta = 0;
  70. scaler->face = face;
  71. scaler->render_mode = FT_RENDER_MODE_NORMAL;
  72. scaler->flags = 0;
  73. af_glyph_hints_rescale( hints, (AF_ScriptMetrics)dummy );
  74. error = af_glyph_hints_reload( hints, &face->glyph->outline );
  75. if ( error )
  76. goto Exit;
  77. for ( dim = 0; dim < AF_DIMENSION_MAX; dim++ )
  78. {
  79. AF_LatinAxis axis = &metrics->axis[dim];
  80. AF_AxisHints axhints = &hints->axis[dim];
  81. AF_Segment seg, limit, link;
  82. FT_UInt num_widths = 0;
  83. error = af_latin_hints_compute_segments( hints,
  84. (AF_Dimension)dim );
  85. if ( error )
  86. goto Exit;
  87. af_latin_hints_link_segments( hints,
  88. (AF_Dimension)dim );
  89. seg = axhints->segments;
  90. limit = seg + axhints->num_segments;
  91. for ( ; seg < limit; seg++ )
  92. {
  93. link = seg->link;
  94. /* we only consider stem segments there! */
  95. if ( link && link->link == seg && link > seg )
  96. {
  97. FT_Pos dist;
  98. dist = seg->pos - link->pos;
  99. if ( dist < 0 )
  100. dist = -dist;
  101. if ( num_widths < AF_LATIN_MAX_WIDTHS )
  102. axis->widths[num_widths++].org = dist;
  103. }
  104. }
  105. af_sort_widths( num_widths, axis->widths );
  106. axis->width_count = num_widths;
  107. }
  108. Exit:
  109. for ( dim = 0; dim < AF_DIMENSION_MAX; dim++ )
  110. {
  111. AF_LatinAxis axis = &metrics->axis[dim];
  112. FT_Pos stdw;
  113. stdw = ( axis->width_count > 0 )
  114. ? axis->widths[0].org
  115. : AF_LATIN_CONSTANT( metrics, 50 );
  116. /* let's try 20% of the smallest width */
  117. axis->edge_distance_threshold = stdw / 5;
  118. axis->standard_width = stdw;
  119. axis->extra_light = 0;
  120. }
  121. }
  122. af_glyph_hints_done( hints );
  123. }
  124. #define AF_LATIN_MAX_TEST_CHARACTERS 12
  125. static const char af_latin_blue_chars[AF_LATIN_MAX_BLUES]
  126. [AF_LATIN_MAX_TEST_CHARACTERS + 1] =
  127. {
  128. "THEZOCQS",
  129. "HEZLOCUS",
  130. "fijkdbh",
  131. "xzroesc",
  132. "xzroesc",
  133. "pqgjy"
  134. };
  135. /* Find all blue zones. Flat segments give the reference points, */
  136. /* round segments the overshoot positions. */
  137. static void
  138. af_latin_metrics_init_blues( AF_LatinMetrics metrics,
  139. FT_Face face )
  140. {
  141. FT_Pos flats [AF_LATIN_MAX_TEST_CHARACTERS];
  142. FT_Pos rounds[AF_LATIN_MAX_TEST_CHARACTERS];
  143. FT_Int num_flats;
  144. FT_Int num_rounds;
  145. FT_Int bb;
  146. AF_LatinBlue blue;
  147. FT_Error error;
  148. AF_LatinAxis axis = &metrics->axis[AF_DIMENSION_VERT];
  149. FT_GlyphSlot glyph = face->glyph;
  150. /* we compute the blues simply by loading each character from the */
  151. /* `af_latin_blue_chars[blues]' string, then finding its top-most or */
  152. /* bottom-most points (depending on `AF_IS_TOP_BLUE') */
  153. FT_TRACE5(( "blue zones computation\n" ));
  154. FT_TRACE5(( "------------------------------------------------\n" ));
  155. for ( bb = 0; bb < AF_LATIN_BLUE_MAX; bb++ )
  156. {
  157. const char* p = af_latin_blue_chars[bb];
  158. const char* limit = p + AF_LATIN_MAX_TEST_CHARACTERS;
  159. FT_Pos* blue_ref;
  160. FT_Pos* blue_shoot;
  161. FT_TRACE5(( "blue %3d: ", bb ));
  162. num_flats = 0;
  163. num_rounds = 0;
  164. for ( ; p < limit && *p; p++ )
  165. {
  166. FT_UInt glyph_index;
  167. FT_Pos best_y; /* same as points.y */
  168. FT_Int best_point, best_first, best_last;
  169. FT_Vector* points;
  170. FT_Bool round = 0;
  171. FT_TRACE5(( "'%c'", *p ));
  172. /* load the character in the face -- skip unknown or empty ones */
  173. glyph_index = FT_Get_Char_Index( face, (FT_UInt)*p );
  174. if ( glyph_index == 0 )
  175. continue;
  176. error = FT_Load_Glyph( face, glyph_index, FT_LOAD_NO_SCALE );
  177. if ( error || glyph->outline.n_points <= 0 )
  178. continue;
  179. /* now compute min or max point indices and coordinates */
  180. points = glyph->outline.points;
  181. best_point = -1;
  182. best_y = 0; /* make compiler happy */
  183. best_first = 0; /* ditto */
  184. best_last = 0; /* ditto */
  185. {
  186. FT_Int nn;
  187. FT_Int first = 0;
  188. FT_Int last = -1;
  189. for ( nn = 0;
  190. nn < glyph->outline.n_contours;
  191. first = last + 1, nn++ )
  192. {
  193. FT_Int old_best_point = best_point;
  194. FT_Int pp;
  195. last = glyph->outline.contours[nn];
  196. /* Avoid single-point contours since they are never rasterized. */
  197. /* In some fonts, they correspond to mark attachment points */
  198. /* which are way outside of the glyph's real outline. */
  199. if ( last <= first )
  200. continue;
  201. if ( AF_LATIN_IS_TOP_BLUE( bb ) )
  202. {
  203. for ( pp = first; pp <= last; pp++ )
  204. if ( best_point < 0 || points[pp].y > best_y )
  205. {
  206. best_point = pp;
  207. best_y = points[pp].y;
  208. }
  209. }
  210. else
  211. {
  212. for ( pp = first; pp <= last; pp++ )
  213. if ( best_point < 0 || points[pp].y < best_y )
  214. {
  215. best_point = pp;
  216. best_y = points[pp].y;
  217. }
  218. }
  219. if ( best_point != old_best_point )
  220. {
  221. best_first = first;
  222. best_last = last;
  223. }
  224. }
  225. FT_TRACE5(( "%5d", best_y ));
  226. }
  227. /* now check whether the point belongs to a straight or round */
  228. /* segment; we first need to find in which contour the extremum */
  229. /* lies, then inspect its previous and next points */
  230. if ( best_point >= 0 )
  231. {
  232. FT_Int prev, next;
  233. FT_Pos dist;
  234. /* now look for the previous and next points that are not on the */
  235. /* same Y coordinate. Threshold the `closeness'... */
  236. prev = best_point;
  237. next = prev;
  238. do
  239. {
  240. if ( prev > best_first )
  241. prev--;
  242. else
  243. prev = best_last;
  244. dist = points[prev].y - best_y;
  245. if ( dist < -5 || dist > 5 )
  246. break;
  247. } while ( prev != best_point );
  248. do
  249. {
  250. if ( next < best_last )
  251. next++;
  252. else
  253. next = best_first;
  254. dist = points[next].y - best_y;
  255. if ( dist < -5 || dist > 5 )
  256. break;
  257. } while ( next != best_point );
  258. /* now set the `round' flag depending on the segment's kind */
  259. round = FT_BOOL(
  260. FT_CURVE_TAG( glyph->outline.tags[prev] ) != FT_CURVE_TAG_ON ||
  261. FT_CURVE_TAG( glyph->outline.tags[next] ) != FT_CURVE_TAG_ON );
  262. FT_TRACE5(( "%c ", round ? 'r' : 'f' ));
  263. }
  264. if ( round )
  265. rounds[num_rounds++] = best_y;
  266. else
  267. flats[num_flats++] = best_y;
  268. }
  269. FT_TRACE5(( "\n" ));
  270. if ( num_flats == 0 && num_rounds == 0 )
  271. {
  272. /*
  273. * we couldn't find a single glyph to compute this blue zone,
  274. * we will simply ignore it then
  275. */
  276. FT_TRACE5(( "empty\n" ));
  277. continue;
  278. }
  279. /* we have computed the contents of the `rounds' and `flats' tables, */
  280. /* now determine the reference and overshoot position of the blue -- */
  281. /* we simply take the median value after a simple sort */
  282. af_sort_pos( num_rounds, rounds );
  283. af_sort_pos( num_flats, flats );
  284. blue = &axis->blues[axis->blue_count];
  285. blue_ref = &blue->ref.org;
  286. blue_shoot = &blue->shoot.org;
  287. axis->blue_count++;
  288. if ( num_flats == 0 )
  289. {
  290. *blue_ref =
  291. *blue_shoot = rounds[num_rounds / 2];
  292. }
  293. else if ( num_rounds == 0 )
  294. {
  295. *blue_ref =
  296. *blue_shoot = flats[num_flats / 2];
  297. }
  298. else
  299. {
  300. *blue_ref = flats[num_flats / 2];
  301. *blue_shoot = rounds[num_rounds / 2];
  302. }
  303. /* there are sometimes problems: if the overshoot position of top */
  304. /* zones is under its reference position, or the opposite for bottom */
  305. /* zones. We must thus check everything there and correct the errors */
  306. if ( *blue_shoot != *blue_ref )
  307. {
  308. FT_Pos ref = *blue_ref;
  309. FT_Pos shoot = *blue_shoot;
  310. FT_Bool over_ref = FT_BOOL( shoot > ref );
  311. if ( AF_LATIN_IS_TOP_BLUE( bb ) ^ over_ref )
  312. *blue_ref =
  313. *blue_shoot = ( shoot + ref ) / 2;
  314. }
  315. blue->flags = 0;
  316. if ( AF_LATIN_IS_TOP_BLUE( bb ) )
  317. blue->flags |= AF_LATIN_BLUE_TOP;
  318. /*
  319. * The following flag is used later to adjust the y and x scales
  320. * in order to optimize the pixel grid alignment of the top of small
  321. * letters.
  322. */
  323. if ( bb == AF_LATIN_BLUE_SMALL_TOP )
  324. blue->flags |= AF_LATIN_BLUE_ADJUSTMENT;
  325. FT_TRACE5(( "-- ref = %ld, shoot = %ld\n", *blue_ref, *blue_shoot ));
  326. }
  327. FT_TRACE5(( "\n" ));
  328. return;
  329. }
  330. /* Check whether all ASCII digits have the same advance width. */
  331. FT_LOCAL_DEF( void )
  332. af_latin_metrics_check_digits( AF_LatinMetrics metrics,
  333. FT_Face face )
  334. {
  335. FT_UInt i;
  336. FT_Bool started = 0, same_width = 1;
  337. FT_Fixed advance, old_advance = 0;
  338. /* digit `0' is 0x30 in all supported charmaps */
  339. for ( i = 0x30; i <= 0x39; i++ )
  340. {
  341. FT_UInt glyph_index;
  342. glyph_index = FT_Get_Char_Index( face, i );
  343. if ( glyph_index == 0 )
  344. continue;
  345. if ( FT_Get_Advance( face, glyph_index,
  346. FT_LOAD_NO_SCALE |
  347. FT_LOAD_NO_HINTING |
  348. FT_LOAD_IGNORE_TRANSFORM,
  349. &advance ) )
  350. continue;
  351. if ( started )
  352. {
  353. if ( advance != old_advance )
  354. {
  355. same_width = 0;
  356. break;
  357. }
  358. }
  359. else
  360. {
  361. old_advance = advance;
  362. started = 1;
  363. }
  364. }
  365. metrics->root.digits_have_same_width = same_width;
  366. }
  367. /* Initialize global metrics. */
  368. FT_LOCAL_DEF( FT_Error )
  369. af_latin_metrics_init( AF_LatinMetrics metrics,
  370. FT_Face face )
  371. {
  372. FT_Error error = AF_Err_Ok;
  373. FT_CharMap oldmap = face->charmap;
  374. FT_UInt ee;
  375. static const FT_Encoding latin_encodings[] =
  376. {
  377. FT_ENCODING_UNICODE,
  378. FT_ENCODING_APPLE_ROMAN,
  379. FT_ENCODING_ADOBE_STANDARD,
  380. FT_ENCODING_ADOBE_LATIN_1,
  381. FT_ENCODING_NONE /* end of list */
  382. };
  383. metrics->units_per_em = face->units_per_EM;
  384. /* do we have a latin charmap in there? */
  385. for ( ee = 0; latin_encodings[ee] != FT_ENCODING_NONE; ee++ )
  386. {
  387. error = FT_Select_Charmap( face, latin_encodings[ee] );
  388. if ( !error )
  389. break;
  390. }
  391. if ( !error )
  392. {
  393. /* For now, compute the standard width and height from the `o'. */
  394. af_latin_metrics_init_widths( metrics, face, 'o' );
  395. af_latin_metrics_init_blues( metrics, face );
  396. af_latin_metrics_check_digits( metrics, face );
  397. }
  398. FT_Set_Charmap( face, oldmap );
  399. return AF_Err_Ok;
  400. }
  401. /* Adjust scaling value, then scale and shift widths */
  402. /* and blue zones (if applicable) for given dimension. */
  403. static void
  404. af_latin_metrics_scale_dim( AF_LatinMetrics metrics,
  405. AF_Scaler scaler,
  406. AF_Dimension dim )
  407. {
  408. FT_Fixed scale;
  409. FT_Pos delta;
  410. AF_LatinAxis axis;
  411. FT_UInt nn;
  412. if ( dim == AF_DIMENSION_HORZ )
  413. {
  414. scale = scaler->x_scale;
  415. delta = scaler->x_delta;
  416. }
  417. else
  418. {
  419. scale = scaler->y_scale;
  420. delta = scaler->y_delta;
  421. }
  422. axis = &metrics->axis[dim];
  423. if ( axis->org_scale == scale && axis->org_delta == delta )
  424. return;
  425. axis->org_scale = scale;
  426. axis->org_delta = delta;
  427. /*
  428. * correct X and Y scale to optimize the alignment of the top of small
  429. * letters to the pixel grid
  430. */
  431. {
  432. AF_LatinAxis Axis = &metrics->axis[AF_DIMENSION_VERT];
  433. AF_LatinBlue blue = NULL;
  434. for ( nn = 0; nn < Axis->blue_count; nn++ )
  435. {
  436. if ( Axis->blues[nn].flags & AF_LATIN_BLUE_ADJUSTMENT )
  437. {
  438. blue = &Axis->blues[nn];
  439. break;
  440. }
  441. }
  442. if ( blue )
  443. {
  444. FT_Pos scaled = FT_MulFix( blue->shoot.org, scaler->y_scale );
  445. FT_Pos fitted = ( scaled + 40 ) & ~63;
  446. if ( scaled != fitted )
  447. {
  448. #if 0
  449. if ( dim == AF_DIMENSION_HORZ )
  450. {
  451. if ( fitted < scaled )
  452. scale -= scale / 50; /* scale *= 0.98 */
  453. }
  454. else
  455. #endif
  456. if ( dim == AF_DIMENSION_VERT )
  457. scale = FT_MulDiv( scale, fitted, scaled );
  458. }
  459. }
  460. }
  461. axis->scale = scale;
  462. axis->delta = delta;
  463. if ( dim == AF_DIMENSION_HORZ )
  464. {
  465. metrics->root.scaler.x_scale = scale;
  466. metrics->root.scaler.x_delta = delta;
  467. }
  468. else
  469. {
  470. metrics->root.scaler.y_scale = scale;
  471. metrics->root.scaler.y_delta = delta;
  472. }
  473. /* scale the widths */
  474. for ( nn = 0; nn < axis->width_count; nn++ )
  475. {
  476. AF_Width width = axis->widths + nn;
  477. width->cur = FT_MulFix( width->org, scale );
  478. width->fit = width->cur;
  479. }
  480. /* an extra-light axis corresponds to a standard width that is */
  481. /* smaller than 5/8 pixels */
  482. axis->extra_light =
  483. (FT_Bool)( FT_MulFix( axis->standard_width, scale ) < 32 + 8 );
  484. if ( dim == AF_DIMENSION_VERT )
  485. {
  486. /* scale the blue zones */
  487. for ( nn = 0; nn < axis->blue_count; nn++ )
  488. {
  489. AF_LatinBlue blue = &axis->blues[nn];
  490. FT_Pos dist;
  491. blue->ref.cur = FT_MulFix( blue->ref.org, scale ) + delta;
  492. blue->ref.fit = blue->ref.cur;
  493. blue->shoot.cur = FT_MulFix( blue->shoot.org, scale ) + delta;
  494. blue->shoot.fit = blue->shoot.cur;
  495. blue->flags &= ~AF_LATIN_BLUE_ACTIVE;
  496. /* a blue zone is only active if it is less than 3/4 pixels tall */
  497. dist = FT_MulFix( blue->ref.org - blue->shoot.org, scale );
  498. if ( dist <= 48 && dist >= -48 )
  499. {
  500. #if 0
  501. FT_Pos delta1;
  502. #endif
  503. FT_Pos delta2;
  504. /* use discrete values for blue zone widths */
  505. #if 0
  506. /* generic, original code */
  507. delta1 = blue->shoot.org - blue->ref.org;
  508. delta2 = delta1;
  509. if ( delta1 < 0 )
  510. delta2 = -delta2;
  511. delta2 = FT_MulFix( delta2, scale );
  512. if ( delta2 < 32 )
  513. delta2 = 0;
  514. else if ( delta2 < 64 )
  515. delta2 = 32 + ( ( ( delta2 - 32 ) + 16 ) & ~31 );
  516. else
  517. delta2 = FT_PIX_ROUND( delta2 );
  518. if ( delta1 < 0 )
  519. delta2 = -delta2;
  520. blue->ref.fit = FT_PIX_ROUND( blue->ref.cur );
  521. blue->shoot.fit = blue->ref.fit + delta2;
  522. #else
  523. /* simplified version due to abs(dist) <= 48 */
  524. delta2 = dist;
  525. if ( dist < 0 )
  526. delta2 = -delta2;
  527. if ( delta2 < 32 )
  528. delta2 = 0;
  529. else if ( delta < 48 )
  530. delta2 = 32;
  531. else
  532. delta2 = 64;
  533. if ( dist < 0 )
  534. delta2 = -delta2;
  535. blue->ref.fit = FT_PIX_ROUND( blue->ref.cur );
  536. blue->shoot.fit = blue->ref.fit - delta2;
  537. #endif
  538. blue->flags |= AF_LATIN_BLUE_ACTIVE;
  539. }
  540. }
  541. }
  542. }
  543. /* Scale global values in both directions. */
  544. FT_LOCAL_DEF( void )
  545. af_latin_metrics_scale( AF_LatinMetrics metrics,
  546. AF_Scaler scaler )
  547. {
  548. metrics->root.scaler.render_mode = scaler->render_mode;
  549. metrics->root.scaler.face = scaler->face;
  550. af_latin_metrics_scale_dim( metrics, scaler, AF_DIMENSION_HORZ );
  551. af_latin_metrics_scale_dim( metrics, scaler, AF_DIMENSION_VERT );
  552. }
  553. /*************************************************************************/
  554. /*************************************************************************/
  555. /***** *****/
  556. /***** L A T I N G L Y P H A N A L Y S I S *****/
  557. /***** *****/
  558. /*************************************************************************/
  559. /*************************************************************************/
  560. /* Walk over all contours and compute its segments. */
  561. FT_LOCAL_DEF( FT_Error )
  562. af_latin_hints_compute_segments( AF_GlyphHints hints,
  563. AF_Dimension dim )
  564. {
  565. AF_AxisHints axis = &hints->axis[dim];
  566. FT_Memory memory = hints->memory;
  567. FT_Error error = AF_Err_Ok;
  568. AF_Segment segment = NULL;
  569. AF_SegmentRec seg0;
  570. AF_Point* contour = hints->contours;
  571. AF_Point* contour_limit = contour + hints->num_contours;
  572. AF_Direction major_dir, segment_dir;
  573. FT_ZERO( &seg0 );
  574. seg0.score = 32000;
  575. seg0.flags = AF_EDGE_NORMAL;
  576. major_dir = (AF_Direction)FT_ABS( axis->major_dir );
  577. segment_dir = major_dir;
  578. axis->num_segments = 0;
  579. /* set up (u,v) in each point */
  580. if ( dim == AF_DIMENSION_HORZ )
  581. {
  582. AF_Point point = hints->points;
  583. AF_Point limit = point + hints->num_points;
  584. for ( ; point < limit; point++ )
  585. {
  586. point->u = point->fx;
  587. point->v = point->fy;
  588. }
  589. }
  590. else
  591. {
  592. AF_Point point = hints->points;
  593. AF_Point limit = point + hints->num_points;
  594. for ( ; point < limit; point++ )
  595. {
  596. point->u = point->fy;
  597. point->v = point->fx;
  598. }
  599. }
  600. /* do each contour separately */
  601. for ( ; contour < contour_limit; contour++ )
  602. {
  603. AF_Point point = contour[0];
  604. AF_Point last = point->prev;
  605. int on_edge = 0;
  606. FT_Pos min_pos = 32000; /* minimum segment pos != min_coord */
  607. FT_Pos max_pos = -32000; /* maximum segment pos != max_coord */
  608. FT_Bool passed;
  609. if ( point == last ) /* skip singletons -- just in case */
  610. continue;
  611. if ( FT_ABS( last->out_dir ) == major_dir &&
  612. FT_ABS( point->out_dir ) == major_dir )
  613. {
  614. /* we are already on an edge, try to locate its start */
  615. last = point;
  616. for (;;)
  617. {
  618. point = point->prev;
  619. if ( FT_ABS( point->out_dir ) != major_dir )
  620. {
  621. point = point->next;
  622. break;
  623. }
  624. if ( point == last )
  625. break;
  626. }
  627. }
  628. last = point;
  629. passed = 0;
  630. for (;;)
  631. {
  632. FT_Pos u, v;
  633. if ( on_edge )
  634. {
  635. u = point->u;
  636. if ( u < min_pos )
  637. min_pos = u;
  638. if ( u > max_pos )
  639. max_pos = u;
  640. if ( point->out_dir != segment_dir || point == last )
  641. {
  642. /* we are just leaving an edge; record a new segment! */
  643. segment->last = point;
  644. segment->pos = (FT_Short)( ( min_pos + max_pos ) >> 1 );
  645. /* a segment is round if either its first or last point */
  646. /* is a control point */
  647. if ( ( segment->first->flags | point->flags ) &
  648. AF_FLAG_CONTROL )
  649. segment->flags |= AF_EDGE_ROUND;
  650. /* compute segment size */
  651. min_pos = max_pos = point->v;
  652. v = segment->first->v;
  653. if ( v < min_pos )
  654. min_pos = v;
  655. if ( v > max_pos )
  656. max_pos = v;
  657. segment->min_coord = (FT_Short)min_pos;
  658. segment->max_coord = (FT_Short)max_pos;
  659. segment->height = (FT_Short)( segment->max_coord -
  660. segment->min_coord );
  661. on_edge = 0;
  662. segment = NULL;
  663. /* fallthrough */
  664. }
  665. }
  666. /* now exit if we are at the start/end point */
  667. if ( point == last )
  668. {
  669. if ( passed )
  670. break;
  671. passed = 1;
  672. }
  673. if ( !on_edge && FT_ABS( point->out_dir ) == major_dir )
  674. {
  675. /* this is the start of a new segment! */
  676. segment_dir = (AF_Direction)point->out_dir;
  677. /* clear all segment fields */
  678. error = af_axis_hints_new_segment( axis, memory, &segment );
  679. if ( error )
  680. goto Exit;
  681. segment[0] = seg0;
  682. segment->dir = (FT_Char)segment_dir;
  683. min_pos = max_pos = point->u;
  684. segment->first = point;
  685. segment->last = point;
  686. on_edge = 1;
  687. }
  688. point = point->next;
  689. }
  690. } /* contours */
  691. /* now slightly increase the height of segments when this makes */
  692. /* sense -- this is used to better detect and ignore serifs */
  693. {
  694. AF_Segment segments = axis->segments;
  695. AF_Segment segments_end = segments + axis->num_segments;
  696. for ( segment = segments; segment < segments_end; segment++ )
  697. {
  698. AF_Point first = segment->first;
  699. AF_Point last = segment->last;
  700. FT_Pos first_v = first->v;
  701. FT_Pos last_v = last->v;
  702. if ( first == last )
  703. continue;
  704. if ( first_v < last_v )
  705. {
  706. AF_Point p;
  707. p = first->prev;
  708. if ( p->v < first_v )
  709. segment->height = (FT_Short)( segment->height +
  710. ( ( first_v - p->v ) >> 1 ) );
  711. p = last->next;
  712. if ( p->v > last_v )
  713. segment->height = (FT_Short)( segment->height +
  714. ( ( p->v - last_v ) >> 1 ) );
  715. }
  716. else
  717. {
  718. AF_Point p;
  719. p = first->prev;
  720. if ( p->v > first_v )
  721. segment->height = (FT_Short)( segment->height +
  722. ( ( p->v - first_v ) >> 1 ) );
  723. p = last->next;
  724. if ( p->v < last_v )
  725. segment->height = (FT_Short)( segment->height +
  726. ( ( last_v - p->v ) >> 1 ) );
  727. }
  728. }
  729. }
  730. Exit:
  731. return error;
  732. }
  733. /* Link segments to form stems and serifs. */
  734. FT_LOCAL_DEF( void )
  735. af_latin_hints_link_segments( AF_GlyphHints hints,
  736. AF_Dimension dim )
  737. {
  738. AF_AxisHints axis = &hints->axis[dim];
  739. AF_Segment segments = axis->segments;
  740. AF_Segment segment_limit = segments + axis->num_segments;
  741. FT_Pos len_threshold, len_score;
  742. AF_Segment seg1, seg2;
  743. len_threshold = AF_LATIN_CONSTANT( hints->metrics, 8 );
  744. if ( len_threshold == 0 )
  745. len_threshold = 1;
  746. len_score = AF_LATIN_CONSTANT( hints->metrics, 6000 );
  747. /* now compare each segment to the others */
  748. for ( seg1 = segments; seg1 < segment_limit; seg1++ )
  749. {
  750. /* the fake segments are introduced to hint the metrics -- */
  751. /* we must never link them to anything */
  752. if ( seg1->dir != axis->major_dir || seg1->first == seg1->last )
  753. continue;
  754. /* search for stems having opposite directions, */
  755. /* with seg1 to the `left' of seg2 */
  756. for ( seg2 = segments; seg2 < segment_limit; seg2++ )
  757. {
  758. FT_Pos pos1 = seg1->pos;
  759. FT_Pos pos2 = seg2->pos;
  760. if ( seg1->dir + seg2->dir == 0 && pos2 > pos1 )
  761. {
  762. /* compute distance between the two segments */
  763. FT_Pos dist = pos2 - pos1;
  764. FT_Pos min = seg1->min_coord;
  765. FT_Pos max = seg1->max_coord;
  766. FT_Pos len, score;
  767. if ( min < seg2->min_coord )
  768. min = seg2->min_coord;
  769. if ( max > seg2->max_coord )
  770. max = seg2->max_coord;
  771. /* compute maximum coordinate difference of the two segments */
  772. len = max - min;
  773. if ( len >= len_threshold )
  774. {
  775. /* small coordinate differences cause a higher score, and */
  776. /* segments with a greater distance cause a higher score also */
  777. score = dist + len_score / len;
  778. /* and we search for the smallest score */
  779. /* of the sum of the two values */
  780. if ( score < seg1->score )
  781. {
  782. seg1->score = score;
  783. seg1->link = seg2;
  784. }
  785. if ( score < seg2->score )
  786. {
  787. seg2->score = score;
  788. seg2->link = seg1;
  789. }
  790. }
  791. }
  792. }
  793. }
  794. /* now compute the `serif' segments, cf. explanations in `afhints.h' */
  795. for ( seg1 = segments; seg1 < segment_limit; seg1++ )
  796. {
  797. seg2 = seg1->link;
  798. if ( seg2 )
  799. {
  800. if ( seg2->link != seg1 )
  801. {
  802. seg1->link = 0;
  803. seg1->serif = seg2->link;
  804. }
  805. }
  806. }
  807. }
  808. /* Link segments to edges, using feature analysis for selection. */
  809. FT_LOCAL_DEF( FT_Error )
  810. af_latin_hints_compute_edges( AF_GlyphHints hints,
  811. AF_Dimension dim )
  812. {
  813. AF_AxisHints axis = &hints->axis[dim];
  814. FT_Error error = AF_Err_Ok;
  815. FT_Memory memory = hints->memory;
  816. AF_LatinAxis laxis = &((AF_LatinMetrics)hints->metrics)->axis[dim];
  817. AF_Segment segments = axis->segments;
  818. AF_Segment segment_limit = segments + axis->num_segments;
  819. AF_Segment seg;
  820. #if 0
  821. AF_Direction up_dir;
  822. #endif
  823. FT_Fixed scale;
  824. FT_Pos edge_distance_threshold;
  825. FT_Pos segment_length_threshold;
  826. axis->num_edges = 0;
  827. scale = ( dim == AF_DIMENSION_HORZ ) ? hints->x_scale
  828. : hints->y_scale;
  829. #if 0
  830. up_dir = ( dim == AF_DIMENSION_HORZ ) ? AF_DIR_UP
  831. : AF_DIR_RIGHT;
  832. #endif
  833. /*
  834. * We ignore all segments that are less than 1 pixel in length
  835. * to avoid many problems with serif fonts. We compute the
  836. * corresponding threshold in font units.
  837. */
  838. if ( dim == AF_DIMENSION_HORZ )
  839. segment_length_threshold = FT_DivFix( 64, hints->y_scale );
  840. else
  841. segment_length_threshold = 0;
  842. /*********************************************************************/
  843. /* */
  844. /* We begin by generating a sorted table of edges for the current */
  845. /* direction. To do so, we simply scan each segment and try to find */
  846. /* an edge in our table that corresponds to its position. */
  847. /* */
  848. /* If no edge is found, we create and insert a new edge in the */
  849. /* sorted table. Otherwise, we simply add the segment to the edge's */
  850. /* list which gets processed in the second step to compute the */
  851. /* edge's properties. */
  852. /* */
  853. /* Note that the table of edges is sorted along the segment/edge */
  854. /* position. */
  855. /* */
  856. /*********************************************************************/
  857. /* assure that edge distance threshold is at most 0.25px */
  858. edge_distance_threshold = FT_MulFix( laxis->edge_distance_threshold,
  859. scale );
  860. if ( edge_distance_threshold > 64 / 4 )
  861. edge_distance_threshold = 64 / 4;
  862. edge_distance_threshold = FT_DivFix( edge_distance_threshold,
  863. scale );
  864. for ( seg = segments; seg < segment_limit; seg++ )
  865. {
  866. AF_Edge found = NULL;
  867. FT_Int ee;
  868. if ( seg->height < segment_length_threshold )
  869. continue;
  870. /* A special case for serif edges: If they are smaller than */
  871. /* 1.5 pixels we ignore them. */
  872. if ( seg->serif &&
  873. 2 * seg->height < 3 * segment_length_threshold )
  874. continue;
  875. /* look for an edge corresponding to the segment */
  876. for ( ee = 0; ee < axis->num_edges; ee++ )
  877. {
  878. AF_Edge edge = axis->edges + ee;
  879. FT_Pos dist;
  880. dist = seg->pos - edge->fpos;
  881. if ( dist < 0 )
  882. dist = -dist;
  883. if ( dist < edge_distance_threshold && edge->dir == seg->dir )
  884. {
  885. found = edge;
  886. break;
  887. }
  888. }
  889. if ( !found )
  890. {
  891. AF_Edge edge;
  892. /* insert a new edge in the list and */
  893. /* sort according to the position */
  894. error = af_axis_hints_new_edge( axis, seg->pos,
  895. (AF_Direction)seg->dir,
  896. memory, &edge );
  897. if ( error )
  898. goto Exit;
  899. /* add the segment to the new edge's list */
  900. FT_ZERO( edge );
  901. edge->first = seg;
  902. edge->last = seg;
  903. edge->dir = seg->dir;
  904. edge->fpos = seg->pos;
  905. edge->opos = FT_MulFix( seg->pos, scale );
  906. edge->pos = edge->opos;
  907. seg->edge_next = seg;
  908. }
  909. else
  910. {
  911. /* if an edge was found, simply add the segment to the edge's */
  912. /* list */
  913. seg->edge_next = found->first;
  914. found->last->edge_next = seg;
  915. found->last = seg;
  916. }
  917. }
  918. /*********************************************************************/
  919. /* */
  920. /* Good, we will now compute each edge's properties according to */
  921. /* the segments found on its position. Basically, these are */
  922. /* */
  923. /* - the edge's main direction */
  924. /* - stem edge, serif edge or both (which defaults to stem then) */
  925. /* - rounded edge, straight or both (which defaults to straight) */
  926. /* - link for edge */
  927. /* */
  928. /*********************************************************************/
  929. /* first of all, set the `edge' field in each segment -- this is */
  930. /* required in order to compute edge links */
  931. /*
  932. * Note that removing this loop and setting the `edge' field of each
  933. * segment directly in the code above slows down execution speed for
  934. * some reasons on platforms like the Sun.
  935. */
  936. {
  937. AF_Edge edges = axis->edges;
  938. AF_Edge edge_limit = edges + axis->num_edges;
  939. AF_Edge edge;
  940. for ( edge = edges; edge < edge_limit; edge++ )
  941. {
  942. seg = edge->first;
  943. if ( seg )
  944. do
  945. {
  946. seg->edge = edge;
  947. seg = seg->edge_next;
  948. } while ( seg != edge->first );
  949. }
  950. /* now compute each edge properties */
  951. for ( edge = edges; edge < edge_limit; edge++ )
  952. {
  953. FT_Int is_round = 0; /* does it contain round segments? */
  954. FT_Int is_straight = 0; /* does it contain straight segments? */
  955. #if 0
  956. FT_Pos ups = 0; /* number of upwards segments */
  957. FT_Pos downs = 0; /* number of downwards segments */
  958. #endif
  959. seg = edge->first;
  960. do
  961. {
  962. FT_Bool is_serif;
  963. /* check for roundness of segment */
  964. if ( seg->flags & AF_EDGE_ROUND )
  965. is_round++;
  966. else
  967. is_straight++;
  968. #if 0
  969. /* check for segment direction */
  970. if ( seg->dir == up_dir )
  971. ups += seg->max_coord - seg->min_coord;
  972. else
  973. downs += seg->max_coord - seg->min_coord;
  974. #endif
  975. /* check for links -- if seg->serif is set, then seg->link must */
  976. /* be ignored */
  977. is_serif = (FT_Bool)( seg->serif &&
  978. seg->serif->edge &&
  979. seg->serif->edge != edge );
  980. if ( ( seg->link && seg->link->edge != NULL ) || is_serif )
  981. {
  982. AF_Edge edge2;
  983. AF_Segment seg2;
  984. edge2 = edge->link;
  985. seg2 = seg->link;
  986. if ( is_serif )
  987. {
  988. seg2 = seg->serif;
  989. edge2 = edge->serif;
  990. }
  991. if ( edge2 )
  992. {
  993. FT_Pos edge_delta;
  994. FT_Pos seg_delta;
  995. edge_delta = edge->fpos - edge2->fpos;
  996. if ( edge_delta < 0 )
  997. edge_delta = -edge_delta;
  998. seg_delta = seg->pos - seg2->pos;
  999. if ( seg_delta < 0 )
  1000. seg_delta = -seg_delta;
  1001. if ( seg_delta < edge_delta )
  1002. edge2 = seg2->edge;
  1003. }
  1004. else
  1005. edge2 = seg2->edge;
  1006. if ( is_serif )
  1007. {
  1008. edge->serif = edge2;
  1009. edge2->flags |= AF_EDGE_SERIF;
  1010. }
  1011. else
  1012. edge->link = edge2;
  1013. }
  1014. seg = seg->edge_next;
  1015. } while ( seg != edge->first );
  1016. /* set the round/straight flags */
  1017. edge->flags = AF_EDGE_NORMAL;
  1018. if ( is_round > 0 && is_round >= is_straight )
  1019. edge->flags |= AF_EDGE_ROUND;
  1020. #if 0
  1021. /* set the edge's main direction */
  1022. edge->dir = AF_DIR_NONE;
  1023. if ( ups > downs )
  1024. edge->dir = (FT_Char)up_dir;
  1025. else if ( ups < downs )
  1026. edge->dir = (FT_Char)-up_dir;
  1027. else if ( ups == downs )
  1028. edge->dir = 0; /* both up and down! */
  1029. #endif
  1030. /* get rid of serifs if link is set */
  1031. /* XXX: This gets rid of many unpleasant artefacts! */
  1032. /* Example: the `c' in cour.pfa at size 13 */
  1033. if ( edge->serif && edge->link )
  1034. edge->serif = 0;
  1035. }
  1036. }
  1037. Exit:
  1038. return error;
  1039. }
  1040. /* Detect segments and edges for given dimension. */
  1041. FT_LOCAL_DEF( FT_Error )
  1042. af_latin_hints_detect_features( AF_GlyphHints hints,
  1043. AF_Dimension dim )
  1044. {
  1045. FT_Error error;
  1046. error = af_latin_hints_compute_segments( hints, dim );
  1047. if ( !error )
  1048. {
  1049. af_latin_hints_link_segments( hints, dim );
  1050. error = af_latin_hints_compute_edges( hints, dim );
  1051. }
  1052. return error;
  1053. }
  1054. /* Compute all edges which lie within blue zones. */
  1055. FT_LOCAL_DEF( void )
  1056. af_latin_hints_compute_blue_edges( AF_GlyphHints hints,
  1057. AF_LatinMetrics metrics )
  1058. {
  1059. AF_AxisHints axis = &hints->axis[AF_DIMENSION_VERT];
  1060. AF_Edge edge = axis->edges;
  1061. AF_Edge edge_limit = edge + axis->num_edges;
  1062. AF_LatinAxis latin = &metrics->axis[AF_DIMENSION_VERT];
  1063. FT_Fixed scale = latin->scale;
  1064. /* compute which blue zones are active, i.e. have their scaled */
  1065. /* size < 3/4 pixels */
  1066. /* for each horizontal edge search the blue zone which is closest */
  1067. for ( ; edge < edge_limit; edge++ )
  1068. {
  1069. FT_Int bb;
  1070. AF_Width best_blue = NULL;
  1071. FT_Pos best_dist; /* initial threshold */
  1072. /* compute the initial threshold as a fraction of the EM size */
  1073. /* (the value 40 is heuristic) */
  1074. best_dist = FT_MulFix( metrics->units_per_em / 40, scale );
  1075. /* assure a minimum distance of 0.5px */
  1076. if ( best_dist > 64 / 2 )
  1077. best_dist = 64 / 2;
  1078. for ( bb = 0; bb < AF_LATIN_BLUE_MAX; bb++ )
  1079. {
  1080. AF_LatinBlue blue = latin->blues + bb;
  1081. FT_Bool is_top_blue, is_major_dir;
  1082. /* skip inactive blue zones (i.e., those that are too large) */
  1083. if ( !( blue->flags & AF_LATIN_BLUE_ACTIVE ) )
  1084. continue;
  1085. /* if it is a top zone, check for right edges -- if it is a bottom */
  1086. /* zone, check for left edges */
  1087. /* */
  1088. /* of course, that's for TrueType */
  1089. is_top_blue = (FT_Byte)( ( blue->flags & AF_LATIN_BLUE_TOP ) != 0 );
  1090. is_major_dir = FT_BOOL( edge->dir == axis->major_dir );
  1091. /* if it is a top zone, the edge must be against the major */
  1092. /* direction; if it is a bottom zone, it must be in the major */
  1093. /* direction */
  1094. if ( is_top_blue ^ is_major_dir )
  1095. {
  1096. FT_Pos dist;
  1097. /* first of all, compare it to the reference position */
  1098. dist = edge->fpos - blue->ref.org;
  1099. if ( dist < 0 )
  1100. dist = -dist;
  1101. dist = FT_MulFix( dist, scale );
  1102. if ( dist < best_dist )
  1103. {
  1104. best_dist = dist;
  1105. best_blue = &blue->ref;
  1106. }
  1107. /* now compare it to the overshoot position and check whether */
  1108. /* the edge is rounded, and whether the edge is over the */
  1109. /* reference position of a top zone, or under the reference */
  1110. /* position of a bottom zone */
  1111. if ( edge->flags & AF_EDGE_ROUND && dist != 0 )
  1112. {
  1113. FT_Bool is_under_ref = FT_BOOL( edge->fpos < blue->ref.org );
  1114. if ( is_top_blue ^ is_under_ref )
  1115. {
  1116. dist = edge->fpos - blue->shoot.org;
  1117. if ( dist < 0 )
  1118. dist = -dist;
  1119. dist = FT_MulFix( dist, scale );
  1120. if ( dist < best_dist )
  1121. {
  1122. best_dist = dist;
  1123. best_blue = &blue->shoot;
  1124. }
  1125. }
  1126. }
  1127. }
  1128. }
  1129. if ( best_blue )
  1130. edge->blue_edge = best_blue;
  1131. }
  1132. }
  1133. /* Initalize hinting engine. */
  1134. static FT_Error
  1135. af_latin_hints_init( AF_GlyphHints hints,
  1136. AF_LatinMetrics metrics )
  1137. {
  1138. FT_Render_Mode mode;
  1139. FT_UInt32 scaler_flags, other_flags;
  1140. FT_Face face = metrics->root.scaler.face;
  1141. af_glyph_hints_rescale( hints, (AF_ScriptMetrics)metrics );
  1142. /*
  1143. * correct x_scale and y_scale if needed, since they may have
  1144. * been modified by `af_latin_metrics_scale_dim' above
  1145. */
  1146. hints->x_scale = metrics->axis[AF_DIMENSION_HORZ].scale;
  1147. hints->x_delta = metrics->axis[AF_DIMENSION_HORZ].delta;
  1148. hints->y_scale = metrics->axis[AF_DIMENSION_VERT].scale;
  1149. hints->y_delta = metrics->axis[AF_DIMENSION_VERT].delta;
  1150. /* compute flags depending on render mode, etc. */
  1151. mode = metrics->root.scaler.render_mode;
  1152. #if 0 /* #ifdef AF_CONFIG_OPTION_USE_WARPER */
  1153. if ( mode == FT_RENDER_MODE_LCD || mode == FT_RENDER_MODE_LCD_V )
  1154. {
  1155. metrics->root.scaler.render_mode = mode = FT_RENDER_MODE_NORMAL;
  1156. }
  1157. #endif
  1158. scaler_flags = hints->scaler_flags;
  1159. other_flags = 0;
  1160. /*
  1161. * We snap the width of vertical stems for the monochrome and
  1162. * horizontal LCD rendering targets only.
  1163. */
  1164. if ( mode == FT_RENDER_MODE_MONO || mode == FT_RENDER_MODE_LCD )
  1165. other_flags |= AF_LATIN_HINTS_HORZ_SNAP;
  1166. /*
  1167. * We snap the width of horizontal stems for the monochrome and
  1168. * vertical LCD rendering targets only.
  1169. */
  1170. if ( mode == FT_RENDER_MODE_MONO || mode == FT_RENDER_MODE_LCD_V )
  1171. other_flags |= AF_LATIN_HINTS_VERT_SNAP;
  1172. /*
  1173. * We adjust stems to full pixels only if we don't use the `light' mode.
  1174. */
  1175. if ( mode != FT_RENDER_MODE_LIGHT )
  1176. other_flags |= AF_LATIN_HINTS_STEM_ADJUST;
  1177. if ( mode == FT_RENDER_MODE_MONO )
  1178. other_flags |= AF_LATIN_HINTS_MONO;
  1179. /*
  1180. * In `light' hinting mode we disable horizontal hinting completely.
  1181. * We also do it if the face is italic.
  1182. */
  1183. if ( mode == FT_RENDER_MODE_LIGHT ||
  1184. ( face->style_flags & FT_STYLE_FLAG_ITALIC ) != 0 )
  1185. scaler_flags |= AF_SCALER_FLAG_NO_HORIZONTAL;
  1186. hints->scaler_flags = scaler_flags;
  1187. hints->other_flags = other_flags;
  1188. return AF_Err_Ok;
  1189. }
  1190. /*************************************************************************/
  1191. /*************************************************************************/
  1192. /***** *****/
  1193. /***** L A T I N G L Y P H G R I D - F I T T I N G *****/
  1194. /***** *****/
  1195. /*************************************************************************/
  1196. /*************************************************************************/
  1197. /* Snap a given width in scaled coordinates to one of the */
  1198. /* current standard widths. */
  1199. static FT_Pos
  1200. af_latin_snap_width( AF_Width widths,
  1201. FT_Int count,
  1202. FT_Pos width )
  1203. {
  1204. int n;
  1205. FT_Pos best = 64 + 32 + 2;
  1206. FT_Pos reference = width;
  1207. FT_Pos scaled;
  1208. for ( n = 0; n < count; n++ )
  1209. {
  1210. FT_Pos w;
  1211. FT_Pos dist;
  1212. w = widths[n].cur;
  1213. dist = width - w;
  1214. if ( dist < 0 )
  1215. dist = -dist;
  1216. if ( dist < best )
  1217. {
  1218. best = dist;
  1219. reference = w;
  1220. }
  1221. }
  1222. scaled = FT_PIX_ROUND( reference );
  1223. if ( width >= reference )
  1224. {
  1225. if ( width < scaled + 48 )
  1226. width = reference;
  1227. }
  1228. else
  1229. {
  1230. if ( width > scaled - 48 )
  1231. width = reference;
  1232. }
  1233. return width;
  1234. }
  1235. /* Compute the snapped width of a given stem, ignoring very thin ones. */
  1236. /* There is a lot of voodoo in this function; changing the hard-coded */
  1237. /* parameters influence the whole hinting process. */
  1238. static FT_Pos
  1239. af_latin_compute_stem_width( AF_GlyphHints hints,
  1240. AF_Dimension dim,
  1241. FT_Pos width,
  1242. AF_Edge_Flags base_flags,
  1243. AF_Edge_Flags stem_flags )
  1244. {
  1245. AF_LatinMetrics metrics = (AF_LatinMetrics) hints->metrics;
  1246. AF_LatinAxis axis = & metrics->axis[dim];
  1247. FT_Pos dist = width;
  1248. FT_Int sign = 0;
  1249. FT_Int vertical = ( dim == AF_DIMENSION_VERT );
  1250. if ( !AF_LATIN_HINTS_DO_STEM_ADJUST( hints ) ||
  1251. axis->extra_light )
  1252. return width;
  1253. if ( dist < 0 )
  1254. {
  1255. dist = -width;
  1256. sign = 1;
  1257. }
  1258. if ( ( vertical && !AF_LATIN_HINTS_DO_VERT_SNAP( hints ) ) ||
  1259. ( !vertical && !AF_LATIN_HINTS_DO_HORZ_SNAP( hints ) ) )
  1260. {
  1261. /* smooth hinting process: very lightly quantize the stem width */
  1262. /* leave the widths of serifs alone */
  1263. if ( ( stem_flags & AF_EDGE_SERIF ) &&
  1264. vertical &&
  1265. ( dist < 3 * 64 ) )
  1266. goto Done_Width;
  1267. else if ( base_flags & AF_EDGE_ROUND )
  1268. {
  1269. if ( dist < 80 )
  1270. dist = 64;
  1271. }
  1272. else if ( dist < 56 )
  1273. dist = 56;
  1274. if ( axis->width_count > 0 )
  1275. {
  1276. FT_Pos delta;
  1277. /* compare to standard width */
  1278. delta = dist - axis->widths[0].cur;
  1279. if ( delta < 0 )
  1280. delta = -delta;
  1281. if ( delta < 40 )
  1282. {
  1283. dist = axis->widths[0].cur;
  1284. if ( dist < 48 )
  1285. dist = 48;
  1286. goto Done_Width;
  1287. }
  1288. if ( dist < 3 * 64 )
  1289. {
  1290. delta = dist & 63;
  1291. dist &= -64;
  1292. if ( delta < 10 )
  1293. dist += delta;
  1294. else if ( delta < 32 )
  1295. dist += 10;
  1296. else if ( delta < 54 )
  1297. dist += 54;
  1298. else
  1299. dist += delta;
  1300. }
  1301. else
  1302. dist = ( dist + 32 ) & ~63;
  1303. }
  1304. }
  1305. else
  1306. {
  1307. /* strong hinting process: snap the stem width to integer pixels */
  1308. FT_Pos org_dist = dist;
  1309. dist = af_latin_snap_width( axis->widths, axis->width_count, dist );
  1310. if ( vertical )
  1311. {
  1312. /* in the case of vertical hinting, always round */
  1313. /* the stem heights to integer pixels */
  1314. if ( dist >= 64 )
  1315. dist = ( dist + 16 ) & ~63;
  1316. else
  1317. dist = 64;
  1318. }
  1319. else
  1320. {
  1321. if ( AF_LATIN_HINTS_DO_MONO( hints ) )
  1322. {
  1323. /* monochrome horizontal hinting: snap widths to integer pixels */
  1324. /* with a different threshold */
  1325. if ( dist < 64 )
  1326. dist = 64;
  1327. else
  1328. dist = ( dist + 32 ) & ~63;
  1329. }
  1330. else
  1331. {
  1332. /* for horizontal anti-aliased hinting, we adopt a more subtle */
  1333. /* approach: we strengthen small stems, round stems whose size */
  1334. /* is between 1 and 2 pixels to an integer, otherwise nothing */
  1335. if ( dist < 48 )
  1336. dist = ( dist + 64 ) >> 1;
  1337. else if ( dist < 128 )
  1338. {
  1339. /* We only round to an integer width if the corresponding */
  1340. /* distortion is less than 1/4 pixel. Otherwise this */
  1341. /* makes everything worse since the diagonals, which are */
  1342. /* not hinted, appear a lot bolder or thinner than the */
  1343. /* vertical stems. */
  1344. FT_Pos delta;
  1345. dist = ( dist + 22 ) & ~63;
  1346. delta = dist - org_dist;
  1347. if ( delta < 0 )
  1348. delta = -delta;
  1349. if (delta >= 16)
  1350. {
  1351. dist = org_dist;
  1352. if ( dist < 48 )
  1353. dist = ( dist + 64 ) >> 1;
  1354. }
  1355. }
  1356. else
  1357. /* round otherwise to prevent color fringes in LCD mode */
  1358. dist = ( dist + 32 ) & ~63;
  1359. }
  1360. }
  1361. }
  1362. Done_Width:
  1363. if ( sign )
  1364. dist = -dist;
  1365. return dist;
  1366. }
  1367. /* Align one stem edge relative to the previous stem edge. */
  1368. static void
  1369. af_latin_align_linked_edge( AF_GlyphHints hints,
  1370. AF_Dimension dim,
  1371. AF_Edge base_edge,
  1372. AF_Edge stem_edge )
  1373. {
  1374. FT_Pos dist = stem_edge->opos - base_edge->opos;
  1375. FT_Pos fitted_width = af_latin_compute_stem_width(
  1376. hints, dim, dist,
  1377. (AF_Edge_Flags)base_edge->flags,
  1378. (AF_Edge_Flags)stem_edge->flags );
  1379. stem_edge->pos = base_edge->pos + fitted_width;
  1380. FT_TRACE5(( " LINK: edge %d (opos=%.2f) linked to (%.2f),"
  1381. " dist was %.2f, now %.2f\n",
  1382. stem_edge-hints->axis[dim].edges, stem_edge->opos / 64.0,
  1383. stem_edge->pos / 64.0, dist / 64.0, fitted_width / 64.0 ));
  1384. }
  1385. /* Shift the coordinates of the `serif' edge by the same amount */
  1386. /* as the corresponding `base' edge has been moved already. */
  1387. static void
  1388. af_latin_align_serif_edge( AF_GlyphHints hints,
  1389. AF_Edge base,
  1390. AF_Edge serif )
  1391. {
  1392. FT_UNUSED( hints );
  1393. serif->pos = base->pos + ( serif->opos - base->opos );
  1394. }
  1395. /*************************************************************************/
  1396. /*************************************************************************/
  1397. /*************************************************************************/
  1398. /**** ****/
  1399. /**** E D G E H I N T I N G ****/
  1400. /**** ****/
  1401. /*************************************************************************/
  1402. /*************************************************************************/
  1403. /*************************************************************************/
  1404. /* The main grid-fitting routine. */
  1405. FT_LOCAL_DEF( void )
  1406. af_latin_hint_edges( AF_GlyphHints hints,
  1407. AF_Dimension dim )
  1408. {
  1409. AF_AxisHints axis = &hints->axis[dim];
  1410. AF_Edge edges = axis->edges;
  1411. AF_Edge edge_limit = edges + axis->num_edges;
  1412. FT_PtrDist n_edges;
  1413. AF_Edge edge;
  1414. AF_Edge anchor = NULL;
  1415. FT_Int has_serifs = 0;
  1416. FT_TRACE5(("%s edge hinting\n", dim == AF_DIMENSION_VERT ? "horizontal"
  1417. : "vertical"));
  1418. /* we begin by aligning all stems relative to the blue zone */
  1419. /* if needed -- that's only for horizontal edges */
  1420. if ( dim == AF_DIMENSION_VERT && AF_HINTS_DO_BLUES( hints ) )
  1421. {
  1422. for ( edge = edges; edge < edge_limit; edge++ )
  1423. {
  1424. AF_Width blue;
  1425. AF_Edge edge1, edge2; /* these edges form the stem to check */
  1426. if ( edge->flags & AF_EDGE_DONE )
  1427. continue;
  1428. blue = edge->blue_edge;
  1429. edge1 = NULL;
  1430. edge2 = edge->link;
  1431. if ( blue )
  1432. edge1 = edge;
  1433. /* flip edges if the other stem is aligned to a blue zone */
  1434. else if ( edge2 && edge2->blue_edge )
  1435. {
  1436. blue = edge2->blue_edge;
  1437. edge1 = edge2;
  1438. edge2 = edge;
  1439. }
  1440. if ( !edge1 )
  1441. continue;
  1442. FT_TRACE5(( " BLUE: edge %d (opos=%.2f) snapped to (%.2f),"
  1443. " was (%.2f)\n",
  1444. edge1 - edges, edge1->opos / 64.0, blue->fit / 64.0,
  1445. edge1->pos / 64.0 ));
  1446. edge1->pos = blue->fit;
  1447. edge1->flags |= AF_EDGE_DONE;
  1448. if ( edge2 && !edge2->blue_edge )
  1449. {
  1450. af_latin_align_linked_edge( hints, dim, edge1, edge2 );
  1451. edge2->flags |= AF_EDGE_DONE;
  1452. }
  1453. if ( !anchor )
  1454. anchor = edge;
  1455. }
  1456. }
  1457. /* now we align all other stem edges, trying to maintain the */
  1458. /* relative order of stems in the glyph */
  1459. for ( edge = edges; edge < edge_limit; edge++ )
  1460. {
  1461. AF_Edge edge2;
  1462. if ( edge->flags & AF_EDGE_DONE )
  1463. continue;
  1464. /* skip all non-stem edges */
  1465. edge2 = edge->link;
  1466. if ( !edge2 )
  1467. {
  1468. has_serifs++;
  1469. continue;
  1470. }
  1471. /* now align the stem */
  1472. /* this should not happen, but it's better to be safe */
  1473. if ( edge2->blue_edge )
  1474. {
  1475. FT_TRACE5(( " ASSERTION FAILED for edge %d\n", edge2-edges ));
  1476. af_latin_align_linked_edge( hints, dim, edge2, edge );
  1477. edge->flags |= AF_EDGE_DONE;
  1478. continue;
  1479. }
  1480. if ( !anchor )
  1481. {
  1482. /* if we reach this if clause, no stem has been aligned yet */
  1483. FT_Pos org_len, org_center, cur_len;
  1484. FT_Pos cur_pos1, error1, error2, u_off, d_off;
  1485. org_len = edge2->opos - edge->opos;
  1486. cur_len = af_latin_compute_stem_width(
  1487. hints, dim, org_len,
  1488. (AF_Edge_Flags)edge->flags,
  1489. (AF_Edge_Flags)edge2->flags );
  1490. /* some voodoo to specially round edges for small stem widths; */
  1491. /* the idea is to align the center of a stem, then shifting */
  1492. /* the stem edges to suitable positions */
  1493. if ( cur_len <= 64 )
  1494. {
  1495. /* width <= 1px */
  1496. u_off = 32;
  1497. d_off = 32;
  1498. }
  1499. else
  1500. {
  1501. /* 1px < width < 1.5px */
  1502. u_off = 38;
  1503. d_off = 26;
  1504. }
  1505. if ( cur_len < 96 )
  1506. {
  1507. org_center = edge->opos + ( org_len >> 1 );
  1508. cur_pos1 = FT_PIX_ROUND( org_center );
  1509. error1 = org_center - ( cur_pos1 - u_off );
  1510. if ( error1 < 0 )
  1511. error1 = -error1;
  1512. error2 = org_center - ( cur_pos1 + d_off );
  1513. if ( error2 < 0 )
  1514. error2 = -error2;
  1515. if ( error1 < error2 )
  1516. cur_pos1 -= u_off;
  1517. else
  1518. cur_pos1 += d_off;
  1519. edge->pos = cur_pos1 - cur_len / 2;
  1520. edge2->pos = edge->pos + cur_len;
  1521. }
  1522. else
  1523. edge->pos = FT_PIX_ROUND( edge->opos );
  1524. FT_TRACE5(( " ANCHOR: edge %d (opos=%.2f) and %d (opos=%.2f)"
  1525. " snapped to (%.2f) (%.2f)\n",
  1526. edge - edges, edge->opos / 64.0,
  1527. edge2 - edges, edge2->opos / 64.0,
  1528. edge->pos / 64.0, edge2->pos / 64.0 ));
  1529. anchor = edge;
  1530. edge->flags |= AF_EDGE_DONE;
  1531. af_latin_align_linked_edge( hints, dim, edge, edge2 );
  1532. }
  1533. else
  1534. {
  1535. FT_Pos org_pos, org_len, org_center, cur_len;
  1536. FT_Pos cur_pos1, cur_pos2, delta1, delta2;
  1537. org_pos = anchor->pos + ( edge->opos - anchor->opos );
  1538. org_len = edge2->opos - edge->opos;
  1539. org_center = org_pos + ( org_len >> 1 );
  1540. cur_len = af_latin_compute_stem_width(
  1541. hints, dim, org_len,
  1542. (AF_Edge_Flags)edge->flags,
  1543. (AF_Edge_Flags)edge2->flags );
  1544. if ( edge2->flags & AF_EDGE_DONE )
  1545. {
  1546. FT_TRACE5(( " ADJUST: edge %d (pos=%.2f) moved to %.2f\n",
  1547. edge - edges, edge->pos / 64.0,
  1548. ( edge2->pos - cur_len ) / 64.0 ));
  1549. edge->pos = edge2->pos - cur_len;
  1550. }
  1551. else if ( cur_len < 96 )
  1552. {
  1553. FT_Pos u_off, d_off;
  1554. cur_pos1 = FT_PIX_ROUND( org_center );
  1555. if (cur_len <= 64 )
  1556. {
  1557. u_off = 32;
  1558. d_off = 32;
  1559. }
  1560. else
  1561. {
  1562. u_off = 38;
  1563. d_off = 26;
  1564. }
  1565. delta1 = org_center - ( cur_pos1 - u_off );
  1566. if ( delta1 < 0 )
  1567. delta1 = -delta1;
  1568. delta2 = org_center - ( cur_pos1 + d_off );
  1569. if ( delta2 < 0 )
  1570. delta2 = -delta2;
  1571. if ( delta1 < delta2 )
  1572. cur_pos1 -= u_off;
  1573. else
  1574. cur_pos1 += d_off;
  1575. edge->pos = cur_pos1 - cur_len / 2;
  1576. edge2->pos = cur_pos1 + cur_len / 2;
  1577. FT_TRACE5(( " STEM: %d (opos=%.2f) to %d (opos=%.2f)"
  1578. " snapped to (%.2f) and (%.2f)\n",
  1579. edge - edges, edge->opos / 64.0,
  1580. edge2 - edges, edge2->opos / 64.0,
  1581. edge->pos / 64.0, edge2->pos / 64.0 ));
  1582. }
  1583. else
  1584. {
  1585. org_pos = anchor->pos + ( edge->opos - anchor->opos );
  1586. org_len = edge2->opos - edge->opos;
  1587. org_center = org_pos + ( org_len >> 1 );
  1588. cur_len = af_latin_compute_stem_width(
  1589. hints, dim, org_len,
  1590. (AF_Edge_Flags)edge->flags,
  1591. (AF_Edge_Flags)edge2->flags );
  1592. cur_pos1 = FT_PIX_ROUND( org_pos );
  1593. delta1 = cur_pos1 + ( cur_len >> 1 ) - org_center;
  1594. if ( delta1 < 0 )
  1595. delta1 = -delta1;
  1596. cur_pos2 = FT_PIX_ROUND( org_pos + org_len ) - cur_len;
  1597. delta2 = cur_pos2 + ( cur_len >> 1 ) - org_center;
  1598. if ( delta2 < 0 )
  1599. delta2 = -delta2;
  1600. edge->pos = ( delta1 < delta2 ) ? cur_pos1 : cur_pos2;
  1601. edge2->pos = edge->pos + cur_len;
  1602. FT_TRACE5(( " STEM: %d (opos=%.2f) to %d (opos=%.2f)"
  1603. " snapped to (%.2f) and (%.2f)\n",
  1604. edge - edges, edge->opos / 64.0,
  1605. edge2 - edges, edge2->opos / 64.0,
  1606. edge->pos / 64.0, edge2->pos / 64.0 ));
  1607. }
  1608. edge->flags |= AF_EDGE_DONE;
  1609. edge2->flags |= AF_EDGE_DONE;
  1610. if ( edge > edges && edge->pos < edge[-1].pos )
  1611. {
  1612. FT_TRACE5(( " BOUND: %d (pos=%.2f) to (%.2f)\n",
  1613. edge - edges, edge->pos / 64.0, edge[-1].pos / 64.0 ));
  1614. edge->pos = edge[-1].pos;
  1615. }
  1616. }
  1617. }
  1618. /* make sure that lowercase m's maintain their symmetry */
  1619. /* In general, lowercase m's have six vertical edges if they are sans */
  1620. /* serif, or twelve if they are with serifs. This implementation is */
  1621. /* based on that assumption, and seems to work very well with most */
  1622. /* faces. However, if for a certain face this assumption is not */
  1623. /* true, the m is just rendered like before. In addition, any stem */
  1624. /* correction will only be applied to symmetrical glyphs (even if the */
  1625. /* glyph is not an m), so the potential for unwanted distortion is */
  1626. /* relatively low. */
  1627. /* We don't handle horizontal edges since we can't easily assure that */
  1628. /* the third (lowest) stem aligns with the base line; it might end up */
  1629. /* one pixel higher or lower. */
  1630. n_edges = edge_limit - edges;
  1631. if ( dim == AF_DIMENSION_HORZ && ( n_edges == 6 || n_edges == 12 ) )
  1632. {
  1633. AF_Edge edge1, edge2, edge3;
  1634. FT_Pos dist1, dist2, span, delta;
  1635. if ( n_edges == 6 )
  1636. {
  1637. edge1 = edges;
  1638. edge2 = edges + 2;
  1639. edge3 = edges + 4;
  1640. }
  1641. else
  1642. {
  1643. edge1 = edges + 1;
  1644. edge2 = edges + 5;
  1645. edge3 = edges + 9;
  1646. }
  1647. dist1 = edge2->opos - edge1->opos;
  1648. dist2 = edge3->opos - edge2->opos;
  1649. span = dist1 - dist2;
  1650. if ( span < 0 )
  1651. span = -span;
  1652. if ( span < 8 )
  1653. {
  1654. delta = edge3->pos - ( 2 * edge2->pos - edge1->pos );
  1655. edge3->pos -= delta;
  1656. if ( edge3->link )
  1657. edge3->link->pos -= delta;
  1658. /* move the serifs along with the stem */
  1659. if ( n_edges == 12 )
  1660. {
  1661. ( edges + 8 )->pos -= delta;
  1662. ( edges + 11 )->pos -= delta;
  1663. }
  1664. edge3->flags |= AF_EDGE_DONE;
  1665. if ( edge3->link )
  1666. edge3->link->flags |= AF_EDGE_DONE;
  1667. }
  1668. }
  1669. if ( has_serifs || !anchor )
  1670. {
  1671. /*
  1672. * now hint the remaining edges (serifs and single) in order
  1673. * to complete our processing
  1674. */
  1675. for ( edge = edges; edge < edge_limit; edge++ )
  1676. {
  1677. FT_Pos delta;
  1678. if ( edge->flags & AF_EDGE_DONE )
  1679. continue;
  1680. delta = 1000;
  1681. if ( edge->serif )
  1682. {
  1683. delta = edge->serif->opos - edge->opos;
  1684. if ( delta < 0 )
  1685. delta = -delta;
  1686. }
  1687. if ( delta < 64 + 16 )
  1688. {
  1689. af_latin_align_serif_edge( hints, edge->serif, edge );
  1690. FT_TRACE5(( " SERIF: edge %d (opos=%.2f) serif to %d (opos=%.2f)"
  1691. " aligned to (%.2f)\n",
  1692. edge - edges, edge->opos / 64.0,
  1693. edge->serif - edges, edge->serif->opos / 64.0,
  1694. edge->pos / 64.0 ));
  1695. }
  1696. else if ( !anchor )
  1697. {
  1698. edge->pos = FT_PIX_ROUND( edge->opos );
  1699. anchor = edge;
  1700. FT_TRACE5(( " SERIF_ANCHOR: edge %d (opos=%.2f)"
  1701. " snapped to (%.2f)\n",
  1702. edge-edges, edge->opos / 64.0, edge->pos / 64.0 ));
  1703. }
  1704. else
  1705. {
  1706. AF_Edge before, after;
  1707. for ( before = edge - 1; before >= edges; before-- )
  1708. if ( before->flags & AF_EDGE_DONE )
  1709. break;
  1710. for ( after = edge + 1; after < edge_limit; after++ )
  1711. if ( after->flags & AF_EDGE_DONE )
  1712. break;
  1713. if ( before >= edges && before < edge &&
  1714. after < edge_limit && after > edge )
  1715. {
  1716. if ( after->opos == before->opos )
  1717. edge->pos = before->pos;
  1718. else
  1719. edge->pos = before->pos +
  1720. FT_MulDiv( edge->opos - before->opos,
  1721. after->pos - before->pos,
  1722. after->opos - before->opos );
  1723. FT_TRACE5(( " SERIF_LINK1: edge %d (opos=%.2f) snapped to (%.2f)"
  1724. " from %d (opos=%.2f)\n",
  1725. edge - edges, edge->opos / 64.0,
  1726. edge->pos / 64.0,
  1727. before - edges, before->opos / 64.0 ));
  1728. }
  1729. else
  1730. {
  1731. edge->pos = anchor->pos +
  1732. ( ( edge->opos - anchor->opos + 16 ) & ~31 );
  1733. FT_TRACE5(( " SERIF_LINK2: edge %d (opos=%.2f)"
  1734. " snapped to (%.2f)\n",
  1735. edge - edges, edge->opos / 64.0, edge->pos / 64.0 ));
  1736. }
  1737. }
  1738. edge->flags |= AF_EDGE_DONE;
  1739. if ( edge > edges && edge->pos < edge[-1].pos )
  1740. edge->pos = edge[-1].pos;
  1741. if ( edge + 1 < edge_limit &&
  1742. edge[1].flags & AF_EDGE_DONE &&
  1743. edge->pos > edge[1].pos )
  1744. edge->pos = edge[1].pos;
  1745. }
  1746. }
  1747. FT_TRACE5(( "\n" ));
  1748. }
  1749. /* Apply the complete hinting algorithm to a latin glyph. */
  1750. static FT_Error
  1751. af_latin_hints_apply( AF_GlyphHints hints,
  1752. FT_Outline* outline,
  1753. AF_LatinMetrics metrics )
  1754. {
  1755. FT_Error error;
  1756. int dim;
  1757. error = af_glyph_hints_reload( hints, outline );
  1758. if ( error )
  1759. goto Exit;
  1760. /* analyze glyph outline */
  1761. #ifdef AF_CONFIG_OPTION_USE_WARPER
  1762. if ( metrics->root.scaler.render_mode == FT_RENDER_MODE_LIGHT ||
  1763. AF_HINTS_DO_HORIZONTAL( hints ) )
  1764. #else
  1765. if ( AF_HINTS_DO_HORIZONTAL( hints ) )
  1766. #endif
  1767. {
  1768. error = af_latin_hints_detect_features( hints, AF_DIMENSION_HORZ );
  1769. if ( error )
  1770. goto Exit;
  1771. }
  1772. if ( AF_HINTS_DO_VERTICAL( hints ) )
  1773. {
  1774. error = af_latin_hints_detect_features( hints, AF_DIMENSION_VERT );
  1775. if ( error )
  1776. goto Exit;
  1777. af_latin_hints_compute_blue_edges( hints, metrics );
  1778. }
  1779. /* grid-fit the outline */
  1780. for ( dim = 0; dim < AF_DIMENSION_MAX; dim++ )
  1781. {
  1782. #ifdef AF_CONFIG_OPTION_USE_WARPER
  1783. if ( dim == AF_DIMENSION_HORZ &&
  1784. metrics->root.scaler.render_mode == FT_RENDER_MODE_LIGHT )
  1785. {
  1786. AF_WarperRec warper;
  1787. FT_Fixed scale;
  1788. FT_Pos delta;
  1789. af_warper_compute( &warper, hints, (AF_Dimension)dim,
  1790. &scale, &delta );
  1791. af_glyph_hints_scale_dim( hints, (AF_Dimension)dim,
  1792. scale, delta );
  1793. continue;
  1794. }
  1795. #endif
  1796. if ( ( dim == AF_DIMENSION_HORZ && AF_HINTS_DO_HORIZONTAL( hints ) ) ||
  1797. ( dim == AF_DIMENSION_VERT && AF_HINTS_DO_VERTICAL( hints ) ) )
  1798. {
  1799. af_latin_hint_edges( hints, (AF_Dimension)dim );
  1800. af_glyph_hints_align_edge_points( hints, (AF_Dimension)dim );
  1801. af_glyph_hints_align_strong_points( hints, (AF_Dimension)dim );
  1802. af_glyph_hints_align_weak_points( hints, (AF_Dimension)dim );
  1803. }
  1804. }
  1805. af_glyph_hints_save( hints, outline );
  1806. Exit:
  1807. return error;
  1808. }
  1809. /*************************************************************************/
  1810. /*************************************************************************/
  1811. /***** *****/
  1812. /***** L A T I N S C R I P T C L A S S *****/
  1813. /***** *****/
  1814. /*************************************************************************/
  1815. /*************************************************************************/
  1816. /* XXX: this should probably fine tuned to differentiate better between */
  1817. /* scripts... */
  1818. static const AF_Script_UniRangeRec af_latin_uniranges[] =
  1819. {
  1820. AF_UNIRANGE_REC( 0x0020UL, 0x007FUL ), /* Basic Latin (no control chars) */
  1821. AF_UNIRANGE_REC( 0x00A0UL, 0x00FFUL ), /* Latin-1 Supplement (no control chars) */
  1822. AF_UNIRANGE_REC( 0x0100UL, 0x017FUL ), /* Latin Extended-A */
  1823. AF_UNIRANGE_REC( 0x0180UL, 0x024FUL ), /* Latin Extended-B */
  1824. AF_UNIRANGE_REC( 0x0250UL, 0x02AFUL ), /* IPA Extensions */
  1825. AF_UNIRANGE_REC( 0x02B0UL, 0x02FFUL ), /* Spacing Modifier Letters */
  1826. AF_UNIRANGE_REC( 0x0300UL, 0x036FUL ), /* Combining Diacritical Marks */
  1827. AF_UNIRANGE_REC( 0x0370UL, 0x03FFUL ), /* Greek and Coptic */
  1828. AF_UNIRANGE_REC( 0x0400UL, 0x04FFUL ), /* Cyrillic */
  1829. AF_UNIRANGE_REC( 0x0500UL, 0x052FUL ), /* Cyrillic Supplement */
  1830. AF_UNIRANGE_REC( 0x1D00UL, 0x1D7FUL ), /* Phonetic Extensions */
  1831. AF_UNIRANGE_REC( 0x1D80UL, 0x1DBFUL ), /* Phonetic Extensions Supplement */
  1832. AF_UNIRANGE_REC( 0x1DC0UL, 0x1DFFUL ), /* Combining Diacritical Marks Supplement */
  1833. AF_UNIRANGE_REC( 0x1E00UL, 0x1EFFUL ), /* Latin Extended Additional */
  1834. AF_UNIRANGE_REC( 0x1F00UL, 0x1FFFUL ), /* Greek Extended */
  1835. AF_UNIRANGE_REC( 0x2000UL, 0x206FUL ), /* General Punctuation */
  1836. AF_UNIRANGE_REC( 0x2070UL, 0x209FUL ), /* Superscripts and Subscripts */
  1837. AF_UNIRANGE_REC( 0x20A0UL, 0x20CFUL ), /* Currency Symbols */
  1838. AF_UNIRANGE_REC( 0x2150UL, 0x218FUL ), /* Number Forms */
  1839. AF_UNIRANGE_REC( 0x2460UL, 0x24FFUL ), /* Enclosed Alphanumerics */
  1840. AF_UNIRANGE_REC( 0x2C60UL, 0x2C7FUL ), /* Latin Extended-C */
  1841. AF_UNIRANGE_REC( 0x2DE0UL, 0x2DFFUL ), /* Cyrillic Extended-A */
  1842. AF_UNIRANGE_REC( 0xA640UL, 0xA69FUL ), /* Cyrillic Extended-B */
  1843. AF_UNIRANGE_REC( 0xA720UL, 0xA7FFUL ), /* Latin Extended-D */
  1844. AF_UNIRANGE_REC( 0xFB00UL, 0xFB06UL ), /* Alphab. Present. Forms (Latin Ligs) */
  1845. AF_UNIRANGE_REC( 0x1D400UL, 0x1D7FFUL ), /* Mathematical Alphanumeric Symbols */
  1846. AF_UNIRANGE_REC( 0UL, 0UL )
  1847. };
  1848. AF_DEFINE_SCRIPT_CLASS( af_latin_script_class,
  1849. AF_SCRIPT_LATIN,
  1850. af_latin_uniranges,
  1851. sizeof ( AF_LatinMetricsRec ),
  1852. (AF_Script_InitMetricsFunc) af_latin_metrics_init,
  1853. (AF_Script_ScaleMetricsFunc)af_latin_metrics_scale,
  1854. (AF_Script_DoneMetricsFunc) NULL,
  1855. (AF_Script_InitHintsFunc) af_latin_hints_init,
  1856. (AF_Script_ApplyHintsFunc) af_latin_hints_apply
  1857. )
  1858. /* END */