From dca73ef8cf8ddd6078d70f4bc759708f3f5d4618 Mon Sep 17 00:00:00 2001 From: Tomaž Vajngerl Date: Sun, 8 Nov 2015 23:00:14 +0100 Subject: support to load SVG images when loading from Image resource Change-Id: Ieda1c334d8d995c774381c52fa1d9aa11751c5ef --- vcl/Library_vcl.mk | 1 + vcl/source/bitmap/BitmapTools.cxx | 95 +++++++++++++++++++++++++++++++++++++++ vcl/source/gdi/impimagetree.cxx | 5 +++ 3 files changed, 101 insertions(+) create mode 100644 vcl/source/bitmap/BitmapTools.cxx (limited to 'vcl') diff --git a/vcl/Library_vcl.mk b/vcl/Library_vcl.mk index 575a64fea777..a59c49b45222 100644 --- a/vcl/Library_vcl.mk +++ b/vcl/Library_vcl.mk @@ -326,6 +326,7 @@ $(eval $(call gb_Library_add_exception_objects,vcl,\ vcl/source/bitmap/BitmapSymmetryCheck \ vcl/source/bitmap/BitmapFilterStackBlur \ vcl/source/bitmap/BitmapProcessor \ + vcl/source/bitmap/BitmapTools \ vcl/source/bitmap/checksum \ vcl/source/helper/canvasbitmap \ vcl/source/helper/canvastools \ diff --git a/vcl/source/bitmap/BitmapTools.cxx b/vcl/source/bitmap/BitmapTools.cxx new file mode 100644 index 000000000000..c65376ac149e --- /dev/null +++ b/vcl/source/bitmap/BitmapTools.cxx @@ -0,0 +1,95 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + */ + +#include + +#include +#include +#include + +#include +#include + +#include + +#include + +using namespace css; + +using drawinglayer::primitive2d::Primitive2DSequence; +using drawinglayer::primitive2d::Primitive2DReference; + +namespace vcl +{ + +void BitmapTools::loadFromSvg(SvStream& rStream, const OUString& sPath, BitmapEx& rBitmapEx, double fScalingFactor, const Size& aSize) +{ + uno::Reference xContext(comphelper::getProcessComponentContext()); + const uno::Reference xSvgParser = graphic::SvgTools::create(xContext); + + sal_Size nSize = rStream.remainingSize(); + std::vector pBuffer(nSize + 1); + rStream.Read(pBuffer.data(), nSize); + pBuffer[nSize] = 0; + + uno::Sequence aData(pBuffer.data(), nSize + 1); + uno::Reference aInputStream(new comphelper::SequenceInputStream(aData)); + + Primitive2DSequence aPrimitiveSequence = xSvgParser->getDecomposition(aInputStream, sPath); + + if (aPrimitiveSequence.hasElements()) + { + uno::Sequence aViewParameters; + + const sal_Int32 nCount(aPrimitiveSequence.getLength()); + geometry::RealRectangle2D aRealRect; + basegfx::B2DRange aRange; + for (sal_Int32 a = 0L; a < nCount; ++a) + { + const Primitive2DReference xReference(aPrimitiveSequence[a]); + + if (xReference.is()) + { + aRealRect = xReference->getRange(aViewParameters); + aRange.expand(basegfx::B2DRange(aRealRect.X1, aRealRect.Y1, aRealRect.X2, aRealRect.Y2)); + } + } + + bool bIsSizeEmpty = (aSize.Width() == 0 && aSize.Height() == 0); + + aRealRect.X1 = 0; + aRealRect.Y1 = 0; + aRealRect.X2 = bIsSizeEmpty ? (aSize.Width() * 2540 / 90) : aRange.getMaxX() - aRange.getMinX(); + aRealRect.Y2 = bIsSizeEmpty ? (aSize.Height() * 2540 / 90) : aRange.getMaxY() - aRange.getMinY(); + + double nDPI = 90 * fScalingFactor; + + const css::uno::Reference xPrimitive2DRenderer = css::graphic::Primitive2DTools::create(xContext); + const css::uno::Reference xBitmap( + xPrimitive2DRenderer->rasterize(aPrimitiveSequence, aViewParameters, nDPI, nDPI, aRealRect, 256*256)); + + printf("%f %f %f %f\n", aRealRect.X1, aRealRect.Y1, aRealRect.X2, aRealRect.Y2); + + if (xBitmap.is()) + { + const css::uno::Reference xIntBmp(xBitmap, uno::UNO_QUERY_THROW); + + if (xIntBmp.is()) + { + rBitmapEx = vcl::unotools::bitmapExFromXBitmap(xIntBmp); + printf("Size: %d %d\n", rBitmapEx.GetSizePixel().Width(), rBitmapEx.GetSizePixel().Height()); + } + } + } +} + +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/vcl/source/gdi/impimagetree.cxx b/vcl/source/gdi/impimagetree.cxx index b7620c8a072c..0a8d1a5b1e27 100644 --- a/vcl/source/gdi/impimagetree.cxx +++ b/vcl/source/gdi/impimagetree.cxx @@ -47,6 +47,7 @@ #include #include +#include using namespace css; @@ -86,6 +87,10 @@ static void loadImageFromStream(std::shared_ptr xStream, OUString cons aPNGReader.SetIgnoreGammaChunk( true ); rBitmap = aPNGReader.Read(); } + else if (rPath.endsWith(".svg")) + { + vcl::BitmapTools::loadFromSvg(*xStream.get(), rPath, rBitmap); + } else { ReadDIBBitmapEx(rBitmap, *xStream); -- cgit