Avoid potential negative array index access to cached text.
[LibreOffice.git] / svgio / inc / svgnode.hxx
blob5231635d62611801a7439abf820973f4520710ac
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 .
20 #pragma once
22 #include "SvgNumber.hxx"
23 #include "svgtoken.hxx"
24 #include <com/sun/star/xml/sax/XAttributeList.hpp>
25 #include <drawinglayer/primitive2d/Primitive2DContainer.hxx>
26 #include <memory>
27 #include <string_view>
28 #include <vector>
29 #include <optional>
31 // predefines
32 namespace svgio::svgreader
34 class SvgNode;
35 class SvgDocument;
36 class SvgStyleAttributes;
41 namespace svgio::svgreader
43 enum class XmlSpace
45 NotSet,
46 Default,
47 Preserve
50 // display property (see SVG 1.1. 11.5), not inheritable
51 enum class Display // #i121656#
53 Inline, // the default
54 Block,
55 ListItem,
56 RunIn,
57 Compact,
58 Marker,
59 Table,
60 InlineTable,
61 TableRowGroup,
62 TableHeaderGroup,
63 TableFooterGroup,
64 TableRow,
65 TableColumnGroup,
66 TableColumn,
67 TableCell,
68 TableCaption,
69 None,
70 Inherit
73 // helper to convert a string associated with a token of type SVGTokenDisplay
74 // to the enum Display. Empty strings return the default 'Display_inline' with
75 // which members should be initialized
76 Display getDisplayFromContent(std::u16string_view aContent);
78 class Visitor;
80 class SvgNode : public InfoProvider
82 private:
83 /// basic data, Type, document we belong to and parent (if not root)
84 SVGToken maType;
85 SvgDocument& mrDocument;
86 const SvgNode* mpParent;
87 const SvgNode* mpAlternativeParent;
89 /// sub hierarchy
90 std::vector< std::unique_ptr<SvgNode> > maChildren;
92 /// Id svan value
93 std::optional<OUString> mpId;
95 /// Class svan value
96 std::optional<OUString> mpClass;
98 /// systemLanguage values
99 std::vector<OUString> maSystemLanguage;
101 /// XmlSpace value
102 XmlSpace maXmlSpace;
104 /// Display value #i121656#
105 Display maDisplay;
107 // CSS style vector chain, used in decompose phase and built up once per node.
108 // It contains the StyleHierarchy for the local node. Independent from the
109 // node hierarchy itself which also needs to be used in style entry solving
110 ::std::vector< const SvgStyleAttributes* > maCssStyleVector;
112 /// possible local CssStyle, e.g. style="fill:red; stroke:red;"
113 std::unique_ptr<SvgStyleAttributes> mpLocalCssStyle;
115 mutable bool mbDecomposing;
117 // flag if maCssStyleVector is already computed (done only once)
118 bool mbCssStyleVectorBuilt : 1;
120 protected:
121 /// helper to evtl. link to css style
122 const SvgStyleAttributes* checkForCssStyle(const SvgStyleAttributes& rOriginal) const;
124 /// helper for filling the CssStyle vector once dependent on mbCssStyleVectorBuilt
125 void fillCssStyleVector(const SvgStyleAttributes& rOriginal);
126 void addCssStyle(
127 const SvgDocument& rDocument,
128 const OUString& aConcatenated);
129 void fillCssStyleVectorUsingHierarchyAndSelectors(
130 const SvgNode& rCurrent,
131 std::u16string_view aConcatenated);
132 void fillCssStyleVectorUsingParent(
133 const SvgNode& rCurrent);
135 public:
136 SvgNode(
137 SVGToken aType,
138 SvgDocument& rDocument,
139 SvgNode* pParent);
140 virtual ~SvgNode() override;
141 SvgNode(const SvgNode&) = delete;
142 SvgNode& operator=(const SvgNode&) = delete;
144 void accept(Visitor& rVisitor);
146 /// scan helper to read and interpret a local CssStyle to mpLocalCssStyle
147 void readLocalCssStyle(std::u16string_view aContent);
149 /// style helpers
150 void parseAttributes(const css::uno::Reference< css::xml::sax::XAttributeList >& xAttribs);
151 virtual const SvgStyleAttributes* getSvgStyleAttributes() const;
152 virtual void parseAttribute(SVGToken aSVGToken, const OUString& aContent);
153 virtual void decomposeSvgNode(drawinglayer::primitive2d::Primitive2DContainer& rTarget, bool bReferenced) const;
155 /// #i125258# tell if this node is allowed to have a parent style (e.g. defs do not)
156 virtual bool supportsParentStyle() const;
158 /// basic data read access
159 SVGToken getType() const { return maType; }
160 const SvgDocument& getDocument() const { return mrDocument; }
161 const SvgNode* getParent() const { if(mpAlternativeParent) return mpAlternativeParent; return mpParent; }
162 const std::vector< std::unique_ptr<SvgNode> > & getChildren() const { return maChildren; }
164 /// InfoProvider support for %, em and ex values
165 virtual basegfx::B2DRange getCurrentViewPort() const override;
166 virtual double getCurrentFontSize() const override;
167 virtual double getCurrentXHeight() const override;
169 /// Id access
170 std::optional<OUString> const & getId() const { return mpId; }
171 void setId(OUString const &);
173 /// Class access
174 std::optional<OUString> const & getClass() const { return mpClass; }
175 void setClass(OUString const &);
177 /// SystemLanguage access
178 std::vector<OUString> const & getSystemLanguage() const { return maSystemLanguage; }
179 void setSystemLanguage(OUString const &);
181 /// XmlSpace access
182 XmlSpace getXmlSpace() const;
183 void setXmlSpace(XmlSpace eXmlSpace) { maXmlSpace = eXmlSpace; }
185 /// Display access #i121656#
186 Display getDisplay() const { return maDisplay; }
187 void setDisplay(Display eDisplay) { maDisplay = eDisplay; }
189 /// alternative parent
190 void setAlternativeParent(const SvgNode* pAlternativeParent = nullptr) { mpAlternativeParent = pAlternativeParent; }
192 /// Check if there is a local css style
193 bool hasLocalCssStyle() { return static_cast<bool>(mpLocalCssStyle); }
196 class Visitor
198 public:
199 virtual ~Visitor() = default;
200 virtual void visit(SvgNode const & pNode) = 0;
203 } // end of namespace svgio::svgreader
205 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */