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

GitLab Mirror gitlab-mirror at kemper.freedesktop.org
Tue May 2 17:56:08 UTC 2023


 poppler/Form.cc |   21 ++++++++++++++++-----
 poppler/Form.h  |   10 ++++++----
 2 files changed, 22 insertions(+), 9 deletions(-)

New commits:
commit 62f2eb80fb2a4d4c656e7583584aa73fbc1de511
Author: Albert Astals Cid <aacid at kde.org>
Date:   Sun Apr 30 11:31:43 2023 +0200

    Form::ensureFontsForAllCharacters: Create resources if there's not one
    
    Fixes #1383

diff --git a/poppler/Form.cc b/poppler/Form.cc
index e63a4852..e76a2271 100644
--- a/poppler/Form.cc
+++ b/poppler/Form.cc
@@ -2735,12 +2735,12 @@ std::string Form::findFontInDefaultResources(const std::string &fontFamily, cons
     return {};
 }
 
-Form::AddFontResult Form::addFontToDefaultResources(const std::string &fontFamily, const std::string &fontStyle)
+Form::AddFontResult Form::addFontToDefaultResources(const std::string &fontFamily, const std::string &fontStyle, bool forceName)
 {
     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);
+        Form::AddFontResult addFontRes = addFontToDefaultResources(findFontRes.filepath, findFontRes.faceIndex, fontFamily, fontStyle, forceName);
         if (!addFontRes.fontName.empty()) {
             return addFontRes;
         }
@@ -2750,7 +2750,7 @@ Form::AddFontResult Form::addFontToDefaultResources(const std::string &fontFamil
     return {};
 }
 
-Form::AddFontResult Form::addFontToDefaultResources(const std::string &filepath, int faceIndex, const std::string &fontFamily, const std::string &fontStyle)
+Form::AddFontResult Form::addFontToDefaultResources(const std::string &filepath, int faceIndex, const std::string &fontFamily, const std::string &fontStyle, bool forceName)
 {
     if (!GooString::endsWith(filepath, ".ttf") && !GooString::endsWith(filepath, ".ttc") && !GooString::endsWith(filepath, ".otf")) {
         error(errIO, -1, "We only support embedding ttf/ttc/otf fonts for now. The font file for {0:s} {1:s} was {2:s}", fontFamily.c_str(), fontStyle.c_str(), filepath.c_str());
@@ -2765,6 +2765,11 @@ Form::AddFontResult Form::addFontToDefaultResources(const std::string &filepath,
 
     const std::string fontFamilyAndStyle = fontStyle.empty() ? fontFamily : fontFamily + " " + fontStyle;
 
+    if (forceName && defaultResources && defaultResources->lookupFont(fontFamilyAndStyle.c_str())) {
+        error(errInternal, -1, "Form::addFontToDefaultResources: Asked to forceName but font name exists {0:s}", fontFamilyAndStyle.c_str());
+        return {};
+    }
+
     XRef *xref = doc->getXRef();
     Object fontDict(new Dict(xref));
     fontDict.dictSet("Type", Object(objName, "Font"));
@@ -2921,7 +2926,7 @@ Form::AddFontResult Form::addFontToDefaultResources(const std::string &filepath,
 
     const Ref fontDictRef = xref->addIndirectObject(fontDict);
 
-    std::string dictFontName = kOurDictFontNamePrefix;
+    std::string dictFontName = forceName ? fontFamilyAndStyle : kOurDictFontNamePrefix;
     Object *acroForm = doc->getCatalog()->getAcroForm();
     if (resDict.isDict()) {
         Ref fontDictObjRef;
@@ -2973,7 +2978,13 @@ std::string Form::getFallbackFontForChar(Unicode uChar, const GfxFont &fontToEmu
 std::vector<Form::AddFontResult> Form::ensureFontsForAllCharacters(const GooString *unicodeText, const std::string &pdfFontNameToEmulate, GfxResources *fieldResources)
 {
     GfxResources *resources = fieldResources ? fieldResources : defaultResources;
-    std::shared_ptr<GfxFont> f = resources->lookupFont(pdfFontNameToEmulate.c_str());
+    std::shared_ptr<GfxFont> f;
+    if (!resources) {
+        // There's no resources, so create one with the needed font name
+        addFontToDefaultResources(pdfFontNameToEmulate, "", /*forceName*/ true);
+        resources = defaultResources;
+    }
+    f = resources->lookupFont(pdfFontNameToEmulate.c_str());
     const CharCodeToUnicode *ccToUnicode = f ? f->getToUnicode() : nullptr;
     if (!ccToUnicode) {
         error(errInternal, -1, "Form::ensureFontsForAllCharacters: No ccToUnicode, this should not happen\n");
diff --git a/poppler/Form.h b/poppler/Form.h
index 5ebdec0e..23c40701 100644
--- a/poppler/Form.h
+++ b/poppler/Form.h
@@ -692,8 +692,9 @@ public:
     };
 
     // Finds in the system a font name matching the given fontFamily and fontStyle
-    // And adds it to the default resources dictionary, font name there will be popplerfontXXX
-    AddFontResult addFontToDefaultResources(const std::string &fontFamily, const std::string &fontStyle);
+    // And adds it to the default resources dictionary, font name there will be popplerfontXXX except if forceName is true,
+    // in that case the font name will be fontFamily + " " + fontStyle (if fontStyle is empty just fontFamily)
+    AddFontResult addFontToDefaultResources(const std::string &fontFamily, const std::string &fontStyle, bool forceName = false);
 
     // Finds in the default resources dictionary a font named popplerfontXXX that
     // emulates fontToEmulate and can draw the given char
@@ -725,8 +726,9 @@ public:
 
 private:
     // Finds in the system a font name matching the given fontFamily and fontStyle
-    // And adds it to the default resources dictionary, font name there will be popplerfontXXX
-    AddFontResult addFontToDefaultResources(const std::string &filepath, int faceIndex, const std::string &fontFamily, const std::string &fontStyle);
+    // And adds it to the default resources dictionary, font name there will be popplerfontXXX except if forceName is true,
+    // in that case the font name will be fontFamily + " " + fontStyle (if fontStyle is empty just fontFamily)
+    AddFontResult addFontToDefaultResources(const std::string &filepath, int faceIndex, const std::string &fontFamily, const std::string &fontStyle, bool forceName = false);
 
     AddFontResult doGetAddFontToDefaultResources(Unicode uChar, const GfxFont &fontToEmulate);
 


More information about the poppler mailing list