Sync with trunk head.
[reactos.git] / lib / 3rdparty / freetype / src / autofit / afcjk.c
1 /***************************************************************************/
2 /* */
3 /* afcjk.c */
4 /* */
5 /* Auto-fitter hinting routines for CJK script (body). */
6 /* */
7 /* Copyright 2006, 2007 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, available here:
20 *
21 * http://www.kde.gr.jp/~akito/patch/freetype2/
22 *
23 */
24
25 #include "aftypes.h"
26 #include "aflatin.h"
27
28
29 #ifdef AF_CONFIG_OPTION_CJK
30
31 #include "afcjk.h"
32 #include "aferrors.h"
33
34
35 #ifdef AF_USE_WARPER
36 #include "afwarp.h"
37 #endif
38
39
40 /*************************************************************************/
41 /*************************************************************************/
42 /***** *****/
43 /***** C J K G L O B A L M E T R I C S *****/
44 /***** *****/
45 /*************************************************************************/
46 /*************************************************************************/
47
48 static FT_Error
49 af_cjk_metrics_init( AF_LatinMetrics metrics,
50 FT_Face face )
51 {
52 FT_CharMap oldmap = face->charmap;
53
54
55 metrics->units_per_em = face->units_per_EM;
56
57 /* TODO are there blues? */
58
59 if ( FT_Select_Charmap( face, FT_ENCODING_UNICODE ) )
60 face->charmap = NULL;
61
62 /* latin's version would suffice */
63 af_latin_metrics_init_widths( metrics, face, 0x7530 );
64
65 FT_Set_Charmap( face, oldmap );
66
67 return AF_Err_Ok;
68 }
69
70
71 static void
72 af_cjk_metrics_scale_dim( AF_LatinMetrics metrics,
73 AF_Scaler scaler,
74 AF_Dimension dim )
75 {
76 AF_LatinAxis axis;
77
78
79 axis = &metrics->axis[dim];
80
81 if ( dim == AF_DIMENSION_HORZ )
82 {
83 axis->scale = scaler->x_scale;
84 axis->delta = scaler->x_delta;
85 }
86 else
87 {
88 axis->scale = scaler->y_scale;
89 axis->delta = scaler->y_delta;
90 }
91 }
92
93
94 static void
95 af_cjk_metrics_scale( AF_LatinMetrics metrics,
96 AF_Scaler scaler )
97 {
98 metrics->root.scaler = *scaler;
99
100 af_cjk_metrics_scale_dim( metrics, scaler, AF_DIMENSION_HORZ );
101 af_cjk_metrics_scale_dim( metrics, scaler, AF_DIMENSION_VERT );
102 }
103
104
105 /*************************************************************************/
106 /*************************************************************************/
107 /***** *****/
108 /***** C J K G L Y P H A N A L Y S I S *****/
109 /***** *****/
110 /*************************************************************************/
111 /*************************************************************************/
112
113 static FT_Error
114 af_cjk_hints_compute_segments( AF_GlyphHints hints,
115 AF_Dimension dim )
116 {
117 AF_AxisHints axis = &hints->axis[dim];
118 AF_Segment segments = axis->segments;
119 AF_Segment segment_limit = segments + axis->num_segments;
120 FT_Error error;
121 AF_Segment seg;
122
123
124 error = af_latin_hints_compute_segments( hints, dim );
125 if ( error )
126 return error;
127
128 /* a segment is round if it doesn't have successive */
129 /* on-curve points. */
130 for ( seg = segments; seg < segment_limit; seg++ )
131 {
132 AF_Point pt = seg->first;
133 AF_Point last = seg->last;
134 AF_Flags f0 = (AF_Flags)(pt->flags & AF_FLAG_CONTROL);
135 AF_Flags f1;
136
137
138 seg->flags &= ~AF_EDGE_ROUND;
139
140 for ( ; pt != last; f0 = f1 )
141 {
142 pt = pt->next;
143 f1 = (AF_Flags)(pt->flags & AF_FLAG_CONTROL);
144
145 if ( !f0 && !f1 )
146 break;
147
148 if ( pt == last )
149 seg->flags |= AF_EDGE_ROUND;
150 }
151 }
152
153 return AF_Err_Ok;
154 }
155
156
157 static void
158 af_cjk_hints_link_segments( AF_GlyphHints hints,
159 AF_Dimension dim )
160 {
161 AF_AxisHints axis = &hints->axis[dim];
162 AF_Segment segments = axis->segments;
163 AF_Segment segment_limit = segments + axis->num_segments;
164 AF_Direction major_dir = axis->major_dir;
165 AF_Segment seg1, seg2;
166 FT_Pos len_threshold;
167 FT_Pos dist_threshold;
168
169
170 len_threshold = AF_LATIN_CONSTANT( hints->metrics, 8 );
171
172 dist_threshold = ( dim == AF_DIMENSION_HORZ ) ? hints->x_scale
173 : hints->y_scale;
174 dist_threshold = FT_DivFix( 64 * 3, dist_threshold );
175
176 /* now compare each segment to the others */
177 for ( seg1 = segments; seg1 < segment_limit; seg1++ )
178 {
179 /* the fake segments are for metrics hinting only */
180 if ( seg1->first == seg1->last )
181 continue;
182
183 if ( seg1->dir != major_dir )
184 continue;
185
186 for ( seg2 = segments; seg2 < segment_limit; seg2++ )
187 if ( seg2 != seg1 && seg1->dir + seg2->dir == 0 )
188 {
189 FT_Pos dist = seg2->pos - seg1->pos;
190
191
192 if ( dist < 0 )
193 continue;
194
195 {
196 FT_Pos min = seg1->min_coord;
197 FT_Pos max = seg1->max_coord;
198 FT_Pos len;
199
200
201 if ( min < seg2->min_coord )
202 min = seg2->min_coord;
203
204 if ( max > seg2->max_coord )
205 max = seg2->max_coord;
206
207 len = max - min;
208 if ( len >= len_threshold )
209 {
210 if ( dist * 8 < seg1->score * 9 &&
211 ( dist * 8 < seg1->score * 7 || seg1->len < len ) )
212 {
213 seg1->score = dist;
214 seg1->len = len;
215 seg1->link = seg2;
216 }
217
218 if ( dist * 8 < seg2->score * 9 &&
219 ( dist * 8 < seg2->score * 7 || seg2->len < len ) )
220 {
221 seg2->score = dist;
222 seg2->len = len;
223 seg2->link = seg1;
224 }
225 }
226 }
227 }
228 }
229
230 /*
231 * now compute the `serif' segments
232 *
233 * In Hanzi, some strokes are wider on one or both of the ends.
234 * We either identify the stems on the ends as serifs or remove
235 * the linkage, depending on the length of the stems.
236 *
237 */
238
239 {
240 AF_Segment link1, link2;
241
242
243 for ( seg1 = segments; seg1 < segment_limit; seg1++ )
244 {
245 link1 = seg1->link;
246 if ( !link1 || link1->link != seg1 || link1->pos <= seg1->pos )
247 continue;
248
249 if ( seg1->score >= dist_threshold )
250 continue;
251
252 for ( seg2 = segments; seg2 < segment_limit; seg2++ )
253 {
254 if ( seg2->pos > seg1->pos || seg1 == seg2 )
255 continue;
256
257 link2 = seg2->link;
258 if ( !link2 || link2->link != seg2 || link2->pos < link1->pos )
259 continue;
260
261 if ( seg1->pos == seg2->pos && link1->pos == link2->pos )
262 continue;
263
264 if ( seg2->score <= seg1->score || seg1->score * 4 <= seg2->score )
265 continue;
266
267 /* seg2 < seg1 < link1 < link2 */
268
269 if ( seg1->len >= seg2->len * 3 )
270 {
271 AF_Segment seg;
272
273
274 for ( seg = segments; seg < segment_limit; seg++ )
275 {
276 AF_Segment link = seg->link;
277
278
279 if ( link == seg2 )
280 {
281 seg->link = 0;
282 seg->serif = link1;
283 }
284 else if ( link == link2 )
285 {
286 seg->link = 0;
287 seg->serif = seg1;
288 }
289 }
290 }
291 else
292 {
293 seg1->link = link1->link = 0;
294
295 break;
296 }
297 }
298 }
299 }
300
301 for ( seg1 = segments; seg1 < segment_limit; seg1++ )
302 {
303 seg2 = seg1->link;
304
305 if ( seg2 )
306 {
307 seg2->num_linked++;
308 if ( seg2->link != seg1 )
309 {
310 seg1->link = 0;
311
312 if ( seg2->score < dist_threshold || seg1->score < seg2->score * 4 )
313 seg1->serif = seg2->link;
314 else
315 seg2->num_linked--;
316 }
317 }
318 }
319 }
320
321
322 static FT_Error
323 af_cjk_hints_compute_edges( AF_GlyphHints hints,
324 AF_Dimension dim )
325 {
326 AF_AxisHints axis = &hints->axis[dim];
327 FT_Error error = AF_Err_Ok;
328 FT_Memory memory = hints->memory;
329 AF_LatinAxis laxis = &((AF_LatinMetrics)hints->metrics)->axis[dim];
330
331 AF_Segment segments = axis->segments;
332 AF_Segment segment_limit = segments + axis->num_segments;
333 AF_Segment seg;
334
335 FT_Fixed scale;
336 FT_Pos edge_distance_threshold;
337
338
339 axis->num_edges = 0;
340
341 scale = ( dim == AF_DIMENSION_HORZ ) ? hints->x_scale
342 : hints->y_scale;
343
344 /*********************************************************************/
345 /* */
346 /* We begin by generating a sorted table of edges for the current */
347 /* direction. To do so, we simply scan each segment and try to find */
348 /* an edge in our table that corresponds to its position. */
349 /* */
350 /* If no edge is found, we create and insert a new edge in the */
351 /* sorted table. Otherwise, we simply add the segment to the edge's */
352 /* list which is then processed in the second step to compute the */
353 /* edge's properties. */
354 /* */
355 /* Note that the edges table is sorted along the segment/edge */
356 /* position. */
357 /* */
358 /*********************************************************************/
359
360 edge_distance_threshold = FT_MulFix( laxis->edge_distance_threshold,
361 scale );
362 if ( edge_distance_threshold > 64 / 4 )
363 edge_distance_threshold = FT_DivFix( 64 / 4, scale );
364 else
365 edge_distance_threshold = laxis->edge_distance_threshold;
366
367 for ( seg = segments; seg < segment_limit; seg++ )
368 {
369 AF_Edge found = 0;
370 FT_Pos best = 0xFFFFU;
371 FT_Int ee;
372
373
374 /* look for an edge corresponding to the segment */
375 for ( ee = 0; ee < axis->num_edges; ee++ )
376 {
377 AF_Edge edge = axis->edges + ee;
378 FT_Pos dist;
379
380
381 if ( edge->dir != seg->dir )
382 continue;
383
384 dist = seg->pos - edge->fpos;
385 if ( dist < 0 )
386 dist = -dist;
387
388 if ( dist < edge_distance_threshold && dist < best )
389 {
390 AF_Segment link = seg->link;
391
392
393 /* check whether all linked segments of the candidate edge */
394 /* can make a single edge. */
395 if ( link )
396 {
397 AF_Segment seg1 = edge->first;
398 AF_Segment link1;
399 FT_Pos dist2 = 0;
400
401
402 do
403 {
404 link1 = seg1->link;
405 if ( link1 )
406 {
407 dist2 = AF_SEGMENT_DIST( link, link1 );
408 if ( dist2 >= edge_distance_threshold )
409 break;
410 }
411
412 } while ( ( seg1 = seg1->edge_next ) != edge->first );
413
414 if ( dist2 >= edge_distance_threshold )
415 continue;
416 }
417
418 best = dist;
419 found = edge;
420 }
421 }
422
423 if ( !found )
424 {
425 AF_Edge edge;
426
427
428 /* insert a new edge in the list and */
429 /* sort according to the position */
430 error = af_axis_hints_new_edge( axis, seg->pos, seg->dir, memory, &edge );
431 if ( error )
432 goto Exit;
433
434 /* add the segment to the new edge's list */
435 FT_ZERO( edge );
436
437 edge->first = seg;
438 edge->last = seg;
439 edge->fpos = seg->pos;
440 edge->opos = edge->pos = FT_MulFix( seg->pos, scale );
441 seg->edge_next = seg;
442 edge->dir = seg->dir;
443 }
444 else
445 {
446 /* if an edge was found, simply add the segment to the edge's */
447 /* list */
448 seg->edge_next = found->first;
449 found->last->edge_next = seg;
450 found->last = seg;
451 }
452 }
453
454 /*********************************************************************/
455 /* */
456 /* Good, we now compute each edge's properties according to segments */
457 /* found on its position. Basically, these are as follows. */
458 /* */
459 /* - edge's main direction */
460 /* - stem edge, serif edge or both (which defaults to stem then) */
461 /* - rounded edge, straight or both (which defaults to straight) */
462 /* - link for edge */
463 /* */
464 /*********************************************************************/
465
466 /* first of all, set the `edge' field in each segment -- this is */
467 /* required in order to compute edge links */
468 /* */
469 /* Note that removing this loop and setting the `edge' field of each */
470 /* segment directly in the code above slows down execution speed for */
471 /* some reasons on platforms like the Sun. */
472
473 {
474 AF_Edge edges = axis->edges;
475 AF_Edge edge_limit = edges + axis->num_edges;
476 AF_Edge edge;
477
478
479 for ( edge = edges; edge < edge_limit; edge++ )
480 {
481 seg = edge->first;
482 if ( seg )
483 do
484 {
485 seg->edge = edge;
486 seg = seg->edge_next;
487
488 } while ( seg != edge->first );
489 }
490
491 /* now compute each edge properties */
492 for ( edge = edges; edge < edge_limit; edge++ )
493 {
494 FT_Int is_round = 0; /* does it contain round segments? */
495 FT_Int is_straight = 0; /* does it contain straight segments? */
496
497
498 seg = edge->first;
499
500 do
501 {
502 FT_Bool is_serif;
503
504
505 /* check for roundness of segment */
506 if ( seg->flags & AF_EDGE_ROUND )
507 is_round++;
508 else
509 is_straight++;
510
511 /* check for links -- if seg->serif is set, then seg->link must */
512 /* be ignored */
513 is_serif = (FT_Bool)( seg->serif && seg->serif->edge != edge );
514
515 if ( seg->link || is_serif )
516 {
517 AF_Edge edge2;
518 AF_Segment seg2;
519
520
521 edge2 = edge->link;
522 seg2 = seg->link;
523
524 if ( is_serif )
525 {
526 seg2 = seg->serif;
527 edge2 = edge->serif;
528 }
529
530 if ( edge2 )
531 {
532 FT_Pos edge_delta;
533 FT_Pos seg_delta;
534
535
536 edge_delta = edge->fpos - edge2->fpos;
537 if ( edge_delta < 0 )
538 edge_delta = -edge_delta;
539
540 seg_delta = AF_SEGMENT_DIST( seg, seg2 );
541
542 if ( seg_delta < edge_delta )
543 edge2 = seg2->edge;
544 }
545 else
546 edge2 = seg2->edge;
547
548 if ( is_serif )
549 {
550 edge->serif = edge2;
551 edge2->flags |= AF_EDGE_SERIF;
552 }
553 else
554 edge->link = edge2;
555 }
556
557 seg = seg->edge_next;
558
559 } while ( seg != edge->first );
560
561 /* set the round/straight flags */
562 edge->flags = AF_EDGE_NORMAL;
563
564 if ( is_round > 0 && is_round >= is_straight )
565 edge->flags |= AF_EDGE_ROUND;
566
567 /* get rid of serifs if link is set */
568 /* XXX: This gets rid of many unpleasant artefacts! */
569 /* Example: the `c' in cour.pfa at size 13 */
570
571 if ( edge->serif && edge->link )
572 edge->serif = 0;
573 }
574 }
575
576 Exit:
577 return error;
578 }
579
580
581 static FT_Error
582 af_cjk_hints_detect_features( AF_GlyphHints hints,
583 AF_Dimension dim )
584 {
585 FT_Error error;
586
587
588 error = af_cjk_hints_compute_segments( hints, dim );
589 if ( !error )
590 {
591 af_cjk_hints_link_segments( hints, dim );
592
593 error = af_cjk_hints_compute_edges( hints, dim );
594 }
595 return error;
596 }
597
598
599 static FT_Error
600 af_cjk_hints_init( AF_GlyphHints hints,
601 AF_LatinMetrics metrics )
602 {
603 FT_Render_Mode mode;
604 FT_UInt32 scaler_flags, other_flags;
605
606
607 af_glyph_hints_rescale( hints, (AF_ScriptMetrics)metrics );
608
609 /*
610 * correct x_scale and y_scale when needed, since they may have
611 * been modified af_cjk_scale_dim above
612 */
613 hints->x_scale = metrics->axis[AF_DIMENSION_HORZ].scale;
614 hints->x_delta = metrics->axis[AF_DIMENSION_HORZ].delta;
615 hints->y_scale = metrics->axis[AF_DIMENSION_VERT].scale;
616 hints->y_delta = metrics->axis[AF_DIMENSION_VERT].delta;
617
618 /* compute flags depending on render mode, etc. */
619 mode = metrics->root.scaler.render_mode;
620
621 #ifdef AF_USE_WARPER
622 if ( mode == FT_RENDER_MODE_LCD || mode == FT_RENDER_MODE_LCD_V )
623 metrics->root.scaler.render_mode = mode = FT_RENDER_MODE_NORMAL;
624 #endif
625
626 scaler_flags = hints->scaler_flags;
627 other_flags = 0;
628
629 /*
630 * We snap the width of vertical stems for the monochrome and
631 * horizontal LCD rendering targets only.
632 */
633 if ( mode == FT_RENDER_MODE_MONO || mode == FT_RENDER_MODE_LCD )
634 other_flags |= AF_LATIN_HINTS_HORZ_SNAP;
635
636 /*
637 * We snap the width of horizontal stems for the monochrome and
638 * vertical LCD rendering targets only.
639 */
640 if ( mode == FT_RENDER_MODE_MONO || mode == FT_RENDER_MODE_LCD_V )
641 other_flags |= AF_LATIN_HINTS_VERT_SNAP;
642
643 /*
644 * We adjust stems to full pixels only if we don't use the `light' mode.
645 */
646 if ( mode != FT_RENDER_MODE_LIGHT )
647 other_flags |= AF_LATIN_HINTS_STEM_ADJUST;
648
649 if ( mode == FT_RENDER_MODE_MONO )
650 other_flags |= AF_LATIN_HINTS_MONO;
651
652 scaler_flags |= AF_SCALER_FLAG_NO_ADVANCE;
653
654 hints->scaler_flags = scaler_flags;
655 hints->other_flags = other_flags;
656
657 return 0;
658 }
659
660
661 /*************************************************************************/
662 /*************************************************************************/
663 /***** *****/
664 /***** C J K G L Y P H G R I D - F I T T I N G *****/
665 /***** *****/
666 /*************************************************************************/
667 /*************************************************************************/
668
669 /* snap a given width in scaled coordinates to one of the */
670 /* current standard widths */
671
672 static FT_Pos
673 af_cjk_snap_width( AF_Width widths,
674 FT_Int count,
675 FT_Pos width )
676 {
677 int n;
678 FT_Pos best = 64 + 32 + 2;
679 FT_Pos reference = width;
680 FT_Pos scaled;
681
682
683 for ( n = 0; n < count; n++ )
684 {
685 FT_Pos w;
686 FT_Pos dist;
687
688
689 w = widths[n].cur;
690 dist = width - w;
691 if ( dist < 0 )
692 dist = -dist;
693 if ( dist < best )
694 {
695 best = dist;
696 reference = w;
697 }
698 }
699
700 scaled = FT_PIX_ROUND( reference );
701
702 if ( width >= reference )
703 {
704 if ( width < scaled + 48 )
705 width = reference;
706 }
707 else
708 {
709 if ( width > scaled - 48 )
710 width = reference;
711 }
712
713 return width;
714 }
715
716
717 /* compute the snapped width of a given stem */
718
719 static FT_Pos
720 af_cjk_compute_stem_width( AF_GlyphHints hints,
721 AF_Dimension dim,
722 FT_Pos width,
723 AF_Edge_Flags base_flags,
724 AF_Edge_Flags stem_flags )
725 {
726 AF_LatinMetrics metrics = (AF_LatinMetrics) hints->metrics;
727 AF_LatinAxis axis = & metrics->axis[dim];
728 FT_Pos dist = width;
729 FT_Int sign = 0;
730 FT_Int vertical = ( dim == AF_DIMENSION_VERT );
731
732 FT_UNUSED( base_flags );
733 FT_UNUSED( stem_flags );
734
735
736 if ( !AF_LATIN_HINTS_DO_STEM_ADJUST( hints ) )
737 return width;
738
739 if ( dist < 0 )
740 {
741 dist = -width;
742 sign = 1;
743 }
744
745 if ( ( vertical && !AF_LATIN_HINTS_DO_VERT_SNAP( hints ) ) ||
746 ( !vertical && !AF_LATIN_HINTS_DO_HORZ_SNAP( hints ) ) )
747 {
748 /* smooth hinting process: very lightly quantize the stem width */
749
750 if ( axis->width_count > 0 )
751 {
752 if ( FT_ABS( dist - axis->widths[0].cur ) < 40 )
753 {
754 dist = axis->widths[0].cur;
755 if ( dist < 48 )
756 dist = 48;
757
758 goto Done_Width;
759 }
760 }
761
762 if ( dist < 54 )
763 dist += ( 54 - dist ) / 2 ;
764 else if ( dist < 3 * 64 )
765 {
766 FT_Pos delta;
767
768
769 delta = dist & 63;
770 dist &= -64;
771
772 if ( delta < 10 )
773 dist += delta;
774 else if ( delta < 22 )
775 dist += 10;
776 else if ( delta < 42 )
777 dist += delta;
778 else if ( delta < 54 )
779 dist += 54;
780 else
781 dist += delta;
782 }
783 }
784 else
785 {
786 /* strong hinting process: snap the stem width to integer pixels */
787
788 dist = af_cjk_snap_width( axis->widths, axis->width_count, dist );
789
790 if ( vertical )
791 {
792 /* in the case of vertical hinting, always round */
793 /* the stem heights to integer pixels */
794
795 if ( dist >= 64 )
796 dist = ( dist + 16 ) & ~63;
797 else
798 dist = 64;
799 }
800 else
801 {
802 if ( AF_LATIN_HINTS_DO_MONO( hints ) )
803 {
804 /* monochrome horizontal hinting: snap widths to integer pixels */
805 /* with a different threshold */
806
807 if ( dist < 64 )
808 dist = 64;
809 else
810 dist = ( dist + 32 ) & ~63;
811 }
812 else
813 {
814 /* for horizontal anti-aliased hinting, we adopt a more subtle */
815 /* approach: we strengthen small stems, round stems whose size */
816 /* is between 1 and 2 pixels to an integer, otherwise nothing */
817
818 if ( dist < 48 )
819 dist = ( dist + 64 ) >> 1;
820
821 else if ( dist < 128 )
822 dist = ( dist + 22 ) & ~63;
823 else
824 /* round otherwise to prevent color fringes in LCD mode */
825 dist = ( dist + 32 ) & ~63;
826 }
827 }
828 }
829
830 Done_Width:
831 if ( sign )
832 dist = -dist;
833
834 return dist;
835 }
836
837
838 /* align one stem edge relative to the previous stem edge */
839
840 static void
841 af_cjk_align_linked_edge( AF_GlyphHints hints,
842 AF_Dimension dim,
843 AF_Edge base_edge,
844 AF_Edge stem_edge )
845 {
846 FT_Pos dist = stem_edge->opos - base_edge->opos;
847
848 FT_Pos fitted_width = af_cjk_compute_stem_width(
849 hints, dim, dist,
850 (AF_Edge_Flags)base_edge->flags,
851 (AF_Edge_Flags)stem_edge->flags );
852
853
854 stem_edge->pos = base_edge->pos + fitted_width;
855 }
856
857
858 static void
859 af_cjk_align_serif_edge( AF_GlyphHints hints,
860 AF_Edge base,
861 AF_Edge serif )
862 {
863 FT_UNUSED( hints );
864
865 serif->pos = base->pos + ( serif->opos - base->opos );
866 }
867
868
869 /*************************************************************************/
870 /*************************************************************************/
871 /*************************************************************************/
872 /**** ****/
873 /**** E D G E H I N T I N G ****/
874 /**** ****/
875 /*************************************************************************/
876 /*************************************************************************/
877 /*************************************************************************/
878
879
880 #define AF_LIGHT_MODE_MAX_HORZ_GAP 9
881 #define AF_LIGHT_MODE_MAX_VERT_GAP 15
882 #define AF_LIGHT_MODE_MAX_DELTA_ABS 14
883
884
885 static FT_Pos
886 af_hint_normal_stem( AF_GlyphHints hints,
887 AF_Edge edge,
888 AF_Edge edge2,
889 FT_Pos anchor,
890 AF_Dimension dim )
891 {
892 FT_Pos org_len, cur_len, org_center;
893 FT_Pos cur_pos1, cur_pos2;
894 FT_Pos d_off1, u_off1, d_off2, u_off2, delta;
895 FT_Pos offset;
896 FT_Pos threshold = 64;
897
898
899 if ( !AF_LATIN_HINTS_DO_STEM_ADJUST( hints ) )
900 {
901 if ( ( edge->flags & AF_EDGE_ROUND ) &&
902 ( edge2->flags & AF_EDGE_ROUND ) )
903 {
904 if ( dim == AF_DIMENSION_VERT )
905 threshold = 64 - AF_LIGHT_MODE_MAX_HORZ_GAP;
906 else
907 threshold = 64 - AF_LIGHT_MODE_MAX_VERT_GAP;
908 }
909 else
910 {
911 if ( dim == AF_DIMENSION_VERT )
912 threshold = 64 - AF_LIGHT_MODE_MAX_HORZ_GAP / 3;
913 else
914 threshold = 64 - AF_LIGHT_MODE_MAX_VERT_GAP / 3;
915 }
916 }
917
918 org_len = edge2->opos - edge->opos;
919 cur_len = af_cjk_compute_stem_width( hints, dim, org_len,
920 (AF_Edge_Flags)edge->flags,
921 (AF_Edge_Flags)edge2->flags );
922
923 org_center = ( edge->opos + edge2->opos ) / 2 + anchor;
924 cur_pos1 = org_center - cur_len / 2;
925 cur_pos2 = cur_pos1 + cur_len;
926 d_off1 = cur_pos1 - FT_PIX_FLOOR( cur_pos1 );
927 d_off2 = cur_pos2 - FT_PIX_FLOOR( cur_pos2 );
928 u_off1 = 64 - d_off1;
929 u_off2 = 64 - d_off2;
930 delta = 0;
931
932
933 if ( d_off1 == 0 || d_off2 == 0 )
934 goto Exit;
935
936 if ( cur_len <= threshold )
937 {
938 if ( d_off2 < cur_len )
939 {
940 if ( u_off1 <= d_off2 )
941 delta = u_off1;
942 else
943 delta = -d_off2;
944 }
945
946 goto Exit;
947 }
948
949 if ( threshold < 64 )
950 {
951 if ( d_off1 >= threshold || u_off1 >= threshold ||
952 d_off2 >= threshold || u_off2 >= threshold )
953 goto Exit;
954 }
955
956 offset = cur_len % 64;
957
958 if ( offset < 32 )
959 {
960 if ( u_off1 <= offset || d_off2 <= offset )
961 goto Exit;
962 }
963 else
964 offset = 64 - threshold;
965
966 d_off1 = threshold - u_off1;
967 u_off1 = u_off1 - offset;
968 u_off2 = threshold - d_off2;
969 d_off2 = d_off2 - offset;
970
971 if ( d_off1 <= u_off1 )
972 u_off1 = -d_off1;
973
974 if ( d_off2 <= u_off2 )
975 u_off2 = -d_off2;
976
977 if ( FT_ABS( u_off1 ) <= FT_ABS( u_off2 ) )
978 delta = u_off1;
979 else
980 delta = u_off2;
981
982 Exit:
983
984 #if 1
985 if ( !AF_LATIN_HINTS_DO_STEM_ADJUST( hints ) )
986 {
987 if ( delta > AF_LIGHT_MODE_MAX_DELTA_ABS )
988 delta = AF_LIGHT_MODE_MAX_DELTA_ABS;
989 else if ( delta < -AF_LIGHT_MODE_MAX_DELTA_ABS )
990 delta = -AF_LIGHT_MODE_MAX_DELTA_ABS;
991 }
992 #endif
993
994 cur_pos1 += delta;
995
996 if ( edge->opos < edge2->opos )
997 {
998 edge->pos = cur_pos1;
999 edge2->pos = cur_pos1 + cur_len;
1000 }
1001 else
1002 {
1003 edge->pos = cur_pos1 + cur_len;
1004 edge2->pos = cur_pos1;
1005 }
1006
1007 return delta;
1008 }
1009
1010
1011 static void
1012 af_cjk_hint_edges( AF_GlyphHints hints,
1013 AF_Dimension dim )
1014 {
1015 AF_AxisHints axis = &hints->axis[dim];
1016 AF_Edge edges = axis->edges;
1017 AF_Edge edge_limit = edges + axis->num_edges;
1018 FT_Int n_edges;
1019 AF_Edge edge;
1020 AF_Edge anchor = 0;
1021 FT_Pos delta = 0;
1022 FT_Int skipped = 0;
1023
1024
1025 /* now we align all stem edges. */
1026 for ( edge = edges; edge < edge_limit; edge++ )
1027 {
1028 AF_Edge edge2;
1029
1030
1031 if ( edge->flags & AF_EDGE_DONE )
1032 continue;
1033
1034 /* skip all non-stem edges */
1035 edge2 = edge->link;
1036 if ( !edge2 )
1037 {
1038 skipped++;
1039 continue;
1040 }
1041
1042 /* now align the stem */
1043
1044 if ( edge2 < edge )
1045 {
1046 af_cjk_align_linked_edge( hints, dim, edge2, edge );
1047 edge->flags |= AF_EDGE_DONE;
1048 continue;
1049 }
1050
1051 if ( dim != AF_DIMENSION_VERT && !anchor )
1052 {
1053
1054 #if 0
1055 if ( fixedpitch )
1056 {
1057 AF_Edge left = edge;
1058 AF_Edge right = edge_limit - 1;
1059 AF_EdgeRec left1, left2, right1, right2;
1060 FT_Pos target, center1, center2;
1061 FT_Pos delta1, delta2, d1, d2;
1062
1063
1064 while ( right > left && !right->link )
1065 right--;
1066
1067 left1 = *left;
1068 left2 = *left->link;
1069 right1 = *right->link;
1070 right2 = *right;
1071
1072 delta = ( ( ( hinter->pp2.x + 32 ) & -64 ) - hinter->pp2.x ) / 2;
1073 target = left->opos + ( right->opos - left->opos ) / 2 + delta - 16;
1074
1075 delta1 = delta;
1076 delta1 += af_hint_normal_stem( hints, left, left->link,
1077 delta1, 0 );
1078
1079 if ( left->link != right )
1080 af_hint_normal_stem( hints, right->link, right, delta1, 0 );
1081
1082 center1 = left->pos + ( right->pos - left->pos ) / 2;
1083
1084 if ( center1 >= target )
1085 delta2 = delta - 32;
1086 else
1087 delta2 = delta + 32;
1088
1089 delta2 += af_hint_normal_stem( hints, &left1, &left2, delta2, 0 );
1090
1091 if ( delta1 != delta2 )
1092 {
1093 if ( left->link != right )
1094 af_hint_normal_stem( hints, &right1, &right2, delta2, 0 );
1095
1096 center2 = left1.pos + ( right2.pos - left1.pos ) / 2;
1097
1098 d1 = center1 - target;
1099 d2 = center2 - target;
1100
1101 if ( FT_ABS( d2 ) < FT_ABS( d1 ) )
1102 {
1103 left->pos = left1.pos;
1104 left->link->pos = left2.pos;
1105
1106 if ( left->link != right )
1107 {
1108 right->link->pos = right1.pos;
1109 right->pos = right2.pos;
1110 }
1111
1112 delta1 = delta2;
1113 }
1114 }
1115
1116 delta = delta1;
1117 right->link->flags |= AF_EDGE_DONE;
1118 right->flags |= AF_EDGE_DONE;
1119 }
1120 else
1121
1122 #endif /* 0 */
1123
1124 delta = af_hint_normal_stem( hints, edge, edge2, 0,
1125 AF_DIMENSION_HORZ );
1126 }
1127 else
1128 af_hint_normal_stem( hints, edge, edge2, delta, dim );
1129
1130 #if 0
1131 printf( "stem (%d,%d) adjusted (%.1f,%.1f)\n",
1132 edge - edges, edge2 - edges,
1133 ( edge->pos - edge->opos ) / 64.0,
1134 ( edge2->pos - edge2->opos ) / 64.0 );
1135 #endif
1136
1137 anchor = edge;
1138 edge->flags |= AF_EDGE_DONE;
1139 edge2->flags |= AF_EDGE_DONE;
1140 }
1141
1142 /* make sure that lowercase m's maintain their symmetry */
1143
1144 /* In general, lowercase m's have six vertical edges if they are sans */
1145 /* serif, or twelve if they are with serifs. This implementation is */
1146 /* based on that assumption, and seems to work very well with most */
1147 /* faces. However, if for a certain face this assumption is not */
1148 /* true, the m is just rendered like before. In addition, any stem */
1149 /* correction will only be applied to symmetrical glyphs (even if the */
1150 /* glyph is not an m), so the potential for unwanted distortion is */
1151 /* relatively low. */
1152
1153 /* We don't handle horizontal edges since we can't easily assure that */
1154 /* the third (lowest) stem aligns with the base line; it might end up */
1155 /* one pixel higher or lower. */
1156
1157 n_edges = edge_limit - edges;
1158 if ( dim == AF_DIMENSION_HORZ && ( n_edges == 6 || n_edges == 12 ) )
1159 {
1160 AF_Edge edge1, edge2, edge3;
1161 FT_Pos dist1, dist2, span;
1162
1163
1164 if ( n_edges == 6 )
1165 {
1166 edge1 = edges;
1167 edge2 = edges + 2;
1168 edge3 = edges + 4;
1169 }
1170 else
1171 {
1172 edge1 = edges + 1;
1173 edge2 = edges + 5;
1174 edge3 = edges + 9;
1175 }
1176
1177 dist1 = edge2->opos - edge1->opos;
1178 dist2 = edge3->opos - edge2->opos;
1179
1180 span = dist1 - dist2;
1181 if ( span < 0 )
1182 span = -span;
1183
1184 if ( edge1->link == edge1 + 1 &&
1185 edge2->link == edge2 + 1 &&
1186 edge3->link == edge3 + 1 && span < 8 )
1187 {
1188 delta = edge3->pos - ( 2 * edge2->pos - edge1->pos );
1189 edge3->pos -= delta;
1190 if ( edge3->link )
1191 edge3->link->pos -= delta;
1192
1193 /* move the serifs along with the stem */
1194 if ( n_edges == 12 )
1195 {
1196 ( edges + 8 )->pos -= delta;
1197 ( edges + 11 )->pos -= delta;
1198 }
1199
1200 edge3->flags |= AF_EDGE_DONE;
1201 if ( edge3->link )
1202 edge3->link->flags |= AF_EDGE_DONE;
1203 }
1204 }
1205
1206 if ( !skipped )
1207 return;
1208
1209 /*
1210 * now hint the remaining edges (serifs and single) in order
1211 * to complete our processing
1212 */
1213 for ( edge = edges; edge < edge_limit; edge++ )
1214 {
1215 if ( edge->flags & AF_EDGE_DONE )
1216 continue;
1217
1218 if ( edge->serif )
1219 {
1220 af_cjk_align_serif_edge( hints, edge->serif, edge );
1221 edge->flags |= AF_EDGE_DONE;
1222 skipped--;
1223 }
1224 }
1225
1226 if ( !skipped )
1227 return;
1228
1229 for ( edge = edges; edge < edge_limit; edge++ )
1230 {
1231 AF_Edge before, after;
1232
1233
1234 if ( edge->flags & AF_EDGE_DONE )
1235 continue;
1236
1237 before = after = edge;
1238
1239 while ( --before >= edges )
1240 if ( before->flags & AF_EDGE_DONE )
1241 break;
1242
1243 while ( ++after < edge_limit )
1244 if ( after->flags & AF_EDGE_DONE )
1245 break;
1246
1247 if ( before >= edges || after < edge_limit )
1248 {
1249 if ( before < edges )
1250 af_cjk_align_serif_edge( hints, after, edge );
1251 else if ( after >= edge_limit )
1252 af_cjk_align_serif_edge( hints, before, edge );
1253 else
1254 edge->pos = before->pos +
1255 FT_MulDiv( edge->fpos - before->fpos,
1256 after->pos - before->pos,
1257 after->fpos - before->fpos );
1258 }
1259 }
1260 }
1261
1262
1263 static void
1264 af_cjk_align_edge_points( AF_GlyphHints hints,
1265 AF_Dimension dim )
1266 {
1267 AF_AxisHints axis = & hints->axis[dim];
1268 AF_Edge edges = axis->edges;
1269 AF_Edge edge_limit = edges + axis->num_edges;
1270 AF_Edge edge;
1271 FT_Bool snapping;
1272
1273
1274 snapping = FT_BOOL( ( dim == AF_DIMENSION_HORZ &&
1275 AF_LATIN_HINTS_DO_HORZ_SNAP( hints ) ) ||
1276 ( dim == AF_DIMENSION_VERT &&
1277 AF_LATIN_HINTS_DO_VERT_SNAP( hints ) ) );
1278
1279 for ( edge = edges; edge < edge_limit; edge++ )
1280 {
1281 /* move the points of each segment */
1282 /* in each edge to the edge's position */
1283 AF_Segment seg = edge->first;
1284
1285
1286 if ( snapping )
1287 {
1288 do
1289 {
1290 AF_Point point = seg->first;
1291
1292
1293 for (;;)
1294 {
1295 if ( dim == AF_DIMENSION_HORZ )
1296 {
1297 point->x = edge->pos;
1298 point->flags |= AF_FLAG_TOUCH_X;
1299 }
1300 else
1301 {
1302 point->y = edge->pos;
1303 point->flags |= AF_FLAG_TOUCH_Y;
1304 }
1305
1306 if ( point == seg->last )
1307 break;
1308
1309 point = point->next;
1310 }
1311
1312 seg = seg->edge_next;
1313
1314 } while ( seg != edge->first );
1315 }
1316 else
1317 {
1318 FT_Pos delta = edge->pos - edge->opos;
1319
1320
1321 do
1322 {
1323 AF_Point point = seg->first;
1324
1325
1326 for (;;)
1327 {
1328 if ( dim == AF_DIMENSION_HORZ )
1329 {
1330 point->x += delta;
1331 point->flags |= AF_FLAG_TOUCH_X;
1332 }
1333 else
1334 {
1335 point->y += delta;
1336 point->flags |= AF_FLAG_TOUCH_Y;
1337 }
1338
1339 if ( point == seg->last )
1340 break;
1341
1342 point = point->next;
1343 }
1344
1345 seg = seg->edge_next;
1346
1347 } while ( seg != edge->first );
1348 }
1349 }
1350 }
1351
1352
1353 static FT_Error
1354 af_cjk_hints_apply( AF_GlyphHints hints,
1355 FT_Outline* outline,
1356 AF_LatinMetrics metrics )
1357 {
1358 FT_Error error;
1359 int dim;
1360
1361 FT_UNUSED( metrics );
1362
1363
1364 error = af_glyph_hints_reload( hints, outline, 0 );
1365 if ( error )
1366 goto Exit;
1367
1368 /* analyze glyph outline */
1369 if ( AF_HINTS_DO_HORIZONTAL( hints ) )
1370 {
1371 error = af_cjk_hints_detect_features( hints, AF_DIMENSION_HORZ );
1372 if ( error )
1373 goto Exit;
1374 }
1375
1376 if ( AF_HINTS_DO_VERTICAL( hints ) )
1377 {
1378 error = af_cjk_hints_detect_features( hints, AF_DIMENSION_VERT );
1379 if ( error )
1380 goto Exit;
1381 }
1382
1383 /* grid-fit the outline */
1384 for ( dim = 0; dim < AF_DIMENSION_MAX; dim++ )
1385 {
1386 if ( ( dim == AF_DIMENSION_HORZ && AF_HINTS_DO_HORIZONTAL( hints ) ) ||
1387 ( dim == AF_DIMENSION_VERT && AF_HINTS_DO_VERTICAL( hints ) ) )
1388 {
1389
1390 #ifdef AF_USE_WARPER
1391 if ( dim == AF_DIMENSION_HORZ &&
1392 metrics->root.scaler.render_mode == FT_RENDER_MODE_NORMAL )
1393 {
1394 AF_WarperRec warper;
1395 FT_Fixed scale;
1396 FT_Pos delta;
1397
1398
1399 af_warper_compute( &warper, hints, dim, &scale, &delta );
1400 af_glyph_hints_scale_dim( hints, dim, scale, delta );
1401 continue;
1402 }
1403 #endif /* AF_USE_WARPER */
1404
1405 af_cjk_hint_edges( hints, (AF_Dimension)dim );
1406 af_cjk_align_edge_points( hints, (AF_Dimension)dim );
1407 af_glyph_hints_align_strong_points( hints, (AF_Dimension)dim );
1408 af_glyph_hints_align_weak_points( hints, (AF_Dimension)dim );
1409 }
1410 }
1411
1412 #if 0
1413 af_glyph_hints_dump_points( hints );
1414 af_glyph_hints_dump_segments( hints );
1415 af_glyph_hints_dump_edges( hints );
1416 #endif
1417
1418 af_glyph_hints_save( hints, outline );
1419
1420 Exit:
1421 return error;
1422 }
1423
1424
1425 /*************************************************************************/
1426 /*************************************************************************/
1427 /***** *****/
1428 /***** C J K S C R I P T C L A S S *****/
1429 /***** *****/
1430 /*************************************************************************/
1431 /*************************************************************************/
1432
1433
1434 static const AF_Script_UniRangeRec af_cjk_uniranges[] =
1435 {
1436 #if 0
1437 { 0x0100, 0xFFFF }, /* why this? */
1438 #endif
1439 { 0x2E80, 0x2EFF }, /* CJK Radicals Supplement */
1440 { 0x2F00, 0x2FDF }, /* Kangxi Radicals */
1441 { 0x3000, 0x303F }, /* CJK Symbols and Punctuation */
1442 { 0x3040, 0x309F }, /* Hiragana */
1443 { 0x30A0, 0x30FF }, /* Katakana */
1444 { 0x3100, 0x312F }, /* Bopomofo */
1445 { 0x3130, 0x318F }, /* Hangul Compatibility Jamo */
1446 { 0x31A0, 0x31BF }, /* Bopomofo Extended */
1447 { 0x31C0, 0x31EF }, /* CJK Strokes */
1448 { 0x31F0, 0x31FF }, /* Katakana Phonetic Extensions */
1449 { 0x3200, 0x32FF }, /* Enclosed CJK Letters and Months */
1450 { 0x3300, 0x33FF }, /* CJK Compatibility */
1451 { 0x3400, 0x4DBF }, /* CJK Unified Ideographs Extension A */
1452 { 0x4DC0, 0x4DFF }, /* Yijing Hexagram Symbols */
1453 { 0x4E00, 0x9FFF }, /* CJK Unified Ideographs */
1454 { 0xF900, 0xFAFF }, /* CJK Compatibility Ideographs */
1455 { 0xFE30, 0xFE4F }, /* CJK Compatibility Forms */
1456 { 0xFF00, 0xFFEF }, /* Halfwidth and Fullwidth Forms */
1457 { 0x20000, 0x2A6DF }, /* CJK Unified Ideographs Extension B */
1458 { 0x2F800, 0x2FA1F }, /* CJK Compatibility Ideographs Supplement */
1459 { 0, 0 }
1460 };
1461
1462
1463 FT_CALLBACK_TABLE_DEF const AF_ScriptClassRec
1464 af_cjk_script_class =
1465 {
1466 AF_SCRIPT_CJK,
1467 af_cjk_uniranges,
1468
1469 sizeof( AF_LatinMetricsRec ),
1470
1471 (AF_Script_InitMetricsFunc) af_cjk_metrics_init,
1472 (AF_Script_ScaleMetricsFunc)af_cjk_metrics_scale,
1473 (AF_Script_DoneMetricsFunc) NULL,
1474
1475 (AF_Script_InitHintsFunc) af_cjk_hints_init,
1476 (AF_Script_ApplyHintsFunc) af_cjk_hints_apply
1477 };
1478
1479 #else /* !AF_CONFIG_OPTION_CJK */
1480
1481 static const AF_Script_UniRangeRec af_cjk_uniranges[] =
1482 {
1483 { 0, 0 }
1484 };
1485
1486
1487 FT_CALLBACK_TABLE_DEF const AF_ScriptClassRec
1488 af_cjk_script_class =
1489 {
1490 AF_SCRIPT_CJK,
1491 af_cjk_uniranges,
1492
1493 sizeof( AF_LatinMetricsRec ),
1494
1495 (AF_Script_InitMetricsFunc) NULL,
1496 (AF_Script_ScaleMetricsFunc)NULL,
1497 (AF_Script_DoneMetricsFunc) NULL,
1498
1499 (AF_Script_InitHintsFunc) NULL,
1500 (AF_Script_ApplyHintsFunc) NULL
1501 };
1502
1503 #endif /* !AF_CONFIG_OPTION_CJK */
1504
1505
1506 /* END */