added ttf support to pdf2pdf
[swftools.git] / lib / ttf.c
index c17b631..f389ff9 100644 (file)
--- a/lib/ttf.c
+++ b/lib/ttf.c
@@ -268,10 +268,10 @@ static table_head_t*head_new(ttf_t*ttf)
        head->xmax = ttf->glyphs[0].xmax;
        head->ymax = ttf->glyphs[0].ymax;
        for(t=1;t<ttf->num_glyphs;t++) {
-           if(ttf->glyphs[0].xmin < head->xmin) head->xmin = ttf->glyphs[0].xmin;
-           if(ttf->glyphs[0].ymin < head->ymin) head->ymin = ttf->glyphs[0].ymin;
-           if(ttf->glyphs[0].xmax > head->xmax) head->xmax = ttf->glyphs[0].xmax;
-           if(ttf->glyphs[0].ymax > head->ymax) head->ymax = ttf->glyphs[0].ymax;
+           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;
@@ -705,10 +705,10 @@ static table_hea_t*hea_new(ttf_t*ttf)
        for(t=0;t<ttf->num_glyphs;t++) {
            if(ttf->glyphs[t].advance > hea->advanceWidthMax)
                hea->advanceWidthMax = ttf->glyphs[t].advance;
-           if(ttf->glyphs[t].xmin < ttf->hea->minLeftSideBearing)
-               ttf->hea->minLeftSideBearing = ttf->glyphs[t].xmin;
-           if(ttf->glyphs[t].xmax < ttf->hea->minRightSideBearing)
-               ttf->hea->minRightSideBearing = ttf->glyphs[t].xmax;
+           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;
@@ -1439,6 +1439,52 @@ 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;t<count;t++) {
+       U16 platform = readU16(r);
+       U16 encoding = readU16(r);
+       U16 language = readU16(r);
+       U16 name_id = readU16(r);
+       U16 len = readU16(r);
+       U16 offset_2 = readU16(r);
+       /*printf("%d %d %d %d at %d, %d bytes:", platform, encoding, language, name_id, offset+offset_2, len);
+       int s;
+       for(s=0;s<len;s++) {
+           printf("%c", r->mem[offset+offset_2+s]);
+       }
+       printf("\n");*/
+    }
+}
+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;t<len;t++) {
+       writeU8(table, ttf->name[t]);
+    }
+}
+void name_delete(ttf_t*ttf)
+{
+    if(ttf->name)
+       free(ttf->name);
+}
 
 static int ttf_parse_tables(ttf_t*ttf)
 {
@@ -1525,6 +1571,13 @@ 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);
+    }
 
     return 1;
 }
@@ -1566,6 +1619,12 @@ 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);
+    }
     
     table = ttf_addtable(ttf, TAG_HEAD);
     head_write(ttf, table, loca_size);
@@ -1799,6 +1858,7 @@ int main(int argn, const char*argv[])
     //msg("<notice> Loading %s", filename);
     memfile_t*m = memfile_open(filename);
     ttf_t*ttf = ttf_load(m->data, m->len);
+    ttf->name = strdup("testfont");
     if(!ttf) return 1;
     memfile_close(m);
     //ttf_dump(ttf);