1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 <svgdocumenthandler.hxx>
21 #include <svgtoken.hxx>
22 #include <svgsvgnode.hxx>
23 #include <svggnode.hxx>
24 #include <svganode.hxx>
25 #include <svgnode.hxx>
26 #include <svgpathnode.hxx>
27 #include <svgrectnode.hxx>
28 #include <svggradientnode.hxx>
29 #include <svggradientstopnode.hxx>
30 #include <svgsymbolnode.hxx>
31 #include <svgusenode.hxx>
32 #include <svgcirclenode.hxx>
33 #include <svgellipsenode.hxx>
34 #include <svglinenode.hxx>
35 #include <svgpolynode.hxx>
36 #include <svgtextnode.hxx>
37 #include <svgcharacternode.hxx>
38 #include <svgtspannode.hxx>
39 #include <svgtrefnode.hxx>
40 #include <svgtextpathnode.hxx>
41 #include <svgstylenode.hxx>
42 #include <svgimagenode.hxx>
43 #include <svgclippathnode.hxx>
44 #include <svgmasknode.hxx>
45 #include <svgmarkernode.hxx>
46 #include <svgpatternnode.hxx>
47 #include <svgtitledescnode.hxx>
48 #include <sal/log.hxx>
50 using namespace com::sun::star
;
52 namespace svgio::svgreader
57 svgio::svgreader::SvgCharacterNode
* whiteSpaceHandling(svgio::svgreader::SvgNode
const * pNode
, svgio::svgreader::SvgCharacterNode
* pLast
)
61 const auto& rChilds
= pNode
->getChildren();
62 const sal_uInt32
nCount(rChilds
.size());
64 for(sal_uInt32
a(0); a
< nCount
; a
++)
66 svgio::svgreader::SvgNode
* pCandidate
= rChilds
[a
].get();
70 switch(pCandidate
->getType())
72 case SVGToken::Character
:
74 // clean whitespace in text span
75 svgio::svgreader::SvgCharacterNode
* pCharNode
= static_cast< svgio::svgreader::SvgCharacterNode
* >(pCandidate
);
76 pCharNode
->whiteSpaceHandling();
78 // pCharNode may have lost all text. If that's the case, ignore
79 // as invalid character node
80 if(!pCharNode
->getText().isEmpty())
85 static bool bNoGapsForBaselineShift(true); // loplugin:constvars:ignore
87 if(bNoGapsForBaselineShift
)
89 // With this option a baseline shift between two char parts ('words')
90 // will not add a space 'gap' to the end of the (non-last) word. This
91 // seems to be the standard behaviour, see last bugdoc attached #122524#
92 const svgio::svgreader::SvgStyleAttributes
* pStyleLast
= pLast
->getSvgStyleAttributes();
93 const svgio::svgreader::SvgStyleAttributes
* pStyleCurrent
= pCandidate
->getSvgStyleAttributes();
95 if(pStyleLast
&& pStyleCurrent
&& pStyleLast
->getBaselineShift() != pStyleCurrent
->getBaselineShift())
101 // add in-between whitespace (single space) to last
102 // known character node
109 // remember new last corrected character node
114 case SVGToken::Tspan
:
115 case SVGToken::TextPath
:
118 // recursively clean whitespaces in subhierarchy
119 pLast
= whiteSpaceHandling(pCandidate
, pLast
);
124 OSL_ENSURE(false, "Unexpected token inside SVGTokenText (!)");
134 } // end anonymous namespace
136 SvgDocHdl::SvgDocHdl(const OUString
& aAbsolutePath
)
137 : maDocument(aAbsolutePath
),
144 SvgDocHdl::~SvgDocHdl()
148 OSL_ENSURE(false, "SvgDocHdl destructed with active target (!)");
150 while (mpTarget
->getParent())
151 mpTarget
= const_cast< SvgNode
* >(mpTarget
->getParent());
153 const SvgNodeVector
& rOwnedTopLevels
= maDocument
.getSvgNodeVector();
154 if (std::none_of(rOwnedTopLevels
.begin(), rOwnedTopLevels
.end(),
155 [&](std::unique_ptr
<SvgNode
> const & p
) { return p
.get() == mpTarget
; }))
158 OSL_ENSURE(maCssContents
.empty(), "SvgDocHdl destructed with active css style stack entry (!)");
161 void SvgDocHdl::startDocument( )
163 OSL_ENSURE(!mpTarget
, "Already a target at document start (!)");
164 OSL_ENSURE(maCssContents
.empty(), "SvgDocHdl startDocument with active css style stack entry (!)");
167 void SvgDocHdl::endDocument( )
169 OSL_ENSURE(!mpTarget
, "Still a target at document end (!)");
170 OSL_ENSURE(maCssContents
.empty(), "SvgDocHdl endDocument with active css style stack entry (!)");
173 void SvgDocHdl::startElement( const OUString
& aName
, const uno::Reference
< xml::sax::XAttributeList
>& xAttribs
)
180 const SVGToken
aSVGToken(StrToSVGToken(aName
, false));
184 /// structural elements
185 case SVGToken::Symbol
:
187 /// new basic node for Symbol. Content gets scanned, but
188 /// will not be decomposed (see SvgNode::decomposeSvgNode and bReferenced)
189 mpTarget
= new SvgSymbolNode(maDocument
, mpTarget
);
190 mpTarget
->parseAttributes(xAttribs
);
196 /// new node for Defs/G
197 mpTarget
= new SvgGNode(aSVGToken
, maDocument
, mpTarget
);
198 mpTarget
->parseAttributes(xAttribs
);
204 mpTarget
= new SvgSvgNode(maDocument
, mpTarget
);
205 mpTarget
->parseAttributes(xAttribs
);
211 mpTarget
= new SvgUseNode(maDocument
, mpTarget
);
212 mpTarget
->parseAttributes(xAttribs
);
218 mpTarget
= new SvgANode(maDocument
, mpTarget
);
219 mpTarget
->parseAttributes(xAttribs
);
224 case SVGToken::Circle
:
226 /// new node for Circle
227 mpTarget
= new SvgCircleNode(maDocument
, mpTarget
);
228 mpTarget
->parseAttributes(xAttribs
);
231 case SVGToken::Ellipse
:
233 /// new node for Ellipse
234 mpTarget
= new SvgEllipseNode(maDocument
, mpTarget
);
235 mpTarget
->parseAttributes(xAttribs
);
240 /// new node for Line
241 mpTarget
= new SvgLineNode(maDocument
, mpTarget
);
242 mpTarget
->parseAttributes(xAttribs
);
247 /// new node for Path
248 mpTarget
= new SvgPathNode(maDocument
, mpTarget
);
249 mpTarget
->parseAttributes(xAttribs
);
252 case SVGToken::Polygon
:
254 /// new node for Polygon
255 mpTarget
= new SvgPolyNode(maDocument
, mpTarget
, false);
256 mpTarget
->parseAttributes(xAttribs
);
259 case SVGToken::Polyline
:
261 /// new node for Polyline
262 mpTarget
= new SvgPolyNode(maDocument
, mpTarget
, true);
263 mpTarget
->parseAttributes(xAttribs
);
268 /// new node for Rect
269 mpTarget
= new SvgRectNode(maDocument
, mpTarget
);
270 mpTarget
->parseAttributes(xAttribs
);
273 case SVGToken::Image
:
275 /// new node for Image
276 mpTarget
= new SvgImageNode(maDocument
, mpTarget
);
277 mpTarget
->parseAttributes(xAttribs
);
281 /// title and description
282 case SVGToken::Title
:
285 /// new node for Title and/or Desc
286 mpTarget
= new SvgTitleDescNode(aSVGToken
, maDocument
, mpTarget
);
291 case SVGToken::LinearGradient
:
292 case SVGToken::RadialGradient
:
294 mpTarget
= new SvgGradientNode(aSVGToken
, maDocument
, mpTarget
);
295 mpTarget
->parseAttributes(xAttribs
);
302 mpTarget
= new SvgGradientStopNode(maDocument
, mpTarget
);
303 mpTarget
->parseAttributes(xAttribs
);
310 mpTarget
= new SvgTextNode(maDocument
, mpTarget
);
311 mpTarget
->parseAttributes(xAttribs
);
314 case SVGToken::Tspan
:
316 mpTarget
= new SvgTspanNode(maDocument
, mpTarget
);
317 mpTarget
->parseAttributes(xAttribs
);
322 mpTarget
= new SvgTrefNode(maDocument
, mpTarget
);
323 mpTarget
->parseAttributes(xAttribs
);
326 case SVGToken::TextPath
:
328 mpTarget
= new SvgTextPathNode(maDocument
, mpTarget
);
329 mpTarget
->parseAttributes(xAttribs
);
333 /// styles (as stylesheets)
334 case SVGToken::Style
:
336 SvgStyleNode
* pNew
= new SvgStyleNode(maDocument
, mpTarget
);
338 const sal_uInt32
nAttributes(xAttribs
->getLength());
342 // #i125326# no attributes, thus also no type="text/css". This is allowed to be missing,
343 // thus do mark this style as CssStyle. This is required to read the contained
344 // text (which defines the css style)
345 pNew
->setTextCss(true);
349 // #i125326# there are attributes, read them. This will set isTextCss to true if
350 // a type="text/css" is contained as exact match, else not
351 mpTarget
->parseAttributes(xAttribs
);
354 if(pNew
->isTextCss())
356 // if it is a Css style, allow reading text between the start and end tag (see
357 // SvgDocHdl::characters for details)
358 maCssContents
.emplace_back();
363 /// structural elements clip-path and mask. Content gets scanned, but
364 /// will not be decomposed (see SvgNode::decomposeSvgNode and bReferenced)
365 case SVGToken::ClipPathNode
:
367 /// new node for ClipPath
368 mpTarget
= new SvgClipPathNode(maDocument
, mpTarget
);
369 mpTarget
->parseAttributes(xAttribs
);
374 /// new node for Mask
375 mpTarget
= new SvgMaskNode(maDocument
, mpTarget
);
376 mpTarget
->parseAttributes(xAttribs
);
380 /// structural element marker
381 case SVGToken::Marker
:
383 /// new node for marker
384 mpTarget
= new SvgMarkerNode(maDocument
, mpTarget
);
385 mpTarget
->parseAttributes(xAttribs
);
389 /// structural element pattern
390 case SVGToken::Pattern
:
392 /// new node for pattern
393 mpTarget
= new SvgPatternNode(maDocument
, mpTarget
);
394 mpTarget
->parseAttributes(xAttribs
);
398 // ignore FlowRoot and child nodes
399 case SVGToken::FlowRoot
:
407 /// invalid token, ignore
408 SAL_INFO( "svgio", "Unknown Base SvgToken <" + aName
+ "> (!)" );
414 void SvgDocHdl::endElement( const OUString
& aName
)
419 const SVGToken
aSVGToken(StrToSVGToken(aName
, false));
420 SvgNode
* pWhitespaceCheck(SVGToken::Text
== aSVGToken
? mpTarget
: nullptr);
421 SvgStyleNode
* pCssStyle(SVGToken::Style
== aSVGToken
? static_cast< SvgStyleNode
* >(mpTarget
) : nullptr);
422 SvgTitleDescNode
* pSvgTitleDescNode(SVGToken::Title
== aSVGToken
|| SVGToken::Desc
== aSVGToken
? static_cast< SvgTitleDescNode
* >(mpTarget
) : nullptr);
424 // if we are in skipping mode and we reach the flowRoot end tag: stop skipping mode
425 if(bSkip
&& aSVGToken
== SVGToken::FlowRoot
)
427 // we are in skipping mode: do nothing until we found the flowRoot end tag
433 /// valid tokens for which a new one was created
435 /// structural elements
439 case SVGToken::Symbol
:
444 case SVGToken::Circle
:
445 case SVGToken::Ellipse
:
448 case SVGToken::Polygon
:
449 case SVGToken::Polyline
:
451 case SVGToken::Image
:
453 /// title and description
454 case SVGToken::Title
:
458 case SVGToken::LinearGradient
:
459 case SVGToken::RadialGradient
:
466 case SVGToken::Tspan
:
467 case SVGToken::TextPath
:
470 /// styles (as stylesheets)
471 case SVGToken::Style
:
473 /// structural elements clip-path and mask
474 case SVGToken::ClipPathNode
:
477 /// structural element marker
478 case SVGToken::Marker
:
480 /// structural element pattern
481 case SVGToken::Pattern
:
483 /// content handling after parsing
487 if(!mpTarget
->getParent())
489 // last element closing, save this tree
490 maDocument
.appendNode(std::unique_ptr
<SvgNode
>(mpTarget
));
493 mpTarget
= const_cast< SvgNode
* >(mpTarget
->getParent());
497 OSL_ENSURE(false, "Closing token, but no context (!)");
503 /// invalid token, ignore
507 if(pSvgTitleDescNode
&& mpTarget
)
509 const OUString
& aText(pSvgTitleDescNode
->getText());
513 if(SVGToken::Title
== aSVGToken
)
515 mpTarget
->parseAttribute(getStrTitle(), aSVGToken
, aText
);
517 else // if(SVGTokenDesc == aSVGToken)
519 mpTarget
->parseAttribute(getStrDesc(), aSVGToken
, aText
);
524 if(pCssStyle
&& pCssStyle
->isTextCss())
527 if(!maCssContents
.empty())
529 // need to interpret css styles and remember them as StyleSheets
530 // #125325# Caution! the Css content may contain block comments
531 // (see http://www.w3.org/wiki/CSS_basics#CSS_comments). These need
532 // to be removed first
533 const OUString
aCommentFreeSource(removeBlockComments(*(maCssContents
.end() - 1)));
535 if(aCommentFreeSource
.getLength())
537 pCssStyle
->addCssStyleSheet(aCommentFreeSource
);
540 maCssContents
.pop_back();
544 OSL_ENSURE(false, "Closing CssStyle, but no collector string on stack (!)");
550 // cleanup read strings
551 whiteSpaceHandling(pWhitespaceCheck
, nullptr);
555 void SvgDocHdl::characters( const OUString
& aChars
)
557 const sal_uInt32
nLength(aChars
.getLength());
559 if(!(mpTarget
&& nLength
))
562 switch(mpTarget
->getType())
565 case SVGToken::Tspan
:
566 case SVGToken::TextPath
:
568 const auto& rChilds
= mpTarget
->getChildren();
569 SvgCharacterNode
* pTarget
= nullptr;
573 pTarget
= dynamic_cast< SvgCharacterNode
* >(rChilds
[rChilds
.size() - 1].get());
578 // concatenate to current character span
579 pTarget
->concatenate(aChars
);
583 // add character span as simplified tspan (no arguments)
584 // as direct child of SvgTextNode/SvgTspanNode/SvgTextPathNode
585 new SvgCharacterNode(maDocument
, mpTarget
, aChars
);
589 case SVGToken::Style
:
591 SvgStyleNode
& rSvgStyleNode
= static_cast< SvgStyleNode
& >(*mpTarget
);
593 if(rSvgStyleNode
.isTextCss())
595 // collect characters for css style
596 if(!maCssContents
.empty())
598 const OUString
aTrimmedChars(aChars
.trim());
600 if(!aTrimmedChars
.isEmpty())
602 std::vector
< OUString
>::iterator
aString(maCssContents
.end() - 1);
603 (*aString
) += aTrimmedChars
;
608 OSL_ENSURE(false, "Closing CssStyle, but no collector string on stack (!)");
613 case SVGToken::Title
:
616 SvgTitleDescNode
& rSvgTitleDescNode
= static_cast< SvgTitleDescNode
& >(*mpTarget
);
618 // add text directly to SvgTitleDescNode
619 rSvgTitleDescNode
.concatenate(aChars
);
624 // characters not used by a known node
630 void SvgDocHdl::ignorableWhitespace(const OUString
& /*aWhitespaces*/)
634 void SvgDocHdl::processingInstruction(const OUString
& /*aTarget*/, const OUString
& /*aData*/)
638 void SvgDocHdl::setDocumentLocator(const uno::Reference
< xml::sax::XLocator
>& /*xLocator*/)
641 } // end of namespace svgio
643 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */