LCOV - code coverage report
Current view: top level - svgio/source/svgreader - svgstylenode.cxx (source / functions) Hit Total Coverage
Test: commit 10e77ab3ff6f4314137acd6e2702a6e5c1ce1fae Lines: 80 85 94.1 %
Date: 2014-11-03 Functions: 8 8 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
       2             : /*
       3             :  * This file is part of the LibreOffice project.
       4             :  *
       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/.
       8             :  *
       9             :  * This file incorporates work covered by the following license notice:
      10             :  *
      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 .
      18             :  */
      19             : 
      20             : #include <svgio/svgreader/svgstylenode.hxx>
      21             : #include <svgio/svgreader/svgdocument.hxx>
      22             : 
      23             : namespace svgio
      24             : {
      25             :     namespace svgreader
      26             :     {
      27           4 :         SvgStyleNode::SvgStyleNode(
      28             :             SvgDocument& rDocument,
      29             :             SvgNode* pParent)
      30             :         :   SvgNode(SVGTokenStyle, rDocument, pParent),
      31             :             maSvgStyleAttributes(),
      32           4 :             mbTextCss(false)
      33             :         {
      34           4 :         }
      35             : 
      36          12 :         SvgStyleNode::~SvgStyleNode()
      37             :         {
      38          12 :             while(!maSvgStyleAttributes.empty())
      39             :             {
      40           4 :                 delete *(maSvgStyleAttributes.end() - 1);
      41           4 :                 maSvgStyleAttributes.pop_back();
      42             :             }
      43           8 :         }
      44             : 
      45             :         // #i125258# no parent when we are a CssStyle holder to break potential loops because
      46             :         // when using CssStyles we jump uncontrolled inside the node tree hierarchy
      47           4 :         bool SvgStyleNode::supportsParentStyle() const
      48             :         {
      49           4 :             if(isTextCss())
      50             :             {
      51           4 :                 return false;
      52             :             }
      53             : 
      54             :             // call parent
      55           0 :             return SvgNode::supportsParentStyle();
      56             :         }
      57             : 
      58           4 :         void SvgStyleNode::parseAttribute(const OUString& rTokenName, SVGToken aSVGToken, const OUString& aContent)
      59             :         {
      60             :             // call parent
      61           4 :             SvgNode::parseAttribute(rTokenName, aSVGToken, aContent);
      62             : 
      63             :             // parse own
      64           4 :             switch(aSVGToken)
      65             :             {
      66             :                 case SVGTokenType:
      67             :                 {
      68           4 :                     if(!aContent.isEmpty())
      69             :                     {
      70           4 :                         if(aContent.startsWith("text/css"))
      71             :                         {
      72           4 :                             setTextCss(true);
      73             :                         }
      74             :                     }
      75           4 :                     break;
      76             :                 }
      77             :                 default:
      78             :                 {
      79           0 :                     break;
      80             :                 }
      81             :             }
      82           4 :         }
      83             : 
      84           4 :         void SvgStyleNode::addCssStyleSheet(const OUString& aSelectors, const SvgStyleAttributes& rNewStyle)
      85             :         {
      86             :             // aSelectors: CssStyle selectors, any combination, no comma separations, no spaces at start/end
      87             :             // rNewStyle: the already preapared style to register on that name
      88           4 :             if(!aSelectors.isEmpty())
      89             :             {
      90           4 :                 std::vector< OUString > aSelectorParts;
      91           4 :                 const sal_Int32 nLen(aSelectors.getLength());
      92           4 :                 sal_Int32 nPos(0);
      93           8 :                 OUStringBuffer aToken;
      94             : 
      95             :                 // split into single tokens (currently only space separator)
      96          12 :                 while(nPos < nLen)
      97             :                 {
      98           4 :                     const sal_Int32 nInitPos(nPos);
      99           4 :                     copyToLimiter(aSelectors, sal_Unicode(' '), nPos, aToken, nLen);
     100           4 :                     skip_char(aSelectors, sal_Unicode(' '), nPos, nLen);
     101           4 :                     const OUString aSelectorPart(aToken.makeStringAndClear().trim());
     102             : 
     103           4 :                     if(!aSelectorPart.isEmpty())
     104             :                     {
     105           4 :                         aSelectorParts.push_back(aSelectorPart);
     106             :                     }
     107             : 
     108           4 :                     if(nInitPos == nPos)
     109             :                     {
     110             :                         OSL_ENSURE(false, "Could not interpret on current position (!)");
     111           0 :                         nPos++;
     112             :                     }
     113           4 :                 }
     114             : 
     115           4 :                 if(aSelectorParts.size())
     116             :                 {
     117           4 :                     OUString aConcatenatedSelector;
     118             : 
     119             :                     // re-combine without spaces, create a unique name (for now)
     120           8 :                     for(sal_uInt32 a(0); a < aSelectorParts.size(); a++)
     121             :                     {
     122           4 :                         aConcatenatedSelector += aSelectorParts[a];
     123             :                     }
     124             : 
     125             :                     // CssStyles in SVG are currently not completely supported; the current idea for
     126             :                     // supporting the needed minimal set is to register CssStyles associated to a string
     127             :                     // which is just the space-char cleaned, concatenated Selectors. The part to 'match'
     128             :                     // these is in fillCssStyleVectorUsingHierarchyAndSelectors. There, the same string is
     129             :                     // built up using the priorities of local CssStyle, Id, Class and other info combined
     130             :                     // with the existing hierarchy. This creates a specificity- and priority-sorted local
     131             :                     // list for each node which is then chained using get/setCssStyleParent.
     132             :                     // The current solution is capable of solving space-separated selectors which can be
     133             :                     // mixed between Id, Class and type specifiers.
     134             :                     // When CssStyles need more specific solving, the start point is here; remember the
     135             :                     // needed infos not in maIdStyleTokenMapperList at the document, but select evtl.
     136             :                     // more specific infos there in a class capable of handling more complex matchings.
     137             :                     // Additionally fillCssStyleVector (or the mechanism above that when a linked list of
     138             :                     // SvgStyleAttributes will not do it) will have to be adapted to make use of it.
     139             : 
     140             :                     // register new style at document for (evtl. concatenated) stylename
     141           4 :                     const_cast< SvgDocument& >(getDocument()).addSvgStyleAttributesToMapper(aConcatenatedSelector, rNewStyle);
     142           4 :                 }
     143             :             }
     144           4 :         }
     145             : 
     146           4 :         void SvgStyleNode::addCssStyleSheet(const OUString& aSelectors, const OUString& aContent)
     147             :         {
     148             :             // aSelectors: possible comma-separated list of CssStyle definitions, no spaces at start/end
     149             :             // aContent: the svg style definitions as string
     150           4 :             if(!aSelectors.isEmpty() && !aContent.isEmpty())
     151             :             {
     152             :                 // create new style and add to local list (for ownership control)
     153           4 :                 SvgStyleAttributes* pNewStyle = new SvgStyleAttributes(*this);
     154           4 :                 maSvgStyleAttributes.push_back(pNewStyle);
     155             : 
     156             :                 // fill with content
     157           4 :                 pNewStyle->readCssStyle(aContent);
     158             : 
     159             :                 // comma-separated split (Css abbreviation for same style for multiple selectors)
     160           4 :                 const sal_Int32 nLen(aSelectors.getLength());
     161           4 :                 sal_Int32 nPos(0);
     162           4 :                 OUStringBuffer aToken;
     163             : 
     164          12 :                 while(nPos < nLen)
     165             :                 {
     166           4 :                     const sal_Int32 nInitPos(nPos);
     167           4 :                     copyToLimiter(aSelectors, sal_Unicode(','), nPos, aToken, nLen);
     168           4 :                     skip_char(aSelectors, sal_Unicode(' '), sal_Unicode(','), nPos, nLen);
     169             : 
     170           4 :                     const OUString aSingleName(aToken.makeStringAndClear().trim());
     171             : 
     172           4 :                     if(aSingleName.getLength())
     173             :                     {
     174           4 :                         addCssStyleSheet(aSingleName, *pNewStyle);
     175             :                     }
     176             : 
     177           4 :                     if(nInitPos == nPos)
     178             :                     {
     179             :                         OSL_ENSURE(false, "Could not interpret on current position (!)");
     180           0 :                         nPos++;
     181             :                     }
     182           8 :                 }
     183             :             }
     184           4 :         }
     185             : 
     186           4 :         void SvgStyleNode::addCssStyleSheet(const OUString& aSelectorsAndContent)
     187             :         {
     188           4 :             const sal_Int32 nLen(aSelectorsAndContent.getLength());
     189             : 
     190           4 :             if(nLen)
     191             :             {
     192           4 :                 sal_Int32 nPos(0);
     193           4 :                 OUStringBuffer aToken;
     194             : 
     195          12 :                 while(nPos < nLen)
     196             :                 {
     197             :                     // read the full selectors (may be multiple, comma-separated)
     198           4 :                     const sal_Int32 nInitPos(nPos);
     199           4 :                     skip_char(aSelectorsAndContent, sal_Unicode(' '), nPos, nLen);
     200           4 :                     copyToLimiter(aSelectorsAndContent, sal_Unicode('{'), nPos, aToken, nLen);
     201           4 :                     skip_char(aSelectorsAndContent, sal_Unicode(' '), sal_Unicode('{'), nPos, nLen);
     202             : 
     203           4 :                     const OUString aSelectors(aToken.makeStringAndClear().trim());
     204           8 :                     OUString aContent;
     205             : 
     206           4 :                     if(!aSelectors.isEmpty() && nPos < nLen)
     207             :                     {
     208             :                         // isolate content as text, embraced by '{' and '}'
     209           4 :                         copyToLimiter(aSelectorsAndContent, sal_Unicode('}'), nPos, aToken, nLen);
     210           4 :                         skip_char(aSelectorsAndContent, sal_Unicode(' '), sal_Unicode('}'), nPos, nLen);
     211             : 
     212           4 :                         aContent = aToken.makeStringAndClear().trim();
     213             :                     }
     214             : 
     215           4 :                     if(!aSelectors.isEmpty() && !aContent.isEmpty())
     216             :                     {
     217           4 :                         addCssStyleSheet(aSelectors, aContent);
     218             :                     }
     219             : 
     220           4 :                     if(nInitPos == nPos)
     221             :                     {
     222             :                         OSL_ENSURE(false, "Could not interpret on current position (!)");
     223           0 :                         nPos++;
     224             :                     }
     225           8 :                 }
     226             :             }
     227           4 :         }
     228             : 
     229             :     } // end of namespace svgreader
     230             : } // end of namespace svgio
     231             : 
     232             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10