Avoid potential negative array index access to cached text.
[LibreOffice.git] / writerfilter / source / dmapper / CellMarginHandler.cxx
blob8b7b5fa77c234f1acf4897ef4c42b41cc89998bb
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 "CellMarginHandler.hxx"
20 #include "ConversionHelper.hxx"
21 #include <ooxml/resourceids.hxx>
22 #include <comphelper/propertysequence.hxx>
23 #include <comphelper/sequence.hxx>
24 #include <sal/log.hxx>
26 namespace writerfilter::dmapper {
28 using namespace ::com::sun::star;
29 using namespace ::writerfilter;
31 CellMarginHandler::CellMarginHandler() :
32 LoggedProperties("CellMarginHandler"),
33 m_nValue( 0 ),
34 m_nWidth( 0 ),
35 m_nType( 0 ),
36 m_nLeftMargin( 0 ),
37 m_bLeftMarginValid( false ),
38 m_nRightMargin( 0 ),
39 m_bRightMarginValid( false ),
40 m_nTopMargin( 0 ),
41 m_bTopMarginValid( false ),
42 m_nBottomMargin( 0 ),
43 m_bBottomMarginValid( false )
47 CellMarginHandler::~CellMarginHandler()
51 void CellMarginHandler::lcl_attribute(Id rName, Value & rVal)
53 sal_Int32 nIntValue = rVal.getInt();
54 switch( rName )
56 case NS_ooxml::LN_CT_TblWidth_w:
57 m_nWidth = nIntValue;
58 m_nValue = ConversionHelper::convertTwipToMM100Unsigned( nIntValue );
59 break;
60 case NS_ooxml::LN_CT_TblWidth_type:
61 SAL_WARN_IF(NS_ooxml::LN_Value_ST_TblWidth_dxa != sal::static_int_cast<Id>(nIntValue), "writerfilter", "CellMarginHandler: cell margins work for absolute values only");
62 m_nType = nIntValue;
63 break;
64 default:
65 SAL_WARN("writerfilter", "CellMarginHandler::lcl_attribute: unknown attribute");
69 void CellMarginHandler::createGrabBag(const OUString& aName)
71 if (m_aInteropGrabBagName.isEmpty())
72 return;
74 beans::PropertyValue aRet;
75 aRet.Name = aName;
77 OUString sType;
78 switch (m_nType)
80 case NS_ooxml::LN_Value_ST_TblWidth_nil: sType = "nil"; break;
81 case NS_ooxml::LN_Value_ST_TblWidth_pct: sType = "pct"; break;
82 case NS_ooxml::LN_Value_ST_TblWidth_dxa: sType = "dxa"; break;
83 case NS_ooxml::LN_Value_ST_TblWidth_auto: sType = "auto"; break;
85 uno::Sequence<beans::PropertyValue> aSeq( comphelper::InitPropertySequence({
86 { "w", uno::Any(m_nWidth) },
87 { "type", uno::Any(sType) }
88 }));
90 aRet.Value <<= aSeq;
91 m_aInteropGrabBag.push_back(aRet);
94 void CellMarginHandler::lcl_sprm(Sprm & rSprm)
96 writerfilter::Reference<Properties>::Pointer_t pProperties = rSprm.getProps();
97 if( pProperties)
99 pProperties->resolve( *this );
100 const bool rtl = false; // TODO
101 switch( rSprm.getId() )
103 case NS_ooxml::LN_CT_TblCellMar_top:
104 case NS_ooxml::LN_CT_TcMar_top:
105 m_nTopMargin = m_nValue;
106 m_bTopMarginValid = true;
107 createGrabBag("top");
108 break;
109 case NS_ooxml::LN_CT_TblCellMar_start:
110 case NS_ooxml::LN_CT_TcMar_start:
111 if( rtl )
113 m_nRightMargin = m_nValue;
114 m_bRightMarginValid = true;
116 else
118 m_nLeftMargin = m_nValue;
119 m_bLeftMarginValid = true;
121 createGrabBag("start");
122 break;
123 case NS_ooxml::LN_CT_TblCellMar_left:
124 case NS_ooxml::LN_CT_TcMar_left:
125 m_nLeftMargin = m_nValue;
126 m_bLeftMarginValid = true;
127 createGrabBag("left");
128 break;
129 case NS_ooxml::LN_CT_TblCellMar_bottom:
130 case NS_ooxml::LN_CT_TcMar_bottom:
131 m_nBottomMargin = m_nValue;
132 m_bBottomMarginValid = true;
133 createGrabBag("bottom");
134 break;
135 case NS_ooxml::LN_CT_TblCellMar_end:
136 case NS_ooxml::LN_CT_TcMar_end:
137 if( rtl )
139 m_nLeftMargin = m_nValue;
140 m_bLeftMarginValid = true;
142 else
144 m_nRightMargin = m_nValue;
145 m_bRightMarginValid = true;
147 createGrabBag("end");
148 break;
149 case NS_ooxml::LN_CT_TblCellMar_right:
150 case NS_ooxml::LN_CT_TcMar_right:
151 m_nRightMargin = m_nValue;
152 m_bRightMarginValid = true;
153 createGrabBag("right");
154 break;
155 default:
156 SAL_WARN("writerfilter", "CellMarginHandler::lcl_sprm: unknown sprm");
159 m_nValue = 0;
162 void CellMarginHandler::enableInteropGrabBag(const OUString& aName)
164 m_aInteropGrabBagName = aName;
167 beans::PropertyValue CellMarginHandler::getInteropGrabBag()
169 beans::PropertyValue aRet;
170 aRet.Name = m_aInteropGrabBagName;
171 aRet.Value <<= comphelper::containerToSequence(m_aInteropGrabBag);
172 return aRet;
175 } //namespace writerfilter::dmapper
177 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */