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     {
34         SvgStyleNode::SvgStyleNode(
35             SvgDocument& rDocument,
36             SvgNode* pParent)
37         :   SvgNode(SVGTokenStyle, rDocument, pParent),
38             maSvgStyleAttributes(),
39             mbTextCss(false)
40         {
41         }
42 
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
54         bool SvgStyleNode::supportsParentStyle() const
55         {
56             if(isTextCss())
57             {
58                 return false;
59             }
60 
61             // call parent
62             return SvgNode::supportsParentStyle();
63         }
64 
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 
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                     // read the full style node names (may be multiple) and put to aStyleName
105                     const sal_Int32 nInitPos(nPos);
106                     skip_char(aContent, sal_Unicode(' '), nPos, nLen);
107                     copyToLimiter(aContent, sal_Unicode('{'), nPos, aTokenValue, nLen);
108                     skip_char(aContent, sal_Unicode(' '), sal_Unicode('{'), nPos, nLen);
109 
110                     const rtl::OUString aStyleName(aTokenValue.makeStringAndClear().trim());
111                     const sal_Int32 nLen2(aStyleName.getLength());
112                     std::vector< rtl::OUString > aStyleNames;
113 
114                     if(nLen2)
115                     {
116                         // extract names
117                         sal_Int32 nPos2(0);
118                         rtl::OUStringBuffer aSingleName;
119 
120                         while(nPos2 < nLen2)
121                         {
122                             skip_char(aStyleName, sal_Unicode('#'), nPos2, nLen2);
123                             copyToLimiter(aStyleName, sal_Unicode(' '), nPos2, aSingleName, nLen2);
124                             skip_char(aStyleName, sal_Unicode(' '), nPos2, nLen2);
125 
126                             const rtl::OUString aOUSingleName(aSingleName.makeStringAndClear().trim());
127 
128                             if(aOUSingleName.getLength())
129                             {
130                                 aStyleNames.push_back(aOUSingleName);
131                             }
132                         }
133                     }
134 
135                     if(aStyleNames.size() && nPos < nLen)
136                     {
137                         copyToLimiter(aContent, sal_Unicode('}'), nPos, aTokenValue, nLen);
138                         skip_char(aContent, sal_Unicode(' '), sal_Unicode('}'), nPos, nLen);
139                         const rtl::OUString aStyleContent(aTokenValue.makeStringAndClear().trim());
140 
141                         if(aStyleContent.getLength())
142                         {
143                             // create new style
144                             SvgStyleAttributes* pNewStyle = new SvgStyleAttributes(*this);
145                             maSvgStyleAttributes.push_back(pNewStyle);
146 
147                             // fill with content
148                             pNewStyle->readStyle(aStyleContent);
149 
150                             // concatenate combined style name
151                             rtl::OUString aConcatenatedStyleName;
152 
153                             for(sal_uInt32 a(0); a < aStyleNames.size(); a++)
154                             {
155                                 aConcatenatedStyleName += aStyleNames[a];
156                             }
157 
158                             // register new style at document for (evtl. concatenated) stylename
159                             const_cast< SvgDocument& >(getDocument()).addSvgStyleAttributesToMapper(aConcatenatedStyleName, *pNewStyle);
160                         }
161                     }
162 
163                     if(nInitPos == nPos)
164                     {
165                         OSL_ENSURE(false, "Could not interpret on current position (!)");
166                         nPos++;
167                     }
168                 }
169             }
170         }
171 
172     } // end of namespace svgreader
173 } // end of namespace svgio
174 
175 //////////////////////////////////////////////////////////////////////////////
176 // eof
177