[Libreoffice-commits] core.git: vcl/inc vcl/unx

Caolán McNamara caolanm at redhat.com
Wed Mar 8 15:44:56 UTC 2017


 vcl/inc/unx/fontmanager.hxx                 |    2 -
 vcl/unx/generic/fontmanager/fontconfig.cxx  |   50 ++++++++++++++--------------
 vcl/unx/generic/fontmanager/fontmanager.cxx |   45 +++++++++----------------
 3 files changed, 43 insertions(+), 54 deletions(-)

New commits:
commit 7ae797878e1c46a3e39e03932ea4c970fa76f5ab
Author: Caolán McNamara <caolanm at redhat.com>
Date:   Wed Mar 8 15:23:07 2017 +0000

    valgrind: fix leak
    
    Change-Id: I174225a93a15a5300a94347faf9132f01e52cfe7

diff --git a/vcl/inc/unx/fontmanager.hxx b/vcl/inc/unx/fontmanager.hxx
index dc0dc9b..d5d6f24 100644
--- a/vcl/inc/unx/fontmanager.hxx
+++ b/vcl/inc/unx/fontmanager.hxx
@@ -166,7 +166,7 @@ class VCL_PLUGIN_PUBLIC PrintFontManager
 
     OString getFontFile(const PrintFont* pFont) const;
 
-    bool analyzeFontFile( int nDirID, const OString& rFileName, std::list< PrintFont* >& rNewFonts, const char *pFormat=nullptr ) const;
+    bool analyzeFontFile(int nDirID, const OString& rFileName, std::list<std::unique_ptr<PrintFont>>& rNewFonts, const char *pFormat=nullptr) const;
     static OUString convertSfntName( void* pNameRecord ); // actually a NameRecord* formt font subsetting code
     static void analyzeSfntFamilyName( void* pTTFont, std::list< OUString >& rnames ); // actually a TrueTypeFont* from font subsetting code
     bool analyzeSfntFile(PrintFont* pFont) const;
diff --git a/vcl/unx/generic/fontmanager/fontconfig.cxx b/vcl/unx/generic/fontmanager/fontconfig.cxx
index 120a116..49cfc23 100644
--- a/vcl/unx/generic/fontmanager/fontconfig.cxx
+++ b/vcl/unx/generic/fontmanager/fontconfig.cxx
@@ -545,7 +545,7 @@ void PrintFontManager::countFontconfigFonts( std::unordered_map<OString, int, OS
 
             // see if this font is already cached
             // update attributes
-            std::list< PrintFont* > aFonts;
+            std::list<std::unique_ptr<PrintFont>> aFonts;
             OString aDir, aBase, aOrgPath( reinterpret_cast<char*>(file) );
             splitPath( aOrgPath, aDir, aBase );
 
@@ -560,13 +560,11 @@ void PrintFontManager::countFontconfigFonts( std::unordered_map<OString, int, OS
             if (eFormatRes != FcResultMatch)
                 format = nullptr;
             analyzeFontFile( nDirID, aBase, aFonts, reinterpret_cast<char*>(format) );
+            if(aFonts.empty())
+            {
 #if OSL_DEBUG_LEVEL > 1
-            if( aFonts.empty() )
                 fprintf( stderr, "Warning: file \"%s\" is unusable to psprint\n", aOrgPath.getStr() );
 #endif
-
-            if( aFonts.empty() )
-            {
                 //remove font, reuse index
                 //we want to remove unusable fonts here, in case there is a usable font
                 //which duplicates the properties of the unusable one
@@ -577,63 +575,65 @@ void PrintFontManager::countFontconfigFonts( std::unordered_map<OString, int, OS
                 continue;
             }
 
-            OUString aFamilyName = OStringToOUString(OString(reinterpret_cast<char*>(family)), RTL_TEXTENCODING_UTF8);
-            PrintFont* pUpdate = aFonts.front();
-            std::list<PrintFont*>::const_iterator second_font = aFonts.begin();
+            std::unique_ptr<PrintFont> xUpdate;
+
+            auto second_font = aFonts.begin();
             ++second_font;
-            if( second_font != aFonts.end() ) // more than one font
+            if (second_font == aFonts.end()) // one font
+                xUpdate = std::move(aFonts.front());
+            else // more than one font
             {
                 // a collection entry, get the correct index
                 if( eIndexRes == FcResultMatch && nCollectionEntry != -1 )
                 {
-                    for( std::list< PrintFont* >::iterator it = aFonts.begin(); it != aFonts.end(); ++it )
+                    for (auto it = aFonts.begin(); it != aFonts.end(); ++it)
                     {
                         if( (*it)->m_nCollectionEntry == nCollectionEntry )
                         {
-                            pUpdate = *it;
+                            xUpdate = std::move(*it);
                             break;
                         }
                     }
+                }
+
+                if (xUpdate)
+                {
                     // update collection entry
                     // additional entries will be created in the cache
                     // if this is a new index (that is if the loop above
                     // ran to the end of the list)
-                    pUpdate->m_nCollectionEntry = nCollectionEntry;
+                    xUpdate->m_nCollectionEntry = nCollectionEntry;
                 }
+#if OSL_DEBUG_LEVEL > 1
                 else
                 {
-#if OSL_DEBUG_LEVEL > 1
                     fprintf( stderr, "multiple fonts for file, but no index in fontconfig pattern ! (index res = %d collection entry = %d\nfile will not be used\n", eIndexRes, nCollectionEntry );
-#endif
                     // we have found more than one font in this file
                     // but fontconfig will not tell us which index is meant
                     // -> something is in disorder, do not use this font
-                    pUpdate = nullptr;
                 }
+#endif
             }
 
-            if( pUpdate )
+            if (xUpdate)
             {
                 // set family name
-                if( pUpdate->m_aFamilyName != aFamilyName )
-                {
-                }
                 if( eWeightRes == FcResultMatch )
-                    pUpdate->m_eWeight = convertWeight(weight);
+                    xUpdate->m_eWeight = convertWeight(weight);
                 if( eWidthRes == FcResultMatch )
-                    pUpdate->m_eWidth = convertWidth(width);
+                    xUpdate->m_eWidth = convertWidth(width);
                 if( eSpacRes == FcResultMatch )
-                    pUpdate->m_ePitch = convertSpacing(spacing);
+                    xUpdate->m_ePitch = convertSpacing(spacing);
                 if( eSlantRes == FcResultMatch )
-                    pUpdate->m_eItalic = convertSlant(slant);
+                    xUpdate->m_eItalic = convertSlant(slant);
                 if( eStyleRes == FcResultMatch )
                 {
-                    pUpdate->m_aStyleName = OStringToOUString( OString( reinterpret_cast<char*>(style) ), RTL_TEXTENCODING_UTF8 );
+                    xUpdate->m_aStyleName = OStringToOUString( OString( reinterpret_cast<char*>(style) ), RTL_TEXTENCODING_UTF8 );
                 }
 
                 // sort into known fonts
                 fontID aFont = m_nNextFontID++;
-                m_aFonts[ aFont ] = pUpdate;
+                m_aFonts[ aFont ] = xUpdate.release();
                 m_aFontFileToFontID[ aBase ].insert( aFont );
 #if OSL_DEBUG_LEVEL > 1
                 nFonts++;
diff --git a/vcl/unx/generic/fontmanager/fontmanager.cxx b/vcl/unx/generic/fontmanager/fontmanager.cxx
index 6812f2a..5efceb8 100644
--- a/vcl/unx/generic/fontmanager/fontmanager.cxx
+++ b/vcl/unx/generic/fontmanager/fontmanager.cxx
@@ -183,14 +183,13 @@ std::vector<fontID> PrintFontManager::addFontFile( const OString& rFileName )
     std::vector<fontID> aFontIds = findFontFileIDs( nDirID, aName );
     if( aFontIds.empty() )
     {
-        ::std::list< PrintFont* > aNewFonts;
-        if( analyzeFontFile( nDirID, aName, aNewFonts ) )
+        std::list<std::unique_ptr<PrintFont>> aNewFonts;
+        if (analyzeFontFile(nDirID, aName, aNewFonts))
         {
-            for( ::std::list< PrintFont* >::iterator it = aNewFonts.begin();
-                 it != aNewFonts.end(); ++it )
+            for (auto it = aNewFonts.begin(); it != aNewFonts.end(); ++it)
             {
                 fontID nFontId = m_nNextFontID++;
-                m_aFonts[nFontId] = *it;
+                m_aFonts[nFontId] = it->release();
                 m_aFontFileToFontID[ aName ].insert( nFontId );
                 aFontIds.push_back(nFontId);
             }
@@ -204,7 +203,7 @@ enum fontFormat
     UNKNOWN, TRUETYPE, CFF
 };
 
-bool PrintFontManager::analyzeFontFile( int nDirID, const OString& rFontFile, ::std::list< PrintFontManager::PrintFont* >& rNewFonts, const char *pFormat ) const
+bool PrintFontManager::analyzeFontFile( int nDirID, const OString& rFontFile, std::list<std::unique_ptr<PrintFontManager::PrintFont>>& rNewFonts, const char *pFormat ) const
 {
     rNewFonts.clear();
 
@@ -275,34 +274,24 @@ bool PrintFontManager::analyzeFontFile( int nDirID, const OString& rFontFile, ::
 
             for( int i = 0; i < nLength; i++ )
             {
-                PrintFont* pFont     = new PrintFont();
-                pFont->m_nDirectory         = nDirID;
-                pFont->m_aFontFile          = rFontFile;
-                pFont->m_nCollectionEntry   = i;
-                if (!analyzeSfntFile(pFont))
-                {
-                    delete pFont;
-                    pFont = nullptr;
-                }
-                else
-                    rNewFonts.push_back( pFont );
+                std::unique_ptr<PrintFont> xFont(new PrintFont);
+                xFont->m_nDirectory         = nDirID;
+                xFont->m_aFontFile          = rFontFile;
+                xFont->m_nCollectionEntry   = i;
+                if (analyzeSfntFile(xFont.get()))
+                    rNewFonts.push_back(std::move(xFont));
             }
         }
         else
         {
-            PrintFont* pFont     = new PrintFont();
-            pFont->m_nDirectory         = nDirID;
-            pFont->m_aFontFile          = rFontFile;
-            pFont->m_nCollectionEntry   = 0;
+            std::unique_ptr<PrintFont> xFont(new PrintFont);
+            xFont->m_nDirectory         = nDirID;
+            xFont->m_aFontFile          = rFontFile;
+            xFont->m_nCollectionEntry   = 0;
 
             // need to read the font anyway to get aliases inside the font file
-            if (!analyzeSfntFile(pFont))
-            {
-                delete pFont;
-                pFont = nullptr;
-            }
-            else
-                rNewFonts.push_back( pFont );
+            if (analyzeSfntFile(xFont.get()))
+                rNewFonts.push_back(std::move(xFont));
         }
     }
     return ! rNewFonts.empty();


More information about the Libreoffice-commits mailing list