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

Generated by: LCOV version 1.10