LCOV - code coverage report
Current view: top level - vcl/source/gdi - svgdata.cxx (source / functions) Hit Total Coverage
Test: commit c8344322a7af75b84dd3ca8f78b05543a976dfd5 Lines: 63 79 79.7 %
Date: 2015-06-13 12:38:46 Functions: 7 8 87.5 %
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         294 : BitmapEx convertPrimitive2DSequenceToBitmapEx(
      35             :     const Primitive2DSequence& rSequence,
      36             :     const basegfx::B2DRange& rTargetRange,
      37             :     const sal_uInt32 nMaximumQuadraticPixels)
      38             : {
      39         294 :     BitmapEx aRetval;
      40             : 
      41         294 :     if(rSequence.hasElements())
      42             :     {
      43             :         // create replacement graphic from maSequence
      44             :         // create XPrimitive2DRenderer
      45         294 :         uno::Reference< uno::XComponentContext > xContext(::comphelper::getProcessComponentContext());
      46             : 
      47             :         try
      48             :         {
      49         294 :             const uno::Reference< graphic::XPrimitive2DRenderer > xPrimitive2DRenderer = graphic::Primitive2DTools::create(xContext);
      50             : 
      51         588 :             uno::Sequence< beans::PropertyValue > aViewParameters;
      52         294 :             geometry::RealRectangle2D aRealRect;
      53             : 
      54         294 :             aRealRect.X1 = rTargetRange.getMinX();
      55         294 :             aRealRect.Y1 = rTargetRange.getMinY();
      56         294 :             aRealRect.X2 = rTargetRange.getMaxX();
      57         294 :             aRealRect.Y2 = rTargetRange.getMaxY();
      58             : 
      59             :             // get system DPI
      60         294 :             const Size aDPI(Application::GetDefaultDevice()->LogicToPixel(Size(1, 1), MAP_INCH));
      61             : 
      62             :             const uno::Reference< rendering::XBitmap > xBitmap(
      63         294 :                 xPrimitive2DRenderer->rasterize(
      64             :                     rSequence,
      65             :                     aViewParameters,
      66         294 :                     aDPI.getWidth(),
      67         294 :                     aDPI.getHeight(),
      68             :                     aRealRect,
      69        1176 :                     nMaximumQuadraticPixels));
      70             : 
      71         294 :             if(xBitmap.is())
      72             :             {
      73         294 :                 const uno::Reference< rendering::XIntegerReadOnlyBitmap> xIntBmp(xBitmap, uno::UNO_QUERY_THROW);
      74             : 
      75         294 :                 if(xIntBmp.is())
      76             :                 {
      77         294 :                     aRetval = vcl::unotools::bitmapExFromXBitmap(xIntBmp);
      78         294 :                 }
      79         294 :             }
      80             :         }
      81           0 :         catch(const uno::Exception& e)
      82             :         {
      83             :             SAL_WARN( "vcl", "Got no graphic::XPrimitive2DRenderer! : " << e.Message);
      84         294 :         }
      85             :     }
      86             : 
      87         294 :     return aRetval;
      88             : }
      89             : 
      90         295 : void SvgData::ensureReplacement()
      91             : {
      92         295 :     ensureSequenceAndRange();
      93             : 
      94         295 :     if(maReplacement.IsEmpty() && maSequence.hasElements())
      95             :     {
      96         294 :         maReplacement = convertPrimitive2DSequenceToBitmapEx(maSequence, getRange());
      97             :     }
      98         295 : }
      99             : 
     100         603 : void SvgData::ensureSequenceAndRange()
     101             : {
     102         603 :     if(!maSequence.hasElements() && mnSvgDataArrayLength)
     103             :     {
     104             :         // import SVG to maSequence, also set maRange
     105         295 :         maRange.reset();
     106             : 
     107             :         // create stream
     108         295 :         const uno::Sequence< sal_Int8 > aPostData(reinterpret_cast<sal_Int8*>(maSvgDataArray.get()), mnSvgDataArrayLength);
     109         590 :         const uno::Reference< io::XInputStream > myInputStream(new comphelper::SequenceInputStream(aPostData));
     110             : 
     111         295 :         if(myInputStream.is())
     112             :         {
     113             :             // create SVG interpreter
     114         295 :             uno::Reference< uno::XComponentContext > xContext(::comphelper::getProcessComponentContext());
     115             : 
     116             :             try
     117             :             {
     118         295 :                 const uno::Reference< graphic::XSvgParser > xSvgParser = graphic::SvgTools::create(xContext);
     119             : 
     120         295 :                 maSequence = xSvgParser->getDecomposition(myInputStream, maPath);
     121             :             }
     122           0 :             catch(const uno::Exception&)
     123             :             {
     124             :                 OSL_ENSURE(false, "Got no graphic::XSvgParser (!)" );
     125         295 :             }
     126             :         }
     127             : 
     128         295 :         if(maSequence.hasElements())
     129             :         {
     130         295 :             const sal_Int32 nCount(maSequence.getLength());
     131         295 :             geometry::RealRectangle2D aRealRect;
     132         295 :             uno::Sequence< beans::PropertyValue > aViewParameters;
     133             : 
     134         590 :             for(sal_Int32 a(0L); a < nCount; a++)
     135             :             {
     136             :                 // get reference
     137         295 :                 const Primitive2DReference xReference(maSequence[a]);
     138             : 
     139         295 :                 if(xReference.is())
     140             :                 {
     141         295 :                     aRealRect = xReference->getRange(aViewParameters);
     142             : 
     143             :                     maRange.expand(
     144             :                         basegfx::B2DRange(
     145             :                             aRealRect.X1,
     146             :                             aRealRect.Y1,
     147             :                             aRealRect.X2,
     148         295 :                             aRealRect.Y2));
     149             :                 }
     150         590 :             }
     151         295 :         }
     152             :     }
     153         603 : }
     154             : 
     155         295 : 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         295 :     maReplacement()
     162             : {
     163         295 : }
     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         306 : const basegfx::B2DRange& SvgData::getRange() const
     191             : {
     192         306 :     const_cast< SvgData* >(this)->ensureSequenceAndRange();
     193             : 
     194         306 :     return maRange;
     195             : }
     196             : 
     197           2 : const Primitive2DSequence& SvgData::getPrimitive2DSequence() const
     198             : {
     199           2 :     const_cast< SvgData* >(this)->ensureSequenceAndRange();
     200             : 
     201           2 :     return maSequence;
     202             : }
     203             : 
     204         295 : const BitmapEx& SvgData::getReplacement() const
     205             : {
     206         295 :     const_cast< SvgData* >(this)->ensureReplacement();
     207             : 
     208         295 :     return maReplacement;
     209             : }
     210             : 
     211             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.11