s/lower_upper/upper_lower/.
[ttfautohint.git] / lib / tabytecode.c
blob74b35f079da8273df3eb8d38ea6a4cb498d2bf00
1 /* tabytecode.c */
3 /*
4 * Copyright (C) 2011-2012 by Werner Lemberg.
6 * This file is part of the ttfautohint library, and may only be used,
7 * modified, and distributed under the terms given in `COPYING'. By
8 * continuing to use, modify, or distribute this file you indicate that you
9 * have read `COPYING' and understand and accept it fully.
11 * The file `COPYING' mentioned in the previous paragraph is distributed
12 * with the ttfautohint library.
16 #include "ta.h"
17 #include <string.h>
20 #undef MISSING
21 #define MISSING (FT_UInt)~0
23 /* a simple macro to emit bytecode instructions */
24 #define BCI(code) *(bufp++) = (code)
26 /* we increase the stack depth by this amount */
27 #define ADDITIONAL_STACK_ELEMENTS 20
30 /* #define DEBUGGING */
33 #ifdef TA_DEBUG
34 int _ta_debug = 1;
35 int _ta_debug_disable_horz_hints;
36 int _ta_debug_disable_vert_hints;
37 int _ta_debug_disable_blue_hints;
38 void* _ta_debug_hints;
39 #endif
42 typedef struct Hints_Record_
44 FT_UInt size;
45 FT_UInt num_actions;
46 FT_Byte* buf;
47 FT_UInt buf_len;
48 } Hints_Record;
50 typedef struct Recorder_
52 FONT* font;
53 GLYPH* glyph; /* the current glyph */
54 Hints_Record hints_record;
56 /* some segments can `wrap around' */
57 /* a contour's start point like 24-25-26-0-1-2 */
58 /* (there can be at most one such segment per contour); */
59 /* later on we append additional records */
60 /* to split them into 24-26 and 0-2 */
61 FT_UInt* wrap_around_segments;
62 FT_UInt num_wrap_around_segments;
64 FT_UShort num_stack_elements; /* the necessary stack depth so far */
66 /* data necessary for strong point interpolation */
67 FT_UInt* ip_before_points;
68 FT_UInt* ip_after_points;
69 FT_UInt* ip_on_point_array;
70 FT_UInt* ip_between_point_array;
72 FT_UInt num_strong_points;
73 FT_UInt num_segments;
74 } Recorder;
77 /* We add a subglyph for each composite glyph. */
78 /* Since subglyphs must contain at least one point, */
79 /* we have to adjust all point indices accordingly. */
80 /* Using the `pointsums' array of the `GLYPH' structure */
81 /* it is straightforward to do that: */
82 /* Assuming that point with index x is in the interval */
83 /* pointsums[n] <= x < pointsums[n + 1], */
84 /* the new point index is x + n. */
86 static FT_UInt
87 TA_adjust_point_index(Recorder* recorder,
88 FT_UInt idx)
90 GLYPH* glyph = recorder->glyph;
91 FT_UShort i;
94 if (!glyph->num_components)
95 return idx; /* not a composite glyph */
97 for (i = 0; i < glyph->num_pointsums; i++)
98 if (idx < glyph->pointsums[i])
99 break;
101 return idx + i;
105 /* we store the segments in the storage area; */
106 /* each segment record consists of the first and last point */
108 static FT_Byte*
109 TA_sfnt_build_glyph_segments(SFNT* sfnt,
110 Recorder* recorder,
111 FT_Byte* bufp)
113 FONT* font = recorder->font;
114 TA_GlyphHints hints = &font->loader->hints;
115 TA_AxisHints axis = &hints->axis[TA_DIMENSION_VERT];
116 TA_Point points = hints->points;
117 TA_Segment segments = axis->segments;
118 TA_Segment seg;
119 TA_Segment seg_limit;
121 FT_Outline outline = font->loader->gloader->base.outline;
123 FT_UInt* args;
124 FT_UInt* arg;
125 FT_UInt num_args;
126 FT_UInt nargs;
127 FT_UInt num_segments;
129 FT_Bool need_words = 0;
131 FT_Int n;
132 FT_UInt i, j;
133 FT_UInt base;
134 FT_UInt num_packed_segments;
135 FT_UInt num_storage;
136 FT_UInt num_stack_elements;
137 FT_UInt num_twilight_points;
140 seg_limit = segments + axis->num_segments;
141 num_segments = axis->num_segments;
143 /* to pack the data in the bytecode more tightly, */
144 /* we store up to the first nine segments in nibbles if possible, */
145 /* using delta values */
146 base = 0;
147 num_packed_segments = 0;
148 for (seg = segments; seg < seg_limit; seg++)
150 FT_UInt first = seg->first - points;
151 FT_UInt last = seg->last - points;
154 first = TA_adjust_point_index(recorder, first);
155 last = TA_adjust_point_index(recorder, last);
157 if (first - base >= 16)
158 break;
159 if (first > last || last - first >= 16)
160 break;
161 if (num_packed_segments == 9)
162 break;
163 num_packed_segments++;
164 base = last;
167 /* also handle wrap-around segments */
168 num_segments += recorder->num_wrap_around_segments;
170 /* wrap-around segments are pushed with four arguments; */
171 /* a segment stored in nibbles needs only one byte instead of two */
172 num_args = num_packed_segments
173 + 2 * (num_segments - num_packed_segments)
174 + 2 * recorder->num_wrap_around_segments
175 + 2;
177 /* collect all arguments temporarily in an array (in reverse order) */
178 /* so that we can easily split into chunks of 255 args */
179 /* as needed by NPUSHB and NPUSHW, respectively */
180 args = (FT_UInt*)malloc(num_args * sizeof (FT_UInt));
181 if (!args)
182 return NULL;
184 arg = args + num_args - 1;
186 if (num_segments > 0xFF)
187 need_words = 1;
189 /* the number of packed segments is indicated by the function number */
190 if (recorder->glyph->num_components)
191 *(arg--) = bci_create_segments_composite_0 + num_packed_segments;
192 else
193 *(arg--) = bci_create_segments_0 + num_packed_segments;
194 *(arg--) = num_segments;
196 base = 0;
197 for (seg = segments; seg < segments + num_packed_segments; seg++)
199 FT_UInt first = seg->first - points;
200 FT_UInt last = seg->last - points;
201 FT_UInt low_nibble;
202 FT_UInt high_nibble;
205 first = TA_adjust_point_index(recorder, first);
206 last = TA_adjust_point_index(recorder, last);
208 low_nibble = first - base;
209 high_nibble = last - first;
211 *(arg--) = 16 * high_nibble + low_nibble;
213 base = last;
215 if (last > 0xFF)
216 need_words = 1;
219 for (seg = segments + num_packed_segments; seg < seg_limit; seg++)
221 FT_UInt first = seg->first - points;
222 FT_UInt last = seg->last - points;
225 *(arg--) = TA_adjust_point_index(recorder, first);
226 *(arg--) = TA_adjust_point_index(recorder, last);
228 /* we push the last and first contour point */
229 /* as a third and fourth argument in wrap-around segments */
230 if (first > last)
232 for (n = 0; n < outline.n_contours; n++)
234 FT_UInt end = (FT_UInt)outline.contours[n];
237 if (first <= end)
239 *(arg--) = TA_adjust_point_index(recorder, end);
240 if (end > 0xFF)
241 need_words = 1;
243 if (n == 0)
244 *(arg--) = TA_adjust_point_index(recorder, 0);
245 else
246 *(arg--) = TA_adjust_point_index(recorder,
247 (FT_UInt)outline.contours[n - 1] + 1);
248 break;
253 if (last > 0xFF)
254 need_words = 1;
257 /* emit the second part of wrap-around segments as separate segments */
258 /* so that edges can easily link to them */
259 for (seg = segments; seg < seg_limit; seg++)
261 FT_UInt first = seg->first - points;
262 FT_UInt last = seg->last - points;
265 if (first > last)
267 for (n = 0; n < outline.n_contours; n++)
269 if (first <= (FT_UInt)outline.contours[n])
271 if (n == 0)
272 *(arg--) = TA_adjust_point_index(recorder, 0);
273 else
274 *(arg--) = TA_adjust_point_index(recorder,
275 (FT_UInt)outline.contours[n - 1] + 1);
276 break;
280 *(arg--) = TA_adjust_point_index(recorder, last);
283 /* with most fonts it is very rare */
284 /* that any of the pushed arguments is larger than 0xFF, */
285 /* thus we refrain from further optimizing this case */
287 arg = args;
289 if (need_words)
291 for (i = 0; i < num_args; i += 255)
293 nargs = (num_args - i > 255) ? 255 : num_args - i;
295 BCI(NPUSHW);
296 BCI(nargs);
297 for (j = 0; j < nargs; j++)
299 BCI(HIGH(*arg));
300 BCI(LOW(*arg));
301 arg++;
305 else
307 for (i = 0; i < num_args; i += 255)
309 nargs = (num_args - i > 255) ? 255 : num_args - i;
311 BCI(NPUSHB);
312 BCI(nargs);
313 for (j = 0; j < nargs; j++)
315 BCI(*arg);
316 arg++;
321 BCI(CALL);
323 num_storage = sal_segment_offset + num_segments * 2;
324 if (num_storage > sfnt->max_storage)
325 sfnt->max_storage = num_storage;
327 num_twilight_points = num_segments * 2;
328 if (num_twilight_points > sfnt->max_twilight_points)
329 sfnt->max_twilight_points = num_twilight_points;
331 /* both this function and `TA_emit_hints_record' */
332 /* push data onto the stack */
333 num_stack_elements = ADDITIONAL_STACK_ELEMENTS
334 + recorder->num_stack_elements + num_args;
335 if (num_stack_elements > sfnt->max_stack_elements)
336 sfnt->max_stack_elements = num_stack_elements;
338 free(args);
340 return bufp;
344 static FT_Byte*
345 TA_sfnt_build_glyph_scaler(SFNT* sfnt,
346 Recorder* recorder,
347 FT_Byte* bufp)
349 FT_GlyphSlot glyph = sfnt->face->glyph;
350 FT_Vector* points = glyph->outline.points;
351 FT_Int num_contours = glyph->outline.n_contours;
353 FT_UInt* args;
354 FT_UInt* arg;
355 FT_UInt num_args;
356 FT_UInt nargs;
358 FT_Bool need_words = 0;
359 FT_Int p, q;
360 FT_UInt i, j;
361 FT_Int start, end;
362 FT_UInt num_stack_elements;
365 num_args = 2 * num_contours + 2;
367 /* collect all arguments temporarily in an array (in reverse order) */
368 /* so that we can easily split into chunks of 255 args */
369 /* as needed by NPUSHB and NPUSHW, respectively */
370 args = (FT_UInt*)malloc(num_args * sizeof (FT_UInt));
371 if (!args)
372 return NULL;
374 arg = args + num_args - 1;
376 if (num_args > 0xFF)
377 need_words = 1;
379 if (recorder->glyph->num_components)
380 *(arg--) = bci_scale_composite_glyph;
381 else
382 *(arg--) = bci_scale_glyph;
383 *(arg--) = num_contours;
385 start = 0;
386 end = 0;
388 for (p = 0; p < num_contours; p++)
390 FT_Int max = start;
391 FT_Int min = start;
394 end = glyph->outline.contours[p];
396 for (q = start; q <= end; q++)
398 if (points[q].y < points[min].y)
399 min = q;
400 if (points[q].y > points[max].y)
401 max = q;
404 if (min > max)
406 *(arg--) = TA_adjust_point_index(recorder, max);
407 *(arg--) = TA_adjust_point_index(recorder, min);
409 else
411 *(arg--) = TA_adjust_point_index(recorder, min);
412 *(arg--) = TA_adjust_point_index(recorder, max);
415 start = end + 1;
418 if (end > 0xFF)
419 need_words = 1;
421 /* with most fonts it is very rare */
422 /* that any of the pushed arguments is larger than 0xFF, */
423 /* thus we refrain from further optimizing this case */
425 arg = args;
427 if (need_words)
429 for (i = 0; i < num_args; i += 255)
431 nargs = (num_args - i > 255) ? 255 : num_args - i;
433 BCI(NPUSHW);
434 BCI(nargs);
435 for (j = 0; j < nargs; j++)
437 BCI(HIGH(*arg));
438 BCI(LOW(*arg));
439 arg++;
443 else
445 for (i = 0; i < num_args; i += 255)
447 nargs = (num_args - i > 255) ? 255 : num_args - i;
449 BCI(NPUSHB);
450 BCI(nargs);
451 for (j = 0; j < nargs; j++)
453 BCI(*arg);
454 arg++;
459 BCI(CALL);
461 num_stack_elements = ADDITIONAL_STACK_ELEMENTS + num_args;
462 if (num_stack_elements > sfnt->max_stack_elements)
463 sfnt->max_stack_elements = num_stack_elements;
465 free(args);
467 return bufp;
471 static FT_Byte*
472 TA_font_build_subglyph_shifter(FONT* font,
473 FT_Byte* bufp)
475 FT_Face face = font->loader->face;
476 FT_GlyphSlot glyph = face->glyph;
478 TA_GlyphLoader gloader = font->loader->gloader;
480 TA_SubGlyph subglyphs = gloader->base.subglyphs;
481 TA_SubGlyph subglyph_limit = subglyphs + gloader->base.num_subglyphs;
482 TA_SubGlyph subglyph;
484 FT_Int curr_contour = 0;
487 for (subglyph = subglyphs; subglyph < subglyph_limit; subglyph++)
489 FT_Error error;
491 FT_UShort flags = subglyph->flags;
492 FT_Pos y_offset = subglyph->arg2;
494 FT_Int num_contours;
497 /* load subglyph to get the number of contours */
498 error = FT_Load_Glyph(face, subglyph->index, FT_LOAD_NO_SCALE);
499 if (error)
500 return NULL;
501 num_contours = glyph->outline.n_contours;
503 /* nothing to do if there is a point-to-point alignment */
504 if (!(flags & FT_SUBGLYPH_FLAG_ARGS_ARE_XY_VALUES))
505 goto End;
507 /* nothing to do if y offset is zero */
508 if (!y_offset)
509 goto End;
511 /* nothing to do if there are no contours */
512 if (!num_contours)
513 goto End;
515 /* note that calling `FT_Load_Glyph' without FT_LOAD_NO_RECURSE */
516 /* ensures that composite subglyphs are represented as simple glyphs */
518 if (num_contours > 0xFF
519 || curr_contour > 0xFF)
521 BCI(PUSHW_2);
522 BCI(HIGH(curr_contour));
523 BCI(LOW(curr_contour));
524 BCI(HIGH(num_contours));
525 BCI(LOW(num_contours));
527 else
529 BCI(PUSHB_2);
530 BCI(curr_contour);
531 BCI(num_contours);
534 /* there are high chances that this value needs PUSHW, */
535 /* thus we handle it separately */
536 if (y_offset > 0xFF || y_offset < 0)
538 BCI(PUSHW_1);
539 BCI(HIGH(y_offset));
540 BCI(LOW(y_offset));
542 else
544 BCI(PUSHB_1);
545 BCI(y_offset);
548 BCI(PUSHB_1);
549 BCI(bci_shift_subglyph);
550 BCI(CALL);
552 End:
553 curr_contour += num_contours;
556 return bufp;
561 * The four `ta_ip_*' actions in the `TA_hints_recorder' callback store its
562 * data in four arrays (which are simple but waste a lot of memory). The
563 * function below converts them into bytecode.
565 * For `ta_ip_before' and `ta_ip_after', the collected points are emitted
566 * together with the edge they correspond to.
568 * For both `ta_ip_on' and `ta_ip_between', an outer loop is constructed to
569 * loop over the edge or edge pairs, respectively, and each edge or edge
570 * pair contains an inner loop to emit the correponding points.
573 static void
574 TA_build_point_hints(Recorder* recorder,
575 TA_GlyphHints hints)
577 TA_AxisHints axis = &hints->axis[TA_DIMENSION_VERT];
578 TA_Segment segments = axis->segments;
579 TA_Edge edges = axis->edges;
581 TA_Edge edge;
582 TA_Edge before;
583 TA_Edge after;
585 FT_Byte* p = recorder->hints_record.buf;
586 FT_UInt num_edges = axis->num_edges;
587 FT_UInt num_strong_points = recorder->num_strong_points;
589 FT_UInt i;
590 FT_UInt j;
591 FT_UInt k;
593 FT_UInt* ip;
594 FT_UInt* iq;
595 FT_UInt* ir;
596 FT_UInt* ip_limit;
597 FT_UInt* iq_limit;
598 FT_UInt* ir_limit;
601 /* we store everything as 16bit numbers; */
602 /* the function numbers (`ta_ip_before', etc.) */
603 /* reflect the order in the TA_Action enumeration */
605 /* ip_before_points */
607 i = 0;
608 ip = recorder->ip_before_points;
609 ip_limit = ip + num_strong_points;
610 for (; ip < ip_limit; ip++)
612 if (*ip != MISSING)
613 i++;
614 else
615 break;
618 if (i)
620 recorder->hints_record.num_actions++;
622 edge = edges;
624 *(p++) = 0;
625 *(p++) = (FT_Byte)ta_ip_before + ACTION_OFFSET;
626 *(p++) = HIGH(edge->first - segments);
627 *(p++) = LOW(edge->first - segments);
628 *(p++) = HIGH(i);
629 *(p++) = LOW(i);
631 ip = recorder->ip_before_points;
632 ip_limit = ip + i;
633 for (; ip < ip_limit; ip++)
635 FT_UInt point = TA_adjust_point_index(recorder, *ip);
638 *(p++) = HIGH(point);
639 *(p++) = LOW(point);
643 /* ip_after_points */
645 i = 0;
646 ip = recorder->ip_after_points;
647 ip_limit = ip + num_strong_points;
648 for (; ip < ip_limit; ip++)
650 if (*ip != MISSING)
651 i++;
652 else
653 break;
656 if (i)
658 recorder->hints_record.num_actions++;
660 edge = edges + axis->num_edges - 1;
662 *(p++) = 0;
663 *(p++) = (FT_Byte)ta_ip_after + ACTION_OFFSET;
664 *(p++) = HIGH(edge->first - segments);
665 *(p++) = LOW(edge->first - segments);
666 *(p++) = HIGH(i);
667 *(p++) = LOW(i);
669 ip = recorder->ip_after_points;
670 ip_limit = ip + i;
671 for (; ip < ip_limit; ip++)
673 FT_UInt point = TA_adjust_point_index(recorder, *ip);
676 *(p++) = HIGH(point);
677 *(p++) = LOW(point);
681 /* ip_on_point_array */
683 i = 0;
684 ip = recorder->ip_on_point_array;
685 ip_limit = ip + num_edges * num_strong_points;
686 for (; ip < ip_limit; ip += num_strong_points)
687 if (*ip != MISSING)
688 i++;
690 if (i)
692 recorder->hints_record.num_actions++;
694 *(p++) = 0;
695 *(p++) = (FT_Byte)ta_ip_on + ACTION_OFFSET;
696 *(p++) = HIGH(i);
697 *(p++) = LOW(i);
699 i = 0;
700 ip = recorder->ip_on_point_array;
701 ip_limit = ip + num_edges * num_strong_points;
702 for (; ip < ip_limit; ip += num_strong_points, i++)
704 if (*ip == MISSING)
705 continue;
707 edge = edges + i;
709 *(p++) = HIGH(edge->first - segments);
710 *(p++) = LOW(edge->first - segments);
712 j = 0;
713 iq = ip;
714 iq_limit = iq + num_strong_points;
715 for (; iq < iq_limit; iq++)
717 if (*iq != MISSING)
718 j++;
719 else
720 break;
723 *(p++) = HIGH(j);
724 *(p++) = LOW(j);
726 iq = ip;
727 iq_limit = iq + j;
728 for (; iq < iq_limit; iq++)
730 FT_UInt point = TA_adjust_point_index(recorder, *iq);
733 *(p++) = HIGH(point);
734 *(p++) = LOW(point);
739 /* ip_between_point_array */
741 i = 0;
742 ip = recorder->ip_between_point_array;
743 ip_limit = ip + num_edges * num_edges * num_strong_points;
744 for (; ip < ip_limit; ip += num_strong_points)
745 if (*ip != MISSING)
746 i++;
748 if (i)
750 recorder->hints_record.num_actions++;
752 *(p++) = 0;
753 *(p++) = (FT_Byte)ta_ip_between + ACTION_OFFSET;
754 *(p++) = HIGH(i);
755 *(p++) = LOW(i);
757 i = 0;
758 ip = recorder->ip_between_point_array;
759 ip_limit = ip + num_edges * num_edges * num_strong_points;
760 for (;
761 ip < ip_limit;
762 ip += num_edges * num_strong_points, i++)
764 before = edges + i;
766 j = 0;
767 iq = ip;
768 iq_limit = iq + num_edges * num_strong_points;
769 for (; iq < iq_limit; iq += num_strong_points, j++)
771 if (*iq == MISSING)
772 continue;
774 after = edges + j;
776 *(p++) = HIGH(after->first - segments);
777 *(p++) = LOW(after->first - segments);
778 *(p++) = HIGH(before->first - segments);
779 *(p++) = LOW(before->first - segments);
781 k = 0;
782 ir = iq;
783 ir_limit = ir + num_strong_points;
784 for (; ir < ir_limit; ir++)
786 if (*ir != MISSING)
787 k++;
788 else
789 break;
792 *(p++) = HIGH(k);
793 *(p++) = LOW(k);
795 ir = iq;
796 ir_limit = ir + k;
797 for (; ir < ir_limit; ir++)
799 FT_UInt point = TA_adjust_point_index(recorder, *ir);
802 *(p++) = HIGH(point);
803 *(p++) = LOW(point);
809 recorder->hints_record.buf = p;
813 static FT_Bool
814 TA_hints_record_is_different(Hints_Record* hints_records,
815 FT_UInt num_hints_records,
816 FT_Byte* start,
817 FT_Byte* end)
819 Hints_Record last_hints_record;
822 if (!hints_records)
823 return 1;
825 /* we only need to compare with the last hints record */
826 last_hints_record = hints_records[num_hints_records - 1];
828 if ((FT_UInt)(end - start) != last_hints_record.buf_len)
829 return 1;
831 if (memcmp(start, last_hints_record.buf, last_hints_record.buf_len))
832 return 1;
834 return 0;
838 static FT_Error
839 TA_add_hints_record(Hints_Record** hints_records,
840 FT_UInt* num_hints_records,
841 FT_Byte* start,
842 Hints_Record hints_record)
844 Hints_Record* hints_records_new;
845 FT_UInt buf_len;
846 /* at this point, `hints_record.buf' still points into `ins_buf' */
847 FT_Byte* end = hints_record.buf;
850 buf_len = (FT_UInt)(end - start);
852 /* now fill the structure completely */
853 hints_record.buf_len = buf_len;
854 hints_record.buf = (FT_Byte*)malloc(buf_len);
855 if (!hints_record.buf)
856 return FT_Err_Out_Of_Memory;
858 memcpy(hints_record.buf, start, buf_len);
860 (*num_hints_records)++;
861 hints_records_new =
862 (Hints_Record*)realloc(*hints_records, *num_hints_records
863 * sizeof (Hints_Record));
864 if (!hints_records_new)
866 free(hints_record.buf);
867 (*num_hints_records)--;
868 return FT_Err_Out_Of_Memory;
870 else
871 *hints_records = hints_records_new;
873 (*hints_records)[*num_hints_records - 1] = hints_record;
875 return FT_Err_Ok;
879 static FT_Byte*
880 TA_emit_hints_record(Recorder* recorder,
881 Hints_Record* hints_record,
882 FT_Byte* bufp)
884 FT_Byte* p;
885 FT_Byte* endp;
886 FT_Bool need_words = 0;
888 FT_UInt i, j;
889 FT_UInt num_arguments;
890 FT_UInt num_args;
893 /* check whether any argument is larger than 0xFF */
894 endp = hints_record->buf + hints_record->buf_len;
895 for (p = hints_record->buf; p < endp; p += 2)
896 if (*p)
897 need_words = 1;
899 /* with most fonts it is very rare */
900 /* that any of the pushed arguments is larger than 0xFF, */
901 /* thus we refrain from further optimizing this case */
903 num_arguments = hints_record->buf_len / 2;
904 p = endp - 2;
906 if (need_words)
908 for (i = 0; i < num_arguments; i += 255)
910 num_args = (num_arguments - i > 255) ? 255 : (num_arguments - i);
912 BCI(NPUSHW);
913 BCI(num_args);
914 for (j = 0; j < num_args; j++)
916 BCI(*p);
917 BCI(*(p + 1));
918 p -= 2;
922 else
924 /* we only need the lower bytes */
925 p++;
927 for (i = 0; i < num_arguments; i += 255)
929 num_args = (num_arguments - i > 255) ? 255 : (num_arguments - i);
931 BCI(NPUSHB);
932 BCI(num_args);
933 for (j = 0; j < num_args; j++)
935 BCI(*p);
936 p -= 2;
941 /* collect stack depth data */
942 if (num_arguments > recorder->num_stack_elements)
943 recorder->num_stack_elements = num_arguments;
945 return bufp;
949 static FT_Byte*
950 TA_emit_hints_records(Recorder* recorder,
951 Hints_Record* hints_records,
952 FT_UInt num_hints_records,
953 FT_Byte* bufp)
955 FT_UInt i;
956 Hints_Record* hints_record;
959 hints_record = hints_records;
961 for (i = 0; i < num_hints_records - 1; i++)
963 BCI(MPPEM);
964 if (hints_record->size > 0xFF)
966 BCI(PUSHW_1);
967 BCI(HIGH((hints_record + 1)->size));
968 BCI(LOW((hints_record + 1)->size));
970 else
972 BCI(PUSHB_1);
973 BCI((hints_record + 1)->size);
975 BCI(LT);
976 BCI(IF);
977 bufp = TA_emit_hints_record(recorder, hints_record, bufp);
978 BCI(ELSE);
980 hints_record++;
983 bufp = TA_emit_hints_record(recorder, hints_record, bufp);
985 for (i = 0; i < num_hints_records - 1; i++)
986 BCI(EIF);
988 return bufp;
992 static void
993 TA_free_hints_records(Hints_Record* hints_records,
994 FT_UInt num_hints_records)
996 FT_UInt i;
999 for (i = 0; i < num_hints_records; i++)
1000 free(hints_records[i].buf);
1002 free(hints_records);
1006 static FT_Byte*
1007 TA_hints_recorder_handle_segments(FT_Byte* bufp,
1008 TA_AxisHints axis,
1009 TA_Edge edge,
1010 FT_UInt* wraps)
1012 TA_Segment segments = axis->segments;
1013 TA_Segment seg;
1014 FT_UInt seg_idx;
1015 FT_UInt num_segs = 0;
1016 FT_UInt* wrap;
1019 seg_idx = edge->first - segments;
1021 /* we store everything as 16bit numbers */
1022 *(bufp++) = HIGH(seg_idx);
1023 *(bufp++) = LOW(seg_idx);
1025 /* wrap-around segments are stored as two segments */
1026 if (edge->first->first > edge->first->last)
1027 num_segs++;
1029 seg = edge->first->edge_next;
1030 while (seg != edge->first)
1032 num_segs++;
1034 if (seg->first > seg->last)
1035 num_segs++;
1037 seg = seg->edge_next;
1040 *(bufp++) = HIGH(num_segs);
1041 *(bufp++) = LOW(num_segs);
1043 if (edge->first->first > edge->first->last)
1045 /* emit second part of wrap-around segment; */
1046 /* the bytecode positions such segments after `normal' ones */
1047 wrap = wraps;
1048 for (;;)
1050 if (seg_idx == *wrap)
1051 break;
1052 wrap++;
1055 *(bufp++) = HIGH(axis->num_segments + (wrap - wraps));
1056 *(bufp++) = LOW(axis->num_segments + (wrap - wraps));
1059 seg = edge->first->edge_next;
1060 while (seg != edge->first)
1062 seg_idx = seg - segments;
1064 *(bufp++) = HIGH(seg_idx);
1065 *(bufp++) = LOW(seg_idx);
1067 if (seg->first > seg->last)
1069 wrap = wraps;
1070 for (;;)
1072 if (seg_idx == *wrap)
1073 break;
1074 wrap++;
1077 *(bufp++) = HIGH(axis->num_segments + (wrap - wraps));
1078 *(bufp++) = LOW(axis->num_segments + (wrap - wraps));
1081 seg = seg->edge_next;
1084 return bufp;
1088 static void
1089 TA_hints_recorder(TA_Action action,
1090 TA_GlyphHints hints,
1091 TA_Dimension dim,
1092 void* arg1,
1093 TA_Edge arg2,
1094 TA_Edge arg3,
1095 TA_Edge lower_bound,
1096 TA_Edge upper_bound)
1098 TA_AxisHints axis = &hints->axis[dim];
1099 TA_Edge edges = axis->edges;
1100 TA_Segment segments = axis->segments;
1101 TA_Point points = hints->points;
1103 Recorder* recorder = (Recorder*)hints->user;
1104 FONT* font = recorder->font;
1105 FT_UInt* wraps = recorder->wrap_around_segments;
1106 FT_Byte* p = recorder->hints_record.buf;
1108 FT_UInt* ip;
1109 FT_UInt* limit;
1112 if (dim == TA_DIMENSION_HORZ)
1113 return;
1115 /* we collect point hints for later processing */
1116 switch (action)
1118 case ta_ip_before:
1120 TA_Point point = (TA_Point)arg1;
1123 ip = recorder->ip_before_points;
1124 limit = ip + recorder->num_strong_points;
1125 for (; ip < limit; ip++)
1127 if (*ip == MISSING)
1129 *ip = point - points;
1130 break;
1134 return;
1136 case ta_ip_after:
1138 TA_Point point = (TA_Point)arg1;
1141 ip = recorder->ip_after_points;
1142 limit = ip + recorder->num_strong_points;
1143 for (; ip < limit; ip++)
1145 if (*ip == MISSING)
1147 *ip = point - points;
1148 break;
1152 return;
1154 case ta_ip_on:
1156 TA_Point point = (TA_Point)arg1;
1157 TA_Edge edge = arg2;
1160 ip = recorder->ip_on_point_array
1161 + recorder->num_strong_points
1162 * (edge - edges);
1163 limit = ip + recorder->num_strong_points;
1164 for (; ip < limit; ip++)
1166 if (*ip == MISSING)
1168 *ip = point - points;
1169 break;
1173 return;
1175 case ta_ip_between:
1177 TA_Point point = (TA_Point)arg1;
1178 TA_Edge before = arg2;
1179 TA_Edge after = arg3;
1182 /* note that `recorder->num_segments' has been used for allocation, */
1183 /* but `axis->num_edges' is used for accessing this array */
1184 ip = recorder->ip_between_point_array
1185 + recorder->num_strong_points * axis->num_edges
1186 * (before - edges)
1187 + recorder->num_strong_points
1188 * (after - edges);
1189 limit = ip + recorder->num_strong_points;
1190 for (; ip < limit; ip++)
1192 if (*ip == MISSING)
1194 *ip = point - points;
1195 break;
1199 return;
1201 case ta_bound:
1202 /* we ignore the BOUND action since we signal this information */
1203 /* with the proper function number */
1204 return;
1206 default:
1207 break;
1210 /* some enum values correspond to four or eight bytecode functions; */
1211 /* if the value is n, the function numbers are n, ..., n+7, */
1212 /* to be differentiated with flags */
1214 switch (action)
1216 case ta_link:
1218 TA_Edge base_edge = (TA_Edge)arg1;
1219 TA_Edge stem_edge = arg2;
1222 *(p++) = 0;
1223 *(p++) = (FT_Byte)action + ACTION_OFFSET
1224 + ((stem_edge->flags & TA_EDGE_SERIF) != 0)
1225 + 2 * ((base_edge->flags & TA_EDGE_ROUND) != 0);
1227 *(p++) = HIGH(base_edge->first - segments);
1228 *(p++) = LOW(base_edge->first - segments);
1229 *(p++) = HIGH(stem_edge->first - segments);
1230 *(p++) = LOW(stem_edge->first - segments);
1232 p = TA_hints_recorder_handle_segments(p, axis, stem_edge, wraps);
1234 break;
1236 case ta_anchor:
1238 TA_Edge edge = (TA_Edge)arg1;
1239 TA_Edge edge2 = arg2;
1242 *(p++) = 0;
1243 *(p++) = (FT_Byte)action + ACTION_OFFSET
1244 + ((edge2->flags & TA_EDGE_SERIF) != 0)
1245 + 2 * ((edge->flags & TA_EDGE_ROUND) != 0);
1247 *(p++) = HIGH(edge->first - segments);
1248 *(p++) = LOW(edge->first - segments);
1249 *(p++) = HIGH(edge2->first - segments);
1250 *(p++) = LOW(edge2->first - segments);
1252 p = TA_hints_recorder_handle_segments(p, axis, edge, wraps);
1254 break;
1256 case ta_adjust:
1258 TA_Edge edge = (TA_Edge)arg1;
1259 TA_Edge edge2 = arg2;
1260 TA_Edge edge_minus_one = lower_bound;
1263 *(p++) = 0;
1264 *(p++) = (FT_Byte)action + ACTION_OFFSET
1265 + ((edge2->flags & TA_EDGE_SERIF) != 0)
1266 + 2 * ((edge->flags & TA_EDGE_ROUND) != 0)
1267 + 4 * (edge_minus_one != NULL);
1269 *(p++) = HIGH(edge->first - segments);
1270 *(p++) = LOW(edge->first - segments);
1271 *(p++) = HIGH(edge2->first - segments);
1272 *(p++) = LOW(edge2->first - segments);
1274 if (edge_minus_one)
1276 *(p++) = HIGH(edge_minus_one->first - segments);
1277 *(p++) = LOW(edge_minus_one->first - segments);
1280 p = TA_hints_recorder_handle_segments(p, axis, edge, wraps);
1282 break;
1284 case ta_blue_anchor:
1286 TA_Edge edge = (TA_Edge)arg1;
1287 TA_Edge blue = arg2;
1290 *(p++) = 0;
1291 *(p++) = (FT_Byte)action + ACTION_OFFSET;
1293 *(p++) = HIGH(blue->first - segments);
1294 *(p++) = LOW(blue->first - segments);
1296 if (edge->best_blue_is_shoot)
1298 *(p++) = HIGH(CVT_BLUE_SHOOTS_OFFSET(font) + edge->best_blue_idx);
1299 *(p++) = LOW(CVT_BLUE_SHOOTS_OFFSET(font) + edge->best_blue_idx);
1301 else
1303 *(p++) = HIGH(CVT_BLUE_REFS_OFFSET(font) + edge->best_blue_idx);
1304 *(p++) = LOW(CVT_BLUE_REFS_OFFSET(font) + edge->best_blue_idx);
1307 *(p++) = HIGH(edge->first - segments);
1308 *(p++) = LOW(edge->first - segments);
1310 p = TA_hints_recorder_handle_segments(p, axis, edge, wraps);
1312 break;
1314 case ta_stem:
1316 TA_Edge edge = (TA_Edge)arg1;
1317 TA_Edge edge2 = arg2;
1318 TA_Edge edge_minus_one = lower_bound;
1321 *(p++) = 0;
1322 *(p++) = (FT_Byte)action + ACTION_OFFSET
1323 + ((edge2->flags & TA_EDGE_SERIF) != 0)
1324 + 2 * ((edge->flags & TA_EDGE_ROUND) != 0)
1325 + 4 * (edge_minus_one != NULL);
1327 *(p++) = HIGH(edge->first - segments);
1328 *(p++) = LOW(edge->first - segments);
1329 *(p++) = HIGH(edge2->first - segments);
1330 *(p++) = LOW(edge2->first - segments);
1332 if (edge_minus_one)
1334 *(p++) = HIGH(edge_minus_one->first - segments);
1335 *(p++) = LOW(edge_minus_one->first - segments);
1338 p = TA_hints_recorder_handle_segments(p, axis, edge, wraps);
1339 p = TA_hints_recorder_handle_segments(p, axis, edge2, wraps);
1341 break;
1343 case ta_blue:
1345 TA_Edge edge = (TA_Edge)arg1;
1348 *(p++) = 0;
1349 *(p++) = (FT_Byte)action + ACTION_OFFSET;
1351 if (edge->best_blue_is_shoot)
1353 *(p++) = HIGH(CVT_BLUE_SHOOTS_OFFSET(font) + edge->best_blue_idx);
1354 *(p++) = LOW(CVT_BLUE_SHOOTS_OFFSET(font) + edge->best_blue_idx);
1356 else
1358 *(p++) = HIGH(CVT_BLUE_REFS_OFFSET(font) + edge->best_blue_idx);
1359 *(p++) = LOW(CVT_BLUE_REFS_OFFSET(font) + edge->best_blue_idx);
1362 *(p++) = HIGH(edge->first - segments);
1363 *(p++) = LOW(edge->first - segments);
1365 p = TA_hints_recorder_handle_segments(p, axis, edge, wraps);
1367 break;
1369 case ta_serif:
1371 TA_Edge serif = (TA_Edge)arg1;
1372 TA_Edge base = serif->serif;
1375 *(p++) = 0;
1376 *(p++) = (FT_Byte)action + ACTION_OFFSET
1377 + (lower_bound != NULL)
1378 + 2 * (upper_bound != NULL);
1380 *(p++) = HIGH(serif->first - segments);
1381 *(p++) = LOW(serif->first - segments);
1382 *(p++) = HIGH(base->first - segments);
1383 *(p++) = LOW(base->first - segments);
1385 if (lower_bound)
1387 *(p++) = HIGH(lower_bound->first - segments);
1388 *(p++) = LOW(lower_bound->first - segments);
1390 if (upper_bound)
1392 *(p++) = HIGH(upper_bound->first - segments);
1393 *(p++) = LOW(upper_bound->first - segments);
1396 p = TA_hints_recorder_handle_segments(p, axis, serif, wraps);
1398 break;
1400 case ta_serif_anchor:
1401 case ta_serif_link2:
1403 TA_Edge edge = (TA_Edge)arg1;
1406 *(p++) = 0;
1407 *(p++) = (FT_Byte)action + ACTION_OFFSET
1408 + (lower_bound != NULL)
1409 + 2 * (upper_bound != NULL);
1411 *(p++) = HIGH(edge->first - segments);
1412 *(p++) = LOW(edge->first - segments);
1414 if (lower_bound)
1416 *(p++) = HIGH(lower_bound->first - segments);
1417 *(p++) = LOW(lower_bound->first - segments);
1419 if (upper_bound)
1421 *(p++) = HIGH(upper_bound->first - segments);
1422 *(p++) = LOW(upper_bound->first - segments);
1425 p = TA_hints_recorder_handle_segments(p, axis, edge, wraps);
1427 break;
1429 case ta_serif_link1:
1431 TA_Edge edge = (TA_Edge)arg1;
1432 TA_Edge before = arg2;
1433 TA_Edge after = arg3;
1436 *(p++) = 0;
1437 *(p++) = (FT_Byte)action + ACTION_OFFSET
1438 + (lower_bound != NULL)
1439 + 2 * (upper_bound != NULL);
1441 *(p++) = HIGH(before->first - segments);
1442 *(p++) = LOW(before->first - segments);
1443 *(p++) = HIGH(edge->first - segments);
1444 *(p++) = LOW(edge->first - segments);
1445 *(p++) = HIGH(after->first - segments);
1446 *(p++) = LOW(after->first - segments);
1448 if (lower_bound)
1450 *(p++) = HIGH(lower_bound->first - segments);
1451 *(p++) = LOW(lower_bound->first - segments);
1453 if (upper_bound)
1455 *(p++) = HIGH(upper_bound->first - segments);
1456 *(p++) = LOW(upper_bound->first - segments);
1459 p = TA_hints_recorder_handle_segments(p, axis, edge, wraps);
1461 break;
1463 default:
1464 /* there are more cases in the enumeration */
1465 /* which are handled with flags */
1466 break;
1469 recorder->hints_record.num_actions++;
1470 recorder->hints_record.buf = p;
1474 static FT_Error
1475 TA_init_recorder(Recorder* recorder,
1476 FONT* font,
1477 GLYPH* glyph,
1478 TA_GlyphHints hints)
1480 TA_AxisHints axis = &hints->axis[TA_DIMENSION_VERT];
1481 TA_Point points = hints->points;
1482 TA_Point point_limit = points + hints->num_points;
1483 TA_Point point;
1485 TA_Segment segments = axis->segments;
1486 TA_Segment seg_limit = segments + axis->num_segments;
1487 TA_Segment seg;
1489 FT_UInt num_strong_points = 0;
1490 FT_UInt* wrap_around_segment;
1492 recorder->font = font;
1493 recorder->glyph = glyph;
1494 recorder->num_segments = axis->num_segments;
1496 recorder->ip_before_points = NULL;
1497 recorder->ip_after_points = NULL;
1498 recorder->ip_on_point_array = NULL;
1499 recorder->ip_between_point_array = NULL;
1501 recorder->num_stack_elements = 0;
1503 /* no need to clean up allocated arrays in case of error; */
1504 /* this is handled later by `TA_free_recorder' */
1506 recorder->num_wrap_around_segments = 0;
1507 for (seg = segments; seg < seg_limit; seg++)
1508 if (seg->first > seg->last)
1509 recorder->num_wrap_around_segments++;
1511 recorder->wrap_around_segments =
1512 (FT_UInt*)malloc(recorder->num_wrap_around_segments * sizeof (FT_UInt));
1513 if (!recorder->wrap_around_segments)
1514 return FT_Err_Out_Of_Memory;
1516 wrap_around_segment = recorder->wrap_around_segments;
1517 for (seg = segments; seg < seg_limit; seg++)
1518 if (seg->first > seg->last)
1519 *(wrap_around_segment++) = seg - segments;
1521 /* get number of strong points */
1522 for (point = points; point < point_limit; point++)
1524 /* actually, we need to test `TA_FLAG_TOUCH_Y' also; */
1525 /* however, this value isn't known yet */
1526 /* (or rather, it can vary between different pixel sizes) */
1527 if (point->flags & TA_FLAG_WEAK_INTERPOLATION)
1528 continue;
1530 num_strong_points++;
1533 recorder->num_strong_points = num_strong_points;
1535 recorder->ip_before_points =
1536 (FT_UInt*)malloc(num_strong_points * sizeof (FT_UInt));
1537 if (!recorder->ip_before_points)
1538 return FT_Err_Out_Of_Memory;
1540 recorder->ip_after_points =
1541 (FT_UInt*)malloc(num_strong_points * sizeof (FT_UInt));
1542 if (!recorder->ip_after_points)
1543 return FT_Err_Out_Of_Memory;
1545 /* actually, we need `hints->num_edges' for the array sizes; */
1546 /* however, this value isn't known yet */
1547 /* (or rather, it can vary between different pixel sizes) */
1548 recorder->ip_on_point_array =
1549 (FT_UInt*)malloc(axis->num_segments
1550 * num_strong_points * sizeof (FT_UInt));
1551 if (!recorder->ip_on_point_array)
1552 return FT_Err_Out_Of_Memory;
1554 recorder->ip_between_point_array =
1555 (FT_UInt*)malloc(axis->num_segments * axis->num_segments
1556 * num_strong_points * sizeof (FT_UInt));
1557 if (!recorder->ip_between_point_array)
1558 return FT_Err_Out_Of_Memory;
1560 return FT_Err_Ok;
1564 static void
1565 TA_reset_recorder(Recorder* recorder,
1566 FT_Byte* bufp)
1568 recorder->hints_record.buf = bufp;
1569 recorder->hints_record.num_actions = 0;
1573 static void
1574 TA_rewind_recorder(Recorder* recorder,
1575 FT_Byte* bufp,
1576 FT_UInt size)
1578 TA_reset_recorder(recorder, bufp);
1580 recorder->hints_record.size = size;
1582 /* We later check with MISSING (which expands to 0xFF bytes) */
1584 memset(recorder->ip_before_points, 0xFF,
1585 recorder->num_strong_points * sizeof (FT_UInt));
1586 memset(recorder->ip_after_points, 0xFF,
1587 recorder->num_strong_points * sizeof (FT_UInt));
1589 memset(recorder->ip_on_point_array, 0xFF,
1590 recorder->num_segments
1591 * recorder->num_strong_points * sizeof (FT_UInt));
1592 memset(recorder->ip_between_point_array, 0xFF,
1593 recorder->num_segments * recorder->num_segments
1594 * recorder->num_strong_points * sizeof (FT_UInt));
1598 static void
1599 TA_free_recorder(Recorder* recorder)
1601 free(recorder->wrap_around_segments);
1603 free(recorder->ip_before_points);
1604 free(recorder->ip_after_points);
1605 free(recorder->ip_on_point_array);
1606 free(recorder->ip_between_point_array);
1610 FT_Error
1611 TA_sfnt_build_glyph_instructions(SFNT* sfnt,
1612 FONT* font,
1613 FT_Long idx)
1615 FT_Face face = sfnt->face;
1616 FT_Error error;
1618 FT_Byte* ins_buf;
1619 FT_UInt ins_len;
1620 FT_Byte* bufp;
1621 FT_Byte* p;
1623 SFNT_Table* glyf_table = &font->tables[sfnt->glyf_idx];
1624 glyf_Data* data = (glyf_Data*)glyf_table->data;
1625 /* `idx' is never negative */
1626 GLYPH* glyph = &data->glyphs[idx];
1628 TA_GlyphHints hints;
1630 FT_UInt num_action_hints_records;
1631 FT_UInt num_point_hints_records;
1632 Hints_Record* action_hints_records;
1633 Hints_Record* point_hints_records;
1635 Recorder recorder;
1636 FT_UInt num_stack_elements;
1638 FT_Int32 load_flags;
1639 FT_UInt size;
1642 /* XXX: right now, we abuse this flag to control */
1643 /* the global behaviour of the auto-hinter */
1644 load_flags = font->fallback_script << 30;
1645 load_flags |= 1 << 28; /* vertical hinting only */
1646 if (font->increase_x_height)
1647 load_flags |= 1 << 29;
1648 if (!font->pre_hinting)
1649 load_flags |= FT_LOAD_NO_SCALE;
1651 /* computing the segments is resolution independent, */
1652 /* thus the pixel size in this call is arbitrary */
1653 error = FT_Set_Pixel_Sizes(face, 20, 20);
1654 if (error)
1655 return error;
1657 ta_loader_register_hints_recorder(font->loader, NULL, NULL);
1658 error = ta_loader_load_glyph(font->loader, face, (FT_UInt)idx, load_flags);
1659 if (error)
1660 return error;
1662 /* do nothing if we have an empty glyph */
1663 if (!face->glyph->outline.n_contours)
1664 return FT_Err_Ok;
1666 hints = &font->loader->hints;
1668 /* do nothing if the setup delivered the dummy module only */
1669 if (!hints->num_points)
1670 return FT_Err_Ok;
1672 /* we allocate a buffer which is certainly large enough */
1673 /* to hold all of the created bytecode instructions; */
1674 /* later on it gets reallocated to its real size */
1675 ins_len = hints->num_points * 1000;
1676 ins_buf = (FT_Byte*)malloc(ins_len);
1677 if (!ins_buf)
1678 return FT_Err_Out_Of_Memory;
1680 /* initialize array with an invalid bytecode */
1681 /* so that we can easily find the array length at reallocation time */
1682 memset(ins_buf, INS_A0, ins_len);
1684 /* handle composite glyph */
1685 if (font->loader->gloader->base.num_subglyphs)
1687 bufp = TA_font_build_subglyph_shifter(font, ins_buf);
1688 if (!bufp)
1690 error = FT_Err_Out_Of_Memory;
1691 goto Err;
1694 goto Done1;
1697 /* only scale the glyph if the dummy hinter has been used */
1698 if (font->loader->metrics->clazz == &ta_dummy_script_class)
1700 /* since `TA_init_recorder' hasn't been called yet, */
1701 /* we manually initialize the `glyph' field */
1702 recorder.glyph = glyph;
1704 bufp = TA_sfnt_build_glyph_scaler(sfnt, &recorder, ins_buf);
1705 if (!bufp)
1707 error = FT_Err_Out_Of_Memory;
1708 goto Err;
1711 goto Done1;
1714 error = TA_init_recorder(&recorder, font, glyph, hints);
1715 if (error)
1716 goto Err;
1718 /* loop over a large range of pixel sizes */
1719 /* to find hints records which get pushed onto the bytecode stack */
1721 #ifdef DEBUGGING
1723 int num_chars, i;
1726 num_chars = fprintf(stderr, "glyph %ld\n", idx);
1727 for (i = 0; i < num_chars - 1; i++)
1728 putc('=', stderr);
1729 fprintf(stderr, "\n\n");
1732 #endif
1734 /* we temporarily use `ins_buf' to record the current glyph hints */
1735 ta_loader_register_hints_recorder(font->loader,
1736 TA_hints_recorder,
1737 (void*)&recorder);
1739 num_action_hints_records = 0;
1740 num_point_hints_records = 0;
1741 action_hints_records = NULL;
1742 point_hints_records = NULL;
1744 for (size = font->hinting_range_min;
1745 size <= font->hinting_range_max;
1746 size++)
1748 #ifdef DEBUGGING
1749 int have_dumps = 0;
1750 #endif
1753 TA_rewind_recorder(&recorder, ins_buf, size);
1755 error = FT_Set_Pixel_Sizes(face, size, size);
1756 if (error)
1757 goto Err;
1759 /* calling `ta_loader_load_glyph' uses the */
1760 /* `TA_hints_recorder' function as a callback, */
1761 /* modifying `hints_record' */
1762 error = ta_loader_load_glyph(font->loader, face, idx, load_flags);
1763 if (error)
1764 goto Err;
1766 if (TA_hints_record_is_different(action_hints_records,
1767 num_action_hints_records,
1768 ins_buf, recorder.hints_record.buf))
1770 #ifdef DEBUGGING
1772 have_dumps = 1;
1774 fprintf(stderr, " size %d:\n", size);
1776 ta_glyph_hints_dump_edges(_ta_debug_hints);
1777 ta_glyph_hints_dump_segments(_ta_debug_hints);
1778 ta_glyph_hints_dump_points(_ta_debug_hints);
1780 fprintf(stderr, " action hints record:\n");
1781 for (p = ins_buf; p < recorder.hints_record.buf; p += 2)
1782 fprintf(stderr, " %2d", *p * 256 + *(p + 1));
1783 fprintf(stderr, "\n");
1785 #endif
1787 error = TA_add_hints_record(&action_hints_records,
1788 &num_action_hints_records,
1789 ins_buf, recorder.hints_record);
1790 if (error)
1791 goto Err;
1794 /* now handle point records */
1796 TA_reset_recorder(&recorder, ins_buf);
1798 /* use the point hints data collected in `TA_hints_recorder' */
1799 TA_build_point_hints(&recorder, hints);
1801 if (TA_hints_record_is_different(point_hints_records,
1802 num_point_hints_records,
1803 ins_buf, recorder.hints_record.buf))
1805 #ifdef DEBUGGING
1807 if (!have_dumps)
1809 fprintf(stderr, " size %d:\n", size);
1811 ta_glyph_hints_dump_edges(_ta_debug_hints);
1812 ta_glyph_hints_dump_segments(_ta_debug_hints);
1813 ta_glyph_hints_dump_points(_ta_debug_hints);
1816 fprintf(stderr, " point hints record:\n");
1817 for (p = ins_buf; p < recorder.hints_record.buf; p += 2)
1818 fprintf(stderr, " %2d", *p * 256 + *(p + 1));
1819 fprintf(stderr, "\n");
1821 #endif
1823 error = TA_add_hints_record(&point_hints_records,
1824 &num_point_hints_records,
1825 ins_buf, recorder.hints_record);
1826 if (error)
1827 goto Err;
1831 if (num_action_hints_records == 1 && !action_hints_records[0].num_actions)
1833 /* since we only have a single empty record we just scale the glyph */
1834 bufp = TA_sfnt_build_glyph_scaler(sfnt, &recorder, ins_buf);
1835 if (!bufp)
1837 error = FT_Err_Out_Of_Memory;
1838 goto Err;
1841 /* clear the rest of the temporarily used part of `ins_buf' */
1842 p = bufp;
1843 while (*p != INS_A0)
1844 *(p++) = INS_A0;
1846 goto Done;
1849 /* store the hints records and handle stack depth */
1850 bufp = TA_emit_hints_records(&recorder,
1851 point_hints_records,
1852 num_point_hints_records,
1853 ins_buf);
1854 num_stack_elements = recorder.num_stack_elements;
1855 recorder.num_stack_elements = 0;
1856 bufp = TA_emit_hints_records(&recorder,
1857 action_hints_records,
1858 num_action_hints_records,
1859 bufp);
1860 recorder.num_stack_elements += num_stack_elements;
1862 bufp = TA_sfnt_build_glyph_segments(sfnt, &recorder, bufp);
1863 if (!bufp)
1865 error = FT_Err_Out_Of_Memory;
1866 goto Err;
1869 /* XXX do nothing if we have NPUSHW in the data */
1870 if (num_action_hints_records == 1
1871 && !memchr(ins_buf, NPUSHW, bufp - ins_buf))
1874 * we optimize two common cases, replacing
1876 * NPUSHB A ... NPUSHB B [... NPUSHB C] ... CALL
1878 * with
1880 * NPUSHB (A+B[+C]) ... CALL
1882 * if possible
1884 FT_Byte* pos[3];
1885 FT_Byte sizes[3];
1886 FT_Byte new_size1;
1887 FT_Byte new_size2;
1889 FT_UInt sum;
1890 FT_UInt i;
1891 FT_UInt pos_idx;
1894 /* there are at least two NPUSHB instructions */
1895 /* (one of them directly at the start) */
1896 pos[0] = ins_buf;
1897 sizes[0] = *(pos[0] + 1);
1898 pos[1] = (FT_Byte*)memchr(pos[0] + 1 + sizes[0], NPUSHB, bufp - ins_buf);
1899 sizes[1] = *(pos[1] + 1);
1900 pos[2] = (FT_Byte*)memchr(pos[1] + 1 + sizes[1], NPUSHB, bufp - ins_buf);
1901 sizes[2] = pos[2] ? *(pos[2] + 1) : 0;
1903 sum = sizes[0] + sizes[1] + sizes[2];
1905 if (sum > 2 * 0xFF)
1906 goto Done2; /* nothing to do since we need three NPUSHB */
1907 else if (!sizes[2] && (sum > 0xFF))
1908 goto Done2; /* nothing to do since we need two NPUSHB */
1910 if (sum > 0xFF)
1912 /* reduce three NPUSHB to two */
1913 new_size1 = 0xFF;
1914 new_size2 = sum - 0xFF;
1916 else
1918 /* reduce two or three NPUSHB to one */
1919 new_size1 = sum;
1920 new_size2 = 0;
1923 /* pack data */
1924 p = ins_buf;
1925 bufp = ins_buf;
1926 pos_idx = 0;
1928 BCI(NPUSHB);
1929 BCI(new_size1);
1930 for (i = 0; i < new_size1; i++)
1932 if (p == pos[pos_idx])
1934 pos_idx++;
1935 p += 2; /* skip old NPUSHB */
1937 *(bufp++) = *(p++);
1940 if (new_size2)
1942 BCI(NPUSHB);
1943 BCI(new_size2);
1944 for (i = 0; i < new_size2; i++)
1946 if (p == pos[pos_idx])
1948 pos_idx++;
1949 p += 2;
1951 *(bufp++) = *(p++);
1955 BCI(CALL);
1958 Done2:
1959 /* clear the rest of the temporarily used part of `ins_buf' */
1960 p = bufp;
1961 while (*p != INS_A0)
1962 *(p++) = INS_A0;
1964 Done:
1965 TA_free_hints_records(action_hints_records, num_action_hints_records);
1966 TA_free_recorder(&recorder);
1968 /* we are done, so reallocate the instruction array to its real size */
1969 if (*bufp == INS_A0)
1971 /* search backwards */
1972 while (*bufp == INS_A0)
1973 bufp--;
1974 bufp++;
1976 else
1978 /* search forwards */
1979 while (*bufp != INS_A0)
1980 bufp++;
1983 Done1:
1984 ins_len = bufp - ins_buf;
1986 if (ins_len > sfnt->max_instructions)
1987 sfnt->max_instructions = ins_len;
1989 glyph->ins_buf = (FT_Byte*)realloc(ins_buf, ins_len);
1990 glyph->ins_len = ins_len;
1992 return FT_Err_Ok;
1994 Err:
1995 TA_free_hints_records(action_hints_records, num_action_hints_records);
1996 TA_free_recorder(&recorder);
1997 free(ins_buf);
1999 return error;
2003 /* end of tabytecode.c */