nss: upgrade to release 3.73
[LibreOffice.git] / drawinglayer / source / primitive2d / graphicprimitive2d.cxx
blob9de4b8a80e6406a8823cab80fbe47717a38f7d8d
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 <sal/config.h>
22 #include <algorithm>
24 #include <drawinglayer/primitive2d/graphicprimitive2d.hxx>
25 #include <primitive2d/cropprimitive2d.hxx>
26 #include <drawinglayer/primitive2d/drawinglayer_primitivetypes2d.hxx>
27 #include <primitive2d/graphicprimitivehelper2d.hxx>
28 #include <drawinglayer/primitive2d/unifiedtransparenceprimitive2d.hxx>
29 #include <basegfx/matrix/b2dhommatrixtools.hxx>
31 namespace drawinglayer::primitive2d
33 void GraphicPrimitive2D::create2DDecomposition(Primitive2DContainer& rContainer,
34 const geometry::ViewInformation2D&) const
36 if (255 == getGraphicAttr().GetTransparency())
38 // content is invisible, done
39 return;
42 // do not apply mirroring from GraphicAttr to the Metafile by calling
43 // GetTransformedGraphic, this will try to mirror the Metafile using Scale()
44 // at the Metafile. This again calls Scale at the single MetaFile actions,
45 // but this implementation never worked. I reworked that implementations,
46 // but for security reasons i will try not to use it.
47 basegfx::B2DHomMatrix aTransform(getTransform());
49 if (getGraphicAttr().IsMirrored())
51 // content needs mirroring
52 const bool bHMirr(getGraphicAttr().GetMirrorFlags() & BmpMirrorFlags::Horizontal);
53 const bool bVMirr(getGraphicAttr().GetMirrorFlags() & BmpMirrorFlags::Vertical);
55 // mirror by applying negative scale to the unit primitive and
56 // applying the object transformation on it.
57 aTransform
58 = basegfx::utils::createScaleB2DHomMatrix(bHMirr ? -1.0 : 1.0, bVMirr ? -1.0 : 1.0);
59 aTransform.translate(bHMirr ? 1.0 : 0.0, bVMirr ? 1.0 : 0.0);
60 aTransform = getTransform() * aTransform;
63 // Get transformed graphic. Suppress rotation and cropping, only filtering is needed
64 // here (and may be replaced later on). Cropping is handled below as mask primitive (if set).
65 // Also need to suppress mirroring, it is part of the transformation now (see above).
66 // Also move transparency handling to embedding to a UnifiedTransparencePrimitive2D; do
67 // that by remembering original transparency and applying that later if needed
68 GraphicAttr aSuppressGraphicAttr(getGraphicAttr());
70 aSuppressGraphicAttr.SetCrop(0, 0, 0, 0);
71 aSuppressGraphicAttr.SetRotation(Degree10(0));
72 aSuppressGraphicAttr.SetMirrorFlags(BmpMirrorFlags::NONE);
73 aSuppressGraphicAttr.SetTransparency(0);
75 const GraphicObject& rGraphicObject = getGraphicObject();
76 Graphic aTransformedGraphic(rGraphicObject.GetGraphic());
77 const bool isBitmap(GraphicType::Bitmap == aTransformedGraphic.GetType()
78 && !aTransformedGraphic.getVectorGraphicData());
79 const bool isAdjusted(getGraphicAttr().IsAdjusted());
80 const bool isDrawMode(GraphicDrawMode::Standard != getGraphicAttr().GetDrawMode());
82 if (isBitmap && (isAdjusted || isDrawMode))
84 // the pure primitive solution with the color modifiers works well, too, but when
85 // it is a bitmap graphic the old modification currently is faster; so use it here
86 // instead of creating all as in create2DColorModifierEmbeddingsAsNeeded (see below).
87 // Still, crop, rotation, mirroring and transparency is handled by primitives already
88 // (see above).
89 // This could even be done when vector graphic, but we explicitly want to have the
90 // pure primitive solution for this; this will allow vector graphics to stay vector
91 // graphics, independent from the color filtering stuff. This will enhance e.g.
92 // SVG and print quality while reducing data size at the same time.
93 // The other way around the old modifications when only used on already bitmap objects
94 // will not lose any quality.
95 aTransformedGraphic = rGraphicObject.GetTransformedGraphic(&aSuppressGraphicAttr);
97 // reset GraphicAttr after use to not apply double
98 aSuppressGraphicAttr = GraphicAttr();
101 // create sub-content; helper takes care of correct handling of
102 // bitmap, svg or metafile content
103 Primitive2DContainer aRetval;
104 create2DDecompositionOfGraphic(aRetval, aTransformedGraphic, aTransform);
106 if (aRetval.empty())
108 // content is invisible, done
109 return;
112 if (isAdjusted || isDrawMode)
114 // embed to needed ModifiedColorPrimitive2D's if necessary. Do this for
115 // adjustments and draw mode specials
116 aRetval = create2DColorModifierEmbeddingsAsNeeded(
117 aRetval, aSuppressGraphicAttr.GetDrawMode(),
118 std::clamp(aSuppressGraphicAttr.GetLuminance() * 0.01, -1.0, 1.0),
119 std::clamp(aSuppressGraphicAttr.GetContrast() * 0.01, -1.0, 1.0),
120 std::clamp(aSuppressGraphicAttr.GetChannelR() * 0.01, -1.0, 1.0),
121 std::clamp(aSuppressGraphicAttr.GetChannelG() * 0.01, -1.0, 1.0),
122 std::clamp(aSuppressGraphicAttr.GetChannelB() * 0.01, -1.0, 1.0),
123 std::clamp(aSuppressGraphicAttr.GetGamma(), 0.0, 10.0),
124 aSuppressGraphicAttr.IsInvert());
126 if (aRetval.empty())
128 // content is invisible, done
129 return;
133 if (getGraphicAttr().IsTransparent())
135 // check for transparency
136 const double fTransparency(
137 std::clamp(getGraphicAttr().GetTransparency() * (1.0 / 255.0), 0.0, 1.0));
139 if (!basegfx::fTools::equalZero(fTransparency))
141 const Primitive2DReference aUnifiedTransparence(
142 new UnifiedTransparencePrimitive2D(aRetval, fTransparency));
144 aRetval = Primitive2DContainer{ aUnifiedTransparence };
148 if (getGraphicAttr().IsCropped())
150 // check for cropping
151 // calculate scalings between real image size and logic object size. This
152 // is necessary since the crop values are relative to original bitmap size
153 const basegfx::B2DVector aObjectScale(aTransform * basegfx::B2DVector(1.0, 1.0));
154 const basegfx::B2DVector aCropScaleFactor(rGraphicObject.calculateCropScaling(
155 aObjectScale.getX(), aObjectScale.getY(), getGraphicAttr().GetLeftCrop(),
156 getGraphicAttr().GetTopCrop(), getGraphicAttr().GetRightCrop(),
157 getGraphicAttr().GetBottomCrop()));
159 // embed content in cropPrimitive
160 Primitive2DReference xPrimitive(new CropPrimitive2D(
161 aRetval, aTransform, getGraphicAttr().GetLeftCrop() * aCropScaleFactor.getX(),
162 getGraphicAttr().GetTopCrop() * aCropScaleFactor.getY(),
163 getGraphicAttr().GetRightCrop() * aCropScaleFactor.getX(),
164 getGraphicAttr().GetBottomCrop() * aCropScaleFactor.getY()));
166 aRetval = Primitive2DContainer{ xPrimitive };
169 rContainer.insert(rContainer.end(), aRetval.begin(), aRetval.end());
172 GraphicPrimitive2D::GraphicPrimitive2D(const basegfx::B2DHomMatrix& rTransform,
173 const GraphicObject& rGraphicObject,
174 const GraphicAttr& rGraphicAttr)
175 : BufferedDecompositionPrimitive2D()
176 , maTransform(rTransform)
177 , maGraphicObject(rGraphicObject)
178 , maGraphicAttr(rGraphicAttr)
182 GraphicPrimitive2D::GraphicPrimitive2D(const basegfx::B2DHomMatrix& rTransform,
183 const GraphicObject& rGraphicObject)
184 : BufferedDecompositionPrimitive2D()
185 , maTransform(rTransform)
186 , maGraphicObject(rGraphicObject)
187 , maGraphicAttr()
191 bool GraphicPrimitive2D::operator==(const BasePrimitive2D& rPrimitive) const
193 if (BufferedDecompositionPrimitive2D::operator==(rPrimitive))
195 const GraphicPrimitive2D& rCompare = static_cast<const GraphicPrimitive2D&>(rPrimitive);
197 return (getTransform() == rCompare.getTransform()
198 && getGraphicObject() == rCompare.getGraphicObject()
199 && getGraphicAttr() == rCompare.getGraphicAttr());
202 return false;
205 basegfx::B2DRange
206 GraphicPrimitive2D::getB2DRange(const geometry::ViewInformation2D& /*rViewInformation*/) const
208 basegfx::B2DRange aRetval(0.0, 0.0, 1.0, 1.0);
209 aRetval.transform(getTransform());
210 return aRetval;
213 // provide unique ID
214 ImplPrimitive2DIDBlock(GraphicPrimitive2D, PRIMITIVE2D_ID_GRAPHICPRIMITIVE2D)
216 } // end of namespace
218 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */