1ddde725dSArmin Le Grand /************************************************************** 2ddde725dSArmin Le Grand * 3ddde725dSArmin Le Grand * Licensed to the Apache Software Foundation (ASF) under one 4ddde725dSArmin Le Grand * or more contributor license agreements. See the NOTICE file 5ddde725dSArmin Le Grand * distributed with this work for additional information 6ddde725dSArmin Le Grand * regarding copyright ownership. The ASF licenses this file 7ddde725dSArmin Le Grand * to you under the Apache License, Version 2.0 (the 8ddde725dSArmin Le Grand * "License"); you may not use this file except in compliance 9ddde725dSArmin Le Grand * with the License. You may obtain a copy of the License at 10ddde725dSArmin Le Grand * 11ddde725dSArmin Le Grand * http://www.apache.org/licenses/LICENSE-2.0 12ddde725dSArmin Le Grand * 13ddde725dSArmin Le Grand * Unless required by applicable law or agreed to in writing, 14ddde725dSArmin Le Grand * software distributed under the License is distributed on an 15ddde725dSArmin Le Grand * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16ddde725dSArmin Le Grand * KIND, either express or implied. See the License for the 17ddde725dSArmin Le Grand * specific language governing permissions and limitations 18ddde725dSArmin Le Grand * under the License. 19ddde725dSArmin Le Grand * 20ddde725dSArmin Le Grand *************************************************************/ 21ddde725dSArmin Le Grand 22ddde725dSArmin Le Grand // MARKER(update_precomp.py): autogen include statement, do not remove 23ddde725dSArmin Le Grand #include "precompiled_svgio.hxx" 24ddde725dSArmin Le Grand 25ddde725dSArmin Le Grand #include <svgio/svgreader/svgimagenode.hxx> 26ddde725dSArmin Le Grand #include <svgio/svgreader/svgdocument.hxx> 27ddde725dSArmin Le Grand #include <sax/tools/converter.hxx> 28ddde725dSArmin Le Grand #include <tools/stream.hxx> 29ddde725dSArmin Le Grand #include <vcl/bitmapex.hxx> 30ddde725dSArmin Le Grand #include <svtools/filter.hxx> 31ddde725dSArmin Le Grand #include <basegfx/matrix/b2dhommatrixtools.hxx> 32ddde725dSArmin Le Grand #include <drawinglayer/primitive2d/bitmapprimitive2d.hxx> 33ddde725dSArmin Le Grand #include <drawinglayer/primitive2d/groupprimitive2d.hxx> 34ddde725dSArmin Le Grand #include <drawinglayer/primitive2d/transformprimitive2d.hxx> 35ddde725dSArmin Le Grand #include <drawinglayer/primitive2d/maskprimitive2d.hxx> 36ddde725dSArmin Le Grand #include <basegfx/polygon/b2dpolygontools.hxx> 37ddde725dSArmin Le Grand #include <basegfx/polygon/b2dpolygon.hxx> 38ddde725dSArmin Le Grand #include <rtl/uri.hxx> 39ddde725dSArmin Le Grand #include <drawinglayer/geometry/viewinformation2d.hxx> 40ddde725dSArmin Le Grand 41ddde725dSArmin Le Grand ////////////////////////////////////////////////////////////////////////////// 42ddde725dSArmin Le Grand 43ddde725dSArmin Le Grand namespace svgio 44ddde725dSArmin Le Grand { 45ddde725dSArmin Le Grand namespace svgreader 46ddde725dSArmin Le Grand { SvgImageNode(SvgDocument & rDocument,SvgNode * pParent)47ddde725dSArmin Le Grand SvgImageNode::SvgImageNode( 48ddde725dSArmin Le Grand SvgDocument& rDocument, 49ddde725dSArmin Le Grand SvgNode* pParent) 50ddde725dSArmin Le Grand : SvgNode(SVGTokenRect, rDocument, pParent), 51ddde725dSArmin Le Grand maSvgStyleAttributes(*this), 52ddde725dSArmin Le Grand maSvgAspectRatio(), 53ddde725dSArmin Le Grand mpaTransform(0), 54ddde725dSArmin Le Grand maX(0), 55ddde725dSArmin Le Grand maY(0), 56ddde725dSArmin Le Grand maWidth(0), 57ddde725dSArmin Le Grand maHeight(0), 58ddde725dSArmin Le Grand maXLink(), 59ddde725dSArmin Le Grand maUrl(), 60ddde725dSArmin Le Grand maMimeType(), 61ddde725dSArmin Le Grand maData() 62ddde725dSArmin Le Grand { 63ddde725dSArmin Le Grand } 64ddde725dSArmin Le Grand ~SvgImageNode()65ddde725dSArmin Le Grand SvgImageNode::~SvgImageNode() 66ddde725dSArmin Le Grand { 67ddde725dSArmin Le Grand if(mpaTransform) delete mpaTransform; 68ddde725dSArmin Le Grand } 69ddde725dSArmin Le Grand getSvgStyleAttributes() const70ddde725dSArmin Le Grand const SvgStyleAttributes* SvgImageNode::getSvgStyleAttributes() const 71ddde725dSArmin Le Grand { 72ddde725dSArmin Le Grand static rtl::OUString aClassStr(rtl::OUString::createFromAscii("image")); 73ddde725dSArmin Le Grand 7450b37974SArmin Le Grand return checkForCssStyle(aClassStr, maSvgStyleAttributes); 75ddde725dSArmin Le Grand } 76ddde725dSArmin Le Grand parseAttribute(const rtl::OUString & rTokenName,SVGToken aSVGToken,const rtl::OUString & aContent)77ddde725dSArmin Le Grand void SvgImageNode::parseAttribute(const rtl::OUString& rTokenName, SVGToken aSVGToken, const rtl::OUString& aContent) 78ddde725dSArmin Le Grand { 79ddde725dSArmin Le Grand // call parent 80ddde725dSArmin Le Grand SvgNode::parseAttribute(rTokenName, aSVGToken, aContent); 81ddde725dSArmin Le Grand 82ddde725dSArmin Le Grand // read style attributes 83ddde725dSArmin Le Grand maSvgStyleAttributes.parseStyleAttribute(rTokenName, aSVGToken, aContent); 84ddde725dSArmin Le Grand 85ddde725dSArmin Le Grand // parse own 86ddde725dSArmin Le Grand switch(aSVGToken) 87ddde725dSArmin Le Grand { 88ddde725dSArmin Le Grand case SVGTokenStyle: 89ddde725dSArmin Le Grand { 90ddde725dSArmin Le Grand maSvgStyleAttributes.readStyle(aContent); 91ddde725dSArmin Le Grand break; 92ddde725dSArmin Le Grand } 93ddde725dSArmin Le Grand case SVGTokenPreserveAspectRatio: 94ddde725dSArmin Le Grand { 95ddde725dSArmin Le Grand setSvgAspectRatio(readSvgAspectRatio(aContent)); 96ddde725dSArmin Le Grand break; 97ddde725dSArmin Le Grand } 98ddde725dSArmin Le Grand case SVGTokenTransform: 99ddde725dSArmin Le Grand { 100ddde725dSArmin Le Grand const basegfx::B2DHomMatrix aMatrix(readTransform(aContent, *this)); 101ddde725dSArmin Le Grand 102ddde725dSArmin Le Grand if(!aMatrix.isIdentity()) 103ddde725dSArmin Le Grand { 104ddde725dSArmin Le Grand setTransform(&aMatrix); 105ddde725dSArmin Le Grand } 106ddde725dSArmin Le Grand break; 107ddde725dSArmin Le Grand } 108ddde725dSArmin Le Grand case SVGTokenX: 109ddde725dSArmin Le Grand { 110ddde725dSArmin Le Grand SvgNumber aNum; 111ddde725dSArmin Le Grand 112ddde725dSArmin Le Grand if(readSingleNumber(aContent, aNum)) 113ddde725dSArmin Le Grand { 114ddde725dSArmin Le Grand setX(aNum); 115ddde725dSArmin Le Grand } 116ddde725dSArmin Le Grand break; 117ddde725dSArmin Le Grand } 118ddde725dSArmin Le Grand case SVGTokenY: 119ddde725dSArmin Le Grand { 120ddde725dSArmin Le Grand SvgNumber aNum; 121ddde725dSArmin Le Grand 122ddde725dSArmin Le Grand if(readSingleNumber(aContent, aNum)) 123ddde725dSArmin Le Grand { 124ddde725dSArmin Le Grand setY(aNum); 125ddde725dSArmin Le Grand } 126ddde725dSArmin Le Grand break; 127ddde725dSArmin Le Grand } 128ddde725dSArmin Le Grand case SVGTokenWidth: 129ddde725dSArmin Le Grand { 130ddde725dSArmin Le Grand SvgNumber aNum; 131ddde725dSArmin Le Grand 132ddde725dSArmin Le Grand if(readSingleNumber(aContent, aNum)) 133ddde725dSArmin Le Grand { 134ddde725dSArmin Le Grand if(aNum.isPositive()) 135ddde725dSArmin Le Grand { 136ddde725dSArmin Le Grand setWidth(aNum); 137ddde725dSArmin Le Grand } 138ddde725dSArmin Le Grand } 139ddde725dSArmin Le Grand break; 140ddde725dSArmin Le Grand } 141ddde725dSArmin Le Grand case SVGTokenHeight: 142ddde725dSArmin Le Grand { 143ddde725dSArmin Le Grand SvgNumber aNum; 144ddde725dSArmin Le Grand 145ddde725dSArmin Le Grand if(readSingleNumber(aContent, aNum)) 146ddde725dSArmin Le Grand { 147ddde725dSArmin Le Grand if(aNum.isPositive()) 148ddde725dSArmin Le Grand { 149ddde725dSArmin Le Grand setHeight(aNum); 150ddde725dSArmin Le Grand } 151ddde725dSArmin Le Grand } 152ddde725dSArmin Le Grand break; 153ddde725dSArmin Le Grand } 154ddde725dSArmin Le Grand case SVGTokenXlinkHref: 155ddde725dSArmin Le Grand { 156ddde725dSArmin Le Grand const sal_Int32 nLen(aContent.getLength()); 157ddde725dSArmin Le Grand 158ddde725dSArmin Le Grand if(nLen) 159ddde725dSArmin Le Grand { 160ddde725dSArmin Le Grand readImageLink(aContent, maXLink, maUrl, maMimeType, maData); 161ddde725dSArmin Le Grand } 162ddde725dSArmin Le Grand break; 163ddde725dSArmin Le Grand } 164e2bf1e9dSArmin Le Grand default: 165e2bf1e9dSArmin Le Grand { 166e2bf1e9dSArmin Le Grand break; 167e2bf1e9dSArmin Le Grand } 168ddde725dSArmin Le Grand } 169ddde725dSArmin Le Grand } 170ddde725dSArmin Le Grand extractFromGraphic(const Graphic & rGraphic,drawinglayer::primitive2d::Primitive2DSequence & rEmbedded,basegfx::B2DRange & rViewBox,BitmapEx & rBitmapEx)171ddde725dSArmin Le Grand void extractFromGraphic( 172ddde725dSArmin Le Grand const Graphic& rGraphic, 173ddde725dSArmin Le Grand drawinglayer::primitive2d::Primitive2DSequence& rEmbedded, 174ddde725dSArmin Le Grand basegfx::B2DRange& rViewBox, 175ddde725dSArmin Le Grand BitmapEx& rBitmapEx) 176ddde725dSArmin Le Grand { 177ddde725dSArmin Le Grand if(GRAPHIC_BITMAP == rGraphic.GetType()) 178ddde725dSArmin Le Grand { 179ddde725dSArmin Le Grand if(rGraphic.getSvgData().get()) 180ddde725dSArmin Le Grand { 181ddde725dSArmin Le Grand // embedded Svg 182ddde725dSArmin Le Grand rEmbedded = rGraphic.getSvgData()->getPrimitive2DSequence(); 183ddde725dSArmin Le Grand 184ddde725dSArmin Le Grand // fill aViewBox 185ddde725dSArmin Le Grand rViewBox = rGraphic.getSvgData()->getRange(); 186ddde725dSArmin Le Grand } 187ddde725dSArmin Le Grand else 188ddde725dSArmin Le Grand { 189ddde725dSArmin Le Grand // get bitmap 190ddde725dSArmin Le Grand rBitmapEx = rGraphic.GetBitmapEx(); 191ddde725dSArmin Le Grand } 192ddde725dSArmin Le Grand } 193ddde725dSArmin Le Grand else 194ddde725dSArmin Le Grand { 195ddde725dSArmin Le Grand // evtl. convert to bitmap 196ddde725dSArmin Le Grand rBitmapEx = rGraphic.GetBitmapEx(); 197ddde725dSArmin Le Grand } 198ddde725dSArmin Le Grand } 199ddde725dSArmin Le Grand decomposeSvgNode(drawinglayer::primitive2d::Primitive2DSequence & rTarget,bool) const200e2bf1e9dSArmin Le Grand void SvgImageNode::decomposeSvgNode(drawinglayer::primitive2d::Primitive2DSequence& rTarget, bool /*bReferenced*/) const 201ddde725dSArmin Le Grand { 202ddde725dSArmin Le Grand // get size range and create path 203ddde725dSArmin Le Grand const SvgStyleAttributes* pStyle = getSvgStyleAttributes(); 204ddde725dSArmin Le Grand 205ddde725dSArmin Le Grand if(pStyle && getWidth().isSet() && getHeight().isSet()) 206ddde725dSArmin Le Grand { 207ddde725dSArmin Le Grand const double fWidth(getWidth().solve(*this, xcoordinate)); 208ddde725dSArmin Le Grand const double fHeight(getHeight().solve(*this, ycoordinate)); 209ddde725dSArmin Le Grand 210ddde725dSArmin Le Grand if(fWidth > 0.0 && fHeight > 0.0) 211ddde725dSArmin Le Grand { 212ddde725dSArmin Le Grand BitmapEx aBitmapEx; 213ddde725dSArmin Le Grand drawinglayer::primitive2d::Primitive2DSequence aNewTarget; 214ddde725dSArmin Le Grand 215ddde725dSArmin Le Grand // prepare Target and ViewBox for evtl. AspectRatio mappings 216ddde725dSArmin Le Grand const double fX(getX().isSet() ? getX().solve(*this, xcoordinate) : 0.0); 217ddde725dSArmin Le Grand const double fY(getY().isSet() ? getY().solve(*this, ycoordinate) : 0.0); 218ddde725dSArmin Le Grand const basegfx::B2DRange aTarget(fX, fY, fX + fWidth, fY + fHeight); 219ddde725dSArmin Le Grand basegfx::B2DRange aViewBox(aTarget); 220ddde725dSArmin Le Grand 221ddde725dSArmin Le Grand if(maMimeType.getLength() && maData.getLength()) 222ddde725dSArmin Le Grand { 223ddde725dSArmin Le Grand // use embedded base64 encoded data 224ddde725dSArmin Le Grand ::com::sun::star::uno::Sequence< sal_Int8 > aPass; 225ddde725dSArmin Le Grand ::sax::Converter::decodeBase64(aPass, maData); 226ddde725dSArmin Le Grand 227ddde725dSArmin Le Grand if(aPass.hasElements()) 228ddde725dSArmin Le Grand { 229ddde725dSArmin Le Grand SvMemoryStream aStream(aPass.getArray(), aPass.getLength(), STREAM_READ); 230ddde725dSArmin Le Grand Graphic aGraphic; 231ddde725dSArmin Le Grand 232ddde725dSArmin Le Grand if(GRFILTER_OK == GraphicFilter::GetGraphicFilter()->ImportGraphic( 233ddde725dSArmin Le Grand aGraphic, 234ddde725dSArmin Le Grand String(), 235ddde725dSArmin Le Grand aStream)) 236ddde725dSArmin Le Grand { 237ddde725dSArmin Le Grand extractFromGraphic(aGraphic, aNewTarget, aViewBox, aBitmapEx); 238ddde725dSArmin Le Grand } 239ddde725dSArmin Le Grand } 240ddde725dSArmin Le Grand } 241ddde725dSArmin Le Grand else if(maUrl.getLength()) 242ddde725dSArmin Le Grand { 243ddde725dSArmin Le Grand const rtl::OUString& rPath = getDocument().getAbsolutePath(); 244ddde725dSArmin Le Grand 245*3706bbdeSArmin Le Grand if(rPath.getLength()) 246ddde725dSArmin Le Grand { 247*3706bbdeSArmin Le Grand const rtl::OUString aAbsUrl(rtl::Uri::convertRelToAbs(rPath, maUrl)); 248ddde725dSArmin Le Grand 249*3706bbdeSArmin Le Grand if(aAbsUrl.getLength()) 250ddde725dSArmin Le Grand { 251*3706bbdeSArmin Le Grand SvFileStream aStream(aAbsUrl, STREAM_STD_READ); 252*3706bbdeSArmin Le Grand Graphic aGraphic; 253*3706bbdeSArmin Le Grand 254*3706bbdeSArmin Le Grand if(GRFILTER_OK == GraphicFilter::GetGraphicFilter()->ImportGraphic( 255*3706bbdeSArmin Le Grand aGraphic, 256*3706bbdeSArmin Le Grand aAbsUrl, 257*3706bbdeSArmin Le Grand aStream)) 258*3706bbdeSArmin Le Grand { 259*3706bbdeSArmin Le Grand extractFromGraphic(aGraphic, aNewTarget, aViewBox, aBitmapEx); 260*3706bbdeSArmin Le Grand } 261ddde725dSArmin Le Grand } 262ddde725dSArmin Le Grand } 263*3706bbdeSArmin Le Grand else 264*3706bbdeSArmin Le Grand { 265*3706bbdeSArmin Le Grand // #123042# detect missing path and assert - content will be missing. The 266*3706bbdeSArmin Le Grand // absolute path to itself needs to be set to correctly import linked 267*3706bbdeSArmin Le Grand // content in a SVG file 268*3706bbdeSArmin Le Grand OSL_ENSURE(false, "SVG graphic with internal links is interpreted, but local AbsolutePath is not set: linked content will be missing (!)"); 269*3706bbdeSArmin Le Grand } 270ddde725dSArmin Le Grand } 271ddde725dSArmin Le Grand else if(maXLink.getLength()) 272ddde725dSArmin Le Grand { 273ddde725dSArmin Le Grand const SvgNode* mpXLink = getDocument().findSvgNodeById(maXLink); 274ddde725dSArmin Le Grand 275a275c134SArmin Le Grand if(mpXLink && Display_none != mpXLink->getDisplay()) 276ddde725dSArmin Le Grand { 277ddde725dSArmin Le Grand mpXLink->decomposeSvgNode(aNewTarget, true); 278ddde725dSArmin Le Grand 279ddde725dSArmin Le Grand if(aNewTarget.hasElements()) 280ddde725dSArmin Le Grand { 281ddde725dSArmin Le Grand aViewBox = drawinglayer::primitive2d::getB2DRangeFromPrimitive2DSequence( 282ddde725dSArmin Le Grand aNewTarget, 283ddde725dSArmin Le Grand drawinglayer::geometry::ViewInformation2D()); 284ddde725dSArmin Le Grand } 285ddde725dSArmin Le Grand } 286ddde725dSArmin Le Grand } 287ddde725dSArmin Le Grand 288ddde725dSArmin Le Grand if(!aBitmapEx.IsEmpty()) 289ddde725dSArmin Le Grand { 290ddde725dSArmin Le Grand // create content from created bitmap 291ddde725dSArmin Le Grand aNewTarget.realloc(1); 292ddde725dSArmin Le Grand aNewTarget[0] = new drawinglayer::primitive2d::BitmapPrimitive2D( 293ddde725dSArmin Le Grand aBitmapEx, 294ddde725dSArmin Le Grand basegfx::B2DHomMatrix()); 295ddde725dSArmin Le Grand 296ddde725dSArmin Le Grand // fill aViewBox. No size set yet, use unit size 297ddde725dSArmin Le Grand aViewBox = basegfx::B2DRange(0.0, 0.0, 1.0, 1.0); 298ddde725dSArmin Le Grand } 299ddde725dSArmin Le Grand 300ddde725dSArmin Le Grand if(aNewTarget.hasElements()) 301ddde725dSArmin Le Grand { 302ddde725dSArmin Le Grand if(aTarget.equal(aViewBox)) 303ddde725dSArmin Le Grand { 304ddde725dSArmin Le Grand // just add to rTarget 305ddde725dSArmin Le Grand drawinglayer::primitive2d::appendPrimitive2DSequenceToPrimitive2DSequence(rTarget, aNewTarget); 306ddde725dSArmin Le Grand } 307ddde725dSArmin Le Grand else 308ddde725dSArmin Le Grand { 309ddde725dSArmin Le Grand // create mapping 310ddde725dSArmin Le Grand const SvgAspectRatio& rRatio = getSvgAspectRatio(); 311ddde725dSArmin Le Grand 312ddde725dSArmin Le Grand if(rRatio.isSet()) 313ddde725dSArmin Le Grand { 314ddde725dSArmin Le Grand // let mapping be created from SvgAspectRatio 315ddde725dSArmin Le Grand const basegfx::B2DHomMatrix aEmbeddingTransform(rRatio.createMapping(aTarget, aViewBox)); 316ddde725dSArmin Le Grand 317ddde725dSArmin Le Grand if(!aEmbeddingTransform.isIdentity()) 318ddde725dSArmin Le Grand { 319ddde725dSArmin Le Grand const drawinglayer::primitive2d::Primitive2DReference xRef( 320ddde725dSArmin Le Grand new drawinglayer::primitive2d::TransformPrimitive2D( 321ddde725dSArmin Le Grand aEmbeddingTransform, 322ddde725dSArmin Le Grand aNewTarget)); 323ddde725dSArmin Le Grand 324ddde725dSArmin Le Grand aNewTarget = drawinglayer::primitive2d::Primitive2DSequence(&xRef, 1); 325ddde725dSArmin Le Grand } 326ddde725dSArmin Le Grand 327ddde725dSArmin Le Grand if(!rRatio.isMeetOrSlice()) 328ddde725dSArmin Le Grand { 329ddde725dSArmin Le Grand // need to embed in MaskPrimitive2D to ensure clipping 330ddde725dSArmin Le Grand const drawinglayer::primitive2d::Primitive2DReference xMask( 331ddde725dSArmin Le Grand new drawinglayer::primitive2d::MaskPrimitive2D( 332ddde725dSArmin Le Grand basegfx::B2DPolyPolygon( 333ddde725dSArmin Le Grand basegfx::tools::createPolygonFromRect(aTarget)), 334ddde725dSArmin Le Grand aNewTarget)); 335ddde725dSArmin Le Grand 336ddde725dSArmin Le Grand aNewTarget = drawinglayer::primitive2d::Primitive2DSequence(&xMask, 1); 337ddde725dSArmin Le Grand } 338ddde725dSArmin Le Grand } 339ddde725dSArmin Le Grand else 340ddde725dSArmin Le Grand { 341ddde725dSArmin Le Grand // choose default mapping 342ddde725dSArmin Le Grand const basegfx::B2DHomMatrix aEmbeddingTransform(rRatio.createLinearMapping(aTarget, aViewBox)); 343ddde725dSArmin Le Grand 344ddde725dSArmin Le Grand if(!aEmbeddingTransform.isIdentity()) 345ddde725dSArmin Le Grand { 346ddde725dSArmin Le Grand const drawinglayer::primitive2d::Primitive2DReference xRef( 347ddde725dSArmin Le Grand new drawinglayer::primitive2d::TransformPrimitive2D( 348ddde725dSArmin Le Grand aEmbeddingTransform, 349ddde725dSArmin Le Grand aNewTarget)); 350ddde725dSArmin Le Grand 351ddde725dSArmin Le Grand aNewTarget = drawinglayer::primitive2d::Primitive2DSequence(&xRef, 1); 352ddde725dSArmin Le Grand } 353ddde725dSArmin Le Grand } 354ddde725dSArmin Le Grand 355ddde725dSArmin Le Grand // embed and add to rTarget, take local extra-transform into account 356ddde725dSArmin Le Grand pStyle->add_postProcess(rTarget, aNewTarget, getTransform()); 357ddde725dSArmin Le Grand } 358ddde725dSArmin Le Grand } 359ddde725dSArmin Le Grand } 360ddde725dSArmin Le Grand } 361ddde725dSArmin Le Grand } 362ddde725dSArmin Le Grand 363ddde725dSArmin Le Grand } // end of namespace svgreader 364ddde725dSArmin Le Grand } // end of namespace svgio 365ddde725dSArmin Le Grand 366ddde725dSArmin Le Grand ////////////////////////////////////////////////////////////////////////////// 367ddde725dSArmin Le Grand // eof 368