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
;
54 svgio::svgreader::SvgCharacterNode
* whiteSpaceHandling(svgio::svgreader::SvgNode
const * pNode
, svgio::svgreader::SvgCharacterNode
* pLast
)
58 const auto& rChilds
= pNode
->getChildren();
59 const sal_uInt32
nCount(rChilds
.size());
61 for(sal_uInt32
a(0); a
< nCount
; a
++)
63 svgio::svgreader::SvgNode
* pCandidate
= rChilds
[a
].get();
67 switch(pCandidate
->getType())
69 case svgio::svgreader::SVGTokenCharacter
:
71 // clean whitespace in text span
72 svgio::svgreader::SvgCharacterNode
* pCharNode
= static_cast< svgio::svgreader::SvgCharacterNode
* >(pCandidate
);
73 pCharNode
->whiteSpaceHandling();
75 // pCharNode may have lost all text. If that's the case, ignore
76 // as invalid character node
77 if(!pCharNode
->getText().isEmpty())
82 static bool bNoGapsForBaselineShift(true); // loplugin:constvars:ignore
84 if(bNoGapsForBaselineShift
)
86 // With this option a baseline shift between two char parts ('words')
87 // will not add a space 'gap' to the end of the (non-last) word. This
88 // seems to be the standard behaviour, see last bugdoc attached #122524#
89 const svgio::svgreader::SvgStyleAttributes
* pStyleLast
= pLast
->getSvgStyleAttributes();
90 const svgio::svgreader::SvgStyleAttributes
* pStyleCurrent
= pCandidate
->getSvgStyleAttributes();
92 if(pStyleLast
&& pStyleCurrent
&& pStyleLast
->getBaselineShift() != pStyleCurrent
->getBaselineShift())
98 // add in-between whitespace (single space) to last
99 // known character node
106 // remember new last corrected character node
111 case svgio::svgreader::SVGTokenTspan
:
112 case svgio::svgreader::SVGTokenTextPath
:
113 case svgio::svgreader::SVGTokenTref
:
115 // recursively clean whitespaces in subhierarchy
116 pLast
= whiteSpaceHandling(pCandidate
, pLast
);
121 OSL_ENSURE(false, "Unexpected token inside SVGTokenText (!)");
138 SvgDocHdl::SvgDocHdl(const OUString
& aAbsolutePath
)
139 : maDocument(aAbsolutePath
),
146 SvgDocHdl::~SvgDocHdl()
150 OSL_ENSURE(false, "SvgDocHdl destructed with active target (!)");
152 while (mpTarget
->getParent())
153 mpTarget
= const_cast< SvgNode
* >(mpTarget
->getParent());
155 const SvgNodeVector
& rOwnedTopLevels
= maDocument
.getSvgNodeVector();
156 if (std::none_of(rOwnedTopLevels
.begin(), rOwnedTopLevels
.end(),
157 [&](std::unique_ptr
<SvgNode
> const & p
) { return p
.get() == mpTarget
; }))
160 OSL_ENSURE(maCssContents
.empty(), "SvgDocHdl destructed with active css style stack entry (!)");
163 void SvgDocHdl::startDocument( )
165 OSL_ENSURE(!mpTarget
, "Already a target at document start (!)");
166 OSL_ENSURE(maCssContents
.empty(), "SvgDocHdl startDocument with active css style stack entry (!)");
169 void SvgDocHdl::endDocument( )
171 OSL_ENSURE(!mpTarget
, "Still a target at document end (!)");
172 OSL_ENSURE(maCssContents
.empty(), "SvgDocHdl endDocument with active css style stack entry (!)");
175 void SvgDocHdl::startElement( const OUString
& aName
, const uno::Reference
< xml::sax::XAttributeList
>& xAttribs
)
182 const SVGToken
aSVGToken(StrToSVGToken(aName
, false));
186 /// structural elements
189 /// new basic node for Symbol. Content gets scanned, but
190 /// will not be decomposed (see SvgNode::decomposeSvgNode and bReferenced)
191 mpTarget
= new SvgSymbolNode(maDocument
, mpTarget
);
192 mpTarget
->parseAttributes(xAttribs
);
198 /// new node for Defs/G
199 mpTarget
= new SvgGNode(aSVGToken
, maDocument
, mpTarget
);
200 mpTarget
->parseAttributes(xAttribs
);
206 mpTarget
= new SvgSvgNode(maDocument
, mpTarget
);
207 mpTarget
->parseAttributes(xAttribs
);
213 mpTarget
= new SvgUseNode(maDocument
, mpTarget
);
214 mpTarget
->parseAttributes(xAttribs
);
220 mpTarget
= new SvgANode(maDocument
, mpTarget
);
221 mpTarget
->parseAttributes(xAttribs
);
228 /// new node for Circle
229 mpTarget
= new SvgCircleNode(maDocument
, mpTarget
);
230 mpTarget
->parseAttributes(xAttribs
);
233 case SVGTokenEllipse
:
235 /// new node for Ellipse
236 mpTarget
= new SvgEllipseNode(maDocument
, mpTarget
);
237 mpTarget
->parseAttributes(xAttribs
);
242 /// new node for Line
243 mpTarget
= new SvgLineNode(maDocument
, mpTarget
);
244 mpTarget
->parseAttributes(xAttribs
);
249 /// new node for Path
250 mpTarget
= new SvgPathNode(maDocument
, mpTarget
);
251 mpTarget
->parseAttributes(xAttribs
);
254 case SVGTokenPolygon
:
256 /// new node for Polygon
257 mpTarget
= new SvgPolyNode(maDocument
, mpTarget
, false);
258 mpTarget
->parseAttributes(xAttribs
);
261 case SVGTokenPolyline
:
263 /// new node for Polyline
264 mpTarget
= new SvgPolyNode(maDocument
, mpTarget
, true);
265 mpTarget
->parseAttributes(xAttribs
);
270 /// new node for Rect
271 mpTarget
= new SvgRectNode(maDocument
, mpTarget
);
272 mpTarget
->parseAttributes(xAttribs
);
277 /// new node for Image
278 mpTarget
= new SvgImageNode(maDocument
, mpTarget
);
279 mpTarget
->parseAttributes(xAttribs
);
283 /// title and description
287 /// new node for Title and/or Desc
288 mpTarget
= new SvgTitleDescNode(aSVGToken
, maDocument
, mpTarget
);
293 case SVGTokenLinearGradient
:
294 case SVGTokenRadialGradient
:
296 mpTarget
= new SvgGradientNode(aSVGToken
, maDocument
, mpTarget
);
297 mpTarget
->parseAttributes(xAttribs
);
304 mpTarget
= new SvgGradientStopNode(maDocument
, mpTarget
);
305 mpTarget
->parseAttributes(xAttribs
);
312 mpTarget
= new SvgTextNode(maDocument
, mpTarget
);
313 mpTarget
->parseAttributes(xAttribs
);
318 mpTarget
= new SvgTspanNode(maDocument
, mpTarget
);
319 mpTarget
->parseAttributes(xAttribs
);
324 mpTarget
= new SvgTrefNode(maDocument
, mpTarget
);
325 mpTarget
->parseAttributes(xAttribs
);
328 case SVGTokenTextPath
:
330 mpTarget
= new SvgTextPathNode(maDocument
, mpTarget
);
331 mpTarget
->parseAttributes(xAttribs
);
335 /// styles (as stylesheets)
338 SvgStyleNode
* pNew
= new SvgStyleNode(maDocument
, mpTarget
);
340 const sal_uInt32
nAttributes(xAttribs
->getLength());
344 // #i125326# no attributes, thus also no type="text/css". This is allowed to be missing,
345 // thus do mark this style as CssStyle. This is required to read the contained
346 // text (which defines the css style)
347 pNew
->setTextCss(true);
351 // #i125326# there are attributes, read them. This will set isTextCss to true if
352 // a type="text/css" is contained as exact match, else not
353 mpTarget
->parseAttributes(xAttribs
);
356 if(pNew
->isTextCss())
358 // if it is a Css style, allow reading text between the start and end tag (see
359 // SvgDocHdl::characters for details)
360 maCssContents
.emplace_back();
365 /// structural elements clip-path and mask. Content gets scanned, but
366 /// will not be decomposed (see SvgNode::decomposeSvgNode and bReferenced)
367 case SVGTokenClipPathNode
:
369 /// new node for ClipPath
370 mpTarget
= new SvgClipPathNode(maDocument
, mpTarget
);
371 mpTarget
->parseAttributes(xAttribs
);
376 /// new node for Mask
377 mpTarget
= new SvgMaskNode(maDocument
, mpTarget
);
378 mpTarget
->parseAttributes(xAttribs
);
382 /// structural element marker
385 /// new node for marker
386 mpTarget
= new SvgMarkerNode(maDocument
, mpTarget
);
387 mpTarget
->parseAttributes(xAttribs
);
391 /// structural element pattern
392 case SVGTokenPattern
:
394 /// new node for pattern
395 mpTarget
= new SvgPatternNode(maDocument
, mpTarget
);
396 mpTarget
->parseAttributes(xAttribs
);
400 // ignore FlowRoot and child nodes
401 case SVGTokenFlowRoot
:
409 /// invalid token, ignore
410 SAL_WARN( "svgio", "Unknown Base SvgToken <" + aName
+ "> (!)" );
416 void SvgDocHdl::endElement( const OUString
& aName
)
421 const SVGToken
aSVGToken(StrToSVGToken(aName
, false));
422 SvgNode
* pWhitespaceCheck(SVGTokenText
== aSVGToken
? mpTarget
: nullptr);
423 SvgStyleNode
* pCssStyle(SVGTokenStyle
== aSVGToken
? static_cast< SvgStyleNode
* >(mpTarget
) : nullptr);
424 SvgTitleDescNode
* pSvgTitleDescNode(SVGTokenTitle
== aSVGToken
|| SVGTokenDesc
== aSVGToken
? static_cast< SvgTitleDescNode
* >(mpTarget
) : nullptr);
426 // if we are in skipping mode and we reach the flowRoot end tag: stop skipping mode
427 if(bSkip
&& aSVGToken
== SVGTokenFlowRoot
)
429 // we are in skipping mode: do nothing until we found the flowRoot end tag
435 /// valid tokens for which a new one was created
437 /// structural elements
447 case SVGTokenEllipse
:
450 case SVGTokenPolygon
:
451 case SVGTokenPolyline
:
455 /// title and description
460 case SVGTokenLinearGradient
:
461 case SVGTokenRadialGradient
:
469 case SVGTokenTextPath
:
472 /// styles (as stylesheets)
475 /// structural elements clip-path and mask
476 case SVGTokenClipPathNode
:
479 /// structural element marker
482 /// structural element pattern
483 case SVGTokenPattern
:
485 /// content handling after parsing
489 if(!mpTarget
->getParent())
491 // last element closing, save this tree
492 maDocument
.appendNode(std::unique_ptr
<SvgNode
>(mpTarget
));
495 mpTarget
= const_cast< SvgNode
* >(mpTarget
->getParent());
499 OSL_ENSURE(false, "Closing token, but no context (!)");
505 /// invalid token, ignore
509 if(pSvgTitleDescNode
&& mpTarget
)
511 const OUString
& aText(pSvgTitleDescNode
->getText());
515 if(SVGTokenTitle
== aSVGToken
)
517 mpTarget
->parseAttribute(getStrTitle(), aSVGToken
, aText
);
519 else // if(SVGTokenDesc == aSVGToken)
521 mpTarget
->parseAttribute(getStrDesc(), aSVGToken
, aText
);
526 if(pCssStyle
&& pCssStyle
->isTextCss())
529 if(!maCssContents
.empty())
531 // need to interpret css styles and remember them as StyleSheets
532 // #125325# Caution! the Css content may contain block comments
533 // (see http://www.w3.org/wiki/CSS_basics#CSS_comments). These need
534 // to be removed first
535 const OUString
aCommentFreeSource(removeBlockComments(*(maCssContents
.end() - 1)));
537 if(aCommentFreeSource
.getLength())
539 pCssStyle
->addCssStyleSheet(aCommentFreeSource
);
542 maCssContents
.pop_back();
546 OSL_ENSURE(false, "Closing CssStyle, but no collector string on stack (!)");
552 // cleanup read strings
553 whiteSpaceHandling(pWhitespaceCheck
, nullptr);
557 void SvgDocHdl::characters( const OUString
& aChars
)
559 const sal_uInt32
nLength(aChars
.getLength());
561 if(!(mpTarget
&& nLength
))
564 switch(mpTarget
->getType())
568 case SVGTokenTextPath
:
570 const auto& rChilds
= mpTarget
->getChildren();
571 SvgCharacterNode
* pTarget
= nullptr;
575 pTarget
= dynamic_cast< SvgCharacterNode
* >(rChilds
[rChilds
.size() - 1].get());
580 // concatenate to current character span
581 pTarget
->concatenate(aChars
);
585 // add character span as simplified tspan (no arguments)
586 // as direct child of SvgTextNode/SvgTspanNode/SvgTextPathNode
587 new SvgCharacterNode(maDocument
, mpTarget
, aChars
);
593 SvgStyleNode
& rSvgStyleNode
= static_cast< SvgStyleNode
& >(*mpTarget
);
595 if(rSvgStyleNode
.isTextCss())
597 // collect characters for css style
598 if(!maCssContents
.empty())
600 const OUString
aTrimmedChars(aChars
.trim());
602 if(!aTrimmedChars
.isEmpty())
604 std::vector
< OUString
>::iterator
aString(maCssContents
.end() - 1);
605 (*aString
) += aTrimmedChars
;
610 OSL_ENSURE(false, "Closing CssStyle, but no collector string on stack (!)");
618 SvgTitleDescNode
& rSvgTitleDescNode
= static_cast< SvgTitleDescNode
& >(*mpTarget
);
620 // add text directly to SvgTitleDescNode
621 rSvgTitleDescNode
.concatenate(aChars
);
626 // characters not used by a known node
632 void SvgDocHdl::ignorableWhitespace(const OUString
& /*aWhitespaces*/)
636 void SvgDocHdl::processingInstruction(const OUString
& /*aTarget*/, const OUString
& /*aData*/)
640 void SvgDocHdl::setDocumentLocator(const uno::Reference
< xml::sax::XLocator
>& /*xLocator*/)
643 } // end of namespace svgreader
644 } // end of namespace svgio
646 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */