| | |
| |
|
| | |
| | |
| | |
| | |
| | |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| |
|
| | #include <cstdio> |
| | #include <cstdlib> |
| | #include <cstring> |
| | #include "pprdrv.h" |
| | #include "truetype.h" |
| | #include <sstream> |
| | #ifdef _POSIX_C_SOURCE |
| | # undef _POSIX_C_SOURCE |
| | #endif |
| | #ifndef _AIX |
| | #ifdef _XOPEN_SOURCE |
| | # undef _XOPEN_SOURCE |
| | #endif |
| | #endif |
| | #include <Python.h> |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| |
|
| | |
| | |
| | |
| | ULONG getULONG(BYTE *p) |
| | { |
| | int x; |
| | ULONG val=0; |
| |
|
| | for (x=0; x<4; x++) |
| | { |
| | val *= 0x100; |
| | val += p[x]; |
| | } |
| |
|
| | return val; |
| | } |
| |
|
| | |
| | |
| | |
| | USHORT getUSHORT(BYTE *p) |
| | { |
| | int x; |
| | USHORT val=0; |
| |
|
| | for (x=0; x<2; x++) |
| | { |
| | val *= 0x100; |
| | val += p[x]; |
| | } |
| |
|
| | return val; |
| | } |
| |
|
| | |
| | |
| | |
| | |
| | Fixed getFixed(BYTE *s) |
| | { |
| | Fixed val={0,0}; |
| |
|
| | val.whole = ((s[0] * 256) + s[1]); |
| | val.fraction = ((s[2] * 256) + s[3]); |
| |
|
| | return val; |
| | } |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | BYTE *GetTable(struct TTFONT *font, const char *name) |
| | { |
| | BYTE *ptr; |
| | ULONG x; |
| | debug("GetTable(file,font,\"%s\")",name); |
| |
|
| | |
| | ptr = font->offset_table + 12; |
| | x=0; |
| | while (true) |
| | { |
| | if ( strncmp((const char*)ptr,name,4) == 0 ) |
| | { |
| | ULONG offset,length; |
| | BYTE *table; |
| |
|
| | offset = getULONG( ptr + 8 ); |
| | length = getULONG( ptr + 12 ); |
| | table = (BYTE*)calloc( sizeof(BYTE), length + 2 ); |
| |
|
| | try |
| | { |
| | debug("Loading table \"%s\" from offset %d, %d bytes",name,offset,length); |
| |
|
| | if ( fseek( font->file, (long)offset, SEEK_SET ) ) |
| | { |
| | throw TTException("TrueType font may be corrupt (reason 3)"); |
| | } |
| |
|
| | if ( fread(table,sizeof(BYTE),length,font->file) != (sizeof(BYTE) * length)) |
| | { |
| | throw TTException("TrueType font may be corrupt (reason 4)"); |
| | } |
| | } |
| | catch (TTException& ) |
| | { |
| | free(table); |
| | throw; |
| | } |
| | |
| | table[length] = '\0'; |
| | table[length + 1] = '\0'; |
| | return table; |
| | } |
| |
|
| | x++; |
| | ptr += 16; |
| | if (x == font->numTables) |
| | { |
| | throw TTException("TrueType font is missing table"); |
| | } |
| | } |
| |
|
| | } |
| |
|
| | static void utf16be_to_ascii(char *dst, char *src, size_t length) { |
| | ++src; |
| | for (; *src != 0 && length; dst++, src += 2, --length) { |
| | *dst = *src; |
| | } |
| | } |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | void Read_name(struct TTFONT *font) |
| | { |
| | BYTE *table_ptr,*ptr2; |
| | int numrecords; |
| | BYTE *strings; |
| | int x; |
| | int platform; |
| | int nameid; |
| | int offset,length; |
| | debug("Read_name()"); |
| |
|
| | table_ptr = NULL; |
| |
|
| | |
| | |
| | |
| | for (char **ptr = &(font->PostName); ptr != NULL; ) |
| | { |
| | *ptr = (char*) calloc(sizeof(char), strlen("unknown")+1); |
| | strcpy(*ptr, "unknown"); |
| | if (ptr == &(font->PostName)) ptr = &(font->FullName); |
| | else if (ptr == &(font->FullName)) ptr = &(font->FamilyName); |
| | else if (ptr == &(font->FamilyName)) ptr = &(font->Version); |
| | else if (ptr == &(font->Version)) ptr = &(font->Style); |
| | else ptr = NULL; |
| | } |
| | font->Copyright = font->Trademark = (char*)NULL; |
| |
|
| | table_ptr = GetTable(font, "name"); |
| | try |
| | { |
| | numrecords = getUSHORT( table_ptr + 2 ); |
| | strings = table_ptr + getUSHORT( table_ptr + 4 ); |
| |
|
| | ptr2 = table_ptr + 6; |
| | for (x=0; x < numrecords; x++,ptr2+=12) |
| | { |
| | platform = getUSHORT(ptr2); |
| | nameid = getUSHORT(ptr2+6); |
| | length = getUSHORT(ptr2+8); |
| | offset = getUSHORT(ptr2+10); |
| | debug("platform %d, encoding %d, language 0x%x, name %d, offset %d, length %d", |
| | platform,encoding,language,nameid,offset,length); |
| |
|
| | |
| | if ( platform == 1 && nameid == 0 ) |
| | { |
| | font->Copyright = (char*)calloc(sizeof(char),length+1); |
| | strncpy(font->Copyright,(const char*)strings+offset,length); |
| | font->Copyright[length]='\0'; |
| | replace_newlines_with_spaces(font->Copyright); |
| | debug("font->Copyright=\"%s\"",font->Copyright); |
| | continue; |
| | } |
| |
|
| |
|
| | |
| | if ( platform == 1 && nameid == 1 ) |
| | { |
| | free(font->FamilyName); |
| | font->FamilyName = (char*)calloc(sizeof(char),length+1); |
| | strncpy(font->FamilyName,(const char*)strings+offset,length); |
| | font->FamilyName[length]='\0'; |
| | replace_newlines_with_spaces(font->FamilyName); |
| | debug("font->FamilyName=\"%s\"",font->FamilyName); |
| | continue; |
| | } |
| |
|
| |
|
| | |
| | if ( platform == 1 && nameid == 2 ) |
| | { |
| | free(font->Style); |
| | font->Style = (char*)calloc(sizeof(char),length+1); |
| | strncpy(font->Style,(const char*)strings+offset,length); |
| | font->Style[length]='\0'; |
| | replace_newlines_with_spaces(font->Style); |
| | debug("font->Style=\"%s\"",font->Style); |
| | continue; |
| | } |
| |
|
| |
|
| | |
| | if ( platform == 1 && nameid == 4 ) |
| | { |
| | free(font->FullName); |
| | font->FullName = (char*)calloc(sizeof(char),length+1); |
| | strncpy(font->FullName,(const char*)strings+offset,length); |
| | font->FullName[length]='\0'; |
| | replace_newlines_with_spaces(font->FullName); |
| | debug("font->FullName=\"%s\"",font->FullName); |
| | continue; |
| | } |
| |
|
| |
|
| | |
| | if ( platform == 1 && nameid == 5 ) |
| | { |
| | free(font->Version); |
| | font->Version = (char*)calloc(sizeof(char),length+1); |
| | strncpy(font->Version,(const char*)strings+offset,length); |
| | font->Version[length]='\0'; |
| | replace_newlines_with_spaces(font->Version); |
| | debug("font->Version=\"%s\"",font->Version); |
| | continue; |
| | } |
| |
|
| |
|
| | |
| | if ( platform == 1 && nameid == 6 ) |
| | { |
| | free(font->PostName); |
| | font->PostName = (char*)calloc(sizeof(char),length+1); |
| | strncpy(font->PostName,(const char*)strings+offset,length); |
| | font->PostName[length]='\0'; |
| | replace_newlines_with_spaces(font->PostName); |
| | debug("font->PostName=\"%s\"",font->PostName); |
| | continue; |
| | } |
| |
|
| | |
| | if ( platform == 3 && nameid == 6 ) |
| | { |
| | free(font->PostName); |
| | font->PostName = (char*)calloc(sizeof(char),length+1); |
| | utf16be_to_ascii(font->PostName, (char *)strings+offset, length); |
| | font->PostName[length/2]='\0'; |
| | replace_newlines_with_spaces(font->PostName); |
| | debug("font->PostName=\"%s\"",font->PostName); |
| | continue; |
| | } |
| |
|
| |
|
| | |
| | if ( platform == 1 && nameid == 7 ) |
| | { |
| | font->Trademark = (char*)calloc(sizeof(char),length+1); |
| | strncpy(font->Trademark,(const char*)strings+offset,length); |
| | font->Trademark[length]='\0'; |
| | replace_newlines_with_spaces(font->Trademark); |
| | debug("font->Trademark=\"%s\"",font->Trademark); |
| | continue; |
| | } |
| | } |
| | } |
| | catch (TTException& ) |
| | { |
| | free(table_ptr); |
| | throw; |
| | } |
| |
|
| | free(table_ptr); |
| | } |
| |
|
| | |
| | |
| | |
| | void ttfont_header(TTStreamWriter& stream, struct TTFONT *font) |
| | { |
| | int VMMin; |
| | int VMMax; |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | if ( font->target_type == PS_TYPE_42 || |
| | font->target_type == PS_TYPE_42_3_HYBRID) |
| | { |
| | stream.printf("%%!PS-TrueTypeFont-%d.%d-%d.%d\n", |
| | font->TTVersion.whole, font->TTVersion.fraction, |
| | font->MfrRevision.whole, font->MfrRevision.fraction); |
| | } |
| |
|
| | |
| | else |
| | { |
| | stream.putline("%!PS-Adobe-3.0 Resource-Font"); |
| | } |
| |
|
| | |
| | stream.printf("%%%%Title: %s\n",font->FullName); |
| |
|
| | |
| | if ( font->Copyright != (char*)NULL ) |
| | { |
| | stream.printf("%%%%Copyright: %s\n",font->Copyright); |
| | } |
| |
|
| | |
| | if ( font->target_type == PS_TYPE_42 ) |
| | { |
| | stream.putline("%%Creator: Converted from TrueType to type 42 by PPR"); |
| | } |
| | else if (font->target_type == PS_TYPE_42_3_HYBRID) |
| | { |
| | stream.putline("%%Creator: Converted from TypeType to type 42/type 3 hybrid by PPR"); |
| | } |
| | else |
| | { |
| | stream.putline("%%Creator: Converted from TrueType to type 3 by PPR"); |
| | } |
| |
|
| | |
| | if ( font->target_type == PS_TYPE_42 || font->target_type == PS_TYPE_42_3_HYBRID) |
| | { |
| | VMMin = (int)getULONG( font->post_table + 16 ); |
| | VMMax = (int)getULONG( font->post_table + 20 ); |
| | if ( VMMin > 0 && VMMax > 0 ) |
| | stream.printf("%%%%VMUsage: %d %d\n",VMMin,VMMax); |
| | } |
| |
|
| | |
| | |
| | if (font->target_type == PS_TYPE_42) |
| | { |
| | stream.putline("15 dict begin"); |
| | } |
| | else |
| | { |
| | stream.putline("25 dict begin"); |
| |
|
| | |
| | stream.putline("/_d{bind def}bind def"); |
| | stream.putline("/_m{moveto}_d"); |
| | stream.putline("/_l{lineto}_d"); |
| | stream.putline("/_cl{closepath eofill}_d"); |
| | stream.putline("/_c{curveto}_d"); |
| | stream.putline("/_sc{7 -1 roll{setcachedevice}{pop pop pop pop pop pop}ifelse}_d"); |
| | stream.putline("/_e{exec}_d"); |
| | } |
| |
|
| | stream.printf("/FontName /%s def\n",font->PostName); |
| | stream.putline("/PaintType 0 def"); |
| |
|
| | if (font->target_type == PS_TYPE_42 || font->target_type == PS_TYPE_42_3_HYBRID) |
| | { |
| | stream.putline("/FontMatrix[1 0 0 1 0 0]def"); |
| | } |
| | else |
| | { |
| | stream.putline("/FontMatrix[.001 0 0 .001 0 0]def"); |
| | } |
| |
|
| | stream.printf("/FontBBox[%d %d %d %d]def\n",font->llx-1,font->lly-1,font->urx,font->ury); |
| | if (font->target_type == PS_TYPE_42 || font->target_type == PS_TYPE_42_3_HYBRID) |
| | { |
| | stream.printf("/FontType 42 def\n", font->target_type ); |
| | } |
| | else |
| | { |
| | stream.printf("/FontType 3 def\n", font->target_type ); |
| | } |
| | } |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | void ttfont_encoding(TTStreamWriter& stream, struct TTFONT *font, std::vector<int>& glyph_ids, font_type_enum target_type) |
| | { |
| | if (target_type == PS_TYPE_3 || target_type == PS_TYPE_42_3_HYBRID) |
| | { |
| | stream.printf("/Encoding [ "); |
| |
|
| | for (std::vector<int>::const_iterator i = glyph_ids.begin(); |
| | i != glyph_ids.end(); ++i) |
| | { |
| | const char* name = ttfont_CharStrings_getname(font, *i); |
| | stream.printf("/%s ", name); |
| | } |
| |
|
| | stream.printf("] def\n"); |
| | } |
| | else |
| | { |
| | stream.putline("/Encoding StandardEncoding def"); |
| | } |
| | } |
| |
|
| | |
| | |
| | |
| | void ttfont_FontInfo(TTStreamWriter& stream, struct TTFONT *font) |
| | { |
| | Fixed ItalicAngle; |
| |
|
| | |
| | |
| | |
| | |
| | stream.putline("/FontInfo 10 dict dup begin"); |
| |
|
| | |
| | stream.printf("/FamilyName (%s) def\n",font->FamilyName); |
| | stream.printf("/FullName (%s) def\n",font->FullName); |
| |
|
| | if ( font->Copyright != (char*)NULL || font->Trademark != (char*)NULL ) |
| | { |
| | stream.printf("/Notice (%s", |
| | font->Copyright != (char*)NULL ? font->Copyright : ""); |
| | stream.printf("%s%s) def\n", |
| | font->Trademark != (char*)NULL ? " " : "", |
| | font->Trademark != (char*)NULL ? font->Trademark : ""); |
| | } |
| |
|
| | |
| | stream.printf("/Weight (%s) def\n",font->Style); |
| |
|
| | |
| | stream.printf("/Version (%s) def\n",font->Version); |
| |
|
| | |
| | ItalicAngle = getFixed( font->post_table + 4 ); |
| | stream.printf("/ItalicAngle %d.%d def\n",ItalicAngle.whole,ItalicAngle.fraction); |
| | stream.printf("/isFixedPitch %s def\n", getULONG( font->post_table + 12 ) ? "true" : "false" ); |
| | stream.printf("/UnderlinePosition %d def\n", (int)getFWord( font->post_table + 8 ) ); |
| | stream.printf("/UnderlineThickness %d def\n", (int)getFWord( font->post_table + 10 ) ); |
| | stream.putline("end readonly def"); |
| | } |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | int string_len; |
| | int line_len; |
| | bool in_string; |
| |
|
| | |
| | |
| | |
| | void sfnts_start(TTStreamWriter& stream) |
| | { |
| | stream.puts("/sfnts[<"); |
| | in_string=true; |
| | string_len=0; |
| | line_len=8; |
| | } |
| |
|
| | |
| | |
| | |
| | void sfnts_pputBYTE(TTStreamWriter& stream, BYTE n) |
| | { |
| | static const char hexdigits[]="0123456789ABCDEF"; |
| |
|
| | if (!in_string) |
| | { |
| | stream.put_char('<'); |
| | string_len=0; |
| | line_len++; |
| | in_string=true; |
| | } |
| |
|
| | stream.put_char( hexdigits[ n / 16 ] ); |
| | stream.put_char( hexdigits[ n % 16 ] ); |
| | string_len++; |
| | line_len+=2; |
| |
|
| | if (line_len > 70) |
| | { |
| | stream.put_char('\n'); |
| | line_len=0; |
| | } |
| |
|
| | } |
| |
|
| | |
| | |
| | |
| | void sfnts_pputUSHORT(TTStreamWriter& stream, USHORT n) |
| | { |
| | sfnts_pputBYTE(stream, n / 256); |
| | sfnts_pputBYTE(stream, n % 256); |
| | } |
| |
|
| | |
| | |
| | |
| | void sfnts_pputULONG(TTStreamWriter& stream, ULONG n) |
| | { |
| | int x1,x2,x3; |
| |
|
| | x1 = n % 256; |
| | n /= 256; |
| | x2 = n % 256; |
| | n /= 256; |
| | x3 = n % 256; |
| | n /= 256; |
| |
|
| | sfnts_pputBYTE(stream, n); |
| | sfnts_pputBYTE(stream, x3); |
| | sfnts_pputBYTE(stream, x2); |
| | sfnts_pputBYTE(stream, x1); |
| | } |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | void sfnts_end_string(TTStreamWriter& stream) |
| | { |
| | if (in_string) |
| | { |
| | string_len=0; |
| |
|
| | #ifdef DEBUG_TRUETYPE_INLINE |
| | puts("\n% dummy byte:\n"); |
| | #endif |
| |
|
| | sfnts_pputBYTE(stream, 0); |
| | stream.put_char('>'); |
| | line_len++; |
| | } |
| | in_string=false; |
| | } |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | void sfnts_new_table(TTStreamWriter& stream, ULONG length) |
| | { |
| | if ( (string_len + length) > 65528 ) |
| | sfnts_end_string(stream); |
| | } |
| |
|
| | |
| | |
| | |
| | |
| | |
| | void sfnts_glyf_table(TTStreamWriter& stream, struct TTFONT *font, ULONG oldoffset, ULONG correct_total_length) |
| | { |
| | ULONG off; |
| | ULONG length; |
| | int c; |
| | ULONG total=0; |
| | int x; |
| | bool loca_is_local=false; |
| | debug("sfnts_glyf_table(font,%d)", (int)correct_total_length); |
| |
|
| | if (font->loca_table == NULL) |
| | { |
| | font->loca_table = GetTable(font,"loca"); |
| | loca_is_local = true; |
| | } |
| |
|
| | |
| | fseek( font->file, oldoffset, SEEK_SET ); |
| |
|
| | |
| | for (x=0; x < font->numGlyphs; x++) |
| | { |
| | |
| | if (font->indexToLocFormat == 0) |
| | { |
| | off = getUSHORT( font->loca_table + (x * 2) ); |
| | off *= 2; |
| | length = getUSHORT( font->loca_table + ((x+1) * 2) ); |
| | length *= 2; |
| | length -= off; |
| | } |
| | else |
| | { |
| | off = getULONG( font->loca_table + (x * 4) ); |
| | length = getULONG( font->loca_table + ((x+1) * 4) ); |
| | length -= off; |
| | } |
| | debug("glyph length=%d",(int)length); |
| |
|
| | |
| | sfnts_new_table( stream, (int)length ); |
| |
|
| | |
| | |
| | |
| | |
| | if ( length % 2 ) { |
| | throw TTException("TrueType font contains a 'glyf' table without 2 byte padding"); |
| | } |
| |
|
| | |
| | while ( length-- ) |
| | { |
| | if ( (c = fgetc(font->file)) == EOF ) { |
| | throw TTException("TrueType font may be corrupt (reason 6)"); |
| | } |
| |
|
| | sfnts_pputBYTE(stream, c); |
| | total++; |
| | } |
| |
|
| | } |
| |
|
| | if (loca_is_local) |
| | { |
| | free(font->loca_table); |
| | font->loca_table = NULL; |
| | } |
| |
|
| | |
| | while ( total < correct_total_length ) |
| | { |
| | sfnts_pputBYTE(stream, 0); |
| | total++; |
| | } |
| |
|
| | } |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | void ttfont_sfnts(TTStreamWriter& stream, struct TTFONT *font) |
| | { |
| | static const char *table_names[] = |
| | { |
| | |
| | "cvt ", |
| | "fpgm", |
| | "glyf", |
| | "head", |
| | "hhea", |
| | "hmtx", |
| | "loca", |
| | "maxp", |
| | "prep" |
| | } ; |
| |
|
| | struct |
| | { |
| | ULONG oldoffset; |
| | ULONG newoffset; |
| | ULONG length; |
| | ULONG checksum; |
| | } tables[9]; |
| |
|
| | BYTE *ptr; |
| | ULONG x,y; |
| | int c; |
| | int diff; |
| | ULONG nextoffset; |
| | int count; |
| |
|
| | ptr = font->offset_table + 12; |
| | nextoffset=0; |
| | count=0; |
| |
|
| | |
| | |
| | |
| | |
| | ULONG num_tables_read = 0; |
| | for (x = 0; x < 9; x++) { |
| | do { |
| | if (num_tables_read < font->numTables) { |
| | |
| | diff = strncmp((char*)ptr, table_names[x], 4); |
| |
|
| | if (diff > 0) { |
| | tables[x].length = 0; |
| | diff = 0; |
| | } else if (diff < 0) { |
| | ptr += 16; |
| | num_tables_read++; |
| | } else if (diff == 0) { |
| | tables[x].newoffset = nextoffset; |
| | tables[x].checksum = getULONG( ptr + 4 ); |
| | tables[x].oldoffset = getULONG( ptr + 8 ); |
| | tables[x].length = getULONG( ptr + 12 ); |
| | nextoffset += ( ((tables[x].length + 3) / 4) * 4 ); |
| | count++; |
| | ptr += 16; |
| | num_tables_read++; |
| | } |
| | } else { |
| | |
| | |
| | tables[x].length = 0; |
| | break; |
| | } |
| | } while (diff != 0); |
| |
|
| | } |
| |
|
| | |
| | sfnts_start(stream); |
| |
|
| | |
| | |
| | ptr = font->offset_table; |
| | for (x=0; x < 4; x++) |
| | { |
| | sfnts_pputBYTE( stream, *(ptr++) ); |
| | } |
| |
|
| | |
| | sfnts_pputUSHORT(stream, count); |
| |
|
| | int search_range = 1; |
| | int entry_sel = 0; |
| |
|
| | while (search_range <= count) { |
| | search_range <<= 1; |
| | entry_sel++; |
| | } |
| | entry_sel = entry_sel > 0 ? entry_sel - 1 : 0; |
| | search_range = (search_range >> 1) * 16; |
| | int range_shift = count * 16 - search_range; |
| |
|
| | sfnts_pputUSHORT(stream, search_range); |
| | sfnts_pputUSHORT(stream, entry_sel); |
| | sfnts_pputUSHORT(stream, range_shift); |
| |
|
| | debug("only %d tables selected",count); |
| |
|
| | |
| | for (x=0; x < 9; x++) |
| | { |
| | if ( tables[x].length == 0 ) |
| | { |
| | continue; |
| | } |
| |
|
| | |
| | sfnts_pputBYTE( stream, table_names[x][0] ); |
| | sfnts_pputBYTE( stream, table_names[x][1] ); |
| | sfnts_pputBYTE( stream, table_names[x][2] ); |
| | sfnts_pputBYTE( stream, table_names[x][3] ); |
| |
|
| | |
| | sfnts_pputULONG( stream, tables[x].checksum ); |
| |
|
| | |
| | sfnts_pputULONG( stream, tables[x].newoffset + 12 + (count * 16) ); |
| |
|
| | |
| | sfnts_pputULONG( stream, tables[x].length ); |
| | } |
| |
|
| | |
| | for (x=0; x < 9; x++) |
| | { |
| | if ( tables[x].length == 0 ) |
| | { |
| | continue; |
| | } |
| | debug("emmiting table '%s'",table_names[x]); |
| |
|
| | |
| | if ( strcmp(table_names[x],"glyf")==0 ) |
| | { |
| | sfnts_glyf_table(stream,font,tables[x].oldoffset,tables[x].length); |
| | } |
| | else |
| | { |
| | |
| | if ( tables[x].length > 65535 ) |
| | { |
| | throw TTException("TrueType font has a table which is too long"); |
| | } |
| |
|
| | |
| | sfnts_new_table(stream, tables[x].length); |
| |
|
| | |
| | fseek( font->file, tables[x].oldoffset, SEEK_SET ); |
| |
|
| | |
| | for ( y=0; y < tables[x].length; y++ ) |
| | { |
| | if ( (c = fgetc(font->file)) == EOF ) |
| | { |
| | throw TTException("TrueType font may be corrupt (reason 7)"); |
| | } |
| |
|
| | sfnts_pputBYTE(stream, c); |
| | } |
| | } |
| |
|
| | |
| | y=tables[x].length; |
| | while ( (y % 4) != 0 ) |
| | { |
| | sfnts_pputBYTE(stream, 0); |
| | y++; |
| | #ifdef DEBUG_TRUETYPE_INLINE |
| | puts("\n% pad byte:\n"); |
| | #endif |
| | } |
| |
|
| | } |
| |
|
| | |
| | sfnts_end_string(stream); |
| | stream.putline("]def"); |
| | } |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | const char *Apple_CharStrings[]= |
| | { |
| | ".notdef",".null","nonmarkingreturn","space","exclam","quotedbl","numbersign", |
| | "dollar","percent","ampersand","quotesingle","parenleft","parenright", |
| | "asterisk","plus", "comma","hyphen","period","slash","zero","one","two", |
| | "three","four","five","six","seven","eight","nine","colon","semicolon", |
| | "less","equal","greater","question","at","A","B","C","D","E","F","G","H","I", |
| | "J","K", "L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z", |
| | "bracketleft","backslash","bracketright","asciicircum","underscore","grave", |
| | "a","b","c","d","e","f","g","h","i","j","k", "l","m","n","o","p","q","r","s", |
| | "t","u","v","w","x","y","z","braceleft","bar","braceright","asciitilde", |
| | "Adieresis","Aring","Ccedilla","Eacute","Ntilde","Odieresis","Udieresis", |
| | "aacute","agrave","acircumflex","adieresis","atilde","aring","ccedilla", |
| | "eacute","egrave","ecircumflex","edieresis","iacute","igrave","icircumflex", |
| | "idieresis","ntilde","oacute","ograve","ocircumflex","odieresis","otilde", |
| | "uacute","ugrave","ucircumflex","udieresis","dagger","degree","cent", |
| | "sterling","section","bullet","paragraph","germandbls","registered", |
| | "copyright","trademark","acute","dieresis","notequal","AE","Oslash", |
| | "infinity","plusminus","lessequal","greaterequal","yen","mu","partialdiff", |
| | "summation","product","pi","integral","ordfeminine","ordmasculine","Omega", |
| | "ae","oslash","questiondown","exclamdown","logicalnot","radical","florin", |
| | "approxequal","Delta","guillemotleft","guillemotright","ellipsis", |
| | "nobreakspace","Agrave","Atilde","Otilde","OE","oe","endash","emdash", |
| | "quotedblleft","quotedblright","quoteleft","quoteright","divide","lozenge", |
| | "ydieresis","Ydieresis","fraction","currency","guilsinglleft","guilsinglright", |
| | "fi","fl","daggerdbl","periodcentered","quotesinglbase","quotedblbase", |
| | "perthousand","Acircumflex","Ecircumflex","Aacute","Edieresis","Egrave", |
| | "Iacute","Icircumflex","Idieresis","Igrave","Oacute","Ocircumflex","apple", |
| | "Ograve","Uacute","Ucircumflex","Ugrave","dotlessi","circumflex","tilde", |
| | "macron","breve","dotaccent","ring","cedilla","hungarumlaut","ogonek","caron", |
| | "Lslash","lslash","Scaron","scaron","Zcaron","zcaron","brokenbar","Eth","eth", |
| | "Yacute","yacute","Thorn","thorn","minus","multiply","onesuperior", |
| | "twosuperior","threesuperior","onehalf","onequarter","threequarters","franc", |
| | "Gbreve","gbreve","Idot","Scedilla","scedilla","Cacute","cacute","Ccaron", |
| | "ccaron","dmacron","markingspace","capslock","shift","propeller","enter", |
| | "markingtabrtol","markingtabltor","control","markingdeleteltor", |
| | "markingdeletertol","option","escape","parbreakltor","parbreakrtol", |
| | "newpage","checkmark","linebreakltor","linebreakrtol","markingnobreakspace", |
| | "diamond","appleoutline" |
| | }; |
| |
|
| | |
| | |
| | |
| | |
| | const char *ttfont_CharStrings_getname(struct TTFONT *font, int charindex) |
| | { |
| | int GlyphIndex; |
| | static char temp[80]; |
| | char *ptr; |
| | ULONG len; |
| |
|
| | Fixed post_format; |
| |
|
| | |
| | post_format = getFixed( font->post_table ); |
| |
|
| | if ( post_format.whole != 2 || post_format.fraction != 0 ) |
| | { |
| | |
| | |
| | |
| | PyOS_snprintf(temp, 80, "uni%08x", charindex); |
| | return temp; |
| | } |
| |
|
| | GlyphIndex = (int)getUSHORT( font->post_table + 34 + (charindex * 2) ); |
| |
|
| | if ( GlyphIndex <= 257 ) |
| | { |
| | return Apple_CharStrings[GlyphIndex]; |
| | } |
| | else |
| | { |
| | |
| | GlyphIndex -= 258; |
| |
|
| | |
| | ptr = (char*)(font->post_table + 34 + (font->numGlyphs * 2)); |
| |
|
| | len = (ULONG)*(ptr++); |
| | while (GlyphIndex--) |
| | { |
| | |
| | ptr += len; |
| | len = (ULONG)*(ptr++); |
| | } |
| |
|
| | if ( len >= sizeof(temp) ) |
| | { |
| | throw TTException("TrueType font file contains a very long PostScript name"); |
| | } |
| |
|
| | strncpy(temp,ptr,len); |
| | temp[len]='\0'; |
| |
|
| | return temp; |
| | } |
| | } |
| |
|
| | |
| | |
| | |
| | void ttfont_CharStrings(TTStreamWriter& stream, struct TTFONT *font, std::vector<int>& glyph_ids) |
| | { |
| | Fixed post_format; |
| |
|
| | |
| | post_format = getFixed( font->post_table ); |
| |
|
| | |
| | stream.printf("/CharStrings %d dict dup begin\n", glyph_ids.size()+1); |
| | |
| | stream.printf("/.notdef 0 def\n"); |
| |
|
| | |
| | for (std::vector<int>::const_iterator i = glyph_ids.begin(); |
| | i != glyph_ids.end(); ++i) |
| | { |
| | if ((font->target_type == PS_TYPE_42 || |
| | font->target_type == PS_TYPE_42_3_HYBRID) |
| | && *i < 256) |
| | { |
| | stream.printf("/%s %d def\n",ttfont_CharStrings_getname(font, *i), *i); |
| | } |
| | else |
| | { |
| | stream.printf("/%s{",ttfont_CharStrings_getname(font, *i)); |
| |
|
| | tt_type3_charproc(stream, font, *i); |
| |
|
| | stream.putline("}_d"); |
| | } |
| | } |
| |
|
| | stream.putline("end readonly def"); |
| | } |
| |
|
| | |
| | |
| | |
| | |
| | void ttfont_trailer(TTStreamWriter& stream, struct TTFONT *font) |
| | { |
| | |
| | |
| | if (font->target_type == PS_TYPE_3 || |
| | font->target_type == PS_TYPE_42_3_HYBRID) |
| | { |
| | stream.put_char('\n'); |
| |
|
| | stream.putline("/BuildGlyph"); |
| | stream.putline(" {exch begin"); |
| | stream.putline(" CharStrings exch"); |
| | stream.putline(" 2 copy known not{pop /.notdef}if"); |
| | stream.putline(" true 3 1 roll get exec"); |
| | stream.putline(" end}_d"); |
| |
|
| | stream.put_char('\n'); |
| |
|
| | |
| | |
| | stream.putline("/BuildChar {"); |
| | stream.putline(" 1 index /Encoding get exch get"); |
| | stream.putline(" 1 index /BuildGlyph get exec"); |
| | stream.putline("}_d"); |
| |
|
| | stream.put_char('\n'); |
| | } |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | if (font->target_type == PS_TYPE_42 || |
| | font->target_type == PS_TYPE_42_3_HYBRID) |
| | { |
| | stream.put_char('\n'); |
| |
|
| | |
| | |
| | stream.putline("systemdict/resourcestatus known"); |
| | stream.putline(" {42 /FontType resourcestatus"); |
| | stream.putline(" {pop pop false}{true}ifelse}"); |
| | stream.putline(" {true}ifelse"); |
| |
|
| | |
| | |
| | stream.putline("{/TrueDict where{pop}{(%%[ Error: no TrueType rasterizer ]%%)= flush}ifelse"); |
| |
|
| | |
| | |
| | stream.putline("/FontType 3 def"); |
| |
|
| | |
| | |
| | stream.putline(" /TrueState 271 string def"); |
| |
|
| | |
| | |
| | stream.putline(" TrueDict begin sfnts save"); |
| | stream.putline(" 72 0 matrix defaultmatrix dtransform dup"); |
| | stream.putline(" mul exch dup mul add sqrt cvi 0 72 matrix"); |
| | stream.putline(" defaultmatrix dtransform dup mul exch dup"); |
| | stream.putline(" mul add sqrt cvi 3 -1 roll restore"); |
| | stream.putline(" TrueState initer end"); |
| |
|
| | |
| | |
| | |
| | |
| |
|
| | |
| | |
| | |
| | |
| | stream.putline(" /BuildGlyph{exch begin"); |
| | |
| |
|
| | |
| | |
| | |
| | stream.putline(" CharStrings dup 2 index known"); |
| | |
| |
|
| | |
| | |
| | |
| | stream.putline(" {exch}{exch pop /.notdef}ifelse"); |
| | |
| |
|
| | |
| | |
| | stream.putline(" get dup xcheck"); |
| | |
| |
|
| | |
| | stream.putline(" {currentdict systemdict begin begin exec end end}"); |
| |
|
| | |
| | stream.putline(" {TrueDict begin /bander load cvlit exch TrueState render end}"); |
| |
|
| | stream.putline(" ifelse"); |
| |
|
| | |
| | stream.putline(" end}bind def"); |
| |
|
| | |
| | |
| | stream.putline(" /BuildChar{"); |
| | stream.putline(" 1 index /Encoding get exch get"); |
| | stream.putline(" 1 index /BuildGlyph get exec"); |
| | stream.putline(" }bind def"); |
| |
|
| | |
| | |
| | |
| | stream.putline("}if"); |
| | stream.put_char('\n'); |
| | } |
| |
|
| | stream.putline("FontName currentdict end definefont pop"); |
| | |
| | } |
| |
|
| | |
| | |
| | |
| |
|
| | void read_font(const char *filename, font_type_enum target_type, std::vector<int>& glyph_ids, TTFONT& font) |
| | { |
| | BYTE *ptr; |
| |
|
| | |
| | font.target_type = target_type; |
| |
|
| | if (font.target_type == PS_TYPE_42) |
| | { |
| | bool has_low = false; |
| | bool has_high = false; |
| |
|
| | for (std::vector<int>::const_iterator i = glyph_ids.begin(); |
| | i != glyph_ids.end(); ++i) |
| | { |
| | if (*i > 255) |
| | { |
| | has_high = true; |
| | if (has_low) break; |
| | } |
| | else |
| | { |
| | has_low = true; |
| | if (has_high) break; |
| | } |
| | } |
| |
|
| | if (has_high && has_low) |
| | { |
| | font.target_type = PS_TYPE_42_3_HYBRID; |
| | } |
| | else if (has_high && !has_low) |
| | { |
| | font.target_type = PS_TYPE_3; |
| | } |
| | } |
| |
|
| | |
| | font.filename=filename; |
| |
|
| | |
| | if ( (font.file = fopen(filename,"rb")) == (FILE*)NULL ) |
| | { |
| | throw TTException("Failed to open TrueType font"); |
| | } |
| |
|
| | |
| | assert(font.offset_table == NULL); |
| | font.offset_table = (BYTE*)calloc( 12, sizeof(BYTE) ); |
| |
|
| | |
| | if ( fread( font.offset_table, sizeof(BYTE), 12, font.file ) != 12 ) |
| | { |
| | throw TTException("TrueType font may be corrupt (reason 1)"); |
| | } |
| |
|
| | |
| | font.numTables = getUSHORT( font.offset_table + 4 ); |
| | debug("numTables=%d",(int)font.numTables); |
| |
|
| | |
| | font.offset_table = (BYTE*)realloc( font.offset_table, sizeof(BYTE) * (12 + font.numTables * 16) ); |
| |
|
| | |
| | if ( fread( font.offset_table + 12, sizeof(BYTE), (font.numTables*16), font.file ) != (font.numTables*16) ) |
| | { |
| | throw TTException("TrueType font may be corrupt (reason 2)"); |
| | } |
| |
|
| | |
| | font.TTVersion = getFixed( font.offset_table ); |
| |
|
| | |
| | ptr = GetTable(&font, "head"); |
| | try |
| | { |
| | font.MfrRevision = getFixed( ptr + 4 ); |
| | font.unitsPerEm = getUSHORT( ptr + 18 ); |
| | font.HUPM = font.unitsPerEm / 2; |
| | debug("unitsPerEm=%d",(int)font.unitsPerEm); |
| | font.llx = topost2( getFWord( ptr + 36 ) ); |
| | font.lly = topost2( getFWord( ptr + 38 ) ); |
| | font.urx = topost2( getFWord( ptr + 40 ) ); |
| | font.ury = topost2( getFWord( ptr + 42 ) ); |
| | font.indexToLocFormat = getSHORT( ptr + 50 ); |
| | if (font.indexToLocFormat != 0 && font.indexToLocFormat != 1) |
| | { |
| | throw TTException("TrueType font is unusable because indexToLocFormat != 0"); |
| | } |
| | if ( getSHORT(ptr+52) != 0 ) |
| | { |
| | throw TTException("TrueType font is unusable because glyphDataFormat != 0"); |
| | } |
| | } |
| | catch (TTException& ) |
| | { |
| | free(ptr); |
| | throw; |
| | } |
| | free(ptr); |
| |
|
| | |
| | Read_name(&font); |
| |
|
| | |
| | assert(font.post_table == NULL); |
| | font.post_table = GetTable(&font, "post"); |
| | font.numGlyphs = getUSHORT( font.post_table + 32 ); |
| |
|
| | |
| | |
| | |
| | if (font.target_type == PS_TYPE_3 || font.target_type == PS_TYPE_42_3_HYBRID) |
| | { |
| | BYTE *ptr; |
| | ptr = GetTable(&font, "hhea"); |
| | font.numberOfHMetrics = getUSHORT(ptr + 34); |
| | free(ptr); |
| |
|
| | assert(font.loca_table == NULL); |
| | font.loca_table = GetTable(&font,"loca"); |
| | assert(font.glyf_table == NULL); |
| | font.glyf_table = GetTable(&font,"glyf"); |
| | assert(font.hmtx_table == NULL); |
| | font.hmtx_table = GetTable(&font,"hmtx"); |
| | } |
| |
|
| | if (glyph_ids.size() == 0) |
| | { |
| | glyph_ids.clear(); |
| | glyph_ids.reserve(font.numGlyphs); |
| | for (int x = 0; x < font.numGlyphs; ++x) |
| | { |
| | glyph_ids.push_back(x); |
| | } |
| | } |
| | else if (font.target_type == PS_TYPE_3 || |
| | font.target_type == PS_TYPE_42_3_HYBRID) |
| | { |
| | ttfont_add_glyph_dependencies(&font, glyph_ids); |
| | } |
| |
|
| | } |
| |
|
| | void insert_ttfont(const char *filename, TTStreamWriter& stream, |
| | font_type_enum target_type, std::vector<int>& glyph_ids) |
| | { |
| | struct TTFONT font; |
| |
|
| | read_font(filename, target_type, glyph_ids, font); |
| |
|
| | |
| | ttfont_header(stream, &font); |
| |
|
| | |
| | ttfont_encoding(stream, &font, glyph_ids, target_type); |
| |
|
| | |
| | ttfont_FontInfo(stream, &font); |
| |
|
| | |
| | |
| | if (font.target_type == PS_TYPE_42 || |
| | font.target_type == PS_TYPE_42_3_HYBRID) |
| | { |
| | ttfont_sfnts(stream, &font); |
| | } |
| |
|
| | |
| | ttfont_CharStrings(stream, &font, glyph_ids); |
| |
|
| | |
| | ttfont_trailer(stream, &font); |
| |
|
| | } |
| |
|
| | TTFONT::TTFONT() : |
| | file(NULL), |
| | PostName(NULL), |
| | FullName(NULL), |
| | FamilyName(NULL), |
| | Style(NULL), |
| | Copyright(NULL), |
| | Version(NULL), |
| | Trademark(NULL), |
| | offset_table(NULL), |
| | post_table(NULL), |
| | loca_table(NULL), |
| | glyf_table(NULL), |
| | hmtx_table(NULL) |
| | { |
| |
|
| | } |
| |
|
| | TTFONT::~TTFONT() |
| | { |
| | if (file) |
| | { |
| | fclose(file); |
| | } |
| | free(PostName); |
| | free(FullName); |
| | free(FamilyName); |
| | free(Style); |
| | free(Copyright); |
| | free(Version); |
| | free(Trademark); |
| | free(offset_table); |
| | free(post_table); |
| | free(loca_table); |
| | free(glyf_table); |
| | free(hmtx_table); |
| | } |
| |
|
| | |
| |
|