LibreOffice Module drawinglayer (master) 1
graphicprimitive2d.cxx
Go to the documentation of this file.
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 <sal/config.h>
21
22#include <algorithm>
23
30#include <utility>
31
33{
35 const geometry::ViewInformation2D&) const
36{
37 if (0 == getGraphicAttr().GetAlpha())
38 {
39 // content is invisible, done
40 return;
41 }
42
43 // do not apply mirroring from GraphicAttr to the Metafile by calling
44 // GetTransformedGraphic, this will try to mirror the Metafile using Scale()
45 // at the Metafile. This again calls Scale at the single MetaFile actions,
46 // but this implementation never worked. I reworked that implementations,
47 // but for security reasons i will try not to use it.
49
51 {
52 // content needs mirroring
53 const bool bHMirr(getGraphicAttr().GetMirrorFlags() & BmpMirrorFlags::Horizontal);
54 const bool bVMirr(getGraphicAttr().GetMirrorFlags() & BmpMirrorFlags::Vertical);
55
56 // mirror by applying negative scale to the unit primitive and
57 // applying the object transformation on it.
58 aTransform
59 = basegfx::utils::createScaleB2DHomMatrix(bHMirr ? -1.0 : 1.0, bVMirr ? -1.0 : 1.0);
60 aTransform.translate(bHMirr ? 1.0 : 0.0, bVMirr ? 1.0 : 0.0);
61 aTransform = getTransform() * aTransform;
62 }
63
64 // Get transformed graphic. Suppress rotation and cropping, only filtering is needed
65 // here (and may be replaced later on). Cropping is handled below as mask primitive (if set).
66 // Also need to suppress mirroring, it is part of the transformation now (see above).
67 // Also move transparency handling to embedding to a UnifiedTransparencePrimitive2D; do
68 // that by remembering original transparency and applying that later if needed
69 GraphicAttr aSuppressGraphicAttr(getGraphicAttr());
70
71 aSuppressGraphicAttr.SetCrop(0, 0, 0, 0);
72 aSuppressGraphicAttr.SetRotation(0_deg10);
73 aSuppressGraphicAttr.SetMirrorFlags(BmpMirrorFlags::NONE);
74 aSuppressGraphicAttr.SetAlpha(255);
75
76 const GraphicObject& rGraphicObject = getGraphicObject();
77 Graphic aTransformedGraphic(rGraphicObject.GetGraphic());
78 const bool isBitmap(GraphicType::Bitmap == aTransformedGraphic.GetType()
79 && !aTransformedGraphic.getVectorGraphicData());
80 const bool isAdjusted(getGraphicAttr().IsAdjusted());
81 const bool isDrawMode(GraphicDrawMode::Standard != getGraphicAttr().GetDrawMode());
82
83 if (isBitmap && (isAdjusted || isDrawMode))
84 {
85 // the pure primitive solution with the color modifiers works well, too, but when
86 // it is a bitmap graphic the old modification currently is faster; so use it here
87 // instead of creating all as in create2DColorModifierEmbeddingsAsNeeded (see below).
88 // Still, crop, rotation, mirroring and transparency is handled by primitives already
89 // (see above).
90 // This could even be done when vector graphic, but we explicitly want to have the
91 // pure primitive solution for this; this will allow vector graphics to stay vector
92 // graphics, independent from the color filtering stuff. This will enhance e.g.
93 // SVG and print quality while reducing data size at the same time.
94 // The other way around the old modifications when only used on already bitmap objects
95 // will not lose any quality.
96 aTransformedGraphic = rGraphicObject.GetTransformedGraphic(&aSuppressGraphicAttr);
97
98 // reset GraphicAttr after use to not apply double
99 aSuppressGraphicAttr = GraphicAttr();
100 }
101
102 // create sub-content; helper takes care of correct handling of
103 // bitmap, svg or metafile content
104 Primitive2DContainer aRetval;
105 create2DDecompositionOfGraphic(aRetval, aTransformedGraphic, aTransform);
106
107 if (aRetval.empty())
108 {
109 // content is invisible, done
110 return;
111 }
112
113 if (isAdjusted || isDrawMode)
114 {
115 // embed to needed ModifiedColorPrimitive2D's if necessary. Do this for
116 // adjustments and draw mode specials
118 std::move(aRetval), aSuppressGraphicAttr.GetDrawMode(),
119 std::clamp(aSuppressGraphicAttr.GetLuminance() * 0.01, -1.0, 1.0),
120 std::clamp(aSuppressGraphicAttr.GetContrast() * 0.01, -1.0, 1.0),
121 std::clamp(aSuppressGraphicAttr.GetChannelR() * 0.01, -1.0, 1.0),
122 std::clamp(aSuppressGraphicAttr.GetChannelG() * 0.01, -1.0, 1.0),
123 std::clamp(aSuppressGraphicAttr.GetChannelB() * 0.01, -1.0, 1.0),
124 std::clamp(aSuppressGraphicAttr.GetGamma(), 0.0, 10.0),
125 aSuppressGraphicAttr.IsInvert());
126
127 if (aRetval.empty())
128 {
129 // content is invisible, done
130 return;
131 }
132 }
133
134 if (getGraphicAttr().IsTransparent())
135 {
136 // check for transparency
137 const double fTransparency(
138 std::clamp((255 - getGraphicAttr().GetAlpha()) * (1.0 / 255.0), 0.0, 1.0));
139
140 if (!basegfx::fTools::equalZero(fTransparency))
141 {
142 Primitive2DReference aUnifiedTransparence(
143 new UnifiedTransparencePrimitive2D(std::move(aRetval), fTransparency));
144
145 aRetval = Primitive2DContainer{ aUnifiedTransparence };
146 }
147 }
148
149 if (getGraphicAttr().IsCropped())
150 {
151 // check for cropping
152 // calculate scalings between real image size and logic object size. This
153 // is necessary since the crop values are relative to original bitmap size
154 const basegfx::B2DVector aObjectScale(aTransform * basegfx::B2DVector(1.0, 1.0));
155 const basegfx::B2DVector aCropScaleFactor(rGraphicObject.calculateCropScaling(
156 aObjectScale.getX(), aObjectScale.getY(), getGraphicAttr().GetLeftCrop(),
157 getGraphicAttr().GetTopCrop(), getGraphicAttr().GetRightCrop(),
158 getGraphicAttr().GetBottomCrop()));
159
160 // embed content in cropPrimitive
161 Primitive2DReference xPrimitive(
162 new CropPrimitive2D(std::move(aRetval), aTransform,
163 getGraphicAttr().GetLeftCrop() * aCropScaleFactor.getX(),
164 getGraphicAttr().GetTopCrop() * aCropScaleFactor.getY(),
165 getGraphicAttr().GetRightCrop() * aCropScaleFactor.getX(),
166 getGraphicAttr().GetBottomCrop() * aCropScaleFactor.getY()));
167
168 aRetval = Primitive2DContainer{ xPrimitive };
169 }
170
171 rContainer.append(std::move(aRetval));
172}
173
175 const GraphicObject& rGraphicObject,
176 const GraphicAttr& rGraphicAttr)
177 : maTransform(std::move(aTransform))
178 , maGraphicObject(rGraphicObject)
179 , maGraphicAttr(rGraphicAttr)
180{
181}
182
184 const GraphicObject& rGraphicObject)
185 : maTransform(std::move(aTransform))
186 , maGraphicObject(rGraphicObject)
187{
188}
189
191{
192 if (BufferedDecompositionPrimitive2D::operator==(rPrimitive))
193 {
194 const GraphicPrimitive2D& rCompare = static_cast<const GraphicPrimitive2D&>(rPrimitive);
195
196 return (getTransform() == rCompare.getTransform()
197 && getGraphicObject() == rCompare.getGraphicObject()
198 && getGraphicAttr() == rCompare.getGraphicAttr());
199 }
200
201 return false;
202}
203
206{
207 basegfx::B2DRange aRetval(0.0, 0.0, 1.0, 1.0);
208 aRetval.transform(getTransform());
209 return aRetval;
210}
211
212// provide unique ID
214{
216}
217
218} // end of namespace
219
220/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
short GetContrast() const
short GetChannelB() const
double GetGamma() const
void SetRotation(Degree10 nRotate10)
void SetMirrorFlags(BmpMirrorFlags nMirrFlags)
GraphicDrawMode GetDrawMode() const
short GetChannelG() const
short GetChannelR() const
void SetCrop(tools::Long nLeft_100TH_MM, tools::Long nTop_100TH_MM, tools::Long nRight_100TH_MM, tools::Long nBottom_100TH_MM)
bool IsInvert() const
void SetAlpha(sal_uInt8 cAlpha)
short GetLuminance() const
basegfx::B2DVector calculateCropScaling(double fWidth, double fHeight, double fLeftCrop, double fTopCrop, double fRightCrop, double fBottomCrop) const
const Graphic & GetGraphic() const
Graphic GetTransformedGraphic(const Size &rDestSize, const MapMode &rDestMap, const GraphicAttr &rAttr) const
GraphicType GetType() const
const std::shared_ptr< VectorGraphicData > & getVectorGraphicData() const
void translate(double fX, double fY)
BASEGFX_DLLPUBLIC void transform(const B2DHomMatrix &rMatrix)
TYPE getX() const
TYPE getY() const
const basegfx::B2DHomMatrix & getTransform() const
data read access
virtual basegfx::B2DRange getB2DRange(const geometry::ViewInformation2D &rViewInformation) const override
get range
virtual void create2DDecomposition(Primitive2DContainer &rContainer, const geometry::ViewInformation2D &rViewInformation) const override
local decomposition
virtual sal_uInt32 getPrimitive2DID() const override
provide unique ID
GraphicPrimitive2D(basegfx::B2DHomMatrix aTransform, const GraphicObject &rGraphicObject, const GraphicAttr &rGraphicAttr)
constructor(s)
virtual bool operator==(const BasePrimitive2D &rPrimitive) const override
compare operator
#define PRIMITIVE2D_ID_GRAPHICPRIMITIVE2D
basegfx::B2DHomMatrix maTransform
the geometric definition
bool equalZero(const T &rfVal)
B2DHomMatrix createScaleB2DHomMatrix(double fScaleX, double fScaleY)
void create2DDecompositionOfGraphic(Primitive2DContainer &rContainer, const Graphic &rGraphic, const basegfx::B2DHomMatrix &rTransform)
Helper method with supports decomposing a Graphic with all possible contents to lower level primitive...
Primitive2DContainer create2DColorModifierEmbeddingsAsNeeded(Primitive2DContainer &&rChildren, GraphicDrawMode aGraphicDrawMode, double fLuminance, double fContrast, double fRed, double fGreen, double fBlue, double fGamma, bool bInvert)
Helper to embed given sequence of primitives to evtl.
bool IsMirrored(const tools::Long nPageLeftMargin, const tools::Long nPageRightMargin, const tools::Long nPageTopMargin, const tools::Long nPageBottomMargin, bool bMirrored)