From 2df0bbe7fe988d353324d987d9610ee086723658 Mon Sep 17 00:00:00 2001 From: Adrian Johnson Date: Tue, 3 Jan 2012 20:46:04 +1030 Subject: [PATCH] Add -subst option to pdffonts to lists the substitute font name and filename --- poppler/FontInfo.cc | 8 ++++- poppler/FontInfo.h | 2 + poppler/GlobalParams.cc | 31 +++++++++++++++++++-- poppler/GlobalParams.h | 3 +- utils/pdffonts.cc | 67 +++++++++++++++++++++++++++++++++------------- 5 files changed, 86 insertions(+), 25 deletions(-) diff --git a/poppler/FontInfo.cc b/poppler/FontInfo.cc index 440e6f5..0952f3c 100644 --- a/poppler/FontInfo.cc +++ b/poppler/FontInfo.cc @@ -189,17 +189,19 @@ FontInfo::FontInfo(GfxFont *font, PDFDoc *doc) { emb = font->getEmbeddedFontID(&embRef); } + file = NULL; + substituteName = NULL; if (!emb) { DisplayFontParam *dfp = globalParams->getDisplayFont(font); if (dfp) { + if (dfp->substituteName) + substituteName = dfp->substituteName->copy(); if (dfp->kind == displayFontT1) file = dfp->t1.fileName->copy(); else file = dfp->tt.fileName->copy(); } - else file = NULL; } - else file = NULL; // look for a ToUnicode map hasToUnicode = gFalse; @@ -236,4 +238,6 @@ FontInfo::FontInfo(FontInfo& f) { FontInfo::~FontInfo() { delete name; delete file; + if (substituteName) + delete substituteName; } diff --git a/poppler/FontInfo.h b/poppler/FontInfo.h index 733194d..a10a5d6 100644 --- a/poppler/FontInfo.h +++ b/poppler/FontInfo.h @@ -55,6 +55,7 @@ public: ~FontInfo(); GooString *getName() { return name; }; + GooString *getSubstituteName() { return substituteName; }; GooString *getFile() { return file; }; Type getType() { return type; }; GBool getEmbedded() { return emb; }; @@ -65,6 +66,7 @@ public: private: GooString *name; + GooString *substituteName; GooString *file; Type type; GBool emb; diff --git a/poppler/GlobalParams.cc b/poppler/GlobalParams.cc index 2e11bb0..f9a9393 100644 --- a/poppler/GlobalParams.cc +++ b/poppler/GlobalParams.cc @@ -126,8 +126,10 @@ GlobalParams *globalParams = NULL; //------------------------------------------------------------------------ DisplayFontParam::DisplayFontParam(GooString *nameA, + GooString *substNameA, DisplayFontParamKind kindA) { name = nameA; + substituteName = substNameA; kind = kindA; switch (kind) { case displayFontT1: @@ -141,6 +143,7 @@ DisplayFontParam::DisplayFontParam(GooString *nameA, DisplayFontParam::~DisplayFontParam() { delete name; + delete substituteName; switch (kind) { case displayFontT1: if (t1.fileName) { @@ -1244,18 +1247,40 @@ DisplayFontParam *GlobalParams::getDisplayFont(GfxFont *font) { continue; } } + FcChar8* s2; + GooString *substName = NULL; + res = FcPatternGetString(set->fonts[i], FC_FULLNAME, 0, &s2); + if (res == FcResultMatch && s2) { + substName = new GooString((char*)s2); + } else { + // fontconfig does not extract fullname for some fonts + // create the fullname from family and style + res = FcPatternGetString(set->fonts[i], FC_FAMILY, 0, &s2); + if (res == FcResultMatch && s2) { + substName = new GooString((char*)s2); + res = FcPatternGetString(set->fonts[i], FC_STYLE, 0, &s2); + if (res == FcResultMatch && s2) { + GooString *style = new GooString((char*)s2); + if (style->cmp("Regular") != 0) { + substName->append(" "); + substName->append(style); + } + delete style; + } + } + } ext = strrchr((char*)s,'.'); if (!ext) continue; if (!strncasecmp(ext,".ttf",4) || !strncasecmp(ext, ".ttc", 4)) { - dfp = new DisplayFontParam(fontName->copy(), displayFontTT); + dfp = new DisplayFontParam(fontName->copy(), substName, displayFontTT); dfp->tt.fileName = new GooString((char*)s); FcPatternGetInteger(set->fonts[i], FC_INDEX, 0, &(dfp->tt.faceIndex)); } - else if (!strncasecmp(ext,".pfa",4) || !strncasecmp(ext,".pfb",4)) + else if (!strncasecmp(ext,".pfa",4) || !strncasecmp(ext,".pfb",4)) { - dfp = new DisplayFontParam(fontName->copy(), displayFontT1); + dfp = new DisplayFontParam(fontName->copy(), substName, displayFontT1); dfp->t1.fileName = new GooString((char*)s); } else diff --git a/poppler/GlobalParams.h b/poppler/GlobalParams.h index 5fff49d..825ff7a 100644 --- a/poppler/GlobalParams.h +++ b/poppler/GlobalParams.h @@ -91,13 +91,14 @@ public: GooString *name; // font name for 8-bit fonts and named // CID fonts; collection name for // generic CID fonts + GooString *substituteName; DisplayFontParamKind kind; union { DisplayFontParamT1 t1; DisplayFontParamTT tt; }; - DisplayFontParam(GooString *nameA, DisplayFontParamKind kindA); + DisplayFontParam(GooString *nameA, GooString *substNameA, DisplayFontParamKind kindA); void setFileName(GooString *fileNameA) { if (displayFontT1 == kind) t1.fileName = fileNameA; diff --git a/utils/pdffonts.cc b/utils/pdffonts.cc index f2e49e5..205bb82 100644 --- a/utils/pdffonts.cc +++ b/utils/pdffonts.cc @@ -55,6 +55,7 @@ static char *fontTypeNames[] = { static int firstPage = 1; static int lastPage = 0; +static GBool showSubst = gFalse; static char ownerPassword[33] = "\001"; static char userPassword[33] = "\001"; static GBool printVersion = gFalse; @@ -65,6 +66,8 @@ static const ArgDesc argDesc[] = { "first page to examine"}, {"-l", argInt, &lastPage, 0, "last page to examine"}, + {"-subst", argFlag, &showSubst, 0, + "show font substitutions"}, {"-opw", argString, ownerPassword, sizeof(ownerPassword), "owner password (for encrypted files)"}, {"-upw", argString, userPassword, sizeof(userPassword), @@ -152,27 +155,53 @@ int main(int argc, char *argv[]) { FontInfoScanner scanner(doc, firstPage - 1); GooList *fonts = scanner.scan(lastPage - firstPage + 1); - // print the font info - printf("name type emb sub uni object ID\n"); - printf("------------------------------------ ----------------- --- --- --- ---------\n"); - if (fonts) { - for (int i = 0; i < fonts->getLength(); ++i) { - FontInfo *font = (FontInfo *)fonts->get(i); - printf("%-36s %-17s %-3s %-3s %-3s", - font->getName() ? font->getName()->getCString() : "[none]", - fontTypeNames[font->getType()], - font->getEmbedded() ? "yes" : "no", - font->getSubset() ? "yes" : "no", - font->getToUnicode() ? "yes" : "no"); - const Ref fontRef = font->getRef(); - if (fontRef.gen >= 100000) { - printf(" [none]\n"); - } else { - printf(" %6d %2d\n", fontRef.num, fontRef.gen); + if (showSubst) { + // print the font substitutions + printf("name object ID substitute font substitute font file\n"); + printf("------------------------------------ --------- ------------------------------------ ------------------------------------\n"); + if (fonts) { + for (int i = 0; i < fonts->getLength(); ++i) { + FontInfo *font = (FontInfo *)fonts->get(i); + if (font->getFile()) { + printf("%-36s", + font->getName() ? font->getName()->getCString() : "[none]"); + const Ref fontRef = font->getRef(); + if (fontRef.gen >= 100000) { + printf(" [none]"); + } else { + printf(" %6d %2d", fontRef.num, fontRef.gen); + } + printf(" %-36s %s\n", + font->getSubstituteName() ? font->getSubstituteName()->getCString() : "[none]", + font->getFile()->getCString()); + } + delete font; } - delete font; + delete fonts; + } + } else { + // print the font info + printf("name type emb sub uni object ID\n"); + printf("------------------------------------ ----------------- --- --- --- ---------\n"); + if (fonts) { + for (int i = 0; i < fonts->getLength(); ++i) { + FontInfo *font = (FontInfo *)fonts->get(i); + printf("%-36s %-17s %-3s %-3s %-3s", + font->getName() ? font->getName()->getCString() : "[none]", + fontTypeNames[font->getType()], + font->getEmbedded() ? "yes" : "no", + font->getSubset() ? "yes" : "no", + font->getToUnicode() ? "yes" : "no"); + const Ref fontRef = font->getRef(); + if (fontRef.gen >= 100000) { + printf(" [none]\n"); + } else { + printf(" %6d %2d\n", fontRef.num, fontRef.gen); + } + delete font; + } + delete fonts; } - delete fonts; } } -- 1.7.5.4