1 | /**************************************************************************** |
2 | * |
3 | * afcjk.c |
4 | * |
5 | * Auto-fitter hinting routines for CJK writing system (body). |
6 | * |
7 | * Copyright (C) 2006-2023 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 | |
18 | /* |
19 | * The algorithm is based on akito's autohint patch, archived at |
20 | * |
21 | * https://web.archive.org/web/20051219160454/http://www.kde.gr.jp:80/~akito/patch/freetype2/2.1.7/ |
22 | * |
23 | */ |
24 | |
25 | #include <freetype/ftadvanc.h> |
26 | #include <freetype/internal/ftdebug.h> |
27 | |
28 | #include "afglobal.h" |
29 | #include "aflatin.h" |
30 | #include "afcjk.h" |
31 | |
32 | |
33 | #ifdef AF_CONFIG_OPTION_CJK |
34 | |
35 | #undef AF_CONFIG_OPTION_CJK_BLUE_HANI_VERT |
36 | |
37 | #include "aferrors.h" |
38 | |
39 | |
40 | /************************************************************************** |
41 | * |
42 | * The macro FT_COMPONENT is used in trace mode. It is an implicit |
43 | * parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log |
44 | * messages during execution. |
45 | */ |
46 | #undef FT_COMPONENT |
47 | #define FT_COMPONENT afcjk |
48 | |
49 | |
50 | /*************************************************************************/ |
51 | /*************************************************************************/ |
52 | /***** *****/ |
53 | /***** C J K G L O B A L M E T R I C S *****/ |
54 | /***** *****/ |
55 | /*************************************************************************/ |
56 | /*************************************************************************/ |
57 | |
58 | |
59 | /* Basically the Latin version with AF_CJKMetrics */ |
60 | /* to replace AF_LatinMetrics. */ |
61 | |
62 | FT_LOCAL_DEF( void ) |
63 | af_cjk_metrics_init_widths( AF_CJKMetrics metrics, |
64 | FT_Face face ) |
65 | { |
66 | /* scan the array of segments in each direction */ |
67 | AF_GlyphHintsRec hints[1]; |
68 | |
69 | |
70 | FT_TRACE5(( "\n" )); |
71 | FT_TRACE5(( "cjk standard widths computation (style `%s')\n" , |
72 | af_style_names[metrics->root.style_class->style] )); |
73 | FT_TRACE5(( "===================================================\n" )); |
74 | FT_TRACE5(( "\n" )); |
75 | |
76 | af_glyph_hints_init( hints, face->memory ); |
77 | |
78 | metrics->axis[AF_DIMENSION_HORZ].width_count = 0; |
79 | metrics->axis[AF_DIMENSION_VERT].width_count = 0; |
80 | |
81 | { |
82 | FT_Error error; |
83 | FT_ULong glyph_index; |
84 | int dim; |
85 | AF_CJKMetricsRec dummy[1]; |
86 | AF_Scaler scaler = &dummy->root.scaler; |
87 | |
88 | AF_StyleClass style_class = metrics->root.style_class; |
89 | AF_ScriptClass script_class = af_script_classes[style_class->script]; |
90 | |
91 | /* If HarfBuzz is not available, we need a pointer to a single */ |
92 | /* unsigned long value. */ |
93 | #ifdef FT_CONFIG_OPTION_USE_HARFBUZZ |
94 | void* shaper_buf; |
95 | #else |
96 | FT_ULong shaper_buf_; |
97 | void* shaper_buf = &shaper_buf_; |
98 | #endif |
99 | |
100 | const char* p; |
101 | |
102 | #ifdef FT_DEBUG_LEVEL_TRACE |
103 | FT_ULong ch = 0; |
104 | #endif |
105 | |
106 | p = script_class->standard_charstring; |
107 | |
108 | #ifdef FT_CONFIG_OPTION_USE_HARFBUZZ |
109 | shaper_buf = af_shaper_buf_create( face ); |
110 | #endif |
111 | |
112 | /* We check a list of standard characters. The first match wins. */ |
113 | |
114 | glyph_index = 0; |
115 | while ( *p ) |
116 | { |
117 | unsigned int num_idx; |
118 | |
119 | #ifdef FT_DEBUG_LEVEL_TRACE |
120 | const char* p_old; |
121 | #endif |
122 | |
123 | |
124 | while ( *p == ' ' ) |
125 | p++; |
126 | |
127 | #ifdef FT_DEBUG_LEVEL_TRACE |
128 | p_old = p; |
129 | GET_UTF8_CHAR( ch, p_old ); |
130 | #endif |
131 | |
132 | /* reject input that maps to more than a single glyph */ |
133 | p = af_shaper_get_cluster( p, &metrics->root, shaper_buf, &num_idx ); |
134 | if ( num_idx > 1 ) |
135 | continue; |
136 | |
137 | /* otherwise exit loop if we have a result */ |
138 | glyph_index = af_shaper_get_elem( &metrics->root, |
139 | shaper_buf, |
140 | 0, |
141 | NULL, |
142 | NULL ); |
143 | if ( glyph_index ) |
144 | break; |
145 | } |
146 | |
147 | af_shaper_buf_destroy( face, shaper_buf ); |
148 | |
149 | if ( !glyph_index ) |
150 | goto Exit; |
151 | |
152 | if ( !glyph_index ) |
153 | goto Exit; |
154 | |
155 | FT_TRACE5(( "standard character: U+%04lX (glyph index %ld)\n" , |
156 | ch, glyph_index )); |
157 | |
158 | error = FT_Load_Glyph( face, glyph_index, FT_LOAD_NO_SCALE ); |
159 | if ( error || face->glyph->outline.n_points <= 0 ) |
160 | goto Exit; |
161 | |
162 | FT_ZERO( dummy ); |
163 | |
164 | dummy->units_per_em = metrics->units_per_em; |
165 | |
166 | scaler->x_scale = 0x10000L; |
167 | scaler->y_scale = 0x10000L; |
168 | scaler->x_delta = 0; |
169 | scaler->y_delta = 0; |
170 | |
171 | scaler->face = face; |
172 | scaler->render_mode = FT_RENDER_MODE_NORMAL; |
173 | scaler->flags = 0; |
174 | |
175 | af_glyph_hints_rescale( hints, (AF_StyleMetrics)dummy ); |
176 | |
177 | error = af_glyph_hints_reload( hints, &face->glyph->outline ); |
178 | if ( error ) |
179 | goto Exit; |
180 | |
181 | for ( dim = 0; dim < AF_DIMENSION_MAX; dim++ ) |
182 | { |
183 | AF_CJKAxis axis = &metrics->axis[dim]; |
184 | AF_AxisHints axhints = &hints->axis[dim]; |
185 | AF_Segment seg, limit, link; |
186 | FT_UInt num_widths = 0; |
187 | |
188 | |
189 | error = af_latin_hints_compute_segments( hints, |
190 | (AF_Dimension)dim ); |
191 | if ( error ) |
192 | goto Exit; |
193 | |
194 | /* |
195 | * We assume that the glyphs selected for the stem width |
196 | * computation are `featureless' enough so that the linking |
197 | * algorithm works fine without adjustments of its scoring |
198 | * function. |
199 | */ |
200 | af_latin_hints_link_segments( hints, |
201 | 0, |
202 | NULL, |
203 | (AF_Dimension)dim ); |
204 | |
205 | seg = axhints->segments; |
206 | limit = seg + axhints->num_segments; |
207 | |
208 | for ( ; seg < limit; seg++ ) |
209 | { |
210 | link = seg->link; |
211 | |
212 | /* we only consider stem segments there! */ |
213 | if ( link && link->link == seg && link > seg ) |
214 | { |
215 | FT_Pos dist; |
216 | |
217 | |
218 | dist = seg->pos - link->pos; |
219 | if ( dist < 0 ) |
220 | dist = -dist; |
221 | |
222 | if ( num_widths < AF_CJK_MAX_WIDTHS ) |
223 | axis->widths[num_widths++].org = dist; |
224 | } |
225 | } |
226 | |
227 | /* this also replaces multiple almost identical stem widths */ |
228 | /* with a single one (the value 100 is heuristic) */ |
229 | af_sort_and_quantize_widths( &num_widths, axis->widths, |
230 | dummy->units_per_em / 100 ); |
231 | axis->width_count = num_widths; |
232 | } |
233 | |
234 | Exit: |
235 | for ( dim = 0; dim < AF_DIMENSION_MAX; dim++ ) |
236 | { |
237 | AF_CJKAxis axis = &metrics->axis[dim]; |
238 | FT_Pos stdw; |
239 | |
240 | |
241 | stdw = ( axis->width_count > 0 ) ? axis->widths[0].org |
242 | : AF_LATIN_CONSTANT( metrics, 50 ); |
243 | |
244 | /* let's try 20% of the smallest width */ |
245 | axis->edge_distance_threshold = stdw / 5; |
246 | axis->standard_width = stdw; |
247 | axis->extra_light = 0; |
248 | |
249 | #ifdef FT_DEBUG_LEVEL_TRACE |
250 | { |
251 | FT_UInt i; |
252 | |
253 | |
254 | FT_TRACE5(( "%s widths:\n" , |
255 | dim == AF_DIMENSION_VERT ? "horizontal" |
256 | : "vertical" )); |
257 | |
258 | FT_TRACE5(( " %ld (standard)" , axis->standard_width )); |
259 | for ( i = 1; i < axis->width_count; i++ ) |
260 | FT_TRACE5(( " %ld" , axis->widths[i].org )); |
261 | |
262 | FT_TRACE5(( "\n" )); |
263 | } |
264 | #endif |
265 | } |
266 | } |
267 | |
268 | FT_TRACE5(( "\n" )); |
269 | |
270 | af_glyph_hints_done( hints ); |
271 | } |
272 | |
273 | |
274 | /* Find all blue zones. */ |
275 | |
276 | static void |
277 | af_cjk_metrics_init_blues( AF_CJKMetrics metrics, |
278 | FT_Face face ) |
279 | { |
280 | FT_Pos fills[AF_BLUE_STRING_MAX_LEN]; |
281 | FT_Pos flats[AF_BLUE_STRING_MAX_LEN]; |
282 | |
283 | FT_UInt num_fills; |
284 | FT_UInt num_flats; |
285 | |
286 | FT_Bool fill; |
287 | |
288 | AF_CJKBlue blue; |
289 | FT_Error error; |
290 | AF_CJKAxis axis; |
291 | FT_Outline outline; |
292 | |
293 | AF_StyleClass sc = metrics->root.style_class; |
294 | |
295 | AF_Blue_Stringset bss = sc->blue_stringset; |
296 | const AF_Blue_StringRec* bs = &af_blue_stringsets[bss]; |
297 | |
298 | /* If HarfBuzz is not available, we need a pointer to a single */ |
299 | /* unsigned long value. */ |
300 | #ifdef FT_CONFIG_OPTION_USE_HARFBUZZ |
301 | void* shaper_buf; |
302 | #else |
303 | FT_ULong shaper_buf_; |
304 | void* shaper_buf = &shaper_buf_; |
305 | #endif |
306 | |
307 | |
308 | /* we walk over the blue character strings as specified in the */ |
309 | /* style's entry in the `af_blue_stringset' array, computing its */ |
310 | /* extremum points (depending on the string properties) */ |
311 | |
312 | FT_TRACE5(( "cjk blue zones computation\n" )); |
313 | FT_TRACE5(( "==========================\n" )); |
314 | FT_TRACE5(( "\n" )); |
315 | |
316 | #ifdef FT_CONFIG_OPTION_USE_HARFBUZZ |
317 | shaper_buf = af_shaper_buf_create( face ); |
318 | #endif |
319 | |
320 | for ( ; bs->string != AF_BLUE_STRING_MAX; bs++ ) |
321 | { |
322 | const char* p = &af_blue_strings[bs->string]; |
323 | FT_Pos* blue_ref; |
324 | FT_Pos* blue_shoot; |
325 | |
326 | |
327 | if ( AF_CJK_IS_HORIZ_BLUE( bs ) ) |
328 | axis = &metrics->axis[AF_DIMENSION_HORZ]; |
329 | else |
330 | axis = &metrics->axis[AF_DIMENSION_VERT]; |
331 | |
332 | #ifdef FT_DEBUG_LEVEL_TRACE |
333 | { |
334 | FT_String* cjk_blue_name[4] = |
335 | { |
336 | (FT_String*)"bottom" , /* -- , -- */ |
337 | (FT_String*)"top" , /* -- , TOP */ |
338 | (FT_String*)"left" , /* HORIZ, -- */ |
339 | (FT_String*)"right" /* HORIZ, TOP */ |
340 | }; |
341 | |
342 | |
343 | FT_TRACE5(( "blue zone %d (%s):\n" , |
344 | axis->blue_count, |
345 | cjk_blue_name[AF_CJK_IS_HORIZ_BLUE( bs ) | |
346 | AF_CJK_IS_TOP_BLUE( bs ) ] )); |
347 | } |
348 | #endif /* FT_DEBUG_LEVEL_TRACE */ |
349 | |
350 | num_fills = 0; |
351 | num_flats = 0; |
352 | |
353 | fill = 1; /* start with characters that define fill values */ |
354 | FT_TRACE5(( " [overshoot values]\n" )); |
355 | |
356 | while ( *p ) |
357 | { |
358 | FT_ULong glyph_index; |
359 | FT_Pos best_pos; /* same as points.y or points.x, resp. */ |
360 | FT_Int best_point; |
361 | FT_Vector* points; |
362 | |
363 | unsigned int num_idx; |
364 | |
365 | #ifdef FT_DEBUG_LEVEL_TRACE |
366 | const char* p_old; |
367 | FT_ULong ch; |
368 | #endif |
369 | |
370 | |
371 | while ( *p == ' ' ) |
372 | p++; |
373 | |
374 | #ifdef FT_DEBUG_LEVEL_TRACE |
375 | p_old = p; |
376 | GET_UTF8_CHAR( ch, p_old ); |
377 | #endif |
378 | |
379 | /* switch to characters that define flat values */ |
380 | if ( *p == '|' ) |
381 | { |
382 | fill = 0; |
383 | FT_TRACE5(( " [reference values]\n" )); |
384 | p++; |
385 | continue; |
386 | } |
387 | |
388 | /* reject input that maps to more than a single glyph */ |
389 | p = af_shaper_get_cluster( p, &metrics->root, shaper_buf, &num_idx ); |
390 | if ( num_idx > 1 ) |
391 | continue; |
392 | |
393 | /* load the character in the face -- skip unknown or empty ones */ |
394 | glyph_index = af_shaper_get_elem( &metrics->root, |
395 | shaper_buf, |
396 | 0, |
397 | NULL, |
398 | NULL ); |
399 | if ( glyph_index == 0 ) |
400 | { |
401 | FT_TRACE5(( " U+%04lX unavailable\n" , ch )); |
402 | continue; |
403 | } |
404 | |
405 | error = FT_Load_Glyph( face, glyph_index, FT_LOAD_NO_SCALE ); |
406 | outline = face->glyph->outline; |
407 | if ( error || outline.n_points <= 2 ) |
408 | { |
409 | FT_TRACE5(( " U+%04lX contains no (usable) outlines\n" , ch )); |
410 | continue; |
411 | } |
412 | |
413 | /* now compute min or max point indices and coordinates */ |
414 | points = outline.points; |
415 | best_point = -1; |
416 | best_pos = 0; /* make compiler happy */ |
417 | |
418 | { |
419 | FT_Int nn; |
420 | FT_Int pp, first, last; |
421 | |
422 | |
423 | last = -1; |
424 | for ( nn = 0; nn < outline.n_contours; nn++ ) |
425 | { |
426 | first = last + 1; |
427 | last = outline.contours[nn]; |
428 | |
429 | /* Avoid single-point contours since they are never rasterized. */ |
430 | /* In some fonts, they correspond to mark attachment points */ |
431 | /* which are way outside of the glyph's real outline. */ |
432 | if ( last <= first ) |
433 | continue; |
434 | |
435 | if ( AF_CJK_IS_HORIZ_BLUE( bs ) ) |
436 | { |
437 | if ( AF_CJK_IS_RIGHT_BLUE( bs ) ) |
438 | { |
439 | for ( pp = first; pp <= last; pp++ ) |
440 | if ( best_point < 0 || points[pp].x > best_pos ) |
441 | { |
442 | best_point = pp; |
443 | best_pos = points[pp].x; |
444 | } |
445 | } |
446 | else |
447 | { |
448 | for ( pp = first; pp <= last; pp++ ) |
449 | if ( best_point < 0 || points[pp].x < best_pos ) |
450 | { |
451 | best_point = pp; |
452 | best_pos = points[pp].x; |
453 | } |
454 | } |
455 | } |
456 | else |
457 | { |
458 | if ( AF_CJK_IS_TOP_BLUE( bs ) ) |
459 | { |
460 | for ( pp = first; pp <= last; pp++ ) |
461 | if ( best_point < 0 || points[pp].y > best_pos ) |
462 | { |
463 | best_point = pp; |
464 | best_pos = points[pp].y; |
465 | } |
466 | } |
467 | else |
468 | { |
469 | for ( pp = first; pp <= last; pp++ ) |
470 | if ( best_point < 0 || points[pp].y < best_pos ) |
471 | { |
472 | best_point = pp; |
473 | best_pos = points[pp].y; |
474 | } |
475 | } |
476 | } |
477 | } |
478 | |
479 | FT_TRACE5(( " U+%04lX: best_pos = %5ld\n" , ch, best_pos )); |
480 | } |
481 | |
482 | if ( fill ) |
483 | fills[num_fills++] = best_pos; |
484 | else |
485 | flats[num_flats++] = best_pos; |
486 | |
487 | } /* end while loop */ |
488 | |
489 | if ( num_flats == 0 && num_fills == 0 ) |
490 | { |
491 | /* |
492 | * we couldn't find a single glyph to compute this blue zone, |
493 | * we will simply ignore it then |
494 | */ |
495 | FT_TRACE5(( " empty\n" )); |
496 | continue; |
497 | } |
498 | |
499 | /* we have computed the contents of the `fill' and `flats' tables, */ |
500 | /* now determine the reference and overshoot position of the blue -- */ |
501 | /* we simply take the median value after a simple sort */ |
502 | af_sort_pos( num_fills, fills ); |
503 | af_sort_pos( num_flats, flats ); |
504 | |
505 | blue = &axis->blues[axis->blue_count]; |
506 | blue_ref = &blue->ref.org; |
507 | blue_shoot = &blue->shoot.org; |
508 | |
509 | axis->blue_count++; |
510 | |
511 | if ( num_flats == 0 ) |
512 | { |
513 | *blue_ref = |
514 | *blue_shoot = fills[num_fills / 2]; |
515 | } |
516 | else if ( num_fills == 0 ) |
517 | { |
518 | *blue_ref = |
519 | *blue_shoot = flats[num_flats / 2]; |
520 | } |
521 | else |
522 | { |
523 | *blue_ref = fills[num_fills / 2]; |
524 | *blue_shoot = flats[num_flats / 2]; |
525 | } |
526 | |
527 | /* make sure blue_ref >= blue_shoot for top/right or */ |
528 | /* vice versa for bottom/left */ |
529 | if ( *blue_shoot != *blue_ref ) |
530 | { |
531 | FT_Pos ref = *blue_ref; |
532 | FT_Pos shoot = *blue_shoot; |
533 | FT_Bool under_ref = FT_BOOL( shoot < ref ); |
534 | |
535 | |
536 | /* AF_CJK_IS_TOP_BLUE covers `right' and `top' */ |
537 | if ( AF_CJK_IS_TOP_BLUE( bs ) ^ under_ref ) |
538 | { |
539 | *blue_ref = |
540 | *blue_shoot = ( shoot + ref ) / 2; |
541 | |
542 | FT_TRACE5(( " [reference smaller than overshoot," |
543 | " taking mean value]\n" )); |
544 | } |
545 | } |
546 | |
547 | blue->flags = 0; |
548 | if ( AF_CJK_IS_TOP_BLUE( bs ) ) |
549 | blue->flags |= AF_CJK_BLUE_TOP; |
550 | |
551 | FT_TRACE5(( " -> reference = %ld\n" , *blue_ref )); |
552 | FT_TRACE5(( " overshoot = %ld\n" , *blue_shoot )); |
553 | |
554 | } /* end for loop */ |
555 | |
556 | af_shaper_buf_destroy( face, shaper_buf ); |
557 | |
558 | FT_TRACE5(( "\n" )); |
559 | |
560 | return; |
561 | } |
562 | |
563 | |
564 | /* Basically the Latin version with type AF_CJKMetrics for metrics. */ |
565 | |
566 | FT_LOCAL_DEF( void ) |
567 | af_cjk_metrics_check_digits( AF_CJKMetrics metrics, |
568 | FT_Face face ) |
569 | { |
570 | FT_Bool started = 0, same_width = 1; |
571 | FT_Long advance = 0, old_advance = 0; |
572 | |
573 | /* If HarfBuzz is not available, we need a pointer to a single */ |
574 | /* unsigned long value. */ |
575 | #ifdef FT_CONFIG_OPTION_USE_HARFBUZZ |
576 | void* shaper_buf; |
577 | #else |
578 | FT_ULong shaper_buf_; |
579 | void* shaper_buf = &shaper_buf_; |
580 | #endif |
581 | |
582 | /* in all supported charmaps, digits have character codes 0x30-0x39 */ |
583 | const char digits[] = "0 1 2 3 4 5 6 7 8 9" ; |
584 | const char* p; |
585 | |
586 | |
587 | p = digits; |
588 | |
589 | #ifdef FT_CONFIG_OPTION_USE_HARFBUZZ |
590 | shaper_buf = af_shaper_buf_create( face ); |
591 | #endif |
592 | |
593 | while ( *p ) |
594 | { |
595 | FT_ULong glyph_index; |
596 | unsigned int num_idx; |
597 | |
598 | |
599 | /* reject input that maps to more than a single glyph */ |
600 | p = af_shaper_get_cluster( p, &metrics->root, shaper_buf, &num_idx ); |
601 | if ( num_idx > 1 ) |
602 | continue; |
603 | |
604 | glyph_index = af_shaper_get_elem( &metrics->root, |
605 | shaper_buf, |
606 | 0, |
607 | &advance, |
608 | NULL ); |
609 | if ( !glyph_index ) |
610 | continue; |
611 | |
612 | if ( started ) |
613 | { |
614 | if ( advance != old_advance ) |
615 | { |
616 | same_width = 0; |
617 | break; |
618 | } |
619 | } |
620 | else |
621 | { |
622 | old_advance = advance; |
623 | started = 1; |
624 | } |
625 | } |
626 | |
627 | af_shaper_buf_destroy( face, shaper_buf ); |
628 | |
629 | metrics->root.digits_have_same_width = same_width; |
630 | } |
631 | |
632 | |
633 | /* Initialize global metrics. */ |
634 | |
635 | FT_LOCAL_DEF( FT_Error ) |
636 | af_cjk_metrics_init( AF_StyleMetrics metrics_, /* AF_CJKMetrics */ |
637 | FT_Face face ) |
638 | { |
639 | AF_CJKMetrics metrics = (AF_CJKMetrics)metrics_; |
640 | FT_CharMap oldmap = face->charmap; |
641 | |
642 | |
643 | metrics->units_per_em = face->units_per_EM; |
644 | |
645 | if ( !FT_Select_Charmap( face, FT_ENCODING_UNICODE ) ) |
646 | { |
647 | af_cjk_metrics_init_widths( metrics, face ); |
648 | af_cjk_metrics_init_blues( metrics, face ); |
649 | af_cjk_metrics_check_digits( metrics, face ); |
650 | } |
651 | |
652 | face->charmap = oldmap; |
653 | return FT_Err_Ok; |
654 | } |
655 | |
656 | |
657 | /* Adjust scaling value, then scale and shift widths */ |
658 | /* and blue zones (if applicable) for given dimension. */ |
659 | |
660 | static void |
661 | af_cjk_metrics_scale_dim( AF_CJKMetrics metrics, |
662 | AF_Scaler scaler, |
663 | AF_Dimension dim ) |
664 | { |
665 | FT_Fixed scale; |
666 | FT_Pos delta; |
667 | AF_CJKAxis axis; |
668 | FT_UInt nn; |
669 | |
670 | |
671 | if ( dim == AF_DIMENSION_HORZ ) |
672 | { |
673 | scale = scaler->x_scale; |
674 | delta = scaler->x_delta; |
675 | } |
676 | else |
677 | { |
678 | scale = scaler->y_scale; |
679 | delta = scaler->y_delta; |
680 | } |
681 | |
682 | axis = &metrics->axis[dim]; |
683 | |
684 | if ( axis->org_scale == scale && axis->org_delta == delta ) |
685 | return; |
686 | |
687 | axis->org_scale = scale; |
688 | axis->org_delta = delta; |
689 | |
690 | axis->scale = scale; |
691 | axis->delta = delta; |
692 | |
693 | /* scale the blue zones */ |
694 | for ( nn = 0; nn < axis->blue_count; nn++ ) |
695 | { |
696 | AF_CJKBlue blue = &axis->blues[nn]; |
697 | FT_Pos dist; |
698 | |
699 | |
700 | blue->ref.cur = FT_MulFix( blue->ref.org, scale ) + delta; |
701 | blue->ref.fit = blue->ref.cur; |
702 | blue->shoot.cur = FT_MulFix( blue->shoot.org, scale ) + delta; |
703 | blue->shoot.fit = blue->shoot.cur; |
704 | blue->flags &= ~AF_CJK_BLUE_ACTIVE; |
705 | |
706 | /* a blue zone is only active if it is less than 3/4 pixels tall */ |
707 | dist = FT_MulFix( blue->ref.org - blue->shoot.org, scale ); |
708 | if ( dist <= 48 && dist >= -48 ) |
709 | { |
710 | FT_Pos delta1, delta2; |
711 | |
712 | |
713 | blue->ref.fit = FT_PIX_ROUND( blue->ref.cur ); |
714 | |
715 | /* shoot is under shoot for cjk */ |
716 | delta1 = FT_DivFix( blue->ref.fit, scale ) - blue->shoot.org; |
717 | delta2 = delta1; |
718 | if ( delta1 < 0 ) |
719 | delta2 = -delta2; |
720 | |
721 | delta2 = FT_MulFix( delta2, scale ); |
722 | |
723 | FT_TRACE5(( "delta: %ld" , delta1 )); |
724 | if ( delta2 < 32 ) |
725 | delta2 = 0; |
726 | #if 0 |
727 | else if ( delta2 < 64 ) |
728 | delta2 = 32 + ( ( ( delta2 - 32 ) + 16 ) & ~31 ); |
729 | #endif |
730 | else |
731 | delta2 = FT_PIX_ROUND( delta2 ); |
732 | FT_TRACE5(( "/%ld\n" , delta2 )); |
733 | |
734 | if ( delta1 < 0 ) |
735 | delta2 = -delta2; |
736 | |
737 | blue->shoot.fit = blue->ref.fit - delta2; |
738 | |
739 | FT_TRACE5(( ">> active cjk blue zone %c%d[%ld/%ld]:\n" , |
740 | ( dim == AF_DIMENSION_HORZ ) ? 'H' : 'V', |
741 | nn, blue->ref.org, blue->shoot.org )); |
742 | FT_TRACE5(( " ref: cur=%.2f fit=%.2f\n" , |
743 | (double)blue->ref.cur / 64, |
744 | (double)blue->ref.fit / 64 )); |
745 | FT_TRACE5(( " shoot: cur=%.2f fit=%.2f\n" , |
746 | (double)blue->shoot.cur / 64, |
747 | (double)blue->shoot.fit / 64 )); |
748 | |
749 | blue->flags |= AF_CJK_BLUE_ACTIVE; |
750 | } |
751 | } |
752 | } |
753 | |
754 | |
755 | /* Scale global values in both directions. */ |
756 | |
757 | FT_LOCAL_DEF( void ) |
758 | af_cjk_metrics_scale( AF_StyleMetrics metrics_, /* AF_CJKMetrics */ |
759 | AF_Scaler scaler ) |
760 | { |
761 | AF_CJKMetrics metrics = (AF_CJKMetrics)metrics_; |
762 | |
763 | |
764 | /* we copy the whole structure since the x and y scaling values */ |
765 | /* are not modified, contrary to e.g. the `latin' auto-hinter */ |
766 | metrics->root.scaler = *scaler; |
767 | |
768 | af_cjk_metrics_scale_dim( metrics, scaler, AF_DIMENSION_HORZ ); |
769 | af_cjk_metrics_scale_dim( metrics, scaler, AF_DIMENSION_VERT ); |
770 | } |
771 | |
772 | |
773 | /* Extract standard_width from writing system/script specific */ |
774 | /* metrics class. */ |
775 | |
776 | FT_CALLBACK_DEF( void ) |
777 | af_cjk_get_standard_widths( AF_StyleMetrics metrics_, /* AF_CJKMetrics */ |
778 | FT_Pos* stdHW, |
779 | FT_Pos* stdVW ) |
780 | { |
781 | AF_CJKMetrics metrics = (AF_CJKMetrics)metrics_; |
782 | |
783 | |
784 | if ( stdHW ) |
785 | *stdHW = metrics->axis[AF_DIMENSION_VERT].standard_width; |
786 | |
787 | if ( stdVW ) |
788 | *stdVW = metrics->axis[AF_DIMENSION_HORZ].standard_width; |
789 | } |
790 | |
791 | |
792 | /*************************************************************************/ |
793 | /*************************************************************************/ |
794 | /***** *****/ |
795 | /***** C J K G L Y P H A N A L Y S I S *****/ |
796 | /***** *****/ |
797 | /*************************************************************************/ |
798 | /*************************************************************************/ |
799 | |
800 | |
801 | /* Walk over all contours and compute its segments. */ |
802 | |
803 | static FT_Error |
804 | af_cjk_hints_compute_segments( AF_GlyphHints hints, |
805 | AF_Dimension dim ) |
806 | { |
807 | AF_AxisHints axis = &hints->axis[dim]; |
808 | AF_Segment segments = axis->segments; |
809 | AF_Segment segment_limit = FT_OFFSET( segments, axis->num_segments ); |
810 | FT_Error error; |
811 | AF_Segment seg; |
812 | |
813 | |
814 | error = af_latin_hints_compute_segments( hints, dim ); |
815 | if ( error ) |
816 | return error; |
817 | |
818 | /* a segment is round if it doesn't have successive */ |
819 | /* on-curve points. */ |
820 | for ( seg = segments; seg < segment_limit; seg++ ) |
821 | { |
822 | AF_Point pt = seg->first; |
823 | AF_Point last = seg->last; |
824 | FT_UInt f0 = pt->flags & AF_FLAG_CONTROL; |
825 | FT_UInt f1; |
826 | |
827 | |
828 | seg->flags &= ~AF_EDGE_ROUND; |
829 | |
830 | for ( ; pt != last; f0 = f1 ) |
831 | { |
832 | pt = pt->next; |
833 | f1 = pt->flags & AF_FLAG_CONTROL; |
834 | |
835 | if ( !f0 && !f1 ) |
836 | break; |
837 | |
838 | if ( pt == last ) |
839 | seg->flags |= AF_EDGE_ROUND; |
840 | } |
841 | } |
842 | |
843 | return FT_Err_Ok; |
844 | } |
845 | |
846 | |
847 | static void |
848 | af_cjk_hints_link_segments( AF_GlyphHints hints, |
849 | AF_Dimension dim ) |
850 | { |
851 | AF_AxisHints axis = &hints->axis[dim]; |
852 | AF_Segment segments = axis->segments; |
853 | AF_Segment segment_limit = FT_OFFSET( segments, axis->num_segments ); |
854 | AF_Direction major_dir = axis->major_dir; |
855 | AF_Segment seg1, seg2; |
856 | FT_Pos len_threshold; |
857 | FT_Pos dist_threshold; |
858 | |
859 | |
860 | len_threshold = AF_LATIN_CONSTANT( hints->metrics, 8 ); |
861 | |
862 | dist_threshold = ( dim == AF_DIMENSION_HORZ ) ? hints->x_scale |
863 | : hints->y_scale; |
864 | dist_threshold = FT_DivFix( 64 * 3, dist_threshold ); |
865 | |
866 | /* now compare each segment to the others */ |
867 | for ( seg1 = segments; seg1 < segment_limit; seg1++ ) |
868 | { |
869 | if ( seg1->dir != major_dir ) |
870 | continue; |
871 | |
872 | for ( seg2 = segments; seg2 < segment_limit; seg2++ ) |
873 | if ( seg2 != seg1 && seg1->dir + seg2->dir == 0 ) |
874 | { |
875 | FT_Pos dist = seg2->pos - seg1->pos; |
876 | |
877 | |
878 | if ( dist < 0 ) |
879 | continue; |
880 | |
881 | { |
882 | FT_Pos min = seg1->min_coord; |
883 | FT_Pos max = seg1->max_coord; |
884 | FT_Pos len; |
885 | |
886 | |
887 | if ( min < seg2->min_coord ) |
888 | min = seg2->min_coord; |
889 | |
890 | if ( max > seg2->max_coord ) |
891 | max = seg2->max_coord; |
892 | |
893 | len = max - min; |
894 | if ( len >= len_threshold ) |
895 | { |
896 | if ( dist * 8 < seg1->score * 9 && |
897 | ( dist * 8 < seg1->score * 7 || seg1->len < len ) ) |
898 | { |
899 | seg1->score = dist; |
900 | seg1->len = len; |
901 | seg1->link = seg2; |
902 | } |
903 | |
904 | if ( dist * 8 < seg2->score * 9 && |
905 | ( dist * 8 < seg2->score * 7 || seg2->len < len ) ) |
906 | { |
907 | seg2->score = dist; |
908 | seg2->len = len; |
909 | seg2->link = seg1; |
910 | } |
911 | } |
912 | } |
913 | } |
914 | } |
915 | |
916 | /* |
917 | * now compute the `serif' segments |
918 | * |
919 | * In Hanzi, some strokes are wider on one or both of the ends. |
920 | * We either identify the stems on the ends as serifs or remove |
921 | * the linkage, depending on the length of the stems. |
922 | * |
923 | */ |
924 | |
925 | { |
926 | AF_Segment link1, link2; |
927 | |
928 | |
929 | for ( seg1 = segments; seg1 < segment_limit; seg1++ ) |
930 | { |
931 | link1 = seg1->link; |
932 | if ( !link1 || link1->link != seg1 || link1->pos <= seg1->pos ) |
933 | continue; |
934 | |
935 | if ( seg1->score >= dist_threshold ) |
936 | continue; |
937 | |
938 | for ( seg2 = segments; seg2 < segment_limit; seg2++ ) |
939 | { |
940 | if ( seg2->pos > seg1->pos || seg1 == seg2 ) |
941 | continue; |
942 | |
943 | link2 = seg2->link; |
944 | if ( !link2 || link2->link != seg2 || link2->pos < link1->pos ) |
945 | continue; |
946 | |
947 | if ( seg1->pos == seg2->pos && link1->pos == link2->pos ) |
948 | continue; |
949 | |
950 | if ( seg2->score <= seg1->score || seg1->score * 4 <= seg2->score ) |
951 | continue; |
952 | |
953 | /* seg2 < seg1 < link1 < link2 */ |
954 | |
955 | if ( seg1->len >= seg2->len * 3 ) |
956 | { |
957 | AF_Segment seg; |
958 | |
959 | |
960 | for ( seg = segments; seg < segment_limit; seg++ ) |
961 | { |
962 | AF_Segment link = seg->link; |
963 | |
964 | |
965 | if ( link == seg2 ) |
966 | { |
967 | seg->link = NULL; |
968 | seg->serif = link1; |
969 | } |
970 | else if ( link == link2 ) |
971 | { |
972 | seg->link = NULL; |
973 | seg->serif = seg1; |
974 | } |
975 | } |
976 | } |
977 | else |
978 | { |
979 | seg1->link = link1->link = NULL; |
980 | |
981 | break; |
982 | } |
983 | } |
984 | } |
985 | } |
986 | |
987 | for ( seg1 = segments; seg1 < segment_limit; seg1++ ) |
988 | { |
989 | seg2 = seg1->link; |
990 | |
991 | if ( seg2 ) |
992 | { |
993 | if ( seg2->link != seg1 ) |
994 | { |
995 | seg1->link = NULL; |
996 | |
997 | if ( seg2->score < dist_threshold || seg1->score < seg2->score * 4 ) |
998 | seg1->serif = seg2->link; |
999 | } |
1000 | } |
1001 | } |
1002 | } |
1003 | |
1004 | |
1005 | static FT_Error |
1006 | af_cjk_hints_compute_edges( AF_GlyphHints hints, |
1007 | AF_Dimension dim ) |
1008 | { |
1009 | AF_AxisHints axis = &hints->axis[dim]; |
1010 | FT_Error error = FT_Err_Ok; |
1011 | FT_Memory memory = hints->memory; |
1012 | AF_CJKAxis laxis = &((AF_CJKMetrics)hints->metrics)->axis[dim]; |
1013 | |
1014 | AF_Segment segments = axis->segments; |
1015 | AF_Segment segment_limit = FT_OFFSET( segments, axis->num_segments ); |
1016 | AF_Segment seg; |
1017 | |
1018 | FT_Fixed scale; |
1019 | FT_Pos edge_distance_threshold; |
1020 | |
1021 | |
1022 | axis->num_edges = 0; |
1023 | |
1024 | scale = ( dim == AF_DIMENSION_HORZ ) ? hints->x_scale |
1025 | : hints->y_scale; |
1026 | |
1027 | /********************************************************************** |
1028 | * |
1029 | * We begin by generating a sorted table of edges for the current |
1030 | * direction. To do so, we simply scan each segment and try to find |
1031 | * an edge in our table that corresponds to its position. |
1032 | * |
1033 | * If no edge is found, we create and insert a new edge in the |
1034 | * sorted table. Otherwise, we simply add the segment to the edge's |
1035 | * list which is then processed in the second step to compute the |
1036 | * edge's properties. |
1037 | * |
1038 | * Note that the edges table is sorted along the segment/edge |
1039 | * position. |
1040 | * |
1041 | */ |
1042 | |
1043 | edge_distance_threshold = FT_MulFix( laxis->edge_distance_threshold, |
1044 | scale ); |
1045 | if ( edge_distance_threshold > 64 / 4 ) |
1046 | edge_distance_threshold = FT_DivFix( 64 / 4, scale ); |
1047 | else |
1048 | edge_distance_threshold = laxis->edge_distance_threshold; |
1049 | |
1050 | for ( seg = segments; seg < segment_limit; seg++ ) |
1051 | { |
1052 | AF_Edge found = NULL; |
1053 | FT_Pos best = 0xFFFFU; |
1054 | FT_UInt ee; |
1055 | |
1056 | |
1057 | /* look for an edge corresponding to the segment */ |
1058 | for ( ee = 0; ee < axis->num_edges; ee++ ) |
1059 | { |
1060 | AF_Edge edge = axis->edges + ee; |
1061 | FT_Pos dist; |
1062 | |
1063 | |
1064 | if ( edge->dir != seg->dir ) |
1065 | continue; |
1066 | |
1067 | dist = seg->pos - edge->fpos; |
1068 | if ( dist < 0 ) |
1069 | dist = -dist; |
1070 | |
1071 | if ( dist < edge_distance_threshold && dist < best ) |
1072 | { |
1073 | AF_Segment link = seg->link; |
1074 | |
1075 | |
1076 | /* check whether all linked segments of the candidate edge */ |
1077 | /* can make a single edge. */ |
1078 | if ( link ) |
1079 | { |
1080 | AF_Segment seg1 = edge->first; |
1081 | FT_Pos dist2 = 0; |
1082 | |
1083 | |
1084 | do |
1085 | { |
1086 | AF_Segment link1 = seg1->link; |
1087 | |
1088 | |
1089 | if ( link1 ) |
1090 | { |
1091 | dist2 = AF_SEGMENT_DIST( link, link1 ); |
1092 | if ( dist2 >= edge_distance_threshold ) |
1093 | break; |
1094 | } |
1095 | |
1096 | } while ( ( seg1 = seg1->edge_next ) != edge->first ); |
1097 | |
1098 | if ( dist2 >= edge_distance_threshold ) |
1099 | continue; |
1100 | } |
1101 | |
1102 | best = dist; |
1103 | found = edge; |
1104 | } |
1105 | } |
1106 | |
1107 | if ( !found ) |
1108 | { |
1109 | AF_Edge edge; |
1110 | |
1111 | |
1112 | /* insert a new edge in the list and */ |
1113 | /* sort according to the position */ |
1114 | error = af_axis_hints_new_edge( axis, seg->pos, |
1115 | (AF_Direction)seg->dir, 0, |
1116 | memory, &edge ); |
1117 | if ( error ) |
1118 | goto Exit; |
1119 | |
1120 | /* add the segment to the new edge's list */ |
1121 | FT_ZERO( edge ); |
1122 | |
1123 | edge->first = seg; |
1124 | edge->last = seg; |
1125 | edge->dir = seg->dir; |
1126 | edge->fpos = seg->pos; |
1127 | edge->opos = FT_MulFix( seg->pos, scale ); |
1128 | edge->pos = edge->opos; |
1129 | seg->edge_next = seg; |
1130 | } |
1131 | else |
1132 | { |
1133 | /* if an edge was found, simply add the segment to the edge's */ |
1134 | /* list */ |
1135 | seg->edge_next = found->first; |
1136 | found->last->edge_next = seg; |
1137 | found->last = seg; |
1138 | } |
1139 | } |
1140 | |
1141 | /******************************************************************* |
1142 | * |
1143 | * Good, we now compute each edge's properties according to the |
1144 | * segments found on its position. Basically, these are |
1145 | * |
1146 | * - the edge's main direction |
1147 | * - stem edge, serif edge or both (which defaults to stem then) |
1148 | * - rounded edge, straight or both (which defaults to straight) |
1149 | * - link for edge |
1150 | * |
1151 | */ |
1152 | |
1153 | /* first of all, set the `edge' field in each segment -- this is */ |
1154 | /* required in order to compute edge links */ |
1155 | |
1156 | /* |
1157 | * Note that removing this loop and setting the `edge' field of each |
1158 | * segment directly in the code above slows down execution speed for |
1159 | * some reasons on platforms like the Sun. |
1160 | */ |
1161 | { |
1162 | AF_Edge edges = axis->edges; |
1163 | AF_Edge edge_limit = FT_OFFSET( edges, axis->num_edges ); |
1164 | AF_Edge edge; |
1165 | |
1166 | |
1167 | for ( edge = edges; edge < edge_limit; edge++ ) |
1168 | { |
1169 | seg = edge->first; |
1170 | if ( seg ) |
1171 | do |
1172 | { |
1173 | seg->edge = edge; |
1174 | seg = seg->edge_next; |
1175 | |
1176 | } while ( seg != edge->first ); |
1177 | } |
1178 | |
1179 | /* now compute each edge properties */ |
1180 | for ( edge = edges; edge < edge_limit; edge++ ) |
1181 | { |
1182 | FT_Int is_round = 0; /* does it contain round segments? */ |
1183 | FT_Int is_straight = 0; /* does it contain straight segments? */ |
1184 | |
1185 | |
1186 | seg = edge->first; |
1187 | if ( !seg ) |
1188 | goto Skip_Loop; |
1189 | |
1190 | do |
1191 | { |
1192 | FT_Bool is_serif; |
1193 | |
1194 | |
1195 | /* check for roundness of segment */ |
1196 | if ( seg->flags & AF_EDGE_ROUND ) |
1197 | is_round++; |
1198 | else |
1199 | is_straight++; |
1200 | |
1201 | /* check for links -- if seg->serif is set, then seg->link must */ |
1202 | /* be ignored */ |
1203 | is_serif = FT_BOOL( seg->serif && seg->serif->edge != edge ); |
1204 | |
1205 | if ( seg->link || is_serif ) |
1206 | { |
1207 | AF_Edge edge2; |
1208 | AF_Segment seg2; |
1209 | |
1210 | |
1211 | edge2 = edge->link; |
1212 | seg2 = seg->link; |
1213 | |
1214 | if ( is_serif ) |
1215 | { |
1216 | seg2 = seg->serif; |
1217 | edge2 = edge->serif; |
1218 | } |
1219 | |
1220 | if ( edge2 ) |
1221 | { |
1222 | FT_Pos edge_delta; |
1223 | FT_Pos seg_delta; |
1224 | |
1225 | |
1226 | edge_delta = edge->fpos - edge2->fpos; |
1227 | if ( edge_delta < 0 ) |
1228 | edge_delta = -edge_delta; |
1229 | |
1230 | seg_delta = AF_SEGMENT_DIST( seg, seg2 ); |
1231 | |
1232 | if ( seg_delta < edge_delta ) |
1233 | edge2 = seg2->edge; |
1234 | } |
1235 | else |
1236 | edge2 = seg2->edge; |
1237 | |
1238 | if ( is_serif ) |
1239 | { |
1240 | edge->serif = edge2; |
1241 | edge2->flags |= AF_EDGE_SERIF; |
1242 | } |
1243 | else |
1244 | edge->link = edge2; |
1245 | } |
1246 | |
1247 | seg = seg->edge_next; |
1248 | |
1249 | } while ( seg != edge->first ); |
1250 | |
1251 | Skip_Loop: |
1252 | /* set the round/straight flags */ |
1253 | edge->flags = AF_EDGE_NORMAL; |
1254 | |
1255 | if ( is_round > 0 && is_round >= is_straight ) |
1256 | edge->flags |= AF_EDGE_ROUND; |
1257 | |
1258 | /* get rid of serifs if link is set */ |
1259 | /* XXX: This gets rid of many unpleasant artefacts! */ |
1260 | /* Example: the `c' in cour.pfa at size 13 */ |
1261 | |
1262 | if ( edge->serif && edge->link ) |
1263 | edge->serif = NULL; |
1264 | } |
1265 | } |
1266 | |
1267 | Exit: |
1268 | return error; |
1269 | } |
1270 | |
1271 | |
1272 | /* Detect segments and edges for given dimension. */ |
1273 | |
1274 | static FT_Error |
1275 | af_cjk_hints_detect_features( AF_GlyphHints hints, |
1276 | AF_Dimension dim ) |
1277 | { |
1278 | FT_Error error; |
1279 | |
1280 | |
1281 | error = af_cjk_hints_compute_segments( hints, dim ); |
1282 | if ( !error ) |
1283 | { |
1284 | af_cjk_hints_link_segments( hints, dim ); |
1285 | |
1286 | error = af_cjk_hints_compute_edges( hints, dim ); |
1287 | } |
1288 | return error; |
1289 | } |
1290 | |
1291 | |
1292 | /* Compute all edges which lie within blue zones. */ |
1293 | |
1294 | static void |
1295 | af_cjk_hints_compute_blue_edges( AF_GlyphHints hints, |
1296 | AF_CJKMetrics metrics, |
1297 | AF_Dimension dim ) |
1298 | { |
1299 | AF_AxisHints axis = &hints->axis[dim]; |
1300 | AF_Edge edge = axis->edges; |
1301 | AF_Edge edge_limit = FT_OFFSET( edge, axis->num_edges ); |
1302 | AF_CJKAxis cjk = &metrics->axis[dim]; |
1303 | FT_Fixed scale = cjk->scale; |
1304 | FT_Pos best_dist0; /* initial threshold */ |
1305 | |
1306 | |
1307 | /* compute the initial threshold as a fraction of the EM size */ |
1308 | best_dist0 = FT_MulFix( metrics->units_per_em / 40, scale ); |
1309 | |
1310 | if ( best_dist0 > 64 / 2 ) /* maximum 1/2 pixel */ |
1311 | best_dist0 = 64 / 2; |
1312 | |
1313 | /* compute which blue zones are active, i.e. have their scaled */ |
1314 | /* size < 3/4 pixels */ |
1315 | |
1316 | /* If the distant between an edge and a blue zone is shorter than */ |
1317 | /* best_dist0, set the blue zone for the edge. Then search for */ |
1318 | /* the blue zone with the smallest best_dist to the edge. */ |
1319 | |
1320 | for ( ; edge < edge_limit; edge++ ) |
1321 | { |
1322 | FT_UInt bb; |
1323 | AF_Width best_blue = NULL; |
1324 | FT_Pos best_dist = best_dist0; |
1325 | |
1326 | |
1327 | for ( bb = 0; bb < cjk->blue_count; bb++ ) |
1328 | { |
1329 | AF_CJKBlue blue = cjk->blues + bb; |
1330 | FT_Bool is_top_right_blue, is_major_dir; |
1331 | |
1332 | |
1333 | /* skip inactive blue zones (i.e., those that are too small) */ |
1334 | if ( !( blue->flags & AF_CJK_BLUE_ACTIVE ) ) |
1335 | continue; |
1336 | |
1337 | /* if it is a top zone, check for right edges -- if it is a bottom */ |
1338 | /* zone, check for left edges */ |
1339 | /* */ |
1340 | /* of course, that's for TrueType */ |
1341 | is_top_right_blue = |
1342 | (FT_Byte)( ( blue->flags & AF_CJK_BLUE_TOP ) != 0 ); |
1343 | is_major_dir = |
1344 | FT_BOOL( edge->dir == axis->major_dir ); |
1345 | |
1346 | /* if it is a top zone, the edge must be against the major */ |
1347 | /* direction; if it is a bottom zone, it must be in the major */ |
1348 | /* direction */ |
1349 | if ( is_top_right_blue ^ is_major_dir ) |
1350 | { |
1351 | FT_Pos dist; |
1352 | AF_Width compare; |
1353 | |
1354 | |
1355 | /* Compare the edge to the closest blue zone type */ |
1356 | if ( FT_ABS( edge->fpos - blue->ref.org ) > |
1357 | FT_ABS( edge->fpos - blue->shoot.org ) ) |
1358 | compare = &blue->shoot; |
1359 | else |
1360 | compare = &blue->ref; |
1361 | |
1362 | dist = edge->fpos - compare->org; |
1363 | if ( dist < 0 ) |
1364 | dist = -dist; |
1365 | |
1366 | dist = FT_MulFix( dist, scale ); |
1367 | if ( dist < best_dist ) |
1368 | { |
1369 | best_dist = dist; |
1370 | best_blue = compare; |
1371 | } |
1372 | } |
1373 | } |
1374 | |
1375 | if ( best_blue ) |
1376 | edge->blue_edge = best_blue; |
1377 | } |
1378 | } |
1379 | |
1380 | |
1381 | /* Initalize hinting engine. */ |
1382 | |
1383 | FT_LOCAL_DEF( FT_Error ) |
1384 | af_cjk_hints_init( AF_GlyphHints hints, |
1385 | AF_StyleMetrics metrics_ ) /* AF_CJKMetrics */ |
1386 | { |
1387 | AF_CJKMetrics metrics = (AF_CJKMetrics)metrics_; |
1388 | FT_Render_Mode mode; |
1389 | FT_UInt32 scaler_flags, other_flags; |
1390 | |
1391 | |
1392 | af_glyph_hints_rescale( hints, (AF_StyleMetrics)metrics ); |
1393 | |
1394 | /* |
1395 | * correct x_scale and y_scale when needed, since they may have |
1396 | * been modified af_cjk_scale_dim above |
1397 | */ |
1398 | hints->x_scale = metrics->axis[AF_DIMENSION_HORZ].scale; |
1399 | hints->x_delta = metrics->axis[AF_DIMENSION_HORZ].delta; |
1400 | hints->y_scale = metrics->axis[AF_DIMENSION_VERT].scale; |
1401 | hints->y_delta = metrics->axis[AF_DIMENSION_VERT].delta; |
1402 | |
1403 | /* compute flags depending on render mode, etc. */ |
1404 | mode = metrics->root.scaler.render_mode; |
1405 | |
1406 | scaler_flags = hints->scaler_flags; |
1407 | other_flags = 0; |
1408 | |
1409 | /* |
1410 | * We snap the width of vertical stems for the monochrome and |
1411 | * horizontal LCD rendering targets only. |
1412 | */ |
1413 | if ( mode == FT_RENDER_MODE_MONO || mode == FT_RENDER_MODE_LCD ) |
1414 | other_flags |= AF_LATIN_HINTS_HORZ_SNAP; |
1415 | |
1416 | /* |
1417 | * We snap the width of horizontal stems for the monochrome and |
1418 | * vertical LCD rendering targets only. |
1419 | */ |
1420 | if ( mode == FT_RENDER_MODE_MONO || mode == FT_RENDER_MODE_LCD_V ) |
1421 | other_flags |= AF_LATIN_HINTS_VERT_SNAP; |
1422 | |
1423 | /* |
1424 | * We adjust stems to full pixels unless in `light' or `lcd' mode. |
1425 | */ |
1426 | if ( mode != FT_RENDER_MODE_LIGHT && mode != FT_RENDER_MODE_LCD ) |
1427 | other_flags |= AF_LATIN_HINTS_STEM_ADJUST; |
1428 | |
1429 | if ( mode == FT_RENDER_MODE_MONO ) |
1430 | other_flags |= AF_LATIN_HINTS_MONO; |
1431 | |
1432 | scaler_flags |= AF_SCALER_FLAG_NO_ADVANCE; |
1433 | |
1434 | hints->scaler_flags = scaler_flags; |
1435 | hints->other_flags = other_flags; |
1436 | |
1437 | return FT_Err_Ok; |
1438 | } |
1439 | |
1440 | |
1441 | /*************************************************************************/ |
1442 | /*************************************************************************/ |
1443 | /***** *****/ |
1444 | /***** C J K G L Y P H G R I D - F I T T I N G *****/ |
1445 | /***** *****/ |
1446 | /*************************************************************************/ |
1447 | /*************************************************************************/ |
1448 | |
1449 | /* Snap a given width in scaled coordinates to one of the */ |
1450 | /* current standard widths. */ |
1451 | |
1452 | static FT_Pos |
1453 | af_cjk_snap_width( AF_Width widths, |
1454 | FT_UInt count, |
1455 | FT_Pos width ) |
1456 | { |
1457 | FT_UInt n; |
1458 | FT_Pos best = 64 + 32 + 2; |
1459 | FT_Pos reference = width; |
1460 | FT_Pos scaled; |
1461 | |
1462 | |
1463 | for ( n = 0; n < count; n++ ) |
1464 | { |
1465 | FT_Pos w; |
1466 | FT_Pos dist; |
1467 | |
1468 | |
1469 | w = widths[n].cur; |
1470 | dist = width - w; |
1471 | if ( dist < 0 ) |
1472 | dist = -dist; |
1473 | if ( dist < best ) |
1474 | { |
1475 | best = dist; |
1476 | reference = w; |
1477 | } |
1478 | } |
1479 | |
1480 | scaled = FT_PIX_ROUND( reference ); |
1481 | |
1482 | if ( width >= reference ) |
1483 | { |
1484 | if ( width < scaled + 48 ) |
1485 | width = reference; |
1486 | } |
1487 | else |
1488 | { |
1489 | if ( width > scaled - 48 ) |
1490 | width = reference; |
1491 | } |
1492 | |
1493 | return width; |
1494 | } |
1495 | |
1496 | |
1497 | /* Compute the snapped width of a given stem. */ |
1498 | /* There is a lot of voodoo in this function; changing the hard-coded */ |
1499 | /* parameters influence the whole hinting process. */ |
1500 | |
1501 | static FT_Pos |
1502 | af_cjk_compute_stem_width( AF_GlyphHints hints, |
1503 | AF_Dimension dim, |
1504 | FT_Pos width, |
1505 | FT_UInt base_flags, |
1506 | FT_UInt stem_flags ) |
1507 | { |
1508 | AF_CJKMetrics metrics = (AF_CJKMetrics)hints->metrics; |
1509 | AF_CJKAxis axis = &metrics->axis[dim]; |
1510 | FT_Pos dist = width; |
1511 | FT_Int sign = 0; |
1512 | FT_Bool vertical = FT_BOOL( dim == AF_DIMENSION_VERT ); |
1513 | |
1514 | FT_UNUSED( base_flags ); |
1515 | FT_UNUSED( stem_flags ); |
1516 | |
1517 | |
1518 | if ( !AF_LATIN_HINTS_DO_STEM_ADJUST( hints ) ) |
1519 | return width; |
1520 | |
1521 | if ( dist < 0 ) |
1522 | { |
1523 | dist = -width; |
1524 | sign = 1; |
1525 | } |
1526 | |
1527 | if ( ( vertical && !AF_LATIN_HINTS_DO_VERT_SNAP( hints ) ) || |
1528 | ( !vertical && !AF_LATIN_HINTS_DO_HORZ_SNAP( hints ) ) ) |
1529 | { |
1530 | /* smooth hinting process: very lightly quantize the stem width */ |
1531 | |
1532 | if ( axis->width_count > 0 ) |
1533 | { |
1534 | if ( FT_ABS( dist - axis->widths[0].cur ) < 40 ) |
1535 | { |
1536 | dist = axis->widths[0].cur; |
1537 | if ( dist < 48 ) |
1538 | dist = 48; |
1539 | |
1540 | goto Done_Width; |
1541 | } |
1542 | } |
1543 | |
1544 | if ( dist < 54 ) |
1545 | dist += ( 54 - dist ) / 2; |
1546 | else if ( dist < 3 * 64 ) |
1547 | { |
1548 | FT_Pos delta; |
1549 | |
1550 | |
1551 | delta = dist & 63; |
1552 | dist &= -64; |
1553 | |
1554 | if ( delta < 10 ) |
1555 | dist += delta; |
1556 | else if ( delta < 22 ) |
1557 | dist += 10; |
1558 | else if ( delta < 42 ) |
1559 | dist += delta; |
1560 | else if ( delta < 54 ) |
1561 | dist += 54; |
1562 | else |
1563 | dist += delta; |
1564 | } |
1565 | } |
1566 | else |
1567 | { |
1568 | /* strong hinting process: snap the stem width to integer pixels */ |
1569 | |
1570 | dist = af_cjk_snap_width( axis->widths, axis->width_count, dist ); |
1571 | |
1572 | if ( vertical ) |
1573 | { |
1574 | /* in the case of vertical hinting, always round */ |
1575 | /* the stem heights to integer pixels */ |
1576 | |
1577 | if ( dist >= 64 ) |
1578 | dist = ( dist + 16 ) & ~63; |
1579 | else |
1580 | dist = 64; |
1581 | } |
1582 | else |
1583 | { |
1584 | if ( AF_LATIN_HINTS_DO_MONO( hints ) ) |
1585 | { |
1586 | /* monochrome horizontal hinting: snap widths to integer pixels */ |
1587 | /* with a different threshold */ |
1588 | |
1589 | if ( dist < 64 ) |
1590 | dist = 64; |
1591 | else |
1592 | dist = ( dist + 32 ) & ~63; |
1593 | } |
1594 | else |
1595 | { |
1596 | /* for horizontal anti-aliased hinting, we adopt a more subtle */ |
1597 | /* approach: we strengthen small stems, round stems whose size */ |
1598 | /* is between 1 and 2 pixels to an integer, otherwise nothing */ |
1599 | |
1600 | if ( dist < 48 ) |
1601 | dist = ( dist + 64 ) >> 1; |
1602 | |
1603 | else if ( dist < 128 ) |
1604 | dist = ( dist + 22 ) & ~63; |
1605 | else |
1606 | /* round otherwise to prevent color fringes in LCD mode */ |
1607 | dist = ( dist + 32 ) & ~63; |
1608 | } |
1609 | } |
1610 | } |
1611 | |
1612 | Done_Width: |
1613 | if ( sign ) |
1614 | dist = -dist; |
1615 | |
1616 | return dist; |
1617 | } |
1618 | |
1619 | |
1620 | /* Align one stem edge relative to the previous stem edge. */ |
1621 | |
1622 | static void |
1623 | af_cjk_align_linked_edge( AF_GlyphHints hints, |
1624 | AF_Dimension dim, |
1625 | AF_Edge base_edge, |
1626 | AF_Edge stem_edge ) |
1627 | { |
1628 | FT_Pos dist = stem_edge->opos - base_edge->opos; |
1629 | |
1630 | FT_Pos fitted_width = af_cjk_compute_stem_width( hints, dim, dist, |
1631 | base_edge->flags, |
1632 | stem_edge->flags ); |
1633 | |
1634 | |
1635 | stem_edge->pos = base_edge->pos + fitted_width; |
1636 | |
1637 | FT_TRACE5(( " CJKLINK: edge %td @%d (opos=%.2f) linked to %.2f," |
1638 | " dist was %.2f, now %.2f\n" , |
1639 | stem_edge - hints->axis[dim].edges, stem_edge->fpos, |
1640 | (double)stem_edge->opos / 64, |
1641 | (double)stem_edge->pos / 64, |
1642 | (double)dist / 64, |
1643 | (double)fitted_width / 64 )); |
1644 | } |
1645 | |
1646 | |
1647 | /* Shift the coordinates of the `serif' edge by the same amount */ |
1648 | /* as the corresponding `base' edge has been moved already. */ |
1649 | |
1650 | static void |
1651 | af_cjk_align_serif_edge( AF_GlyphHints hints, |
1652 | AF_Edge base, |
1653 | AF_Edge serif ) |
1654 | { |
1655 | FT_UNUSED( hints ); |
1656 | |
1657 | serif->pos = base->pos + ( serif->opos - base->opos ); |
1658 | } |
1659 | |
1660 | |
1661 | /*************************************************************************/ |
1662 | /*************************************************************************/ |
1663 | /*************************************************************************/ |
1664 | /**** ****/ |
1665 | /**** E D G E H I N T I N G ****/ |
1666 | /**** ****/ |
1667 | /*************************************************************************/ |
1668 | /*************************************************************************/ |
1669 | /*************************************************************************/ |
1670 | |
1671 | |
1672 | #define AF_LIGHT_MODE_MAX_HORZ_GAP 9 |
1673 | #define AF_LIGHT_MODE_MAX_VERT_GAP 15 |
1674 | #define AF_LIGHT_MODE_MAX_DELTA_ABS 14 |
1675 | |
1676 | |
1677 | static FT_Pos |
1678 | af_hint_normal_stem( AF_GlyphHints hints, |
1679 | AF_Edge edge, |
1680 | AF_Edge edge2, |
1681 | FT_Pos anchor, |
1682 | AF_Dimension dim ) |
1683 | { |
1684 | FT_Pos org_len, cur_len, org_center; |
1685 | FT_Pos cur_pos1, cur_pos2; |
1686 | FT_Pos d_off1, u_off1, d_off2, u_off2, delta; |
1687 | FT_Pos offset; |
1688 | FT_Pos threshold = 64; |
1689 | |
1690 | |
1691 | if ( !AF_LATIN_HINTS_DO_STEM_ADJUST( hints ) ) |
1692 | { |
1693 | if ( ( edge->flags & AF_EDGE_ROUND ) && |
1694 | ( edge2->flags & AF_EDGE_ROUND ) ) |
1695 | { |
1696 | if ( dim == AF_DIMENSION_VERT ) |
1697 | threshold = 64 - AF_LIGHT_MODE_MAX_HORZ_GAP; |
1698 | else |
1699 | threshold = 64 - AF_LIGHT_MODE_MAX_VERT_GAP; |
1700 | } |
1701 | else |
1702 | { |
1703 | if ( dim == AF_DIMENSION_VERT ) |
1704 | threshold = 64 - AF_LIGHT_MODE_MAX_HORZ_GAP / 3; |
1705 | else |
1706 | threshold = 64 - AF_LIGHT_MODE_MAX_VERT_GAP / 3; |
1707 | } |
1708 | } |
1709 | |
1710 | org_len = edge2->opos - edge->opos; |
1711 | cur_len = af_cjk_compute_stem_width( hints, dim, org_len, |
1712 | edge->flags, |
1713 | edge2->flags ); |
1714 | |
1715 | org_center = ( edge->opos + edge2->opos ) / 2 + anchor; |
1716 | cur_pos1 = org_center - cur_len / 2; |
1717 | cur_pos2 = cur_pos1 + cur_len; |
1718 | d_off1 = cur_pos1 - FT_PIX_FLOOR( cur_pos1 ); |
1719 | d_off2 = cur_pos2 - FT_PIX_FLOOR( cur_pos2 ); |
1720 | u_off1 = 64 - d_off1; |
1721 | u_off2 = 64 - d_off2; |
1722 | delta = 0; |
1723 | |
1724 | |
1725 | if ( d_off1 == 0 || d_off2 == 0 ) |
1726 | goto Exit; |
1727 | |
1728 | if ( cur_len <= threshold ) |
1729 | { |
1730 | if ( d_off2 < cur_len ) |
1731 | { |
1732 | if ( u_off1 <= d_off2 ) |
1733 | delta = u_off1; |
1734 | else |
1735 | delta = -d_off2; |
1736 | } |
1737 | |
1738 | goto Exit; |
1739 | } |
1740 | |
1741 | if ( threshold < 64 ) |
1742 | { |
1743 | if ( d_off1 >= threshold || u_off1 >= threshold || |
1744 | d_off2 >= threshold || u_off2 >= threshold ) |
1745 | goto Exit; |
1746 | } |
1747 | |
1748 | offset = cur_len & 63; |
1749 | |
1750 | if ( offset < 32 ) |
1751 | { |
1752 | if ( u_off1 <= offset || d_off2 <= offset ) |
1753 | goto Exit; |
1754 | } |
1755 | else |
1756 | offset = 64 - threshold; |
1757 | |
1758 | d_off1 = threshold - u_off1; |
1759 | u_off1 = u_off1 - offset; |
1760 | u_off2 = threshold - d_off2; |
1761 | d_off2 = d_off2 - offset; |
1762 | |
1763 | if ( d_off1 <= u_off1 ) |
1764 | u_off1 = -d_off1; |
1765 | |
1766 | if ( d_off2 <= u_off2 ) |
1767 | u_off2 = -d_off2; |
1768 | |
1769 | if ( FT_ABS( u_off1 ) <= FT_ABS( u_off2 ) ) |
1770 | delta = u_off1; |
1771 | else |
1772 | delta = u_off2; |
1773 | |
1774 | Exit: |
1775 | |
1776 | #if 1 |
1777 | if ( !AF_LATIN_HINTS_DO_STEM_ADJUST( hints ) ) |
1778 | { |
1779 | if ( delta > AF_LIGHT_MODE_MAX_DELTA_ABS ) |
1780 | delta = AF_LIGHT_MODE_MAX_DELTA_ABS; |
1781 | else if ( delta < -AF_LIGHT_MODE_MAX_DELTA_ABS ) |
1782 | delta = -AF_LIGHT_MODE_MAX_DELTA_ABS; |
1783 | } |
1784 | #endif |
1785 | |
1786 | cur_pos1 += delta; |
1787 | |
1788 | if ( edge->opos < edge2->opos ) |
1789 | { |
1790 | edge->pos = cur_pos1; |
1791 | edge2->pos = cur_pos1 + cur_len; |
1792 | } |
1793 | else |
1794 | { |
1795 | edge->pos = cur_pos1 + cur_len; |
1796 | edge2->pos = cur_pos1; |
1797 | } |
1798 | |
1799 | return delta; |
1800 | } |
1801 | |
1802 | |
1803 | /* The main grid-fitting routine. */ |
1804 | |
1805 | static void |
1806 | af_cjk_hint_edges( AF_GlyphHints hints, |
1807 | AF_Dimension dim ) |
1808 | { |
1809 | AF_AxisHints axis = &hints->axis[dim]; |
1810 | AF_Edge edges = axis->edges; |
1811 | AF_Edge edge_limit = FT_OFFSET( edges, axis->num_edges ); |
1812 | FT_PtrDist n_edges; |
1813 | AF_Edge edge; |
1814 | AF_Edge anchor = NULL; |
1815 | FT_Pos delta = 0; |
1816 | FT_Int skipped = 0; |
1817 | FT_Bool has_last_stem = FALSE; |
1818 | FT_Pos last_stem_pos = 0; |
1819 | |
1820 | #ifdef FT_DEBUG_LEVEL_TRACE |
1821 | FT_UInt num_actions = 0; |
1822 | #endif |
1823 | |
1824 | |
1825 | FT_TRACE5(( "cjk %s edge hinting (style `%s')\n" , |
1826 | dim == AF_DIMENSION_VERT ? "horizontal" : "vertical" , |
1827 | af_style_names[hints->metrics->style_class->style] )); |
1828 | |
1829 | /* we begin by aligning all stems relative to the blue zone */ |
1830 | |
1831 | if ( AF_HINTS_DO_BLUES( hints ) ) |
1832 | { |
1833 | for ( edge = edges; edge < edge_limit; edge++ ) |
1834 | { |
1835 | AF_Width blue; |
1836 | AF_Edge edge1, edge2; |
1837 | |
1838 | |
1839 | if ( edge->flags & AF_EDGE_DONE ) |
1840 | continue; |
1841 | |
1842 | blue = edge->blue_edge; |
1843 | edge1 = NULL; |
1844 | edge2 = edge->link; |
1845 | |
1846 | if ( blue ) |
1847 | { |
1848 | edge1 = edge; |
1849 | } |
1850 | else if ( edge2 && edge2->blue_edge ) |
1851 | { |
1852 | blue = edge2->blue_edge; |
1853 | edge1 = edge2; |
1854 | edge2 = edge; |
1855 | } |
1856 | |
1857 | if ( !edge1 ) |
1858 | continue; |
1859 | |
1860 | #ifdef FT_DEBUG_LEVEL_TRACE |
1861 | FT_TRACE5(( " CJKBLUE: edge %td @%d (opos=%.2f) snapped to %.2f," |
1862 | " was %.2f\n" , |
1863 | edge1 - edges, edge1->fpos, (double)edge1->opos / 64, |
1864 | (double)blue->fit / 64, (double)edge1->pos / 64 )); |
1865 | |
1866 | num_actions++; |
1867 | #endif |
1868 | |
1869 | edge1->pos = blue->fit; |
1870 | edge1->flags |= AF_EDGE_DONE; |
1871 | |
1872 | if ( edge2 && !edge2->blue_edge ) |
1873 | { |
1874 | af_cjk_align_linked_edge( hints, dim, edge1, edge2 ); |
1875 | edge2->flags |= AF_EDGE_DONE; |
1876 | |
1877 | #ifdef FT_DEBUG_LEVEL_TRACE |
1878 | num_actions++; |
1879 | #endif |
1880 | } |
1881 | |
1882 | if ( !anchor ) |
1883 | anchor = edge; |
1884 | } |
1885 | } |
1886 | |
1887 | /* now we align all stem edges. */ |
1888 | for ( edge = edges; edge < edge_limit; edge++ ) |
1889 | { |
1890 | AF_Edge edge2; |
1891 | |
1892 | |
1893 | if ( edge->flags & AF_EDGE_DONE ) |
1894 | continue; |
1895 | |
1896 | /* skip all non-stem edges */ |
1897 | edge2 = edge->link; |
1898 | if ( !edge2 ) |
1899 | { |
1900 | skipped++; |
1901 | continue; |
1902 | } |
1903 | |
1904 | /* Some CJK characters have so many stems that |
1905 | * the hinter is likely to merge two adjacent ones. |
1906 | * To solve this problem, if either edge of a stem |
1907 | * is too close to the previous one, we avoid |
1908 | * aligning the two edges, but rather interpolate |
1909 | * their locations at the end of this function in |
1910 | * order to preserve the space between the stems. |
1911 | */ |
1912 | if ( has_last_stem && |
1913 | ( edge->pos < last_stem_pos + 64 || |
1914 | edge2->pos < last_stem_pos + 64 ) ) |
1915 | { |
1916 | skipped++; |
1917 | continue; |
1918 | } |
1919 | |
1920 | /* now align the stem */ |
1921 | |
1922 | /* this should not happen, but it's better to be safe */ |
1923 | if ( edge2->blue_edge ) |
1924 | { |
1925 | FT_TRACE5(( "ASSERTION FAILED for edge %td\n" , edge2 - edges )); |
1926 | |
1927 | af_cjk_align_linked_edge( hints, dim, edge2, edge ); |
1928 | edge->flags |= AF_EDGE_DONE; |
1929 | |
1930 | #ifdef FT_DEBUG_LEVEL_TRACE |
1931 | num_actions++; |
1932 | #endif |
1933 | |
1934 | continue; |
1935 | } |
1936 | |
1937 | if ( edge2 < edge ) |
1938 | { |
1939 | af_cjk_align_linked_edge( hints, dim, edge2, edge ); |
1940 | edge->flags |= AF_EDGE_DONE; |
1941 | |
1942 | #ifdef FT_DEBUG_LEVEL_TRACE |
1943 | num_actions++; |
1944 | #endif |
1945 | |
1946 | /* We rarely reaches here it seems; |
1947 | * usually the two edges belonging |
1948 | * to one stem are marked as DONE together |
1949 | */ |
1950 | has_last_stem = TRUE; |
1951 | last_stem_pos = edge->pos; |
1952 | continue; |
1953 | } |
1954 | |
1955 | if ( dim != AF_DIMENSION_VERT && !anchor ) |
1956 | { |
1957 | |
1958 | #if 0 |
1959 | if ( fixedpitch ) |
1960 | { |
1961 | AF_Edge left = edge; |
1962 | AF_Edge right = edge_limit - 1; |
1963 | AF_EdgeRec left1, left2, right1, right2; |
1964 | FT_Pos target, center1, center2; |
1965 | FT_Pos delta1, delta2, d1, d2; |
1966 | |
1967 | |
1968 | while ( right > left && !right->link ) |
1969 | right--; |
1970 | |
1971 | left1 = *left; |
1972 | left2 = *left->link; |
1973 | right1 = *right->link; |
1974 | right2 = *right; |
1975 | |
1976 | delta = ( ( ( hinter->pp2.x + 32 ) & -64 ) - hinter->pp2.x ) / 2; |
1977 | target = left->opos + ( right->opos - left->opos ) / 2 + delta - 16; |
1978 | |
1979 | delta1 = delta; |
1980 | delta1 += af_hint_normal_stem( hints, left, left->link, |
1981 | delta1, 0 ); |
1982 | |
1983 | if ( left->link != right ) |
1984 | af_hint_normal_stem( hints, right->link, right, delta1, 0 ); |
1985 | |
1986 | center1 = left->pos + ( right->pos - left->pos ) / 2; |
1987 | |
1988 | if ( center1 >= target ) |
1989 | delta2 = delta - 32; |
1990 | else |
1991 | delta2 = delta + 32; |
1992 | |
1993 | delta2 += af_hint_normal_stem( hints, &left1, &left2, delta2, 0 ); |
1994 | |
1995 | if ( delta1 != delta2 ) |
1996 | { |
1997 | if ( left->link != right ) |
1998 | af_hint_normal_stem( hints, &right1, &right2, delta2, 0 ); |
1999 | |
2000 | center2 = left1.pos + ( right2.pos - left1.pos ) / 2; |
2001 | |
2002 | d1 = center1 - target; |
2003 | d2 = center2 - target; |
2004 | |
2005 | if ( FT_ABS( d2 ) < FT_ABS( d1 ) ) |
2006 | { |
2007 | left->pos = left1.pos; |
2008 | left->link->pos = left2.pos; |
2009 | |
2010 | if ( left->link != right ) |
2011 | { |
2012 | right->link->pos = right1.pos; |
2013 | right->pos = right2.pos; |
2014 | } |
2015 | |
2016 | delta1 = delta2; |
2017 | } |
2018 | } |
2019 | |
2020 | delta = delta1; |
2021 | right->link->flags |= AF_EDGE_DONE; |
2022 | right->flags |= AF_EDGE_DONE; |
2023 | } |
2024 | else |
2025 | |
2026 | #endif /* 0 */ |
2027 | |
2028 | delta = af_hint_normal_stem( hints, edge, edge2, 0, |
2029 | AF_DIMENSION_HORZ ); |
2030 | } |
2031 | else |
2032 | af_hint_normal_stem( hints, edge, edge2, delta, dim ); |
2033 | |
2034 | #if 0 |
2035 | printf( "stem (%d,%d) adjusted (%.1f,%.1f)\n" , |
2036 | edge - edges, edge2 - edges, |
2037 | (double)( edge->pos - edge->opos ) / 64, |
2038 | (double)( edge2->pos - edge2->opos ) / 64 ); |
2039 | #endif |
2040 | |
2041 | anchor = edge; |
2042 | edge->flags |= AF_EDGE_DONE; |
2043 | edge2->flags |= AF_EDGE_DONE; |
2044 | has_last_stem = TRUE; |
2045 | last_stem_pos = edge2->pos; |
2046 | } |
2047 | |
2048 | /* make sure that lowercase m's maintain their symmetry */ |
2049 | |
2050 | /* In general, lowercase m's have six vertical edges if they are sans */ |
2051 | /* serif, or twelve if they are with serifs. This implementation is */ |
2052 | /* based on that assumption, and seems to work very well with most */ |
2053 | /* faces. However, if for a certain face this assumption is not */ |
2054 | /* true, the m is just rendered like before. In addition, any stem */ |
2055 | /* correction will only be applied to symmetrical glyphs (even if the */ |
2056 | /* glyph is not an m), so the potential for unwanted distortion is */ |
2057 | /* relatively low. */ |
2058 | |
2059 | /* We don't handle horizontal edges since we can't easily assure that */ |
2060 | /* the third (lowest) stem aligns with the base line; it might end up */ |
2061 | /* one pixel higher or lower. */ |
2062 | |
2063 | n_edges = edge_limit - edges; |
2064 | if ( dim == AF_DIMENSION_HORZ && ( n_edges == 6 || n_edges == 12 ) ) |
2065 | { |
2066 | AF_Edge edge1, edge2, edge3; |
2067 | FT_Pos dist1, dist2, span; |
2068 | |
2069 | |
2070 | if ( n_edges == 6 ) |
2071 | { |
2072 | edge1 = edges; |
2073 | edge2 = edges + 2; |
2074 | edge3 = edges + 4; |
2075 | } |
2076 | else |
2077 | { |
2078 | edge1 = edges + 1; |
2079 | edge2 = edges + 5; |
2080 | edge3 = edges + 9; |
2081 | } |
2082 | |
2083 | dist1 = edge2->opos - edge1->opos; |
2084 | dist2 = edge3->opos - edge2->opos; |
2085 | |
2086 | span = dist1 - dist2; |
2087 | if ( span < 0 ) |
2088 | span = -span; |
2089 | |
2090 | if ( edge1->link == edge1 + 1 && |
2091 | edge2->link == edge2 + 1 && |
2092 | edge3->link == edge3 + 1 && span < 8 ) |
2093 | { |
2094 | delta = edge3->pos - ( 2 * edge2->pos - edge1->pos ); |
2095 | edge3->pos -= delta; |
2096 | if ( edge3->link ) |
2097 | edge3->link->pos -= delta; |
2098 | |
2099 | /* move the serifs along with the stem */ |
2100 | if ( n_edges == 12 ) |
2101 | { |
2102 | ( edges + 8 )->pos -= delta; |
2103 | ( edges + 11 )->pos -= delta; |
2104 | } |
2105 | |
2106 | edge3->flags |= AF_EDGE_DONE; |
2107 | if ( edge3->link ) |
2108 | edge3->link->flags |= AF_EDGE_DONE; |
2109 | } |
2110 | } |
2111 | |
2112 | if ( !skipped ) |
2113 | goto Exit; |
2114 | |
2115 | /* |
2116 | * now hint the remaining edges (serifs and single) in order |
2117 | * to complete our processing |
2118 | */ |
2119 | for ( edge = edges; edge < edge_limit; edge++ ) |
2120 | { |
2121 | if ( edge->flags & AF_EDGE_DONE ) |
2122 | continue; |
2123 | |
2124 | if ( edge->serif ) |
2125 | { |
2126 | af_cjk_align_serif_edge( hints, edge->serif, edge ); |
2127 | edge->flags |= AF_EDGE_DONE; |
2128 | skipped--; |
2129 | } |
2130 | } |
2131 | |
2132 | if ( !skipped ) |
2133 | goto Exit; |
2134 | |
2135 | for ( edge = edges; edge < edge_limit; edge++ ) |
2136 | { |
2137 | AF_Edge before, after; |
2138 | |
2139 | |
2140 | if ( edge->flags & AF_EDGE_DONE ) |
2141 | continue; |
2142 | |
2143 | before = after = edge; |
2144 | |
2145 | while ( --before >= edges ) |
2146 | if ( before->flags & AF_EDGE_DONE ) |
2147 | break; |
2148 | |
2149 | while ( ++after < edge_limit ) |
2150 | if ( after->flags & AF_EDGE_DONE ) |
2151 | break; |
2152 | |
2153 | if ( before >= edges || after < edge_limit ) |
2154 | { |
2155 | if ( before < edges ) |
2156 | af_cjk_align_serif_edge( hints, after, edge ); |
2157 | else if ( after >= edge_limit ) |
2158 | af_cjk_align_serif_edge( hints, before, edge ); |
2159 | else |
2160 | { |
2161 | if ( after->fpos == before->fpos ) |
2162 | edge->pos = before->pos; |
2163 | else |
2164 | edge->pos = before->pos + |
2165 | FT_MulDiv( edge->fpos - before->fpos, |
2166 | after->pos - before->pos, |
2167 | after->fpos - before->fpos ); |
2168 | } |
2169 | } |
2170 | } |
2171 | |
2172 | Exit: |
2173 | |
2174 | #ifdef FT_DEBUG_LEVEL_TRACE |
2175 | if ( !num_actions ) |
2176 | FT_TRACE5(( " (none)\n" )); |
2177 | FT_TRACE5(( "\n" )); |
2178 | #endif |
2179 | |
2180 | return; |
2181 | } |
2182 | |
2183 | |
2184 | static void |
2185 | af_cjk_align_edge_points( AF_GlyphHints hints, |
2186 | AF_Dimension dim ) |
2187 | { |
2188 | AF_AxisHints axis = & hints->axis[dim]; |
2189 | AF_Edge edges = axis->edges; |
2190 | AF_Edge edge_limit = FT_OFFSET( edges, axis->num_edges ); |
2191 | AF_Edge edge; |
2192 | FT_Bool snapping; |
2193 | |
2194 | |
2195 | snapping = FT_BOOL( ( dim == AF_DIMENSION_HORZ && |
2196 | AF_LATIN_HINTS_DO_HORZ_SNAP( hints ) ) || |
2197 | ( dim == AF_DIMENSION_VERT && |
2198 | AF_LATIN_HINTS_DO_VERT_SNAP( hints ) ) ); |
2199 | |
2200 | for ( edge = edges; edge < edge_limit; edge++ ) |
2201 | { |
2202 | /* move the points of each segment */ |
2203 | /* in each edge to the edge's position */ |
2204 | AF_Segment seg = edge->first; |
2205 | |
2206 | |
2207 | if ( snapping ) |
2208 | { |
2209 | do |
2210 | { |
2211 | AF_Point point = seg->first; |
2212 | |
2213 | |
2214 | for (;;) |
2215 | { |
2216 | if ( dim == AF_DIMENSION_HORZ ) |
2217 | { |
2218 | point->x = edge->pos; |
2219 | point->flags |= AF_FLAG_TOUCH_X; |
2220 | } |
2221 | else |
2222 | { |
2223 | point->y = edge->pos; |
2224 | point->flags |= AF_FLAG_TOUCH_Y; |
2225 | } |
2226 | |
2227 | if ( point == seg->last ) |
2228 | break; |
2229 | |
2230 | point = point->next; |
2231 | } |
2232 | |
2233 | seg = seg->edge_next; |
2234 | |
2235 | } while ( seg != edge->first ); |
2236 | } |
2237 | else |
2238 | { |
2239 | FT_Pos delta = edge->pos - edge->opos; |
2240 | |
2241 | |
2242 | do |
2243 | { |
2244 | AF_Point point = seg->first; |
2245 | |
2246 | |
2247 | for (;;) |
2248 | { |
2249 | if ( dim == AF_DIMENSION_HORZ ) |
2250 | { |
2251 | point->x += delta; |
2252 | point->flags |= AF_FLAG_TOUCH_X; |
2253 | } |
2254 | else |
2255 | { |
2256 | point->y += delta; |
2257 | point->flags |= AF_FLAG_TOUCH_Y; |
2258 | } |
2259 | |
2260 | if ( point == seg->last ) |
2261 | break; |
2262 | |
2263 | point = point->next; |
2264 | } |
2265 | |
2266 | seg = seg->edge_next; |
2267 | |
2268 | } while ( seg != edge->first ); |
2269 | } |
2270 | } |
2271 | } |
2272 | |
2273 | |
2274 | /* Apply the complete hinting algorithm to a CJK glyph. */ |
2275 | |
2276 | FT_LOCAL_DEF( FT_Error ) |
2277 | af_cjk_hints_apply( FT_UInt glyph_index, |
2278 | AF_GlyphHints hints, |
2279 | FT_Outline* outline, |
2280 | AF_StyleMetrics metrics_ ) /* AF_CJKMetrics */ |
2281 | { |
2282 | AF_CJKMetrics metrics = (AF_CJKMetrics)metrics_; |
2283 | |
2284 | FT_Error error; |
2285 | int dim; |
2286 | |
2287 | FT_UNUSED( metrics ); |
2288 | FT_UNUSED( glyph_index ); |
2289 | |
2290 | |
2291 | error = af_glyph_hints_reload( hints, outline ); |
2292 | if ( error ) |
2293 | goto Exit; |
2294 | |
2295 | /* analyze glyph outline */ |
2296 | if ( AF_HINTS_DO_HORIZONTAL( hints ) ) |
2297 | { |
2298 | error = af_cjk_hints_detect_features( hints, AF_DIMENSION_HORZ ); |
2299 | if ( error ) |
2300 | goto Exit; |
2301 | |
2302 | af_cjk_hints_compute_blue_edges( hints, metrics, AF_DIMENSION_HORZ ); |
2303 | } |
2304 | |
2305 | if ( AF_HINTS_DO_VERTICAL( hints ) ) |
2306 | { |
2307 | error = af_cjk_hints_detect_features( hints, AF_DIMENSION_VERT ); |
2308 | if ( error ) |
2309 | goto Exit; |
2310 | |
2311 | af_cjk_hints_compute_blue_edges( hints, metrics, AF_DIMENSION_VERT ); |
2312 | } |
2313 | |
2314 | /* grid-fit the outline */ |
2315 | for ( dim = 0; dim < AF_DIMENSION_MAX; dim++ ) |
2316 | { |
2317 | if ( ( dim == AF_DIMENSION_HORZ && AF_HINTS_DO_HORIZONTAL( hints ) ) || |
2318 | ( dim == AF_DIMENSION_VERT && AF_HINTS_DO_VERTICAL( hints ) ) ) |
2319 | { |
2320 | af_cjk_hint_edges( hints, (AF_Dimension)dim ); |
2321 | af_cjk_align_edge_points( hints, (AF_Dimension)dim ); |
2322 | af_glyph_hints_align_strong_points( hints, (AF_Dimension)dim ); |
2323 | af_glyph_hints_align_weak_points( hints, (AF_Dimension)dim ); |
2324 | } |
2325 | } |
2326 | |
2327 | af_glyph_hints_save( hints, outline ); |
2328 | |
2329 | Exit: |
2330 | return error; |
2331 | } |
2332 | |
2333 | |
2334 | /*************************************************************************/ |
2335 | /*************************************************************************/ |
2336 | /***** *****/ |
2337 | /***** C J K S C R I P T C L A S S *****/ |
2338 | /***** *****/ |
2339 | /*************************************************************************/ |
2340 | /*************************************************************************/ |
2341 | |
2342 | |
2343 | AF_DEFINE_WRITING_SYSTEM_CLASS( |
2344 | af_cjk_writing_system_class, |
2345 | |
2346 | AF_WRITING_SYSTEM_CJK, |
2347 | |
2348 | sizeof ( AF_CJKMetricsRec ), |
2349 | |
2350 | (AF_WritingSystem_InitMetricsFunc) af_cjk_metrics_init, /* style_metrics_init */ |
2351 | (AF_WritingSystem_ScaleMetricsFunc)af_cjk_metrics_scale, /* style_metrics_scale */ |
2352 | (AF_WritingSystem_DoneMetricsFunc) NULL, /* style_metrics_done */ |
2353 | (AF_WritingSystem_GetStdWidthsFunc)af_cjk_get_standard_widths, /* style_metrics_getstdw */ |
2354 | |
2355 | (AF_WritingSystem_InitHintsFunc) af_cjk_hints_init, /* style_hints_init */ |
2356 | (AF_WritingSystem_ApplyHintsFunc) af_cjk_hints_apply /* style_hints_apply */ |
2357 | ) |
2358 | |
2359 | |
2360 | #else /* !AF_CONFIG_OPTION_CJK */ |
2361 | |
2362 | |
2363 | AF_DEFINE_WRITING_SYSTEM_CLASS( |
2364 | af_cjk_writing_system_class, |
2365 | |
2366 | AF_WRITING_SYSTEM_CJK, |
2367 | |
2368 | sizeof ( AF_CJKMetricsRec ), |
2369 | |
2370 | (AF_WritingSystem_InitMetricsFunc) NULL, /* style_metrics_init */ |
2371 | (AF_WritingSystem_ScaleMetricsFunc)NULL, /* style_metrics_scale */ |
2372 | (AF_WritingSystem_DoneMetricsFunc) NULL, /* style_metrics_done */ |
2373 | (AF_WritingSystem_GetStdWidthsFunc)NULL, /* style_metrics_getstdw */ |
2374 | |
2375 | (AF_WritingSystem_InitHintsFunc) NULL, /* style_hints_init */ |
2376 | (AF_WritingSystem_ApplyHintsFunc) NULL /* style_hints_apply */ |
2377 | ) |
2378 | |
2379 | |
2380 | #endif /* !AF_CONFIG_OPTION_CJK */ |
2381 | |
2382 | |
2383 | /* END */ |
2384 | |