[poppler] poppler/Form.cc poppler/GlobalParams.cc poppler/GlobalParams.h poppler/GlobalParamsWin.cc

GitLab Mirror gitlab-mirror at kemper.freedesktop.org
Sun Nov 20 00:39:51 UTC 2022


 poppler/Form.cc            |   14 +++++++++---
 poppler/GlobalParams.cc    |   52 +++++++++++++++++++++++++--------------------
 poppler/GlobalParams.h     |    6 ++---
 poppler/GlobalParamsWin.cc |    4 +--
 4 files changed, 46 insertions(+), 30 deletions(-)

New commits:
commit 198dc1d0674c0a462668e6868c35b1ee0e731005
Author: Albert Astals Cid <aacid at kde.org>
Date:   Wed Sep 7 01:12:35 2022 +0200

    Form::addFontToDefaultResources: Be stubborn in finding a font we can use
    
    Fixes #1272

diff --git a/poppler/Form.cc b/poppler/Form.cc
index 9407b6c5..5e885fa1 100644
--- a/poppler/Form.cc
+++ b/poppler/Form.cc
@@ -2727,9 +2727,17 @@ std::string Form::findFontInDefaultResources(const std::string &fontFamily, cons
 
 Form::AddFontResult Form::addFontToDefaultResources(const std::string &fontFamily, const std::string &fontStyle)
 {
-    const FamilyStyleFontSearchResult res = globalParams->findSystemFontFileForFamilyAndStyle(fontFamily, fontStyle);
-
-    return addFontToDefaultResources(res.filepath, res.faceIndex, fontFamily, fontStyle);
+    FamilyStyleFontSearchResult findFontRes = globalParams->findSystemFontFileForFamilyAndStyle(fontFamily, fontStyle);
+    std::vector<std::string> filesToIgnore;
+    while (!findFontRes.filepath.empty()) {
+        Form::AddFontResult addFontRes = addFontToDefaultResources(findFontRes.filepath, findFontRes.faceIndex, fontFamily, fontStyle);
+        if (!addFontRes.fontName.empty()) {
+            return addFontRes;
+        }
+        filesToIgnore.emplace_back(findFontRes.filepath);
+        findFontRes = globalParams->findSystemFontFileForFamilyAndStyle(fontFamily, fontStyle, filesToIgnore);
+    }
+    return {};
 }
 
 Form::AddFontResult Form::addFontToDefaultResources(const std::string &filepath, int faceIndex, const std::string &fontFamily, const std::string &fontStyle)
diff --git a/poppler/GlobalParams.cc b/poppler/GlobalParams.cc
index cf635dce..a1fd8973 100644
--- a/poppler/GlobalParams.cc
+++ b/poppler/GlobalParams.cc
@@ -254,7 +254,7 @@ public:
     ~SysFontList();
     SysFontList(const SysFontList &) = delete;
     SysFontList &operator=(const SysFontList &) = delete;
-    const SysFontInfo *find(const std::string &name, bool isFixedWidth, bool exact);
+    const SysFontInfo *find(const std::string &name, bool isFixedWidth, bool exact, const std::vector<std::string> &filesToIgnore = {});
 
     const std::vector<SysFontInfo *> &getFonts() const { return fonts; }
 
@@ -284,7 +284,7 @@ SysFontList::~SysFontList()
     }
 }
 
-const SysFontInfo *SysFontList::find(const std::string &name, bool fixedWidth, bool exact)
+const SysFontInfo *SysFontList::find(const std::string &name, bool fixedWidth, bool exact, const std::vector<std::string> &filesToIgnore)
 {
     GooString *name2;
     bool bold, italic, oblique;
@@ -368,7 +368,9 @@ const SysFontInfo *SysFontList::find(const std::string &name, bool fixedWidth, b
     for (const SysFontInfo *f : fonts) {
         fi = f;
         if (fi->match(name2, bold, italic, oblique, fixedWidth)) {
-            break;
+            if (std::find(filesToIgnore.begin(), filesToIgnore.end(), fi->path->toStr()) == filesToIgnore.end()) {
+                break;
+            }
         }
         fi = nullptr;
     }
@@ -377,7 +379,9 @@ const SysFontInfo *SysFontList::find(const std::string &name, bool fixedWidth, b
         for (const SysFontInfo *f : fonts) {
             fi = f;
             if (fi->match(name2, false, italic)) {
-                break;
+                if (std::find(filesToIgnore.begin(), filesToIgnore.end(), fi->path->toStr()) == filesToIgnore.end()) {
+                    break;
+                }
             }
             fi = nullptr;
         }
@@ -387,7 +391,9 @@ const SysFontInfo *SysFontList::find(const std::string &name, bool fixedWidth, b
         for (const SysFontInfo *f : fonts) {
             fi = f;
             if (fi->match(name2, false, false)) {
-                break;
+                if (std::find(filesToIgnore.begin(), filesToIgnore.end(), fi->path->toStr()) == filesToIgnore.end()) {
+                    break;
+                }
             }
             fi = nullptr;
         }
@@ -1104,32 +1110,34 @@ fin:
     return path;
 }
 
-FamilyStyleFontSearchResult GlobalParams::findSystemFontFileForFamilyAndStyle(const std::string &fontFamily, const std::string &fontStyle)
+FamilyStyleFontSearchResult GlobalParams::findSystemFontFileForFamilyAndStyle(const std::string &fontFamily, const std::string &fontStyle, const std::vector<std::string> &filesToIgnore)
 {
-    FcChar8 *fcFilePath = nullptr;
-    int faceIndex = 0;
     FcPattern *p = FcPatternBuild(nullptr, FC_FAMILY, FcTypeString, fontFamily.c_str(), FC_STYLE, FcTypeString, fontStyle.c_str(), nullptr);
     FcConfigSubstitute(nullptr, p, FcMatchPattern);
     FcDefaultSubstitute(p);
     if (p) {
         FcResult res;
-        FcFontSet *set = FcFontSort(nullptr, p, FcFalse, nullptr, &res);
-        if (set) {
-            if (res == FcResultMatch && set->nfont > 0) {
-                FcPatternGetString(set->fonts[0], FC_FILE, 0, &fcFilePath);
-                FcPatternGetInteger(set->fonts[0], FC_INDEX, 0, &faceIndex);
+        FcFontSet *fontSet = FcFontSort(nullptr, p, FcFalse, nullptr, &res);
+        if (fontSet) {
+            const std::unique_ptr<FcFontSet, void (*)(FcFontSet *)> fontSetDeleter(fontSet, [](FcFontSet *fSet) { FcFontSetDestroy(fSet); });
+            if (res == FcResultMatch) {
+                for (int i = 0; i < fontSet->nfont; i++) {
+                    FcChar8 *fcFilePath = nullptr;
+                    int faceIndex = 0;
+                    FcPatternGetString(fontSet->fonts[i], FC_FILE, 0, &fcFilePath);
+                    FcPatternGetInteger(fontSet->fonts[i], FC_INDEX, 0, &faceIndex);
+
+                    const std::string sFilePath = reinterpret_cast<char *>(fcFilePath);
+                    if (std::find(filesToIgnore.begin(), filesToIgnore.end(), sFilePath) == filesToIgnore.end()) {
+                        return FamilyStyleFontSearchResult(sFilePath, faceIndex);
+                    }
+                }
             }
-            FcFontSetDestroy(set);
         }
-        FcPatternDestroy(p);
     }
 
-    if (!fcFilePath) {
-        error(errIO, -1, "Couldn't find font file for {0:s} {1:s}", fontFamily.c_str(), fontStyle.c_str());
-        return {};
-    }
-
-    return FamilyStyleFontSearchResult(reinterpret_cast<char *>(fcFilePath), faceIndex);
+    error(errIO, -1, "Couldn't find font file for {0:s} {1:s}", fontFamily.c_str(), fontStyle.c_str());
+    return {};
 }
 
 UCharFontSearchResult GlobalParams::findSystemFontFileForUChar(Unicode uChar, const GfxFont &fontToEmulate)
@@ -1179,7 +1187,7 @@ GooString *GlobalParams::findBase14FontFile(const GooString *base14Name, const G
 
 #else
 
-FamilyStyleFontSearchResult GlobalParams::findSystemFontFileForFamilyAndStyle(const std::string &fontFamily, const std::string &fontStyle)
+FamilyStyleFontSearchResult GlobalParams::findSystemFontFileForFamilyAndStyle(const std::string &fontFamily, const std::string &fontStyle, const std::vector<std::string> &filesToIgnore)
 {
     error(errUnimplemented, -1, "GlobalParams::findSystemFontFileForFamilyAndStyle not implemented for this platform");
     return {};
diff --git a/poppler/GlobalParams.h b/poppler/GlobalParams.h
index af3e53dc..67399f62 100644
--- a/poppler/GlobalParams.h
+++ b/poppler/GlobalParams.h
@@ -85,8 +85,8 @@ struct FamilyStyleFontSearchResult
 
     FamilyStyleFontSearchResult(const std::string &filepathA, int faceIndexA) : filepath(filepathA), faceIndex(faceIndexA) { }
 
-    const std::string filepath;
-    const int faceIndex = 0;
+    std::string filepath;
+    int faceIndex = 0;
 };
 
 //------------------------------------------------------------------------
@@ -137,7 +137,7 @@ public:
     GooString *findFontFile(const std::string &fontName);
     GooString *findBase14FontFile(const GooString *base14Name, const GfxFont *font);
     GooString *findSystemFontFile(const GfxFont *font, SysFontType *type, int *fontNum, GooString *substituteFontName = nullptr, const GooString *base14Name = nullptr);
-    FamilyStyleFontSearchResult findSystemFontFileForFamilyAndStyle(const std::string &fontFamily, const std::string &fontStyle);
+    FamilyStyleFontSearchResult findSystemFontFileForFamilyAndStyle(const std::string &fontFamily, const std::string &fontStyle, const std::vector<std::string> &filesToIgnore = {});
     UCharFontSearchResult findSystemFontFileForUChar(Unicode uChar, const GfxFont &fontToEmulate);
     std::string getTextEncodingName() const;
     bool getPrintCommands();
diff --git a/poppler/GlobalParamsWin.cc b/poppler/GlobalParamsWin.cc
index d33c09bd..93b47d03 100644
--- a/poppler/GlobalParamsWin.cc
+++ b/poppler/GlobalParamsWin.cc
@@ -503,14 +503,14 @@ GooString *GlobalParams::findSystemFontFile(const GfxFont *font, SysFontType *ty
     return path;
 }
 
-FamilyStyleFontSearchResult GlobalParams::findSystemFontFileForFamilyAndStyle(const std::string &fontFamily, const std::string &fontStyle)
+FamilyStyleFontSearchResult GlobalParams::findSystemFontFileForFamilyAndStyle(const std::string &fontFamily, const std::string &fontStyle, const std::vector<std::string> &filesToIgnore)
 {
     const std::scoped_lock locker(mutex);
     setupBaseFonts(POPPLER_FONTSDIR);
 
     const std::string familyAndStyle = fontFamily + " " + fontStyle;
 
-    const SysFontInfo *fi = sysFonts->find(familyAndStyle, false, false);
+    const SysFontInfo *fi = sysFonts->find(familyAndStyle, false, false, filesToIgnore);
     if (fi) {
         return FamilyStyleFontSearchResult(fi->path->toStr(), fi->fontNum);
     }


More information about the poppler mailing list