include/vcl/filter/PDFiumLibrary.hxx |   18 +++++++---
 svx/source/inc/svdpdf.hxx            |    2 -
 svx/source/svdraw/svdpdf.cxx         |   19 ++++++----
 vcl/source/pdf/PDFiumLibrary.cxx     |   61 ++++++++++++++++++++++-------------
 4 files changed, 65 insertions(+), 35 deletions(-)

New commits:
commit 21930e3cfc08b9d562478f81854d0d95f73bab96
Author:     Caolán McNamara <[email protected]>
AuthorDate: Thu Oct 23 15:21:46 2025 +0100
Commit:     Caolán McNamara <[email protected]>
CommitDate: Fri Oct 24 10:12:53 2025 +0200

    make PDFiumFont a separate class
    
    Change-Id: I25c804bb9dee31cc26c18c8aa4038aeccd73f709
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/192921
    Reviewed-by: Mike Kaganski <[email protected]>
    Tested-by: Jenkins CollaboraOffice <[email protected]>

diff --git a/include/vcl/filter/PDFiumLibrary.hxx 
b/include/vcl/filter/PDFiumLibrary.hxx
index 4665172068d0..27725534b420 100644
--- a/include/vcl/filter/PDFiumLibrary.hxx
+++ b/include/vcl/filter/PDFiumLibrary.hxx
@@ -53,7 +53,18 @@ inline constexpr OString constDictionaryKey_RichContent = 
"RC"_ostr;
 class PDFiumBitmap;
 class PDFiumDocument;
 class PDFiumPageObject;
-typedef void* PDFiumFont;
+
+class PDFiumFont
+{
+public:
+    virtual ~PDFiumFont() = default;
+
+    virtual bool getFontData(std::vector<uint8_t>& rData) const = 0;
+    virtual bool getFontToUnicode(std::vector<uint8_t>& rData) const = 0;
+    virtual bool getIsEmbedded() const = 0;
+
+    virtual sal_Int64 getUniqueId() const = 0;
+};
 
 class VCL_DLLPUBLIC PDFium
 {
@@ -158,10 +169,7 @@ public:
     virtual OUString getFontName() = 0;
     virtual OUString getBaseFontName() = 0;
     virtual int getFontAngle() = 0;
-    virtual PDFiumFont getFont() = 0;
-    virtual bool getFontData(PDFiumFont font, std::vector<uint8_t>& rData) = 0;
-    virtual bool getFontToUnicode(PDFiumFont font, std::vector<uint8_t>& 
rData) = 0;
-    virtual bool getIsEmbedded(PDFiumFont font) = 0;
+    virtual std::unique_ptr<PDFiumFont> getFont() = 0;
     virtual bool getFontProperties(FontWeight& weight) = 0;
     virtual PDFTextRenderMode getTextRenderMode() = 0;
     virtual Color getFillColor() = 0;
diff --git a/svx/source/inc/svdpdf.hxx b/svx/source/inc/svdpdf.hxx
index 73d43ebee2fb..054462079185 100644
--- a/svx/source/inc/svdpdf.hxx
+++ b/svx/source/inc/svdpdf.hxx
@@ -93,7 +93,7 @@ class ImpSdrPdfImport final
 {
     std::vector<rtl::Reference<SdrObject>> maTmpList;
 
-    std::map<vcl::pdf::PDFiumFont, OfficeFontInfo> maImportedFonts;
+    std::map<sal_Int64, OfficeFontInfo> maImportedFonts;
     std::map<OUString, SubSetInfo> maDifferentSubsetsForFont;
     // map of PostScriptName->Merged Font File for that font
     std::map<OUString, EmbeddedFontInfo> maEmbeddedFonts;
diff --git a/svx/source/svdraw/svdpdf.cxx b/svx/source/svdraw/svdpdf.cxx
index ed9d8ec1d719..93865fff984c 100644
--- a/svx/source/svdraw/svdpdf.cxx
+++ b/svx/source/svdraw/svdpdf.cxx
@@ -248,11 +248,11 @@ void ImpSdrPdfImport::CollectFonts()
             const vcl::pdf::PDFPageObjectType ePageObjectType = 
pPageObject->getType();
             if (ePageObjectType != vcl::pdf::PDFPageObjectType::Text)
                 continue;
-            vcl::pdf::PDFiumFont font = pPageObject->getFont();
+            std::unique_ptr<vcl::pdf::PDFiumFont> font = 
pPageObject->getFont();
             if (!font)
                 continue;
 
-            auto itImportedFont = maImportedFonts.find(font);
+            auto itImportedFont = maImportedFonts.find(font->getUniqueId());
             if (itImportedFont == maImportedFonts.end())
             {
                 OUString sPostScriptName = 
GetPostScriptName(pPageObject->getBaseFontName());
@@ -270,17 +270,17 @@ void ImpSdrPdfImport::CollectFonts()
                                  << sFontName);
                 }
 
-                if (!pPageObject->getIsEmbedded(font))
+                if (!font->getIsEmbedded())
                 {
                     SAL_WARN("sd.filter", "skipping not embedded font, map: "
                                               << sFontName << " to " << 
sPostScriptFontFamily);
-                    maImportedFonts.emplace(font,
+                    maImportedFonts.emplace(font->getUniqueId(),
                                             OfficeFontInfo{ 
sPostScriptFontFamily, eFontWeight });
                     continue;
                 }
 
                 std::vector<uint8_t> aFontData;
-                if (!pPageObject->getFontData(font, aFontData) || 
aFontData.empty())
+                if (!font->getFontData(aFontData) || aFontData.empty())
                 {
                     SAL_WARN("sd.filter", "that's worrying, skipping " << 
sFontName);
                     continue;
@@ -316,7 +316,7 @@ void ImpSdrPdfImport::CollectFonts()
                 else
                     SAL_INFO("sd.filter", "ttf written to: " << fileUrl);
                 std::vector<uint8_t> aToUnicodeData;
-                if (!pPageObject->getFontToUnicode(font, aToUnicodeData))
+                if (!font->getFontToUnicode(aToUnicodeData))
                     SAL_WARN("sd.filter", "that's maybe worrying");
                 if (!bTTF || !aToUnicodeData.empty())
                 {
@@ -330,7 +330,8 @@ void ImpSdrPdfImport::CollectFonts()
 
                 if (fileUrl.getLength())
                 {
-                    maImportedFonts.emplace(font, OfficeFontInfo{ sFontName, 
eFontWeight });
+                    maImportedFonts.emplace(font->getUniqueId(),
+                                            OfficeFontInfo{ sFontName, 
eFontWeight });
                     maEmbeddedFonts[sPostScriptName]
                         = EmbeddedFontInfo{ sFontName, fileUrl, eFontWeight };
                 }
@@ -1876,7 +1877,9 @@ void 
ImpSdrPdfImport::ImportText(std::unique_ptr<vcl::pdf::PDFiumPageObject> con
 
     OUString sFontName;
     FontWeight eFontWeight(WEIGHT_DONTKNOW);
-    auto itImportedFont = maImportedFonts.find(pPageObject->getFont());
+    auto xFont = pPageObject->getFont();
+    auto itImportedFont
+        = xFont ? maImportedFonts.find(xFont->getUniqueId()) : 
maImportedFonts.end();
     if (itImportedFont != maImportedFonts.end())
     {
         // We expand a name like "Foo" with non-traditional styles like
diff --git a/vcl/source/pdf/PDFiumLibrary.cxx b/vcl/source/pdf/PDFiumLibrary.cxx
index cb2ff7f377a5..70aa8812b72b 100644
--- a/vcl/source/pdf/PDFiumLibrary.cxx
+++ b/vcl/source/pdf/PDFiumLibrary.cxx
@@ -419,10 +419,7 @@ public:
     OUString getFontName() override;
     OUString getBaseFontName() override;
     int getFontAngle() override;
-    PDFiumFont getFont() override;
-    bool getFontData(PDFiumFont font, std::vector<uint8_t>& rData) override;
-    bool getFontToUnicode(PDFiumFont font, std::vector<uint8_t>& rData) 
override;
-    bool getIsEmbedded(PDFiumFont font) override;
+    std::unique_ptr<PDFiumFont> getFont() override;
     bool getFontProperties(FontWeight& weight) override;
     PDFTextRenderMode getTextRenderMode() override;
     Color getFillColor() override;
@@ -440,6 +437,24 @@ public:
     bool getDrawMode(PDFFillMode& eFillMode, bool& bStroke) override;
 };
 
+class PDFiumFontImpl final : public PDFiumFont
+{
+private:
+    FPDF_FONT mpFont;
+
+    PDFiumFontImpl(const PDFiumFontImpl&) = delete;
+    PDFiumFontImpl& operator=(const PDFiumFontImpl&) = delete;
+
+public:
+    PDFiumFontImpl(FPDF_FONT pFont);
+
+    bool getFontData(std::vector<uint8_t>& rData) const override;
+    bool getFontToUnicode(std::vector<uint8_t>& rData) const override;
+    bool getIsEmbedded() const override;
+
+    sal_Int64 getUniqueId() const override { return 
reinterpret_cast<sal_Int64>(mpFont); }
+};
+
 class PDFiumSearchHandleImpl final : public PDFiumSearchHandle
 {
 private:
@@ -1237,46 +1252,50 @@ int PDFiumPageObjectImpl::getFontAngle()
     return nFontAngle;
 }
 
-PDFiumFont PDFiumPageObjectImpl::getFont() { return 
FPDFTextObj_GetFont(mpPageObject); }
+std::unique_ptr<PDFiumFont> PDFiumPageObjectImpl::getFont()
+{
+    std::unique_ptr<PDFiumFont> pPDFiumFont;
+    if (FPDF_FONT pFont = FPDFTextObj_GetFont(mpPageObject))
+        pPDFiumFont = std::make_unique<PDFiumFontImpl>(pFont);
+    return pPDFiumFont;
+}
+
+PDFiumFontImpl::PDFiumFontImpl(FPDF_FONT pFont)
+    : mpFont(pFont)
+{
+}
 
-bool PDFiumPageObjectImpl::getFontData(PDFiumFont font, std::vector<uint8_t>& 
rData)
+bool PDFiumFontImpl::getFontData(std::vector<uint8_t>& rData) const
 {
-    FPDF_FONT pFontObject = static_cast<FPDF_FONT>(font);
     size_t buflen(0);
-    bool bOk = FPDFFont_GetFontData(pFontObject, nullptr, 0, &buflen);
+    bool bOk = FPDFFont_GetFontData(mpFont, nullptr, 0, &buflen);
     if (!bOk)
     {
         SAL_WARN("vcl.filter", "PDFiumImpl: failed to get font data");
         return false;
     }
     rData.resize(buflen);
-    bOk = FPDFFont_GetFontData(pFontObject, rData.data(), rData.size(), 
&buflen);
+    bOk = FPDFFont_GetFontData(mpFont, rData.data(), rData.size(), &buflen);
     assert(bOk && rData.size() == buflen);
     return bOk;
 }
 
-bool PDFiumPageObjectImpl::getFontToUnicode(PDFiumFont font, 
std::vector<uint8_t>& rData)
+bool PDFiumFontImpl::getFontToUnicode(std::vector<uint8_t>& rData) const
 {
-    FPDF_FONT pFontObject = static_cast<FPDF_FONT>(font);
-
     size_t buflen(0);
-    bool bOk = FPDFFont_GetToUnicodeContent(pFontObject, nullptr, 0, &buflen);
+    bool bOk = FPDFFont_GetToUnicodeContent(mpFont, nullptr, 0, &buflen);
     if (!bOk)
     {
         SAL_WARN("vcl.filter", "PDFiumImpl: failed to get font data");
         return false;
     }
     rData.resize(buflen);
-    bOk = FPDFFont_GetToUnicodeContent(pFontObject, rData.data(), 
rData.size(), &buflen);
+    bOk = FPDFFont_GetToUnicodeContent(mpFont, rData.data(), rData.size(), 
&buflen);
     assert(bOk && rData.size() == buflen);
     return bOk;
 }
 
-bool PDFiumPageObjectImpl::getIsEmbedded(PDFiumFont font)
-{
-    FPDF_FONT pFontObject = static_cast<FPDF_FONT>(font);
-    return FPDFFont_GetIsEmbedded(pFontObject) == 1;
-}
+bool PDFiumFontImpl::getIsEmbedded() const { return 
FPDFFont_GetIsEmbedded(mpFont) == 1; }
 
 bool PDFiumPageObjectImpl::getFontProperties(FontWeight& weight)
 {
@@ -1286,11 +1305,11 @@ bool 
PDFiumPageObjectImpl::getFontProperties(FontWeight& weight)
     // So pull the font data and analyze it directly. Though the font might not
     // have an OS/2 table so we may end up eventually inferring the weight from
     // the style name.
-    PDFiumFont font = getFont();
+    auto font = getFont();
     if (!font)
         return false;
     std::vector<uint8_t> aData;
-    if (!getFontData(font, aData))
+    if (!font->getFontData(aData))
         return false;
     if (!EmbeddedFontsManager::analyzeTTF(aData.data(), aData.size(), weight))
     {

Reply via email to