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

/src/compiler/android-ndk/jni/freetype/src/autofit/aflatin.c

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