diff options
Diffstat (limited to 'xps/xpsfont.c')
-rw-r--r-- | xps/xpsfont.c | 348 |
1 files changed, 174 insertions, 174 deletions
diff --git a/xps/xpsfont.c b/xps/xpsfont.c index 0ddb6b265..e2d375bac 100644 --- a/xps/xpsfont.c +++ b/xps/xpsfont.c @@ -44,7 +44,7 @@ int xps_init_font_cache(xps_context_t *ctx) { ctx->fontdir = gs_font_dir_alloc(ctx->memory); if (!ctx->fontdir) - return gs_throw(-1, "cannot gs_font_dir_alloc()"); + return gs_throw(-1, "cannot gs_font_dir_alloc()"); gs_setaligntopixels(ctx->fontdir, 1); /* no subpixels */ gs_setgridfittt(ctx->fontdir, 1); /* see gx_ttf_outline in gxttfn.c for values */ @@ -61,8 +61,8 @@ xps_new_font(xps_context_t *ctx, byte *buf, int buflen, int index) font = xps_alloc(ctx, sizeof(xps_font_t)); if (!font) { - gs_throw(-1, "out of memory"); - return NULL; + gs_throw(-1, "out of memory"); + return NULL; } font->data = buf; @@ -82,31 +82,31 @@ xps_new_font(xps_context_t *ctx, byte *buf, int buflen, int index) font->charstrings = 0; if (memcmp(font->data, "OTTO", 4) == 0) - code = xps_init_postscript_font(ctx, font); + code = xps_init_postscript_font(ctx, font); else if (memcmp(font->data, "\0\1\0\0", 4) == 0) - code = xps_init_truetype_font(ctx, font); + code = xps_init_truetype_font(ctx, font); else if (memcmp(font->data, "true", 4) == 0) - code = xps_init_truetype_font(ctx, font); + code = xps_init_truetype_font(ctx, font); else if (memcmp(font->data, "ttcf", 4) == 0) - code = xps_init_truetype_font(ctx, font); + code = xps_init_truetype_font(ctx, font); else { - xps_free_font(ctx, font); - gs_throw(-1, "not an opentype font"); - return NULL; + xps_free_font(ctx, font); + gs_throw(-1, "not an opentype font"); + return NULL; } if (code < 0) { - xps_free_font(ctx, font); - gs_rethrow(-1, "cannot init font"); - return NULL; + xps_free_font(ctx, font); + gs_rethrow(-1, "cannot init font"); + return NULL; } code = xps_load_sfnt_cmap(font); if (code < 0) { - errprintf("warning: no cmap table found in font\n"); + errprintf("warning: no cmap table found in font\n"); } return font; @@ -117,8 +117,8 @@ xps_free_font(xps_context_t *ctx, xps_font_t *font) { if (font->font) { - gs_font_finalize(font->font); - gs_free_object(ctx->memory, font->font, "font object"); + gs_font_finalize(font->font); + gs_free_object(ctx->memory, font->font, "font object"); } xps_free(ctx, font); } @@ -137,33 +137,33 @@ xps_find_sfnt_table(xps_font_t *font, char *name, int *lengthp) int i; if (font->length < 12) - return -1; + return -1; if (!memcmp(font->data, "ttcf", 4)) { - int nfonts = u32(font->data + 8); - if (font->subfontid < 0 || font->subfontid >= nfonts) - return gs_throw(-1, "Invalid subfont ID"); - offset = u32(font->data + 12 + font->subfontid * 4); + int nfonts = u32(font->data + 8); + if (font->subfontid < 0 || font->subfontid >= nfonts) + return gs_throw(-1, "Invalid subfont ID"); + offset = u32(font->data + 12 + font->subfontid * 4); } else { - offset = 0; + offset = 0; } ntables = u16(font->data + offset + 4); if (font->length < offset + 12 + ntables * 16) - return -1; + return -1; for (i = 0; i < ntables; i++) { - byte *entry = font->data + offset + 12 + i * 16; - if (!memcmp(entry, name, 4)) - { - if (lengthp) - *lengthp = u32(entry + 12); - return u32(entry + 8); - } + byte *entry = font->data + offset + 12 + i * 16; + if (!memcmp(entry, name, 4)) + { + if (lengthp) + *lengthp = u32(entry + 12); + return u32(entry + 8); + } } return -1; @@ -183,9 +183,9 @@ int xps_load_sfnt_name(xps_font_t *font, char *namep) offset = xps_find_sfnt_table(font, "name", &length); if (offset < 0) - return -1; + return -1; if (length < 6) - return -1; + return -1; namedata = font->data + offset; @@ -195,24 +195,24 @@ int xps_load_sfnt_name(xps_font_t *font, char *namep) for (i = 0; i < count; i++) { - byte *record = namedata + 6 + i * 12; - int pid = u16(record + 0); - int eid = u16(record + 2); - int langid = u16(record + 4); - int nameid = u16(record + 6); - length = u16(record + 8); - offset = u16(record + 10); - - /* Mac Roman English */ - if (pid == 1 && eid == 0 && langid == 0) - { - /* Full font name or postscript name */ - if (nameid == 4 || nameid == 6) - { - memcpy(namep, namedata + stringoffset + offset, length); - namep[length] = 0; - } - } + byte *record = namedata + 6 + i * 12; + int pid = u16(record + 0); + int eid = u16(record + 2); + int langid = u16(record + 4); + int nameid = u16(record + 6); + length = u16(record + 8); + offset = u16(record + 10); + + /* Mac Roman English */ + if (pid == 1 && eid == 0 && langid == 0) + { + /* Full font name or postscript name */ + if (nameid == 4 || nameid == 6) + { + memcpy(namep, namedata + stringoffset + offset, length); + namep[length] = 0; + } + } } return 0; @@ -231,18 +231,18 @@ xps_load_sfnt_cmap(xps_font_t *font) offset = xps_find_sfnt_table(font, "cmap", &length); if (offset < 0) - return -1; + return -1; if (length < 4) - return -1; + return -1; cmapdata = font->data + offset; nsubtables = u16(cmapdata + 2); if (nsubtables < 0) - return -1; + return -1; if (length < 4 + nsubtables * 8) - return -1; + return -1; font->cmaptable = offset; font->cmapsubcount = nsubtables; @@ -270,7 +270,7 @@ xps_identify_font_encoding(xps_font_t *font, int idx, int *pid, int *eid) { byte *cmapdata, *entry; if (idx < 0 || idx >= font->cmapsubcount) - return -1; + return -1; cmapdata = font->data + font->cmaptable; entry = cmapdata + 4 + idx * 8; *pid = u16(entry + 0); @@ -288,7 +288,7 @@ xps_select_font_encoding(xps_font_t *font, int idx) byte *cmapdata, *entry; int pid, eid; if (idx < 0 || idx >= font->cmapsubcount) - return -1; + return -1; cmapdata = font->data + font->cmaptable; entry = cmapdata + 4 + idx * 8; pid = u16(entry + 0); @@ -311,97 +311,97 @@ xps_encode_font_char_int(xps_font_t *font, int code) /* no cmap selected: return identity */ if (font->cmapsubtable <= 0) - return code; + return code; table = font->data + font->cmapsubtable; switch (u16(table)) { case 0: /* Apple standard 1-to-1 mapping. */ - return table[code + 6]; + return table[code + 6]; case 4: /* Microsoft/Adobe segmented mapping. */ - { - int segCount2 = u16(table + 6); - byte *endCount = table + 14; - byte *startCount = endCount + segCount2 + 2; - byte *idDelta = startCount + segCount2; - byte *idRangeOffset = idDelta + segCount2; - int i2; - - for (i2 = 0; i2 < segCount2 - 3; i2 += 2) - { - int delta, roff; - int start = u16(startCount + i2); - int glyph; - - if ( code < start ) - return 0; - if ( code > u16(endCount + i2) ) - continue; - delta = s16(idDelta + i2); - roff = s16(idRangeOffset + i2); - if ( roff == 0 ) - { - return ( code + delta ) & 0xffff; /* mod 65536 */ - return 0; - } - glyph = u16(idRangeOffset + i2 + roff + ((code - start) << 1)); - return (glyph == 0 ? 0 : glyph + delta); - } - - /* - * The TrueType documentation says that the last range is - * always supposed to end with 0xffff, so this shouldn't - * happen; however, in some real fonts, it does. - */ - return 0; - } + { + int segCount2 = u16(table + 6); + byte *endCount = table + 14; + byte *startCount = endCount + segCount2 + 2; + byte *idDelta = startCount + segCount2; + byte *idRangeOffset = idDelta + segCount2; + int i2; + + for (i2 = 0; i2 < segCount2 - 3; i2 += 2) + { + int delta, roff; + int start = u16(startCount + i2); + int glyph; + + if ( code < start ) + return 0; + if ( code > u16(endCount + i2) ) + continue; + delta = s16(idDelta + i2); + roff = s16(idRangeOffset + i2); + if ( roff == 0 ) + { + return ( code + delta ) & 0xffff; /* mod 65536 */ + return 0; + } + glyph = u16(idRangeOffset + i2 + roff + ((code - start) << 1)); + return (glyph == 0 ? 0 : glyph + delta); + } + + /* + * The TrueType documentation says that the last range is + * always supposed to end with 0xffff, so this shouldn't + * happen; however, in some real fonts, it does. + */ + return 0; + } case 6: /* Single interval lookup. */ - { - int firstCode = u16(table + 6); - int entryCount = u16(table + 8); - if ( code < firstCode || code >= firstCode + entryCount ) - return 0; - return u16(table + 10 + ((code - firstCode) << 1)); - } + { + int firstCode = u16(table + 6); + int entryCount = u16(table + 8); + if ( code < firstCode || code >= firstCode + entryCount ) + return 0; + return u16(table + 10 + ((code - firstCode) << 1)); + } case 10: /* Trimmed array (like 6) */ - { - int startCharCode = u32(table + 12); - int numChars = u32(table + 16); - if ( code < startCharCode || code >= startCharCode + numChars ) - return 0; - return u32(table + 20 + (code - startCharCode) * 4); - } + { + int startCharCode = u32(table + 12); + int numChars = u32(table + 16); + if ( code < startCharCode || code >= startCharCode + numChars ) + return 0; + return u32(table + 20 + (code - startCharCode) * 4); + } case 12: /* Segmented coverage. (like 4) */ - { - int nGroups = u32(table + 12); - byte *group = table + 16; - int i; - - for (i = 0; i < nGroups; i++) - { - int startCharCode = u32(group + 0); - int endCharCode = u32(group + 4); - int startGlyphID = u32(group + 8); - if ( code < startCharCode ) - return 0; - if ( code <= endCharCode ) - return startGlyphID + (code - startCharCode); - group += 12; - } - - return 0; - } + { + int nGroups = u32(table + 12); + byte *group = table + 16; + int i; + + for (i = 0; i < nGroups; i++) + { + int startCharCode = u32(group + 0); + int endCharCode = u32(group + 4); + int startGlyphID = u32(group + 8); + if ( code < startCharCode ) + return 0; + if ( code <= endCharCode ) + return startGlyphID + (code - startCharCode); + group += 12; + } + + return 0; + } case 2: /* High-byte mapping through table. */ case 8: /* Mixed 16-bit and 32-bit coverage (like 2) */ default: - errprintf("error: unknown cmap format: %d\n", u16(table)); - return 0; + errprintf("error: unknown cmap format: %d\n", u16(table)); + return 0; } return 0; @@ -412,7 +412,7 @@ xps_encode_font_char(xps_font_t *font, int code) { int gid = xps_encode_font_char_int(font, code); if (gid == 0 && font->usepua) - gid = xps_encode_font_char_int(font, 0xF000 | code); + gid = xps_encode_font_char_int(font, 0xF000 | code); return gid; } @@ -445,24 +445,24 @@ int xps_measure_font_glyph(xps_context_t *ctx, xps_font_t *font, int gid, xps_gl ofs = xps_find_sfnt_table(font, "hhea", &len); if (ofs < 0) - return gs_throw(-1, "cannot find hhea table"); + return gs_throw(-1, "cannot find hhea table"); if (len < 2 * 18) - return gs_throw(-1, "hhea table is too short"); + return gs_throw(-1, "hhea table is too short"); vorg = s16(font->data + ofs + 4); /* ascender is default vorg */ desc = s16(font->data + ofs + 6); /* descender */ if (desc < 0) - desc = -desc; + desc = -desc; n = u16(font->data + ofs + 17 * 2); ofs = xps_find_sfnt_table(font, "hmtx", &len); if (ofs < 0) - return gs_throw(-1, "cannot find hmtx table"); + return gs_throw(-1, "cannot find hmtx table"); idx = gid; if (idx > n - 1) - idx = n - 1; + idx = n - 1; hadv = u16(font->data + ofs + idx * 4); vadv = 0; @@ -474,71 +474,71 @@ int xps_measure_font_glyph(xps_context_t *ctx, xps_font_t *font, int gid, xps_gl head = xps_find_sfnt_table(font, "head", &len); if (head > 0) { - scale = u16(font->data + head + 18); /* units per em */ + scale = u16(font->data + head + 18); /* units per em */ } ofs = xps_find_sfnt_table(font, "OS/2", &len); if (ofs > 0 && len > 70) { - vorg = s16(font->data + ofs + 68); /* sTypoAscender */ - desc = s16(font->data + ofs + 70); /* sTypoDescender */ - if (desc < 0) - desc = -desc; + vorg = s16(font->data + ofs + 68); /* sTypoAscender */ + desc = s16(font->data + ofs + 70); /* sTypoDescender */ + if (desc < 0) + desc = -desc; } ofs = xps_find_sfnt_table(font, "vhea", &len); if (ofs > 0) { - if (len < 2 * 18) - return gs_throw(-1, "vhea table is too short"); + if (len < 2 * 18) + return gs_throw(-1, "vhea table is too short"); - n = u16(font->data + ofs + 17 * 2); + n = u16(font->data + ofs + 17 * 2); - ofs = xps_find_sfnt_table(font, "vmtx", &len); - if (ofs < 0) - return gs_throw(-1, "cannot find vmtx table"); + ofs = xps_find_sfnt_table(font, "vmtx", &len); + if (ofs < 0) + return gs_throw(-1, "cannot find vmtx table"); - idx = gid; - if (idx > n - 1) - idx = n - 1; + idx = gid; + if (idx > n - 1) + idx = n - 1; - vadv = u16(font->data + ofs + idx * 4); - vtop = u16(font->data + ofs + idx * 4 + 2); + vadv = u16(font->data + ofs + idx * 4); + vtop = u16(font->data + ofs + idx * 4 + 2); - glyf = xps_find_sfnt_table(font, "glyf", &len); - loca = xps_find_sfnt_table(font, "loca", &len); - if (head > 0 && glyf > 0 && loca > 0) - { - format = u16(font->data + head + 50); /* indexToLocaFormat */ + glyf = xps_find_sfnt_table(font, "glyf", &len); + loca = xps_find_sfnt_table(font, "loca", &len); + if (head > 0 && glyf > 0 && loca > 0) + { + format = u16(font->data + head + 50); /* indexToLocaFormat */ - if (format == 0) - ofs = u16(font->data + loca + gid * 2) * 2; - else - ofs = u32(font->data + loca + gid * 4); + if (format == 0) + ofs = u16(font->data + loca + gid * 2) * 2; + else + ofs = u32(font->data + loca + gid * 4); - ymax = u16(font->data + glyf + ofs + 8); /* yMax */ + ymax = u16(font->data + glyf + ofs + 8); /* yMax */ - vorg = ymax + vtop; - } + vorg = ymax + vtop; + } } ofs = xps_find_sfnt_table(font, "VORG", &len); if (ofs > 0) { - vorg = u16(font->data + ofs + 6); - n = u16(font->data + ofs + 6); - for (i = 0; i < n; i++) - { - if (u16(font->data + ofs + 8 + 4 * i) == gid) - { - vorg = s16(font->data + ofs + 8 + 4 * i + 2); - break; - } - } + vorg = u16(font->data + ofs + 6); + n = u16(font->data + ofs + 6); + for (i = 0; i < n; i++) + { + if (u16(font->data + ofs + 8 + 4 * i) == gid) + { + vorg = s16(font->data + ofs + 8 + 4 * i + 2); + break; + } + } } if (vadv == 0) - vadv = vorg + desc; + vadv = vorg + desc; mtx->hadv = hadv / (float) scale; mtx->vadv = vadv / (float) scale; |