bump product version to 7.2.5.1
[LibreOffice.git] / svgio / source / svgreader / svgstylenode.cxx
blobd9a335fede12e2f3897a37aa060e933384f738d7
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 #include <svgstylenode.hxx>
21 #include <svgdocument.hxx>
22 #include <osl/diagnose.h>
24 namespace svgio::svgreader
26 SvgStyleNode::SvgStyleNode(
27 SvgDocument& rDocument,
28 SvgNode* pParent)
29 : SvgNode(SVGToken::Style, rDocument, pParent),
30 maSvgStyleAttributes(),
31 mbTextCss(false)
35 SvgStyleNode::~SvgStyleNode()
37 while(!maSvgStyleAttributes.empty())
39 delete *(maSvgStyleAttributes.end() - 1);
40 maSvgStyleAttributes.pop_back();
44 // #i125258# no parent when we are a CssStyle holder to break potential loops because
45 // when using CssStyles we jump uncontrolled inside the node tree hierarchy
46 bool SvgStyleNode::supportsParentStyle() const
48 if(isTextCss())
50 return false;
53 // call parent
54 return SvgNode::supportsParentStyle();
57 void SvgStyleNode::parseAttribute(const OUString& rTokenName, SVGToken aSVGToken, const OUString& aContent)
59 // call parent
60 SvgNode::parseAttribute(rTokenName, aSVGToken, aContent);
62 // parse own
63 switch(aSVGToken)
65 case SVGToken::Type:
67 if(!aContent.isEmpty())
69 if(aContent.startsWith("text/css"))
71 setTextCss(true);
74 break;
76 default:
78 break;
83 void SvgStyleNode::addCssStyleSheet(const OUString& aSelectors, const SvgStyleAttributes& rNewStyle)
85 // aSelectors: CssStyle selectors, any combination, no comma separations, no spaces at start/end
86 // rNewStyle: the already prepared style to register on that name
87 if(aSelectors.isEmpty())
88 return;
90 std::vector< OUString > aSelectorParts;
91 const sal_Int32 nLen(aSelectors.getLength());
92 sal_Int32 nPos(0);
93 OUStringBuffer aToken;
95 // split into single tokens (currently only space separator)
96 while(nPos < nLen)
98 const sal_Int32 nInitPos(nPos);
99 copyToLimiter(aSelectors, u' ', nPos, aToken, nLen);
100 skip_char(aSelectors, u' ', nPos, nLen);
101 const OUString aSelectorPart(aToken.makeStringAndClear().trim());
103 if(!aSelectorPart.isEmpty())
105 aSelectorParts.push_back(aSelectorPart);
108 if(nInitPos == nPos)
110 OSL_ENSURE(false, "Could not interpret on current position (!)");
111 nPos++;
115 if(aSelectorParts.empty())
116 return;
118 OUStringBuffer aConcatenatedSelector;
120 // re-combine without spaces, create a unique name (for now)
121 for(size_t a(0); a < aSelectorParts.size(); a++)
123 aConcatenatedSelector.append(aSelectorParts[a]);
126 // CssStyles in SVG are currently not completely supported; the current idea for
127 // supporting the needed minimal set is to register CssStyles associated to a string
128 // which is just the space-char cleaned, concatenated Selectors. The part to 'match'
129 // these is in fillCssStyleVectorUsingHierarchyAndSelectors. There, the same string is
130 // built up using the priorities of local CssStyle, Id, Class and other info combined
131 // with the existing hierarchy. This creates a specificity and priority-sorted local
132 // list for each node which is then chained using get/setCssStyleParent.
133 // The current solution is capable of solving space-separated selectors which can be
134 // mixed between Id, Class and type specifiers.
135 // When CssStyles need more specific solving, the start point is here; remember the
136 // needed infos not in maIdStyleTokenMapperList at the document, but select evtl.
137 // more specific infos there in a class capable of handling more complex matchings.
138 // Additionally fillCssStyleVector (or the mechanism above that when a linked list of
139 // SvgStyleAttributes will not do it) will have to be adapted to make use of it.
141 // register new style at document for (evtl. concatenated) stylename
142 const_cast< SvgDocument& >(getDocument()).addSvgStyleAttributesToMapper(aConcatenatedSelector.makeStringAndClear(), rNewStyle);
145 void SvgStyleNode::addCssStyleSheet(const OUString& aSelectors, const OUString& aContent)
147 // aSelectors: possible comma-separated list of CssStyle definitions, no spaces at start/end
148 // aContent: the svg style definitions as string
149 if(aSelectors.isEmpty() || aContent.isEmpty())
150 return;
152 // create new style and add to local list (for ownership control)
153 SvgStyleAttributes* pNewStyle = new SvgStyleAttributes(*this);
154 maSvgStyleAttributes.push_back(pNewStyle);
156 // fill with content
157 pNewStyle->readCssStyle(aContent);
159 // comma-separated split (Css abbreviation for same style for multiple selectors)
160 const sal_Int32 nLen(aSelectors.getLength());
161 sal_Int32 nPos(0);
162 OUStringBuffer aToken;
164 while(nPos < nLen)
166 const sal_Int32 nInitPos(nPos);
167 copyToLimiter(aSelectors, u',', nPos, aToken, nLen);
168 skip_char(aSelectors, u' ', u',', nPos, nLen);
170 const OUString aSingleName(aToken.makeStringAndClear().trim());
172 if(aSingleName.getLength())
174 addCssStyleSheet(aSingleName, *pNewStyle);
177 if(nInitPos == nPos)
179 OSL_ENSURE(false, "Could not interpret on current position (!)");
180 nPos++;
185 void SvgStyleNode::addCssStyleSheet(const OUString& aSelectorsAndContent)
187 const sal_Int32 nLen(aSelectorsAndContent.getLength());
189 if(!nLen)
190 return;
192 sal_Int32 nPos(0);
193 OUStringBuffer aToken;
195 while(nPos < nLen)
197 // read the full selectors (may be multiple, comma-separated)
198 const sal_Int32 nInitPos(nPos);
199 skip_char(aSelectorsAndContent, u' ', nPos, nLen);
200 copyToLimiter(aSelectorsAndContent, u'{', nPos, aToken, nLen);
201 skip_char(aSelectorsAndContent, u' ', u'{', nPos, nLen);
203 const OUString aSelectors(aToken.makeStringAndClear().trim());
204 OUString aContent;
206 if(!aSelectors.isEmpty() && nPos < nLen)
208 // isolate content as text, embraced by '{' and '}'
209 copyToLimiter(aSelectorsAndContent, u'}', nPos, aToken, nLen);
210 skip_char(aSelectorsAndContent, u' ', u'}', nPos, nLen);
212 aContent = aToken.makeStringAndClear().trim();
215 if(!aSelectors.isEmpty() && !aContent.isEmpty())
217 addCssStyleSheet(aSelectors, aContent);
220 if(nInitPos == nPos)
222 OSL_ENSURE(false, "Could not interpret on current position (!)");
223 nPos++;
228 } // end of namespace svgio::svgreader
230 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */