cid#1636693 COPY_INSTEAD_OF_MOVE
[LibreOffice.git] / drawinglayer / source / primitive2d / metafileprimitive2d.cxx
blob46ddf658257150c11657c53713fd0e7467d119e1
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <drawinglayer/primitive2d/metafileprimitive2d.hxx>
21 #include <utility>
22 #include <wmfemfhelper.hxx>
24 #include <drawinglayer/primitive2d/drawinglayer_primitivetypes2d.hxx>
25 #include <drawinglayer/primitive2d/transformprimitive2d.hxx>
26 #include <basegfx/polygon/b2dpolygontools.hxx>
27 #include <drawinglayer/primitive2d/maskprimitive2d.hxx>
28 #include <vcl/canvastools.hxx>
30 using namespace com::sun::star;
32 namespace drawinglayer::primitive2d
34 Primitive2DReference MetafilePrimitive2D::create2DDecomposition(const geometry::ViewInformation2D& rViewInformation) const
36 // Interpret the Metafile and get the content. There should be only one target, as in the start condition,
37 // but iterating will be the right thing to do when some push/pop is not closed
38 Primitive2DContainer xRetval(wmfemfhelper::interpretMetafile(getMetaFile(), rViewInformation));
40 if(xRetval.empty())
41 return nullptr;
43 // get target size
44 const ::tools::Rectangle aMtfTarget(getMetaFile().GetPrefMapMode().GetOrigin(), getMetaFile().GetPrefSize());
45 const basegfx::B2DRange aMtfRange(vcl::unotools::b2DRectangleFromRectangle(aMtfTarget));
47 // tdf#113197 get content range and check if we have an overlap with
48 // defined target range (aMtfRange)
49 if (!aMtfRange.isEmpty())
51 const basegfx::B2DRange aContentRange(xRetval.getB2DRange(rViewInformation));
53 // also test equal since isInside gives also true for equal
54 if (!aMtfRange.equal(aContentRange) && !aMtfRange.isInside(aContentRange))
56 // contentRange is partly larger than aMtfRange (stuff sticks
57 // outside), clipping is needed
58 const drawinglayer::primitive2d::Primitive2DReference xMask(
59 new drawinglayer::primitive2d::MaskPrimitive2D(
60 basegfx::B2DPolyPolygon(
61 basegfx::utils::createPolygonFromRect(
62 aMtfRange)),
63 std::move(xRetval)));
65 xRetval = drawinglayer::primitive2d::Primitive2DContainer{ xMask };
69 // create transformation
70 basegfx::B2DHomMatrix aAdaptedTransform;
72 aAdaptedTransform.translate(-aMtfTarget.Left(), -aMtfTarget.Top());
73 aAdaptedTransform.scale(
74 aMtfTarget.getOpenWidth() ? 1.0 / aMtfTarget.getOpenWidth() : 1.0,
75 aMtfTarget.getOpenHeight() ? 1.0 / aMtfTarget.getOpenHeight() : 1.0);
76 aAdaptedTransform = getTransform() * aAdaptedTransform;
78 // embed to target transformation
79 const Primitive2DReference aEmbeddedTransform(
80 new TransformPrimitive2D(
81 aAdaptedTransform,
82 std::move(xRetval)));
84 return aEmbeddedTransform;
87 MetafilePrimitive2D::MetafilePrimitive2D(
88 basegfx::B2DHomMatrix aMetaFileTransform,
89 const GDIMetaFile& rMetaFile)
90 : maMetaFileTransform(std::move(aMetaFileTransform)),
91 maMetaFile(rMetaFile)
93 // activate callback to flush buffered decomposition content
94 setCallbackSeconds(20);
97 bool MetafilePrimitive2D::operator==(const BasePrimitive2D& rPrimitive) const
99 if(BufferedDecompositionPrimitive2D::operator==(rPrimitive))
101 const MetafilePrimitive2D& rCompare = static_cast<const MetafilePrimitive2D&>(rPrimitive);
103 return (getTransform() == rCompare.getTransform()
104 && getMetaFile() == rCompare.getMetaFile());
107 return false;
110 basegfx::B2DRange MetafilePrimitive2D::getB2DRange(const geometry::ViewInformation2D& /*rViewInformation*/) const
112 // use own implementation to quickly answer the getB2DRange question. The
113 // MetafilePrimitive2D assumes that all geometry is inside of the shape. If
114 // this is not the case (i have already seen some wrong Metafiles) it should
115 // be embedded to a MaskPrimitive2D
116 basegfx::B2DRange aRetval(0.0, 0.0, 1.0, 1.0);
117 aRetval.transform(getTransform());
119 return aRetval;
122 // from MetafileAccessor
123 void MetafilePrimitive2D::accessMetafile(GDIMetaFile& rTargetMetafile) const
125 rTargetMetafile = maMetaFile;
128 // provide unique ID
129 sal_uInt32 MetafilePrimitive2D::getPrimitive2DID() const
131 return PRIMITIVE2D_ID_METAFILEPRIMITIVE2D;
134 } // end of namespace
136 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */