autotroll.m4: Clean up Qt path stuff.
[ttfautohint.git] / lib / taglyf.c
blobf41fe17553478a6ef45f825e47f5b58abc624b1f
1 /* taglyf.c */
3 /*
4 * Copyright (C) 2011-2017 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"
19 static FT_Error
20 TA_sfnt_build_glyf_hints(SFNT* sfnt,
21 FONT* font)
23 SFNT_Table* glyf_table = &font->tables[sfnt->glyf_idx];
24 glyf_Data* data = (glyf_Data*)glyf_table->data;
26 FT_Long idx;
27 FT_Error error;
29 FT_UShort loop_count;
32 /* this loop doesn't include the artificial `.ttfautohint' glyph */
33 loop_count = data->num_glyphs;
34 if (sfnt->max_components && font->hint_composites)
35 loop_count--;
37 for (idx = 0; idx < loop_count; idx++)
39 error = TA_sfnt_build_glyph_instructions(sfnt, font, idx);
40 if (error)
41 return error;
42 if (font->progress)
44 FT_Int ret;
47 ret = font->progress(idx, loop_count,
48 sfnt - font->sfnts, font->num_sfnts,
49 font->progress_data);
50 if (ret)
51 return TA_Err_Canceled;
55 return FT_Err_Ok;
59 static FT_Error
60 TA_glyph_get_components(GLYPH* glyph,
61 FT_Byte* buf,
62 FT_ULong len)
64 FT_UShort flags;
65 FT_UShort component;
66 FT_UShort* components_new;
68 FT_Byte* p;
69 FT_Byte* endp;
72 p = buf;
73 endp = buf + len;
75 /* skip header */
76 p += 10;
78 /* walk over component records */
81 if (p + 4 > endp)
82 return FT_Err_Invalid_Table;
84 flags = NEXT_USHORT(p);
86 /* add component to list */
87 component = NEXT_USHORT(p);
89 glyph->num_components++;
90 components_new = (FT_UShort*)realloc(glyph->components,
91 glyph->num_components
92 * sizeof (FT_UShort));
93 if (!components_new)
95 glyph->num_components--;
96 return FT_Err_Out_Of_Memory;
98 else
99 glyph->components = components_new;
101 glyph->components[glyph->num_components - 1] = component;
103 /* skip scaling and offset arguments */
104 if (flags & ARGS_ARE_WORDS)
105 p += 4;
106 else
107 p += 2;
109 if (flags & WE_HAVE_A_SCALE)
110 p += 2;
111 else if (flags & WE_HAVE_AN_XY_SCALE)
112 p += 4;
113 else if (flags & WE_HAVE_A_2X2)
114 p += 8;
115 } while (flags & MORE_COMPONENTS);
117 return TA_Err_Ok;
121 static FT_Error
122 TA_glyph_parse_composite(GLYPH* glyphs,
123 FT_UShort idx,
124 FT_Byte* buf,
125 FT_ULong len,
126 FT_UShort num_glyphs,
127 FT_Bool hint_composites)
129 GLYPH* glyph = &glyphs[idx];
131 FT_ULong flags_offset; /* after the loop, this is the offset */
132 /* to the last element in the flags array */
133 FT_UShort flags;
135 FT_Byte* p;
136 FT_Byte* q;
138 FT_UShort* curr_component;
139 FT_UShort curr_num_points;
142 /* we allocate too large a buffer */
143 /* (including space for the new component */
144 /* and possible argument size changes for shifted point indices) */
145 /* and reallocate it later to its real size */
146 glyph->buf = (FT_Byte*)malloc(len + 8 + glyph->num_components * 2);
147 if (!glyph->buf)
148 return FT_Err_Out_Of_Memory;
150 p = buf;
151 q = glyph->buf;
153 /* copy header */
154 memcpy(q, p, 10);
155 p += 10;
156 q += 10;
158 /* if the composite glyph contains one or more contours, */
159 /* we prepend a composite glyph component to call some bytecode */
160 /* which eventually becomes the last glyph in the `glyf' table; */
161 /* for convenience, however, it is not added to the `components' array */
162 /* (doing so simplifies the conversion of point indices later on) */
163 if (glyph->num_composite_contours && hint_composites)
165 FT_Short x_min;
166 FT_Short x_max;
167 FT_Short y_min;
168 FT_Short y_max;
169 FT_Short x_offset;
170 FT_Short y_offset;
173 /* the composite glyph's bounding box */
174 x_min = (FT_Short)((buf[2] << 8) + buf[3]);
175 y_min = (FT_Short)((buf[4] << 8) + buf[5]);
176 x_max = (FT_Short)((buf[6] << 8) + buf[7]);
177 y_max = (FT_Short)((buf[8] << 8) + buf[9]);
179 /* use ARGS_ARE_WORDS only if necessary; */
180 /* note that the offset value of the component doesn't matter */
181 /* as long as it stays within the bounding box */
182 if (x_min <= 0 && x_max >= 0)
183 x_offset = 0;
184 else if (x_max < 0)
185 x_offset = x_max;
186 else
187 x_offset = x_min;
189 if (y_min <= 0 && y_max >= 0)
190 y_offset = 0;
191 else if (y_max < 0)
192 y_offset = y_max;
193 else
194 y_offset = y_min;
196 if (x_offset >= -128 && x_offset <= 127
197 && y_offset >= -128 && y_offset <= 127)
199 *(q++) = 0x00;
200 *(q++) = ARGS_ARE_XY_VALUES | MORE_COMPONENTS;
201 *(q++) = HIGH(num_glyphs - 1);
202 *(q++) = LOW(num_glyphs - 1);
203 *(q++) = (FT_Byte)x_offset;
204 *(q++) = (FT_Byte)y_offset;
206 else
208 *(q++) = 0x00;
209 *(q++) = ARGS_ARE_WORDS | ARGS_ARE_XY_VALUES | MORE_COMPONENTS;
210 *(q++) = HIGH(num_glyphs - 1);
211 *(q++) = LOW(num_glyphs - 1);
212 *(q++) = HIGH(x_offset);
213 *(q++) = LOW(x_offset);
214 *(q++) = HIGH(y_offset);
215 *(q++) = LOW(y_offset);
219 curr_component = glyph->components;
220 curr_num_points = 0;
222 /* walk over component records */
225 flags_offset = (FT_ULong)(q - glyph->buf);
227 *(q++) = *p;
228 *(q++) = *(p + 1);
229 flags = NEXT_USHORT(p);
231 /* copy component */
232 *(q++) = *(p++);
233 *(q++) = *(p++);
235 if (flags & ARGS_ARE_XY_VALUES)
237 /* copy offsets */
238 *(q++) = *(p++);
239 *(q++) = *(p++);
241 if (flags & ARGS_ARE_WORDS)
243 *(q++) = *(p++);
244 *(q++) = *(p++);
247 else
249 /* handle point numbers */
250 FT_UShort arg1;
251 FT_UShort arg2;
252 FT_UShort idx2;
253 FT_UShort i;
254 FT_UShort j;
257 if (flags & ARGS_ARE_WORDS)
259 arg1 = *(p++) >> 8;
260 arg1 += *(p++);
261 arg2 = *(p++) >> 8;
262 arg2 += *(p++);
264 else
266 arg1 = *(p++);
267 arg2 = *(p++);
270 curr_num_points += glyphs[*curr_component++].num_points;
272 /* compute the point index of arg2 after adding the subglyph */
273 idx2 = curr_num_points + arg2;
275 /* adjust point indices */
276 /* (see `TA_adjust_point_index' in `tabytecode.c' for more) */
277 for (i = 0; i < glyph->num_pointsums; i++)
278 if (arg1 < glyph->pointsums[i])
279 break;
280 arg1 += i;
282 for (j = 0; j < glyph->num_pointsums; j++)
283 if (idx2 < glyph->pointsums[j])
284 break;
285 arg2 += j - i;
287 if (arg1 <= 0xFF && arg2 <= 0xFF)
289 glyph->buf[flags_offset + 1] &= ~ARGS_ARE_WORDS;
291 *(q++) = (FT_Byte)arg1;
292 *(q++) = (FT_Byte)arg2;
294 else
296 glyph->buf[flags_offset + 1] |= ARGS_ARE_WORDS;
298 *(q++) = HIGH(arg1);
299 *(q++) = LOW(arg1);
300 *(q++) = HIGH(arg2);
301 *(q++) = LOW(arg2);
305 /* copy scaling arguments */
306 if (flags & (WE_HAVE_A_SCALE | WE_HAVE_AN_XY_SCALE | WE_HAVE_A_2X2))
308 *(q++) = *(p++);
309 *(q++) = *(p++);
311 if (flags & (WE_HAVE_AN_XY_SCALE | WE_HAVE_A_2X2))
313 *(q++) = *(p++);
314 *(q++) = *(p++);
316 if (flags & WE_HAVE_A_2X2)
318 *(q++) = *(p++);
319 *(q++) = *(p++);
320 *(q++) = *(p++);
321 *(q++) = *(p++);
323 } while (flags & MORE_COMPONENTS);
325 glyph->len1 = (FT_ULong)(q - glyph->buf);
326 /* glyph->len2 = 0; */
327 glyph->flags_offset = flags_offset;
328 glyph->buf = (FT_Byte*)realloc(glyph->buf, glyph->len1);
330 /* we discard instructions (if any) */
331 glyph->buf[glyph->flags_offset] &= ~(WE_HAVE_INSTR >> 8);
333 return TA_Err_Ok;
337 static FT_Error
338 TA_glyph_parse_simple(GLYPH* glyph,
339 FT_Byte* buf,
340 FT_ULong len,
341 FT_Bool dehint)
343 FT_ULong ins_offset;
344 FT_Byte* flags_start;
346 FT_UShort num_ins;
348 FT_ULong flags_size; /* size of the flags array */
349 FT_ULong xy_size; /* size of x and y coordinate arrays together */
351 FT_Byte* p;
352 FT_Byte* endp;
354 FT_UShort i;
357 p = buf;
358 endp = buf + len;
360 ins_offset = 10 + (FT_ULong)glyph->num_contours * 2;
362 p += ins_offset;
364 if (p + 2 > endp)
365 return FT_Err_Invalid_Table;
367 /* get number of instructions */
368 num_ins = NEXT_USHORT(p);
370 /* assure that we don't process a font */
371 /* which already contains a `.ttfautohint' glyph */
372 /* (a font with a `post' table version 3.0 doesn't contain glyph names, */
373 /* so we have to check it this way) */
374 if (glyph->num_points == 1
375 && num_ins >= sizeof (ttfautohint_glyph_bytecode)
376 && !dehint)
378 if (!strncmp((char*)p, (char*)ttfautohint_glyph_bytecode,
379 sizeof (ttfautohint_glyph_bytecode)))
380 return TA_Err_Already_Processed;
383 p += num_ins;
385 if (p > endp)
386 return FT_Err_Invalid_Table;
388 flags_start = p;
389 xy_size = 0;
390 i = 0;
392 while (i < glyph->num_points)
394 FT_Byte flags;
395 FT_Byte x_short;
396 FT_Byte y_short;
397 FT_Byte have_x;
398 FT_Byte have_y;
399 FT_UInt count;
402 if (p + 1 > endp)
403 return FT_Err_Invalid_Table;
405 flags = *(p++);
407 x_short = (flags & X_SHORT_VECTOR) ? 1 : 2;
408 y_short = (flags & Y_SHORT_VECTOR) ? 1 : 2;
410 have_x = ((flags & SAME_X) && !(flags & X_SHORT_VECTOR)) ? 0 : 1;
411 have_y = ((flags & SAME_Y) && !(flags & Y_SHORT_VECTOR)) ? 0 : 1;
413 count = 1;
415 if (flags & REPEAT)
417 if (p + 1 > endp)
418 return FT_Err_Invalid_Table;
420 count += *(p++);
422 if (i + count > glyph->num_points)
423 return FT_Err_Invalid_Table;
426 xy_size += count * x_short * have_x;
427 xy_size += count * y_short * have_y;
429 i += count;
432 if (p + xy_size > endp)
433 return FT_Err_Invalid_Table;
435 flags_size = (FT_ULong)(p - flags_start);
437 /* store the data before and after the bytecode instructions */
438 /* in the same array */
439 glyph->len1 = ins_offset;
440 glyph->len2 = flags_size + xy_size;
441 glyph->buf = (FT_Byte*)malloc(glyph->len1 + glyph->len2);
442 if (!glyph->buf)
443 return FT_Err_Out_Of_Memory;
445 /* now copy everything but the instructions */
446 memcpy(glyph->buf, buf, glyph->len1);
447 memcpy(glyph->buf + glyph->len1, flags_start, glyph->len2);
449 return TA_Err_Ok;
453 static FT_Error
454 TA_iterate_composite_glyph(glyf_Data* data,
455 FT_UShort* components,
456 FT_UShort num_components,
457 FT_UShort** pointsums,
458 FT_UShort* num_pointsums,
459 FT_UShort* num_composite_contours,
460 FT_UShort* num_composite_points)
462 FT_UShort* pointsums_new;
463 FT_UShort i;
466 /* save current state */
468 if (*num_pointsums == 0xFFFF)
469 return FT_Err_Invalid_Table;
471 (*num_pointsums)++;
472 pointsums_new = (FT_UShort*)realloc(*pointsums,
473 *num_pointsums
474 * sizeof (FT_UShort));
475 if (!pointsums_new)
477 (*num_pointsums)--;
478 return FT_Err_Out_Of_Memory;
480 else
481 *pointsums = pointsums_new;
483 (*pointsums)[*num_pointsums - 1] = *num_composite_points;
485 for (i = 0; i < num_components; i++)
487 GLYPH* glyph;
488 FT_UShort component = components[i];
489 FT_Error error;
492 if (component >= data->num_glyphs)
493 return FT_Err_Invalid_Table;
495 glyph = &data->glyphs[component];
497 if (glyph->num_components)
499 error = TA_iterate_composite_glyph(data,
500 glyph->components,
501 glyph->num_components,
502 pointsums,
503 num_pointsums,
504 num_composite_contours,
505 num_composite_points);
506 if (error)
507 return error;
509 else
511 /* no need for checking overflow of the number of contours */
512 /* since the number of points is always larger or equal */
513 if (*num_composite_points > 0xFFFF - glyph->num_points)
514 return FT_Err_Invalid_Table;
516 *num_composite_contours += glyph->num_contours;
517 *num_composite_points += glyph->num_points;
521 return TA_Err_Ok;
525 static FT_Error
526 TA_sfnt_compute_composite_pointsums(SFNT* sfnt,
527 FONT* font)
529 SFNT_Table* glyf_table = &font->tables[sfnt->glyf_idx];
530 glyf_Data* data = (glyf_Data*)glyf_table->data;
532 FT_UShort i;
535 for (i = 0; i < data->num_glyphs; i++)
537 GLYPH* glyph = &data->glyphs[i];
540 if (glyph->num_components)
542 FT_Error error;
543 FT_UShort num_composite_contours = 0;
544 FT_UShort num_composite_points = 0;
547 error = TA_iterate_composite_glyph(data,
548 glyph->components,
549 glyph->num_components,
550 &glyph->pointsums,
551 &glyph->num_pointsums,
552 &num_composite_contours,
553 &num_composite_points);
554 if (error)
555 return error;
557 glyph->num_composite_contours = num_composite_contours;
558 /* we set the number of points (after expanding all subglyphs) */
559 /* for composite glyphs also */
560 glyph->num_points = num_composite_points;
562 if (font->hint_composites)
564 /* update maximum values, */
565 /* including the subglyphs not in `components' array */
566 /* (each of them has a single point in a single contour) */
567 if (num_composite_points + glyph->num_pointsums
568 > sfnt->max_composite_points)
569 sfnt->max_composite_points = num_composite_points
570 + glyph->num_pointsums;
571 if (num_composite_contours + glyph->num_pointsums
572 > sfnt->max_composite_contours)
573 sfnt->max_composite_contours = num_composite_contours
574 + glyph->num_pointsums;
579 return TA_Err_Ok;
583 FT_Error
584 TA_sfnt_split_glyf_table(SFNT* sfnt,
585 FONT* font)
587 SFNT_Table* glyf_table = &font->tables[sfnt->glyf_idx];
588 SFNT_Table* loca_table = &font->tables[sfnt->loca_idx];
589 SFNT_Table* head_table = &font->tables[sfnt->head_idx];
591 glyf_Data* data;
592 FT_Byte loca_format;
594 FT_ULong offset;
595 FT_ULong offset_next;
597 FT_Byte* p;
598 FT_UShort i;
599 FT_UShort loop_count;
601 FT_Error error;
604 /* in case of success, all allocated arrays are */
605 /* linked and eventually freed in `TA_font_unload' */
607 /* nothing to do if table has already been split */
608 if (glyf_table->data)
609 return TA_Err_Ok;
611 data = (glyf_Data*)calloc(1, sizeof (glyf_Data));
612 if (!data)
613 return FT_Err_Out_Of_Memory;
615 glyf_table->data = data;
617 loca_format = head_table->buf[LOCA_FORMAT_OFFSET];
619 data->num_glyphs = (FT_UShort)(loca_format ? loca_table->len / 4
620 : loca_table->len / 2);
621 loop_count = data->num_glyphs - 1;
623 /* allocate one more glyph slot if we have composite glyphs */
624 if (!sfnt->max_components || !font->hint_composites)
625 data->num_glyphs -= 1;
626 data->glyphs = (GLYPH*)calloc(1, data->num_glyphs * sizeof (GLYPH));
627 if (!data->glyphs)
628 return FT_Err_Out_Of_Memory;
630 data->master_globals = NULL;
631 data->cvt_idx = MISSING;
632 data->fpgm_idx = MISSING;
633 data->prep_idx = MISSING;
635 /* first loop over `loca' and `glyf' data */
637 p = loca_table->buf;
639 if (loca_format)
640 offset_next = NEXT_ULONG(p);
641 else
643 offset_next = NEXT_USHORT(p);
644 offset_next <<= 1;
647 for (i = 0; i < loop_count; i++)
649 GLYPH* glyph = &data->glyphs[i];
650 FT_ULong len;
653 offset = offset_next;
655 if (loca_format)
656 offset_next = NEXT_ULONG(p);
657 else
659 offset_next = NEXT_USHORT(p);
660 offset_next <<= 1;
663 if (offset_next < offset
664 || offset_next > glyf_table->len)
665 return FT_Err_Invalid_Table;
667 len = offset_next - offset;
668 if (!len)
669 continue; /* empty glyph */
670 else
672 FT_Byte* buf;
675 /* check header size */
676 if (len < 10)
677 return FT_Err_Invalid_Table;
679 /* we need the number of contours and points for */
680 /* `TA_sfnt_compute_composite_pointsums' */
681 buf = glyf_table->buf + offset;
682 glyph->num_contours = (FT_Short)((buf[0] << 8) + buf[1]);
684 if (glyph->num_contours < 0)
686 error = TA_glyph_get_components(glyph, buf, len);
687 if (error)
688 return error;
690 else
692 FT_ULong off;
695 /* use the last contour's end point to compute number of points */
696 off = 10 + ((FT_ULong)glyph->num_contours - 1) * 2;
697 if (off >= len - 1)
698 return FT_Err_Invalid_Table;
700 glyph->num_points = (FT_UShort)((buf[off] << 8) + buf[off + 1] + 1);
705 if (sfnt->max_components && font->hint_composites)
707 error = TA_sfnt_compute_composite_pointsums(sfnt, font);
708 if (error)
709 return error;
712 /* second loop over `loca' and `glyf' data */
714 p = loca_table->buf;
716 if (loca_format)
717 offset_next = NEXT_ULONG(p);
718 else
720 offset_next = NEXT_USHORT(p);
721 offset_next <<= 1;
724 for (i = 0; i < loop_count; i++)
726 GLYPH* glyph = &data->glyphs[i];
727 FT_ULong len;
730 offset = offset_next;
732 if (loca_format)
733 offset_next = NEXT_ULONG(p);
734 else
736 offset_next = NEXT_USHORT(p);
737 offset_next <<= 1;
740 len = offset_next - offset;
741 if (!len)
742 continue; /* empty glyph */
743 else
745 FT_Byte* buf;
748 buf = glyf_table->buf + offset;
750 /* We must parse the rest of the glyph record to get the exact */
751 /* record length. Since the `loca' table rounds record lengths */
752 /* up to multiples of 4 (or 2 for older fonts), and we must round */
753 /* up again after stripping off the instructions, it would be */
754 /* possible otherwise to have more than 4 bytes of padding which */
755 /* is more or less invalid. */
757 if (glyph->num_contours < 0)
758 error = TA_glyph_parse_composite(data->glyphs, i, buf, len,
759 data->num_glyphs,
760 font->hint_composites);
761 else
762 error = TA_glyph_parse_simple(glyph, buf, len, font->dehint);
763 if (error)
764 return error;
768 if (sfnt->max_components && font->hint_composites)
770 /* construct and append our special glyph used as a composite element */
771 GLYPH* glyph = &data->glyphs[data->num_glyphs - 1];
772 FT_Byte* buf;
774 glyph->len1 = 12;
775 glyph->len2 = 1;
776 glyph->buf = (FT_Byte*)malloc(glyph->len1 + glyph->len2);
777 if (!glyph->buf)
778 return FT_Err_Out_Of_Memory;
780 buf = glyph->buf;
782 buf[0] = 0x00; /* one contour */
783 buf[1] = 0x01;
784 buf[2] = 0x00; /* no dimensions */
785 buf[3] = 0x00;
786 buf[4] = 0x00;
787 buf[5] = 0x00;
788 buf[6] = 0x00;
789 buf[7] = 0x00;
790 buf[8] = 0x00;
791 buf[9] = 0x00;
792 buf[10] = 0x00; /* one contour end point */
793 buf[11] = 0x00;
795 buf[12] = ON_CURVE | SAME_X | SAME_Y; /* the flags for a point at 0,0 */
797 /* add bytecode also; */
798 /* this works because the loop in `TA_sfnt_build_glyf_hints' */
799 /* doesn't include the newly appended glyph */
800 glyph->ins_len = sizeof (ttfautohint_glyph_bytecode);
801 glyph->ins_buf = (FT_Byte*)malloc(glyph->ins_len);
802 if (!glyph->ins_buf)
803 return FT_Err_Out_Of_Memory;
804 memcpy(glyph->ins_buf, ttfautohint_glyph_bytecode, glyph->ins_len);
806 sfnt->max_components += 1;
809 return TA_Err_Ok;
813 FT_Error
814 TA_sfnt_build_glyf_table(SFNT* sfnt,
815 FONT* font)
817 FT_Error error;
819 SFNT_Table* glyf_table = &font->tables[sfnt->glyf_idx];
820 glyf_Data* data = (glyf_Data*)glyf_table->data;
822 GLYPH* glyph;
824 FT_ULong len;
825 FT_Byte* buf_new;
826 FT_Byte* p;
827 FT_UShort i;
830 if (glyf_table->processed)
831 return TA_Err_Ok;
833 if (!font->dehint)
835 error = TA_sfnt_build_glyf_hints(sfnt, font);
836 if (error)
837 return error;
840 /* get table size */
841 len = 0;
842 glyph = data->glyphs;
843 for (i = 0; i < data->num_glyphs; i++, glyph++)
845 /* glyph records should have offsets which are multiples of 4 */
846 len = (len + 3) & ~3U;
847 len += glyph->len1 + glyph->len2
848 + glyph->ins_extra_len + glyph->ins_len;
849 /* add two bytes for the instructionLength field */
850 if (glyph->len2 || glyph->ins_len)
851 len += 2;
854 /* to make the short format of the `loca' table always work, */
855 /* assure an even length of the `glyf' table */
856 glyf_table->len = (len + 1) & ~1U;
858 buf_new = (FT_Byte*)realloc(glyf_table->buf, (len + 3) & ~3U);
859 if (!buf_new)
860 return FT_Err_Out_Of_Memory;
861 else
862 glyf_table->buf = buf_new;
864 p = glyf_table->buf;
865 glyph = data->glyphs;
866 for (i = 0; i < data->num_glyphs; i++, glyph++)
868 len = glyph->len1 + glyph->len2
869 + glyph->ins_extra_len + glyph->ins_len;
870 if (glyph->len2 || glyph->ins_len)
871 len += 2;
873 if (len)
875 /* copy glyph data and insert new instructions */
876 memcpy(p, glyph->buf, glyph->len1);
878 if (glyph->len2)
880 /* simple glyph */
881 p += glyph->len1;
882 *(p++) = HIGH(glyph->ins_extra_len + glyph->ins_len);
883 *(p++) = LOW(glyph->ins_extra_len + glyph->ins_len);
884 if (glyph->ins_extra_len)
886 memcpy(p, glyph->ins_extra_buf, glyph->ins_extra_len);
887 p += glyph->ins_extra_len;
889 if (glyph->ins_len)
891 memcpy(p, glyph->ins_buf, glyph->ins_len);
892 p += glyph->ins_len;
894 memcpy(p, glyph->buf + glyph->len1, glyph->len2);
895 p += glyph->len2;
897 else
899 /* composite glyph */
900 if (glyph->ins_len)
902 *(p + glyph->flags_offset) |= (WE_HAVE_INSTR >> 8);
903 p += glyph->len1;
904 *(p++) = HIGH(glyph->ins_extra_len + glyph->ins_len);
905 *(p++) = LOW(glyph->ins_extra_len + glyph->ins_len);
906 if (glyph->ins_extra_len)
908 memcpy(p, glyph->ins_extra_buf, glyph->ins_extra_len);
909 p += glyph->ins_extra_len;
911 memcpy(p, glyph->ins_buf, glyph->ins_len);
912 p += glyph->ins_len;
914 else
915 p += glyph->len1;
918 /* pad with zero bytes to have an offset which is a multiple of 4; */
919 /* this works even for the last glyph record since the `glyf' */
920 /* table length is a multiple of 4 also */
921 switch (len % 4)
923 case 1:
924 *(p++) = 0;
925 case 2:
926 *(p++) = 0;
927 case 3:
928 *(p++) = 0;
929 default:
930 break;
935 glyf_table->checksum = TA_table_compute_checksum(glyf_table->buf,
936 glyf_table->len);
937 glyf_table->processed = 1;
939 return TA_Err_Ok;
943 static FT_Error
944 TA_create_glyph_data(FT_Outline* outline,
945 GLYPH* glyph)
947 FT_Error error = TA_Err_Ok;
949 FT_Pos xmin, ymin;
950 FT_Pos xmax, ymax;
952 FT_Byte header[10];
953 FT_Byte* flags = NULL;
954 FT_Byte* flagsp;
955 FT_Byte oldf, f;
956 FT_Byte* x = NULL;
957 FT_Byte* xp;
958 FT_Byte* y = NULL;
959 FT_Byte* yp;
961 FT_Pos lastx, lasty;
963 FT_Short i;
964 FT_Byte* p;
967 if (!outline->n_contours)
968 return TA_Err_Ok; /* empty glyph */
970 /* in case of success, all non-local allocated arrays are */
971 /* linked and eventually freed in `TA_font_unload' */
973 glyph->buf = NULL;
975 /* we use `calloc' since we rely on the array */
976 /* being initialized to zero; */
977 /* additionally, we need one more byte for a test after the loop */
978 flags = (FT_Byte*)calloc(1, (size_t)outline->n_points + 1);
979 if (!flags)
981 error = FT_Err_Out_Of_Memory;
982 goto Exit;
985 /* we have either one-byte or two-byte elements */
986 x = (FT_Byte*)malloc(2 * (size_t)outline->n_points);
987 if (!x)
989 error = FT_Err_Out_Of_Memory;
990 goto Exit;
993 y = (FT_Byte*)malloc(2 * (size_t)outline->n_points);
994 if (!y)
996 error = FT_Err_Out_Of_Memory;
997 goto Exit;
1000 flagsp = flags;
1001 xp = x;
1002 yp = y;
1003 xmin = xmax = (outline->points[0].x + 32) >> 6;
1004 ymin = ymax = (outline->points[0].y + 32) >> 6;
1005 lastx = 0;
1006 lasty = 0;
1007 oldf = 0x80; /* start with an impossible value */
1009 /* convert the FreeType representation of the glyph's outline */
1010 /* into the representation format of the `glyf' table */
1011 for (i = 0; i < outline->n_points; i++)
1013 FT_Pos xcur = (outline->points[i].x + 32) >> 6;
1014 FT_Pos ycur = (outline->points[i].y + 32) >> 6;
1016 FT_Pos xdelta = xcur - lastx;
1017 FT_Pos ydelta = ycur - lasty;
1020 /* we are only interested in bit 0 of the `tags' array */
1021 f = outline->tags[i] & ON_CURVE;
1023 /* x value */
1025 if (xdelta == 0)
1026 f |= SAME_X;
1027 else
1029 if (xdelta < 256 && xdelta > -256)
1031 f |= X_SHORT_VECTOR;
1033 if (xdelta < 0)
1034 xdelta = -xdelta;
1035 else
1036 f |= SAME_X;
1038 *(xp++) = (FT_Byte)xdelta;
1040 else
1042 *(xp++) = HIGH(xdelta);
1043 *(xp++) = LOW(xdelta);
1047 /* y value */
1049 if (ydelta == 0)
1050 f |= SAME_Y;
1051 else
1053 if (ydelta < 256 && ydelta > -256)
1055 f |= Y_SHORT_VECTOR;
1057 if (ydelta < 0)
1058 ydelta = -ydelta;
1059 else
1060 f |= SAME_Y;
1062 *(yp++) = (FT_Byte)ydelta;
1064 else
1066 *(yp++) = HIGH(ydelta);
1067 *(yp++) = LOW(ydelta);
1071 if (f == oldf)
1073 /* set repeat flag */
1074 *(flagsp - 1) |= REPEAT;
1076 if (*flagsp == 255)
1078 /* we can only handle 256 repetitions at once, */
1079 /* so use a new counter */
1080 flagsp++;
1081 *(flagsp++) = f;
1083 else
1084 *flagsp += 1; /* increase repetition counter */
1086 else
1088 if (*flagsp)
1089 flagsp++; /* skip repetition counter */
1090 *(flagsp++) = f;
1091 oldf = f;
1094 if (xcur > xmax)
1095 xmax = xcur;
1096 if (ycur > ymax)
1097 ymax = ycur;
1098 if (xcur < xmin)
1099 xmin = xcur;
1100 if (ycur < ymin)
1101 ymin = ycur;
1103 lastx = xcur;
1104 lasty = ycur;
1107 /* if the last byte was a repetition counter, */
1108 /* we must increase by one to get the correct array size */
1109 if (*flagsp)
1110 flagsp++;
1112 header[0] = HIGH(outline->n_contours);
1113 header[1] = LOW(outline->n_contours);
1114 header[2] = HIGH(xmin);
1115 header[3] = LOW(xmin);
1116 header[4] = HIGH(ymin);
1117 header[5] = LOW(ymin);
1118 header[6] = HIGH(xmax);
1119 header[7] = LOW(xmax);
1120 header[8] = HIGH(ymax);
1121 header[9] = LOW(ymax);
1123 /* concatenate all arrays and fill needed GLYPH structure elements */
1125 glyph->len1 = (FT_ULong)(10 + 2 * outline->n_contours);
1126 glyph->len2 = (FT_ULong)((flagsp - flags) + (xp - x) + (yp - y));
1128 glyph->buf = (FT_Byte*)malloc(glyph->len1 + glyph->len2);
1129 if (!glyph->buf)
1131 error = FT_Err_Out_Of_Memory;
1132 goto Exit;
1135 p = glyph->buf;
1136 memcpy(p, header, 10);
1137 p += 10;
1139 glyph->ins_extra_len = 0;
1140 glyph->ins_extra_buf = NULL;
1141 glyph->ins_len = 0;
1142 glyph->ins_buf = NULL;
1144 for (i = 0; i < outline->n_contours; i++)
1146 *(p++) = HIGH(outline->contours[i]);
1147 *(p++) = LOW(outline->contours[i]);
1150 memcpy(p, flags, (size_t)(flagsp - flags));
1151 p += flagsp - flags;
1152 memcpy(p, x, (size_t)(xp - x));
1153 p += xp - x;
1154 memcpy(p, y, (size_t)(yp - y));
1156 Exit:
1157 free(flags);
1158 free(x);
1159 free(y);
1161 return error;
1165 /* We hint each glyph at EM size and construct a new `glyf' table. */
1166 /* Some fonts need this; in particular, */
1167 /* there are CJK fonts which use hints to scale and position subglyphs. */
1168 /* As a consequence, there are no longer composite glyphs. */
1170 FT_Error
1171 TA_sfnt_create_glyf_data(SFNT* sfnt,
1172 FONT* font)
1174 SFNT_Table* glyf_table = &font->tables[sfnt->glyf_idx];
1175 FT_Face face = sfnt->face;
1176 FT_Error error;
1178 glyf_Data* data;
1180 FT_UShort i;
1183 /* in case of success, all allocated arrays are */
1184 /* linked and eventually freed in `TA_font_unload' */
1186 /* nothing to do if table has already been created */
1187 if (glyf_table->data)
1188 return TA_Err_Ok;
1190 data = (glyf_Data*)calloc(1, sizeof (glyf_Data));
1191 if (!data)
1192 return FT_Err_Out_Of_Memory;
1194 glyf_table->data = data;
1196 data->num_glyphs = (FT_UShort)face->num_glyphs;
1197 data->glyphs = (GLYPH*)calloc(1, data->num_glyphs * sizeof (GLYPH));
1198 if (!data->glyphs)
1199 return FT_Err_Out_Of_Memory;
1201 /* XXX: Make size configurable */
1202 /* we use the EM size */
1203 /* so that the resulting coordinates can be used without transformation */
1204 error = FT_Set_Char_Size(face, face->units_per_EM * 64, 0, 72, 0);
1205 if (error)
1206 return error;
1208 /* loop over all glyphs in font face */
1209 for (i = 0; i < data->num_glyphs; i++)
1211 GLYPH* glyph = &data->glyphs[i];
1214 error = FT_Load_Glyph(face, i, FT_LOAD_NO_BITMAP | FT_LOAD_NO_AUTOHINT);
1215 if (error)
1216 return error;
1218 error = TA_create_glyph_data(&face->glyph->outline, glyph);
1219 if (error)
1220 return error;
1223 return TA_Err_Ok;
1228 * While the auto-hinter is glyph oriented (this is, using `glyf' data), it
1229 * relies on the `cmap' table and OpenType features to get style coverage
1230 * data. In TTCs, subfonts normally share the same `glyf' table but use
1231 * different `cmap's and OpenType features (in `GSUB' and `GPOS' tables).
1232 * To handle this gracefully, ttfautohint collects (and merges) the coverage
1233 * information in the `glyf_Data' structure.
1236 FT_Error
1237 TA_sfnt_handle_coverage(SFNT* sfnt,
1238 FONT* font)
1240 FT_Error error;
1242 SFNT_Table* glyf_table = &font->tables[sfnt->glyf_idx];
1243 glyf_Data* data = (glyf_Data*)glyf_table->data;
1245 FT_Face face = sfnt->face;
1246 TA_FaceGlobals curr_globals;
1248 TA_Style saved_fallback_style = font->fallback_style;
1251 /* using TA_STYLE_UNASSIGNED as the fallback style ensures */
1252 /* that uncovered glyphs stay as-is */
1253 /* (we handle the fallback style later on) */
1254 font->fallback_style = (TA_Style)TA_STYLE_UNASSIGNED;
1256 /* trigger computation of coverage */
1257 error = ta_loader_init(font);
1258 if (error)
1259 goto Exit;
1260 error = ta_loader_reset(font, face);
1261 if (error)
1262 goto Exit;
1263 ta_loader_done(font);
1265 font->fallback_style = saved_fallback_style;
1266 curr_globals = (TA_FaceGlobals)face->autohint.data;
1268 if (!data->master_globals)
1270 /* initialize */
1271 data->master_globals = curr_globals;
1272 goto Exit;
1275 /* we have the same `glyf' table for another subfont; */
1276 /* merge the current coverage info into the `master' coverage info */
1278 TA_FaceGlobals master_globals = data->master_globals;
1279 FT_Long count = master_globals->glyph_count;
1281 FT_UShort* master = master_globals->glyph_styles;
1282 FT_UShort* curr = curr_globals->glyph_styles;
1284 FT_UShort* limit = master + count;
1287 /* we simply copy the data, */
1288 /* assuming that a given glyph always has the same properties -- */
1289 /* as soon as we make the style selection more fine-grained, */
1290 /* it is possible that this assumption doesn't hold: */
1291 /* for example, glyph `A' can be used for both Cyrillic and Latin */
1292 while (master < limit)
1294 if ((*curr & TA_STYLE_MASK) != TA_STYLE_UNASSIGNED)
1295 *master = *curr;
1297 master++;
1298 curr++;
1302 Exit:
1303 return error;
1307 void
1308 TA_sfnt_adjust_coverage(SFNT* sfnt,
1309 FONT* font)
1311 SFNT_Table* glyf_table = &font->tables[sfnt->glyf_idx];
1312 glyf_Data* data = (glyf_Data*)glyf_table->data;
1314 TA_FaceGlobals master_globals = data->master_globals;
1317 /* use fallback style for uncovered glyphs */
1318 if (!data->adjusted)
1320 FT_Long nn;
1321 FT_UShort* gstyles = master_globals->glyph_styles;
1322 #ifdef TA_DEBUG
1323 FT_UInt count;
1326 if (sfnt->face->num_faces > 1)
1327 TA_LOG_GLOBAL(("\n"
1328 "using fallback style `%s' for unassigned glyphs"
1329 " (glyf table index %d):\n",
1330 ta_style_names[master_globals->font->fallback_style],
1331 sfnt->glyf_idx));
1332 else
1333 TA_LOG_GLOBAL(("\n"
1334 "using fallback style `%s' for unassigned glyphs:\n",
1335 ta_style_names[master_globals->font->fallback_style]));
1337 count = 0;
1339 for (nn = 0; nn < master_globals->glyph_count; nn++)
1341 if ((gstyles[nn] & TA_STYLE_MASK) == TA_STYLE_UNASSIGNED)
1343 if (!(count % 10))
1344 TA_LOG_GLOBAL((" "));
1346 TA_LOG_GLOBAL((" %d", nn));
1347 count++;
1349 if (!(count % 10))
1350 TA_LOG_GLOBAL(("\n"));
1354 if (!count)
1355 TA_LOG_GLOBAL((" (none)\n"));
1356 if (count % 10)
1357 TA_LOG_GLOBAL(("\n"));
1359 #endif /* TA_DEBUG */
1361 for (nn = 0; nn < master_globals->glyph_count; nn++)
1363 if ((gstyles[nn] & TA_STYLE_MASK) == TA_STYLE_UNASSIGNED)
1365 gstyles[nn] &= ~TA_STYLE_MASK;
1366 gstyles[nn] |= master_globals->font->fallback_style;
1370 data->adjusted = 1;
1375 #if 0
1377 void
1378 TA_sfnt_copy_master_coverage(SFNT* sfnt,
1379 FONT* font)
1381 SFNT_Table* glyf_table = &font->tables[sfnt->glyf_idx];
1382 glyf_Data* data = (glyf_Data*)glyf_table->data;
1384 FT_Face face = sfnt->face;
1386 TA_FaceGlobals master_globals = data->master_globals;
1387 TA_FaceGlobals curr_globals = (TA_FaceGlobals)face->autohint.data;
1390 if (master_globals != curr_globals)
1392 FT_Long count = master_globals->glyph_count;
1393 FT_UShort* master = master_globals->glyph_styles;
1394 FT_UShort* curr = curr_globals->glyph_styles;
1397 memcpy(curr, master, count * sizeof (FT_UShort));
1401 #endif /* 0 */
1403 /* end of taglyf.c */