1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 <sdr/contact/viewcontactofgraphic.hxx>
21 #include <sdr/contact/viewobjectcontactofgraphic.hxx>
22 #include <svx/svdograf.hxx>
23 #include <sdgtritm.hxx>
24 #include <svx/sdgluitm.hxx>
25 #include <sdgcoitm.hxx>
26 #include <svx/sdggaitm.hxx>
27 #include <sdginitm.hxx>
28 #include <svx/sdgmoitm.hxx>
29 #include <sdr/primitive2d/sdrattributecreator.hxx>
30 #include <svl/itemset.hxx>
31 #include <tools/debug.hxx>
33 #include <svx/sdgcpitm.hxx>
34 #include <svx/sdr/contact/viewobjectcontact.hxx>
35 #include <svx/sdr/contact/objectcontact.hxx>
36 #include <basegfx/matrix/b2dhommatrix.hxx>
37 #include <sdr/primitive2d/sdrgrafprimitive2d.hxx>
38 #include <vcl/canvastools.hxx>
39 #include <vcl/svapp.hxx>
40 #include <vcl/settings.hxx>
41 #include <basegfx/polygon/b2dpolygontools.hxx>
42 #include <drawinglayer/primitive2d/PolygonHairlinePrimitive2D.hxx>
43 #include <drawinglayer/primitive2d/bitmapprimitive2d.hxx>
44 #include <sdr/primitive2d/sdrtextprimitive2d.hxx>
45 #include <editeng/eeitem.hxx>
46 #include <editeng/colritem.hxx>
47 #include <basegfx/matrix/b2dhommatrixtools.hxx>
48 #include <drawinglayer/primitive2d/sdrdecompositiontools2d.hxx>
49 #include <toolkit/helper/vclunohelper.hxx>
51 #include <bitmaps.hlst>
53 namespace sdr::contact
55 // Create an Object-Specific ViewObjectContact, set ViewContact and
56 // ObjectContact. Always needs to return something.
57 ViewObjectContact
& ViewContactOfGraphic::CreateObjectSpecificViewObjectContact(ObjectContact
& rObjectContact
)
59 ViewObjectContact
* pRetval
= new ViewObjectContactOfGraphic(rObjectContact
, *this);
60 DBG_ASSERT(pRetval
, "ViewContact::CreateObjectSpecificViewObjectContact() failed (!)");
65 ViewContactOfGraphic::ViewContactOfGraphic(SdrGrafObj
& rGrafObj
)
66 : ViewContactOfTextObj(rGrafObj
)
70 ViewContactOfGraphic::~ViewContactOfGraphic()
74 drawinglayer::primitive2d::Primitive2DContainer
ViewContactOfGraphic::createVIP2DSForPresObj(
75 const basegfx::B2DHomMatrix
& rObjectMatrix
,
76 const drawinglayer::attribute::SdrLineFillEffectsTextAttribute
& rAttribute
) const
78 drawinglayer::primitive2d::Primitive2DContainer xRetval
;
79 GraphicObject aEmptyGraphicObject
;
80 GraphicAttr aEmptyGraphicAttr
;
82 // SdrGrafPrimitive2D without content in original size which carries all eventual attributes and texts
83 const drawinglayer::primitive2d::Primitive2DReference
xReferenceA(new drawinglayer::primitive2d::SdrGrafPrimitive2D(
88 xRetval
= drawinglayer::primitive2d::Primitive2DContainer
{ xReferenceA
};
90 // SdrGrafPrimitive2D with content (which is the preview graphic) scaled to smaller size and
92 basegfx::B2DHomMatrix aSmallerMatrix
;
94 // #i94431# for some reason, i forgot to take the PrefMapMode of the graphic
95 // into account. Since EmptyPresObj's are only used in Draw/Impress, it is
96 // safe to assume 100th mm as target.
97 Size
aPrefSize(GetGrafObject().GetGrafPrefSize());
99 if(MapUnit::MapPixel
== GetGrafObject().GetGrafPrefMapMode().GetMapUnit())
101 aPrefSize
= Application::GetDefaultDevice()->PixelToLogic(aPrefSize
, MapMode(MapUnit::Map100thMM
));
105 aPrefSize
= OutputDevice::LogicToLogic(aPrefSize
, GetGrafObject().GetGrafPrefMapMode(), MapMode(MapUnit::Map100thMM
));
108 // decompose object matrix to get single values
109 basegfx::B2DVector aScale
, aTranslate
;
110 double fRotate
, fShearX
;
111 rObjectMatrix
.decompose(aScale
, aTranslate
, fRotate
, fShearX
);
113 const double fOffsetX((aScale
.getX() - aPrefSize
.getWidth()) / 2.0);
114 const double fOffsetY((aScale
.getY() - aPrefSize
.getHeight()) / 2.0);
116 if(basegfx::fTools::moreOrEqual(fOffsetX
, 0.0) && basegfx::fTools::moreOrEqual(fOffsetY
, 0.0))
118 // create the EmptyPresObj fallback visualisation. The fallback graphic
119 // is already provided in rGraphicObject in this case, use it
120 aSmallerMatrix
= basegfx::utils::createScaleTranslateB2DHomMatrix(aPrefSize
.getWidth(), aPrefSize
.getHeight(), fOffsetX
, fOffsetY
);
121 aSmallerMatrix
= basegfx::utils::createShearXRotateTranslateB2DHomMatrix(fShearX
, fRotate
, aTranslate
)
124 const GraphicObject
& rGraphicObject
= GetGrafObject().GetGraphicObject();
125 const GraphicAttr aLocalGrafInfo
;
126 const drawinglayer::primitive2d::Primitive2DReference
xReferenceB(new drawinglayer::primitive2d::SdrGrafPrimitive2D(
128 drawinglayer::attribute::SdrLineFillEffectsTextAttribute(),
132 xRetval
.push_back(xReferenceB
);
138 drawinglayer::primitive2d::Primitive2DContainer
ViewContactOfGraphic::createVIP2DSForDraft(
139 const basegfx::B2DHomMatrix
& rObjectMatrix
,
140 const drawinglayer::attribute::SdrLineFillEffectsTextAttribute
& rAttribute
) const
142 drawinglayer::primitive2d::Primitive2DContainer xRetval
;
143 GraphicObject aEmptyGraphicObject
;
144 GraphicAttr aEmptyGraphicAttr
;
146 // SdrGrafPrimitive2D without content in original size which carries all eventual attributes and texts
147 const drawinglayer::primitive2d::Primitive2DReference
xReferenceA(new drawinglayer::primitive2d::SdrGrafPrimitive2D(
152 xRetval
= drawinglayer::primitive2d::Primitive2DContainer
{ xReferenceA
};
154 if(rAttribute
.getLine().isDefault())
156 // create a surrounding frame when no linestyle given
157 const Color
aColor(Application::GetSettings().GetStyleSettings().GetShadowColor());
158 const basegfx::BColor
aBColor(aColor
.getBColor());
159 basegfx::B2DPolygon
aOutline(basegfx::utils::createUnitPolygon());
160 aOutline
.transform(rObjectMatrix
);
163 drawinglayer::primitive2d::Primitive2DReference(
164 new drawinglayer::primitive2d::PolygonHairlinePrimitive2D(
169 // decompose object matrix to get single values
170 basegfx::B2DVector aScale
, aTranslate
;
171 double fRotate
, fShearX
;
172 rObjectMatrix
.decompose(aScale
, aTranslate
, fRotate
, fShearX
);
174 // define a distance value, used for distance from bitmap to borders and from bitmap
175 // to text, too (2 mm)
176 const double fDistance(200.0);
178 // consume borders from values
179 aScale
.setX(std::max(0.0, aScale
.getX() - (2.0 * fDistance
)));
180 aScale
.setY(std::max(0.0, aScale
.getY() - (2.0 * fDistance
)));
181 aTranslate
.setX(aTranslate
.getX() + fDistance
);
182 aTranslate
.setY(aTranslate
.getY() + fDistance
);
184 // draw a draft bitmap
185 const BitmapEx
aDraftBitmap(BMAP_GrafikEi
);
187 if(!aDraftBitmap
.IsEmpty())
189 Size
aPrefSize(aDraftBitmap
.GetPrefSize());
191 if(MapUnit::MapPixel
== aDraftBitmap
.GetPrefMapMode().GetMapUnit())
193 aPrefSize
= Application::GetDefaultDevice()->PixelToLogic(aDraftBitmap
.GetSizePixel(), MapMode(MapUnit::Map100thMM
));
197 aPrefSize
= OutputDevice::LogicToLogic(aPrefSize
, aDraftBitmap
.GetPrefMapMode(), MapMode(MapUnit::Map100thMM
));
200 const double fBitmapScaling(2.0);
201 const double fWidth(aPrefSize
.getWidth() * fBitmapScaling
);
202 const double fHeight(aPrefSize
.getHeight() * fBitmapScaling
);
204 if(basegfx::fTools::more(fWidth
, 1.0)
205 && basegfx::fTools::more(fHeight
, 1.0)
206 && basegfx::fTools::lessOrEqual(fWidth
, aScale
.getX())
207 && basegfx::fTools::lessOrEqual(fHeight
, aScale
.getY()))
209 const basegfx::B2DHomMatrix
aBitmapMatrix(basegfx::utils::createScaleShearXRotateTranslateB2DHomMatrix(
210 fWidth
, fHeight
, fShearX
, fRotate
, aTranslate
.getX(), aTranslate
.getY()));
213 drawinglayer::primitive2d::Primitive2DReference(
214 new drawinglayer::primitive2d::BitmapPrimitive2D(
218 // consume bitmap size in X
219 aScale
.setX(std::max(0.0, aScale
.getX() - (fWidth
+ fDistance
)));
220 aTranslate
.setX(aTranslate
.getX() + fWidth
+ fDistance
);
224 // Build the text for the draft object
225 OUString aDraftText
= GetGrafObject().GetFileName();
227 if (aDraftText
.isEmpty())
229 aDraftText
= GetGrafObject().GetName() + " ...";
232 if (!aDraftText
.isEmpty())
234 // #i103255# Goal is to produce TextPrimitives which hold the given text as
235 // BlockText in the available space. It would be very tricky to do
236 // an own word wrap/line layout here.
237 // Using SdrBlockTextPrimitive2D OTOH is critical since it internally
238 // uses the SdrObject it references. To solve this, create a temp
239 // SdrObject with Attributes and Text, generate a SdrBlockTextPrimitive2D
240 // directly and immediately decompose it. After that, it is no longer
241 // needed and can be deleted.
243 // create temp RectObj as TextObj and set needed attributes
244 rtl::Reference
<SdrRectObj
> pRectObj(new SdrRectObj(GetGrafObject().getSdrModelFromSdrObject(), SdrObjKind::Text
));
245 pRectObj
->NbcSetText(aDraftText
);
246 pRectObj
->SetMergedItem(SvxColorItem(COL_LIGHTRED
, EE_CHAR_COLOR
));
248 // get SdrText and OPO
249 SdrText
* pSdrText(pRectObj
->getText(0));
250 OutlinerParaObject
* pOPO(pRectObj
->GetOutlinerParaObject());
254 // directly use the remaining space as TextRangeTransform
255 const basegfx::B2DHomMatrix
aTextRangeTransform(basegfx::utils::createScaleShearXRotateTranslateB2DHomMatrix(
256 aScale
, fShearX
, fRotate
, aTranslate
));
258 // directly create temp SdrBlockTextPrimitive2D
259 rtl::Reference
< drawinglayer::primitive2d::SdrBlockTextPrimitive2D
> xBlockTextPrimitive(new drawinglayer::primitive2d::SdrBlockTextPrimitive2D(
263 SDRTEXTHORZADJUST_LEFT
,
264 SDRTEXTVERTADJUST_TOP
,
270 // decompose immediately with neutral ViewInformation. This will
271 // layout the text to more simple TextPrimitives from drawinglayer
272 const drawinglayer::geometry::ViewInformation2D aViewInformation2D
;
273 xBlockTextPrimitive
->get2DDecomposition(xRetval
, aViewInformation2D
);
280 void ViewContactOfGraphic::createViewIndependentPrimitive2DSequence(drawinglayer::primitive2d::Primitive2DDecompositionVisitor
& rVisitor
) const
282 const SfxItemSet
& rItemSet
= GetGrafObject().GetMergedItemSet();
284 // create and fill GraphicAttr
285 GraphicAttr aLocalGrafInfo
;
286 const sal_uInt16
nTrans(rItemSet
.Get(SDRATTR_GRAFTRANSPARENCE
).GetValue());
287 const SdrGrafCropItem
& rCrop(rItemSet
.Get(SDRATTR_GRAFCROP
));
288 aLocalGrafInfo
.SetLuminance(rItemSet
.Get(SDRATTR_GRAFLUMINANCE
).GetValue());
289 aLocalGrafInfo
.SetContrast(rItemSet
.Get(SDRATTR_GRAFCONTRAST
).GetValue());
290 aLocalGrafInfo
.SetChannelR(rItemSet
.Get(SDRATTR_GRAFRED
).GetValue());
291 aLocalGrafInfo
.SetChannelG(rItemSet
.Get(SDRATTR_GRAFGREEN
).GetValue());
292 aLocalGrafInfo
.SetChannelB(rItemSet
.Get(SDRATTR_GRAFBLUE
).GetValue());
293 aLocalGrafInfo
.SetGamma(rItemSet
.Get(SDRATTR_GRAFGAMMA
).GetValue() * 0.01);
294 aLocalGrafInfo
.SetAlpha(255 - static_cast<sal_uInt8
>(::basegfx::fround(std::min(nTrans
, sal_uInt16(100)) * 2.55)));
295 aLocalGrafInfo
.SetInvert(rItemSet
.Get(SDRATTR_GRAFINVERT
).GetValue());
296 aLocalGrafInfo
.SetDrawMode(rItemSet
.Get(SDRATTR_GRAFMODE
).GetValue());
297 aLocalGrafInfo
.SetCrop(rCrop
.GetLeft(), rCrop
.GetTop(), rCrop
.GetRight(), rCrop
.GetBottom());
299 // we have content if graphic is not completely transparent
300 const bool bHasContent(0 != aLocalGrafInfo
.GetAlpha());
301 drawinglayer::attribute::SdrLineFillEffectsTextAttribute
aAttribute(
302 drawinglayer::primitive2d::createNewSdrLineFillEffectsTextAttribute(
304 GetGrafObject().getText(0),
307 // take unrotated snap rect for position and size. Directly use model data, not getBoundRect() or getSnapRect()
308 // which will use the primitive data we just create in the near future
309 const ::basegfx::B2DRange aObjectRange
= vcl::unotools::b2DRectangleFromRectangle(GetGrafObject().GetGeoRect());
311 // look for mirroring
312 const GeoStat
& rGeoStat(GetGrafObject().GetGeoStat());
313 const Degree100
nRotationAngle(rGeoStat
.nRotationAngle
);
314 const bool bMirrored(GetGrafObject().IsMirrored());
317 aLocalGrafInfo
.SetMirrorFlags(BmpMirrorFlags::Horizontal
);
319 // fill object matrix
320 const double fShearX(-rGeoStat
.mfTanShearAngle
);
321 const double fRotate(nRotationAngle
? toRadians(36000_deg100
- nRotationAngle
) : 0.0);
322 const basegfx::B2DHomMatrix
aObjectMatrix(basegfx::utils::createScaleShearXRotateTranslateB2DHomMatrix(
323 aObjectRange
.getWidth(), aObjectRange
.getHeight(),
325 aObjectRange
.getMinX(), aObjectRange
.getMinY()));
327 // get the current, unchanged graphic object from SdrGrafObj
328 const GraphicObject
& rGraphicObject
= GetGrafObject().GetGraphicObject();
330 if(visualisationUsesPresObj())
332 // it's an EmptyPresObj, create the SdrGrafPrimitive2D without content and another scaled one
333 // with the content which is the placeholder graphic
334 rVisitor
.visit(createVIP2DSForPresObj(aObjectMatrix
, aAttribute
));
336 #ifndef IOS // Enforce swap-in for tiled rendering for now, while we have no delayed updating mechanism
337 else if(visualisationUsesDraft())
339 // #i102380# The graphic is swapped out. To not force a swap-in here, there is a mechanism
340 // which shows a swapped-out-visualisation (which gets created here now) and an asynchronous
341 // visual update mechanism for swapped-out graphics when they were loaded (see AsynchGraphicLoadingEvent
342 // and ViewObjectContactOfGraphic implementation). Not forcing the swap-in here allows faster
343 // (non-blocking) processing here and thus in the effect e.g. fast scrolling through pages
344 rVisitor
.visit(createVIP2DSForDraft(aObjectMatrix
, aAttribute
));
349 // create primitive. Info: Calling the copy-constructor of GraphicObject in this
350 // SdrGrafPrimitive2D constructor will force a full swap-in of the graphic
351 const drawinglayer::primitive2d::Primitive2DReference
xReference(
352 new drawinglayer::primitive2d::SdrGrafPrimitive2D(
358 rVisitor
.visit(xReference
);
361 // always append an invisible outline for the cases where no visible content exists
363 drawinglayer::primitive2d::createHiddenGeometryPrimitives2D(
367 bool ViewContactOfGraphic::visualisationUsesPresObj() const
369 return GetGrafObject().IsEmptyPresObj();
372 bool ViewContactOfGraphic::visualisationUsesDraft() const
374 // no draft when already PresObj
375 if(visualisationUsesPresObj())
378 // draft when swapped out
379 const GraphicObject
& rGraphicObject
= GetGrafObject().GetGraphicObject();
381 // draft when no graphic
382 return GraphicType::NONE
== rGraphicObject
.GetType() || GraphicType::Default
== rGraphicObject
.GetType();
385 } // end of namespace
387 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */