sdext/source/pdfimport/wrapper/wrapper.cxx |  113 +++++++++++------------------
 1 file changed, 46 insertions(+), 67 deletions(-)

New commits:
commit 2d486bac81e06c64d13c647f35d3f4affbeb183e
Author:     Kevin Suo <suokunl...@126.com>
AuthorDate: Mon Oct 11 12:29:00 2021 +0800
Commit:     Noel Grandin <noel.gran...@collabora.co.uk>
CommitDate: Mon Oct 11 09:08:37 2021 +0200

    tdf#143959 sdext.pdfimport: call vcl::Font::identifyFont directly
    
    Previously the unittest testTdf143959_nameFromFontFile did not
    fail even if the fixing code in wrapper.cxx is removed.
    That was because the "if (xHolder.is())" condition is always false
    in the unittest run, which suggests that the calling of
    com.sun.star.awt.FontIdentificator through the uno did not work
    in some circumstances. See comments in:
    https://gerrit.libreoffice.org/c/core/+/120815
    
    In this patch, we call the vcl::Font::identifyFont directly rather
    than through the uno calling. This is proven to work in both the
    manual and unittest running.
    
    Change-Id: I6334bca2defaa27cf6ac72af3d621fbb59e57980
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/123358
    Tested-by: Jenkins
    Reviewed-by: Noel Grandin <noel.gran...@collabora.co.uk>

diff --git a/sdext/source/pdfimport/wrapper/wrapper.cxx 
b/sdext/source/pdfimport/wrapper/wrapper.cxx
index 4de5a8516297..4d1f328d6f34 100644
--- a/sdext/source/pdfimport/wrapper/wrapper.cxx
+++ b/sdext/source/pdfimport/wrapper/wrapper.cxx
@@ -484,14 +484,14 @@ e.g., TimesNewRoman -> Times New Roman
 */
 void LineParser::parseFontFamilyName( FontAttributes& rResult )
 {
-    SAL_WARN("sdext.pdfimport", "Processing " << rResult.familyName << " ---");
+    SAL_INFO("sdext.pdfimport", "Processing " << rResult.familyName << " ---");
     rResult.familyName = rResult.familyName.trim();
     for (const OUString& fontAttributesSuffix: fontAttributesSuffixes)
     {
         if ( rResult.familyName.endsWith(fontAttributesSuffix) )
         {
             rResult.familyName = 
rResult.familyName.replaceAll(fontAttributesSuffix, "");
-            SAL_WARN("sdext.pdfimport", rResult.familyName);
+            SAL_INFO("sdext.pdfimport", rResult.familyName);
             if (fontAttributesSuffix == u"Bold")
             {
                 rResult.fontWeight = u"bold";
@@ -579,7 +579,7 @@ void LineParser::readFont()
         sFontWeight = u"800";
     else if (nFontWeight == 9)                 // W900, Black
         sFontWeight = u"900";
-    SAL_WARN("sdext.pdfimport", "Font weight passed from xpdfimport is: " << 
sFontWeight);
+    SAL_INFO("sdext.pdfimport", "Font weight passed from xpdfimport is: " << 
sFontWeight);
 
     FontAttributes aResult( OStringToOUString( aFontName, 
RTL_TEXTENCODING_UTF8 ),
                             sFontWeight,
@@ -602,79 +602,58 @@ void LineParser::readFont()
         uno::Sequence<sal_Int8> aFontFile(nFileLen);
         readBinaryData(aFontFile);  // Read fontFile.
 
-        uno::Sequence<uno::Any> aArgs(1);
-        awt::FontDescriptor aFontDescriptor;
-        aArgs[0] <<= aFontFile;
+        vcl::Font aFontReadResult = 
vcl::Font::identifyFont(aFontFile.getArray(), nFileLen);
+        SAL_INFO("sdext.pdfimport", "familyName: " << 
aFontReadResult.GetFamilyName());
 
-        try
+        if (!aFontReadResult.GetFamilyName().isEmpty()) // font detection 
successful
         {
-            uno::Reference<beans::XMaterialHolder> xHolder(
-                
m_parser.m_xContext->getServiceManager()->createInstanceWithArgumentsAndContext(
-                    "com.sun.star.awt.FontIdentificator", aArgs, 
m_parser.m_xContext),
-                uno::UNO_QUERY);
-            if (xHolder.is())
-            {
-                uno::Any aFontReadResult(xHolder->getMaterial());
-                aFontReadResult >>= aFontDescriptor;
-                if (!aFontDescriptor.Name.isEmpty())
-                {
-                    // Family name
-                    aResult.familyName = aFontDescriptor.Name;
-                    SAL_INFO("sdext.pdfimport", aResult.familyName);
-                    // tdf#143959: there are cases when the family name 
returned by font descriptor
-                    // is like "AAAAAA+TimesNewRoman,Bold". In this case, use 
the font name
-                    // determined by parseFontFamilyName instead, but still 
determine the font
-                    // attributes (bold italic etc) from the font descriptor.
-                    if (aResult.familyName.getLength() > 7 and 
aResult.familyName.indexOf(u"+", 6) == 6)
-                    {
-                        aResult.familyName = aResult.familyName.copy(7, 
aResult.familyName.getLength() - 7);
-                        parseFontFamilyName(aResult);
-                    }
-
-                    // Font weight
-                    if (aFontDescriptor.Weight == 
com::sun::star::awt::FontWeight::THIN)
-                        aResult.fontWeight = u"100";
-                    else if (aFontDescriptor.Weight == 
com::sun::star::awt::FontWeight::ULTRALIGHT)
-                        aResult.fontWeight = u"200";
-                    else if (aFontDescriptor.Weight == 
com::sun::star::awt::FontWeight::LIGHT)
-                        aResult.fontWeight = u"300";
-                    else if (aFontDescriptor.Weight == 
com::sun::star::awt::FontWeight::SEMILIGHT)
-                        aResult.fontWeight = u"350";
-                    // no need to check "normal" here as this is default in 
nFontWeight above
-                    else if (aFontDescriptor.Weight == 
com::sun::star::awt::FontWeight::SEMIBOLD)
-                        aResult.fontWeight = u"600";
-                    else if (aFontDescriptor.Weight == 
com::sun::star::awt::FontWeight::BOLD)
-                        aResult.fontWeight = u"bold";
-                    else if (aFontDescriptor.Weight == 
com::sun::star::awt::FontWeight::ULTRABOLD)
-                        aResult.fontWeight = u"800";
-                    else if (aFontDescriptor.Weight == 
com::sun::star::awt::FontWeight::BLACK)
-                        aResult.fontWeight = u"900";
-                    SAL_INFO("sdext.pdfimport", aResult.fontWeight);
-
-                    // Italic
-                    aResult.isItalic = (aFontDescriptor.Slant == 
awt::FontSlant_OBLIQUE ||
-                                        aFontDescriptor.Slant == 
awt::FontSlant_ITALIC);
-                } else
-                {
-                    SAL_WARN("sdext.pdfimport",
-                        "Font detection from fontFile returned empty result.\
-                        Guessing font info from font name.");
-                    parseFontFamilyName(aResult);
-                }
-            } else
+            // Family name
+            aResult.familyName = aFontReadResult.GetFamilyName();
+            SAL_INFO("sdext.pdfimport", aResult.familyName);
+            // tdf#143959: there are cases when the family name returned by 
font descriptor
+            // is like "AAAAAA+TimesNewRoman,Bold". In this case, use the font 
name
+            // determined by parseFontFamilyName instead, but still determine 
the font
+            // attributes (bold italic etc) from the font descriptor.
+            if (aResult.familyName.getLength() > 7 and 
aResult.familyName.indexOf(u"+", 6) == 6)
             {
-                SAL_WARN("sdext.pdfimport",
-                    "Failed to run FontIdentificator service.\
-                    Guessing font info from font name.");
+                aResult.familyName = aResult.familyName.copy(7, 
aResult.familyName.getLength() - 7);
                 parseFontFamilyName(aResult);
             }
-        } catch (uno::Exception&)
+
+            // Font weight
+            if (aFontReadResult.GetWeight() == 
com::sun::star::awt::FontWeight::THIN)
+                aResult.fontWeight = u"100";
+            else if (aFontReadResult.GetWeight() == 
com::sun::star::awt::FontWeight::ULTRALIGHT)
+                aResult.fontWeight = u"200";
+            else if (aFontReadResult.GetWeight() == 
com::sun::star::awt::FontWeight::LIGHT)
+                aResult.fontWeight = u"300";
+            else if (aFontReadResult.GetWeight() == 
com::sun::star::awt::FontWeight::SEMILIGHT)
+                aResult.fontWeight = u"350";
+            // no need to check "normal" here as this is default in 
nFontWeight above
+            else if (aFontReadResult.GetWeight() == 
com::sun::star::awt::FontWeight::SEMIBOLD)
+                aResult.fontWeight = u"600";
+            else if (aFontReadResult.GetWeight() == 
com::sun::star::awt::FontWeight::BOLD)
+                aResult.fontWeight = u"bold";
+            else if (aFontReadResult.GetWeight() == 
com::sun::star::awt::FontWeight::ULTRABOLD)
+                aResult.fontWeight = u"800";
+            else if (aFontReadResult.GetWeight() == 
com::sun::star::awt::FontWeight::BLACK)
+                aResult.fontWeight = u"900";
+            SAL_INFO("sdext.pdfimport", aResult.fontWeight);
+
+            // Italic
+            aResult.isItalic = (aFontReadResult.GetItalic() == ITALIC_OBLIQUE 
||
+                                aFontReadResult.GetItalic() == ITALIC_NORMAL);
+        } else  // font detection failed
         {
-            TOOLS_WARN_EXCEPTION("sdext.pdfimport", "Exception when trying to 
read font file.");
+            SAL_WARN("sdext.pdfimport",
+                "Font detection from fontFile returned empty result. Guessing 
font info from font name.");
             parseFontFamilyName(aResult);
         }
-    } else
+
+    } else  // no embedded font file - guess font attributes from font name
+    {
         parseFontFamilyName(aResult);
+    }
 
     // last fallback
     if (aResult.familyName.isEmpty())

Reply via email to