Avoid potential negative array index access to cached text.
[LibreOffice.git] / xmloff / source / text / XMLComplexColorContext.cxx
blobc580b022256ad92da96192e410ab7897be6e01ac
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/.
8 */
10 #include <sal/config.h>
12 #include <sal/log.hxx>
13 #include <sax/tools/converter.hxx>
14 #include <xmloff/xmluconv.hxx>
15 #include <xmloff/namespacemap.hxx>
16 #include <xmloff/xmlnamespace.hxx>
17 #include <xmloff/xmlimp.hxx>
18 #include <xmloff/xmltoken.hxx>
19 #include <xmloff/xmlement.hxx>
20 #include <xmloff/xmlprhdl.hxx>
21 #include <xmloff/XMLComplexColorContext.hxx>
22 #include <docmodel/uno/UnoComplexColor.hxx>
24 using namespace css;
25 using namespace xmloff::token;
27 SvXMLEnumMapEntry<sal_Int16> const pXML_ThemeColor_Enum[] = { { XML_NONE, -1 },
28 { XML_DARK1, 0 },
29 { XML_LIGHT1, 1 },
30 { XML_DARK2, 2 },
31 { XML_LIGHT2, 3 },
32 { XML_ACCENT1, 4 },
33 { XML_ACCENT2, 5 },
34 { XML_ACCENT3, 6 },
35 { XML_ACCENT4, 7 },
36 { XML_ACCENT5, 8 },
37 { XML_ACCENT6, 9 },
38 { XML_HYPERLINK, 10 },
39 { XML_FOLLOWED_HYPERLINK, 11 },
40 { XML_TOKEN_INVALID, 0 } };
42 XMLComplexColorImport::XMLComplexColorImport(model::ComplexColor& rComplexColor)
43 : mrComplexColor(rComplexColor)
47 void XMLComplexColorImport::fillAttributes(
48 const uno::Reference<xml::sax::XFastAttributeList>& xAttrList)
50 for (auto& aIter : sax_fastparser::castToFastAttributeList(xAttrList))
52 switch (aIter.getToken())
54 case XML_ELEMENT(LO_EXT, XML_THEME_TYPE):
56 sal_Int16 nValue = -1;
57 if (SvXMLUnitConverter::convertEnum(nValue, aIter.toView(), pXML_ThemeColor_Enum))
59 mrComplexColor.setThemeColor(model::convertToThemeColorType(nValue));
61 break;
63 case XML_ELEMENT(LO_EXT, XML_COLOR_TYPE):
65 const OUString aValue = aIter.toString();
66 if (aValue == u"theme")
67 mrComplexColor.setType(model::ColorType::Theme);
68 // TODO - handle other color types
69 break;
71 default:
72 XMLOFF_WARN_UNKNOWN("xmloff", aIter);
73 break;
78 bool XMLComplexColorImport::handleTransformContext(
79 sal_Int32 nElement, const css::uno::Reference<css::xml::sax::XFastAttributeList>& xAttrList)
81 if (nElement == XML_ELEMENT(LO_EXT, XML_TRANSFORMATION))
83 auto eTransformationType = model::TransformationType::Undefined;
84 sal_Int16 nTransformationValue = 0;
85 for (auto& aIter : sax_fastparser::castToFastAttributeList(xAttrList))
87 switch (aIter.getToken())
89 case XML_ELEMENT(LO_EXT, XML_TYPE):
91 const OUString aValue = aIter.toString();
92 if (aValue == u"tint")
93 eTransformationType = model::TransformationType::Tint;
94 else if (aValue == u"shade")
95 eTransformationType = model::TransformationType::Shade;
96 else if (aValue == u"lumoff")
97 eTransformationType = model::TransformationType::LumOff;
98 else if (aValue == u"lummod")
99 eTransformationType = model::TransformationType::LumMod;
100 break;
102 case XML_ELEMENT(LO_EXT, XML_VALUE):
104 sal_Int32 nValue;
105 if (::sax::Converter::convertNumber(nValue, aIter.toView(), SHRT_MIN, SHRT_MAX))
106 nTransformationValue = static_cast<sal_Int16>(nValue);
107 break;
109 default:
110 XMLOFF_WARN_UNKNOWN("xmloff", aIter);
111 break;
114 mrComplexColor.addTransformation({ eTransformationType, nTransformationValue });
115 return true;
117 XMLOFF_WARN_UNKNOWN_ELEMENT("xmloff", nElement);
118 return false;
121 XMLPropertyComplexColorContext::XMLPropertyComplexColorContext(
122 SvXMLImport& rImport, sal_Int32 nElement,
123 const uno::Reference<xml::sax::XFastAttributeList>& xAttrList, const XMLPropertyState& rProp,
124 std::vector<XMLPropertyState>& rProps)
125 : XMLElementPropertyContext(rImport, nElement, rProp, rProps)
126 , mnRootElement(nElement)
127 , maComplexColorImport(maComplexColor)
129 maComplexColorImport.fillAttributes(xAttrList);
132 css::uno::Reference<css::xml::sax::XFastContextHandler>
133 XMLPropertyComplexColorContext::createFastChildContext(
134 sal_Int32 nElement, const css::uno::Reference<css::xml::sax::XFastAttributeList>& xAttrList)
136 if (maComplexColorImport.handleTransformContext(nElement, xAttrList))
137 return this;
138 return nullptr;
141 void XMLPropertyComplexColorContext::endFastElement(sal_Int32 nElement)
143 if (nElement == mnRootElement)
145 if (getComplexColor().getThemeColorType() != model::ThemeColorType::Unknown)
147 aProp.maValue <<= model::color::createXComplexColor(getComplexColor());
148 SetInsert(true);
151 XMLElementPropertyContext::endFastElement(nElement);
154 XMLComplexColorContext::XMLComplexColorContext(
155 SvXMLImport& rImport, model::ComplexColor& rComplexColor,
156 const uno::Reference<xml::sax::XFastAttributeList>& xAttrList)
157 : SvXMLImportContext(rImport)
158 , maComplexColorImport(rComplexColor)
160 maComplexColorImport.fillAttributes(xAttrList);
163 css::uno::Reference<css::xml::sax::XFastContextHandler>
164 XMLComplexColorContext::createFastChildContext(
165 sal_Int32 nElement, const css::uno::Reference<css::xml::sax::XFastAttributeList>& xAttrList)
167 if (maComplexColorImport.handleTransformContext(nElement, xAttrList))
168 return this;
169 return nullptr;
172 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */