vcl/inc/sft.hxx                             |   62 +++++++-------
 vcl/source/font/font.cxx                    |   12 +-
 vcl/source/fontsubset/sft.cxx               |  118 ++++++++++++----------------
 vcl/unx/generic/fontmanager/fontmanager.cxx |   10 +-
 4 files changed, 93 insertions(+), 109 deletions(-)

New commits:
commit 2e932f8aeb62ec05057c680ae23fc0fb3ed4f978
Author:     Noel Grandin <noelgran...@gmail.com>
AuthorDate: Mon Sep 19 21:46:48 2022 +0200
Commit:     Noel Grandin <noel.gran...@collabora.co.uk>
CommitDate: Tue Sep 20 13:17:47 2022 +0200

    use O[U]String in TrueType code
    
    instead of manual memory management
    
    Change-Id: Iccde191bfcfbf9ff2488205fffc12b94b46ab658
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/140207
    Tested-by: Jenkins
    Reviewed-by: Noel Grandin <noel.gran...@collabora.co.uk>

diff --git a/vcl/inc/sft.hxx b/vcl/inc/sft.hxx
index c1d2c5c8e3fe..a870baf57033 100644
--- a/vcl/inc/sft.hxx
+++ b/vcl/inc/sft.hxx
@@ -147,33 +147,33 @@ namespace vcl
 /** Return value of GetTTGlobalFontInfo() */
 
     typedef struct TTGlobalFontInfo_ {
-        char *family;             /**< family name                             
                */
-        sal_Unicode *ufamily;     /**< family name UCS2                        
                 */
-        char *subfamily;          /**< subfamily name                          
                */
-        sal_Unicode *usubfamily;  /**< subfamily name UCS2 */
-        char *psname;             /**< PostScript name                         
                */
-        sal_uInt16 macStyle;      /**< macstyle bits from 'HEAD' table */
-        int   weight;             /**< value of WeightClass or 0 if can't be 
determined        */
-        int   width;              /**< value of WidthClass or 0 if can't be 
determined         */
-        int   pitch;              /**< 0: proportional font, otherwise: 
monospaced             */
-        int   italicAngle;        /**< in counter-clockwise degrees * 65536    
                */
-        int   xMin;               /**< global bounding box: xMin               
                */
-        int   yMin;               /**< global bounding box: yMin               
                */
-        int   xMax;               /**< global bounding box: xMax               
                */
-        int   yMax;               /**< global bounding box: yMax               
                */
-        int   ascender;           /**< typographic ascent.                     
                */
-        int   descender;          /**< typographic descent.                    
                */
-        int   linegap;            /**< typographic line gap.\ Negative values 
are treated as
+        OString    family;        /**< family name                             
                */
+        OUString   ufamily;       /**< family name UCS2                        
                 */
+        OString    subfamily;     /**< subfamily name                          
                */
+        OUString   usubfamily;    /**< subfamily name UCS2 */
+        OString    psname;        /**< PostScript name                         
                */
+        sal_uInt16 macStyle = 0;      /**< macstyle bits from 'HEAD' table */
+        int   weight = 0;             /**< value of WeightClass or 0 if can't 
be determined        */
+        int   width = 0;              /**< value of WidthClass or 0 if can't 
be determined         */
+        int   pitch = 0;              /**< 0: proportional font, otherwise: 
monospaced             */
+        int   italicAngle = 0;        /**< in counter-clockwise degrees * 
65536                    */
+        int   xMin = 0;               /**< global bounding box: xMin           
                    */
+        int   yMin = 0;               /**< global bounding box: yMin           
                    */
+        int   xMax = 0;               /**< global bounding box: xMax           
                    */
+        int   yMax = 0;               /**< global bounding box: yMax           
                    */
+        int   ascender = 0;           /**< typographic ascent.                 
                    */
+        int   descender = 0;          /**< typographic descent.                
                    */
+        int   linegap = 0;            /**< typographic line gap.\ Negative 
values are treated as
                                      zero in Win 3.1, System 6 and System 7.   
              */
-        int   typoAscender;       /**< OS/2 portable typographic ascender      
                */
-        int   typoDescender;      /**< OS/2 portable typographic descender     
                */
-        int   typoLineGap;        /**< OS/2 portable typographic line gap      
                 */
-        int   winAscent;          /**< ascender metric for Windows             
                */
-        int   winDescent;         /**< descender metric for Windows            
                */
-        bool  symbolEncoded;      /**< true: MS symbol encoded */
-        sal_uInt8  panose[10];    /**< PANOSE classification number            
                */
-        sal_uInt32 typeFlags;     /**< type flags (copyright bits)             
                */
-        sal_uInt16 fsSelection;   /**< OS/2 fsSelection */
+        int   typoAscender = 0;       /**< OS/2 portable typographic ascender  
                    */
+        int   typoDescender = 0;      /**< OS/2 portable typographic descender 
                    */
+        int   typoLineGap = 0;        /**< OS/2 portable typographic line gap  
                     */
+        int   winAscent = 0;          /**< ascender metric for Windows         
                    */
+        int   winDescent = 0;         /**< descender metric for Windows        
                    */
+        bool  symbolEncoded = false;      /**< true: MS symbol encoded */
+        sal_uInt8  panose[10] = {};   /**< PANOSE classification number        
                    */
+        sal_uInt32 typeFlags = 0;     /**< type flags (copyright bits)         
                    */
+        sal_uInt16 fsSelection = 0;   /**< OS/2 fsSelection */
     } TTGlobalFontInfo;
 
 /** ControlPoint structure used by GetTTGlyphPoints() */
@@ -768,11 +768,11 @@ public:
     virtual bool hasTable(sal_uInt32 ord) const = 0;
     virtual const sal_uInt8* table(sal_uInt32 ord, sal_uInt32& size) const = 0;
 
-    char        *psname;
-    char        *family;
-    sal_Unicode *ufamily;
-    char        *subfamily;
-    sal_Unicode *usubfamily;
+    OString     psname;
+    OString     family;
+    OUString    ufamily;
+    OString     subfamily;
+    OUString    usubfamily;
 };
 
 class TrueTypeFont final : public AbstractTrueTypeFont
diff --git a/vcl/source/font/font.cxx b/vcl/source/font/font.cxx
index 90cdba4163db..ffe4717d1a58 100644
--- a/vcl/source/font/font.cxx
+++ b/vcl/source/font/font.cxx
@@ -721,9 +721,9 @@ namespace
             TTGlobalFontInfo aInfo;
             GetTTGlobalFontInfo( pTTF, &aInfo );
             // most importantly: the family name
-            if( aInfo.ufamily )
-                o_rResult.SetFamilyName( OUString(aInfo.ufamily) );
-            else if( aInfo.family )
+            if( !aInfo.ufamily.isEmpty() )
+                o_rResult.SetFamilyName( aInfo.ufamily );
+            else if( !aInfo.family.isEmpty() )
                 o_rResult.SetFamilyName( OStringToOUString( aInfo.family, 
RTL_TEXTENCODING_ASCII_US ) );
             // set weight
             if( aInfo.weight )
@@ -778,9 +778,9 @@ namespace
             o_rResult.SetPitch( (aInfo.pitch == 0) ? PITCH_VARIABLE : 
PITCH_FIXED );
 
             // set style name
-            if( aInfo.usubfamily )
-                o_rResult.SetStyleName( OUString( aInfo.usubfamily ) );
-            else if( aInfo.subfamily )
+            if( !aInfo.usubfamily.isEmpty() )
+                o_rResult.SetStyleName( aInfo.usubfamily );
+            else if( !aInfo.subfamily.isEmpty() )
                 o_rResult.SetStyleName( OUString::createFromAscii( 
aInfo.subfamily ) );
 
             // cleanup
diff --git a/vcl/source/fontsubset/sft.cxx b/vcl/source/fontsubset/sft.cxx
index 892366ea3a62..8f8dd7b60892 100644
--- a/vcl/source/fontsubset/sft.cxx
+++ b/vcl/source/fontsubset/sft.cxx
@@ -45,6 +45,7 @@
 #include "xlat.hxx"
 #include <rtl/crc.h>
 #include <rtl/ustring.hxx>
+#include <rtl/ustrbuf.hxx>
 #include <sal/log.hxx>
 #include <o3tl/safeint.hxx>
 #include <osl/endian.h>
@@ -877,9 +878,9 @@ static int BSplineToPSPath(ControlPoint const *srcA, int 
srcCount, PSPathElement
 
 /*- Extracts a string from the name table and allocates memory for it -*/
 
-static char *nameExtract( const sal_uInt8* name, int nTableSize, int n, int 
dbFlag, sal_Unicode** ucs2result )
+static OString nameExtract( const sal_uInt8* name, int nTableSize, int n, int 
dbFlag, OUString* ucs2result )
 {
-    char *res;
+    OStringBuffer res;
     const sal_uInt8* ptr = name + GetUInt16(name, 4) + GetUInt16(name + 6, 12 
* n + 10);
     int len = GetUInt16(name+6, 12 * n + 8);
 
@@ -889,33 +890,36 @@ static char *nameExtract( const sal_uInt8* name, int 
nTableSize, int n, int dbFl
     if( (len <= 0) || len > available_space)
     {
         if( ucs2result )
-            *ucs2result = nullptr;
-        return nullptr;
+            ucs2result->clear();
+        return OString();
     }
 
     if( ucs2result )
-        *ucs2result = nullptr;
+        ucs2result->clear();
     if (dbFlag) {
-        res = static_cast<char*>(malloc(1 + len/2));
-        assert(res != nullptr);
+        res.setLength(len/2);
         for (int i = 0; i < len/2; i++)
+        {
             res[i] = *(ptr + i * 2 + 1);
-        res[len/2] = 0;
+            assert(res[i] != 0);
+        }
         if( ucs2result )
         {
-            *ucs2result = static_cast<sal_Unicode*>(malloc( len+2 ));
+            OUStringBuffer buf(len/2);
+            buf.setLength(len/2);
             for (int i = 0; i < len/2; i++ )
-                (*ucs2result)[i] = GetUInt16( ptr, 2*i );
-            (*ucs2result)[len/2] = 0;
+            {
+                buf[i] = GetUInt16( ptr, 2*i );
+                assert(buf[i] != 0);
+            }
+            *ucs2result = buf.makeStringAndClear();
         }
     } else {
-        res = static_cast<char*>(malloc(1 + len));
-        assert(res != nullptr);
-        memcpy(res, ptr, len);
-        res[len] = 0;
+        res.setLength(len);
+        memcpy(static_cast<void*>(const_cast<char*>(res.getStr())), ptr, len);
     }
 
-    return res;
+    return res.makeStringAndClear();
 }
 
 static int findname( const sal_uInt8 *name, sal_uInt16 n, sal_uInt16 
platformID,
@@ -985,72 +989,65 @@ static void GetNames(AbstractTrueTypeFont *t)
     bool bPSNameOK = true;
 
     /* PostScript name: preferred Microsoft */
-    t->psname = nullptr;
+    t->psname.clear();
     if ((r = findname(table, n, 3, 1, 0x0409, 6)) != -1)
         t->psname = nameExtract(table, nTableSize, r, 1, nullptr);
-    if ( ! t->psname && (r = findname(table, n, 1, 0, 0, 6)) != -1)
+    if ( t->psname.isEmpty() && (r = findname(table, n, 1, 0, 0, 6)) != -1)
         t->psname = nameExtract(table, nTableSize, r, 0, nullptr);
-    if ( ! t->psname && (r = findname(table, n, 3, 0, 0x0409, 6)) != -1)
+    if ( t->psname.isEmpty() && (r = findname(table, n, 3, 0, 0x0409, 6)) != 
-1)
     {
         // some symbol fonts like Marlett have a 3,0 name!
         t->psname = nameExtract(table, nTableSize, r, 1, nullptr);
     }
     // for embedded font in Ghostscript PDFs
-    if ( ! t->psname && (r = findname(table, n, 2, 2, 0, 6)) != -1)
+    if ( t->psname.isEmpty() && (r = findname(table, n, 2, 2, 0, 6)) != -1)
     {
         t->psname = nameExtract(table, nTableSize, r, 0, nullptr);
     }
-    if ( ! t->psname )
+    if ( t->psname.isEmpty() )
     {
         if (!t->fileName().empty())
         {
             const char* pReverse = t->fileName().data() + 
t->fileName().length();
             /* take only last token of filename */
-            while (pReverse != t->fileName() && *pReverse != '/') pReverse--;
+            while (pReverse != t->fileName().data() && *pReverse != '/') 
pReverse--;
             if(*pReverse == '/') pReverse++;
-            t->psname = strdup(pReverse);
-            assert(t->psname != nullptr);
-            for (i=strlen(t->psname) - 1; i > 0; i--)
+            int nReverseLen = strlen(pReverse);
+            for (i=nReverseLen - 1; i > 0; i--)
             {
                 /*- Remove the suffix  -*/
-                if (t->psname[i] == '.' ) {
-                    t->psname[i] = 0;
+                if (*(pReverse + i) == '.' ) {
+                    nReverseLen = i;
                     break;
                 }
             }
+            t->psname = OString(std::string_view(pReverse, nReverseLen));
         }
         else
-            t->psname = strdup( "Unknown" );
+            t->psname = "Unknown";
     }
 
     /* Font family and subfamily names: preferred Apple */
-    t->family = nullptr;
+    t->family.clear();
     if ((r = findname(table, n, 0, 0, 0, 1)) != -1)
         t->family = nameExtract(table, nTableSize, r, 1, &t->ufamily);
-    if ( ! t->family && (r = findname(table, n, 3, 1, 0x0409, 1)) != -1)
+    if ( t->family.isEmpty() && (r = findname(table, n, 3, 1, 0x0409, 1)) != 
-1)
         t->family = nameExtract(table, nTableSize, r, 1, &t->ufamily);
-    if ( ! t->family && (r = findname(table, n, 1, 0, 0, 1)) != -1)
+    if ( t->family.isEmpty() && (r = findname(table, n, 1, 0, 0, 1)) != -1)
         t->family = nameExtract(table, nTableSize, r, 0, nullptr);
-    if ( ! t->family && (r = findname(table, n, 3, 1, 0x0411, 1)) != -1)
+    if ( t->family.isEmpty() && (r = findname(table, n, 3, 1, 0x0411, 1)) != 
-1)
         t->family = nameExtract(table, nTableSize, r, 1, &t->ufamily);
-    if ( ! t->family && (r = findname(table, n, 3, 0, 0x0409, 1)) != -1)
+    if ( t->family.isEmpty() && (r = findname(table, n, 3, 0, 0x0409, 1)) != 
-1)
         t->family = nameExtract(table, nTableSize, r, 1, &t->ufamily);
-    if ( ! t->family )
-    {
-        t->family = strdup(t->psname);
-        assert(t->family != nullptr);
-    }
+    if ( t->family.isEmpty() )
+        t->family = t->psname;
 
-    t->subfamily = nullptr;
-    t->usubfamily = nullptr;
+    t->subfamily.clear();
+    t->usubfamily.clear();
     if ((r = findname(table, n, 1, 0, 0, 2)) != -1)
         t->subfamily = nameExtract(table, nTableSize, r, 0, &t->usubfamily);
-    if ( ! t->subfamily && (r = findname(table, n, 3, 1, 0x0409, 2)) != -1)
+    if ( t->subfamily.isEmpty() && (r = findname(table, n, 3, 1, 0x0409, 2)) 
!= -1)
         t->subfamily = nameExtract(table, nTableSize, r, 1, &t->usubfamily);
-    if ( ! t->subfamily )
-    {
-        t->subfamily = strdup("");
-    }
 
     /* #i60349# sanity check psname
      * psname practically has to be 7bit ASCII and should not contain spaces
@@ -1058,25 +1055,24 @@ static void GetNames(AbstractTrueTypeFont *t)
      * if the family name is 7bit ASCII and take it instead if so
      */
     /* check psname */
-    for( i = 0; t->psname[i] != 0 && bPSNameOK; i++ )
+    for( i = 0; i < t->psname.getLength() && bPSNameOK; i++ )
         if( t->psname[ i ] < 33 || (t->psname[ i ] & 0x80) )
             bPSNameOK = false;
     if( bPSNameOK )
         return;
 
     /* check if family is a suitable replacement */
-    if( !(t->ufamily && t->family) )
+    if( t->ufamily.isEmpty() && t->family.isEmpty() )
         return;
 
     bool bReplace = true;
 
-    for( i = 0; t->ufamily[ i ] != 0 && bReplace; i++ )
+    for( i = 0; i < t->ufamily.getLength() && bReplace; i++ )
         if( t->ufamily[ i ] < 33 || t->ufamily[ i ] > 127 )
             bReplace = false;
     if( bReplace )
     {
-        free( t->psname );
-        t->psname = strdup( t->family );
+        t->psname = t->family;
     }
 }
 
@@ -1197,11 +1193,6 @@ AbstractTrueTypeFont::AbstractTrueTypeFont(const char* 
pFileName, const FontChar
     , m_nUnitsPerEm(0)
     , m_xCharMap(xCharMap)
     , m_bIsSymbolFont(false)
-    , psname(nullptr)
-    , family(nullptr)
-    , ufamily(nullptr)
-    , subfamily(nullptr)
-    , usubfamily(nullptr)
 {
     if (pFileName)
         m_sFileName = pFileName;
@@ -1209,11 +1200,6 @@ AbstractTrueTypeFont::AbstractTrueTypeFont(const char* 
pFileName, const FontChar
 
 AbstractTrueTypeFont::~AbstractTrueTypeFont()
 {
-    free(psname);
-    free(family);
-    free(ufamily);
-    free(subfamily);
-    free(usubfamily);
 }
 
 TrueTypeFont::TrueTypeFont(const char* pFileName, const FontCharMapRef 
xCharMap)
@@ -1656,11 +1642,11 @@ SFErrCodes CreateT3FromTTGlyphs(TrueTypeFont *ttf, FILE 
*outf, const char *fname
 
     if ((nGlyphs <= 0) || (nGlyphs > 256)) return SFErrCodes::GlyphNum;
     if (!glyphArray) return SFErrCodes::BadArg;
-    if (!fname) fname = ttf->psname;
+    if (!fname) fname = ttf->psname.getStr();
 
     fprintf(outf, h01, GetInt16(table, 0), GetUInt16(table, 2), 
GetInt16(table, 4), GetUInt16(table, 6));
     fprintf(outf, h02, modname, modver, modextra);
-    fprintf(outf, h09, ttf->psname);
+    fprintf(outf, h09, ttf->psname.getStr());
 
     fprintf(outf, "%s", h10);
     fprintf(outf, h11, fname);
@@ -2185,9 +2171,9 @@ SFErrCodes CreateT42FromTTGlyphs(TrueTypeFont  *ttf,
     fprintf(outf, "%%!PS-TrueTypeFont-%d.%d-%d.%d\n", static_cast<int>(ver), 
static_cast<int>(ver & 0xFF), static_cast<int>(rev>>16), static_cast<int>(rev & 
0xFFFF));
     fprintf(outf, "%%%%Creator: %s %s %s\n", modname, modver, modextra);
     fprintf(outf, "%%- Font subset generated from a source font file: '%s'\n", 
ttf->fileName().data());
-    fprintf(outf, "%%- Original font name: %s\n", ttf->psname);
-    fprintf(outf, "%%- Original font family: %s\n", ttf->family);
-    fprintf(outf, "%%- Original font sub-family: %s\n", ttf->subfamily);
+    fprintf(outf, "%%- Original font name: %s\n", ttf->psname.getStr());
+    fprintf(outf, "%%- Original font family: %s\n", ttf->family.getStr());
+    fprintf(outf, "%%- Original font sub-family: %s\n", 
ttf->subfamily.getStr());
     fprintf(outf, "11 dict begin\n");
     fprintf(outf, "/FontName (%s) cvn def\n", psname);
     fprintf(outf, "/PaintType 0 def\n");
@@ -2309,8 +2295,6 @@ void GetTTGlobalFontInfo(AbstractTrueTypeFont *ttf, 
TTGlobalFontInfo *info)
 {
     int UPEm = ttf->unitsPerEm();
 
-    memset(info, 0, sizeof(TTGlobalFontInfo));
-
     info->family = ttf->family;
     info->ufamily = ttf->ufamily;
     info->subfamily = ttf->subfamily;
diff --git a/vcl/unx/generic/fontmanager/fontmanager.cxx 
b/vcl/unx/generic/fontmanager/fontmanager.cxx
index d9151ecf19c0..1a5a598427a4 100644
--- a/vcl/unx/generic/fontmanager/fontmanager.cxx
+++ b/vcl/unx/generic/fontmanager/fontmanager.cxx
@@ -574,13 +574,13 @@ bool PrintFontManager::analyzeSfntFile( PrintFont& rFont 
) const
             }
         }
 
-        if( aInfo.usubfamily )
-            rFont.m_aStyleName = OUString( aInfo.usubfamily );
+        if( !aInfo.usubfamily.isEmpty() )
+            rFont.m_aStyleName = aInfo.usubfamily;
 
-        SAL_WARN_IF( !aInfo.psname, "vcl.fonts", "No PostScript name in font:" 
<< aFile );
+        SAL_WARN_IF( aInfo.psname.isEmpty(), "vcl.fonts", "No PostScript name 
in font:" << aFile );
 
-        rFont.m_aPSName = aInfo.psname ?
-            OUString(aInfo.psname, rtl_str_getLength(aInfo.psname), aEncoding) 
:
+        rFont.m_aPSName = !aInfo.psname.isEmpty() ?
+            OStringToOUString(aInfo.psname, aEncoding) :
             rFont.m_aFamilyName; // poor font does not have a postscript name
 
         rFont.m_eFamilyStyle = matchFamilyName(rFont.m_aFamilyName);

Reply via email to