From c8b2849d140677f7b35523096eb2bc715b3dc507 Mon Sep 17 00:00:00 2001
From: Miklos Vajna <vmiklos@collabora.co.uk>
Date: Fri, 21 Sep 2018 16:28:58 +0200
Subject: Related: tdf#120028 PPTX import: fix font size of multi-col shape
 text

Normal shapes have a TextFitToSize property for this, but table cell
content has to be scaled manually, as the rendering has no automatic
support for them. Luckily the file format has the info we need to scale
at import time.

Change-Id: Ibbcc4b8685995261536cce88b8c0774e148f708e
Reviewed-on: https://gerrit.libreoffice.org/60880
Reviewed-by: Miklos Vajna <vmiklos@collabora.co.uk>
Tested-by: Jenkins
---
 oox/inc/drawingml/textbodyproperties.hxx           |  2 ++
 oox/inc/drawingml/textcharacterproperties.hxx      |  2 ++
 oox/source/drawingml/table/tableproperties.cxx     | 14 +++++++++++++-
 oox/source/drawingml/textbodypropertiescontext.cxx |  3 +++
 oox/source/drawingml/textcharacterproperties.cxx   |  3 +++
 sd/qa/unit/import-tests.cxx                        | 12 ++++++++++++
 6 files changed, 35 insertions(+), 1 deletion(-)

diff --git a/oox/inc/drawingml/textbodyproperties.hxx b/oox/inc/drawingml/textbodyproperties.hxx
index 247851ef6cc8..785f117568a9 100644
--- a/oox/inc/drawingml/textbodyproperties.hxx
+++ b/oox/inc/drawingml/textbodyproperties.hxx
@@ -44,6 +44,8 @@ struct TextBodyProperties
     OUString                                        msPrst;
     /// Number of requested columns.
     sal_Int32 mnNumCol = 1;
+    /// Normal autofit: font scale (default: 100%).
+    sal_Int32 mnFontScale = 100000;
 
     explicit            TextBodyProperties();
 
diff --git a/oox/inc/drawingml/textcharacterproperties.hxx b/oox/inc/drawingml/textcharacterproperties.hxx
index 4dfab74e3c1f..a034121e47d0 100644
--- a/oox/inc/drawingml/textcharacterproperties.hxx
+++ b/oox/inc/drawingml/textcharacterproperties.hxx
@@ -47,6 +47,8 @@ struct TextCharacterProperties
     Color               maHighlightColor;
     OptValue< OUString > moLang;
     OptValue< sal_Int32 > moHeight;
+    /// If a font scale has to be applied manually to moHeight.
+    OptValue< double > moFontScale;
     OptValue< sal_Int32 > moSpacing;
     OptValue< sal_Int32 > moUnderline;
     OptValue< sal_Int32 > moBaseline;
diff --git a/oox/source/drawingml/table/tableproperties.cxx b/oox/source/drawingml/table/tableproperties.cxx
index 8a59bae15738..39ab4cd19eac 100644
--- a/oox/source/drawingml/table/tableproperties.cxx
+++ b/oox/source/drawingml/table/tableproperties.cxx
@@ -20,6 +20,7 @@
 #include <drawingml/table/tableproperties.hxx>
 #include <drawingml/table/tablestylelist.hxx>
 #include <drawingml/textbody.hxx>
+#include <drawingml/textparagraph.hxx>
 #include <oox/drawingml/drawingmltypes.hxx>
 #include <com/sun/star/table/XTable.hpp>
 #include <com/sun/star/container/XNameContainer.hpp>
@@ -321,6 +322,8 @@ void TableProperties::pullFromTextBody(oox::drawingml::TextBodyPtr pTextBody, sa
     // Create the cells and distribute the paragraphs from pTextBody.
     sal_Int32 nNumPara = pTextBody->getParagraphs().size();
     sal_Int32 nParaPerCol = std::ceil(double(nNumPara) / nNumCol);
+    // Font scale of text body will be applied at a text run level.
+    sal_Int32 nFontScale = pTextBody->getTextProperties().mnFontScale;
     size_t nPara = 0;
     for (sal_Int32 nCol = 0; nCol < nNumCol; ++nCol)
     {
@@ -331,7 +334,16 @@ void TableProperties::pullFromTextBody(oox::drawingml::TextBodyPtr pTextBody, sa
         for (sal_Int32 nParaInCol = 0; nParaInCol < nParaPerCol; ++nParaInCol)
         {
             if (nPara < pTextBody->getParagraphs().size())
-                pCellTextBody->appendParagraph(pTextBody->getParagraphs()[nPara]);
+            {
+                std::shared_ptr<oox::drawingml::TextParagraph> pParagraph
+                    = pTextBody->getParagraphs()[nPara];
+                if (nFontScale != 100000)
+                {
+                    for (auto& pRun : pParagraph->getRuns())
+                        pRun->getTextCharacterProperties().moFontScale = nFontScale;
+                }
+                pCellTextBody->appendParagraph(pParagraph);
+            }
             ++nPara;
         }
     }
diff --git a/oox/source/drawingml/textbodypropertiescontext.cxx b/oox/source/drawingml/textbodypropertiescontext.cxx
index 0684de52a7c8..9c18536a4a86 100644
--- a/oox/source/drawingml/textbodypropertiescontext.cxx
+++ b/oox/source/drawingml/textbodypropertiescontext.cxx
@@ -155,9 +155,12 @@ ContextHandlerRef TextBodyPropertiesContext::onCreateContext( sal_Int32 aElement
                 mrTextBodyProp.maPropertyMap.setProperty( PROP_TextAutoGrowHeight, false);   // CT_TextNoAutofit
                 break;
             case A_TOKEN( normAutofit ):    // CT_TextNormalAutofit
+            {
                 mrTextBodyProp.maPropertyMap.setProperty( PROP_TextFitToSize, TextFitToSizeType_AUTOFIT);
                 mrTextBodyProp.maPropertyMap.setProperty( PROP_TextAutoGrowHeight, false);
+                mrTextBodyProp.mnFontScale = rAttribs.getInteger(XML_fontScale, 100000);
                 break;
+            }
             case A_TOKEN( spAutoFit ):
                 {
                     const sal_Int32 tVert = mrTextBodyProp.moVert.get( XML_horz );
diff --git a/oox/source/drawingml/textcharacterproperties.cxx b/oox/source/drawingml/textcharacterproperties.cxx
index 6443064e40d2..552dcf933e81 100644
--- a/oox/source/drawingml/textcharacterproperties.cxx
+++ b/oox/source/drawingml/textcharacterproperties.cxx
@@ -52,6 +52,7 @@ void TextCharacterProperties::assignUsed( const TextCharacterProperties& rSource
     maHighlightColor.assignIfUsed( rSourceProps.maHighlightColor );
     maUnderlineColor.assignIfUsed( rSourceProps.maUnderlineColor );
     moHeight.assignIfUsed( rSourceProps.moHeight );
+    moFontScale.assignIfUsed(rSourceProps.moFontScale);
     moSpacing.assignIfUsed( rSourceProps.moSpacing );
     moUnderline.assignIfUsed( rSourceProps.moUnderline );
     moBaseline.assignIfUsed( rSourceProps.moBaseline );
@@ -117,6 +118,8 @@ void TextCharacterProperties::pushToPropMap( PropertyMap& rPropMap, const XmlFil
     if( moHeight.has() )
     {
         float fHeight = GetFontHeight( moHeight.get() );
+        if (moFontScale.has())
+            fHeight *= (moFontScale.get() / 100000);
         rPropMap.setProperty( PROP_CharHeight, fHeight);
         rPropMap.setProperty( PROP_CharHeightAsian, fHeight);
         rPropMap.setProperty( PROP_CharHeightComplex, fHeight);
diff --git a/sd/qa/unit/import-tests.cxx b/sd/qa/unit/import-tests.cxx
index cca17846c666..37e50f3ef931 100644
--- a/sd/qa/unit/import-tests.cxx
+++ b/sd/qa/unit/import-tests.cxx
@@ -2561,6 +2561,7 @@ void SdImportTest::testTdf119015()
 
 void SdImportTest::testTdf120028()
 {
+    // Check that the table shape has 4 columns.
     ::sd::DrawDocShellRef xDocShRef
         = loadURL(m_directories.getURLFromSrc("/sd/qa/unit/data/pptx/tdf120028.pptx"), PPTX);
     uno::Reference<drawing::XDrawPagesSupplier> xDoc(xDocShRef->GetDoc()->getUnoModel(),
@@ -2580,6 +2581,17 @@ void SdImportTest::testTdf120028()
     uno::Reference<table::XTableColumns> xColumns = xModel->getColumns();
     CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(4), xColumns->getCount());
 
+    // Check font size in the A1 cell.
+    uno::Reference<table::XCellRange> xCells(xModel, uno::UNO_QUERY);
+    uno::Reference<beans::XPropertySet> xCell(xCells->getCellByPosition(0, 0), uno::UNO_QUERY);
+    uno::Reference<text::XTextRange> xParagraph(getParagraphFromShape(0, xCell));
+    uno::Reference<text::XTextRange> xRun(getRunFromParagraph(0, xParagraph));
+    uno::Reference<beans::XPropertySet> xPropSet(xRun, uno::UNO_QUERY);
+    double fCharHeight = 0;
+    xPropSet->getPropertyValue("CharHeight") >>= fCharHeight;
+    // This failed, non-scaled height was 13.5.
+    CPPUNIT_ASSERT_DOUBLES_EQUAL(11.5, fCharHeight, 1E-12);
+
     xDocShRef->DoClose();
 }
 
-- 
cgit