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 .
20 #include <svgstylenode.hxx>
21 #include <svgdocument.hxx>
23 namespace svgio::svgreader
25 SvgStyleNode::SvgStyleNode(
26 SvgDocument
& rDocument
,
28 : SvgNode(SVGTokenStyle
, rDocument
, pParent
),
29 maSvgStyleAttributes(),
34 SvgStyleNode::~SvgStyleNode()
36 while(!maSvgStyleAttributes
.empty())
38 delete *(maSvgStyleAttributes
.end() - 1);
39 maSvgStyleAttributes
.pop_back();
43 // #i125258# no parent when we are a CssStyle holder to break potential loops because
44 // when using CssStyles we jump uncontrolled inside the node tree hierarchy
45 bool SvgStyleNode::supportsParentStyle() const
53 return SvgNode::supportsParentStyle();
56 void SvgStyleNode::parseAttribute(const OUString
& rTokenName
, SVGToken aSVGToken
, const OUString
& aContent
)
59 SvgNode::parseAttribute(rTokenName
, aSVGToken
, aContent
);
66 if(!aContent
.isEmpty())
68 if(aContent
.startsWith("text/css"))
82 void SvgStyleNode::addCssStyleSheet(const OUString
& aSelectors
, const SvgStyleAttributes
& rNewStyle
)
84 // aSelectors: CssStyle selectors, any combination, no comma separations, no spaces at start/end
85 // rNewStyle: the already prepared style to register on that name
86 if(aSelectors
.isEmpty())
89 std::vector
< OUString
> aSelectorParts
;
90 const sal_Int32
nLen(aSelectors
.getLength());
92 OUStringBuffer aToken
;
94 // split into single tokens (currently only space separator)
97 const sal_Int32
nInitPos(nPos
);
98 copyToLimiter(aSelectors
, u
' ', nPos
, aToken
, nLen
);
99 skip_char(aSelectors
, u
' ', nPos
, nLen
);
100 const OUString
aSelectorPart(aToken
.makeStringAndClear().trim());
102 if(!aSelectorPart
.isEmpty())
104 aSelectorParts
.push_back(aSelectorPart
);
109 OSL_ENSURE(false, "Could not interpret on current position (!)");
114 if(aSelectorParts
.empty())
117 OUStringBuffer aConcatenatedSelector
;
119 // re-combine without spaces, create a unique name (for now)
120 for(size_t a(0); a
< aSelectorParts
.size(); a
++)
122 aConcatenatedSelector
.append(aSelectorParts
[a
]);
125 // CssStyles in SVG are currently not completely supported; the current idea for
126 // supporting the needed minimal set is to register CssStyles associated to a string
127 // which is just the space-char cleaned, concatenated Selectors. The part to 'match'
128 // these is in fillCssStyleVectorUsingHierarchyAndSelectors. There, the same string is
129 // built up using the priorities of local CssStyle, Id, Class and other info combined
130 // with the existing hierarchy. This creates a specificity and priority-sorted local
131 // list for each node which is then chained using get/setCssStyleParent.
132 // The current solution is capable of solving space-separated selectors which can be
133 // mixed between Id, Class and type specifiers.
134 // When CssStyles need more specific solving, the start point is here; remember the
135 // needed infos not in maIdStyleTokenMapperList at the document, but select evtl.
136 // more specific infos there in a class capable of handling more complex matchings.
137 // Additionally fillCssStyleVector (or the mechanism above that when a linked list of
138 // SvgStyleAttributes will not do it) will have to be adapted to make use of it.
140 // register new style at document for (evtl. concatenated) stylename
141 const_cast< SvgDocument
& >(getDocument()).addSvgStyleAttributesToMapper(aConcatenatedSelector
.makeStringAndClear(), rNewStyle
);
144 void SvgStyleNode::addCssStyleSheet(const OUString
& aSelectors
, const OUString
& aContent
)
146 // aSelectors: possible comma-separated list of CssStyle definitions, no spaces at start/end
147 // aContent: the svg style definitions as string
148 if(aSelectors
.isEmpty() || aContent
.isEmpty())
151 // create new style and add to local list (for ownership control)
152 SvgStyleAttributes
* pNewStyle
= new SvgStyleAttributes(*this);
153 maSvgStyleAttributes
.push_back(pNewStyle
);
156 pNewStyle
->readCssStyle(aContent
);
158 // comma-separated split (Css abbreviation for same style for multiple selectors)
159 const sal_Int32
nLen(aSelectors
.getLength());
161 OUStringBuffer aToken
;
165 const sal_Int32
nInitPos(nPos
);
166 copyToLimiter(aSelectors
, u
',', nPos
, aToken
, nLen
);
167 skip_char(aSelectors
, u
' ', u
',', nPos
, nLen
);
169 const OUString
aSingleName(aToken
.makeStringAndClear().trim());
171 if(aSingleName
.getLength())
173 addCssStyleSheet(aSingleName
, *pNewStyle
);
178 OSL_ENSURE(false, "Could not interpret on current position (!)");
184 void SvgStyleNode::addCssStyleSheet(const OUString
& aSelectorsAndContent
)
186 const sal_Int32
nLen(aSelectorsAndContent
.getLength());
192 OUStringBuffer aToken
;
196 // read the full selectors (may be multiple, comma-separated)
197 const sal_Int32
nInitPos(nPos
);
198 skip_char(aSelectorsAndContent
, u
' ', nPos
, nLen
);
199 copyToLimiter(aSelectorsAndContent
, u
'{', nPos
, aToken
, nLen
);
200 skip_char(aSelectorsAndContent
, u
' ', u
'{', nPos
, nLen
);
202 const OUString
aSelectors(aToken
.makeStringAndClear().trim());
205 if(!aSelectors
.isEmpty() && nPos
< nLen
)
207 // isolate content as text, embraced by '{' and '}'
208 copyToLimiter(aSelectorsAndContent
, u
'}', nPos
, aToken
, nLen
);
209 skip_char(aSelectorsAndContent
, u
' ', u
'}', nPos
, nLen
);
211 aContent
= aToken
.makeStringAndClear().trim();
214 if(!aSelectors
.isEmpty() && !aContent
.isEmpty())
216 addCssStyleSheet(aSelectors
, aContent
);
221 OSL_ENSURE(false, "Could not interpret on current position (!)");
227 } // end of namespace svgio::svgreader
229 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */