vcl/inc/font/PhysicalFontFace.hxx              |    3 +
 vcl/inc/qt5/QtFontFace.hxx                     |    3 -
 vcl/inc/quartz/salgdi.h                        |    3 -
 vcl/inc/unx/freetype_glyphcache.hxx            |    5 --
 vcl/inc/unx/glyphcache.hxx                     |    1 
 vcl/inc/win/salgdi.h                           |   20 -----------
 vcl/qt5/QtFontFace.cxx                         |   24 -------------
 vcl/quartz/salgdi.cxx                          |   41 -----------------------
 vcl/source/font/PhysicalFontFace.cxx           |   24 +++++++++++++
 vcl/unx/generic/gdi/freetypetextrender.cxx     |    2 -
 vcl/unx/generic/glyphs/freetype_glyphcache.cxx |   33 ------------------
 vcl/unx/generic/print/genpspgraphics.cxx       |    2 -
 vcl/win/gdi/salfont.cxx                        |   44 -------------------------
 13 files changed, 30 insertions(+), 175 deletions(-)

New commits:
commit ca7d6be2d64a48e61db7a1c614d6325151817f59
Author:     Khaled Hosny <kha...@aliftype.com>
AuthorDate: Sun Sep 4 20:39:22 2022 +0200
Commit:     خالد حسني <kha...@aliftype.com>
CommitDate: Mon Sep 5 06:09:11 2022 +0200

    vcl: Consolidate PhysicalFontFace::GetFontCharMap()
    
    All subclasses are doing the same thing in slightly different ways, so
    move it to the base class that we can now access font tables there.
    
    Also drop unused PhysicalFontFace::HasChar().
    
    Change-Id: I77022b4dc3688de2788c18966f39f407a6abb730
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/139340
    Tested-by: Jenkins
    Reviewed-by: خالد حسني <kha...@aliftype.com>

diff --git a/vcl/inc/font/PhysicalFontFace.hxx 
b/vcl/inc/font/PhysicalFontFace.hxx
index d4851ba507be..52acaacf08ec 100644
--- a/vcl/inc/font/PhysicalFontFace.hxx
+++ b/vcl/inc/font/PhysicalFontFace.hxx
@@ -73,7 +73,7 @@ public:
     virtual rtl::Reference<LogicalFontInstance> CreateFontInstance(const 
vcl::font::FontSelectPattern&) const = 0;
 
     virtual sal_IntPtr      GetFontId() const = 0;
-    virtual FontCharMapRef GetFontCharMap() const = 0;
+    virtual FontCharMapRef  GetFontCharMap() const;
     virtual bool GetFontCapabilities(vcl::FontCapabilities&) const = 0;
 
     bool                    IsBetterMatch( const 
vcl::font::FontSelectPattern&, FontMatchStatus& ) const;
@@ -84,6 +84,7 @@ public:
 
 protected:
     mutable hb_face_t*      mpHbFace;
+    mutable FontCharMapRef  mxCharMap;
 
     explicit PhysicalFontFace(const FontAttributes&);
 };
diff --git a/vcl/inc/qt5/QtFontFace.hxx b/vcl/inc/qt5/QtFontFace.hxx
index eb4846ef772d..3db0c8a6eb5b 100644
--- a/vcl/inc/qt5/QtFontFace.hxx
+++ b/vcl/inc/qt5/QtFontFace.hxx
@@ -52,9 +52,7 @@ public:
 
     QFont CreateFont() const;
 
-    FontCharMapRef GetFontCharMap() const override;
     bool GetFontCapabilities(vcl::FontCapabilities&) const override;
-    bool HasChar(sal_uInt32 cChar) const;
 
     rtl::Reference<LogicalFontInstance>
     CreateFontInstance(const vcl::font::FontSelectPattern& rFSD) const 
override;
@@ -69,7 +67,6 @@ private:
 
     const QString m_aFontId;
     const FontIdType m_eFontIdType;
-    mutable FontCharMapRef m_xCharMap;
     mutable vcl::FontCapabilities m_aFontCapabilities;
     mutable bool m_bFontCapabilitiesRead;
 };
diff --git a/vcl/inc/quartz/salgdi.h b/vcl/inc/quartz/salgdi.h
index 9fc3e6b2d74c..cbdffdeecc19 100644
--- a/vcl/inc/quartz/salgdi.h
+++ b/vcl/inc/quartz/salgdi.h
@@ -69,9 +69,7 @@ public:
     int                             GetFontTable( uint32_t nTagCode, unsigned 
char* ) const;
     int                             GetFontTable( const char pTagName[5], 
unsigned char* ) const;
 
-    FontCharMapRef GetFontCharMap() const override;
     bool GetFontCapabilities(vcl::FontCapabilities&) const override;
-    bool                            HasChar( sal_uInt32 cChar ) const;
 
     rtl::Reference<LogicalFontInstance> CreateFontInstance(const 
vcl::font::FontSelectPattern&) const override;
 
@@ -79,7 +77,6 @@ public:
 
 private:
     const sal_IntPtr                mnFontId;
-    mutable FontCharMapRef          mxCharMap;
     mutable vcl::FontCapabilities   maFontCapabilities;
     mutable bool                    mbFontCapabilitiesRead;
 };
diff --git a/vcl/inc/unx/freetype_glyphcache.hxx 
b/vcl/inc/unx/freetype_glyphcache.hxx
index c3ca90591f59..b77978061d09 100644
--- a/vcl/inc/unx/freetype_glyphcache.hxx
+++ b/vcl/inc/unx/freetype_glyphcache.hxx
@@ -27,8 +27,6 @@
 
 #include <glyphid.hxx>
 
-class CmapResult;
-
 // FreetypeFontFile has the responsibility that a font file is only mapped 
once.
 // (#86621#) the old directly ft-managed solution caused it to be mapped
 // in up to nTTC*nSizes*nOrientation*nSynthetic times
@@ -74,7 +72,6 @@ public:
 
     void                  AnnounceFont( vcl::font::PhysicalFontCollection* );
 
-    const FontCharMapRef & GetFontCharMap() const;
     bool GetFontCapabilities(vcl::FontCapabilities&) const;
 
 private:
@@ -90,7 +87,6 @@ private:
     sal_IntPtr      mnFontId;
     FontAttributes  maDevFontAttributes;
 
-    mutable FontCharMapRef mxFontCharMap;
 };
 
 class FreetypeFontFace : public vcl::font::PhysicalFontFace
@@ -104,7 +100,6 @@ public:
     virtual rtl::Reference<LogicalFontInstance> CreateFontInstance(const 
vcl::font::FontSelectPattern&) const override;
     virtual sal_IntPtr      GetFontId() const override { return 
mpFreetypeFontInfo->GetFontId(); }
 
-    FontCharMapRef GetFontCharMap() const override { return 
mpFreetypeFontInfo->GetFontCharMap(); }
     inline bool GetFontCapabilities(vcl::FontCapabilities&) const override;
 
     virtual hb_face_t* GetHbFace() const override;
diff --git a/vcl/inc/unx/glyphcache.hxx b/vcl/inc/unx/glyphcache.hxx
index 27ddd6cb32b5..81bf645626d5 100644
--- a/vcl/inc/unx/glyphcache.hxx
+++ b/vcl/inc/unx/glyphcache.hxx
@@ -125,7 +125,6 @@ public:
 
     void                    GetFontMetric(ImplFontMetricDataRef const &) const;
     const unsigned char*    GetTable( const char* pName, sal_uLong* pLength ) 
const;
-    const FontCharMapRef &  GetFontCharMap() const;
     bool                    GetFontCapabilities(vcl::FontCapabilities &) const;
 
     bool                    GetGlyphBoundRect(sal_GlyphId, tools::Rectangle&, 
bool) const;
diff --git a/vcl/inc/win/salgdi.h b/vcl/inc/win/salgdi.h
index 3f442614c3d3..0faedda67585 100644
--- a/vcl/inc/win/salgdi.h
+++ b/vcl/inc/win/salgdi.h
@@ -68,12 +68,9 @@ public:
     virtual sal_IntPtr      GetFontId() const override;
     void                    SetFontId( sal_IntPtr nId ) { mnId = nId; }
 
-    bool                    HasChar( sal_uInt32 cChar ) const;
-
     BYTE                    GetCharSet() const          { return meWinCharSet; 
}
     BYTE                    GetPitchAndFamily() const   { return 
mnPitchAndFamily; }
 
-    FontCharMapRef GetFontCharMap() const override;
     bool GetFontCapabilities(vcl::FontCapabilities&) const override;
 
     virtual hb_blob_t*      GetHbTable(hb_tag_t nTag) const override;
@@ -83,7 +80,6 @@ private:
 
     // some members that are initialized lazily when the font gets selected 
into a HDC
     mutable bool                    mbFontCapabilitiesRead;
-    mutable FontCharMapRef          mxUnicodeMap;
     mutable vcl::FontCapabilities   maFontCapabilities;
 
     BYTE                    meWinCharSet;
@@ -92,7 +88,6 @@ private:
     bool                    mbAliasSymbolsLow;
     HFONT                   mhFont;
 
-    void                    ReadCmapTable() const;
     void                    GetFontCapabilities() const;
 };
 
@@ -414,19 +409,4 @@ void    ImplGetLogFontFromFontSelect( const 
vcl::font::FontSelectPattern&,
 
 #define MAX_64KSALPOINTS    ((((sal_uInt16)0xFFFF)-8)/sizeof(POINTS))
 
-// called extremely often from just one spot => inline
-inline bool WinFontFace::HasChar( sal_uInt32 cChar ) const
-{
-    if( mxUnicodeMap->HasChar( cChar ) )
-        return true;
-    // second chance to allow symbol aliasing
-    if( mbAliasSymbolsLow && ((cChar-0xF000) <= 0xFF) )
-        cChar -= 0xF000;
-    else if( mbAliasSymbolsHigh && (cChar <= 0xFF) )
-        cChar += 0xF000;
-    else
-        return false;
-    return mxUnicodeMap->HasChar( cChar );
-}
-
 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/vcl/qt5/QtFontFace.cxx b/vcl/qt5/QtFontFace.cxx
index 60e42422d454..c449cca690c3 100644
--- a/vcl/qt5/QtFontFace.cxx
+++ b/vcl/qt5/QtFontFace.cxx
@@ -44,8 +44,6 @@ QtFontFace::QtFontFace(const QtFontFace& rSrc)
     , m_aFontId(rSrc.m_aFontId)
     , m_eFontIdType(rSrc.m_eFontIdType)
 {
-    if (rSrc.m_xCharMap.is())
-        m_xCharMap = rSrc.m_xCharMap;
 }
 
 FontWeight QtFontFace::toFontWeight(const int nWeight)
@@ -207,28 +205,6 @@ QtFontFace::CreateFontInstance(const 
vcl::font::FontSelectPattern& rFSD) const
     return new QtFont(*this, rFSD);
 }
 
-FontCharMapRef QtFontFace::GetFontCharMap() const
-{
-    if (m_xCharMap.is())
-        return m_xCharMap;
-
-    QFont aFont = CreateFont();
-    QRawFont aRawFont(QRawFont::fromFont(aFont));
-    QByteArray aCMapTable = aRawFont.fontTable("cmap");
-    if (aCMapTable.isEmpty())
-    {
-        m_xCharMap = new FontCharMap();
-        return m_xCharMap;
-    }
-
-    CmapResult aCmapResult;
-    if (ParseCMAP(reinterpret_cast<const unsigned char*>(aCMapTable.data()), 
aCMapTable.size(),
-                  aCmapResult))
-        m_xCharMap = new FontCharMap(aCmapResult);
-
-    return m_xCharMap;
-}
-
 bool QtFontFace::GetFontCapabilities(vcl::FontCapabilities& rFontCapabilities) 
const
 {
     // read this only once per font
diff --git a/vcl/quartz/salgdi.cxx b/vcl/quartz/salgdi.cxx
index 8634bfa8ff9b..f893fe20fae4 100644
--- a/vcl/quartz/salgdi.cxx
+++ b/vcl/quartz/salgdi.cxx
@@ -42,7 +42,6 @@
 #include <quartz/ctfonts.hxx>
 #include <fontsubset.hxx>
 #include <impfont.hxx>
-#include <impfontcharmap.hxx>
 #include <impfontmetricdata.hxx>
 #include <font/fontsubstitution.hxx>
 #include <font/PhysicalFontCollection.hxx>
@@ -144,44 +143,6 @@ sal_IntPtr CoreTextFontFace::GetFontId() const
     return mnFontId;
 }
 
-FontCharMapRef CoreTextFontFace::GetFontCharMap() const
-{
-    // return the cached charmap
-    if( mxCharMap.is() )
-        return mxCharMap;
-
-    // set the default charmap
-    FontCharMapRef pCharMap( new FontCharMap() );
-    mxCharMap = pCharMap;
-
-    // get the CMAP byte size
-    // allocate a buffer for the CMAP raw data
-    const int nBufSize = GetFontTable( "cmap", nullptr );
-    SAL_WARN_IF( (nBufSize <= 0), "vcl", "CoreTextFontFace::GetFontCharMap : 
GetFontTable1 failed!");
-    if( nBufSize <= 0 )
-        return mxCharMap;
-
-    // get the CMAP raw data
-    std::vector<unsigned char> aBuffer( nBufSize );
-    const int nRawLength = GetFontTable( "cmap", aBuffer.data() );
-    SAL_WARN_IF( (nRawLength <= 0), "vcl", "CoreTextFontFace::GetFontCharMap : 
GetFontTable2 failed!");
-    if( nRawLength <= 0 )
-        return mxCharMap;
-
-    SAL_WARN_IF( (nBufSize!=nRawLength), "vcl", 
"CoreTextFontFace::GetFontCharMap : ByteCount mismatch!");
-
-    // parse the CMAP
-    CmapResult aCmapResult;
-    if( ParseCMAP( aBuffer.data(), nRawLength, aCmapResult ) )
-    {
-        FontCharMapRef xDefFontCharMap( new FontCharMap(aCmapResult) );
-        // create the matching charmap
-        mxCharMap = xDefFontCharMap;
-    }
-
-    return mxCharMap;
-}
-
 bool CoreTextFontFace::GetFontCapabilities(vcl::FontCapabilities 
&rFontCapabilities) const
 {
     // read this only once per font
@@ -543,7 +504,7 @@ FontCharMapRef AquaSalGraphics::GetFontCharMap() const
         return FontCharMapRef( new FontCharMap() );
     }
 
-    return static_cast<const 
CoreTextFontFace*>(mpTextStyle[0]->GetFontFace())->GetFontCharMap();
+    return mpTextStyle[0]->GetFontFace()->GetFontCharMap();
 }
 
 bool AquaSalGraphics::GetFontCapabilities(vcl::FontCapabilities 
&rFontCapabilities) const
diff --git a/vcl/source/font/PhysicalFontFace.cxx 
b/vcl/source/font/PhysicalFontFace.cxx
index 9969304e512a..917295f6b50b 100644
--- a/vcl/source/font/PhysicalFontFace.cxx
+++ b/vcl/source/font/PhysicalFontFace.cxx
@@ -24,6 +24,7 @@
 #include <unotools/fontdefs.hxx>
 
 #include <fontattributes.hxx>
+#include <impfontcharmap.hxx>
 
 #include <font/FontSelectPattern.hxx>
 #include <font/PhysicalFontFace.hxx>
@@ -220,6 +221,29 @@ hb_face_t* PhysicalFontFace::GetHbFace() const
         mpHbFace = hb_face_create_for_tables(getTable, 
const_cast<PhysicalFontFace*>(this), nullptr);
     return mpHbFace;
 }
+
+FontCharMapRef PhysicalFontFace::GetFontCharMap() const
+{
+    if (mxCharMap.is())
+        return mxCharMap;
+
+    hb_blob_t* pBlob = GetHbTable(HB_TAG('c', 'm', 'a', 'p'));
+    if (pBlob)
+    {
+        unsigned int nSize = 0;
+        auto* pData = reinterpret_cast<const unsigned 
char*>(hb_blob_get_data(pBlob, &nSize));
+
+        CmapResult aCmapResult(IsSymbolFont());
+        if (ParseCMAP(pData, nSize, aCmapResult))
+            mxCharMap = new FontCharMap(aCmapResult);
+        hb_blob_destroy(pBlob);
+    }
+
+    if (!mxCharMap.is())
+        mxCharMap = FontCharMap::GetDefaultMap(IsSymbolFont());
+
+    return mxCharMap;
+}
 }
 
 /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s 
cinkeys+=0=break: */
diff --git a/vcl/unx/generic/gdi/freetypetextrender.cxx 
b/vcl/unx/generic/gdi/freetypetextrender.cxx
index 1ec3a1522b8e..2e748bf85711 100644
--- a/vcl/unx/generic/gdi/freetypetextrender.cxx
+++ b/vcl/unx/generic/gdi/freetypetextrender.cxx
@@ -73,7 +73,7 @@ FontCharMapRef FreeTypeTextRenderImpl::GetFontCharMap() const
 {
     if (!mpFreetypeFont[0])
         return nullptr;
-    return mpFreetypeFont[0]->GetFreetypeFont().GetFontCharMap();
+    return mpFreetypeFont[0]->GetFontFace()->GetFontCharMap();
 }
 
 bool FreeTypeTextRenderImpl::GetFontCapabilities(vcl::FontCapabilities 
&rGetImplFontCapabilities) const
diff --git a/vcl/unx/generic/glyphs/freetype_glyphcache.cxx 
b/vcl/unx/generic/glyphs/freetype_glyphcache.cxx
index 5945a4813326..5e55b502090b 100644
--- a/vcl/unx/generic/glyphs/freetype_glyphcache.cxx
+++ b/vcl/unx/generic/glyphs/freetype_glyphcache.cxx
@@ -663,44 +663,11 @@ bool FreetypeFont::GetAntialiasAdvice() const
     return !mrFontInstance.GetFontSelectPattern().mbNonAntialiased && 
(mnPrioAntiAlias > 0);
 }
 
-// determine unicode ranges in font
-
-const FontCharMapRef & FreetypeFont::GetFontCharMap() const
-{
-    return mxFontInfo->GetFontCharMap();
-}
-
 bool FreetypeFont::GetFontCapabilities(vcl::FontCapabilities 
&rFontCapabilities) const
 {
     return mxFontInfo->GetFontCapabilities(rFontCapabilities);
 }
 
-const FontCharMapRef & FreetypeFontInfo::GetFontCharMap() const
-{
-    // check if the charmap is already cached
-    if( mxFontCharMap.is() )
-        return mxFontCharMap;
-
-    // get the charmap and cache it
-    CmapResult aCmapResult;
-    aCmapResult.mbSymbolic = IsSymbolFont();
-
-    sal_uLong nLength = 0;
-    const unsigned char* pCmap = GetTable("cmap", &nLength);
-    if (pCmap && (nLength > 0) && ParseCMAP(pCmap, nLength, aCmapResult))
-    {
-        FontCharMapRef xFontCharMap( new FontCharMap ( aCmapResult ) );
-        mxFontCharMap = xFontCharMap;
-    }
-    else
-    {
-        FontCharMapRef xFontCharMap( new FontCharMap() );
-        mxFontCharMap = xFontCharMap;
-    }
-    // mxFontCharMap on either branch now has a refcount of 1
-    return mxFontCharMap;
-}
-
 bool FreetypeFontInfo::GetFontCapabilities(vcl::FontCapabilities 
&rFontCapabilities) const
 {
     bool bRet = false;
diff --git a/vcl/unx/generic/print/genpspgraphics.cxx 
b/vcl/unx/generic/print/genpspgraphics.cxx
index ce29ecfeb51b..b1068082c26d 100644
--- a/vcl/unx/generic/print/genpspgraphics.cxx
+++ b/vcl/unx/generic/print/genpspgraphics.cxx
@@ -142,7 +142,7 @@ FontCharMapRef GenPspGraphics::GetFontCharMap() const
     if (!m_pFreetypeFont[0])
         return nullptr;
 
-    return m_pFreetypeFont[0]->GetFreetypeFont().GetFontCharMap();
+    return m_pFreetypeFont[0]->GetFontFace()->GetFontCharMap();
 }
 
 bool GenPspGraphics::GetFontCapabilities(vcl::FontCapabilities 
&rFontCapabilities) const
diff --git a/vcl/win/gdi/salfont.cxx b/vcl/win/gdi/salfont.cxx
index f2922c186c01..1c4ef2355f56 100644
--- a/vcl/win/gdi/salfont.cxx
+++ b/vcl/win/gdi/salfont.cxx
@@ -179,8 +179,7 @@ private:
 // does a font face hold the given missing characters?
 bool 
WinGlyphFallbackSubstititution::HasMissingChars(vcl::font::PhysicalFontFace* 
pFace, OUString& rMissingChars) const
 {
-    WinFontFace* pWinFont = static_cast< WinFontFace* >(pFace);
-    FontCharMapRef xFontCharMap = pWinFont->GetFontCharMap();
+    FontCharMapRef xFontCharMap = pFace->GetFontCharMap();
 
     // avoid fonts with unknown CMAP subtables for glyph fallback
     if( !xFontCharMap.is() || xFontCharMap->IsDefaultMap() )
@@ -615,7 +614,6 @@ WinFontFace::WinFontFace(const ENUMLOGFONTEXW& rEnumFont, 
const NEWTEXTMETRICW&
 WinFontFace::~WinFontFace()
 {
     DeleteFont(mhFont);
-    mxUnicodeMap.clear();
 }
 
 sal_IntPtr WinFontFace::GetFontId() const
@@ -713,13 +711,6 @@ hb_blob_t* WinFontFace::GetHbTable(hb_tag_t nTag) const
 
 static DWORD CalcTag( const char p[5]) { return 
(p[0]+(p[1]<<8)+(p[2]<<16)+(p[3]<<24)); }
 
-FontCharMapRef WinFontFace::GetFontCharMap() const
-{
-    if (!mxUnicodeMap.is())
-        ReadCmapTable();
-    return mxUnicodeMap;
-}
-
 bool WinFontFace::GetFontCapabilities(vcl::FontCapabilities 
&rFontCapabilities) const
 {
     if (!mbFontCapabilitiesRead)
@@ -728,39 +719,6 @@ bool 
WinFontFace::GetFontCapabilities(vcl::FontCapabilities &rFontCapabilities)
     return rFontCapabilities.oUnicodeRange || rFontCapabilities.oCodePageRange;
 }
 
-void WinFontFace::ReadCmapTable() const
-{
-    if( mxUnicodeMap.is() )
-        return;
-
-    HDC hDC(::GetDC(nullptr));
-    HFONT hOldFont = ::SelectFont(hDC, mhFont);
-
-    bool bIsSymbolFont = (meWinCharSet == SYMBOL_CHARSET);
-    // get the CMAP table from the font which is selected into the DC
-    const DWORD nCmapTag = CalcTag( "cmap" );
-    const RawFontData aRawFontData( hDC, nCmapTag );
-    // parse the CMAP table if available
-    if( aRawFontData.get() ) {
-        CmapResult aResult;
-        ParseCMAP( aRawFontData.get(), aRawFontData.size(), aResult );
-        aResult.mbSymbolic = bIsSymbolFont;
-        if( aResult.mnRangeCount > 0 )
-        {
-            FontCharMapRef pUnicodeMap(new FontCharMap(aResult));
-            mxUnicodeMap = pUnicodeMap;
-        }
-    }
-
-    ::SelectFont(hDC, hOldFont);
-    ::ReleaseDC(nullptr, hDC);
-
-    if( !mxUnicodeMap.is() )
-    {
-        mxUnicodeMap = FontCharMap::GetDefaultMap( bIsSymbolFont );
-    }
-}
-
 void WinFontFace::GetFontCapabilities() const
 {
     // read this only once per font

Reply via email to