3 /* written 2011 by Werner Lemberg <wl@gnu.org> */
5 /* This file needs FreeType 2.4.5 or newer. */
15 TA_font_read(FONT
* font
,
18 fseek(in
, 0, SEEK_END
);
19 font
->in_len
= ftell(in
);
20 fseek(in
, 0, SEEK_SET
);
22 /* a valid TTF can never be that small */
23 if (font
->in_len
< 100)
24 return FT_Err_Invalid_Argument
;
26 font
->in_buf
= (FT_Byte
*)malloc(font
->in_len
);
28 return FT_Err_Out_Of_Memory
;
30 if (fread(font
->in_buf
, 1, font
->in_len
, in
) != font
->in_len
)
31 return FT_Err_Invalid_Stream_Read
;
38 TA_font_init(FONT
* font
)
44 error
= FT_Init_FreeType(&font
->lib
);
48 /* get number of faces (i.e. subfonts) */
49 error
= FT_New_Memory_Face(font
->lib
, font
->in_buf
, font
->in_len
, -1, &f
);
52 font
->num_sfnts
= f
->num_faces
;
55 /* it is a TTC if we have more than a single subfont */
56 font
->sfnts
= (SFNT
*)calloc(1, font
->num_sfnts
* sizeof (SFNT
));
58 return FT_Err_Out_Of_Memory
;
65 TA_sfnt_split_into_SFNT_tables(SFNT
* sfnt
,
72 /* basic check whether font is a TTF or TTC */
73 if (!FT_IS_SFNT(sfnt
->face
))
74 return FT_Err_Invalid_Argument
;
76 error
= FT_Sfnt_Table_Info(sfnt
->face
, 0, NULL
, &sfnt
->num_table_infos
);
80 sfnt
->table_infos
= (SFNT_Table_Info
*)malloc(sfnt
->num_table_infos
81 * sizeof (SFNT_Table_Info
));
82 if (!sfnt
->table_infos
)
83 return FT_Err_Out_Of_Memory
;
85 /* collect SFNT tables and search for `glyf' and `loca' table */
86 sfnt
->glyf_idx
= MISSING
;
87 sfnt
->loca_idx
= MISSING
;
88 sfnt
->head_idx
= MISSING
;
89 sfnt
->maxp_idx
= MISSING
;
91 for (i
= 0; i
< sfnt
->num_table_infos
; i
++)
93 SFNT_Table_Info
* table_info
= &sfnt
->table_infos
[i
];
102 *table_info
= MISSING
;
104 error
= FT_Sfnt_Table_Info(sfnt
->face
, i
, &tag
, &len
);
107 /* this ignores both missing and zero-length tables */
108 if (error
== FT_Err_Table_Missing
)
114 /* ignore tables which we are going to create by ourselves, */
115 /* or which would become invalid otherwise */
116 else if (tag
== TTAG_fpgm
126 /* make the allocated buffer length a multiple of 4 */
127 buf_len
= (len
+ 3) & -3;
128 buf
= (FT_Byte
*)malloc(buf_len
);
130 return FT_Err_Out_Of_Memory
;
132 /* pad end of buffer with zeros */
133 buf
[buf_len
- 1] = 0x00;
134 buf
[buf_len
- 2] = 0x00;
135 buf
[buf_len
- 3] = 0x00;
138 error
= FT_Load_Sfnt_Table(sfnt
->face
, tag
, 0, buf
, &len
);
142 /* check whether we already have this table */
143 for (j
= 0; j
< font
->num_tables
; j
++)
145 SFNT_Table
* table
= &font
->tables
[j
];
148 if (table
->tag
== tag
150 && !memcmp(table
->buf
, buf
, len
))
154 if (tag
== TTAG_head
)
156 else if (tag
== TTAG_glyf
)
158 else if (tag
== TTAG_loca
)
160 else if (tag
== TTAG_maxp
)
163 if (j
== font
->num_tables
)
165 /* add element to table array if it is missing or different; */
166 /* in case of success, `buf' gets linked and is eventually */
167 /* freed in `TA_font_unload' */
168 error
= TA_font_add_table(font
, table_info
, tag
, len
, buf
);
174 /* reuse existing SFNT table */
185 /* no (non-empty) `glyf', `loca', `head', or `maxp' table; */
186 /* this can't be a valid TTF with outlines */
187 if (sfnt
->glyf_idx
== MISSING
188 || sfnt
->loca_idx
== MISSING
189 || sfnt
->head_idx
== MISSING
190 || sfnt
->maxp_idx
== MISSING
)
191 return FT_Err_Invalid_Argument
;
198 TA_glyph_parse_composite(GLYPH
* glyph
,
202 FT_ULong flags_offset
; /* after the loop, this is the offset */
203 /* to the last element in the flags array */
216 /* walk over component records */
220 return FT_Err_Invalid_Table
;
222 flags_offset
= p
- buf
;
227 /* skip glyph component index */
230 /* skip scaling and offset arguments */
231 if (flags
& ARGS_ARE_WORDS
)
236 if (flags
& WE_HAVE_A_SCALE
)
238 else if (flags
& WE_HAVE_AN_XY_SCALE
)
240 else if (flags
& WE_HAVE_A_2X2
)
242 } while (flags
& MORE_COMPONENTS
);
244 glyph
->flags_offset
= flags_offset
;
246 /* adjust glyph record length */
250 /* glyph->len2 = 0; */
251 glyph
->buf
= (FT_Byte
*)malloc(len
);
253 return FT_Err_Out_Of_Memory
;
255 /* copy record without instructions (if any) */
256 memcpy(glyph
->buf
, buf
, len
);
257 glyph
->buf
[flags_offset
] &= ~(WE_HAVE_INSTR
>> 8);
264 TA_glyph_parse_simple(GLYPH
* glyph
,
266 FT_UShort num_contours
,
270 FT_Byte
* flags_start
;
275 FT_ULong flags_size
; /* size of the flags array */
276 FT_ULong xy_size
; /* size of x and y coordinate arrays together */
287 ins_offset
= 10 + num_contours
* 2;
292 return FT_Err_Invalid_Table
;
294 /* get number of instructions */
295 num_ins
= *(p
++) << 8;
301 return FT_Err_Invalid_Table
;
303 /* get number of points from last outline point */
304 num_pts
= buf
[ins_offset
- 2] << 8;
305 num_pts
+= buf
[ins_offset
- 1];
323 return FT_Err_Invalid_Table
;
327 x_short
= (flags
& X_SHORT_VECTOR
) ? 1 : 2;
328 y_short
= (flags
& Y_SHORT_VECTOR
) ? 1 : 2;
330 have_x
= ((flags
& SAME_X
) && !(flags
& X_SHORT_VECTOR
)) ? 0 : 1;
331 have_y
= ((flags
& SAME_Y
) && !(flags
& Y_SHORT_VECTOR
)) ? 0 : 1;
338 return FT_Err_Invalid_Table
;
342 if (i
+ count
> num_pts
)
343 return FT_Err_Invalid_Table
;
346 xy_size
+= count
* x_short
* have_x
;
347 xy_size
+= count
* y_short
* have_y
;
352 if (p
+ xy_size
> endp
)
353 return FT_Err_Invalid_Table
;
355 flags_size
= p
- flags_start
;
357 /* store the data before and after the bytecode instructions */
358 /* in the same array */
359 glyph
->len1
= ins_offset
;
360 glyph
->len2
= flags_size
+ xy_size
;
361 glyph
->buf
= (FT_Byte
*)malloc(glyph
->len1
+ glyph
->len2
);
363 return FT_Err_Out_Of_Memory
;
365 /* now copy everything but the instructions */
366 memcpy(glyph
->buf
, buf
, glyph
->len1
);
367 memcpy(glyph
->buf
+ glyph
->len1
, flags_start
, glyph
->len2
);
374 TA_sfnt_split_glyf_table(SFNT
* sfnt
,
377 SFNT_Table
* glyf_table
= &font
->tables
[sfnt
->glyf_idx
];
378 SFNT_Table
* loca_table
= &font
->tables
[sfnt
->loca_idx
];
379 SFNT_Table
* head_table
= &font
->tables
[sfnt
->head_idx
];
385 FT_ULong offset_next
;
391 /* in case of success, all allocated arrays are */
392 /* linked and eventually freed in `TA_font_unload' */
394 /* nothing to do if table has already been split */
395 if (glyf_table
->data
)
398 data
= (glyf_Data
*)calloc(1, sizeof (glyf_Data
));
400 return FT_Err_Out_Of_Memory
;
402 glyf_table
->data
= data
;
404 loca_format
= head_table
->buf
[LOCA_FORMAT_OFFSET
];
406 data
->num_glyphs
= loca_format
? loca_table
->len
/ 4 - 1
407 : loca_table
->len
/ 2 - 1;
408 data
->glyphs
= (GLYPH
*)calloc(1, data
->num_glyphs
* sizeof (GLYPH
));
410 return FT_Err_Out_Of_Memory
;
416 offset_next
= *(p
++) << 24;
417 offset_next
+= *(p
++) << 16;
418 offset_next
+= *(p
++) << 8;
419 offset_next
+= *(p
++);
423 offset_next
= *(p
++) << 8;
424 offset_next
+= *(p
++);
428 /* loop over `loca' and `glyf' data */
429 for (i
= 0; i
< data
->num_glyphs
; i
++)
431 GLYPH
* glyph
= &data
->glyphs
[i
];
435 offset
= offset_next
;
439 offset_next
= *(p
++) << 24;
440 offset_next
+= *(p
++) << 16;
441 offset_next
+= *(p
++) << 8;
442 offset_next
+= *(p
++);
446 offset_next
= *(p
++) << 8;
447 offset_next
+= *(p
++);
451 if (offset_next
< offset
452 || offset_next
> glyf_table
->len
)
453 return FT_Err_Invalid_Table
;
455 len
= offset_next
- offset
;
457 continue; /* empty glyph */
461 FT_Short num_contours
;
465 /* check header size */
467 return FT_Err_Invalid_Table
;
469 buf
= glyf_table
->buf
+ offset
;
470 num_contours
= (FT_Short
)((buf
[0] << 8) + buf
[1]);
472 /* We must parse the rest of the glyph record to get the exact */
473 /* record length. Since the `loca' table rounds record lengths */
474 /* up to multiples of 4 (or 2 for older fonts), and we must round */
475 /* up again after stripping off the instructions, it would be */
476 /* possible otherwise to have more than 4 bytes of padding which */
477 /* is more or less invalid. */
479 if (num_contours
< 0)
481 error
= TA_glyph_parse_composite(glyph
, buf
, len
);
487 error
= TA_glyph_parse_simple(glyph
, buf
, num_contours
, len
);
499 TA_sfnt_build_glyf_table(SFNT
* sfnt
,
502 SFNT_Table
* glyf_table
= &font
->tables
[sfnt
->glyf_idx
];
503 glyf_Data
* data
= (glyf_Data
*)glyf_table
->data
;
513 if (glyf_table
->processed
)
518 glyph
= data
->glyphs
;
519 for (i
= 0; i
< data
->num_glyphs
; i
++, glyph
++)
521 /* glyph records should have offsets which are multiples of 4 */
522 len
= (len
+ 3) & ~3;
523 len
+= glyph
->len1
+ glyph
->len2
+ glyph
->ins_len
;
524 /* add two bytes for the instructionLength field */
525 if (glyph
->len2
|| glyph
->ins_len
)
529 glyf_table
->len
= len
;
530 buf_new
= (FT_Byte
*)realloc(glyf_table
->buf
, (len
+ 3) & ~3);
532 return FT_Err_Out_Of_Memory
;
534 glyf_table
->buf
= buf_new
;
537 glyph
= data
->glyphs
;
538 for (i
= 0; i
< data
->num_glyphs
; i
++, glyph
++)
540 len
= glyph
->len1
+ glyph
->len2
+ glyph
->ins_len
;
541 if (glyph
->len2
|| glyph
->ins_len
)
546 /* copy glyph data and insert new instructions */
547 memcpy(p
, glyph
->buf
, glyph
->len1
);
553 *(p
++) = HIGH(glyph
->ins_len
);
554 *(p
++) = LOW(glyph
->ins_len
);
555 memcpy(p
, glyph
->ins_buf
, glyph
->ins_len
);
557 memcpy(p
, glyph
->buf
+ glyph
->len1
, glyph
->len2
);
562 /* composite glyph */
565 *(p
+ glyph
->flags_offset
) |= (WE_HAVE_INSTR
>> 8);
567 *(p
++) = HIGH(glyph
->ins_len
);
568 *(p
++) = LOW(glyph
->ins_len
);
569 memcpy(p
, glyph
->ins_buf
, glyph
->ins_len
);
576 /* pad with zero bytes to have an offset which is a multiple of 4; */
577 /* this works even for the last glyph record since the `glyf' */
578 /* table length is a multiple of 4 also */
593 glyf_table
->checksum
= TA_table_compute_checksum(glyf_table
->buf
,
595 glyf_table
->processed
= 1;
602 TA_sfnt_build_loca_table(SFNT
* sfnt
,
605 SFNT_Table
* loca_table
= &font
->tables
[sfnt
->loca_idx
];
606 SFNT_Table
* glyf_table
= &font
->tables
[sfnt
->glyf_idx
];
607 SFNT_Table
* head_table
= &font
->tables
[sfnt
->head_idx
];
609 glyf_Data
* data
= (glyf_Data
*)glyf_table
->data
;
619 if (loca_table
->processed
)
622 /* get largest offset */
624 glyph
= data
->glyphs
;
626 for (i
= 0; i
< data
->num_glyphs
; i
++, glyph
++)
628 /* glyph records should have offsets which are multiples of 4 */
629 offset
= (offset
+ 3) & ~3;
630 offset
+= glyph
->len1
+ glyph
->len2
+ glyph
->ins_len
;
631 /* add two bytes for the instructionLength field */
632 if (glyph
->len2
|| glyph
->ins_len
)
636 if (offset
> 0xFFFF * 2)
644 loca_table
->len
= (data
->num_glyphs
+ 1) * 4;
645 buf_new
= (FT_Byte
*)realloc(loca_table
->buf
, loca_table
->len
);
647 return FT_Err_Out_Of_Memory
;
649 loca_table
->buf
= buf_new
;
653 glyph
= data
->glyphs
;
655 for (i
= 0; i
< data
->num_glyphs
; i
++, glyph
++)
657 offset
= (offset
+ 3) & ~3;
659 *(p
++) = BYTE1(offset
);
660 *(p
++) = BYTE2(offset
);
661 *(p
++) = BYTE3(offset
);
662 *(p
++) = BYTE4(offset
);
664 offset
+= glyph
->len1
+ glyph
->len2
+ glyph
->ins_len
;
665 if (glyph
->len2
|| glyph
->ins_len
)
669 /* last element holds the size of the `glyf' table */
670 *(p
++) = BYTE1(offset
);
671 *(p
++) = BYTE2(offset
);
672 *(p
++) = BYTE3(offset
);
673 *(p
++) = BYTE4(offset
);
677 loca_table
->len
= (data
->num_glyphs
+ 1) * 2;
678 buf_new
= (FT_Byte
*)realloc(loca_table
->buf
,
679 (loca_table
->len
+ 3) & ~3);
681 return FT_Err_Out_Of_Memory
;
683 loca_table
->buf
= buf_new
;
687 glyph
= data
->glyphs
;
689 for (i
= 0; i
< data
->num_glyphs
; i
++, glyph
++)
691 offset
= (offset
+ 1) & ~1;
693 *(p
++) = HIGH(offset
);
694 *(p
++) = LOW(offset
);
696 offset
+= (glyph
->len1
+ glyph
->len2
+ glyph
->ins_len
+ 1) >> 1;
697 if (glyph
->len2
|| glyph
->ins_len
)
701 /* last element holds the size of the `glyf' table */
702 *(p
++) = HIGH(offset
);
703 *(p
++) = LOW(offset
);
705 /* pad `loca' table to make its length a multiple of 4 */
706 if (loca_table
->len
% 4 == 2)
713 loca_table
->checksum
= TA_table_compute_checksum(loca_table
->buf
,
715 loca_table
->processed
= 1;
717 head_table
->buf
[LOCA_FORMAT_OFFSET
] = loca_format
;
724 TA_sfnt_update_maxp_table(SFNT
* sfnt
,
727 SFNT_Table
* maxp_table
= &font
->tables
[sfnt
->maxp_idx
];
728 FT_Byte
* buf
= maxp_table
->buf
;
731 if (maxp_table
->processed
)
734 if (maxp_table
->len
!= MAXP_LEN
)
735 return FT_Err_Invalid_Table
;
737 buf
[MAXP_MAX_ZONES_OFFSET
] = 0;
738 buf
[MAXP_MAX_ZONES_OFFSET
+ 1] = 2;
739 buf
[MAXP_MAX_TWILIGHT_POINTS_OFFSET
] = HIGH(sfnt
->max_twilight_points
);
740 buf
[MAXP_MAX_TWILIGHT_POINTS_OFFSET
+ 1] = LOW(sfnt
->max_twilight_points
);
741 buf
[MAXP_MAX_STORAGE_OFFSET
] = HIGH(sfnt
->max_storage
);
742 buf
[MAXP_MAX_STORAGE_OFFSET
+ 1] = LOW(sfnt
->max_storage
);
743 buf
[MAXP_MAX_FUNCTION_DEFS_OFFSET
] = 0;
744 buf
[MAXP_MAX_FUNCTION_DEFS_OFFSET
+ 1] = NUM_FDEFS
;
745 buf
[MAXP_MAX_INSTRUCTION_DEFS_OFFSET
] = 0;
746 buf
[MAXP_MAX_INSTRUCTION_DEFS_OFFSET
+ 1] = 0;
747 buf
[MAXP_MAX_STACK_ELEMENTS_OFFSET
] = HIGH(sfnt
->max_stack_elements
);
748 buf
[MAXP_MAX_STACK_ELEMENTS_OFFSET
+ 1] = LOW(sfnt
->max_stack_elements
);
749 buf
[MAXP_MAX_INSTRUCTIONS_OFFSET
] = HIGH(sfnt
->max_instructions
);
750 buf
[MAXP_MAX_INSTRUCTIONS_OFFSET
+ 1] = LOW(sfnt
->max_instructions
);
752 maxp_table
->checksum
= TA_table_compute_checksum(maxp_table
->buf
,
754 maxp_table
->processed
= 1;
760 /* we build a dummy `DSIG' table only */
763 TA_table_build_DSIG(FT_Byte
** DSIG
)
768 buf
= (FT_Byte
*)malloc(DSIG_LEN
);
770 return FT_Err_Out_Of_Memory
;
778 /* zero signatures */
782 /* permission flags */
793 TA_table_build_gasp(FT_Byte
** gasp
)
798 buf
= (FT_Byte
*)malloc(GASP_LEN
);
800 return FT_Err_Out_Of_Memory
;
810 /* entry valid for all sizes */
814 buf
[7] = 0x0F; /* always use grayscale rendering with grid-fitting, */
815 /* symmetric grid-fitting and symmetric smoothing */
824 TA_sfnt_build_gasp_table(SFNT
* sfnt
,
832 error
= TA_sfnt_add_table_info(sfnt
);
836 error
= TA_table_build_gasp(&gasp_buf
);
840 /* in case of success, `gasp_buf' gets linked */
841 /* and is eventually freed in `TA_font_unload' */
842 error
= TA_font_add_table(font
,
843 &sfnt
->table_infos
[sfnt
->num_table_infos
- 1],
844 TTAG_gasp
, GASP_LEN
, gasp_buf
);
856 TA_font_compute_table_offsets(FONT
* font
,
860 FT_ULong offset
= start
;
863 for (i
= 0; i
< font
->num_tables
; i
++)
865 SFNT_Table
* table
= &font
->tables
[i
];
868 table
->offset
= offset
;
870 /* table offsets must be multiples of 4; */
871 /* this also fits the actual buffer lengths */
872 offset
+= (table
->len
+ 3) & ~3;
877 /* If `do_complete' is 0, only return `header_len'. */
880 TA_sfnt_build_TTF_header(SFNT
* sfnt
,
882 FT_Byte
** header_buf
,
883 FT_ULong
* header_len
,
886 SFNT_Table
* tables
= font
->tables
;
888 SFNT_Table_Info
* table_infos
= sfnt
->table_infos
;
889 FT_ULong num_table_infos
= sfnt
->num_table_infos
;
894 FT_Byte
* table_record
;
896 FT_Byte
* head_buf
= NULL
; /* pointer to `head' table */
897 FT_ULong head_checksum
; /* checksum in `head' table */
899 FT_ULong num_tables_in_header
;
903 num_tables_in_header
= 0;
904 for (i
= 0; i
< num_table_infos
; i
++)
906 /* ignore empty tables */
907 if (table_infos
[i
] != MISSING
)
908 num_tables_in_header
++;
911 len
= 12 + 16 * num_tables_in_header
;
917 buf
= (FT_Byte
*)malloc(len
);
919 return FT_Err_Out_Of_Memory
;
927 /* number of tables */
928 buf
[4] = HIGH(num_tables_in_header
);
929 buf
[5] = LOW(num_tables_in_header
);
933 FT_ULong search_range
, entry_selector
, range_shift
;
937 for (i
= 1, j
= 2; j
<= num_tables_in_header
; i
++, j
<<= 1)
940 entry_selector
= i
- 1;
941 search_range
= 0x10 << entry_selector
;
942 range_shift
= (num_tables_in_header
<< 4) - search_range
;
944 buf
[6] = HIGH(search_range
);
945 buf
[7] = LOW(search_range
);
946 buf
[8] = HIGH(entry_selector
);
947 buf
[9] = LOW(entry_selector
);
948 buf
[10] = HIGH(range_shift
);
949 buf
[11] = LOW(range_shift
);
952 /* location of the first table info record */
953 table_record
= &buf
[12];
957 /* loop over all tables */
958 for (i
= 0; i
< num_table_infos
; i
++)
960 SFNT_Table_Info table_info
= table_infos
[i
];
964 /* ignore empty slots */
965 if (table_info
== MISSING
)
968 table
= &tables
[table_info
];
970 if (table
->tag
== TTAG_head
)
972 /* we always reach this IF clause since FreeType would */
973 /* have aborted already if the `head' table were missing */
975 head_buf
= table
->buf
;
977 /* reset checksum in `head' table for recalculation */
984 head_checksum
+= table
->checksum
;
986 table_record
[0] = BYTE1(table
->tag
);
987 table_record
[1] = BYTE2(table
->tag
);
988 table_record
[2] = BYTE3(table
->tag
);
989 table_record
[3] = BYTE4(table
->tag
);
991 table_record
[4] = BYTE1(table
->checksum
);
992 table_record
[5] = BYTE2(table
->checksum
);
993 table_record
[6] = BYTE3(table
->checksum
);
994 table_record
[7] = BYTE4(table
->checksum
);
996 table_record
[8] = BYTE1(table
->offset
);
997 table_record
[9] = BYTE2(table
->offset
);
998 table_record
[10] = BYTE3(table
->offset
);
999 table_record
[11] = BYTE4(table
->offset
);
1001 table_record
[12] = BYTE1(table
->len
);
1002 table_record
[13] = BYTE2(table
->len
);
1003 table_record
[14] = BYTE3(table
->len
);
1004 table_record
[15] = BYTE4(table
->len
);
1009 /* the font header is complete; compute `head' checksum */
1010 head_checksum
+= TA_table_compute_checksum(buf
, len
);
1011 head_checksum
= 0xB1B0AFBAUL
- head_checksum
;
1013 /* store checksum in `head' table; */
1014 head_buf
[8] = BYTE1(head_checksum
);
1015 head_buf
[9] = BYTE2(head_checksum
);
1016 head_buf
[10] = BYTE3(head_checksum
);
1017 head_buf
[11] = BYTE4(head_checksum
);
1027 TA_font_build_TTF(FONT
* font
)
1029 SFNT
* sfnt
= &font
->sfnts
[0];
1032 FT_ULong num_tables
;
1034 FT_ULong SFNT_offset
;
1038 FT_Byte
* header_buf
;
1039 FT_ULong header_len
;
1045 /* add a dummy `DSIG' table */
1047 error
= TA_sfnt_add_table_info(sfnt
);
1051 error
= TA_table_build_DSIG(&DSIG_buf
);
1055 /* in case of success, `DSIG_buf' gets linked */
1056 /* and is eventually freed in `TA_font_unload' */
1057 error
= TA_font_add_table(font
,
1058 &sfnt
->table_infos
[sfnt
->num_table_infos
- 1],
1059 TTAG_DSIG
, DSIG_LEN
, DSIG_buf
);
1066 TA_sfnt_sort_table_info(sfnt
, font
);
1068 /* the first SFNT table immediately follows the header */
1069 (void)TA_sfnt_build_TTF_header(sfnt
, font
, NULL
, &SFNT_offset
, 0);
1070 TA_font_compute_table_offsets(font
, SFNT_offset
);
1072 error
= TA_sfnt_build_TTF_header(sfnt
, font
,
1073 &header_buf
, &header_len
, 1);
1079 tables
= font
->tables
;
1080 num_tables
= font
->num_tables
;
1082 /* get font length from last SFNT table array element */
1083 font
->out_len
= tables
[num_tables
- 1].offset
1084 + ((tables
[num_tables
- 1].len
+ 3) & ~3);
1085 font
->out_buf
= (FT_Byte
*)malloc(font
->out_len
);
1088 error
= FT_Err_Out_Of_Memory
;
1092 memcpy(font
->out_buf
, header_buf
, header_len
);
1094 for (i
= 0; i
< num_tables
; i
++)
1096 SFNT_Table
* table
= &tables
[i
];
1099 /* buffer length is a multiple of 4 */
1100 memcpy(font
->out_buf
+ table
->offset
,
1101 table
->buf
, (table
->len
+ 3) & ~3);
1114 TA_font_build_TTC_header(FONT
* font
,
1115 FT_Byte
** header_buf
,
1116 FT_ULong
* header_len
)
1118 SFNT
* sfnts
= font
->sfnts
;
1119 FT_Long num_sfnts
= font
->num_sfnts
;
1121 SFNT_Table
* tables
= font
->tables
;
1122 FT_ULong num_tables
= font
->num_tables
;
1124 FT_ULong TTF_offset
;
1125 FT_ULong DSIG_offset
;
1134 len
= 24 + 4 * num_sfnts
;
1135 buf
= (FT_Byte
*)malloc(len
);
1137 return FT_Err_Out_Of_Memory
;
1147 /* TTC header version */
1153 /* number of subfonts */
1154 *(p
++) = BYTE1(num_sfnts
);
1155 *(p
++) = BYTE2(num_sfnts
);
1156 *(p
++) = BYTE3(num_sfnts
);
1157 *(p
++) = BYTE4(num_sfnts
);
1159 /* the first TTF subfont header immediately follows the TTC header */
1162 /* loop over all subfonts */
1163 for (i
= 0; i
< num_sfnts
; i
++)
1165 SFNT
* sfnt
= &sfnts
[i
];
1169 TA_sfnt_sort_table_info(sfnt
, font
);
1170 /* only get header length */
1171 (void)TA_sfnt_build_TTF_header(sfnt
, font
, NULL
, &l
, 0);
1173 *(p
++) = BYTE1(TTF_offset
);
1174 *(p
++) = BYTE2(TTF_offset
);
1175 *(p
++) = BYTE3(TTF_offset
);
1176 *(p
++) = BYTE4(TTF_offset
);
1181 /* the first SFNT table immediately follows the subfont TTF headers */
1182 TA_font_compute_table_offsets(font
, TTF_offset
);
1196 /* DSIG offset; in a TTC this is always the last SFNT table */
1197 DSIG_offset
= tables
[num_tables
- 1].offset
;
1199 *(p
++) = BYTE1(DSIG_offset
);
1200 *(p
++) = BYTE2(DSIG_offset
);
1201 *(p
++) = BYTE3(DSIG_offset
);
1202 *(p
++) = BYTE4(DSIG_offset
);
1212 TA_font_build_TTC(FONT
* font
)
1214 SFNT
* sfnts
= font
->sfnts
;
1215 FT_Long num_sfnts
= font
->num_sfnts
;
1218 FT_ULong num_tables
;
1221 SFNT_Table_Info dummy
;
1223 FT_Byte
* TTC_header_buf
;
1224 FT_ULong TTC_header_len
;
1226 FT_Byte
** TTF_header_bufs
;
1227 FT_ULong
* TTF_header_lens
;
1235 /* add a dummy `DSIG' table */
1237 error
= TA_table_build_DSIG(&DSIG_buf
);
1241 /* in case of success, `DSIG_buf' gets linked */
1242 /* and is eventually freed in `TA_font_unload' */
1243 error
= TA_font_add_table(font
, &dummy
, TTAG_DSIG
, DSIG_LEN
, DSIG_buf
);
1250 /* this also computes the SFNT table offsets */
1251 error
= TA_font_build_TTC_header(font
,
1252 &TTC_header_buf
, &TTC_header_len
);
1256 TTF_header_bufs
= (FT_Byte
**)calloc(1, num_sfnts
* sizeof (FT_Byte
*));
1257 if (!TTF_header_bufs
)
1260 TTF_header_lens
= (FT_ULong
*)malloc(num_sfnts
* sizeof (FT_ULong
));
1261 if (!TTF_header_lens
)
1264 for (i
= 0; i
< num_sfnts
; i
++)
1266 error
= TA_sfnt_build_TTF_header(&sfnts
[i
], font
,
1267 &TTF_header_bufs
[i
],
1268 &TTF_header_lens
[i
], 1);
1275 tables
= font
->tables
;
1276 num_tables
= font
->num_tables
;
1278 /* get font length from last SFNT table array element */
1279 font
->out_len
= tables
[num_tables
- 1].offset
1280 + ((tables
[num_tables
- 1].len
+ 3) & ~3);
1281 font
->out_buf
= (FT_Byte
*)malloc(font
->out_len
);
1284 error
= FT_Err_Out_Of_Memory
;
1288 memcpy(font
->out_buf
, TTC_header_buf
, TTC_header_len
);
1290 offset
= TTC_header_len
;
1292 for (i
= 0; i
< num_sfnts
; i
++)
1294 memcpy(font
->out_buf
+ offset
,
1295 TTF_header_bufs
[i
], TTF_header_lens
[i
]);
1297 offset
+= TTF_header_lens
[i
];
1300 for (j
= 0; j
< num_tables
; j
++)
1302 SFNT_Table
* table
= &tables
[j
];
1305 /* buffer length is a multiple of 4 */
1306 memcpy(font
->out_buf
+ table
->offset
,
1307 table
->buf
, (table
->len
+ 3) & ~3);
1313 free(TTC_header_buf
);
1314 if (TTF_header_bufs
)
1316 for (i
= 0; i
< font
->num_sfnts
; i
++)
1317 free(TTF_header_bufs
[i
]);
1318 free(TTF_header_bufs
);
1320 free(TTF_header_lens
);
1327 TA_font_write(FONT
* font
,
1330 if (fwrite(font
->out_buf
, 1, font
->out_len
, out
) != font
->out_len
)
1331 return TA_Err_Invalid_Stream_Write
;
1338 TA_font_unload(FONT
* font
)
1340 /* in case of error it is expected that unallocated pointers */
1341 /* are NULL (and counters are zero) */
1347 ta_loader_done(font
->loader
);
1354 for (i
= 0; i
< font
->num_tables
; i
++)
1356 free(font
->tables
[i
].buf
);
1357 if (font
->tables
[i
].data
)
1359 if (font
->tables
[i
].tag
== TTAG_glyf
)
1361 glyf_Data
* data
= (glyf_Data
*)font
->tables
[i
].data
;
1365 for (j
= 0; j
< data
->num_glyphs
; j
++)
1367 free(data
->glyphs
[j
].buf
);
1368 free(data
->glyphs
[j
].ins_buf
);
1383 for (i
= 0; i
< font
->num_sfnts
; i
++)
1385 FT_Done_Face(font
->sfnts
[i
].face
);
1386 free(font
->sfnts
[i
].table_infos
);
1391 FT_Done_FreeType(font
->lib
);
1393 free(font
->out_buf
);
1399 TTF_autohint(FILE* in
,
1407 font
= (FONT
*)calloc(1, sizeof (FONT
));
1409 return FT_Err_Out_Of_Memory
;
1411 error
= TA_font_read(font
, in
);
1415 error
= TA_font_init(font
);
1419 /* loop over subfonts */
1420 for (i
= 0; i
< font
->num_sfnts
; i
++)
1422 SFNT
* sfnt
= &font
->sfnts
[i
];
1425 error
= FT_New_Memory_Face(font
->lib
, font
->in_buf
, font
->in_len
,
1430 error
= TA_sfnt_split_into_SFNT_tables(sfnt
, font
);
1434 error
= TA_sfnt_split_glyf_table(sfnt
, font
);
1439 /* build `gasp' table */
1440 error
= TA_sfnt_build_gasp_table(&font
->sfnts
[0], font
);
1444 /* XXX handle subfonts for bytecode tables? */
1446 /* build `cvt ' table */
1447 error
= TA_sfnt_build_cvt_table(&font
->sfnts
[0], font
);
1451 /* build `fpgm' table */
1452 error
= TA_sfnt_build_fpgm_table(&font
->sfnts
[0], font
);
1456 /* build `prep' table */
1457 error
= TA_sfnt_build_prep_table(&font
->sfnts
[0], font
);
1461 /* hint the glyphs and build bytecode */
1462 error
= TA_sfnt_build_glyf_hints(&font
->sfnts
[0], font
);
1466 /* loop again over subfonts */
1467 for (i
= 0; i
< font
->num_sfnts
; i
++)
1469 SFNT
* sfnt
= &font
->sfnts
[i
];
1472 error
= TA_sfnt_build_glyf_table(sfnt
, font
);
1475 error
= TA_sfnt_build_loca_table(sfnt
, font
);
1478 error
= TA_sfnt_update_maxp_table(sfnt
, font
);
1483 if (font
->num_sfnts
== 1)
1484 error
= TA_font_build_TTF(font
);
1486 error
= TA_font_build_TTC(font
);
1490 error
= TA_font_write(font
, out
);
1497 TA_font_unload(font
);
1502 /* end of ttfautohint.c */