Updated core
[LibreOffice.git] / svgio / source / svgreader / svgstylenode.cxx
blobab9fa025ec2578098663dc10870c9080ce388184
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 <svgio/svgreader/svgstylenode.hxx>
21 #include <svgio/svgreader/svgdocument.hxx>
23 namespace svgio
25 namespace svgreader
27 SvgStyleNode::SvgStyleNode(
28 SvgDocument& rDocument,
29 SvgNode* pParent)
30 : SvgNode(SVGTokenStyle, rDocument, pParent),
31 maSvgStyleAttributes(),
32 mbTextCss(false)
36 SvgStyleNode::~SvgStyleNode()
38 while(!maSvgStyleAttributes.empty())
40 delete *(maSvgStyleAttributes.end() - 1);
41 maSvgStyleAttributes.pop_back();
45 // #i125258# no parent when we are a CssStyle holder to break potential loops because
46 // when using CssStyles we jump uncontrolled inside the node tree hierarchy
47 bool SvgStyleNode::supportsParentStyle() const
49 if(isTextCss())
51 return false;
54 // call parent
55 return SvgNode::supportsParentStyle();
58 void SvgStyleNode::parseAttribute(const OUString& rTokenName, SVGToken aSVGToken, const OUString& aContent)
60 // call parent
61 SvgNode::parseAttribute(rTokenName, aSVGToken, aContent);
63 // parse own
64 switch(aSVGToken)
66 case SVGTokenType:
68 if(!aContent.isEmpty())
70 if(aContent.startsWith("text/css"))
72 setTextCss(true);
75 break;
77 default:
79 break;
84 void SvgStyleNode::addCssStyleSheet(const OUString& aSelectors, const SvgStyleAttributes& rNewStyle)
86 // aSelectors: CssStyle selectors, any combination, no comma separations, no spaces at start/end
87 // rNewStyle: the already preapared style to register on that name
88 if(!aSelectors.isEmpty())
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, sal_Unicode(' '), nPos, aToken, nLen);
100 skip_char(aSelectors, sal_Unicode(' '), 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.size())
117 OUString aConcatenatedSelector;
119 // re-combine without spaces, create a unique name (for now)
120 for(sal_uInt32 a(0); a < aSelectorParts.size(); a++)
122 aConcatenatedSelector += 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, rNewStyle);
146 void SvgStyleNode::addCssStyleSheet(const OUString& aSelectors, const OUString& aContent)
148 // aSelectors: possible comma-separated list of CssStyle definitions, no spaces at start/end
149 // aContent: the svg style definitions as string
150 if(!aSelectors.isEmpty() && !aContent.isEmpty())
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, sal_Unicode(','), nPos, aToken, nLen);
168 skip_char(aSelectors, sal_Unicode(' '), sal_Unicode(','), 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++;
186 void SvgStyleNode::addCssStyleSheet(const OUString& aSelectorsAndContent)
188 const sal_Int32 nLen(aSelectorsAndContent.getLength());
190 if(nLen)
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, sal_Unicode(' '), nPos, nLen);
200 copyToLimiter(aSelectorsAndContent, sal_Unicode('{'), nPos, aToken, nLen);
201 skip_char(aSelectorsAndContent, sal_Unicode(' '), sal_Unicode('{'), 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, sal_Unicode('}'), nPos, aToken, nLen);
210 skip_char(aSelectorsAndContent, sal_Unicode(' '), sal_Unicode('}'), 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++;
229 } // end of namespace svgreader
230 } // end of namespace svgio
232 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */