4 * Copyright (C) 2011-2014 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_font_build_TTC_header(FONT
* font
,
24 SFNT
* sfnts
= font
->sfnts
;
25 FT_Long num_sfnts
= font
->num_sfnts
;
27 SFNT_Table
* tables
= font
->tables
;
28 FT_ULong num_tables
= font
->num_tables
;
40 len
= (font
->have_DSIG
? 24 : 12) + 4 * num_sfnts
;
41 buf
= (FT_Byte
*)malloc(len
);
43 return FT_Err_Out_Of_Memory
;
53 /* TTC header version */
55 *(p
++) = font
->have_DSIG
? 0x02 : 0x01;
59 /* number of subfonts */
60 *(p
++) = BYTE1(num_sfnts
);
61 *(p
++) = BYTE2(num_sfnts
);
62 *(p
++) = BYTE3(num_sfnts
);
63 *(p
++) = BYTE4(num_sfnts
);
65 /* the first TTF subfont header immediately follows the TTC header */
68 /* loop over all subfonts */
69 for (i
= 0; i
< num_sfnts
; i
++)
71 SFNT
* sfnt
= &sfnts
[i
];
75 TA_sfnt_sort_table_info(sfnt
, font
);
76 /* only get header length */
77 (void)TA_sfnt_build_TTF_header(sfnt
, font
, NULL
, &l
, 0);
79 *(p
++) = BYTE1(TTF_offset
);
80 *(p
++) = BYTE2(TTF_offset
);
81 *(p
++) = BYTE3(TTF_offset
);
82 *(p
++) = BYTE4(TTF_offset
);
87 /* the first SFNT table immediately follows the subfont TTF headers */
88 TA_font_compute_table_offsets(font
, TTF_offset
);
104 /* DSIG offset; in a TTC this is always the last SFNT table */
105 DSIG_offset
= tables
[num_tables
- 1].offset
;
107 *(p
++) = BYTE1(DSIG_offset
);
108 *(p
++) = BYTE2(DSIG_offset
);
109 *(p
++) = BYTE3(DSIG_offset
);
110 *(p
++) = BYTE4(DSIG_offset
);
121 TA_font_build_TTC(FONT
* font
)
123 SFNT
* sfnts
= font
->sfnts
;
124 FT_Long num_sfnts
= font
->num_sfnts
;
130 SFNT_Table_Info dummy
;
132 FT_Byte
* TTC_header_buf
;
133 FT_ULong TTC_header_len
;
135 FT_Byte
** TTF_header_bufs
= NULL
;
136 FT_ULong
* TTF_header_lens
= NULL
;
144 /* replace an existing `DSIG' table with a dummy */
148 error
= TA_table_build_DSIG(&DSIG_buf
);
152 /* in case of success, `DSIG_buf' gets linked */
153 /* and is eventually freed in `TA_font_unload' */
154 error
= TA_font_add_table(font
, &dummy
, TTAG_DSIG
, DSIG_LEN
, DSIG_buf
);
162 /* this also computes the SFNT table offsets */
163 error
= TA_font_build_TTC_header(font
,
164 &TTC_header_buf
, &TTC_header_len
);
168 TTF_header_bufs
= (FT_Byte
**)calloc(1, num_sfnts
* sizeof (FT_Byte
*));
169 if (!TTF_header_bufs
)
172 TTF_header_lens
= (FT_ULong
*)malloc(num_sfnts
* sizeof (FT_ULong
));
173 if (!TTF_header_lens
)
176 for (i
= 0; i
< num_sfnts
; i
++)
178 error
= TA_sfnt_build_TTF_header(&sfnts
[i
], font
,
180 &TTF_header_lens
[i
], 1);
187 tables
= font
->tables
;
188 num_tables
= font
->num_tables
;
190 /* get font length from last SFNT table array element */
191 font
->out_len
= tables
[num_tables
- 1].offset
192 + ((tables
[num_tables
- 1].len
+ 3) & ~3);
193 font
->out_buf
= (FT_Byte
*)malloc(font
->out_len
);
196 error
= FT_Err_Out_Of_Memory
;
200 memcpy(font
->out_buf
, TTC_header_buf
, TTC_header_len
);
202 offset
= TTC_header_len
;
204 for (i
= 0; i
< num_sfnts
; i
++)
206 memcpy(font
->out_buf
+ offset
,
207 TTF_header_bufs
[i
], TTF_header_lens
[i
]);
209 offset
+= TTF_header_lens
[i
];
212 for (j
= 0; j
< num_tables
; j
++)
214 SFNT_Table
* table
= &tables
[j
];
217 /* buffer length is a multiple of 4 */
218 memcpy(font
->out_buf
+ table
->offset
,
219 table
->buf
, (table
->len
+ 3) & ~3);
225 free(TTC_header_buf
);
228 for (i
= 0; i
< font
->num_sfnts
; i
++)
229 free(TTF_header_bufs
[i
]);
230 free(TTF_header_bufs
);
232 free(TTF_header_lens
);