Line data Source code
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 <drawinglayer/primitive2d/graphicprimitive2d.hxx>
21 : #include <drawinglayer/primitive2d/cropprimitive2d.hxx>
22 : #include <drawinglayer/primitive2d/drawinglayer_primitivetypes2d.hxx>
23 : #include <drawinglayer/primitive2d/maskprimitive2d.hxx>
24 : #include <drawinglayer/primitive2d/graphicprimitivehelper2d.hxx>
25 : #include <drawinglayer/primitive2d/unifiedtransparenceprimitive2d.hxx>
26 : #include <drawinglayer/primitive2d/modifiedcolorprimitive2d.hxx>
27 : #include <basegfx/matrix/b2dhommatrixtools.hxx>
28 : #include <vcl/svapp.hxx>
29 : #include <vcl/outdev.hxx>
30 :
31 : namespace drawinglayer
32 : {
33 : namespace primitive2d
34 : {
35 0 : Primitive2DSequence GraphicPrimitive2D::create2DDecomposition(const geometry::ViewInformation2D&
36 : ) const
37 : {
38 0 : Primitive2DSequence aRetval;
39 :
40 0 : if(255L == getGraphicAttr().GetTransparency())
41 : {
42 : // content is invisible, done
43 0 : return aRetval;
44 : }
45 :
46 : // do not apply mirroring from GraphicAttr to the Metafile by calling
47 : // GetTransformedGraphic, this will try to mirror the Metafile using Scale()
48 : // at the Metafile. This again calls Scale at the single MetaFile actions,
49 : // but this implementation never worked. I reworked that implementations,
50 : // but for security reasons i will try not to use it.
51 0 : basegfx::B2DHomMatrix aTransform(getTransform());
52 :
53 0 : if(getGraphicAttr().IsMirrored())
54 : {
55 : // content needs mirroring
56 0 : const bool bHMirr(getGraphicAttr().GetMirrorFlags() & BMP_MIRROR_HORZ);
57 0 : const bool bVMirr(getGraphicAttr().GetMirrorFlags() & BMP_MIRROR_VERT);
58 :
59 : // mirror by applying negative scale to the unit primitive and
60 : // applying the object transformation on it.
61 0 : aTransform = basegfx::tools::createScaleB2DHomMatrix(
62 : bHMirr ? -1.0 : 1.0,
63 0 : bVMirr ? -1.0 : 1.0);
64 : aTransform.translate(
65 : bHMirr ? 1.0 : 0.0,
66 0 : bVMirr ? 1.0 : 0.0);
67 0 : aTransform = getTransform() * aTransform;
68 : }
69 :
70 : // Get transformed graphic. Suppress rotation and cropping, only filtering is needed
71 : // here (and may be replaced later on). Cropping is handled below as mask primitive (if set).
72 : // Also need to suppress mirroring, it is part of the transformation now (see above).
73 : // Also move transparency handling to embedding to a UnifiedTransparencePrimitive2D; do
74 : // that by remembering original transparency and applying that later if needed
75 0 : GraphicAttr aSuppressGraphicAttr(getGraphicAttr());
76 :
77 0 : aSuppressGraphicAttr.SetCrop(0, 0, 0, 0);
78 0 : aSuppressGraphicAttr.SetRotation(0);
79 0 : aSuppressGraphicAttr.SetMirrorFlags(0);
80 0 : aSuppressGraphicAttr.SetTransparency(0);
81 :
82 0 : const GraphicObject& rGraphicObject = getGraphicObject();
83 0 : Graphic aTransformedGraphic(rGraphicObject.GetGraphic());
84 0 : const bool isBitmap(GRAPHIC_BITMAP == aTransformedGraphic.GetType() && !aTransformedGraphic.getSvgData().get());
85 0 : const bool isAdjusted(getGraphicAttr().IsAdjusted());
86 0 : const bool isDrawMode(GRAPHICDRAWMODE_STANDARD != getGraphicAttr().GetDrawMode());
87 :
88 0 : if(isBitmap && (isAdjusted || isDrawMode))
89 : {
90 : // the pure primitive solution with the color modifiers works well, too, but when
91 : // it is a bitmap graphic the old modification currently is faster; so use it here
92 : // instead of creating all as in create2DColorModifierEmbeddingsAsNeeded (see below).
93 : // Still, crop, rotation, mirroring and transparency is handled by primitives already
94 : // (see above).
95 : // This could even be done when vector graphic, but we explicitely want to have the
96 : // pure primitive solution for this; this will allow vector graphics to stay vector
97 : // geraphics, independent from the color filtering stuff. This will enhance e.g.
98 : // SVG and print quality while reducing data size at the same time.
99 : // The other way around the old modifications when only used on already bitmap objects
100 : // will not loose any quality.
101 0 : aTransformedGraphic = rGraphicObject.GetTransformedGraphic(&aSuppressGraphicAttr);
102 :
103 : // reset GraphicAttr after use to not apply double
104 0 : aSuppressGraphicAttr = GraphicAttr();
105 : }
106 :
107 : // create sub-content; helper takes care of correct handling of
108 : // bitmap, svg or metafile content
109 0 : aRetval = create2DDecompositionOfGraphic(
110 : aTransformedGraphic,
111 0 : aTransform);
112 :
113 0 : if(!aRetval.getLength())
114 : {
115 : // content is invisible, done
116 0 : return aRetval;
117 : }
118 :
119 0 : if(isAdjusted || isDrawMode)
120 : {
121 : // embed to needed ModifiedColorPrimitive2D's if necessary. Do this for
122 : // adjustments and draw mode specials
123 0 : aRetval = create2DColorModifierEmbeddingsAsNeeded(
124 : aRetval,
125 : aSuppressGraphicAttr.GetDrawMode(),
126 0 : basegfx::clamp(aSuppressGraphicAttr.GetLuminance() * 0.01, -1.0, 1.0),
127 0 : basegfx::clamp(aSuppressGraphicAttr.GetContrast() * 0.01, -1.0, 1.0),
128 0 : basegfx::clamp(aSuppressGraphicAttr.GetChannelR() * 0.01, -1.0, 1.0),
129 0 : basegfx::clamp(aSuppressGraphicAttr.GetChannelG() * 0.01, -1.0, 1.0),
130 0 : basegfx::clamp(aSuppressGraphicAttr.GetChannelB() * 0.01, -1.0, 1.0),
131 0 : basegfx::clamp(aSuppressGraphicAttr.GetGamma(), 0.0, 10.0),
132 0 : aSuppressGraphicAttr.IsInvert());
133 :
134 0 : if(!aRetval.getLength())
135 : {
136 : // content is invisible, done
137 0 : return aRetval;
138 : }
139 : }
140 :
141 0 : if(getGraphicAttr().IsTransparent())
142 : {
143 : // check for transparency
144 0 : const double fTransparency(basegfx::clamp(getGraphicAttr().GetTransparency() * (1.0 / 255.0), 0.0, 1.0));
145 :
146 0 : if(!basegfx::fTools::equalZero(fTransparency))
147 : {
148 : const Primitive2DReference aUnifiedTransparence(
149 : new UnifiedTransparencePrimitive2D(
150 : aRetval,
151 0 : fTransparency));
152 :
153 0 : aRetval = Primitive2DSequence(&aUnifiedTransparence, 1);
154 : }
155 : }
156 :
157 0 : if(getGraphicAttr().IsCropped())
158 : {
159 : // check for cropping
160 : // calculate scalings between real image size and logic object size. This
161 : // is necessary since the crop values are relative to original bitmap size
162 0 : const basegfx::B2DVector aObjectScale(aTransform * basegfx::B2DVector(1.0, 1.0));
163 : const basegfx::B2DVector aCropScaleFactor(
164 : rGraphicObject.calculateCropScaling(
165 : aObjectScale.getX(),
166 : aObjectScale.getY(),
167 0 : getGraphicAttr().GetLeftCrop(),
168 0 : getGraphicAttr().GetTopCrop(),
169 0 : getGraphicAttr().GetRightCrop(),
170 0 : getGraphicAttr().GetBottomCrop()));
171 :
172 : // embed content in cropPrimitive
173 : Primitive2DReference xPrimitive(
174 : new CropPrimitive2D(
175 : aRetval,
176 : aTransform,
177 0 : getGraphicAttr().GetLeftCrop() * aCropScaleFactor.getX(),
178 0 : getGraphicAttr().GetTopCrop() * aCropScaleFactor.getY(),
179 0 : getGraphicAttr().GetRightCrop() * aCropScaleFactor.getX(),
180 0 : getGraphicAttr().GetBottomCrop() * aCropScaleFactor.getY()));
181 :
182 0 : aRetval = Primitive2DSequence(&xPrimitive, 1);
183 : }
184 :
185 0 : return aRetval;
186 : }
187 :
188 0 : GraphicPrimitive2D::GraphicPrimitive2D(
189 : const basegfx::B2DHomMatrix& rTransform,
190 : const GraphicObject& rGraphicObject,
191 : const GraphicAttr& rGraphicAttr)
192 : : BufferedDecompositionPrimitive2D(),
193 : maTransform(rTransform),
194 : maGraphicObject(rGraphicObject),
195 0 : maGraphicAttr(rGraphicAttr)
196 : {
197 0 : }
198 :
199 0 : GraphicPrimitive2D::GraphicPrimitive2D(
200 : const basegfx::B2DHomMatrix& rTransform,
201 : const GraphicObject& rGraphicObject)
202 : : BufferedDecompositionPrimitive2D(),
203 : maTransform(rTransform),
204 : maGraphicObject(rGraphicObject),
205 0 : maGraphicAttr()
206 : {
207 0 : }
208 :
209 0 : bool GraphicPrimitive2D::operator==(const BasePrimitive2D& rPrimitive) const
210 : {
211 0 : if(BufferedDecompositionPrimitive2D::operator==(rPrimitive))
212 : {
213 0 : const GraphicPrimitive2D& rCompare = (GraphicPrimitive2D&)rPrimitive;
214 :
215 0 : return (getTransform() == rCompare.getTransform()
216 0 : && getGraphicObject() == rCompare.getGraphicObject()
217 0 : && getGraphicAttr() == rCompare.getGraphicAttr());
218 : }
219 :
220 0 : return false;
221 : }
222 :
223 0 : basegfx::B2DRange GraphicPrimitive2D::getB2DRange(const geometry::ViewInformation2D& /*rViewInformation*/) const
224 : {
225 0 : basegfx::B2DRange aRetval(0.0, 0.0, 1.0, 1.0);
226 0 : aRetval.transform(getTransform());
227 0 : return aRetval;
228 : }
229 :
230 : // provide unique ID
231 0 : ImplPrimitive2DIDBlock(GraphicPrimitive2D, PRIMITIVE2D_ID_GRAPHICPRIMITIVE2D)
232 :
233 : } // end of namespace primitive2d
234 : } // end of namespace drawinglayer
235 :
236 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|