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

New commits:
commit e6be502d833e0380d225218abf4896a635525301
Author:     Jaume Pujantell <jaume.pujant...@collabora.com>
AuthorDate: Wed Jan 3 19:32:25 2024 +0100
Commit:     Andras Timar <andras.ti...@collabora.com>
CommitDate: Mon Jan 15 20:00:26 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>
    (cherry picked from commit 96d52b9e6879df3ab9697a7c0322cda5e08eb9af)
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/161942
    Tested-by: Jenkins
    Reviewed-by: Andras Timar <andras.ti...@collabora.com>

diff --git a/sw/source/filter/ww8/docxattributeoutput.cxx 
b/sw/source/filter/ww8/docxattributeoutput.cxx
index 5ec368a97b40..a865697d4a89 100644
--- a/sw/source/filter/ww8/docxattributeoutput.cxx
+++ b/sw/source/filter/ww8/docxattributeoutput.cxx
@@ -7352,10 +7352,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 )
@@ -7363,21 +7369,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" );
@@ -7396,7 +7402,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;
@@ -7413,7 +7419,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;
@@ -7421,7 +7427,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;
@@ -7440,6 +7446,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 d6a024cc3c61..b1da698c3dec 100644
--- a/sw/source/filter/ww8/docxattributeoutput.hxx
+++ b/sw/source/filter/ww8/docxattributeoutput.hxx
@@ -512,7 +512,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