LCOV - code coverage report
Current view: top level - vcl/source/gdi - svgdata.cxx (source / functions) Hit Total Coverage
Test: commit 10e77ab3ff6f4314137acd6e2702a6e5c1ce1fae Lines: 65 80 81.2 %
Date: 2014-11-03 Functions: 9 10 90.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 <tools/stream.hxx>
      21             : #include <vcl/svgdata.hxx>
      22             : #include <comphelper/processfactory.hxx>
      23             : #include <com/sun/star/lang/XMultiServiceFactory.hpp>
      24             : #include <com/sun/star/graphic/SvgTools.hpp>
      25             : #include <com/sun/star/graphic/Primitive2DTools.hpp>
      26             : #include <com/sun/star/rendering/XIntegerReadOnlyBitmap.hpp>
      27             : #include <vcl/canvastools.hxx>
      28             : #include <comphelper/seqstream.hxx>
      29             : #include <vcl/svapp.hxx>
      30             : #include <vcl/outdev.hxx>
      31             : 
      32             : using namespace ::com::sun::star;
      33             : 
      34         292 : BitmapEx VCL_DLLPUBLIC convertPrimitive2DSequenceToBitmapEx(
      35             :     const Primitive2DSequence& rSequence,
      36             :     const basegfx::B2DRange& rTargetRange,
      37             :     const sal_uInt32 nMaximumQuadraticPixels)
      38             : {
      39         292 :     BitmapEx aRetval;
      40             : 
      41         292 :     if(rSequence.hasElements())
      42             :     {
      43             :         // create replacement graphic from maSequence
      44             :         // create XPrimitive2DRenderer
      45         292 :         uno::Reference< uno::XComponentContext > xContext(::comphelper::getProcessComponentContext());
      46             : 
      47             :         try
      48             :         {
      49         292 :             const uno::Reference< graphic::XPrimitive2DRenderer > xPrimitive2DRenderer = graphic::Primitive2DTools::create(xContext);
      50             : 
      51         584 :             uno::Sequence< beans::PropertyValue > aViewParameters;
      52         292 :             geometry::RealRectangle2D aRealRect;
      53             : 
      54         292 :             aRealRect.X1 = rTargetRange.getMinX();
      55         292 :             aRealRect.Y1 = rTargetRange.getMinY();
      56         292 :             aRealRect.X2 = rTargetRange.getMaxX();
      57         292 :             aRealRect.Y2 = rTargetRange.getMaxY();
      58             : 
      59             :             // get system DPI
      60         292 :             const Size aDPI(Application::GetDefaultDevice()->LogicToPixel(Size(1, 1), MAP_INCH));
      61             : 
      62             :             const uno::Reference< rendering::XBitmap > xBitmap(
      63         292 :                 xPrimitive2DRenderer->rasterize(
      64             :                     rSequence,
      65             :                     aViewParameters,
      66         292 :                     aDPI.getWidth(),
      67         292 :                     aDPI.getHeight(),
      68             :                     aRealRect,
      69        1168 :                     nMaximumQuadraticPixels));
      70             : 
      71         292 :             if(xBitmap.is())
      72             :             {
      73         292 :                 const uno::Reference< rendering::XIntegerReadOnlyBitmap> xIntBmp(xBitmap, uno::UNO_QUERY_THROW);
      74             : 
      75         292 :                 if(xIntBmp.is())
      76             :                 {
      77         292 :                     aRetval = vcl::unotools::bitmapExFromXBitmap(xIntBmp);
      78         292 :                 }
      79         292 :             }
      80             :         }
      81           0 :         catch(const uno::Exception& e)
      82             :         {
      83             :             SAL_WARN( "vcl", "Got no graphic::XPrimitive2DRenderer! : " << e.Message);
      84         292 :         }
      85             :     }
      86             : 
      87         292 :     return aRetval;
      88             : }
      89             : 
      90         300 : void SvgData::ensureReplacement()
      91             : {
      92         300 :     ensureSequenceAndRange();
      93             : 
      94         300 :     if(maReplacement.IsEmpty() && maSequence.hasElements())
      95             :     {
      96         292 :         maReplacement = convertPrimitive2DSequenceToBitmapEx(maSequence, getRange());
      97             :     }
      98         300 : }
      99             : 
     100         638 : void SvgData::ensureSequenceAndRange()
     101             : {
     102         638 :     if(!maSequence.hasElements() && mnSvgDataArrayLength)
     103             :     {
     104             :         // import SVG to maSequence, also set maRange
     105         346 :         maRange.reset();
     106             : 
     107             :         // create stream
     108         346 :         const uno::Sequence< sal_Int8 > aPostData((sal_Int8*)maSvgDataArray.get(), mnSvgDataArrayLength);
     109         692 :         const uno::Reference< io::XInputStream > myInputStream(new comphelper::SequenceInputStream(aPostData));
     110             : 
     111         346 :         if(myInputStream.is())
     112             :         {
     113             :             // create SVG interpreter
     114         346 :             uno::Reference< uno::XComponentContext > xContext(::comphelper::getProcessComponentContext());
     115             : 
     116             :             try
     117             :             {
     118         346 :                 const uno::Reference< graphic::XSvgParser > xSvgParser = graphic::SvgTools::create(xContext);
     119             : 
     120         292 :                 maSequence = xSvgParser->getDecomposition(myInputStream, maPath);
     121             :             }
     122          54 :             catch(const uno::Exception&)
     123             :             {
     124             :                 OSL_ENSURE(false, "Got no graphic::XSvgParser (!)" );
     125         346 :             }
     126             :         }
     127             : 
     128         346 :         if(maSequence.hasElements())
     129             :         {
     130         292 :             const sal_Int32 nCount(maSequence.getLength());
     131         292 :             geometry::RealRectangle2D aRealRect;
     132         292 :             uno::Sequence< beans::PropertyValue > aViewParameters;
     133             : 
     134         584 :             for(sal_Int32 a(0L); a < nCount; a++)
     135             :             {
     136             :                 // get reference
     137         292 :                 const Primitive2DReference xReference(maSequence[a]);
     138             : 
     139         292 :                 if(xReference.is())
     140             :                 {
     141         292 :                     aRealRect = xReference->getRange(aViewParameters);
     142             : 
     143             :                     maRange.expand(
     144             :                         basegfx::B2DRange(
     145             :                             aRealRect.X1,
     146             :                             aRealRect.Y1,
     147             :                             aRealRect.X2,
     148         292 :                             aRealRect.Y2));
     149             :                 }
     150         584 :             }
     151         346 :         }
     152             :     }
     153         638 : }
     154             : 
     155         298 : SvgData::SvgData(const SvgDataArray& rSvgDataArray, sal_uInt32 nSvgDataArrayLength, const OUString& rPath)
     156             : :   maSvgDataArray(rSvgDataArray),
     157             :     mnSvgDataArrayLength(nSvgDataArrayLength),
     158             :     maPath(rPath),
     159             :     maRange(),
     160             :     maSequence(),
     161         298 :     maReplacement()
     162             : {
     163         298 : }
     164             : 
     165           0 : SvgData::SvgData(const OUString& rPath):
     166             :     maSvgDataArray(),
     167             :     mnSvgDataArrayLength(0),
     168             :     maPath(rPath),
     169             :     maRange(),
     170             :     maSequence(),
     171           0 :     maReplacement()
     172             : {
     173           0 :     SvFileStream rIStm(rPath, STREAM_STD_READ);
     174           0 :     if(rIStm.GetError())
     175           0 :         return;
     176           0 :     const sal_uInt32 nStmLen(rIStm.remainingSize());
     177           0 :     if(nStmLen)
     178             :     {
     179           0 :         SvgDataArray aNewData(new sal_uInt8[nStmLen]);
     180           0 :         rIStm.Read(aNewData.get(), nStmLen);
     181             : 
     182           0 :         if(!rIStm.GetError())
     183             :         {
     184           0 :             maSvgDataArray = aNewData;
     185           0 :             mnSvgDataArrayLength = nStmLen;
     186           0 :         }
     187           0 :     }
     188             : }
     189             : 
     190         334 : const basegfx::B2DRange& SvgData::getRange() const
     191             : {
     192         334 :     const_cast< SvgData* >(this)->ensureSequenceAndRange();
     193             : 
     194         334 :     return maRange;
     195             : }
     196             : 
     197           4 : const Primitive2DSequence& SvgData::getPrimitive2DSequence() const
     198             : {
     199           4 :     const_cast< SvgData* >(this)->ensureSequenceAndRange();
     200             : 
     201           4 :     return maSequence;
     202             : }
     203             : 
     204         300 : const BitmapEx& SvgData::getReplacement() const
     205             : {
     206         300 :     const_cast< SvgData* >(this)->ensureReplacement();
     207             : 
     208         300 :     return maReplacement;
     209        1233 : }
     210             : 
     211             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10