sw/source/filter/ww8/docxattributeoutput.cxx |   29 ++++++++++++++++-----------
 sw/source/filter/ww8/docxattributeoutput.hxx |    2 -
 2 files changed, 19 insertions(+), 12 deletions(-)

New commits:
commit 96d52b9e6879df3ab9697a7c0322cda5e08eb9af
Author:     Jaume Pujantell <jaume.pujant...@collabora.com>
AuthorDate: Wed Jan 3 19:32:25 2024 +0100
Commit:     Jaume Pujantell <jaume.pujant...@collabora.com>
CommitDate: Fri Jan 12 09:01:01 2024 +0100

    sw: search for alternative weights when embeding on docx
    
    Sometimes a docx document might come whith a font with niether normal
    or bold weight embedded as regular, so when embedding a font in docx
    search for alternative weights if neither a normal or bold matching
    font has been found.
    
    Change-Id: I95cf2634c392cec6e97e5dd12a90de6e50228ac1
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/161596
    Tested-by: Jenkins CollaboraOffice <jenkinscollaboraoff...@gmail.com>
    Reviewed-by: Ashod Nakashian <a...@collabora.com>

diff --git a/sw/source/filter/ww8/docxattributeoutput.cxx 
b/sw/source/filter/ww8/docxattributeoutput.cxx
index 3ae94d3c6e69..394d4a715e1a 100644
--- a/sw/source/filter/ww8/docxattributeoutput.cxx
+++ b/sw/source/filter/ww8/docxattributeoutput.cxx
@@ -7259,10 +7259,16 @@ void DocxAttributeOutput::EmbedFont( 
std::u16string_view name, FontFamily family
 {
     if( !m_rExport.m_rDoc.getIDocumentSettingAccess().get( 
DocumentSettingId::EMBED_FONTS ))
         return; // no font embedding with this document
-    EmbedFontStyle( name, XML_embedRegular, family, ITALIC_NONE, 
WEIGHT_NORMAL, pitch );
-    EmbedFontStyle( name, XML_embedBold, family, ITALIC_NONE, WEIGHT_BOLD, 
pitch );
-    EmbedFontStyle( name, XML_embedItalic, family, ITALIC_NORMAL, 
WEIGHT_NORMAL, pitch );
-    EmbedFontStyle( name, XML_embedBoldItalic, family, ITALIC_NORMAL, 
WEIGHT_BOLD, pitch );
+    bool foundFont
+        = EmbedFontStyle(name, XML_embedRegular, family, ITALIC_NONE, 
WEIGHT_NORMAL, pitch);
+    foundFont
+        = EmbedFontStyle(name, XML_embedBold, family, ITALIC_NONE, 
WEIGHT_BOLD, pitch) || foundFont;
+    foundFont = EmbedFontStyle(name, XML_embedItalic, family, ITALIC_NORMAL, 
WEIGHT_NORMAL, pitch)
+                || foundFont;
+    foundFont = EmbedFontStyle(name, XML_embedBoldItalic, family, 
ITALIC_NORMAL, WEIGHT_BOLD, pitch)
+                || foundFont;
+    if (!foundFont)
+        EmbedFontStyle(name, XML_embedRegular, family, ITALIC_NONE, 
WEIGHT_DONTKNOW, pitch);
 }
 
 static char toHexChar( int value )
@@ -7270,21 +7276,21 @@ static char toHexChar( int value )
     return value >= 10 ? value + 'A' - 10 : value + '0';
 }
 
-void DocxAttributeOutput::EmbedFontStyle( std::u16string_view name, int tag, 
FontFamily family, FontItalic italic,
-    FontWeight weight, FontPitch pitch )
+bool DocxAttributeOutput::EmbedFontStyle(std::u16string_view name, int tag, 
FontFamily family,
+                                         FontItalic italic, FontWeight weight, 
FontPitch pitch)
 {
     // Embed font if at least viewing is allowed (in which case the opening 
app must check
     // the font license rights too and open either read-only or not use the 
font for editing).
     OUString fontUrl = EmbeddedFontsHelper::fontFileUrl( name, family, italic, 
weight, pitch,
         EmbeddedFontsHelper::FontRights::ViewingAllowed );
     if( fontUrl.isEmpty())
-        return;
+        return false;
     // TODO IDocumentSettingAccess::EMBED_SYSTEM_FONTS
     if( !m_FontFilesMap.count( fontUrl ))
     {
         osl::File file( fontUrl );
         if( file.open( osl_File_OpenFlag_Read ) != osl::File::E_None )
-            return;
+            return false;
         uno::Reference< css::io::XOutputStream > xOutStream = 
m_rExport.GetFilter().openFragmentStream(
             "word/fonts/font" + OUString::number(m_nextFontId) + ".odttf",
             "application/vnd.openxmlformats-officedocument.obfuscatedFont" );
@@ -7303,7 +7309,7 @@ void DocxAttributeOutput::EmbedFontStyle( 
std::u16string_view name, int tag, Fon
         {
             SAL_WARN( "sw.ww8", "Font file size too small (" << fontUrl << ")" 
);
             xOutStream->closeOutput();
-            return;
+            return false;
         }
         for( int i = 0;
              i < 16;
@@ -7320,7 +7326,7 @@ void DocxAttributeOutput::EmbedFontStyle( 
std::u16string_view name, int tag, Fon
             {
                 SAL_WARN( "sw.ww8", "Error reading font file " << fontUrl );
                 xOutStream->closeOutput();
-                return;
+                return false;
             }
             if( eof )
                 break;
@@ -7328,7 +7334,7 @@ void DocxAttributeOutput::EmbedFontStyle( 
std::u16string_view name, int tag, Fon
             {
                 SAL_WARN( "sw.ww8", "Error reading font file " << fontUrl );
                 xOutStream->closeOutput();
-                return;
+                return false;
             }
             if( readSize == 0 )
                 break;
@@ -7348,6 +7354,7 @@ void DocxAttributeOutput::EmbedFontStyle( 
std::u16string_view name, int tag, Fon
     m_pSerializer->singleElementNS( XML_w, tag,
         FSNS( XML_r, XML_id ), m_FontFilesMap[ fontUrl ].relId,
         FSNS( XML_w, XML_fontKey ), m_FontFilesMap[ fontUrl ].fontKey );
+    return true;
 }
 
 OString DocxAttributeOutput::TransHighlightColor( sal_uInt8 nIco )
diff --git a/sw/source/filter/ww8/docxattributeoutput.hxx 
b/sw/source/filter/ww8/docxattributeoutput.hxx
index 9a7212b40ec1..efb696171e8a 100644
--- a/sw/source/filter/ww8/docxattributeoutput.hxx
+++ b/sw/source/filter/ww8/docxattributeoutput.hxx
@@ -498,7 +498,7 @@ private:
     void WriteFFData( const FieldInfos& rInfos );
     void WritePendingPlaceholder();
 
-    void EmbedFontStyle( std::u16string_view name, int tag, FontFamily family, 
FontItalic italic, FontWeight weight,
+    bool EmbedFontStyle( std::u16string_view name, int tag, FontFamily family, 
FontItalic italic, FontWeight weight,
         FontPitch pitch );
 
     /**

Reply via email to