X-Git-Url: http://git.asbjorn.biz/?p=swftools.git;a=blobdiff_plain;f=lib%2Fttf.c;h=28bdee2ab13f2e3ab2aae05cd0c6c3a9b84324d7;hp=c637f6e615d42b1f520d303f2fed6779c77051ab;hb=219ea5ad983fc389d894c967528b451ea321b8c6;hpb=f4d875b670dfcdfd8b62c396c88098dc1ba7f8d8 diff --git a/lib/ttf.c b/lib/ttf.c index c637f6e..28bdee2 100644 --- a/lib/ttf.c +++ b/lib/ttf.c @@ -22,9 +22,9 @@ #include #include #include -#include "bitio.h" #include "log.h" #include "os.h" +#include "q.h" #include "mem.h" #include "ttf.h" @@ -126,13 +126,13 @@ static void expand(ttf_table_t*w, int newsize) w->memsize = v1>v2?v1:v2; w->data = rfx_realloc(w->data, w->memsize); } -static void writeU8(ttf_table_t*w, unsigned char b) +static inline void writeU8(ttf_table_t*w, unsigned char b) { if(w->memsizelen+1) expand(w, w->len+1); w->data[w->len++] = b; } -static void writeU16(ttf_table_t*w, unsigned short v) +static inline void writeU16(ttf_table_t*w, unsigned short v) { if(w->memsizelen+2) expand(w, w->len+2); @@ -140,7 +140,7 @@ static void writeU16(ttf_table_t*w, unsigned short v) w->data[w->len++] = v; } #define writeS16 writeU16 -static void writeU32(ttf_table_t*w, unsigned long v) +static inline void writeU32(ttf_table_t*w, unsigned long v) { if(w->memsizelen+4) expand(w, w->len+4); @@ -149,7 +149,7 @@ static void writeU32(ttf_table_t*w, unsigned long v) w->data[w->len++] = v>>8; w->data[w->len++] = v; } -static void writeBlock(ttf_table_t*w, void*data, int len) +static inline void writeBlock(ttf_table_t*w, void*data, int len) { if(w->memsizelen+len) expand(w, w->len+len); @@ -258,16 +258,182 @@ static void ttf_table_dump(ttf_table_t*t, const char*prefix) hexdump(t->data, t->len, prefix); } +static table_head_t*head_new(ttf_t*ttf) +{ + table_head_t*head = rfx_calloc(sizeof(table_head_t)); + head->units_per_em = 1024; + int t; + if(ttf->num_glyphs) { + head->xmin = ttf->glyphs[0].xmin; + head->ymin = ttf->glyphs[0].ymin; + head->xmax = ttf->glyphs[0].xmax; + head->ymax = ttf->glyphs[0].ymax; + for(t=1;tnum_glyphs;t++) { + if(ttf->glyphs[t].xmin < head->xmin) head->xmin = ttf->glyphs[t].xmin; + if(ttf->glyphs[t].ymin < head->ymin) head->ymin = ttf->glyphs[t].ymin; + if(ttf->glyphs[t].xmax > head->xmax) head->xmax = ttf->glyphs[t].xmax; + if(ttf->glyphs[t].ymax > head->ymax) head->ymax = ttf->glyphs[t].ymax; + } + } + head->macStyle = 0; + head->lowest_readable_size = 8; // not sure what font renderers actually do with this + head->dir_hint = 0; + return head; +} +static int head_parse(ttf_t*ttf, memreader_t*r) +{ + ttf->head = rfx_calloc(sizeof(table_head_t)); + U32 version = readU32(r); + if(version!=VERSION_1_0) + msg(" Font HEAD has unknown version %08x", version); + U32 revision = readU32(r); + U32 checksum2 = readU32(r); + U32 magic = readU32(r); + if(magic!=0x5f0f3cf5) + msg(" Font HEAD has unknown magic number %08x", magic); + ttf->head->flags = readU16(r); + ttf->head->units_per_em = readU16(r); + readU32(r);readU32(r); //created + readU32(r);readU32(r); //modified + ttf->head->xmin = readU16(r); + ttf->head->ymin = readU16(r); + ttf->head->xmax = readU16(r); + ttf->head->ymax = readU16(r); + ttf->head->macStyle = readU16(r); + ttf->head->lowest_readable_size = readU16(r); //in pixels + ttf->head->dir_hint = readS16(r); + int loc_index = readS16(r); //used in 'loca' table + if(loc_index>1) + msg(" loca index format %d unknown", loc_index); + U16 glyph_data_format = readS16(r); + if(glyph_data_format!=0) + msg(" Font glyph data format unknown: %04x", glyph_data_format); + if(r->pos < r->size) { + msg(" Leftover bytes (%d) in HEAD tag", r->size - r->pos); + } + return loc_index; +} +static void head_write(ttf_t*ttf, ttf_table_t*w, int loca_size) +{ + writeU32(w, 0x10000); + writeU32(w, 0x10000); + writeU32(w, 0); //checksum + writeU32(w, 0x5f0f3cf5); //magic + writeU16(w, ttf->head->flags); + writeU16(w, ttf->head->units_per_em); + writeU32(w, 0);writeU32(w, 0); //created + writeU32(w, 0);writeU32(w, 0); //modified + writeU16(w, ttf->head->xmin); + writeU16(w, ttf->head->ymin); + writeU16(w, ttf->head->xmax); + writeU16(w, ttf->head->ymax); + writeU16(w, ttf->head->macStyle); + writeU16(w, ttf->head->lowest_readable_size); + writeS16(w, ttf->head->dir_hint); + writeS16(w, loca_size); //loca index size (32 bit) + writeS16(w, 0); //glyph data format +} +static void head_dump(ttf_t*ttf) +{ + printf("head->flags: %d\n", ttf->head->flags); + printf("head->units_per_em: %d\n", ttf->head->units_per_em); + printf("head->xmin: %d\n", ttf->head->xmin); + printf("head->ymin: %d\n", ttf->head->ymin); + printf("head->xmax: %d\n", ttf->head->xmax); + printf("head->ymax: %d\n", ttf->head->ymax); + printf("head->macStyle: %d\n", ttf->head->macStyle); + printf("head->lowest_readable_size: %d\n", ttf->head->lowest_readable_size); + printf("head->dir_hint: %d\n", ttf->head->dir_hint); +} +static void head_delete(ttf_t*ttf) +{ + if(ttf->head) { + free(ttf->head); + ttf->head=0; + } +} + +static table_os2_t*os2_new(ttf_t*ttf) +{ + table_os2_t*os2 = rfx_calloc(sizeof(table_os2_t)); + if(ttf->num_glyphs) { + int average_width=0; + int t; + for(t=0;tnum_glyphs;t++) { + average_width += (ttf->glyphs[t].advance + ttf->glyphs[t].bearing); + } + os2->xAvgCharWidth = average_width / ttf->num_glyphs; + } + + /* that's what everybody seems to fill in */ + os2->usWeightClass = 400; + os2->usWidthClass = 5; + + if(ttf->head) { + int advance = (ttf->head->xmax - ttf->head->xmin)/2; + int height = (ttf->head->xmax - ttf->head->xmin); + int ymid = height/2; + /* I do believe a sane font rendering engine will actually use + the font advance here- the subscript/superscript position will + not be the same for each glyph */ + os2->ySuperscriptXSize = os2->ySubscriptXSize = (ttf->head->xmax - ttf->head->xmin)/2; + os2->ySuperscriptYSize = os2->ySubscriptYSize = (ttf->head->ymax - ttf->head->ymin)/2; + os2->ySubscriptXOffset = advance; + os2->ySubscriptYOffset = 0; + os2->ySuperscriptXOffset = advance; + os2->ySuperscriptYOffset = (ttf->head->ymax - ttf->head->ymin)/2; + os2->yStrikeoutSize = ttf->head->units_per_em / 10; + os2->yStrikeoutPosition = ymid; + os2->usWinAscent = ttf->head->ymax; + os2->usWinDescent = ttf->head->ymin<0?0:ttf->head->ymin; + os2->sxHeight = ymid; + os2->sCapHeight = height*2/3; + } + os2->panose_Weight = 4; + + /* strictly speaking we'd have to set 92/64 bits in these tables, depending on + what parts of the unicode table is filled. (e.g. bit 90 = tibetan). */ + os2->ulCharRange[0] = 1; + os2->ulCharRange[1] = 0; + os2->ulCharRange[2] = 0; + os2->ulCharRange[3] = 0; + os2->ulCodePageRange1 = 1; + os2->ulCodePageRange2 = 0; + + if(ttf->unicode_size) { + int min,max; + for(min=0;minunicode_size;min++) + if(ttf->unicode[min]) break; + for(max=ttf->unicode_size-1;max>=0;max--) + if(ttf->unicode[max]) break; + if(min<=max) { + os2->fsFirstCharIndex = min; + os2->fsLastCharIndex = max; + } + } + os2->sTypoAscender = ttf->ascent; + os2->sTypoDescender = ttf->descent; + os2->sTypoLineGap = ttf->lineGap; + + os2->usDefaultChar = 0; + os2->usBreakChar = (ttf->unicode_size>0x20 && ttf->unicode[0x20])?0x20:0; + os2->usMaxContext = 0; // we don't use ligatures yet + return os2; +} static table_os2_t*os2_parse(memreader_t*r) { table_os2_t*os2 = rfx_calloc(sizeof(table_os2_t)); U16 version = readU16(r); - if(version!=0 && version!=1 && version!=2) + /* 0 = TrueType 1.5 + 1 = TrueType 1.66 + 2 = OpenType 1.2 + 3 = OpenType 1.4 */ + if(version!=0 && version!=1 && version!=2 && version!=3) msg(" Unknown OS2 version: %04x", version); os2->xAvgCharWidth = readS16(r); os2->usWeightClass = readU16(r); os2->usWidthClass = readU16(r); - os2->fsType = readU16(r); + readU16(r); //fstype os2->ySubscriptXSize = readU16(r); os2->ySubscriptYSize = readU16(r); os2->ySubscriptXOffset = readU16(r); @@ -293,10 +459,7 @@ static table_os2_t*os2_parse(memreader_t*r) os2->ulCharRange[1] = readU32(r); os2->ulCharRange[2] = readU32(r); os2->ulCharRange[3] = readU32(r); - os2->achVendID[0] = readU8(r); - os2->achVendID[1] = readU8(r); - os2->achVendID[2] = readU8(r); - os2->achVendID[3] = readU8(r); + readU32(r); //vendor os2->fsSelection = readU16(r); os2->fsFirstCharIndex = readU16(r); os2->fsLastCharIndex = readU16(r); @@ -314,10 +477,13 @@ static table_os2_t*os2_parse(memreader_t*r) os2->usDefaultChar = readU16(r); os2->usBreakChar = readU16(r); os2->usMaxContext = readU16(r); + + if(r->pos < r->size) { + msg(" Leftover bytes (%d) in OS2 tag", r->size - r->pos); + } return os2; } - -static os2_write(ttf_t*ttf, ttf_table_t*w) +static void os2_write(ttf_t*ttf, ttf_table_t*w) { table_os2_t*os2 = ttf->os2; U16 version=1; @@ -328,7 +494,7 @@ static os2_write(ttf_t*ttf, ttf_table_t*w) writeS16(w, os2->xAvgCharWidth); writeU16(w, os2->usWeightClass); writeU16(w, os2->usWidthClass); - writeU16(w, os2->fsType); + writeU16(w, 0); //fstype writeU16(w, os2->ySubscriptXSize); writeU16(w, os2->ySubscriptYSize); writeU16(w, os2->ySubscriptXOffset); @@ -354,10 +520,7 @@ static os2_write(ttf_t*ttf, ttf_table_t*w) writeU32(w, os2->ulCharRange[1]); writeU32(w, os2->ulCharRange[2]); writeU32(w, os2->ulCharRange[3]); - writeU8(w, os2->achVendID[0]); - writeU8(w, os2->achVendID[1]); - writeU8(w, os2->achVendID[2]); - writeU8(w, os2->achVendID[3]); + writeU32(w, 0x53434244); //vendor writeU16(w, os2->fsSelection); writeU16(w, os2->fsFirstCharIndex); writeU16(w, os2->fsLastCharIndex); @@ -376,21 +539,13 @@ static os2_write(ttf_t*ttf, ttf_table_t*w) writeU16(w, os2->usBreakChar); writeU16(w, os2->usMaxContext); } -static void os2_delete(ttf_t*ttf) -{ - if(ttf->os2) - free(ttf->os2); - ttf->os2=0; -} - -static os2_dump(ttf_t*ttf) +static void os2_dump(ttf_t*ttf) { table_os2_t*os2 = ttf->os2; if(!os2) return; printf("os2->xAvgCharWidth: %d\n", os2->xAvgCharWidth); printf("os2->usWeightClass: %d\n", os2->usWeightClass); printf("os2->usWidthClass: %d\n", os2->usWidthClass); - printf("os2->fsType: %d\n", os2->fsType); printf("os2->ySubscriptXSize: %d\n", os2->ySubscriptXSize); printf("os2->ySubscriptYSize: %d\n", os2->ySubscriptYSize); printf("os2->ySubscriptXOffset: %d\n", os2->ySubscriptXOffset); @@ -416,10 +571,6 @@ static os2_dump(ttf_t*ttf) printf("os2->ulCharRange[1]: %d\n", os2->ulCharRange[1]); printf("os2->ulCharRange[2]: %d\n", os2->ulCharRange[2]); printf("os2->ulCharRange[3]: %d\n", os2->ulCharRange[3]); - printf("os2->achVendID[0]: %d\n", os2->achVendID[0]); - printf("os2->achVendID[1]: %d\n", os2->achVendID[1]); - printf("os2->achVendID[2]: %d\n", os2->achVendID[2]); - printf("os2->achVendID[3]: %d\n", os2->achVendID[3]); printf("os2->fsSelection: %d\n", os2->fsSelection); printf("os2->fsFirstCharIndex: %d\n", os2->fsFirstCharIndex); printf("os2->fsLastCharIndex: %d\n", os2->fsLastCharIndex); @@ -436,80 +587,39 @@ static os2_dump(ttf_t*ttf) printf("os2->usBreakChar: %d\n", os2->usBreakChar); printf("os2->usMaxContext: %d\n", os2->usMaxContext); } - -static int head_parse(ttf_t*ttf, memreader_t*r) +static void os2_delete(ttf_t*ttf) { - ttf->head = rfx_calloc(sizeof(table_head_t)); - U32 version = readU32(r); - if(version!=VERSION_1_0) - msg(" Font HEAD has unknown version %08x", version); - U32 revision = readU32(r); - if(revision!=VERSION_1_0) - msg(" Font HEAD has unknown revision %08x", revision); - U32 checksum2 = readU32(r); - U32 magic = readU32(r); - if(magic!=0x5f0f3cf5) - msg(" Font HEAD has unknown magic number %08x", magic); - ttf->head->flags = readU16(r); - ttf->head->units_per_em = readU16(r); - readU32(r);readU32(r); //created - readU32(r);readU32(r); //modified - ttf->head->xmin = readU16(r); - ttf->head->ymin = readU16(r); - ttf->head->xmax = readU16(r); - ttf->head->ymax = readU16(r); - ttf->head->macStyle = readU16(r); - ttf->head->lowest_readable_size = readU16(r); //in pixels - ttf->head->dir_hint = readS16(r); - int loc_index = readS16(r); //used in 'loca' table - if(loc_index>1) - msg(" loca index format %d unknown", loc_index); - U16 glyph_data_format = readS16(r); - if(glyph_data_format!=0) - msg(" Font glyph data format unknown: %04x", glyph_data_format); - return loc_index; + if(ttf->os2) + free(ttf->os2); + ttf->os2=0; } -static void head_write(ttf_t*ttf, ttf_table_t*w, int loca_size) +static table_maxp_t*maxp_new(ttf_t*ttf) { - writeU32(w, 0x10000); - writeU32(w, 0x10000); - writeU32(w, 0); //checksum - writeU32(w, 0x5f0f3cf5); //magic - writeU16(w, ttf->head->flags); - writeU16(w, ttf->head->units_per_em); - writeU32(w, 0);writeU32(w, 0); //created - writeU32(w, 0);writeU32(w, 0); //modified - writeU16(w, ttf->head->xmin); - writeU16(w, ttf->head->ymin); - writeU16(w, ttf->head->xmax); - writeU16(w, ttf->head->ymax); - writeU16(w, ttf->head->macStyle); - writeU16(w, ttf->head->lowest_readable_size); - writeS16(w, ttf->head->dir_hint); - writeS16(w, loca_size); //loca index size (32 bit) - writeS16(w, 0); //glyph data format -} -static void head_dump(ttf_t*ttf) -{ - printf("head->flags: %d\n", ttf->head->flags); - printf("head->units_per_em: %d\n", ttf->head->units_per_em); - printf("head->xmin: %d\n", ttf->head->xmin); - printf("head->ymin: %d\n", ttf->head->ymin); - printf("head->xmax: %d\n", ttf->head->xmax); - printf("head->ymax: %d\n", ttf->head->ymax); - printf("head->macStyle: %d\n", ttf->head->macStyle); - printf("head->lowest_readable_size: %d\n", ttf->head->lowest_readable_size); - printf("head->dir_hint: %d\n", ttf->head->dir_hint); -} -static void head_delete(ttf_t*ttf) -{ - if(ttf->head) { - free(ttf->head); - ttf->head=0; + table_maxp_t*maxp = rfx_calloc(sizeof(table_maxp_t)); + int t; + if(ttf->num_glyphs) { + int max = 0; + for(t=0;tnum_glyphs;t++) { + if(ttf->glyphs[t].num_points>max) + max = ttf->glyphs[t].num_points; + int contours = 0; + int s; + for(s=0;sglyphs[t].num_points;s++) { + if(ttf->glyphs[t].points[s].flags&GLYPH_CONTOUR_END) + contours++; + } + if(maxp->maxContours < contours) + maxp->maxContours = contours; + } + maxp->maxPoints = max; + + /* we don't generate composite glyphs yet */ + maxp->maxComponentPoints = 0; + maxp->maxComponentContours = 0; } + return maxp; } - static table_maxp_t* maxp_parse(ttf_t*ttf, memreader_t*r) { U32 version = readU32(r); @@ -517,7 +627,10 @@ static table_maxp_t* maxp_parse(ttf_t*ttf, memreader_t*r) /* according to freetype, older fonts (version<0x10000) apparently only contain the number of glyphs. this is rather rare, though. */ - if(version<0x10000) return 0; + if(version<0x10000 && r->size==6) return 0; + + if(r->size<32) + msg(" Truncated maxp table (version %d)", version); table_maxp_t*maxp = rfx_calloc(sizeof(table_maxp_t)); maxp->maxPoints = readU16(r); @@ -535,10 +648,15 @@ static table_maxp_t* maxp_parse(ttf_t*ttf, memreader_t*r) maxp->maxComponentDepth = readU16(r); return maxp; } - static void maxp_write(ttf_t*ttf, ttf_table_t*w) { table_maxp_t*maxp = ttf->maxp; + if(!maxp) { + /* version 0.5 simplified maxp table */ + writeU32(w, 0x00005000); + writeU16(w, ttf->num_glyphs); + return; + } writeU32(w, 0x10000); //version writeU16(w, ttf->num_glyphs); writeU16(w, maxp->maxPoints); @@ -555,7 +673,6 @@ static void maxp_write(ttf_t*ttf, ttf_table_t*w) writeU16(w, maxp->maxComponentElements); writeU16(w, maxp->maxComponentDepth); } - static void maxp_dump(ttf_t*ttf) { table_maxp_t*maxp = ttf->maxp; @@ -574,7 +691,6 @@ static void maxp_dump(ttf_t*ttf) printf("maxp->maxComponentElements: %d\n", maxp->maxComponentElements); printf("maxp->maxComponentDepth: %d\n", maxp->maxComponentDepth); } - static void maxp_delete(ttf_t*ttf) { if(ttf->maxp) @@ -582,14 +698,33 @@ static void maxp_delete(ttf_t*ttf) ttf->maxp=0; } - +static table_hea_t*hea_new(ttf_t*ttf) +{ + table_hea_t*hea = rfx_calloc(sizeof(table_hea_t)); + if(ttf->num_glyphs) { + int t; + for(t=0;tnum_glyphs;t++) { + if(ttf->glyphs[t].advance > hea->advanceWidthMax) + hea->advanceWidthMax = ttf->glyphs[t].advance; + if(ttf->glyphs[t].xmin < hea->minLeftSideBearing) + hea->minLeftSideBearing = ttf->glyphs[t].xmin; + if(ttf->glyphs[t].xmax < hea->minRightSideBearing) + hea->minRightSideBearing = ttf->glyphs[t].xmax; + int width = ttf->glyphs[t].xmax - ttf->glyphs[t].xmin; + if(width > hea->xMaxExtent) + hea->xMaxExtent = width; + } + /* TODO: caret */ + } + return hea; +} static int hea_parse(memreader_t*r, ttf_t*ttf) { table_hea_t*hea = ttf->hea = rfx_calloc(sizeof(table_hea_t)); U32 version = readU32(r); - hea->ascent = readS16(r); - hea->descent = readS16(r); - hea->lineGap = readS16(r); + ttf->ascent = readS16(r); + ttf->descent = readS16(r); + ttf->lineGap = readS16(r); hea->advanceWidthMax = readU16(r); hea->minLeftSideBearing = readS16(r); hea->minRightSideBearing = readS16(r); @@ -612,14 +747,13 @@ static int hea_parse(memreader_t*r, ttf_t*ttf) } return num_advances; } - static table_hea_t*hea_write(ttf_t*ttf, ttf_table_t*w, int num_advances) { table_hea_t*hea = ttf->hea; writeU32(w, 0x00010000); - writeS16(w, hea->ascent); - writeS16(w, hea->descent); - writeS16(w, hea->lineGap); + writeS16(w, ttf->ascent); + writeS16(w, ttf->descent); + writeS16(w, ttf->lineGap); writeU16(w, hea->advanceWidthMax); writeS16(w, hea->minLeftSideBearing); writeS16(w, hea->minRightSideBearing); @@ -638,10 +772,11 @@ static table_hea_t*hea_write(ttf_t*ttf, ttf_table_t*w, int num_advances) static void hea_dump(ttf_t*ttf) { table_hea_t*hea = ttf->hea; + if(!hea) return; const char*dir = ttf->is_vertical?"v":"h"; - printf("%shea->ascent: %d\n", dir, hea->ascent); - printf("%shea->descent: %d\n", dir, hea->descent); - printf("%shea->lineGap: %d\n", dir, hea->lineGap); + printf("%shea->ascent: %d\n", dir, ttf->ascent); + printf("%shea->descent: %d\n", dir, ttf->descent); + printf("%shea->lineGap: %d\n", dir, ttf->lineGap); printf("%shea->advanceWidthMax: %d\n", dir, hea->advanceWidthMax); printf("%shea->minLeftSideBearing: %d\n", dir, hea->minLeftSideBearing); printf("%shea->minRightSideBearing: %d\n", dir, hea->minRightSideBearing); @@ -658,7 +793,7 @@ static void hea_delete(ttf_t*ttf) } } -static void hmtx_parse(memreader_t*r, ttf_t*ttf, int num_advances) +static void mtx_parse(memreader_t*r, ttf_t*ttf, int num_advances) { U16 old_advance = 0; int t; @@ -701,21 +836,30 @@ static int mtx_write(ttf_t*ttf, ttf_table_t*w) } return num_advances; } + static U32*loca_parse(memreader_t*r, ttf_t*ttf, int size) { int t; int num = ttf->num_glyphs+1; U32*locations = rfx_calloc(num*sizeof(U32)); + U32 lastloc = 0; + U32 loc = 0; + char warn_unsorted = 1; if(size) { if(num*4 > r->size) { - msg(" Short 'loca' table (32 bit)"); + msg(" Short 'loca' table (32 bit): %d/%d", r->size/4, num); num=r->size/4; } if(num*4 < r->size) { msg(" Extraneous data (%d bytes) in 'loca' table (32 bit)", r->size-num*4); } for(t=0;t loc && warn_unsorted) { + msg(" Unsorted 'loca' table (32 bit)"); + warn_unsorted=0; + } + lastloc = loc; } } else { if(num*2 > r->size) { @@ -726,7 +870,12 @@ static U32*loca_parse(memreader_t*r, ttf_t*ttf, int size) msg(" Extraneous data (%d bytes) in 'loca' table (16 bit)", r->size-num*2); } for(t=0;t loc && warn_unsorted) { + msg(" Unsorted 'loca' table"); + warn_unsorted=0; + } + lastloc = loc; } } return locations; @@ -754,6 +903,7 @@ static int loca_write(ttf_t*ttf, ttf_table_t*w, U32*locations) return 0; } } + static int parse_simple_glyph(ttf_t*ttf, memreader_t*r, int num_contours, int glyphnr) { ttfglyph_t*glyph = &ttf->glyphs[glyphnr]; @@ -863,11 +1013,14 @@ static int parse_simple_glyph(ttf_t*ttf, memreader_t*r, int num_contours, int gl static void glyf_parse(memreader_t*rr, ttf_t*ttf, U32*loca) { int t; - char warn_about_compound_glyphs=1; + char warn_about_compound_glyphs=0; for(t=0;tnum_glyphs;t++) { INIT_READ(r, rr->mem, rr->size, loca[t]); + if(loca[t]==loca[t+1] || loca[t]==r.size) + continue; //empty glyph if(r.pos+10>r.size) { - msg(" Unexpected end of glyph array (or bad loca entry %d/%d)", loca[t], r.size); + msg(" Truncated glyph entry %d/%d (or bad loca entry %d/%d, next loca: %d)", + t, ttf->num_glyphs, loca[t], r.size, loca[t+1]); break; } S16 num_contours = readS16(&r); @@ -887,7 +1040,6 @@ static void glyf_parse(memreader_t*rr, ttf_t*ttf, U32*loca) } } - void write_simple_glyph(ttf_table_t*w, ttfglyph_t*g) { /* endpoints array */ @@ -979,7 +1131,6 @@ void write_simple_glyph(ttf_table_t*w, ttfglyph_t*g) else if(dy) writeS16(w, dy); } } - U32* glyf_write(ttf_t* ttf, ttf_table_t*w) { U32*locations = malloc(sizeof(U32)*(ttf->num_glyphs+1)); @@ -1016,6 +1167,7 @@ U32* glyf_write(ttf_t* ttf, ttf_table_t*w) } void glyf_dump(ttf_t* ttf) { + if(!ttf->glyphs) return; int t; for(t=0;tnum_glyphs;t++) { ttfglyph_t*g = &ttf->glyphs[t]; @@ -1034,7 +1186,6 @@ void glyf_dump(ttf_t* ttf) hexdump(g->code, g->code_size, " "); } } - void glyf_delete(ttf_t* ttf) { if(!ttf->glyphs) @@ -1064,12 +1215,12 @@ static void grow_unicode(ttf_t*ttf, int index) } ttf->unicode_size = size; } - void cmap_parse(memreader_t*r, ttf_t*ttf) { readU16(r); // version (0) int num_subtables = readU16(r); int t; + char warn=1; if(r->pos+num_subtables*8 > r->size) { msg(" CMap overflow"); num_subtables = (r->size-r->pos)/8; @@ -1127,6 +1278,7 @@ void cmap_parse(memreader_t*r, ttf_t*ttf) INIT_READ(r_delta, t.mem, t.size, t.pos+2+segment_count*4); INIT_READ(r_range, t.mem, t.size, t.pos+2+segment_count*6); int glyphmap_start = t.pos+2+segment_count*8; + int glyphmap_size = t.size - glyphmap_start; int s; for(s=0;sunicode[u] = (u + delta) & 0xffff; } } else { - INIT_READ(g, t.mem, t.size, r_range.pos-2+range); + int pos = r_range.pos-2+range; + if(warn && pos+end-start+1 > t.size) { + msg(" glyphmap index out of bounds (%d-%d/%d)", pos, pos+end-start, t.size); + warn=0; + } + INIT_READ(g, t.mem, t.size, pos); for(u=start;u<=end;u++) { ttf->unicode[u] = readU16(&g); } @@ -1157,7 +1315,6 @@ void cmap_parse(memreader_t*r, ttf_t*ttf) } } } - static int segment_size(unicode_t*unicode, int pos, int size) { int s; @@ -1177,7 +1334,6 @@ static int segment_size(unicode_t*unicode, int pos, int size) return size-1; return s; } - void cmap_write(ttf_t* ttf, ttf_table_t*w) { writeU16(w, 0); //version @@ -1212,6 +1368,11 @@ void cmap_write(ttf_t* ttf, ttf_table_t*w) pos = s+1; num_segments++; } + + num_segments++; // account for 0xffff mapping + + int glyphmap_start = w->len+2+num_segments*8; + int t; int end_pos = w->len; for(t=0;tlen; for(t=0;tdata[num_segments_pos]=(num_segments*2)>>8; + w->data[num_segments_pos+1]=(num_segments*2); - w->data[num_segments_pos]=num_segments>>8; - w->data[num_segments_pos+1]=num_segments; - pos=0; num_segments = 0; while(pos < ttf->unicode_size) { @@ -1242,7 +1403,7 @@ void cmap_write(ttf_t* ttf, ttf_table_t*w) U16 delta = ttf->unicode[pos]-pos; char do_delta=1; for(s=pos+1;s<=end;s++) { - U16 delta2 = ttf->unicode[pos]-pos; + U16 delta2 = ttf->unicode[s]-s; if(delta2!=delta) { do_delta=0; break; @@ -1253,7 +1414,7 @@ void cmap_write(ttf_t* ttf, ttf_table_t*w) range = 0; } else { delta = 0; - range = w->len - range_pos+num_segments*2; + range = w->len - range_pos; for(s=pos;s<=end;s++) { writeU16(w, ttf->unicode[s]); } @@ -1265,6 +1426,18 @@ void cmap_write(ttf_t* ttf, ttf_table_t*w) num_segments++; pos = end+1; } + + /* write out a mapping from 0xffff to 0- seems to be required + by some libraries (e.g. fonttools) */ + w->data[end_pos++]=0xff; + w->data[end_pos++]=0xff; + w->data[start_pos++]=0xff; + w->data[start_pos++]=0xff; + w->data[delta_pos++]=0; + w->data[delta_pos++]=1; + w->data[range_pos++]=0; + w->data[range_pos++]=0; + w->data[length_pos]=(w->len-20)>>8; w->data[length_pos+1]=w->len-20; } @@ -1276,6 +1449,92 @@ void cmap_delete(ttf_t*ttf) } ttf->unicode_size=0; } +void name_parse(memreader_t*r, ttf_t*ttf) +{ + U16 format = readU16(r); + U16 count = readU16(r); + U16 offset = readU16(r); + + int t; + for(t=0;tname) + free(ttf->name); + ttf->name = strdup_n(&r->mem[offset+offset_2], len); + } + } +} +void name_write(ttf_t*ttf, ttf_table_t*table) +{ + writeU16(table, 0); //format + writeU16(table, 1); //count + int offset = 18; + writeU16(table, offset); //offset + + writeU16(table, 1); //platform id + writeU16(table, 0); //encoding id + writeU16(table, 0); //language + writeU16(table, 4); //4: full name + int len = strlen(ttf->name); + writeU16(table, len); + writeU16(table, table->len+2 - offset); + int t; + for(t=0;tname[t]); + } +} +void name_delete(ttf_t*ttf) +{ + if(ttf->name) { + free(ttf->name); + ttf->name=0; + } +} + +static table_post_t*post_new(ttf_t*ttf) +{ + table_post_t*post = rfx_calloc(sizeof(table_post_t)); + return post; +} +void post_parse(memreader_t*r, ttf_t*ttf) +{ + table_post_t*post = ttf->post = rfx_calloc(sizeof(table_post_t)); + U16 format = readU16(r); + post->italic_angle = readU16(r); + post->underline_position = readU16(r); + post->underline_thickness = readU16(r); + U16 is_monospaced = readU16(r); + readU16(r); // min mem 42 + readU16(r); + readU16(r); // min mem 1 + readU16(r); +} +void post_write(ttf_t*ttf, ttf_table_t*table) +{ + table_post_t*post = ttf->post; + writeU32(table, 0x00030000); + writeU32(table, post->italic_angle); + writeU16(table, post->underline_position); + writeU16(table, post->underline_thickness); + writeU32(table, 0); //is monospaced TODO + writeU32(table, 0); //min mem 42 + writeU32(table, 0); + writeU32(table, 0); //min mem 1 + writeU32(table, 0); +} +void post_delete(ttf_t*ttf) +{ + if(ttf->post) { + free(ttf->post); + ttf->post = 0; + } +} static int ttf_parse_tables(ttf_t*ttf) { @@ -1321,7 +1580,7 @@ static int ttf_parse_tables(ttf_t*ttf) table = ttf_find_table(ttf, TAG_HMTX); if(table) { INIT_READ(m, table->data, table->len, 0); - hmtx_parse(&m, ttf, num_advances); + mtx_parse(&m, ttf, num_advances); ttf_table_delete(ttf, table); } } else { @@ -1335,7 +1594,7 @@ static int ttf_parse_tables(ttf_t*ttf) table = ttf_find_table(ttf, TAG_VMTX); if(table) { INIT_READ(m, table->data, table->len, 0); - hmtx_parse(&m, ttf, num_advances); + mtx_parse(&m, ttf, num_advances); ttf_table_delete(ttf, table); } } else { @@ -1362,13 +1621,27 @@ static int ttf_parse_tables(ttf_t*ttf) cmap_parse(&m, ttf); ttf_table_delete(ttf, table); } + + table = ttf_find_table(ttf, TAG_NAME); + if(table) { + INIT_READ(m, table->data, table->len, 0); + name_parse(&m, ttf); + ttf_table_delete(ttf, table); + } + + table = ttf_find_table(ttf, TAG_POST); + if(table) { + INIT_READ(m, table->data, table->len, 0); + post_parse(&m, ttf); + ttf_table_delete(ttf, table); + } return 1; } static void ttf_collapse_tables(ttf_t*ttf) { ttf_table_t*table; - + table = ttf_addtable(ttf, TAG_MAXP); maxp_write(ttf, table); maxp_delete(ttf); @@ -1403,13 +1676,30 @@ static void ttf_collapse_tables(ttf_t*ttf) loca_size = loca_write(ttf, table, locations); free(locations); } + + if(ttf->name) { + table = ttf_addtable(ttf, TAG_NAME); + name_write(ttf, table); + name_delete(ttf); + } + if(ttf->post) { + table = ttf_addtable(ttf, TAG_POST); + post_write(ttf, table); + post_delete(ttf); + } table = ttf_addtable(ttf, TAG_HEAD); head_write(ttf, table, loca_size); head_delete(ttf); } -ttf_t* load_ttf(void*data, int length) +ttf_t*ttf_new() +{ + ttf_t*ttf = rfx_calloc(sizeof(ttf_t)); + ttf->version = VERSION_1_0; + return ttf; +} +ttf_t* ttf_load(void*data, int length) { INIT_READ(r,data,length, 0); @@ -1475,7 +1765,7 @@ ttf_t* load_ttf(void*data, int length) ttf_table_t*table = ttf_addtable(ttf, tag); table->data = mem; table->len = table->memsize = len; - +#if 0 U32 checksum2 = ttf_table_checksum(table); if(checksum2!=checksum) { msg(" Checksum mismatch in tag %02x%02x%02x%02x %c%c%c%c (%d bytes) %08x!=%08x", @@ -1483,7 +1773,7 @@ ttf_t* load_ttf(void*data, int length) (tag>>24)&0xff, (tag>>16)&0xff, (tag>>8)&0xff, (tag)&0xff, len, checksum2, checksum); } - +#endif } } free(table_data); @@ -1493,7 +1783,19 @@ ttf_t* load_ttf(void*data, int length) return ttf; } - +void ttf_create_truetype_tables(ttf_t*ttf) +{ + if(!ttf->head) + ttf->head = head_new(ttf); + if(!ttf->maxp) + ttf->maxp = maxp_new(ttf); + if(!ttf->hea) + ttf->hea = hea_new(ttf); + if(!ttf->os2) + ttf->os2 = os2_new(ttf); + if(!ttf->post) + ttf->post = post_new(ttf); +} ttf_table_t* ttf_write(ttf_t*ttf) { ttf_collapse_tables(ttf); @@ -1562,7 +1864,6 @@ ttf_table_t* ttf_write(ttf_t*ttf) checksum2[3] = checksum>>0; return file; } - void ttf_save(ttf_t*ttf, const char*filename) { ttf_table_t* t = ttf_write(ttf); @@ -1575,7 +1876,6 @@ void ttf_save(ttf_t*ttf, const char*filename) fclose(fi); ttf_table_delete(0, t); } - void ttf_dump(ttf_t*ttf) { msg(" Truetype file version %08x%s", ttf->version, ttf->version == OPENTYPE?" (opentype)":""); @@ -1595,7 +1895,7 @@ void ttf_dump(ttf_t*ttf) maxp_dump(ttf); glyf_dump(ttf); } -void ttf_destroy(ttf_t*ttf) +void ttf_destroy_tables(ttf_t*ttf) { ttf_table_t*table = ttf->tables; while(table) { @@ -1604,11 +1904,22 @@ void ttf_destroy(ttf_t*ttf) free(table); table = next; } + ttf->tables = 0; +} +void ttf_reduce(ttf_t*ttf) +{ + ttf_destroy_tables(ttf); +} +void ttf_destroy(ttf_t*ttf) +{ + ttf_destroy_tables(ttf); maxp_delete(ttf); os2_delete(ttf); head_delete(ttf); hea_delete(ttf); glyf_delete(ttf); + post_delete(ttf); + name_delete(ttf); free(ttf); } @@ -1621,13 +1932,19 @@ int main(int argn, const char*argv[]) filename = argv[1]; //msg(" Loading %s", filename); memfile_t*m = memfile_open(filename); - ttf_t*ttf = load_ttf(m->data, m->len); + ttf_t*ttf = ttf_load(m->data, m->len); + if(!ttf) { + msg(" Couldn't load %s", filename); + return 1; + } + ttf_reduce(ttf); + ttf->name = strdup("testfont"); if(!ttf) return 1; memfile_close(m); //ttf_dump(ttf); //printf("os2 version: %04x (%d), maxp size: %d\n", // ttf->os2->version, ttf->os2->size, ttf->maxp->size); - ttf_save(ttf, "comic2.ttf"); + ttf_save(ttf, "testfont.ttf"); ttf_destroy(ttf); return 0;