1ddde725dSArmin Le Grand /**************************************************************
2ddde725dSArmin Le Grand  *
3ddde725dSArmin Le Grand  * Licensed to the Apache Software Foundation (ASF) under one
4ddde725dSArmin Le Grand  * or more contributor license agreements.  See the NOTICE file
5ddde725dSArmin Le Grand  * distributed with this work for additional information
6ddde725dSArmin Le Grand  * regarding copyright ownership.  The ASF licenses this file
7ddde725dSArmin Le Grand  * to you under the Apache License, Version 2.0 (the
8ddde725dSArmin Le Grand  * "License"); you may not use this file except in compliance
9ddde725dSArmin Le Grand  * with the License.  You may obtain a copy of the License at
10ddde725dSArmin Le Grand  *
11ddde725dSArmin Le Grand  *   http://www.apache.org/licenses/LICENSE-2.0
12ddde725dSArmin Le Grand  *
13ddde725dSArmin Le Grand  * Unless required by applicable law or agreed to in writing,
14ddde725dSArmin Le Grand  * software distributed under the License is distributed on an
15ddde725dSArmin Le Grand  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16ddde725dSArmin Le Grand  * KIND, either express or implied.  See the License for the
17ddde725dSArmin Le Grand  * specific language governing permissions and limitations
18ddde725dSArmin Le Grand  * under the License.
19ddde725dSArmin Le Grand  *
20ddde725dSArmin Le Grand  *************************************************************/
21ddde725dSArmin Le Grand 
22ddde725dSArmin Le Grand // MARKER(update_precomp.py): autogen include statement, do not remove
23ddde725dSArmin Le Grand #include "precompiled_svgio.hxx"
24ddde725dSArmin Le Grand 
25ddde725dSArmin Le Grand #include <svgio/svgreader/svgstylenode.hxx>
26ddde725dSArmin Le Grand #include <svgio/svgreader/svgdocument.hxx>
27ddde725dSArmin Le Grand 
28ddde725dSArmin Le Grand //////////////////////////////////////////////////////////////////////////////
29ddde725dSArmin Le Grand 
30ddde725dSArmin Le Grand namespace svgio
31ddde725dSArmin Le Grand {
32ddde725dSArmin Le Grand     namespace svgreader
33ddde725dSArmin Le Grand     {
34ddde725dSArmin Le Grand         SvgStyleNode::SvgStyleNode(
35ddde725dSArmin Le Grand             SvgDocument& rDocument,
36ddde725dSArmin Le Grand             SvgNode* pParent)
37ddde725dSArmin Le Grand         :   SvgNode(SVGTokenStyle, rDocument, pParent),
38ddde725dSArmin Le Grand             maSvgStyleAttributes(),
39ddde725dSArmin Le Grand             mbTextCss(false)
40ddde725dSArmin Le Grand         {
41ddde725dSArmin Le Grand         }
42ddde725dSArmin Le Grand 
43ddde725dSArmin Le Grand         SvgStyleNode::~SvgStyleNode()
44ddde725dSArmin Le Grand         {
45ddde725dSArmin Le Grand             while(!maSvgStyleAttributes.empty())
46ddde725dSArmin Le Grand             {
47ddde725dSArmin Le Grand                 delete *(maSvgStyleAttributes.end() - 1);
48ddde725dSArmin Le Grand                 maSvgStyleAttributes.pop_back();
49ddde725dSArmin Le Grand             }
50ddde725dSArmin Le Grand         }
51ddde725dSArmin Le Grand 
524374d266SArmin Le Grand         // #125258# no parent when we are a CssStyle holder to break potential loops because
534374d266SArmin Le Grand         // when using CssStyles we jump uncontrolled inside the node tree hierarchy
544374d266SArmin Le Grand         bool SvgStyleNode::supportsParentStyle() const
554374d266SArmin Le Grand         {
564374d266SArmin Le Grand             if(isTextCss())
574374d266SArmin Le Grand             {
584374d266SArmin Le Grand                 return false;
594374d266SArmin Le Grand             }
604374d266SArmin Le Grand 
614374d266SArmin Le Grand             // call parent
624374d266SArmin Le Grand             return SvgNode::supportsParentStyle();
634374d266SArmin Le Grand         }
644374d266SArmin Le Grand 
65ddde725dSArmin Le Grand         void SvgStyleNode::parseAttribute(const rtl::OUString& rTokenName, SVGToken aSVGToken, const rtl::OUString& aContent)
66ddde725dSArmin Le Grand         {
67ddde725dSArmin Le Grand             // call parent
68ddde725dSArmin Le Grand             SvgNode::parseAttribute(rTokenName, aSVGToken, aContent);
69ddde725dSArmin Le Grand 
70ddde725dSArmin Le Grand             // parse own
71ddde725dSArmin Le Grand             switch(aSVGToken)
72ddde725dSArmin Le Grand             {
73ddde725dSArmin Le Grand                 case SVGTokenType:
74ddde725dSArmin Le Grand                 {
75ddde725dSArmin Le Grand                     if(aContent.getLength())
76ddde725dSArmin Le Grand                     {
77ddde725dSArmin Le Grand                         static rtl::OUString aStrTextCss(rtl::OUString::createFromAscii("text/css"));
78ddde725dSArmin Le Grand 
79ddde725dSArmin Le Grand                         if(aContent.match(aStrTextCss))
80ddde725dSArmin Le Grand                         {
81ddde725dSArmin Le Grand                             setTextCss(true);
82ddde725dSArmin Le Grand                         }
83ddde725dSArmin Le Grand                     }
84ddde725dSArmin Le Grand                     break;
85ddde725dSArmin Le Grand                 }
86e2bf1e9dSArmin Le Grand                 default:
87e2bf1e9dSArmin Le Grand                 {
88e2bf1e9dSArmin Le Grand                     break;
89e2bf1e9dSArmin Le Grand                 }
90ddde725dSArmin Le Grand             }
91ddde725dSArmin Le Grand         }
92ddde725dSArmin Le Grand 
93ddde725dSArmin Le Grand         void SvgStyleNode::addCssStyleSheet(const rtl::OUString& aContent)
94ddde725dSArmin Le Grand         {
95ddde725dSArmin Le Grand             const sal_Int32 nLen(aContent.getLength());
96ddde725dSArmin Le Grand 
97ddde725dSArmin Le Grand             if(nLen)
98ddde725dSArmin Le Grand             {
99ddde725dSArmin Le Grand                 sal_Int32 nPos(0);
100ddde725dSArmin Le Grand                 rtl::OUStringBuffer aTokenValue;
101ddde725dSArmin Le Grand 
102311ea6abSArmin Le Grand                 while(nPos < nLen)
103ddde725dSArmin Le Grand                 {
104*9d01bcdeSArmin Le Grand                     // read the full style node names (may be multiple) and put to aStyleName
105311ea6abSArmin Le Grand                     const sal_Int32 nInitPos(nPos);
106*9d01bcdeSArmin Le Grand                     skip_char(aContent, sal_Unicode(' '), nPos, nLen);
107311ea6abSArmin Le Grand                     copyToLimiter(aContent, sal_Unicode('{'), nPos, aTokenValue, nLen);
108*9d01bcdeSArmin Le Grand                     skip_char(aContent, sal_Unicode(' '), sal_Unicode('{'), nPos, nLen);
109ddde725dSArmin Le Grand 
110*9d01bcdeSArmin Le Grand                     const rtl::OUString aStyleName(aTokenValue.makeStringAndClear().trim());
111*9d01bcdeSArmin Le Grand                     const sal_Int32 nLen2(aStyleName.getLength());
112*9d01bcdeSArmin Le Grand                     std::vector< rtl::OUString > aStyleNames;
113*9d01bcdeSArmin Le Grand 
114*9d01bcdeSArmin Le Grand                     if(nLen2)
115*9d01bcdeSArmin Le Grand                     {
116*9d01bcdeSArmin Le Grand                         // extract names
117*9d01bcdeSArmin Le Grand                         sal_Int32 nPos2(0);
118*9d01bcdeSArmin Le Grand                         rtl::OUStringBuffer aSingleName;
119*9d01bcdeSArmin Le Grand 
120*9d01bcdeSArmin Le Grand                         while(nPos2 < nLen2)
121*9d01bcdeSArmin Le Grand                         {
122*9d01bcdeSArmin Le Grand                             skip_char(aStyleName, sal_Unicode('#'), nPos2, nLen2);
123*9d01bcdeSArmin Le Grand                             copyToLimiter(aStyleName, sal_Unicode(' '), nPos2, aSingleName, nLen2);
124*9d01bcdeSArmin Le Grand                             skip_char(aStyleName, sal_Unicode(' '), nPos2, nLen2);
125*9d01bcdeSArmin Le Grand 
126*9d01bcdeSArmin Le Grand                             const rtl::OUString aOUSingleName(aSingleName.makeStringAndClear().trim());
127*9d01bcdeSArmin Le Grand 
128*9d01bcdeSArmin Le Grand                             if(aOUSingleName.getLength())
129*9d01bcdeSArmin Le Grand                             {
130*9d01bcdeSArmin Le Grand                                 aStyleNames.push_back(aOUSingleName);
131*9d01bcdeSArmin Le Grand                             }
132*9d01bcdeSArmin Le Grand                         }
133*9d01bcdeSArmin Le Grand                     }
134*9d01bcdeSArmin Le Grand 
135*9d01bcdeSArmin Le Grand                     if(aStyleNames.size() && nPos < nLen)
136ddde725dSArmin Le Grand                     {
137311ea6abSArmin Le Grand                         copyToLimiter(aContent, sal_Unicode('}'), nPos, aTokenValue, nLen);
138311ea6abSArmin Le Grand                         skip_char(aContent, sal_Unicode(' '), sal_Unicode('}'), nPos, nLen);
139*9d01bcdeSArmin Le Grand                         const rtl::OUString aStyleContent(aTokenValue.makeStringAndClear().trim());
140311ea6abSArmin Le Grand 
141311ea6abSArmin Le Grand                         if(aStyleContent.getLength())
142311ea6abSArmin Le Grand                         {
143311ea6abSArmin Le Grand                             // create new style
144311ea6abSArmin Le Grand                             SvgStyleAttributes* pNewStyle = new SvgStyleAttributes(*this);
145311ea6abSArmin Le Grand                             maSvgStyleAttributes.push_back(pNewStyle);
146ddde725dSArmin Le Grand 
147311ea6abSArmin Le Grand                             // fill with content
148311ea6abSArmin Le Grand                             pNewStyle->readStyle(aStyleContent);
149ddde725dSArmin Le Grand 
150*9d01bcdeSArmin Le Grand                             // concatenate combined style name
151*9d01bcdeSArmin Le Grand                             rtl::OUString aConcatenatedStyleName;
152*9d01bcdeSArmin Le Grand 
153*9d01bcdeSArmin Le Grand                             for(sal_uInt32 a(0); a < aStyleNames.size(); a++)
154*9d01bcdeSArmin Le Grand                             {
155*9d01bcdeSArmin Le Grand                                 aConcatenatedStyleName += aStyleNames[a];
156*9d01bcdeSArmin Le Grand                             }
157*9d01bcdeSArmin Le Grand 
158*9d01bcdeSArmin Le Grand                             // register new style at document for (evtl. concatenated) stylename
159*9d01bcdeSArmin Le Grand                             const_cast< SvgDocument& >(getDocument()).addSvgStyleAttributesToMapper(aConcatenatedStyleName, *pNewStyle);
160311ea6abSArmin Le Grand                         }
161311ea6abSArmin Le Grand                     }
162311ea6abSArmin Le Grand 
163311ea6abSArmin Le Grand                     if(nInitPos == nPos)
164311ea6abSArmin Le Grand                     {
165311ea6abSArmin Le Grand                         OSL_ENSURE(false, "Could not interpret on current position (!)");
166311ea6abSArmin Le Grand                         nPos++;
167ddde725dSArmin Le Grand                     }
168ddde725dSArmin Le Grand                 }
169ddde725dSArmin Le Grand             }
170ddde725dSArmin Le Grand         }
171ddde725dSArmin Le Grand 
172ddde725dSArmin Le Grand     } // end of namespace svgreader
173ddde725dSArmin Le Grand } // end of namespace svgio
174ddde725dSArmin Le Grand 
175ddde725dSArmin Le Grand //////////////////////////////////////////////////////////////////////////////
176ddde725dSArmin Le Grand // eof
177