ahglobal.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. /***************************************************************************/
  2. /* */
  3. /* ahglobal.c */
  4. /* */
  5. /* Routines used to compute global metrics automatically (body). */
  6. /* */
  7. /* Copyright 2000-2001, 2002 Catharon Productions Inc. */
  8. /* Author: David Turner */
  9. /* */
  10. /* This file is part of the Catharon Typography Project and shall only */
  11. /* be used, modified, and distributed under the terms of the Catharon */
  12. /* Open Source License that should come with this file under the name */
  13. /* `CatharonLicense.txt'. By continuing to use, modify, or distribute */
  14. /* this file you indicate that you have read the license and */
  15. /* understand and accept it fully. */
  16. /* */
  17. /* Note that this license is compatible with the FreeType license. */
  18. /* */
  19. /***************************************************************************/
  20. #include <ft2build.h>
  21. #include FT_INTERNAL_DEBUG_H
  22. #include "ahglobal.h"
  23. #include "ahglyph.h"
  24. #define MAX_TEST_CHARACTERS 12
  25. static
  26. const char* blue_chars[AH_BLUE_MAX] =
  27. {
  28. "THEZOCQS",
  29. "HEZLOCUS",
  30. "xzroesc",
  31. "xzroesc",
  32. "pqgjy"
  33. };
  34. /* simple insertion sort */
  35. static void
  36. sort_values( FT_Int count,
  37. FT_Pos* table )
  38. {
  39. FT_Int i, j;
  40. FT_Pos swap;
  41. for ( i = 1; i < count; i++ )
  42. {
  43. for ( j = i; j > 0; j-- )
  44. {
  45. if ( table[j] > table[j - 1] )
  46. break;
  47. swap = table[j];
  48. table[j] = table[j - 1];
  49. table[j - 1] = swap;
  50. }
  51. }
  52. }
  53. static FT_Error
  54. ah_hinter_compute_blues( AH_Hinter hinter )
  55. {
  56. AH_Blue blue;
  57. AH_Globals globals = &hinter->globals->design;
  58. FT_Pos flats [MAX_TEST_CHARACTERS];
  59. FT_Pos rounds[MAX_TEST_CHARACTERS];
  60. FT_Int num_flats;
  61. FT_Int num_rounds;
  62. FT_Face face;
  63. FT_GlyphSlot glyph;
  64. FT_Error error;
  65. FT_CharMap charmap;
  66. face = hinter->face;
  67. glyph = face->glyph;
  68. /* save current charmap */
  69. charmap = face->charmap;
  70. /* do we have a Unicode charmap in there? */
  71. error = FT_Select_Charmap( face, FT_ENCODING_UNICODE );
  72. if ( error )
  73. goto Exit;
  74. /* we compute the blues simply by loading each character from the */
  75. /* 'blue_chars[blues]' string, then compute its top-most and */
  76. /* bottom-most points */
  77. AH_LOG(( "blue zones computation\n" ));
  78. AH_LOG(( "------------------------------------------------\n" ));
  79. for ( blue = AH_BLUE_CAPITAL_TOP; blue < AH_BLUE_MAX; blue++ )
  80. {
  81. const char* p = blue_chars[blue];
  82. const char* limit = p + MAX_TEST_CHARACTERS;
  83. FT_Pos *blue_ref, *blue_shoot;
  84. AH_LOG(( "blue %3d: ", blue ));
  85. num_flats = 0;
  86. num_rounds = 0;
  87. for ( ; p < limit; p++ )
  88. {
  89. FT_UInt glyph_index;
  90. FT_Vector* extremum;
  91. FT_Vector* points;
  92. FT_Vector* point_limit;
  93. FT_Vector* point;
  94. FT_Bool round;
  95. /* exit if we reach the end of the string */
  96. if ( !*p )
  97. break;
  98. AH_LOG(( "`%c'", *p ));
  99. /* load the character in the face -- skip unknown or empty ones */
  100. glyph_index = FT_Get_Char_Index( face, (FT_UInt)*p );
  101. if ( glyph_index == 0 )
  102. continue;
  103. error = FT_Load_Glyph( face, glyph_index, FT_LOAD_NO_SCALE );
  104. if ( error || glyph->outline.n_points <= 0 )
  105. continue;
  106. /* now compute min or max point indices and coordinates */
  107. points = glyph->outline.points;
  108. point_limit = points + glyph->outline.n_points;
  109. point = points;
  110. extremum = point;
  111. point++;
  112. if ( AH_IS_TOP_BLUE( blue ) )
  113. {
  114. for ( ; point < point_limit; point++ )
  115. if ( point->y > extremum->y )
  116. extremum = point;
  117. }
  118. else
  119. {
  120. for ( ; point < point_limit; point++ )
  121. if ( point->y < extremum->y )
  122. extremum = point;
  123. }
  124. AH_LOG(( "%5d", (int)extremum->y ));
  125. /* now, check whether the point belongs to a straight or round */
  126. /* segment; we first need to find in which contour the extremum */
  127. /* lies, then see its previous and next points */
  128. {
  129. FT_Int idx = (FT_Int)( extremum - points );
  130. FT_Int n;
  131. FT_Int first, last, prev, next, end;
  132. FT_Pos dist;
  133. last = -1;
  134. first = 0;
  135. for ( n = 0; n < glyph->outline.n_contours; n++ )
  136. {
  137. end = glyph->outline.contours[n];
  138. if ( end >= idx )
  139. {
  140. last = end;
  141. break;
  142. }
  143. first = end + 1;
  144. }
  145. /* XXX: should never happen! */
  146. if ( last < 0 )
  147. continue;
  148. /* now look for the previous and next points that are not on the */
  149. /* same Y coordinate. Threshold the `closeness'... */
  150. prev = idx;
  151. next = prev;
  152. do
  153. {
  154. if ( prev > first )
  155. prev--;
  156. else
  157. prev = last;
  158. dist = points[prev].y - extremum->y;
  159. if ( dist < -5 || dist > 5 )
  160. break;
  161. } while ( prev != idx );
  162. do
  163. {
  164. if ( next < last )
  165. next++;
  166. else
  167. next = first;
  168. dist = points[next].y - extremum->y;
  169. if ( dist < -5 || dist > 5 )
  170. break;
  171. } while ( next != idx );
  172. /* now, set the `round' flag depending on the segment's kind */
  173. round = FT_BOOL(
  174. FT_CURVE_TAG( glyph->outline.tags[prev] ) != FT_CURVE_TAG_ON ||
  175. FT_CURVE_TAG( glyph->outline.tags[next] ) != FT_CURVE_TAG_ON );
  176. AH_LOG(( "%c ", round ? 'r' : 'f' ));
  177. }
  178. if ( round )
  179. rounds[num_rounds++] = extremum->y;
  180. else
  181. flats[num_flats++] = extremum->y;
  182. }
  183. AH_LOG(( "\n" ));
  184. /* we have computed the contents of the `rounds' and `flats' tables, */
  185. /* now determine the reference and overshoot position of the blue; */
  186. /* we simply take the median value after a simple short */
  187. sort_values( num_rounds, rounds );
  188. sort_values( num_flats, flats );
  189. blue_ref = globals->blue_refs + blue;
  190. blue_shoot = globals->blue_shoots + blue;
  191. if ( num_flats == 0 && num_rounds == 0 )
  192. {
  193. *blue_ref = -10000;
  194. *blue_shoot = -10000;
  195. }
  196. else if ( num_flats == 0 )
  197. {
  198. *blue_ref =
  199. *blue_shoot = rounds[num_rounds / 2];
  200. }
  201. else if ( num_rounds == 0 )
  202. {
  203. *blue_ref =
  204. *blue_shoot = flats[num_flats / 2];
  205. }
  206. else
  207. {
  208. *blue_ref = flats[num_flats / 2];
  209. *blue_shoot = rounds[num_rounds / 2];
  210. }
  211. /* there are sometimes problems: if the overshoot position of top */
  212. /* zones is under its reference position, or the opposite for bottom */
  213. /* zones. We must thus check everything there and correct the errors */
  214. if ( *blue_shoot != *blue_ref )
  215. {
  216. FT_Pos ref = *blue_ref;
  217. FT_Pos shoot = *blue_shoot;
  218. FT_Bool over_ref = FT_BOOL( shoot > ref );
  219. if ( AH_IS_TOP_BLUE( blue ) ^ over_ref )
  220. *blue_shoot = *blue_ref = ( shoot + ref ) / 2;
  221. }
  222. AH_LOG(( "-- ref = %ld, shoot = %ld\n", *blue_ref, *blue_shoot ));
  223. }
  224. /* reset original face charmap */
  225. FT_Set_Charmap( face, charmap );
  226. error = 0;
  227. Exit:
  228. return error;
  229. }
  230. static FT_Error
  231. ah_hinter_compute_widths( AH_Hinter hinter )
  232. {
  233. /* scan the array of segments in each direction */
  234. AH_Outline outline = hinter->glyph;
  235. AH_Segment segments;
  236. AH_Segment limit;
  237. AH_Globals globals = &hinter->globals->design;
  238. FT_Pos* widths;
  239. FT_Int dimension;
  240. FT_Int* p_num_widths;
  241. FT_Error error = 0;
  242. FT_Pos edge_distance_threshold = 32000;
  243. globals->num_widths = 0;
  244. globals->num_heights = 0;
  245. /* For now, compute the standard width and height from the `o' */
  246. /* character. I started computing the stem width of the `i' and the */
  247. /* stem height of the "-", but it wasn't too good. Moreover, we now */
  248. /* have a single character that gives us standard width and height. */
  249. {
  250. FT_UInt glyph_index;
  251. glyph_index = FT_Get_Char_Index( hinter->face, 'o' );
  252. if ( glyph_index == 0 )
  253. return 0;
  254. error = FT_Load_Glyph( hinter->face, glyph_index, FT_LOAD_NO_SCALE );
  255. if ( error )
  256. goto Exit;
  257. error = ah_outline_load( hinter->glyph, hinter->face );
  258. if ( error )
  259. goto Exit;
  260. ah_outline_compute_segments( hinter->glyph );
  261. ah_outline_link_segments( hinter->glyph );
  262. }
  263. segments = outline->horz_segments;
  264. limit = segments + outline->num_hsegments;
  265. widths = globals->heights;
  266. p_num_widths = &globals->num_heights;
  267. for ( dimension = 1; dimension >= 0; dimension-- )
  268. {
  269. AH_Segment seg = segments;
  270. AH_Segment link;
  271. FT_Int num_widths = 0;
  272. for ( ; seg < limit; seg++ )
  273. {
  274. link = seg->link;
  275. /* we only consider stem segments there! */
  276. if ( link && link->link == seg && link > seg )
  277. {
  278. FT_Pos dist;
  279. dist = seg->pos - link->pos;
  280. if ( dist < 0 )
  281. dist = -dist;
  282. if ( num_widths < AH_MAX_WIDTHS )
  283. widths[num_widths++] = dist;
  284. }
  285. }
  286. sort_values( num_widths, widths );
  287. *p_num_widths = num_widths;
  288. /* we will now try to find the smallest width */
  289. if ( num_widths > 0 && widths[0] < edge_distance_threshold )
  290. edge_distance_threshold = widths[0];
  291. segments = outline->vert_segments;
  292. limit = segments + outline->num_vsegments;
  293. widths = globals->widths;
  294. p_num_widths = &globals->num_widths;
  295. }
  296. /* Now, compute the edge distance threshold as a fraction of the */
  297. /* smallest width in the font. Set it in `hinter.glyph' too! */
  298. if ( edge_distance_threshold == 32000 )
  299. edge_distance_threshold = 50;
  300. /* let's try 20% */
  301. hinter->glyph->edge_distance_threshold = edge_distance_threshold / 5;
  302. Exit:
  303. return error;
  304. }
  305. FT_LOCAL_DEF( FT_Error )
  306. ah_hinter_compute_globals( AH_Hinter hinter )
  307. {
  308. return ah_hinter_compute_widths( hinter ) ||
  309. ah_hinter_compute_blues ( hinter );
  310. }
  311. /* END */