Avoid potential negative array index access to cached text.
[LibreOffice.git] / writerfilter / source / dmapper / BorderHandler.cxx
blobed14194c2a01941a093dd1deb1bd3dfd7faf0f8e
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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
39 m_nLineType(0),
40 m_nLineColor(0),
41 m_nLineDistance(0),
42 m_bShadow(false),
43 m_bOOXML( bOOXML )
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();
56 switch( rName )
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));
62 break;
63 case NS_ooxml::LN_CT_Border_val:
64 m_nLineType = nIntValue;
65 appendGrabBag("val", TDefTableHandler::getBorderTypeString(nIntValue));
66 break;
67 case NS_ooxml::LN_CT_Border_color:
68 m_nLineColor = nIntValue;
69 appendGrabBag("color", msfilter::util::ConvertColorOU(Color(ColorTransparency, nIntValue)));
70 break;
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));
74 break;
75 case NS_ooxml::LN_CT_Border_shadow:
76 m_bShadow = nIntValue;
77 break;
78 case NS_ooxml::LN_CT_Border_frame:
79 appendGrabBag("frame", OUString::number(nIntValue, 16));
80 break;
81 case NS_ooxml::LN_CT_Border_themeTint:
82 m_nThemeTint = nIntValue;
83 appendGrabBag("themeTint", OUString::number(nIntValue, 16));
84 break;
85 case NS_ooxml::LN_CT_Border_themeShade:
86 m_nThemeShade = nIntValue;
87 appendGrabBag("themeShade", OUString::number(nIntValue, 16));
88 break;
89 case NS_ooxml::LN_CT_Border_themeColor:
90 m_eThemeColorType = TDefTableHandler::getThemeColorTypeIndex(nIntValue);
91 appendGrabBag("themeColor", TDefTableHandler::getThemeColorTypeString(nIntValue));
92 break;
93 default:
94 OSL_FAIL( "unknown attribute");
98 void BorderHandler::lcl_sprm(Sprm & rSprm)
100 BorderPosition pos;
101 const bool rtl = false; // TODO detect
102 OUString aBorderPos;
103 switch( rSprm.getId())
105 case NS_ooxml::LN_CT_TblBorders_top:
106 pos = BorderPosition::Top;
107 aBorderPos = "top";
108 break;
109 case NS_ooxml::LN_CT_TblBorders_start:
110 pos = rtl ? BorderPosition::Right : BorderPosition::Left;
111 aBorderPos = "start";
112 break;
113 case NS_ooxml::LN_CT_TblBorders_left:
114 pos = BorderPosition::Left;
115 aBorderPos = "left";
116 break;
117 case NS_ooxml::LN_CT_TblBorders_bottom:
118 pos = BorderPosition::Bottom;
119 aBorderPos = "bottom";
120 break;
121 case NS_ooxml::LN_CT_TblBorders_end:
122 pos = rtl ? BorderPosition::Left : BorderPosition::Right;
123 aBorderPos = "end";
124 break;
125 case NS_ooxml::LN_CT_TblBorders_right:
126 pos = BorderPosition::Right;
127 aBorderPos = "right";
128 break;
129 case NS_ooxml::LN_CT_TblBorders_insideH:
130 pos = BorderPosition::Horizontal;
131 aBorderPos = "insideH";
132 break;
133 case NS_ooxml::LN_CT_TblBorders_insideV:
134 pos = BorderPosition::Vertical;
135 aBorderPos = "insideV";
136 break;
137 default:
138 return;
140 writerfilter::Reference<Properties>::Pointer_t pProperties = rSprm.getProps();
141 if( pProperties)
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 =
165 PROP_TOP_BORDER,
166 PROP_LEFT_BORDER,
167 PROP_BOTTOM_BORDER,
168 PROP_RIGHT_BORDER,
169 META_PROP_HORIZONTAL_BORDER,
170 META_PROP_VERTICAL_BORDER
172 PropertyMapPtr pPropertyMap(new PropertyMap);
173 // don't fill in default properties
174 if( m_bOOXML )
176 for( auto nProp: o3tl::enumrange<BorderPosition>())
178 if ( m_aFilledLines[nProp] ) {
179 pPropertyMap->Insert( aPropNames[nProp], uno::Any( m_aBorderLines[nProp] ) );
183 return pPropertyMap;
186 table::BorderLine2 BorderHandler::getBorderLine()
188 table::BorderLine2 aBorderLine;
189 ConversionHelper::MakeBorderLine( m_nLineWidth, m_nLineType, m_nLineColor, aBorderLine, m_bOOXML );
190 return aBorderLine;
194 void BorderHandler::enableInteropGrabBag(const OUString& aName)
196 m_aInteropGrabBagName = aName;
199 beans::PropertyValue BorderHandler::getInteropGrabBag(const OUString& aName)
201 beans::PropertyValue aRet;
202 if (aName.isEmpty())
203 aRet.Name = m_aInteropGrabBagName;
204 else
205 aRet.Name = aName;
207 aRet.Value <<= comphelper::containerToSequence(m_aInteropGrabBag);
208 return aRet;
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: */