From 0d28feacfceb237ca022543c3d31069ab4727e0e Mon Sep 17 00:00:00 2001 From: Werner Lemberg Date: Thu, 10 Mar 2011 18:55:09 +0100 Subject: [PATCH] Add code to split font into its SFNT tables. --- src/ttfautohint.c | 92 +++++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 76 insertions(+), 16 deletions(-) diff --git a/src/ttfautohint.c b/src/ttfautohint.c index 06b45c8..fd14f0f 100644 --- a/src/ttfautohint.c +++ b/src/ttfautohint.c @@ -15,6 +15,13 @@ #include "ttfautohint.h" +typedef struct SFNT_Table_ { + FT_ULong tag; + FT_ULong len; + FT_Byte* buf; +} SFNT_Table; + + TA_Error TTF_autohint(FILE *in, FILE *out) @@ -27,6 +34,9 @@ TTF_autohint(FILE *in, FT_Byte* in_buf; size_t in_len; + SFNT_Table* SFNT_Tables; + FT_ULong glyf_idx; + FT_Error error; FT_ULong i; @@ -77,29 +87,75 @@ TTF_autohint(FILE *in, if (error) goto Err2; - for (i = 0; i < num_tables; i++) + /* allocate three more slots for the tables we will eventually add */ + num_tables += 3; + SFNT_Tables = (SFNT_Table*)calloc(1, num_tables * sizeof (SFNT_Table)); + if (!SFNT_Tables) { - FT_ULong tag, dummy; + error = FT_Err_Out_Of_Memory; + goto Err2; + } + + /* collect SFNT table data */ + glyf_idx = num_tables - 3; + for (i = 0; i < num_tables - 3; i++) + { + FT_ULong tag, len; - error = FT_Sfnt_Table_Info(face, i, &tag, &dummy); + error = FT_Sfnt_Table_Info(face, i, &tag, &len); if (error && error != FT_Err_Table_Missing) - goto Err2; - if (tag == TTAG_glyf) - break; + goto Err3; + + if (!error) + { + if (tag == TTAG_glyf) + glyf_idx = i; + + /* ignore tables which we are going to create by ourselves */ + if (!(tag == TTAG_fpgm + || tag == TTAG_prep + || tag == TTAG_cvt)) + { + SFNT_Tables[i].tag = tag; + SFNT_Tables[i].len = len; + } + } } - /* no `glyf' table; this can't be a TTF with outlines */ - if (i == num_tables) + /* no (non-empty) `glyf' table; this can't be a TTF with outlines */ + if (glyf_idx == num_tables - 3) { error = FT_Err_Invalid_Argument; - goto Err2; + goto Err3; } - /* split font into SFNT tables */ - /* strip `fpgm' table */ - /* strip `prep' table */ - /* strip `cvt ' table */ + + /*** split font into SFNT tables ***/ + + { + SFNT_Table* st = SFNT_Tables; + + + for (i = 0; i < num_tables - 3; i++) + { + if (st->len) + { + st->buf = (FT_Byte*)malloc(st->len); + if (!st->buf) + { + error = FT_Err_Out_Of_Memory; + goto Err4; + } + + error = FT_Load_Sfnt_Table(face, st->tag, 0, st->buf, &st->len); + if (error) + goto Err4; + + st++; + } + } + } /* compute global hints */ /* construct `fpgm' table */ @@ -114,18 +170,22 @@ TTF_autohint(FILE *in, /* construct `glyf' table */ /* build font from SFNT tables */ - /* write font from memory */ + /* write font from memory */ error = TA_Err_Ok; +Err4: + for (i = 0; i < num_tables; i++) + free(SFNT_Tables[i].buf); +Err3: + free(SFNT_Tables); Err2: FT_Done_Face(face); - Err1: FT_Done_FreeType(lib); - Err: free(in_buf); + return error; } -- 2.11.4.GIT