1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
19 #include "BorderHandler.hxx"
20 #include "TDefTableHandler.hxx"
21 #include "PropertyMap.hxx"
22 #include "ConversionHelper.hxx"
23 #include <com/sun/star/table/BorderLine2.hpp>
24 #include <o3tl/enumarray.hxx>
25 #include <o3tl/enumrange.hxx>
26 #include <ooxml/resourceids.hxx>
27 #include <filter/msfilter/util.hxx>
28 #include <comphelper/sequence.hxx>
29 #include <tools/color.hxx>
31 namespace writerfilter::dmapper
{
33 using namespace ::com::sun::star
;
36 BorderHandler::BorderHandler( bool bOOXML
) :
37 LoggedProperties("BorderHandler"),
38 m_nLineWidth(15), // Word default, in twips
45 m_aFilledLines
.fill(false);
46 m_aBorderLines
.fill(table::BorderLine2());
49 BorderHandler::~BorderHandler()
53 void BorderHandler::lcl_attribute(Id rName
, Value
& rVal
)
55 sal_Int32 nIntValue
= rVal
.getInt();
58 case NS_ooxml::LN_CT_Border_sz
:
59 // width of a single line in 1/8 pt, max of 32 pt -> twip * 5 / 2.
60 m_nLineWidth
= nIntValue
* 5 / 2;
61 appendGrabBag("sz", OUString::number(nIntValue
));
63 case NS_ooxml::LN_CT_Border_val
:
64 m_nLineType
= nIntValue
;
65 appendGrabBag("val", TDefTableHandler::getBorderTypeString(nIntValue
));
67 case NS_ooxml::LN_CT_Border_color
:
68 m_nLineColor
= nIntValue
;
69 appendGrabBag("color", msfilter::util::ConvertColorOU(Color(ColorTransparency
, nIntValue
)));
71 case NS_ooxml::LN_CT_Border_space
: // border distance in points
72 m_nLineDistance
= ConversionHelper::convertTwipToMM100( nIntValue
* 20 );
73 appendGrabBag("space", OUString::number(nIntValue
));
75 case NS_ooxml::LN_CT_Border_shadow
:
76 m_bShadow
= nIntValue
;
78 case NS_ooxml::LN_CT_Border_frame
:
79 appendGrabBag("frame", OUString::number(nIntValue
, 16));
81 case NS_ooxml::LN_CT_Border_themeTint
:
82 m_nThemeTint
= nIntValue
;
83 appendGrabBag("themeTint", OUString::number(nIntValue
, 16));
85 case NS_ooxml::LN_CT_Border_themeShade
:
86 m_nThemeShade
= nIntValue
;
87 appendGrabBag("themeShade", OUString::number(nIntValue
, 16));
89 case NS_ooxml::LN_CT_Border_themeColor
:
90 m_eThemeColorType
= TDefTableHandler::getThemeColorTypeIndex(nIntValue
);
91 appendGrabBag("themeColor", TDefTableHandler::getThemeColorTypeString(nIntValue
));
94 OSL_FAIL( "unknown attribute");
98 void BorderHandler::lcl_sprm(Sprm
& rSprm
)
101 const bool rtl
= false; // TODO detect
103 switch( rSprm
.getId())
105 case NS_ooxml::LN_CT_TblBorders_top
:
106 pos
= BorderPosition::Top
;
109 case NS_ooxml::LN_CT_TblBorders_start
:
110 pos
= rtl
? BorderPosition::Right
: BorderPosition::Left
;
111 aBorderPos
= "start";
113 case NS_ooxml::LN_CT_TblBorders_left
:
114 pos
= BorderPosition::Left
;
117 case NS_ooxml::LN_CT_TblBorders_bottom
:
118 pos
= BorderPosition::Bottom
;
119 aBorderPos
= "bottom";
121 case NS_ooxml::LN_CT_TblBorders_end
:
122 pos
= rtl
? BorderPosition::Left
: BorderPosition::Right
;
125 case NS_ooxml::LN_CT_TblBorders_right
:
126 pos
= BorderPosition::Right
;
127 aBorderPos
= "right";
129 case NS_ooxml::LN_CT_TblBorders_insideH
:
130 pos
= BorderPosition::Horizontal
;
131 aBorderPos
= "insideH";
133 case NS_ooxml::LN_CT_TblBorders_insideV
:
134 pos
= BorderPosition::Vertical
;
135 aBorderPos
= "insideV";
140 writerfilter::Reference
<Properties
>::Pointer_t pProperties
= rSprm
.getProps();
143 std::vector
<beans::PropertyValue
> aSavedGrabBag
;
144 if (!m_aInteropGrabBagName
.isEmpty())
146 aSavedGrabBag
= m_aInteropGrabBag
;
147 m_aInteropGrabBag
.clear();
149 pProperties
->resolve(*this);
150 if (!m_aInteropGrabBagName
.isEmpty())
152 aSavedGrabBag
.push_back(getInteropGrabBag(aBorderPos
));
153 m_aInteropGrabBag
= aSavedGrabBag
;
156 ConversionHelper::MakeBorderLine( m_nLineWidth
, m_nLineType
, m_nLineColor
,
157 m_aBorderLines
[ pos
], m_bOOXML
);
158 m_aFilledLines
[ pos
] = true;
161 PropertyMapPtr
BorderHandler::getProperties()
163 static const o3tl::enumarray
<BorderPosition
, PropertyIds
> aPropNames
=
169 META_PROP_HORIZONTAL_BORDER
,
170 META_PROP_VERTICAL_BORDER
172 PropertyMapPtr
pPropertyMap(new PropertyMap
);
173 // don't fill in default properties
176 for( auto nProp
: o3tl::enumrange
<BorderPosition
>())
178 if ( m_aFilledLines
[nProp
] ) {
179 pPropertyMap
->Insert( aPropNames
[nProp
], uno::Any( m_aBorderLines
[nProp
] ) );
186 table::BorderLine2
BorderHandler::getBorderLine()
188 table::BorderLine2 aBorderLine
;
189 ConversionHelper::MakeBorderLine( m_nLineWidth
, m_nLineType
, m_nLineColor
, aBorderLine
, m_bOOXML
);
194 void BorderHandler::enableInteropGrabBag(const OUString
& aName
)
196 m_aInteropGrabBagName
= aName
;
199 beans::PropertyValue
BorderHandler::getInteropGrabBag(const OUString
& aName
)
201 beans::PropertyValue aRet
;
203 aRet
.Name
= m_aInteropGrabBagName
;
207 aRet
.Value
<<= comphelper::containerToSequence(m_aInteropGrabBag
);
211 void BorderHandler::appendGrabBag(const OUString
& aKey
, const OUString
& aValue
)
213 beans::PropertyValue aProperty
;
214 aProperty
.Name
= aKey
;
215 aProperty
.Value
<<= aValue
;
216 m_aInteropGrabBag
.push_back(aProperty
);
219 model::ComplexColor
BorderHandler::getComplexColor() const
221 model::ComplexColor aComplexColor
;
222 if (m_eThemeColorType
== model::ThemeColorType::Unknown
)
223 return aComplexColor
;
225 aComplexColor
.setThemeColor(m_eThemeColorType
);
227 if (m_nThemeTint
> 0 )
229 sal_Int16 nTint
= sal_Int16((255.0 - m_nThemeTint
) * 10000.0 / 255.0);
230 aComplexColor
.addTransformation({model::TransformationType::Tint
, nTint
});
232 if (m_nThemeShade
> 0)
234 sal_Int16 nShade
= sal_Int16((255.0 - m_nThemeShade
) * 10000 / 255.0);
235 aComplexColor
.addTransformation({model::TransformationType::Shade
, nShade
});
238 return aComplexColor
;
241 } //namespace writerfilter::dmapper
243 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */