static pdc_bool pdf_parse_pfm(PDF *p, pdc_file *fp, pdf_font *font) { static const char fn[] = "pdf_parse_pfm"; fnt_font_metric *ftm = &font->ft.m; size_t length; pdc_byte *pfm; pdc_bool ismem; int i, dfFirstChar, dfLastChar, default_width; unsigned long dfExtentTable; /* read whole file and close it */ pfm = (pdc_byte *) pdc_freadall(fp, &length, &ismem); pdc_fclose(fp); /* check whether this is really a valid PostScript PFM file */ if (pfm == NULL || (header_dfVersion != 0x100 && header_dfVersion != 0x200) || dfDevice > length || strncmp((const char *) pfm + dfDevice, PDF_STRING_PostScript, 10) || ext_dfDriverInfo > length) { if (!ismem) pdc_free(p->pdc, pfm); return pdc_false; } /* fetch relevant data from the PFM */ ftm->type = fnt_Type1; font->ft.name = pdc_strdup(p->pdc, (const char *)pfm + ext_dfDriverInfo); ftm->name = pdc_strdup(p->pdc, font->ft.name); pdc_logg_cond(p->pdc, 1, trc_font, "\tPostScript font name: \"%s\"\n", ftm->name); switch (header_dfPitchAndFamily & 0xF0) { case PDF_ROMAN: ftm->flags |= FNT_SERIF; break; case PDF_MODERN: /* Has to be ignored, contrary to MS's specs */ break; case PDF_SCRIPT: ftm->flags |= FNT_SCRIPT; break; case PDF_DECORATIVE: /* the dfCharSet flag lies in this case... */ header_dfCharSet = PFM_SYMBOL_CHARSET; break; case PDF_SWISS: case PDF_DONTCARE: default: break; } /* temporarily */ font->ft.enc = (pdc_encoding) header_dfCharSet; dfFirstChar = header_dfFirstChar; dfLastChar = header_dfLastChar; dfExtentTable = ext_dfExtentTable; /* * Some rare PFMs do not contain any ExtentTable if the fixed pitch flag * is set. Use the dfMaxWidth entry for all glyphs in this case. * If the user forced the font to be monospaced we use this value instead. */ if ((!(header_dfPitchAndFamily & PDF_FIXED_PITCH) && dfExtentTable == 0) || font->opt.monospace) { ftm->isFixedPitch = pdc_true; default_width = font->opt.monospace ? font->opt.monospace : (int) header_dfMaxWidth; } else { /* default values -- don't take the width of the default character */ default_width = FNT_DEFAULT_WIDTH; } font->ft.numcodes = 256; ftm->numwidths = font->ft.numcodes; ftm->widths = (int *) pdc_calloc(p->pdc, ftm->numwidths * sizeof(int), fn); for (i = 0; i < font->ft.numcodes; i++) ftm->widths[i] = default_width; if (!ftm->isFixedPitch) { if (ext_dfExtentTable == 0 || ext_dfExtentTable + 2 * (header_dfLastChar-header_dfFirstChar) + 1 > length) { if (!ismem) pdc_free(p->pdc, pfm); return pdc_false; } for (i = dfFirstChar; i <= dfLastChar; i++) ftm->widths[i] = (int) PFM_WORD(dfExtentTable + 2 * (i - dfFirstChar)); /* * Check whether the font is actually opt.monospaced * (the fixed pitch flag is not necessarily set) */ default_width = ftm->widths[dfFirstChar]; for (i = dfFirstChar+1; i <= dfLastChar; i++) if (default_width != ftm->widths[i]) break; if (i == dfLastChar + 1) ftm->isFixedPitch = pdc_true; } font->ft.weight = fnt_check_weight(header_dfWeight); ftm->defwidth = default_width; ftm->italicAngle = (header_dfItalic ? etmSlant/(10.0) : 0.0); ftm->capHeight = etmCapHeight; ftm->xHeight = etmXHeight; ftm->descender = -etmLowerCaseDescent; ftm->ascender = (int) header_dfAscent; ftm->underlinePosition = -etmUnderlineOffset; ftm->underlineThickness = etmUnderlineWidth; ftm->urx = header_dfMaxWidth; if (!ismem) pdc_free(p->pdc, pfm); return pdc_true; }
static pdc_bool pdf_parse_afm( PDF *p, pdc_file *fp, pdf_font *font, const char *fontname, const char *filename) { static const char fn[] = "pdf_parse_afm"; fnt_font_metric *ftm = &font->ft.m; const char *afmtype = NULL; char **wordlist = NULL, *keyword, *arg1; char line[AFM_LINEBUF]; int i, cmp, lo, hi, nwords, nglyphs = 0, nline = 0; int tablen = ((sizeof keyStrings) / (sizeof (char *))); pdc_sint32 iz; double dz; pdc_scalar charwidth = -1; pdf_afmkey keynumber; fnt_glyphwidth *glw; pdc_bool toskip = pdc_false; pdc_bool is_zadbfont = !strcmp(fontname, "ZapfDingbats"); /* all new glyph names of AGL 2.0 are missing */ font->missingglyphs = 0xFFFFFFFF; /* read loop. because of Mac files we use pdc_fgetline */ while (pdc_fgetline(line, AFM_LINEBUF, fp) != NULL) { /* split line */ nline++; nwords = pdc_split_stringlist(p->pdc, line, AFM_SEPARATORS, 0, &wordlist); if (!nwords) continue; keyword = wordlist[0]; /* find keynumber */ lo = 0; hi = tablen; keynumber = NOPE; while (lo < hi) { i = (lo + hi) / 2; cmp = strcmp(keyword, keyStrings[i]); if (cmp == 0) { keynumber = (pdf_afmkey) i; break; } if (cmp < 0) hi = i; else lo = i + 1; } /* unkown key */ if (keynumber == NOPE) { pdc_warning(p->pdc, PDF_E_T1_AFMBADKEY, keyword, filename, 0,0); goto PDF_PARSECONTD; } if (keynumber == ENDDIRECTION) toskip = pdc_false; if (nwords == 1 || toskip == pdc_true) goto PDF_PARSECONTD; /* key switch */ arg1 = wordlist[1]; switch (keynumber) { case STARTDIRECTION: if (pdc_str2integer(arg1, 0, &iz) != pdc_true) goto PDF_SYNTAXERROR; if (iz) toskip = pdc_true; break; case STARTCOMPFONTMETRICS: afmtype = "ACFM"; goto PDF_SYNTAXERROR; case STARTMASTERFONTMETRICS: afmtype = "AMFM"; goto PDF_SYNTAXERROR; case ISCIDFONT: afmtype = "CID font"; if (!strcmp(arg1, "true")) goto PDF_SYNTAXERROR; break; case FONTNAME: font->ft.name = pdc_strdup(p->pdc, arg1); ftm->name = pdc_strdup(p->pdc, arg1); pdc_logg_cond(p->pdc, 1, trc_font, "\tPostScript font name: \"%s\"\n", ftm->name); break; /* Recognize Multiple Master fonts by last part of name */ case FAMILYNAME: if (!strcmp(wordlist[nwords-1], "MM")) ftm->type = fnt_MMType1; else ftm->type = fnt_Type1; break; /* Default: FontSpecific */ case ENCODINGSCHEME: if (!pdc_stricmp(arg1, "StandardEncoding") || !pdc_stricmp(arg1, "AdobeStandardEncoding")) font->ft.issymbfont = pdc_false; break; case STDHW: if (pdc_str2double(arg1, &dz) != pdc_true) goto PDF_SYNTAXERROR; ftm->StdHW = (int) dz; break; case STDVW: if (pdc_str2double(arg1, &dz) != pdc_true) goto PDF_SYNTAXERROR; ftm->StdVW = (int) dz; break; case WEIGHT: font->ft.weight = fnt_check_weight(fnt_weightname2weight(arg1)); break; case ISFIXEDPITCH: if (!pdc_stricmp(arg1, "false")) ftm->isFixedPitch = pdc_false; else ftm->isFixedPitch = pdc_true; break; /* New AFM 4.1 keyword "CharWidth" implies fixed pitch */ case CHARWIDTH: if (pdc_str2double(arg1, &dz) != pdc_true) goto PDF_SYNTAXERROR; charwidth = dz; ftm->isFixedPitch = pdc_true; break; case ITALICANGLE: { if (pdc_str2double(arg1, &dz) != pdc_true) goto PDF_SYNTAXERROR; ftm->italicAngle = dz; } break; case UNDERLINEPOSITION: if (pdc_str2double(arg1, &dz) != pdc_true) goto PDF_SYNTAXERROR; ftm->underlinePosition = (int) dz; break; case UNDERLINETHICKNESS: if (pdc_str2double(arg1, &dz) != pdc_true) goto PDF_SYNTAXERROR; ftm->underlineThickness = (int) dz; break; case FONTBBOX: { if (nwords != 5) goto PDF_SYNTAXERROR; for (i = 1; i < nwords; i++) { if (pdc_str2double(wordlist[i], &dz) != pdc_true) goto PDF_SYNTAXERROR; if (i == 1) ftm->llx = dz; else if (i == 2) ftm->lly = dz; else if (i == 3) ftm->urx = dz; else if (i == 4) ftm->ury = dz; } } break; case CAPHEIGHT: if (pdc_str2double(arg1, &dz) != pdc_true) goto PDF_SYNTAXERROR; ftm->capHeight = (int) dz; break; case XHEIGHT: if (pdc_str2double(arg1, &dz) != pdc_true) goto PDF_SYNTAXERROR; ftm->xHeight = (int) dz; break; case DESCENDER: if (pdc_str2double(arg1, &dz) != pdc_true) goto PDF_SYNTAXERROR; ftm->descender = (int) dz; break; case ASCENDER: if (pdc_str2double(arg1, &dz) != pdc_true) goto PDF_SYNTAXERROR; ftm->ascender = (int) dz; break; /* Character widths */ case STARTCHARMETRICS: if (pdc_str2integer(arg1, PDC_INT_UNSIGNED, (pdc_sint32 *) &nglyphs) != pdc_true || nglyphs <= 0) goto PDF_SYNTAXERROR; ftm->glw = (fnt_glyphwidth *) pdc_calloc(p->pdc, (size_t) nglyphs * sizeof(fnt_glyphwidth), fn); break; /* Character code */ case CODE: case CODEHEX: if (!nglyphs || !ftm->glw) goto PDF_SYNTAXERROR; if (font->ft.numglyphs >= nglyphs) { nglyphs++; ftm->glw = (fnt_glyphwidth *) pdc_realloc(p->pdc, ftm->glw, (size_t) nglyphs * sizeof(fnt_glyphwidth), fn); } glw = &ftm->glw[font->ft.numglyphs]; if (keynumber == CODE) { if (pdc_str2integer(arg1, 0, &iz) != pdc_true) goto PDF_SYNTAXERROR; } else { if (pdc_str2integer(arg1, PDC_INT_HEXADEC, &iz) != pdc_true) goto PDF_SYNTAXERROR; } glw->code = (pdc_short) iz; glw->unicode = 0; glw->width = (pdc_ushort) (font->opt.monospace ? font->opt.monospace : charwidth); font->ft.numglyphs++; /* Character width and name */ for (i = 2; i < nwords; i++) { if (!strcmp(wordlist[i], "WX") || !strcmp(wordlist[i], "W0X") || !strcmp(wordlist[i], "W")) { i++; if (i == nwords) goto PDF_SYNTAXERROR; if (pdc_str2double(wordlist[i], &dz) != pdc_true) goto PDF_SYNTAXERROR; glw->width = (pdc_ushort) (font->opt.monospace ? font->opt.monospace : dz); } if (!strcmp(wordlist[i], "N")) { i++; if (i == nwords) goto PDF_SYNTAXERROR; /* Unicode value by means of AGL, * internal and private table */ glw->unicode = is_zadbfont ? (pdc_ushort) pdc_zadb2unicode(wordlist[i]): pdc_insert_glyphname(p->pdc, wordlist[i]); pdc_delete_missingglyph_bit(glw->unicode, &font->missingglyphs); } } break; default: break; } PDF_PARSECONTD: pdc_cleanup_stringlist(p->pdc, wordlist); wordlist = NULL; if (keynumber == ENDFONTMETRICS) break; } /* necessary font struct members */ if (font->ft.name == NULL || ftm->glw == NULL) goto PDF_SYNTAXERROR; pdc_fclose(fp); ftm->numglwidths = font->ft.numglyphs; return pdc_true; PDF_SYNTAXERROR: pdc_fclose(fp); pdc_cleanup_stringlist(p->pdc, wordlist); if (afmtype) pdc_set_errmsg(p->pdc, PDF_E_T1_UNSUPP_FORMAT, afmtype, 0, 0, 0); else pdc_set_errmsg(p->pdc, PDC_E_IO_ILLSYNTAX, "AFM ", filename, pdc_errprintf(p->pdc, "%d", nline), 0); return pdc_false; }