lok: vcl: fix multiple floatwin removal case more robustly.
[LibreOffice.git] / svgio / source / svgreader / svgnode.cxx
blob0c40f16437166ce72ecffa86b1f12c825090b6ec
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 <basegfx/polygon/b2dpolypolygontools.hxx>
21 #include <svgdocument.hxx>
22 #include <svgnode.hxx>
23 #include <svgstyleattributes.hxx>
24 #include <drawinglayer/primitive2d/objectinfoprimitive2d.hxx>
25 #include <tools/urlobj.hxx>
28 namespace svgio
30 namespace svgreader
32 /// #i125258#
33 bool SvgNode::supportsParentStyle() const
35 return true;
38 const SvgStyleAttributes* SvgNode::getSvgStyleAttributes() const
40 return nullptr;
43 void SvgNode::fillCssStyleVectorUsingHierarchyAndSelectors(
44 const OUString& rClassStr,
45 const SvgNode& rCurrent,
46 const OUString& aConcatenated)
48 const SvgDocument& rDocument = getDocument();
50 if(rDocument.hasGlobalCssStyleAttributes())
52 const SvgNode* pParent = rCurrent.getParent();
54 // check for ID (highest priority)
55 if(rCurrent.getId())
57 const OUString& rId = *rCurrent.getId();
59 if(rId.getLength())
61 const OUString aNewConcatenated(
62 "#" + rId + aConcatenated);
64 if(pParent)
66 // check for combined selectors at parent firstso that higher specificity will be in front
67 fillCssStyleVectorUsingHierarchyAndSelectors(rClassStr, *pParent, aNewConcatenated);
70 const SvgStyleAttributes* pNew = rDocument.findGlobalCssStyleAttributes(aNewConcatenated);
72 if(pNew)
74 // add CssStyle if found
75 maCssStyleVector.push_back(pNew);
80 // check for 'class' references (a list of entries is allowed)
81 if(rCurrent.getClass())
83 const OUString& rClassList = *rCurrent.getClass();
84 const sal_Int32 nLen(rClassList.getLength());
86 if(nLen)
88 std::vector< OUString > aParts;
89 sal_Int32 nPos(0);
90 OUStringBuffer aToken;
92 while(nPos < nLen)
94 const sal_Int32 nInitPos(nPos);
95 copyToLimiter(rClassList, u' ', nPos, aToken, nLen);
96 skip_char(rClassList, u' ', nPos, nLen);
97 const OUString aPart(aToken.makeStringAndClear().trim());
99 if(aPart.getLength())
101 aParts.push_back(aPart);
104 if(nInitPos == nPos)
106 OSL_ENSURE(false, "Could not interpret on current position (!)");
107 nPos++;
111 for(size_t a(0); a < aParts.size(); a++)
113 const OUString aNewConcatenated(
114 "." + aParts[a] + aConcatenated);
116 if(pParent)
118 // check for combined selectors at parent firstso that higher specificity will be in front
119 fillCssStyleVectorUsingHierarchyAndSelectors(rClassStr, *pParent, aNewConcatenated);
122 const SvgStyleAttributes* pNew = rDocument.findGlobalCssStyleAttributes(aNewConcatenated);
124 if(pNew)
126 // add CssStyle if found
127 maCssStyleVector.push_back(pNew);
133 // check for class-dependent references to CssStyles
134 if(!rClassStr.isEmpty())
136 OUString aNewConcatenated(aConcatenated);
138 if(!rCurrent.getId() && !rCurrent.getClass() && 0 == aConcatenated.indexOf(rClassStr))
140 // no new CssStyle Selector and already starts with rClassStr, do not concatenate;
141 // we pass an 'empty' node (in the sense of CssStyle Selector)
143 else
145 aNewConcatenated = rClassStr + aConcatenated;
148 if(pParent)
150 // check for combined selectors at parent firstso that higher specificity will be in front
151 fillCssStyleVectorUsingHierarchyAndSelectors(rClassStr, *pParent, aNewConcatenated);
154 const SvgStyleAttributes* pNew = rDocument.findGlobalCssStyleAttributes(aNewConcatenated);
156 if(pNew)
158 // add CssStyle if found
159 maCssStyleVector.push_back(pNew);
165 void SvgNode::fillCssStyleVector(const OUString& rClassStr, const SvgStyleAttributes& rOriginal)
167 OSL_ENSURE(!mbCssStyleVectorBuilt, "OOps, fillCssStyleVector called double ?!?");
168 mbCssStyleVectorBuilt = true;
170 // #i125293# If we have CssStyles we need to build a linked list of SvgStyleAttributes
171 // which represent this for the current object. There are various methods to
172 // specify CssStyles which need to be taken into account in a given order:
173 // - local CssStyle (independent from global CssStyles at SvgDocument)
174 // - 'id' CssStyle
175 // - 'class' CssStyle(s)
176 // - type-dependent elements (e..g. 'rect' for all rect elements)
177 // - Css selector '*'
178 // - local attributes (rOriginal)
179 // - inherited attributes (up the hierarchy)
180 // The first four will be collected in maCssStyleVector for the current element
181 // (once, this will not change) and be linked in the needed order using the
182 // get/setCssStyleParent at the SvgStyleAttributes which will be used preferred in
183 // member evaluation over the existing parent hierarchy
185 // check for local CssStyle with highest priority
186 if(mpLocalCssStyle)
188 // if we have one, use as first entry
189 maCssStyleVector.push_back(mpLocalCssStyle.get());
192 // check the hierarchy for concatenated patterns of Selectors
193 fillCssStyleVectorUsingHierarchyAndSelectors(rClassStr, *this, OUString());
195 // tdf#99115, Add css selector '*' style only if the element is on top of the hierarchy
196 // meaning its parent is <svg>
197 const SvgNode* pParent = this->getParent();
199 if(pParent && pParent->getType() == SVGTokenSvg)
201 // #i125329# find Css selector '*', add as last element if found
202 const SvgStyleAttributes* pNew = getDocument().findGlobalCssStyleAttributes("*");
204 if(pNew)
206 // add CssStyle for selector '*' if found
207 maCssStyleVector.push_back(pNew);
211 //local attributes
212 maCssStyleVector.push_back(&rOriginal);
215 const SvgStyleAttributes* SvgNode::checkForCssStyle(const OUString& rClassStr, const SvgStyleAttributes& rOriginal) const
217 if(!mbCssStyleVectorBuilt)
219 // build needed CssStyleVector for local node
220 const_cast< SvgNode* >(this)->fillCssStyleVector(rClassStr, rOriginal);
223 if(maCssStyleVector.empty())
225 // return given original if no CssStyles found
226 return &rOriginal;
228 else
230 // #i125293# rOriginal will be the last element in the linked list; use no CssStyleParent
231 // there (reset it) to ensure that the parent hierarchy will be used when it's base
232 // is referenced. This new chaining inserts the CssStyles before the original style,
233 // this makes the whole process much safer since the original style when used will
234 // be not different to the situation without CssStyles; thus loops which may be caused
235 // by trying to use the parent hierarchy of the owner of the style will be avoided
236 // already in this mechanism. It's still good to keep the supportsParentStyle
237 // from #i125258# in place, though.
238 // This chain building using pointers will be done every time when checkForCssStyle
239 // is used (not the search, only the chaining). This is needed since the CssStyles
240 // themselves will be potentially used multiple times. It is not expensive since it's
241 // only changing some pointers.
242 // The alternative would be to create the style hierarchy for every element (or even
243 // for the element containing the hierarchy) in a vector of pointers and to use that.
244 // Resetting the CssStyleParent on rOriginal is probably not needed
245 // but simply safer to do.
247 // loop over the existing CssStyles and link them. There is a first one, take
248 // as current
249 SvgStyleAttributes* pCurrent = const_cast< SvgStyleAttributes* >(maCssStyleVector[0]);
251 for(size_t a(1); a < maCssStyleVector.size(); a++)
253 SvgStyleAttributes* pNext = const_cast< SvgStyleAttributes* >(maCssStyleVector[a]);
255 pCurrent->setCssStyleParent(pNext);
256 pCurrent = pNext;
259 // return 1st CssStyle as style chain start element (only for the
260 // local element, still no hierarchy used here)
261 return maCssStyleVector[0];
265 SvgNode::SvgNode(
266 SVGToken aType,
267 SvgDocument& rDocument,
268 SvgNode* pParent)
269 : maType(aType),
270 mrDocument(rDocument),
271 mpParent(pParent),
272 mpAlternativeParent(nullptr),
273 maChildren(),
274 maXmlSpace(XmlSpace_notset),
275 maDisplay(Display_inline),
276 maCssStyleVector(),
277 mbDecomposing(false),
278 mbCssStyleVectorBuilt(false)
280 OSL_ENSURE(SVGTokenUnknown != maType, "SvgNode with unknown type created (!)");
282 if(pParent)
284 pParent->maChildren.emplace_back(this);
286 else
288 #ifdef DBG_UTIL
289 if(SVGTokenSvg != getType())
291 OSL_ENSURE(false, "No parent for this node (!)");
293 #endif
297 SvgNode::~SvgNode()
301 void SvgNode::readLocalCssStyle(const OUString& aContent)
303 if(!mpLocalCssStyle)
305 // create LocalCssStyle if needed but not yet added
306 mpLocalCssStyle.reset(new SvgStyleAttributes(*this));
308 else
310 // 2nd fill would be an error
311 OSL_ENSURE(false, "Svg node has two local CssStyles, this may lead to problems (!)");
314 if(mpLocalCssStyle)
316 // parse and set values to it
317 mpLocalCssStyle->readCssStyle(aContent);
319 else
321 OSL_ENSURE(false, "Could not get/create a local CssStyle for a node (!)");
325 void SvgNode::parseAttributes(const css::uno::Reference< css::xml::sax::XAttributeList >& xAttribs)
327 // no longer need to pre-sort moving 'style' entries to the back so that
328 // values get overwritten - that was the previous, not complete solution for
329 // handling the priorities between svg and Css properties
330 const sal_uInt32 nAttributes(xAttribs->getLength());
332 for(sal_uInt32 a(0); a < nAttributes; a++)
334 const OUString aTokenName(xAttribs->getNameByIndex(a));
335 const SVGToken aSVGToken(StrToSVGToken(aTokenName, false));
337 parseAttribute(aTokenName, aSVGToken, xAttribs->getValueByIndex(a));
341 Display getDisplayFromContent(const OUString& aContent)
343 if(!aContent.isEmpty())
345 if(aContent.startsWith("inline"))
347 return Display_inline;
349 else if(aContent.startsWith("none"))
351 return Display_none;
353 else if(aContent.startsWith("inherit"))
355 return Display_inherit;
357 else if(aContent.startsWith("block"))
359 return Display_block;
361 else if(aContent.startsWith("list-item"))
363 return Display_list_item;
365 else if(aContent.startsWith("run-in"))
367 return Display_run_in;
369 else if(aContent.startsWith("compact"))
371 return Display_compact;
373 else if(aContent.startsWith("marker"))
375 return Display_marker;
377 else if(aContent.startsWith("table"))
379 return Display_table;
381 else if(aContent.startsWith("inline-table"))
383 return Display_inline_table;
385 else if(aContent.startsWith("table-row-group"))
387 return Display_table_row_group;
389 else if(aContent.startsWith("table-header-group"))
391 return Display_table_header_group;
393 else if(aContent.startsWith("table-footer-group"))
395 return Display_table_footer_group;
397 else if(aContent.startsWith("table-row"))
399 return Display_table_row;
401 else if(aContent.startsWith("table-column-group"))
403 return Display_table_column_group;
405 else if(aContent.startsWith("table-column"))
407 return Display_table_column;
409 else if(aContent.startsWith("table-cell"))
411 return Display_table_cell;
413 else if(aContent.startsWith("table-caption"))
415 return Display_table_caption;
419 // return the default
420 return Display_inline;
423 void SvgNode::parseAttribute(const OUString& /*rTokenName*/, SVGToken aSVGToken, const OUString& aContent)
425 switch(aSVGToken)
427 case SVGTokenId:
429 if(!aContent.isEmpty())
431 setId(aContent);
433 break;
435 case SVGTokenClass:
437 if(!aContent.isEmpty())
439 setClass(aContent);
441 break;
443 case SVGTokenXmlSpace:
445 if(!aContent.isEmpty())
447 if(aContent.startsWith("default"))
449 setXmlSpace(XmlSpace_default);
451 else if(aContent.startsWith("preserve"))
453 setXmlSpace(XmlSpace_preserve);
456 break;
458 case SVGTokenDisplay:
460 if(!aContent.isEmpty())
462 setDisplay(getDisplayFromContent(aContent));
464 break;
466 default:
468 break;
473 void SvgNode::decomposeSvgNode(drawinglayer::primitive2d::Primitive2DContainer& rTarget, bool bReferenced) const
475 if (mbDecomposing) //guard against infinite recurse
476 return;
478 if(Display_none == getDisplay())
480 return;
483 if(!bReferenced)
485 if(SVGTokenDefs == getType() ||
486 SVGTokenSymbol == getType() ||
487 SVGTokenClipPathNode == getType() ||
488 SVGTokenMask == getType() ||
489 SVGTokenMarker == getType() ||
490 SVGTokenPattern == getType())
492 // do not decompose defs or symbol nodes (these hold only style-like
493 // objects which may be used by referencing them) except when doing
494 // so controlled referenced
496 // also do not decompose ClipPaths and Masks. These should be embedded
497 // in a defs node (which gets not decomposed by itself), but you never
498 // know
500 // also not directly used are Markers and Patterns, only indirectly used
501 // by reference
503 // #i121656# also do not decompose nodes which have display="none" set
504 // as property
505 return;
509 const auto& rChildren = getChildren();
511 if(!rChildren.empty())
513 mbDecomposing = true;
515 const sal_uInt32 nCount(rChildren.size());
517 for(sal_uInt32 a(0); a < nCount; a++)
519 SvgNode* pCandidate = rChildren[a].get();
521 if(pCandidate && Display_none != pCandidate->getDisplay())
523 const auto& rGrandChildren = pCandidate->getChildren();
524 const SvgStyleAttributes* pChildStyles = pCandidate->getSvgStyleAttributes();
525 // decompose:
526 // - visible terminal nodes
527 // - all non-terminal nodes (might contain visible nodes down the hierarchy)
528 if( !rGrandChildren.empty() || ( pChildStyles && (Visibility_visible == pChildStyles->getVisibility())) )
530 drawinglayer::primitive2d::Primitive2DContainer aNewTarget;
531 pCandidate->decomposeSvgNode(aNewTarget, bReferenced);
533 if(!aNewTarget.empty())
535 rTarget.append(aNewTarget);
539 else if(!pCandidate)
541 OSL_ENSURE(false, "Null-Pointer in child node list (!)");
545 if(!rTarget.empty())
547 const SvgStyleAttributes* pStyles = getSvgStyleAttributes();
548 if(pStyles)
550 // check if we have Title or Desc
551 const OUString& rTitle = pStyles->getTitle();
552 const OUString& rDesc = pStyles->getDesc();
554 if(!rTitle.isEmpty() || !rDesc.isEmpty())
556 // default object name is empty
557 OUString aObjectName;
559 // use path as object name when outmost element
560 if(SVGTokenSvg == getType())
562 aObjectName = getDocument().getAbsolutePath();
564 if(!aObjectName.isEmpty())
566 INetURLObject aURL(aObjectName);
568 aObjectName = aURL.getName(
569 INetURLObject::LAST_SEGMENT,
570 true,
571 INetURLObject::DecodeMechanism::WithCharset);
575 // pack in ObjectInfoPrimitive2D group
576 const drawinglayer::primitive2d::Primitive2DReference xRef(
577 new drawinglayer::primitive2d::ObjectInfoPrimitive2D(
578 rTarget,
579 aObjectName,
580 rTitle,
581 rDesc));
583 rTarget = drawinglayer::primitive2d::Primitive2DContainer { xRef };
587 mbDecomposing = false;
591 const basegfx::B2DRange SvgNode::getCurrentViewPort() const
593 if(getParent())
595 return getParent()->getCurrentViewPort();
597 else
599 return basegfx::B2DRange(); // return empty B2DRange
603 double SvgNode::getCurrentFontSizeInherited() const
605 if(getParent())
607 return getParent()->getCurrentFontSize();
609 else
611 return 0.0;
615 double SvgNode::getCurrentFontSize() const
617 if(getSvgStyleAttributes())
618 return getSvgStyleAttributes()->getFontSizeNumber().solve(*this, xcoordinate);
620 return getCurrentFontSizeInherited();
623 double SvgNode::getCurrentXHeightInherited() const
625 if(getParent())
627 return getParent()->getCurrentXHeight();
629 else
631 return 0.0;
635 double SvgNode::getCurrentXHeight() const
637 if(getSvgStyleAttributes())
638 // for XHeight, use FontSize currently
639 return getSvgStyleAttributes()->getFontSizeNumber().solve(*this, ycoordinate);
641 return getCurrentXHeightInherited();
644 void SvgNode::setId(OUString const & rId)
646 if(mpId)
648 mrDocument.removeSvgNodeFromMapper(*mpId);
649 mpId.reset();
652 mpId = rId;
653 mrDocument.addSvgNodeToMapper(*mpId, *this);
656 void SvgNode::setClass(OUString const & rClass)
658 if(mpClass)
660 mrDocument.removeSvgNodeFromMapper(*mpClass);
661 mpClass.reset();
664 mpClass = rClass;
665 mrDocument.addSvgNodeToMapper(*mpClass, *this);
668 XmlSpace SvgNode::getXmlSpace() const
670 if(maXmlSpace != XmlSpace_notset)
672 return maXmlSpace;
675 if(getParent())
677 return getParent()->getXmlSpace();
680 // default is XmlSpace_default
681 return XmlSpace_default;
684 void SvgNode::accept(Visitor & rVisitor)
686 rVisitor.visit(*this);
688 } // end of namespace svgreader
689 } // end of namespace svgio
692 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */