4 * Copyright (C) 2011 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.
20 TA_sfnt_build_glyf_hints(SFNT
* sfnt
,
23 FT_Face face
= sfnt
->face
;
28 /* this loop doesn't include the artificial `.ttfautohint' glyph */
29 for (idx
= 0; idx
< face
->num_glyphs
; idx
++)
31 error
= TA_sfnt_build_glyph_instructions(sfnt
, font
, idx
);
35 font
->progress(idx
, face
->num_glyphs
,
36 sfnt
- font
->sfnts
, font
->num_sfnts
,
45 TA_glyph_get_components(GLYPH
* glyph
,
51 FT_UShort
* components_new
;
63 /* walk over component records */
67 return FT_Err_Invalid_Table
;
72 /* add component to list */
73 component
= *(p
++) << 8;
76 glyph
->num_components
++;
77 components_new
= (FT_UShort
*)realloc(glyph
->components
,
79 * sizeof (FT_UShort
));
82 glyph
->num_components
--;
83 return FT_Err_Out_Of_Memory
;
86 glyph
->components
= components_new
;
88 glyph
->components
[glyph
->num_components
- 1] = component
;
90 /* skip scaling and offset arguments */
91 if (flags
& ARGS_ARE_WORDS
)
96 if (flags
& WE_HAVE_A_SCALE
)
98 else if (flags
& WE_HAVE_AN_XY_SCALE
)
100 else if (flags
& WE_HAVE_A_2X2
)
102 } while (flags
& MORE_COMPONENTS
);
109 TA_glyph_parse_composite(GLYPH
* glyph
,
111 FT_UShort num_glyphs
)
113 FT_ULong flags_offset
; /* after the loop, this is the offset */
114 /* to the last element in the flags array */
126 /* walk over component records */
129 flags_offset
= p
- buf
;
137 /* skip scaling and offset arguments */
138 if (flags
& ARGS_ARE_WORDS
)
143 /* XXX adjust point indices for !ARGS_ARE_XY_VALUES */
145 if (flags
& WE_HAVE_A_SCALE
)
147 else if (flags
& WE_HAVE_AN_XY_SCALE
)
149 else if (flags
& WE_HAVE_A_2X2
)
151 } while (flags
& MORE_COMPONENTS
);
153 /* we prepend a composite glyph component to call some bytecode */
154 /* which eventually becomes the last glyph in the `glyf' table; */
155 /* for convenience, however, it is not added to the `components' array */
156 /* (doing so simplifies the conversion of point indices later on) */
158 /* adjust glyph record length (6 bytes for the additional component) */
159 new_len
= p
- buf
+ 6;
161 glyph
->flags_offset
= flags_offset
+ 6;
163 glyph
->len1
= new_len
;
164 /* glyph->len2 = 0; */
165 glyph
->buf
= (FT_Byte
*)malloc(new_len
);
167 return FT_Err_Out_Of_Memory
;
169 /* copy record without instructions (if any) */
170 /* and construct additional component */
172 memcpy(glyph
->buf
, buf
, 10); /* header */
174 glyph
->buf
[10] = 0x00; /* additional component */
175 glyph
->buf
[11] = ARGS_ARE_XY_VALUES
| MORE_COMPONENTS
;
176 glyph
->buf
[12] = HIGH(num_glyphs
- 1);
177 glyph
->buf
[13] = LOW(num_glyphs
- 1);
178 glyph
->buf
[14] = 0x00;
179 glyph
->buf
[15] = 0x00;
181 memcpy(glyph
->buf
+ 16, buf
+ 10, new_len
- 6 - 10); /* the rest */
183 glyph
->buf
[glyph
->flags_offset
] &= ~(WE_HAVE_INSTR
>> 8);
190 TA_glyph_parse_simple(GLYPH
* glyph
,
195 FT_Byte
* flags_start
;
199 FT_ULong flags_size
; /* size of the flags array */
200 FT_ULong xy_size
; /* size of x and y coordinate arrays together */
211 ins_offset
= 10 + glyph
->num_contours
* 2;
216 return FT_Err_Invalid_Table
;
218 /* get number of instructions */
219 num_ins
= *(p
++) << 8;
225 return FT_Err_Invalid_Table
;
231 while (i
< glyph
->num_points
)
242 return FT_Err_Invalid_Table
;
246 x_short
= (flags
& X_SHORT_VECTOR
) ? 1 : 2;
247 y_short
= (flags
& Y_SHORT_VECTOR
) ? 1 : 2;
249 have_x
= ((flags
& SAME_X
) && !(flags
& X_SHORT_VECTOR
)) ? 0 : 1;
250 have_y
= ((flags
& SAME_Y
) && !(flags
& Y_SHORT_VECTOR
)) ? 0 : 1;
257 return FT_Err_Invalid_Table
;
261 if (i
+ count
> glyph
->num_points
)
262 return FT_Err_Invalid_Table
;
265 xy_size
+= count
* x_short
* have_x
;
266 xy_size
+= count
* y_short
* have_y
;
271 if (p
+ xy_size
> endp
)
272 return FT_Err_Invalid_Table
;
274 flags_size
= p
- flags_start
;
276 /* store the data before and after the bytecode instructions */
277 /* in the same array */
278 glyph
->len1
= ins_offset
;
279 glyph
->len2
= flags_size
+ xy_size
;
280 glyph
->buf
= (FT_Byte
*)malloc(glyph
->len1
+ glyph
->len2
);
282 return FT_Err_Out_Of_Memory
;
284 /* now copy everything but the instructions */
285 memcpy(glyph
->buf
, buf
, glyph
->len1
);
286 memcpy(glyph
->buf
+ glyph
->len1
, flags_start
, glyph
->len2
);
293 TA_iterate_composite_glyph(glyf_Data
* data
,
294 FT_UShort
* components
,
295 FT_UShort num_components
,
296 FT_UShort
** pointsums
,
297 FT_UShort
* num_pointsums
,
298 FT_UShort
* num_composite_contours
,
299 FT_UShort
* num_composite_points
)
301 FT_UShort
* pointsums_new
;
305 /* save current state */
307 if (*num_pointsums
== 0xFFFF)
308 return FT_Err_Invalid_Table
;
311 pointsums_new
= (FT_UShort
*)realloc(*pointsums
,
313 * sizeof (FT_UShort
));
317 return FT_Err_Out_Of_Memory
;
320 *pointsums
= pointsums_new
;
322 (*pointsums
)[*num_pointsums
- 1] = *num_composite_points
;
324 for (i
= 0; i
< num_components
; i
++)
327 FT_UShort component
= components
[i
];
331 if (component
>= data
->num_glyphs
)
332 return FT_Err_Invalid_Table
;
334 glyph
= &data
->glyphs
[component
];
336 if (glyph
->num_components
)
338 error
= TA_iterate_composite_glyph(data
,
340 glyph
->num_components
,
343 num_composite_contours
,
344 num_composite_points
);
350 /* no need for checking overflow of the number of contours */
351 /* since the number of points is always larger or equal */
352 if (*num_composite_points
> 0xFFFF - glyph
->num_points
)
353 return FT_Err_Invalid_Table
;
355 *num_composite_contours
+= glyph
->num_contours
;
356 *num_composite_points
+= glyph
->num_points
;
365 TA_sfnt_compute_composite_pointsums(SFNT
* sfnt
,
368 SFNT_Table
* glyf_table
= &font
->tables
[sfnt
->glyf_idx
];
369 glyf_Data
* data
= (glyf_Data
*)glyf_table
->data
;
374 for (i
= 0; i
< data
->num_glyphs
; i
++)
376 GLYPH
* glyph
= &data
->glyphs
[i
];
379 if (glyph
->num_components
)
382 FT_UShort num_composite_contours
= 0;
383 FT_UShort num_composite_points
= 0;
386 error
= TA_iterate_composite_glyph(data
,
388 glyph
->num_components
,
390 &glyph
->num_pointsums
,
391 &num_composite_contours
,
392 &num_composite_points
);
396 /* update maximum values, */
397 /* including the subglyphs not in `components' array */
398 /* (each of them has a single point in a single contour) */
399 if (num_composite_points
+ glyph
->num_pointsums
400 > sfnt
->max_composite_points
)
401 sfnt
->max_composite_points
= num_composite_points
402 + glyph
->num_pointsums
;
403 if (num_composite_contours
+ glyph
->num_pointsums
404 > sfnt
->max_composite_contours
)
405 sfnt
->max_composite_contours
= num_composite_contours
406 + glyph
->num_pointsums
;
415 TA_sfnt_split_glyf_table(SFNT
* sfnt
,
418 SFNT_Table
* glyf_table
= &font
->tables
[sfnt
->glyf_idx
];
419 SFNT_Table
* loca_table
= &font
->tables
[sfnt
->loca_idx
];
420 SFNT_Table
* head_table
= &font
->tables
[sfnt
->head_idx
];
426 FT_ULong offset_next
;
430 FT_UShort loop_count
;
435 /* in case of success, all allocated arrays are */
436 /* linked and eventually freed in `TA_font_unload' */
438 /* nothing to do if table has already been split */
439 if (glyf_table
->data
)
442 data
= (glyf_Data
*)calloc(1, sizeof (glyf_Data
));
444 return FT_Err_Out_Of_Memory
;
446 glyf_table
->data
= data
;
448 loca_format
= head_table
->buf
[LOCA_FORMAT_OFFSET
];
450 data
->num_glyphs
= loca_format
? loca_table
->len
/ 4
451 : loca_table
->len
/ 2;
452 loop_count
= data
->num_glyphs
- 1;
454 /* allocate one more glyph slot if we have composite glyphs */
455 if (!sfnt
->max_components
)
456 data
->num_glyphs
-= 1;
457 data
->glyphs
= (GLYPH
*)calloc(1, data
->num_glyphs
* sizeof (GLYPH
));
459 return FT_Err_Out_Of_Memory
;
461 /* first loop over `loca' and `glyf' data */
467 offset_next
= *(p
++) << 24;
468 offset_next
+= *(p
++) << 16;
469 offset_next
+= *(p
++) << 8;
470 offset_next
+= *(p
++);
474 offset_next
= *(p
++) << 8;
475 offset_next
+= *(p
++);
479 for (i
= 0; i
< loop_count
; i
++)
481 GLYPH
* glyph
= &data
->glyphs
[i
];
485 offset
= offset_next
;
489 offset_next
= *(p
++) << 24;
490 offset_next
+= *(p
++) << 16;
491 offset_next
+= *(p
++) << 8;
492 offset_next
+= *(p
++);
496 offset_next
= *(p
++) << 8;
497 offset_next
+= *(p
++);
501 if (offset_next
< offset
502 || offset_next
> glyf_table
->len
)
503 return FT_Err_Invalid_Table
;
505 len
= offset_next
- offset
;
507 continue; /* empty glyph */
513 /* check header size */
515 return FT_Err_Invalid_Table
;
517 /* we need the number of contours and points for */
518 /* `TA_sfnt_compute_composite_pointsums' */
519 buf
= glyf_table
->buf
+ offset
;
520 glyph
->num_contours
= (FT_Short
)((buf
[0] << 8) + buf
[1]);
522 if (glyph
->num_contours
< 0)
524 error
= TA_glyph_get_components(glyph
, buf
, len
);
533 /* use the last contour's end point to compute number of points */
534 off
= 10 + (glyph
->num_contours
- 1) * 2;
535 glyph
->num_points
= buf
[off
] << 8;
536 glyph
->num_points
+= buf
[off
+ 1] + 1;
541 if (sfnt
->max_components
)
543 error
= TA_sfnt_compute_composite_pointsums(sfnt
, font
);
548 /* second loop over `loca' and `glyf' data */
554 offset_next
= *(p
++) << 24;
555 offset_next
+= *(p
++) << 16;
556 offset_next
+= *(p
++) << 8;
557 offset_next
+= *(p
++);
561 offset_next
= *(p
++) << 8;
562 offset_next
+= *(p
++);
566 for (i
= 0; i
< loop_count
; i
++)
568 GLYPH
* glyph
= &data
->glyphs
[i
];
572 offset
= offset_next
;
576 offset_next
= *(p
++) << 24;
577 offset_next
+= *(p
++) << 16;
578 offset_next
+= *(p
++) << 8;
579 offset_next
+= *(p
++);
583 offset_next
= *(p
++) << 8;
584 offset_next
+= *(p
++);
588 len
= offset_next
- offset
;
590 continue; /* empty glyph */
596 buf
= glyf_table
->buf
+ offset
;
598 /* We must parse the rest of the glyph record to get the exact */
599 /* record length. Since the `loca' table rounds record lengths */
600 /* up to multiples of 4 (or 2 for older fonts), and we must round */
601 /* up again after stripping off the instructions, it would be */
602 /* possible otherwise to have more than 4 bytes of padding which */
603 /* is more or less invalid. */
605 if (glyph
->num_contours
< 0)
606 error
= TA_glyph_parse_composite(glyph
, buf
, data
->num_glyphs
);
608 error
= TA_glyph_parse_simple(glyph
, buf
, len
);
614 if (sfnt
->max_components
)
616 /* construct and append our special glyph used as a composite element */
617 GLYPH
* glyph
= &data
->glyphs
[data
->num_glyphs
- 1];
620 FT_Byte bytecode
[] = {
622 /* increment `cvtl_is_subglyph' counter */
635 glyph
->buf
= (FT_Byte
*)malloc(glyph
->len1
+ glyph
->len2
);
637 return FT_Err_Out_Of_Memory
;
641 buf
[0] = 0x00; /* one contour */
643 buf
[2] = 0x00; /* no dimensions */
651 buf
[10] = 0x00; /* one contour end point */
654 buf
[12] = ON_CURVE
| SAME_X
| SAME_Y
; /* the flags for a point at 0,0 */
656 /* add bytecode also; */
657 /* this works because the loop in `TA_sfnt_build_glyf_hints' */
658 /* doesn't include the newly appended glyph */
659 glyph
->ins_len
= sizeof (bytecode
);
660 glyph
->ins_buf
= (FT_Byte
*)malloc(glyph
->ins_len
);
662 return FT_Err_Out_Of_Memory
;
663 memcpy(glyph
->ins_buf
, bytecode
, glyph
->ins_len
);
665 sfnt
->max_components
+= 1;
673 TA_sfnt_build_glyf_table(SFNT
* sfnt
,
676 SFNT_Table
* glyf_table
= &font
->tables
[sfnt
->glyf_idx
];
677 glyf_Data
* data
= (glyf_Data
*)glyf_table
->data
;
687 if (glyf_table
->processed
)
692 glyph
= data
->glyphs
;
693 for (i
= 0; i
< data
->num_glyphs
; i
++, glyph
++)
695 /* glyph records should have offsets which are multiples of 4 */
696 len
= (len
+ 3) & ~3;
697 len
+= glyph
->len1
+ glyph
->len2
+ glyph
->ins_len
;
698 /* add two bytes for the instructionLength field */
699 if (glyph
->len2
|| glyph
->ins_len
)
703 /* to make the short format of the `loca' table always work, */
704 /* assure an even length of the `glyf' table */
705 glyf_table
->len
= (len
+ 1) & ~1;
707 buf_new
= (FT_Byte
*)realloc(glyf_table
->buf
, (len
+ 3) & ~3);
709 return FT_Err_Out_Of_Memory
;
711 glyf_table
->buf
= buf_new
;
714 glyph
= data
->glyphs
;
715 for (i
= 0; i
< data
->num_glyphs
; i
++, glyph
++)
717 len
= glyph
->len1
+ glyph
->len2
+ glyph
->ins_len
;
718 if (glyph
->len2
|| glyph
->ins_len
)
723 /* copy glyph data and insert new instructions */
724 memcpy(p
, glyph
->buf
, glyph
->len1
);
730 *(p
++) = HIGH(glyph
->ins_len
);
731 *(p
++) = LOW(glyph
->ins_len
);
732 memcpy(p
, glyph
->ins_buf
, glyph
->ins_len
);
734 memcpy(p
, glyph
->buf
+ glyph
->len1
, glyph
->len2
);
739 /* composite glyph */
742 *(p
+ glyph
->flags_offset
) |= (WE_HAVE_INSTR
>> 8);
744 *(p
++) = HIGH(glyph
->ins_len
);
745 *(p
++) = LOW(glyph
->ins_len
);
746 memcpy(p
, glyph
->ins_buf
, glyph
->ins_len
);
753 /* pad with zero bytes to have an offset which is a multiple of 4; */
754 /* this works even for the last glyph record since the `glyf' */
755 /* table length is a multiple of 4 also */
770 glyf_table
->checksum
= TA_table_compute_checksum(glyf_table
->buf
,
772 glyf_table
->processed
= 1;
779 TA_create_glyph_data(FT_Outline
* outline
,
782 FT_Error error
= TA_Err_Ok
;
788 FT_Byte
* flags
= NULL
;
802 if (!outline
->n_contours
)
803 return TA_Err_Ok
; /* empty glyph */
805 /* in case of success, all non-local allocated arrays are */
806 /* linked and eventually freed in `TA_font_unload' */
810 /* we use `calloc' since we rely on the array */
811 /* being initialized to zero; */
812 /* additionally, we need one more byte for a test after the loop */
813 flags
= (FT_Byte
*)calloc(1, outline
->n_points
+ 1);
816 error
= FT_Err_Out_Of_Memory
;
820 /* we have either one-byte or two-byte elements */
821 x
= (FT_Byte
*)malloc(2 * outline
->n_points
);
824 error
= FT_Err_Out_Of_Memory
;
828 y
= (FT_Byte
*)malloc(2 * outline
->n_points
);
831 error
= FT_Err_Out_Of_Memory
;
838 xmin
= xmax
= (outline
->points
[0].x
+ 32) >> 6;
839 ymin
= ymax
= (outline
->points
[0].y
+ 32) >> 6;
842 oldf
= 0x80; /* start with an impossible value */
844 /* convert the FreeType representation of the glyph's outline */
845 /* into the representation format of the `glyf' table */
846 for (i
= 0; i
< outline
->n_points
; i
++)
848 FT_Pos xcur
= (outline
->points
[i
].x
+ 32) >> 6;
849 FT_Pos ycur
= (outline
->points
[i
].y
+ 32) >> 6;
851 FT_Pos xdelta
= xcur
- lastx
;
852 FT_Pos ydelta
= ycur
- lasty
;
855 /* we are only interested in bit 0 of the `tags' array */
856 f
= outline
->tags
[i
] & ON_CURVE
;
864 if (xdelta
< 256 && xdelta
> -256)
873 *(xp
++) = (FT_Byte
)xdelta
;
877 *(xp
++) = HIGH(xdelta
);
878 *(xp
++) = LOW(xdelta
);
888 if (ydelta
< 256 && ydelta
> -256)
897 *(yp
++) = (FT_Byte
)ydelta
;
901 *(yp
++) = HIGH(ydelta
);
902 *(yp
++) = LOW(ydelta
);
908 /* set repeat flag */
909 *(flagsp
- 1) |= REPEAT
;
913 /* we can only handle 256 repetitions at once, */
914 /* so use a new counter */
919 *flagsp
+= 1; /* increase repetition counter */
924 flagsp
++; /* skip repetition counter */
942 /* if the last byte was a repetition counter, */
943 /* we must increase by one to get the correct array size */
947 header
[0] = HIGH(outline
->n_contours
);
948 header
[1] = LOW(outline
->n_contours
);
949 header
[2] = HIGH(xmin
);
950 header
[3] = LOW(xmin
);
951 header
[4] = HIGH(ymin
);
952 header
[5] = LOW(ymin
);
953 header
[6] = HIGH(xmax
);
954 header
[7] = LOW(xmax
);
955 header
[8] = HIGH(ymax
);
956 header
[9] = LOW(ymax
);
958 /* concatenate all arrays and fill needed GLYPH structure elements */
960 glyph
->len1
= 10 + 2 * outline
->n_contours
;
961 glyph
->len2
= (flagsp
- flags
) + (xp
- x
) + (yp
- y
);
963 glyph
->buf
= (FT_Byte
*)malloc(glyph
->len1
+ glyph
->len2
);
966 error
= FT_Err_Out_Of_Memory
;
971 memcpy(p
, header
, 10);
975 glyph
->ins_buf
= NULL
;
977 for (i
= 0; i
< outline
->n_contours
; i
++)
979 *(p
++) = HIGH(outline
->contours
[i
]);
980 *(p
++) = LOW(outline
->contours
[i
]);
983 memcpy(p
, flags
, flagsp
- flags
);
985 memcpy(p
, x
, xp
- x
);
987 memcpy(p
, y
, yp
- y
);
998 /* We hint each glyph at EM size and construct a new `glyf' table. */
999 /* Some fonts need this; in particular, */
1000 /* there are CJK fonts which use hints to scale and position subglyphs. */
1001 /* As a consequence, there are no longer composite glyphs. */
1004 TA_sfnt_create_glyf_data(SFNT
* sfnt
,
1007 SFNT_Table
* glyf_table
= &font
->tables
[sfnt
->glyf_idx
];
1008 FT_Face face
= sfnt
->face
;
1016 /* in case of success, all allocated arrays are */
1017 /* linked and eventually freed in `TA_font_unload' */
1019 /* nothing to do if table has already been created */
1020 if (glyf_table
->data
)
1023 data
= (glyf_Data
*)calloc(1, sizeof (glyf_Data
));
1025 return FT_Err_Out_Of_Memory
;
1027 glyf_table
->data
= data
;
1029 data
->num_glyphs
= face
->num_glyphs
;
1030 data
->glyphs
= (GLYPH
*)calloc(1, data
->num_glyphs
* sizeof (GLYPH
));
1032 return FT_Err_Out_Of_Memory
;
1034 /* XXX: Make size configurable */
1035 /* we use the EM size */
1036 /* so that the resulting coordinates can be used without transformation */
1037 error
= FT_Set_Char_Size(face
, face
->units_per_EM
* 64, 0, 72, 0);
1041 /* loop over all glyphs in font face */
1042 for (i
= 0; i
< data
->num_glyphs
; i
++)
1044 GLYPH
* glyph
= &data
->glyphs
[i
];
1047 error
= FT_Load_Glyph(face
, i
, FT_LOAD_NO_BITMAP
| FT_LOAD_NO_AUTOHINT
);
1051 error
= TA_create_glyph_data(&face
->glyph
->outline
, glyph
);
1059 /* end of taglyf.c */