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
125 /* make the allocated buffer length a multiple of 4 */
126 buf_len
= (len
+ 3) & -3;
127 buf
= (FT_Byte
*)malloc(buf_len
);
129 return FT_Err_Out_Of_Memory
;
131 /* pad end of buffer with zeros */
132 buf
[buf_len
- 1] = 0x00;
133 buf
[buf_len
- 2] = 0x00;
134 buf
[buf_len
- 3] = 0x00;
137 error
= FT_Load_Sfnt_Table(sfnt
->face
, tag
, 0, buf
, &len
);
141 /* check whether we already have this table */
142 for (j
= 0; j
< font
->num_tables
; j
++)
144 SFNT_Table
* table
= &font
->tables
[j
];
147 if (table
->tag
== tag
149 && !memcmp(table
->buf
, buf
, len
))
153 if (tag
== TTAG_head
)
155 else if (tag
== TTAG_glyf
)
157 else if (tag
== TTAG_loca
)
159 else if (tag
== TTAG_maxp
)
162 if (j
== font
->num_tables
)
164 /* add element to table array if it is missing or different; */
165 /* in case of success, `buf' gets linked and is eventually */
166 /* freed in `TA_font_unload' */
167 error
= TA_font_add_table(font
, table_info
, tag
, len
, buf
);
173 /* reuse existing SFNT table */
184 /* no (non-empty) `glyf', `loca', `head', or `maxp' table; */
185 /* this can't be a valid TTF with outlines */
186 if (sfnt
->glyf_idx
== MISSING
187 || sfnt
->loca_idx
== MISSING
188 || sfnt
->head_idx
== MISSING
189 || sfnt
->maxp_idx
== MISSING
)
190 return FT_Err_Invalid_Argument
;
197 TA_glyph_parse_composite(GLYPH
* glyph
,
201 FT_ULong flags_offset
; /* after the loop, this is the offset */
202 /* to the last element in the flags array */
215 /* walk over component records */
219 return FT_Err_Invalid_Table
;
221 flags_offset
= p
- buf
;
226 /* skip glyph component index */
229 /* skip scaling and offset arguments */
230 if (flags
& ARGS_ARE_WORDS
)
235 if (flags
& WE_HAVE_A_SCALE
)
237 else if (flags
& WE_HAVE_AN_XY_SCALE
)
239 else if (flags
& WE_HAVE_A_2X2
)
241 } while (flags
& MORE_COMPONENTS
);
243 glyph
->flags_offset
= flags_offset
;
245 /* adjust glyph record length */
249 /* glyph->len2 = 0; */
250 glyph
->buf
= (FT_Byte
*)malloc(len
);
252 return FT_Err_Out_Of_Memory
;
254 /* copy record without instructions (if any) */
255 memcpy(glyph
->buf
, buf
, len
);
256 glyph
->buf
[flags_offset
] &= ~(WE_HAVE_INSTR
>> 8);
263 TA_glyph_parse_simple(GLYPH
* glyph
,
265 FT_UShort num_contours
,
269 FT_Byte
* flags_start
;
274 FT_ULong flags_size
; /* size of the flags array */
275 FT_ULong xy_size
; /* size of x and y coordinate arrays together */
286 ins_offset
= 10 + num_contours
* 2;
291 return FT_Err_Invalid_Table
;
293 /* get number of instructions */
294 num_ins
= *(p
++) << 8;
300 return FT_Err_Invalid_Table
;
302 /* get number of points from last outline point */
303 num_pts
= buf
[ins_offset
- 2] << 8;
304 num_pts
+= buf
[ins_offset
- 1];
322 return FT_Err_Invalid_Table
;
326 x_short
= (flags
& X_SHORT_VECTOR
) ? 1 : 2;
327 y_short
= (flags
& Y_SHORT_VECTOR
) ? 1 : 2;
329 have_x
= ((flags
& SAME_X
) && !(flags
& X_SHORT_VECTOR
)) ? 0 : 1;
330 have_y
= ((flags
& SAME_Y
) && !(flags
& Y_SHORT_VECTOR
)) ? 0 : 1;
337 return FT_Err_Invalid_Table
;
341 if (i
+ count
> num_pts
)
342 return FT_Err_Invalid_Table
;
345 xy_size
+= count
* x_short
* have_x
;
346 xy_size
+= count
* y_short
* have_y
;
351 if (p
+ xy_size
> endp
)
352 return FT_Err_Invalid_Table
;
354 flags_size
= p
- flags_start
;
356 /* store the data before and after the bytecode instructions */
357 /* in the same array */
358 glyph
->len1
= ins_offset
;
359 glyph
->len2
= flags_size
+ xy_size
;
360 glyph
->buf
= (FT_Byte
*)malloc(glyph
->len1
+ glyph
->len2
);
362 return FT_Err_Out_Of_Memory
;
364 /* now copy everything but the instructions */
365 memcpy(glyph
->buf
, buf
, glyph
->len1
);
366 memcpy(glyph
->buf
+ glyph
->len1
, flags_start
, glyph
->len2
);
373 TA_sfnt_split_glyf_table(SFNT
* sfnt
,
376 SFNT_Table
* glyf_table
= &font
->tables
[sfnt
->glyf_idx
];
377 SFNT_Table
* loca_table
= &font
->tables
[sfnt
->loca_idx
];
378 SFNT_Table
* head_table
= &font
->tables
[sfnt
->head_idx
];
384 FT_ULong offset_next
;
390 /* in case of success, all allocated arrays are */
391 /* linked and eventually freed in `TA_font_unload' */
393 /* nothing to do if table has already been split */
394 if (glyf_table
->data
)
397 data
= (glyf_Data
*)calloc(1, sizeof (glyf_Data
));
399 return FT_Err_Out_Of_Memory
;
401 glyf_table
->data
= data
;
403 loca_format
= head_table
->buf
[LOCA_FORMAT_OFFSET
];
405 data
->num_glyphs
= loca_format
? loca_table
->len
/ 4 - 1
406 : loca_table
->len
/ 2 - 1;
407 data
->glyphs
= (GLYPH
*)calloc(1, data
->num_glyphs
* sizeof (GLYPH
));
409 return FT_Err_Out_Of_Memory
;
415 offset_next
= *(p
++) << 24;
416 offset_next
+= *(p
++) << 16;
417 offset_next
+= *(p
++) << 8;
418 offset_next
+= *(p
++);
422 offset_next
= *(p
++) << 8;
423 offset_next
+= *(p
++);
427 /* loop over `loca' and `glyf' data */
428 for (i
= 0; i
< data
->num_glyphs
; i
++)
430 GLYPH
* glyph
= &data
->glyphs
[i
];
434 offset
= offset_next
;
438 offset_next
= *(p
++) << 24;
439 offset_next
+= *(p
++) << 16;
440 offset_next
+= *(p
++) << 8;
441 offset_next
+= *(p
++);
445 offset_next
= *(p
++) << 8;
446 offset_next
+= *(p
++);
450 if (offset_next
< offset
451 || offset_next
> glyf_table
->len
)
452 return FT_Err_Invalid_Table
;
454 len
= offset_next
- offset
;
456 continue; /* empty glyph */
460 FT_Short num_contours
;
464 /* check header size */
466 return FT_Err_Invalid_Table
;
468 buf
= glyf_table
->buf
+ offset
;
469 num_contours
= (FT_Short
)((buf
[0] << 8) + buf
[1]);
471 /* We must parse the rest of the glyph record to get the exact */
472 /* record length. Since the `loca' table rounds record lengths */
473 /* up to multiples of 4 (or 2 for older fonts), and we must round */
474 /* up again after stripping off the instructions, it would be */
475 /* possible otherwise to have more than 4 bytes of padding which */
476 /* is more or less invalid. */
478 if (num_contours
< 0)
480 error
= TA_glyph_parse_composite(glyph
, buf
, len
);
486 error
= TA_glyph_parse_simple(glyph
, buf
, num_contours
, len
);
498 TA_sfnt_build_glyf_table(SFNT
* sfnt
,
501 SFNT_Table
* glyf_table
= &font
->tables
[sfnt
->glyf_idx
];
502 glyf_Data
* data
= (glyf_Data
*)glyf_table
->data
;
512 if (glyf_table
->processed
)
517 glyph
= data
->glyphs
;
518 for (i
= 0; i
< data
->num_glyphs
; i
++, glyph
++)
520 /* glyph records should have offsets which are multiples of 4 */
521 len
= (len
+ 3) & ~3;
522 len
+= glyph
->len1
+ glyph
->len2
+ glyph
->ins_len
;
523 /* add two bytes for the instructionLength field */
524 if (glyph
->len2
|| glyph
->ins_len
)
528 glyf_table
->len
= len
;
529 buf_new
= (FT_Byte
*)realloc(glyf_table
->buf
, (len
+ 3) & ~3);
531 return FT_Err_Out_Of_Memory
;
533 glyf_table
->buf
= buf_new
;
536 glyph
= data
->glyphs
;
537 for (i
= 0; i
< data
->num_glyphs
; i
++, glyph
++)
539 len
= glyph
->len1
+ glyph
->len2
+ glyph
->ins_len
;
540 if (glyph
->len2
|| glyph
->ins_len
)
545 /* copy glyph data and insert new instructions */
546 memcpy(p
, glyph
->buf
, glyph
->len1
);
552 *(p
++) = HIGH(glyph
->ins_len
);
553 *(p
++) = LOW(glyph
->ins_len
);
554 memcpy(p
, glyph
->ins_buf
, glyph
->ins_len
);
556 memcpy(p
, glyph
->buf
+ glyph
->len1
, glyph
->len2
);
561 /* composite glyph */
564 *(p
+ glyph
->flags_offset
) |= (WE_HAVE_INSTR
>> 8);
566 *(p
++) = HIGH(glyph
->ins_len
);
567 *(p
++) = LOW(glyph
->ins_len
);
568 memcpy(p
, glyph
->ins_buf
, glyph
->ins_len
);
575 /* pad with zero bytes to have an offset which is a multiple of 4; */
576 /* this works even for the last glyph record since the `glyf' */
577 /* table length is a multiple of 4 also */
592 glyf_table
->checksum
= TA_table_compute_checksum(glyf_table
->buf
,
594 glyf_table
->processed
= 1;
601 TA_sfnt_build_loca_table(SFNT
* sfnt
,
604 SFNT_Table
* loca_table
= &font
->tables
[sfnt
->loca_idx
];
605 SFNT_Table
* glyf_table
= &font
->tables
[sfnt
->glyf_idx
];
606 SFNT_Table
* head_table
= &font
->tables
[sfnt
->head_idx
];
608 glyf_Data
* data
= (glyf_Data
*)glyf_table
->data
;
618 if (loca_table
->processed
)
621 /* get largest offset */
623 glyph
= data
->glyphs
;
625 for (i
= 0; i
< data
->num_glyphs
; i
++, glyph
++)
627 /* glyph records should have offsets which are multiples of 4 */
628 offset
= (offset
+ 3) & ~3;
629 offset
+= glyph
->len1
+ glyph
->len2
+ glyph
->ins_len
;
630 /* add two bytes for the instructionLength field */
631 if (glyph
->len2
|| glyph
->ins_len
)
635 if (offset
> 0xFFFF * 2)
643 loca_table
->len
= (data
->num_glyphs
+ 1) * 4;
644 buf_new
= (FT_Byte
*)realloc(loca_table
->buf
, loca_table
->len
);
646 return FT_Err_Out_Of_Memory
;
648 loca_table
->buf
= buf_new
;
652 glyph
= data
->glyphs
;
654 for (i
= 0; i
< data
->num_glyphs
; i
++, glyph
++)
656 offset
= (offset
+ 3) & ~3;
658 *(p
++) = BYTE1(offset
);
659 *(p
++) = BYTE2(offset
);
660 *(p
++) = BYTE3(offset
);
661 *(p
++) = BYTE4(offset
);
663 offset
+= glyph
->len1
+ glyph
->len2
+ glyph
->ins_len
;
664 if (glyph
->len2
|| glyph
->ins_len
)
668 /* last element holds the size of the `glyf' table */
669 *(p
++) = BYTE1(offset
);
670 *(p
++) = BYTE2(offset
);
671 *(p
++) = BYTE3(offset
);
672 *(p
++) = BYTE4(offset
);
676 loca_table
->len
= (data
->num_glyphs
+ 1) * 2;
677 buf_new
= (FT_Byte
*)realloc(loca_table
->buf
,
678 (loca_table
->len
+ 3) & ~3);
680 return FT_Err_Out_Of_Memory
;
682 loca_table
->buf
= buf_new
;
686 glyph
= data
->glyphs
;
688 for (i
= 0; i
< data
->num_glyphs
; i
++, glyph
++)
690 offset
= (offset
+ 1) & ~1;
692 *(p
++) = HIGH(offset
);
693 *(p
++) = LOW(offset
);
695 offset
+= (glyph
->len1
+ glyph
->len2
+ glyph
->ins_len
+ 1) >> 1;
696 if (glyph
->len2
|| glyph
->ins_len
)
700 /* last element holds the size of the `glyf' table */
701 *(p
++) = HIGH(offset
);
702 *(p
++) = LOW(offset
);
704 /* pad `loca' table to make its length a multiple of 4 */
705 if (loca_table
->len
% 4 == 2)
712 loca_table
->checksum
= TA_table_compute_checksum(loca_table
->buf
,
714 loca_table
->processed
= 1;
716 head_table
->buf
[LOCA_FORMAT_OFFSET
] = loca_format
;
723 TA_sfnt_update_maxp_table(SFNT
* sfnt
,
726 SFNT_Table
* maxp_table
= &font
->tables
[sfnt
->maxp_idx
];
727 FT_Byte
* buf
= maxp_table
->buf
;
730 if (maxp_table
->processed
)
733 if (maxp_table
->len
!= MAXP_LEN
)
734 return FT_Err_Invalid_Table
;
736 buf
[MAXP_MAX_ZONES_OFFSET
] = 0;
737 buf
[MAXP_MAX_ZONES_OFFSET
+ 1] = 1;
738 buf
[MAXP_MAX_TWILIGHT_POINTS_OFFSET
] = 0;
739 buf
[MAXP_MAX_TWILIGHT_POINTS_OFFSET
+ 1] = 0;
740 buf
[MAXP_MAX_STORAGE_OFFSET
] = 0;
741 buf
[MAXP_MAX_STORAGE_OFFSET
+ 1] = 0;
742 buf
[MAXP_MAX_FUNCTION_DEFS_OFFSET
] = 0;
743 buf
[MAXP_MAX_FUNCTION_DEFS_OFFSET
+ 1] = 0;
744 buf
[MAXP_MAX_INSTRUCTION_DEFS_OFFSET
] = 0;
745 buf
[MAXP_MAX_INSTRUCTION_DEFS_OFFSET
+ 1] = 0;
746 buf
[MAXP_MAX_STACK_ELEMENTS_OFFSET
] = 0;
747 buf
[MAXP_MAX_STACK_ELEMENTS_OFFSET
+ 1] = 0;
748 buf
[MAXP_MAX_INSTRUCTIONS_OFFSET
] = 0;
749 buf
[MAXP_MAX_INSTRUCTIONS_OFFSET
+ 1] = 0;
751 maxp_table
->checksum
= TA_table_compute_checksum(maxp_table
->buf
,
753 maxp_table
->processed
= 1;
759 /* we build a dummy `DSIG' table only */
762 TA_table_build_DSIG(FT_Byte
** DSIG
)
767 buf
= (FT_Byte
*)malloc(DSIG_LEN
);
769 return FT_Err_Out_Of_Memory
;
777 /* zero signatures */
781 /* permission flags */
792 TA_font_compute_table_offsets(FONT
* font
,
796 FT_ULong offset
= start
;
799 for (i
= 0; i
< font
->num_tables
; i
++)
801 SFNT_Table
* table
= &font
->tables
[i
];
804 table
->offset
= offset
;
806 /* table offsets must be multiples of 4; */
807 /* this also fits the actual buffer lengths */
808 offset
+= (table
->len
+ 3) & ~3;
813 /* If `do_complete' is 0, only return `header_len'. */
816 TA_sfnt_build_TTF_header(SFNT
* sfnt
,
818 FT_Byte
** header_buf
,
819 FT_ULong
* header_len
,
822 SFNT_Table
* tables
= font
->tables
;
824 SFNT_Table_Info
* table_infos
= sfnt
->table_infos
;
825 FT_ULong num_table_infos
= sfnt
->num_table_infos
;
830 FT_Byte
* table_record
;
832 FT_Byte
* head_buf
= NULL
; /* pointer to `head' table */
833 FT_ULong head_checksum
; /* checksum in `head' table */
835 FT_ULong num_tables_in_header
;
839 num_tables_in_header
= 0;
840 for (i
= 0; i
< num_table_infos
; i
++)
842 /* ignore empty tables */
843 if (table_infos
[i
] != MISSING
)
844 num_tables_in_header
++;
847 len
= 12 + 16 * num_tables_in_header
;
853 buf
= (FT_Byte
*)malloc(len
);
855 return FT_Err_Out_Of_Memory
;
863 /* number of tables */
864 buf
[4] = HIGH(num_tables_in_header
);
865 buf
[5] = LOW(num_tables_in_header
);
869 FT_ULong search_range
, entry_selector
, range_shift
;
873 for (i
= 1, j
= 2; j
<= num_tables_in_header
; i
++, j
<<= 1)
876 entry_selector
= i
- 1;
877 search_range
= 0x10 << entry_selector
;
878 range_shift
= (num_tables_in_header
<< 4) - search_range
;
880 buf
[6] = HIGH(search_range
);
881 buf
[7] = LOW(search_range
);
882 buf
[8] = HIGH(entry_selector
);
883 buf
[9] = LOW(entry_selector
);
884 buf
[10] = HIGH(range_shift
);
885 buf
[11] = LOW(range_shift
);
888 /* location of the first table info record */
889 table_record
= &buf
[12];
893 /* loop over all tables */
894 for (i
= 0; i
< num_table_infos
; i
++)
896 SFNT_Table_Info table_info
= table_infos
[i
];
900 /* ignore empty slots */
901 if (table_info
== MISSING
)
904 table
= &tables
[table_info
];
906 if (table
->tag
== TTAG_head
)
908 /* we always reach this IF clause since FreeType would */
909 /* have aborted already if the `head' table were missing */
911 head_buf
= table
->buf
;
913 /* reset checksum in `head' table for recalculation */
920 head_checksum
+= table
->checksum
;
922 table_record
[0] = BYTE1(table
->tag
);
923 table_record
[1] = BYTE2(table
->tag
);
924 table_record
[2] = BYTE3(table
->tag
);
925 table_record
[3] = BYTE4(table
->tag
);
927 table_record
[4] = BYTE1(table
->checksum
);
928 table_record
[5] = BYTE2(table
->checksum
);
929 table_record
[6] = BYTE3(table
->checksum
);
930 table_record
[7] = BYTE4(table
->checksum
);
932 table_record
[8] = BYTE1(table
->offset
);
933 table_record
[9] = BYTE2(table
->offset
);
934 table_record
[10] = BYTE3(table
->offset
);
935 table_record
[11] = BYTE4(table
->offset
);
937 table_record
[12] = BYTE1(table
->len
);
938 table_record
[13] = BYTE2(table
->len
);
939 table_record
[14] = BYTE3(table
->len
);
940 table_record
[15] = BYTE4(table
->len
);
945 /* the font header is complete; compute `head' checksum */
946 head_checksum
+= TA_table_compute_checksum(buf
, len
);
947 head_checksum
= 0xB1B0AFBAUL
- head_checksum
;
949 /* store checksum in `head' table; */
950 head_buf
[8] = BYTE1(head_checksum
);
951 head_buf
[9] = BYTE2(head_checksum
);
952 head_buf
[10] = BYTE3(head_checksum
);
953 head_buf
[11] = BYTE4(head_checksum
);
963 TA_font_build_TTF(FONT
* font
)
965 SFNT
* sfnt
= &font
->sfnts
[0];
970 FT_ULong SFNT_offset
;
981 /* add a dummy `DSIG' table */
983 error
= TA_sfnt_add_table_info(sfnt
);
987 error
= TA_table_build_DSIG(&DSIG_buf
);
991 /* in case of success, `DSIG_buf' gets linked */
992 /* and is eventually freed in `TA_font_unload' */
993 error
= TA_font_add_table(font
,
994 &sfnt
->table_infos
[sfnt
->num_table_infos
- 1],
995 TTAG_DSIG
, DSIG_LEN
, DSIG_buf
);
1002 TA_sfnt_sort_table_info(sfnt
, font
);
1004 /* the first SFNT table immediately follows the header */
1005 (void)TA_sfnt_build_TTF_header(sfnt
, font
, NULL
, &SFNT_offset
, 0);
1006 TA_font_compute_table_offsets(font
, SFNT_offset
);
1008 error
= TA_sfnt_build_TTF_header(sfnt
, font
,
1009 &header_buf
, &header_len
, 1);
1015 tables
= font
->tables
;
1016 num_tables
= font
->num_tables
;
1018 /* get font length from last SFNT table array element */
1019 font
->out_len
= tables
[num_tables
- 1].offset
1020 + ((tables
[num_tables
- 1].len
+ 3) & ~3);
1021 font
->out_buf
= (FT_Byte
*)malloc(font
->out_len
);
1024 error
= FT_Err_Out_Of_Memory
;
1028 memcpy(font
->out_buf
, header_buf
, header_len
);
1030 for (i
= 0; i
< num_tables
; i
++)
1032 SFNT_Table
* table
= &tables
[i
];
1035 /* buffer length is a multiple of 4 */
1036 memcpy(font
->out_buf
+ table
->offset
,
1037 table
->buf
, (table
->len
+ 3) & ~3);
1050 TA_font_build_TTC_header(FONT
* font
,
1051 FT_Byte
** header_buf
,
1052 FT_ULong
* header_len
)
1054 SFNT
* sfnts
= font
->sfnts
;
1055 FT_Long num_sfnts
= font
->num_sfnts
;
1057 SFNT_Table
* tables
= font
->tables
;
1058 FT_ULong num_tables
= font
->num_tables
;
1060 FT_ULong TTF_offset
;
1061 FT_ULong DSIG_offset
;
1070 len
= 24 + 4 * num_sfnts
;
1071 buf
= (FT_Byte
*)malloc(len
);
1073 return FT_Err_Out_Of_Memory
;
1083 /* TTC header version */
1089 /* number of subfonts */
1090 *(p
++) = BYTE1(num_sfnts
);
1091 *(p
++) = BYTE2(num_sfnts
);
1092 *(p
++) = BYTE3(num_sfnts
);
1093 *(p
++) = BYTE4(num_sfnts
);
1095 /* the first TTF subfont header immediately follows the TTC header */
1098 /* loop over all subfonts */
1099 for (i
= 0; i
< num_sfnts
; i
++)
1101 SFNT
* sfnt
= &sfnts
[i
];
1105 TA_sfnt_sort_table_info(sfnt
, font
);
1106 /* only get header length */
1107 (void)TA_sfnt_build_TTF_header(sfnt
, font
, NULL
, &l
, 0);
1109 *(p
++) = BYTE1(TTF_offset
);
1110 *(p
++) = BYTE2(TTF_offset
);
1111 *(p
++) = BYTE3(TTF_offset
);
1112 *(p
++) = BYTE4(TTF_offset
);
1117 /* the first SFNT table immediately follows the subfont TTF headers */
1118 TA_font_compute_table_offsets(font
, TTF_offset
);
1132 /* DSIG offset; in a TTC this is always the last SFNT table */
1133 DSIG_offset
= tables
[num_tables
- 1].offset
;
1135 *(p
++) = BYTE1(DSIG_offset
);
1136 *(p
++) = BYTE2(DSIG_offset
);
1137 *(p
++) = BYTE3(DSIG_offset
);
1138 *(p
++) = BYTE4(DSIG_offset
);
1148 TA_font_build_TTC(FONT
* font
)
1150 SFNT
* sfnts
= font
->sfnts
;
1151 FT_Long num_sfnts
= font
->num_sfnts
;
1154 FT_ULong num_tables
;
1157 SFNT_Table_Info dummy
;
1159 FT_Byte
* TTC_header_buf
;
1160 FT_ULong TTC_header_len
;
1162 FT_Byte
** TTF_header_bufs
;
1163 FT_ULong
* TTF_header_lens
;
1171 /* add a dummy `DSIG' table */
1173 error
= TA_table_build_DSIG(&DSIG_buf
);
1177 /* in case of success, `DSIG_buf' gets linked */
1178 /* and is eventually freed in `TA_font_unload' */
1179 error
= TA_font_add_table(font
, &dummy
, TTAG_DSIG
, DSIG_LEN
, DSIG_buf
);
1186 /* this also computes the SFNT table offsets */
1187 error
= TA_font_build_TTC_header(font
,
1188 &TTC_header_buf
, &TTC_header_len
);
1192 TTF_header_bufs
= (FT_Byte
**)calloc(1, num_sfnts
* sizeof (FT_Byte
*));
1193 if (!TTF_header_bufs
)
1196 TTF_header_lens
= (FT_ULong
*)malloc(num_sfnts
* sizeof (FT_ULong
));
1197 if (!TTF_header_lens
)
1200 for (i
= 0; i
< num_sfnts
; i
++)
1202 error
= TA_sfnt_build_TTF_header(&sfnts
[i
], font
,
1203 &TTF_header_bufs
[i
],
1204 &TTF_header_lens
[i
], 1);
1211 tables
= font
->tables
;
1212 num_tables
= font
->num_tables
;
1214 /* get font length from last SFNT table array element */
1215 font
->out_len
= tables
[num_tables
- 1].offset
1216 + ((tables
[num_tables
- 1].len
+ 3) & ~3);
1217 font
->out_buf
= (FT_Byte
*)malloc(font
->out_len
);
1220 error
= FT_Err_Out_Of_Memory
;
1224 memcpy(font
->out_buf
, TTC_header_buf
, TTC_header_len
);
1226 offset
= TTC_header_len
;
1228 for (i
= 0; i
< num_sfnts
; i
++)
1230 memcpy(font
->out_buf
+ offset
,
1231 TTF_header_bufs
[i
], TTF_header_lens
[i
]);
1233 offset
+= TTF_header_lens
[i
];
1236 for (j
= 0; j
< num_tables
; j
++)
1238 SFNT_Table
* table
= &tables
[j
];
1241 /* buffer length is a multiple of 4 */
1242 memcpy(font
->out_buf
+ table
->offset
,
1243 table
->buf
, (table
->len
+ 3) & ~3);
1249 free(TTC_header_buf
);
1250 if (TTF_header_bufs
)
1252 for (i
= 0; i
< font
->num_sfnts
; i
++)
1253 free(TTF_header_bufs
[i
]);
1254 free(TTF_header_bufs
);
1256 free(TTF_header_lens
);
1263 TA_font_write(FONT
* font
,
1266 if (fwrite(font
->out_buf
, 1, font
->out_len
, out
) != font
->out_len
)
1267 return TA_Err_Invalid_Stream_Write
;
1274 TA_font_unload(FONT
* font
)
1276 /* in case of error it is expected that unallocated pointers */
1277 /* are NULL (and counters are zero) */
1283 ta_loader_done(font
->loader
);
1290 for (i
= 0; i
< font
->num_tables
; i
++)
1292 free(font
->tables
[i
].buf
);
1293 if (font
->tables
[i
].data
)
1295 if (font
->tables
[i
].tag
== TTAG_glyf
)
1297 glyf_Data
* data
= (glyf_Data
*)font
->tables
[i
].data
;
1301 for (j
= 0; j
< data
->num_glyphs
; j
++)
1302 free(data
->glyphs
[j
].buf
);
1316 for (i
= 0; i
< font
->num_sfnts
; i
++)
1318 FT_Done_Face(font
->sfnts
[i
].face
);
1319 free(font
->sfnts
[i
].table_infos
);
1324 FT_Done_FreeType(font
->lib
);
1326 free(font
->out_buf
);
1332 TTF_autohint(FILE* in
,
1340 font
= (FONT
*)calloc(1, sizeof (FONT
));
1342 return FT_Err_Out_Of_Memory
;
1344 error
= TA_font_read(font
, in
);
1348 error
= TA_font_init(font
);
1352 /* loop over subfonts */
1353 for (i
= 0; i
< font
->num_sfnts
; i
++)
1355 SFNT
* sfnt
= &font
->sfnts
[i
];
1358 error
= FT_New_Memory_Face(font
->lib
, font
->in_buf
, font
->in_len
,
1363 error
= TA_sfnt_split_into_SFNT_tables(sfnt
, font
);
1367 error
= TA_sfnt_split_glyf_table(sfnt
, font
);
1372 /* XXX handle subfonts for bytecode tables? */
1374 /* build `cvt ' table */
1375 error
= TA_sfnt_build_cvt_table(&font
->sfnts
[0], font
);
1379 /* build `fpgm' table */
1380 error
= TA_sfnt_build_fpgm_table(&font
->sfnts
[0], font
);
1384 /* build `prep' table */
1386 /* handle all glyphs in a loop */
1387 /* hint the glyph */
1388 /* build bytecode */
1390 /* adjust `maxp' table */
1392 /* loop again over subfonts */
1393 for (i
= 0; i
< font
->num_sfnts
; i
++)
1395 SFNT
* sfnt
= &font
->sfnts
[i
];
1398 error
= TA_sfnt_build_glyf_table(sfnt
, font
);
1401 error
= TA_sfnt_build_loca_table(sfnt
, font
);
1404 error
= TA_sfnt_update_maxp_table(sfnt
, font
);
1409 if (font
->num_sfnts
== 1)
1410 error
= TA_font_build_TTF(font
);
1412 error
= TA_font_build_TTC(font
);
1416 error
= TA_font_write(font
, out
);
1423 TA_font_unload(font
);
1428 /* end of ttfautohint.c */