oox: add model::LineStyle and import line style elements of a theme

Also extend the SwCoreThemeTest with asserts for the LineStyles
for the example theme.

Change-Id: I72aa64345d39fea84544e39b0a37a22f3e6baa5d
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/149360
Tested-by: Jenkins
Reviewed-by: Tomaž Vajngerl <quikee@gmail.com>
This commit is contained in:
Tomaž Vajngerl
2023-03-01 22:59:02 +09:00
committed by Tomaž Vajngerl
parent 0ffa50031e
commit f69614d811
5 changed files with 356 additions and 29 deletions

View File

@@ -344,19 +344,133 @@ public:
}
};
// Format Scheme
class DOCMODEL_DLLPUBLIC FillStyle
{
public:
std::shared_ptr<Fill> mpFill;
};
enum class CapType
{
Unset,
Flat,
Round,
Square
};
enum class PenAlignmentType
{
Unset,
Center,
Inset
};
enum class CompoundLineType
{
Unset,
Single,
Double,
ThickThin_Double,
ThinThick_Double,
Triple,
};
enum class PresetDashType
{
Unset,
Dash,
DashDot,
Dot,
LargeDash,
LargeDashDot,
LargeDashDotDot,
Solid,
SystemDash,
SystemDashDot,
SystemDashDotDot,
SystemDot,
};
enum class LineJoinType
{
Unset,
Round,
Bevel,
Miter,
};
struct DOCMODEL_DLLPUBLIC LineJoin
{
LineJoinType meType = LineJoinType::Unset;
sal_Int32 mnMiterLimit = 0; // Percentage
};
enum class LineEndType
{
None,
Triangle,
Stealth,
Diamond,
Oval,
Arrow
};
enum class LineEndWidth
{
Unset,
Small,
Medium,
Large
};
enum class LineEndLength
{
Unset,
Small,
Medium,
Large
};
struct DOCMODEL_DLLPUBLIC LineEnd
{
LineEndType meType;
LineEndWidth meWidth;
LineEndLength meLength;
};
struct DOCMODEL_DLLPUBLIC DashStop
{
sal_Int32 mnDashLength = 0;
sal_Int32 mnStopLength = 0;
};
struct DOCMODEL_DLLPUBLIC LineDash
{
PresetDashType mePresetType = PresetDashType::Unset;
std::vector<DashStop> maCustomList;
};
class DOCMODEL_DLLPUBLIC LineStyle
{
public:
sal_Int32 mnWidth;
CapType meCapType;
PenAlignmentType mePenAlignment;
CompoundLineType meCompoundLineType;
LineDash maLineDash;
LineJoin maLineJoin;
LineEnd maHeadEnd;
LineEnd maTailEnd;
FillStyle maLineFillStyle;
};
class DOCMODEL_DLLPUBLIC FormatScheme
{
private:
OUString maName;
std::vector<FillStyle> maFillStyleList;
std::vector<LineStyle> maLineStyleList;
std::vector<FillStyle> maBackgroundFillStyleList;
public:
@@ -369,6 +483,16 @@ public:
const OUString& getName() const { return maName; }
std::vector<LineStyle> const& getLineStyleList() const { return maLineStyleList; }
LineStyle* addLineStyle()
{
if (maLineStyleList.size() > 3)
return nullptr;
auto& rLineStyle = maLineStyleList.emplace_back();
return &rLineStyle;
}
std::vector<FillStyle> const& getFillStyleList() const { return maFillStyleList; }
FillStyle* addFillStyle()

View File

@@ -22,23 +22,25 @@
#include <oox/core/contexthandler2.hxx>
namespace oox::drawingml {
namespace model { class LineStyle; }
namespace oox::drawingml
{
struct LineProperties;
class LinePropertiesContext final : public ::oox::core::ContextHandler2
{
public:
LinePropertiesContext( ::oox::core::ContextHandler2Helper const & rParent,
const ::oox::AttributeList& rAttributes,
LineProperties& rLineProperties ) noexcept;
LinePropertiesContext(::oox::core::ContextHandler2Helper const & rParent, const ::oox::AttributeList& rAttributes,
LineProperties& rLineProperties, model::LineStyle* pLineStyle = nullptr) noexcept;
virtual ~LinePropertiesContext() override;
virtual ::oox::core::ContextHandlerRef
onCreateContext( ::sal_Int32 Element, const ::oox::AttributeList& rAttribs ) override;
private:
model::LineStyle* mpLineStyle;
LineProperties& mrLineProperties;
};

View File

@@ -23,6 +23,7 @@
#include <oox/helper/attributelist.hxx>
#include <oox/token/namespaces.hxx>
#include <oox/token/tokens.hxx>
#include <docmodel/theme/FormatScheme.hxx>
using namespace ::oox::core;
using namespace ::com::sun::star::uno;
@@ -33,13 +34,47 @@ using namespace ::com::sun::star::xml::sax;
namespace oox::drawingml {
LinePropertiesContext::LinePropertiesContext( ContextHandler2Helper const & rParent, const AttributeList& rAttribs,
LineProperties& rLineProperties ) noexcept
: ContextHandler2( rParent )
, mrLineProperties( rLineProperties )
LineProperties& rLineProperties, model::LineStyle* pLineStyle) noexcept
: ContextHandler2(rParent)
, mpLineStyle(pLineStyle)
, mrLineProperties(rLineProperties)
{
mrLineProperties.moLineWidth = rAttribs.getInteger( XML_w );
mrLineProperties.moLineCompound = rAttribs.getToken( XML_cmpd );
mrLineProperties.moLineCap = rAttribs.getToken( XML_cap );
if (mpLineStyle)
{
mpLineStyle->mnWidth = rAttribs.getInteger(XML_w, 0);
switch (rAttribs.getToken(XML_cap, XML_TOKEN_INVALID))
{
case XML_rnd: mpLineStyle->meCapType = model::CapType::Round; break;
case XML_sq: mpLineStyle->meCapType = model::CapType::Square; break;
case XML_flat: mpLineStyle->meCapType = model::CapType::Flat; break;
default:
mpLineStyle->meCapType = model::CapType::Unset; break;
}
switch (rAttribs.getToken(XML_cmpd, XML_TOKEN_INVALID))
{
case XML_sng: mpLineStyle->meCompoundLineType = model::CompoundLineType::Single; break;
case XML_dbl: mpLineStyle->meCompoundLineType = model::CompoundLineType::Double; break;
case XML_thickThin: mpLineStyle->meCompoundLineType = model::CompoundLineType::ThickThin_Double; break;
case XML_thinThick: mpLineStyle->meCompoundLineType = model::CompoundLineType::ThinThick_Double; break;
case XML_tri: mpLineStyle->meCompoundLineType = model::CompoundLineType::Triple; break;
default:
mpLineStyle->meCompoundLineType = model::CompoundLineType::Unset; break;
}
switch (rAttribs.getToken(XML_algn, XML_TOKEN_INVALID))
{
case XML_ctr: mpLineStyle->mePenAlignment = model::PenAlignmentType::Center; break;
case XML_in: mpLineStyle->mePenAlignment = model::PenAlignmentType::Inset; break;
default:
mpLineStyle->mePenAlignment = model::PenAlignmentType::Unset; break;
}
}
}
LinePropertiesContext::~LinePropertiesContext()
@@ -55,12 +90,42 @@ ContextHandlerRef LinePropertiesContext::onCreateContext( sal_Int32 nElement, co
case A_TOKEN( solidFill ):
case A_TOKEN( gradFill ):
case A_TOKEN( pattFill ):
return FillPropertiesContext::createFillContext(*this, nElement, rAttribs, mrLineProperties.maLineFill, nullptr);
{
model::FillStyle* pFillStyle = nullptr;
if (mpLineStyle)
{
pFillStyle = &mpLineStyle->maLineFillStyle;
}
return FillPropertiesContext::createFillContext(*this, nElement, rAttribs, mrLineProperties.maLineFill, pFillStyle);
}
break;
// LineDashPropertiesGroup
case A_TOKEN( prstDash ): // CT_PresetLineDashProperties
{
mrLineProperties.moPresetDash = rAttribs.getToken( XML_val );
if (mpLineStyle)
{
auto& rLineDash = mpLineStyle->maLineDash;
switch (rAttribs.getToken(XML_val, XML_TOKEN_INVALID))
{
case XML_solid: rLineDash.mePresetType = model::PresetDashType::Solid; break;
case XML_dot: rLineDash.mePresetType = model::PresetDashType::Dot; break;
case XML_dash: rLineDash.mePresetType = model::PresetDashType::Dash; break;
case XML_lgDash: rLineDash.mePresetType = model::PresetDashType::LargeDash; break;
case XML_dashDot: rLineDash.mePresetType = model::PresetDashType::DashDot; break;
case XML_lgDashDot: rLineDash.mePresetType = model::PresetDashType::LargeDashDot; break;
case XML_lgDashDotDot: rLineDash.mePresetType = model::PresetDashType::LargeDashDotDot; break;
case XML_sysDash: rLineDash.mePresetType = model::PresetDashType::SystemDash; break;
case XML_sysDot: rLineDash.mePresetType = model::PresetDashType::SystemDot; break;
case XML_sysDashDot: rLineDash.mePresetType = model::PresetDashType::SystemDashDot; break;
case XML_sysDashDotDot: rLineDash.mePresetType = model::PresetDashType::SystemDashDotDot; break;
default:
rLineDash.mePresetType = model::PresetDashType::Unset; break;
}
}
}
break;
case A_TOKEN( custDash ): // CT_DashStopList
return this;
@@ -73,41 +138,46 @@ ContextHandlerRef LinePropertiesContext::onCreateContext( sal_Int32 nElement, co
// as 1000th's of a percent without a trailing percent sign.
// The code below takes care of both scenarios by converting to '1000th of a percent' always
OUString aStr;
sal_Int32 nDash = 0;
sal_Int32 nDashLength = 0;
aStr = rAttribs.getStringDefaulted( XML_d);
if ( aStr.endsWith("%") )
{
// Ends with a '%'
aStr = aStr.copy(0, aStr.getLength() - 1);
aStr = aStr.trim();
nDash = aStr.toInt32();
nDashLength = aStr.toInt32();
// Convert to 1000th of a percent
nDash *= 1000;
nDashLength *= 1000;
}
else
{
nDash = rAttribs.getInteger( XML_d, 0 );
nDashLength = rAttribs.getInteger( XML_d, 0 );
}
sal_Int32 nSp = 0;
sal_Int32 nSpaceLength = 0;
aStr = rAttribs.getStringDefaulted( XML_sp);
if ( aStr.endsWith("%") )
{
// Ends with a '%'
aStr = aStr.copy(0, aStr.getLength() - 1);
aStr = aStr.trim();
nSp = aStr.toInt32();
nSpaceLength = aStr.toInt32();
// Convert to 1000th of a percent
nSp *= 1000;
nSpaceLength *= 1000;
}
else
{
nSp = rAttribs.getInteger( XML_sp, 0 );
nSpaceLength = rAttribs.getInteger( XML_sp, 0 );
}
mrLineProperties.maCustomDash.emplace_back( nDash, nSp );
mrLineProperties.maCustomDash.emplace_back( nDashLength, nSpaceLength );
if (mpLineStyle)
{
mpLineStyle->maLineDash.maCustomList.push_back({ nDashLength, nSpaceLength });
}
}
break;
@@ -115,7 +185,27 @@ ContextHandlerRef LinePropertiesContext::onCreateContext( sal_Int32 nElement, co
case A_TOKEN( round ):
case A_TOKEN( bevel ):
case A_TOKEN( miter ):
mrLineProperties.moLineJoint = getBaseToken( nElement );
{
sal_Int32 nToken = getBaseToken(nElement);
mrLineProperties.moLineJoint = nToken;
if (mpLineStyle)
{
switch (nToken)
{
case XML_round: mpLineStyle->maLineJoin.meType = model::LineJoinType::Round; break;
case XML_bevel: mpLineStyle->maLineJoin.meType = model::LineJoinType::Bevel; break;
case XML_miter: mpLineStyle->maLineJoin.meType = model::LineJoinType::Miter; break;
default:
mpLineStyle->maLineJoin.meType = model::LineJoinType::Miter;
}
if (nToken == XML_miter)
{
sal_Int32 nMiterLimit = rAttribs.getInteger(XML_lim, 0);
mpLineStyle->maLineJoin.mnMiterLimit = nMiterLimit;
}
}
}
break;
case A_TOKEN( headEnd ): // CT_LineEndProperties
@@ -126,6 +216,54 @@ ContextHandlerRef LinePropertiesContext::onCreateContext( sal_Int32 nElement, co
rArrowProps.moArrowType = rAttribs.getToken( XML_type );
rArrowProps.moArrowWidth = rAttribs.getToken( XML_w );
rArrowProps.moArrowLength = rAttribs.getToken( XML_len );
if (mpLineStyle)
{
model::LineEndType eLineEndType = model::LineEndType::None;
switch (rAttribs.getToken(XML_type, XML_none))
{
case XML_triangle: eLineEndType = model::LineEndType::Triangle; break;
case XML_stealth: eLineEndType = model::LineEndType::Stealth; break;
case XML_diamond: eLineEndType = model::LineEndType::Diamond; break;
case XML_oval: eLineEndType = model::LineEndType::Oval; break;
case XML_arrow: eLineEndType = model::LineEndType::Arrow; break;
default:
case XML_none: eLineEndType = model::LineEndType::None; break;
}
model::LineEndLength eLineEndLength = model::LineEndLength::Unset;
switch (rAttribs.getToken(XML_len, XML_TOKEN_INVALID))
{
case XML_sm: eLineEndLength = model::LineEndLength::Small; break;
case XML_med: eLineEndLength = model::LineEndLength::Medium; break;
case XML_lg: eLineEndLength = model::LineEndLength::Large; break;
default:
break;
}
model::LineEndWidth eLineEndWidth = model::LineEndWidth::Unset;
switch (rAttribs.getToken(XML_w, XML_TOKEN_INVALID))
{
case XML_sm: eLineEndWidth = model::LineEndWidth::Small; break;
case XML_med: eLineEndWidth = model::LineEndWidth::Medium; break;
case XML_lg: eLineEndWidth = model::LineEndWidth::Large; break;
default:
break;
}
if (nElement == A_TOKEN(tailEnd))
{
mpLineStyle->maTailEnd.meType = eLineEndType;
mpLineStyle->maTailEnd.meLength = eLineEndLength;
mpLineStyle->maTailEnd.meWidth = eLineEndWidth;
}
else if (nElement == A_TOKEN(headEnd))
{
mpLineStyle->maHeadEnd.meType = eLineEndType;
mpLineStyle->maHeadEnd.meLength = eLineEndLength;
mpLineStyle->maHeadEnd.meWidth = eLineEndWidth;
}
}
}
break;
}

View File

@@ -106,28 +106,33 @@ namespace {
class LineStyleListContext : public ContextHandler2
{
public:
LineStyleListContext( ContextHandler2Helper const & rParent, LineStyleList& rLineStyleList );
LineStyleListContext(ContextHandler2Helper const & rParent, LineStyleList& rLineStyleList, model::FormatScheme& rFormatScheme);
virtual ContextHandlerRef onCreateContext( sal_Int32 nElement, const AttributeList& rAttribs ) override;
private:
model::FormatScheme& mrFormatScheme;
LineStyleList& mrLineStyleList;
};
}
LineStyleListContext::LineStyleListContext( ContextHandler2Helper const & rParent, LineStyleList& rLineStyleList ) :
ContextHandler2( rParent ),
mrLineStyleList( rLineStyleList )
LineStyleListContext::LineStyleListContext( ContextHandler2Helper const & rParent, LineStyleList& rLineStyleList, model::FormatScheme& rFormatScheme)
: ContextHandler2(rParent)
, mrFormatScheme(rFormatScheme)
, mrLineStyleList(rLineStyleList)
{
}
ContextHandlerRef LineStyleListContext::onCreateContext( sal_Int32 nElement, const AttributeList& rAttribs )
{
switch( nElement )
switch (nElement)
{
case A_TOKEN( ln ):
case A_TOKEN(ln):
{
mrLineStyleList.push_back( std::make_shared<LineProperties>( ) );
return new LinePropertiesContext( *this, rAttribs, *mrLineStyleList.back() );
model::LineStyle* pLineStyle = mrFormatScheme.addLineStyle();
return new LinePropertiesContext(*this, rAttribs, *mrLineStyleList.back(), pLineStyle);
}
}
return nullptr;
}
@@ -335,9 +340,9 @@ ContextHandlerRef ThemeElementsContext::onCreateContext(sal_Int32 nElement, cons
}
case A_TOKEN( fillStyleLst ): // CT_FillStyleList
return new FillStyleListContext( *this, mrOoxTheme.getFillStyleList(), mrTheme.getFormatScheme());
return new FillStyleListContext(*this, mrOoxTheme.getFillStyleList(), mrTheme.getFormatScheme());
case A_TOKEN( lnStyleLst ): // CT_LineStyleList
return new LineStyleListContext( *this, mrOoxTheme.getLineStyleList() );
return new LineStyleListContext(*this, mrOoxTheme.getLineStyleList(), mrTheme.getFormatScheme());
case A_TOKEN( effectStyleLst ): // CT_EffectStyleList
return new EffectStyleListContext( *this, mrOoxTheme.getEffectStyleList() );
case A_TOKEN( bgFillStyleLst ): // CT_BackgroundFillStyleList

View File

@@ -85,6 +85,7 @@ CPPUNIT_TEST_FIXTURE(SwCoreThemeTest, testDrawPageThemeExistsDOCX)
model::FormatScheme const& rFormatScheme = pTheme->getFormatScheme();
CPPUNIT_ASSERT_EQUAL(size_t(3), rFormatScheme.getFillStyleList().size());
CPPUNIT_ASSERT_EQUAL(size_t(3), rFormatScheme.getLineStyleList().size());
CPPUNIT_ASSERT_EQUAL(size_t(3), rFormatScheme.getBackgroundFillStyleList().size());
// Fill style 1
@@ -257,6 +258,63 @@ CPPUNIT_TEST_FIXTURE(SwCoreThemeTest, testDrawPageThemeExistsDOCX)
}
}
}
// Line style 1
{
model::LineStyle const& rLineStyle = rFormatScheme.getLineStyleList().at(0);
CPPUNIT_ASSERT_EQUAL(sal_Int32(6350), rLineStyle.mnWidth);
CPPUNIT_ASSERT_EQUAL(model::CapType::Flat, rLineStyle.meCapType);
CPPUNIT_ASSERT_EQUAL(model::PenAlignmentType::Center, rLineStyle.mePenAlignment);
CPPUNIT_ASSERT_EQUAL(model::CompoundLineType::Single, rLineStyle.meCompoundLineType);
CPPUNIT_ASSERT_EQUAL(model::PresetDashType::Solid, rLineStyle.maLineDash.mePresetType);
CPPUNIT_ASSERT_EQUAL(model::LineJoinType::Miter, rLineStyle.maLineJoin.meType);
CPPUNIT_ASSERT_EQUAL(sal_Int32(800000), rLineStyle.maLineJoin.mnMiterLimit);
model::FillStyle const& rFillStyle = rLineStyle.maLineFillStyle;
CPPUNIT_ASSERT(rFillStyle.mpFill);
CPPUNIT_ASSERT_EQUAL(model::FillType::Solid, rFillStyle.mpFill->meType);
auto* pSolidFill = static_cast<model::SolidFill*>(rFillStyle.mpFill.get());
CPPUNIT_ASSERT_EQUAL(model::ColorType::Placeholder, pSolidFill->maColorDefinition.meType);
CPPUNIT_ASSERT_EQUAL(size_t(0), pSolidFill->maColorDefinition.maTransformations.size());
}
// Line style 2
{
model::LineStyle const& rLineStyle = rFormatScheme.getLineStyleList().at(1);
CPPUNIT_ASSERT_EQUAL(sal_Int32(12700), rLineStyle.mnWidth);
CPPUNIT_ASSERT_EQUAL(model::CapType::Flat, rLineStyle.meCapType);
CPPUNIT_ASSERT_EQUAL(model::PenAlignmentType::Center, rLineStyle.mePenAlignment);
CPPUNIT_ASSERT_EQUAL(model::CompoundLineType::Single, rLineStyle.meCompoundLineType);
CPPUNIT_ASSERT_EQUAL(model::PresetDashType::Solid, rLineStyle.maLineDash.mePresetType);
CPPUNIT_ASSERT_EQUAL(model::LineJoinType::Miter, rLineStyle.maLineJoin.meType);
CPPUNIT_ASSERT_EQUAL(sal_Int32(800000), rLineStyle.maLineJoin.mnMiterLimit);
model::FillStyle const& rFillStyle = rLineStyle.maLineFillStyle;
CPPUNIT_ASSERT(rFillStyle.mpFill);
CPPUNIT_ASSERT_EQUAL(model::FillType::Solid, rFillStyle.mpFill->meType);
auto* pSolidFill = static_cast<model::SolidFill*>(rFillStyle.mpFill.get());
CPPUNIT_ASSERT_EQUAL(model::ColorType::Placeholder, pSolidFill->maColorDefinition.meType);
CPPUNIT_ASSERT_EQUAL(size_t(0), pSolidFill->maColorDefinition.maTransformations.size());
}
// Line style 3
{
model::LineStyle const& rLineStyle = rFormatScheme.getLineStyleList().at(2);
CPPUNIT_ASSERT_EQUAL(sal_Int32(19050), rLineStyle.mnWidth);
CPPUNIT_ASSERT_EQUAL(model::CapType::Flat, rLineStyle.meCapType);
CPPUNIT_ASSERT_EQUAL(model::PenAlignmentType::Center, rLineStyle.mePenAlignment);
CPPUNIT_ASSERT_EQUAL(model::CompoundLineType::Single, rLineStyle.meCompoundLineType);
CPPUNIT_ASSERT_EQUAL(model::PresetDashType::Solid, rLineStyle.maLineDash.mePresetType);
CPPUNIT_ASSERT_EQUAL(model::LineJoinType::Miter, rLineStyle.maLineJoin.meType);
CPPUNIT_ASSERT_EQUAL(sal_Int32(800000), rLineStyle.maLineJoin.mnMiterLimit);
model::FillStyle const& rFillStyle = rLineStyle.maLineFillStyle;
CPPUNIT_ASSERT(rFillStyle.mpFill);
CPPUNIT_ASSERT_EQUAL(model::FillType::Solid, rFillStyle.mpFill->meType);
auto* pSolidFill = static_cast<model::SolidFill*>(rFillStyle.mpFill.get());
CPPUNIT_ASSERT_EQUAL(model::ColorType::Placeholder, pSolidFill->maColorDefinition.meType);
CPPUNIT_ASSERT_EQUAL(size_t(0), pSolidFill->maColorDefinition.maTransformations.size());
}
}
CPPUNIT_TEST_FIXTURE(SwCoreThemeTest, testDrawPageThemeExistsODT)