1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 // MARKER(update_precomp.py): autogen include statement, do not remove
23 #include "precompiled_svgio.hxx"
24 
25 #include <svgio/svgreader/svgstylenode.hxx>
26 #include <svgio/svgreader/svgdocument.hxx>
27 
28 //////////////////////////////////////////////////////////////////////////////
29 
30 namespace svgio
31 {
32     namespace svgreader
33     {
SvgStyleNode(SvgDocument & rDocument,SvgNode * pParent)34         SvgStyleNode::SvgStyleNode(
35             SvgDocument& rDocument,
36             SvgNode* pParent)
37         :   SvgNode(SVGTokenStyle, rDocument, pParent),
38             maSvgStyleAttributes(),
39             mbTextCss(false)
40         {
41         }
42 
~SvgStyleNode()43         SvgStyleNode::~SvgStyleNode()
44         {
45             while(!maSvgStyleAttributes.empty())
46             {
47                 delete *(maSvgStyleAttributes.end() - 1);
48                 maSvgStyleAttributes.pop_back();
49             }
50         }
51 
52         // #125258# no parent when we are a CssStyle holder to break potential loops because
53         // when using CssStyles we jump uncontrolled inside the node tree hierarchy
supportsParentStyle() const54         bool SvgStyleNode::supportsParentStyle() const
55         {
56             if(isTextCss())
57             {
58                 return false;
59             }
60 
61             // call parent
62             return SvgNode::supportsParentStyle();
63         }
64 
parseAttribute(const rtl::OUString & rTokenName,SVGToken aSVGToken,const rtl::OUString & aContent)65         void SvgStyleNode::parseAttribute(const rtl::OUString& rTokenName, SVGToken aSVGToken, const rtl::OUString& aContent)
66         {
67             // call parent
68             SvgNode::parseAttribute(rTokenName, aSVGToken, aContent);
69 
70             // parse own
71             switch(aSVGToken)
72             {
73                 case SVGTokenType:
74                 {
75                     if(aContent.getLength())
76                     {
77                         static rtl::OUString aStrTextCss(rtl::OUString::createFromAscii("text/css"));
78 
79                         if(aContent.match(aStrTextCss))
80                         {
81                             setTextCss(true);
82                         }
83                     }
84                     break;
85                 }
86                 default:
87                 {
88                     break;
89                 }
90             }
91         }
92 
addCssStyleSheet(const rtl::OUString & aContent)93         void SvgStyleNode::addCssStyleSheet(const rtl::OUString& aContent)
94         {
95             const sal_Int32 nLen(aContent.getLength());
96 
97             if(nLen)
98             {
99                 sal_Int32 nPos(0);
100                 rtl::OUStringBuffer aTokenValue;
101 
102                 while(nPos < nLen)
103                 {
104                     const sal_Int32 nInitPos(nPos);
105                     skip_char(aContent, sal_Unicode(' '), sal_Unicode('#'), nPos, nLen);
106                     copyToLimiter(aContent, sal_Unicode('{'), nPos, aTokenValue, nLen);
107                     const rtl::OUString aStyleName = aTokenValue.makeStringAndClear().trim();
108 
109                     if(aStyleName.getLength() && nPos < nLen)
110                     {
111                         skip_char(aContent, sal_Unicode(' '), sal_Unicode('{'), nPos, nLen);
112                         copyToLimiter(aContent, sal_Unicode('}'), nPos, aTokenValue, nLen);
113                         skip_char(aContent, sal_Unicode(' '), sal_Unicode('}'), nPos, nLen);
114                         const rtl::OUString aStyleContent = aTokenValue.makeStringAndClear().trim();
115 
116                         if(aStyleContent.getLength())
117                         {
118                             // create new style
119                             SvgStyleAttributes* pNewStyle = new SvgStyleAttributes(*this);
120                             maSvgStyleAttributes.push_back(pNewStyle);
121 
122                             // fill with content
123                             pNewStyle->readStyle(aStyleContent);
124 
125                             // register new style at document
126                             const_cast< SvgDocument& >(getDocument()).addSvgStyleAttributesToMapper(aStyleName, *pNewStyle);
127                         }
128                     }
129 
130                     if(nInitPos == nPos)
131                     {
132                         OSL_ENSURE(false, "Could not interpret on current position (!)");
133                         nPos++;
134                     }
135                 }
136             }
137         }
138 
139     } // end of namespace svgreader
140 } // end of namespace svgio
141 
142 //////////////////////////////////////////////////////////////////////////////
143 // eof
144