LibreOffice Module svx (master)  1
viewcontactofgraphic.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 
22 #include <svx/svdograf.hxx>
23 #include <svx/sdgtritm.hxx>
24 #include <svx/sdgluitm.hxx>
25 #include <svx/sdgcoitm.hxx>
26 #include <svx/sdggaitm.hxx>
27 #include <svx/sdginitm.hxx>
28 #include <svx/sdgmoitm.hxx>
30 #include <svl/itemset.hxx>
31 #include <tools/debug.hxx>
32 
33 #include <svx/sdgcpitm.hxx>
39 #include <vcl/canvastools.hxx>
40 #include <vcl/svapp.hxx>
41 #include <vcl/settings.hxx>
49 #include <editeng/eeitem.hxx>
50 #include <editeng/colritem.hxx>
53 
54 #include <bitmaps.hlst>
55 
56 namespace sdr::contact
57 {
58  // Create an Object-Specific ViewObjectContact, set ViewContact and
59  // ObjectContact. Always needs to return something.
61  {
62  ViewObjectContact* pRetval = new ViewObjectContactOfGraphic(rObjectContact, *this);
63  DBG_ASSERT(pRetval, "ViewContact::CreateObjectSpecificViewObjectContact() failed (!)");
64 
65  return *pRetval;
66  }
67 
69  : ViewContactOfTextObj(rGrafObj)
70  {
71  }
72 
74  {
75  }
76 
78  const basegfx::B2DHomMatrix& rObjectMatrix,
80  {
82  GraphicObject aEmptyGraphicObject;
83  GraphicAttr aEmptyGraphicAttr;
84 
85  // SdrGrafPrimitive2D without content in original size which carries all eventual attributes and texts
87  rObjectMatrix,
88  rAttribute,
89  aEmptyGraphicObject,
90  aEmptyGraphicAttr));
91  xRetval = drawinglayer::primitive2d::Primitive2DContainer { xReferenceA };
92 
93  // SdrGrafPrimitive2D with content (which is the preview graphic) scaled to smaller size and
94  // without attributes
95  basegfx::B2DHomMatrix aSmallerMatrix;
96 
97  // #i94431# for some reason, i forgot to take the PrefMapMode of the graphic
98  // into account. Since EmptyPresObj's are only used in Draw/Impress, it is
99  // safe to assume 100th mm as target.
100  Size aPrefSize(GetGrafObject().GetGrafPrefSize());
101 
102  if(MapUnit::MapPixel == GetGrafObject().GetGrafPrefMapMode().GetMapUnit())
103  {
104  aPrefSize = Application::GetDefaultDevice()->PixelToLogic(aPrefSize, MapMode(MapUnit::Map100thMM));
105  }
106  else
107  {
108  aPrefSize = OutputDevice::LogicToLogic(aPrefSize, GetGrafObject().GetGrafPrefMapMode(), MapMode(MapUnit::Map100thMM));
109  }
110 
111  // decompose object matrix to get single values
112  basegfx::B2DVector aScale, aTranslate;
113  double fRotate, fShearX;
114  rObjectMatrix.decompose(aScale, aTranslate, fRotate, fShearX);
115 
116  const double fOffsetX((aScale.getX() - aPrefSize.getWidth()) / 2.0);
117  const double fOffsetY((aScale.getY() - aPrefSize.getHeight()) / 2.0);
118 
119  if(basegfx::fTools::moreOrEqual(fOffsetX, 0.0) && basegfx::fTools::moreOrEqual(fOffsetY, 0.0))
120  {
121  // create the EmptyPresObj fallback visualisation. The fallback graphic
122  // is already provided in rGraphicObject in this case, use it
123  aSmallerMatrix = basegfx::utils::createScaleTranslateB2DHomMatrix(aPrefSize.getWidth(), aPrefSize.getHeight(), fOffsetX, fOffsetY);
124  aSmallerMatrix = basegfx::utils::createShearXRotateTranslateB2DHomMatrix(fShearX, fRotate, aTranslate)
125  * aSmallerMatrix;
126 
127  const GraphicObject& rGraphicObject = GetGrafObject().GetGraphicObject();
128  const GraphicAttr aLocalGrafInfo;
130  aSmallerMatrix,
132  rGraphicObject,
133  aLocalGrafInfo));
134 
135  xRetval.push_back(xReferenceB);
136  }
137 
138  return xRetval;
139  }
140 
142  const basegfx::B2DHomMatrix& rObjectMatrix,
144  {
146  GraphicObject aEmptyGraphicObject;
147  GraphicAttr aEmptyGraphicAttr;
148 
149  // SdrGrafPrimitive2D without content in original size which carries all eventual attributes and texts
151  rObjectMatrix,
152  rAttribute,
153  aEmptyGraphicObject,
154  aEmptyGraphicAttr));
155  xRetval = drawinglayer::primitive2d::Primitive2DContainer { xReferenceA };
156 
157  if(rAttribute.getLine().isDefault())
158  {
159  // create a surrounding frame when no linestyle given
160  const Color aColor(Application::GetSettings().GetStyleSettings().GetShadowColor());
161  const basegfx::BColor aBColor(aColor.getBColor());
163  aOutline.transform(rObjectMatrix);
164 
165  xRetval.push_back(
168  aOutline,
169  aBColor)));
170  }
171 
172  // decompose object matrix to get single values
173  basegfx::B2DVector aScale, aTranslate;
174  double fRotate, fShearX;
175  rObjectMatrix.decompose(aScale, aTranslate, fRotate, fShearX);
176 
177  // define a distance value, used for distance from bitmap to borders and from bitmap
178  // to text, too (2 mm)
179  const double fDistance(200.0);
180 
181  // consume borders from values
182  aScale.setX(std::max(0.0, aScale.getX() - (2.0 * fDistance)));
183  aScale.setY(std::max(0.0, aScale.getY() - (2.0 * fDistance)));
184  aTranslate.setX(aTranslate.getX() + fDistance);
185  aTranslate.setY(aTranslate.getY() + fDistance);
186 
187  // draw a draft bitmap
188  const BitmapEx aDraftBitmap(BMAP_GrafikEi);
189 
190  if(!aDraftBitmap.IsEmpty())
191  {
192  Size aPrefSize(aDraftBitmap.GetPrefSize());
193 
194  if(MapUnit::MapPixel == aDraftBitmap.GetPrefMapMode().GetMapUnit())
195  {
196  aPrefSize = Application::GetDefaultDevice()->PixelToLogic(aDraftBitmap.GetSizePixel(), MapMode(MapUnit::Map100thMM));
197  }
198  else
199  {
200  aPrefSize = OutputDevice::LogicToLogic(aPrefSize, aDraftBitmap.GetPrefMapMode(), MapMode(MapUnit::Map100thMM));
201  }
202 
203  const double fBitmapScaling(2.0);
204  const double fWidth(aPrefSize.getWidth() * fBitmapScaling);
205  const double fHeight(aPrefSize.getHeight() * fBitmapScaling);
206 
207  if(basegfx::fTools::more(fWidth, 1.0)
208  && basegfx::fTools::more(fHeight, 1.0)
209  && basegfx::fTools::lessOrEqual(fWidth, aScale.getX())
210  && basegfx::fTools::lessOrEqual(fHeight, aScale.getY()))
211  {
213  fWidth, fHeight, fShearX, fRotate, aTranslate.getX(), aTranslate.getY()));
214 
215  xRetval.push_back(
218  aDraftBitmap, aBitmapMatrix)));
219 
220  // consume bitmap size in X
221  aScale.setX(std::max(0.0, aScale.getX() - (fWidth + fDistance)));
222  aTranslate.setX(aTranslate.getX() + fWidth + fDistance);
223  }
224  }
225 
226  // Build the text for the draft object
227  OUString aDraftText = GetGrafObject().GetFileName();
228 
229  if (aDraftText.isEmpty())
230  {
231  aDraftText = GetGrafObject().GetName() + " ...";
232  }
233 
234  if (!aDraftText.isEmpty())
235  {
236  // #i103255# Goal is to produce TextPrimitives which hold the given text as
237  // BlockText in the available space. It would be very tricky to do
238  // an own word wrap/line layout here.
239  // Using SdrBlockTextPrimitive2D OTOH is critical since it internally
240  // uses the SdrObject it references. To solve this, create a temp
241  // SdrObject with Attributes and Text, generate a SdrBlockTextPrimitive2D
242  // directly and immediately decompose it. After that, it is no longer
243  // needed and can be deleted.
244 
245  // create temp RectObj as TextObj and set needed attributes
246  SdrRectObj* pRectObj(new SdrRectObj(GetGrafObject().getSdrModelFromSdrObject(), OBJ_TEXT));
247  pRectObj->NbcSetText(aDraftText);
249 
250  // get SdrText and OPO
251  SdrText* pSdrText(pRectObj->getText(0));
252  OutlinerParaObject* pOPO(pRectObj->GetOutlinerParaObject());
253 
254  if(pSdrText && pOPO)
255  {
256  // directly use the remaining space as TextRangeTransform
258  aScale, fShearX, fRotate, aTranslate));
259 
260  // directly create temp SdrBlockTextPrimitive2D
262  pSdrText,
263  *pOPO,
264  aTextRangeTransform,
267  false,
268  false,
269  false,
270  false));
271 
272  // decompose immediately with neutral ViewInformation. This will
273  // layout the text to more simple TextPrimitives from drawinglayer
274  const drawinglayer::geometry::ViewInformation2D aViewInformation2D;
275  xBlockTextPrimitive->get2DDecomposition(xRetval, aViewInformation2D);
276  }
277 
278  // always use SdrObject::Free(...) for SdrObjects (!)
279  SdrObject* pTemp(pRectObj);
280  SdrObject::Free(pTemp);
281  }
282 
283  return xRetval;
284  }
285 
287  {
289  const SfxItemSet& rItemSet = GetGrafObject().GetMergedItemSet();
290 
291  // create and fill GraphicAttr
292  GraphicAttr aLocalGrafInfo;
293  const sal_uInt16 nTrans(rItemSet.Get(SDRATTR_GRAFTRANSPARENCE).GetValue());
294  const SdrGrafCropItem& rCrop(rItemSet.Get(SDRATTR_GRAFCROP));
295  aLocalGrafInfo.SetLuminance(rItemSet.Get(SDRATTR_GRAFLUMINANCE).GetValue());
296  aLocalGrafInfo.SetContrast(rItemSet.Get(SDRATTR_GRAFCONTRAST).GetValue());
297  aLocalGrafInfo.SetChannelR(rItemSet.Get(SDRATTR_GRAFRED).GetValue());
298  aLocalGrafInfo.SetChannelG(rItemSet.Get(SDRATTR_GRAFGREEN).GetValue());
299  aLocalGrafInfo.SetChannelB(rItemSet.Get(SDRATTR_GRAFBLUE).GetValue());
300  aLocalGrafInfo.SetGamma(rItemSet.Get(SDRATTR_GRAFGAMMA).GetValue() * 0.01);
301  aLocalGrafInfo.SetTransparency(static_cast<sal_uInt8>(::basegfx::fround(std::min(nTrans, sal_uInt16(100)) * 2.55)));
302  aLocalGrafInfo.SetInvert(rItemSet.Get(SDRATTR_GRAFINVERT).GetValue());
303  aLocalGrafInfo.SetDrawMode(rItemSet.Get(SDRATTR_GRAFMODE).GetValue());
304  aLocalGrafInfo.SetCrop(rCrop.GetLeft(), rCrop.GetTop(), rCrop.GetRight(), rCrop.GetBottom());
305 
306  // we have content if graphic is not completely transparent
307  const bool bHasContent(255L != aLocalGrafInfo.GetTransparency());
310  rItemSet,
311  GetGrafObject().getText(0),
312  bHasContent));
313 
314  // take unrotated snap rect for position and size. Directly use model data, not getBoundRect() or getSnapRect()
315  // which will use the primitive data we just create in the near future
316  const ::basegfx::B2DRange aObjectRange = vcl::unotools::b2DRectangleFromRectangle(GetGrafObject().GetGeoRect());
317 
318  // look for mirroring
319  const GeoStat& rGeoStat(GetGrafObject().GetGeoStat());
320  const sal_Int32 nRotationAngle(rGeoStat.nRotationAngle);
321  const bool bRota180(18000 == nRotationAngle);
322  const bool bMirrored(GetGrafObject().IsMirrored());
323  const sal_uInt16 nMirrorCase(bRota180 ? (bMirrored ? 3 : 4) : (bMirrored ? 2 : 1));
324  bool bHMirr((2 == nMirrorCase ) || (4 == nMirrorCase));
325  bool bVMirr((3 == nMirrorCase ) || (4 == nMirrorCase));
326 
327  // set mirror flags at LocalGrafInfo. Take into account that the geometry in
328  // aObjectRange is already changed and rotated when bRota180 is used. To rebuild
329  // that old behaviour (as long as part of the model data), correct the H/V flags
330  // accordingly. The created bitmapPrimitive WILL use the rotation, too.
331  if(bRota180)
332  {
333  // if bRota180 which is used for vertical mirroring, the graphic will already be rotated
334  // by 180 degrees. To correct, switch off VMirror and invert HMirroring.
335  bHMirr = !bHMirr;
336  bVMirr = false;
337  }
338 
339  if(bHMirr || bVMirr)
340  {
341  aLocalGrafInfo.SetMirrorFlags((bHMirr ? BmpMirrorFlags::Horizontal : BmpMirrorFlags::NONE)|(bVMirr ? BmpMirrorFlags::Vertical : BmpMirrorFlags::NONE));
342  }
343 
344  // fill object matrix
345  const double fShearX(rGeoStat.nShearAngle ? tan((36000 - rGeoStat.nShearAngle) * F_PI18000) : 0.0);
346  const double fRotate(nRotationAngle ? (36000 - nRotationAngle) * F_PI18000 : 0.0);
348  aObjectRange.getWidth(), aObjectRange.getHeight(),
349  fShearX, fRotate,
350  aObjectRange.getMinX(), aObjectRange.getMinY()));
351 
352  // get the current, unchanged graphic object from SdrGrafObj
353  const GraphicObject& rGraphicObject = GetGrafObject().GetGraphicObject();
354 
356  {
357  // it's an EmptyPresObj, create the SdrGrafPrimitive2D without content and another scaled one
358  // with the content which is the placeholder graphic
359  xRetval = createVIP2DSForPresObj(aObjectMatrix, aAttribute);
360  }
361 #ifndef IOS // Enforce swap-in for tiled rendering for now, while we have no delayed updating mechanism
362  else if(visualisationUsesDraft())
363  {
364  // #i102380# The graphic is swapped out. To not force a swap-in here, there is a mechanism
365  // which shows a swapped-out-visualisation (which gets created here now) and an asynchronous
366  // visual update mechanism for swapped-out graphics when they were loaded (see AsynchGraphicLoadingEvent
367  // and ViewObjectContactOfGraphic implementation). Not forcing the swap-in here allows faster
368  // (non-blocking) processing here and thus in the effect e.g. fast scrolling through pages
369  xRetval = createVIP2DSForDraft(aObjectMatrix, aAttribute);
370  }
371 #endif
372  else
373  {
374  // create primitive. Info: Calling the copy-constructor of GraphicObject in this
375  // SdrGrafPrimitive2D constructor will force a full swap-in of the graphic
378  aObjectMatrix,
379  aAttribute,
380  rGraphicObject,
381  aLocalGrafInfo));
382 
383  xRetval = drawinglayer::primitive2d::Primitive2DContainer { xReference };
384  }
385 
386  // always append an invisible outline for the cases where no visible content exists
387  xRetval.push_back(
389  aObjectMatrix));
390 
391  return xRetval;
392  }
393 
395  {
396  return GetGrafObject().IsEmptyPresObj();
397  }
398 
400  {
401  // no draft when already PresObj
403  return false;
404 
405  // draft when swapped out
406  const GraphicObject& rGraphicObject = GetGrafObject().GetGraphicObject();
407 
408  // draft when no graphic
409  return GraphicType::NONE == rGraphicObject.GetType() || GraphicType::Default == rGraphicObject.GetType();
410  }
411 
412 } // end of namespace
413 
414 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
#define SDRATTR_GRAFRED
Definition: svddef.hxx:308
static bool lessOrEqual(const double &rfValA, const double &rfValB)
void SetTransparency(sal_uInt8 cTransparency)
void SetGamma(double fGamma)
static bool moreOrEqual(const double &rfValA, const double &rfValB)
void setX(double fX)
const OUString & GetFileName() const
Definition: svdograf.hxx:172
B2DPolygon const & createUnitPolygon()
const MapMode & GetPrefMapMode() const
Point LogicToLogic(const Point &rPtSource, const MapMode *pMapModeSource, const MapMode *pMapModeDest) const
#define SDRATTR_GRAFMODE
Definition: svddef.hxx:316
static bool more(const double &rfValA, const double &rfValB)
virtual SdrText * getText(sal_Int32 nIndex) const override
returns the nth available text.
Definition: svdotext.cxx:2066
#define SDRATTR_GRAFGAMMA
Definition: svddef.hxx:313
periodic cubic Spline (ni)
Definition: svdobj.hxx:133
void SetInvert(bool bInvert)
static const AllSettings & GetSettings()
long nRotationAngle
Definition: svdtrans.hxx:216
double getX() const
void SetMirrorFlags(BmpMirrorFlags nMirrFlags)
virtual ViewObjectContact & CreateObjectSpecificViewObjectContact(ObjectContact &rObjectContact) override
double getY() const
constexpr::Color COL_LIGHTRED(0xFF, 0x00, 0x00)
const GraphicObject & GetGraphicObject(bool bForceSwapIn=false) const
Definition: svdograf.cxx:286
#define SDRATTR_GRAFINVERT
Definition: svddef.hxx:315
#define SDRATTR_GRAFGREEN
Definition: svddef.hxx:309
static void Free(SdrObject *&_rpObject)
Definition: svdobj.cxx:420
void SetDrawMode(GraphicDrawMode eDrawMode)
Rectangle objects (rectangle, circle, ...)
Definition: svdorect.hxx:39
static OutputDevice * GetDefaultDevice()
bool IsMirrored(const long nPageLeftMargin, const long nPageRightMargin, const long nPageTopMargin, const long nPageBottomMargin, bool bMirrored)
B2DHomMatrix createScaleShearXRotateTranslateB2DHomMatrix(double fScaleX, double fScaleY, double fShearX, double fRadiant, double fTranslateX, double fTranslateY)
const Size & GetPrefSize() const
The transformation of a rectangle into a polygon, by using angle parameters from GeoStat.
Definition: svdtrans.hxx:214
const SdrGrafObj & GetGrafObject() const
B2DHomMatrix createScaleTranslateB2DHomMatrix(double fScaleX, double fScaleY, double fTranslateX, double fTranslateY)
virtual drawinglayer::primitive2d::Primitive2DContainer createViewIndependentPrimitive2DSequence() const override
B2IRange fround(const B2DRange &rRange)
bool IsEmpty() const
attribute::SdrLineFillShadowTextAttribute createNewSdrLineFillShadowTextAttribute(const SfxItemSet &rSet, const SdrText *pText, bool bHasContent)
#define DBG_ASSERT(sCon, aError)
void SetContrast(short nContrastPercent)
bool decompose(B2DTuple &rScale, B2DTuple &rTranslate, double &rRotate, double &rShearX) const
void SetCrop(long nLeft_100TH_MM, long nTop_100TH_MM, long nRight_100TH_MM, long nBottom_100TH_MM)
#define SDRATTR_GRAFCROP
Definition: svddef.hxx:317
void SetChannelG(short nChannelGPercent)
#define EE_CHAR_COLOR
void SetMergedItem(const SfxPoolItem &rItem)
Definition: svdobj.cxx:1918
MapUnit GetMapUnit() const
void SetChannelR(short nChannelRPercent)
This class represents an embedded or linked bitmap graphic object.
Definition: svdograf.hxx:79
Abstract DrawObject.
Definition: svdobj.hxx:312
#define SDRATTR_GRAFBLUE
Definition: svddef.hxx:310
OUString GetName() const
Definition: svdobj.cxx:722
long nShearAngle
Definition: svdtrans.hxx:217
GraphicType GetType() const
void SetChannelB(short nChannelBPercent)
basegfx::B2DRange b2DRectangleFromRectangle(const ::tools::Rectangle &rRect)
Point PixelToLogic(const Point &rDevicePt) const
css::uno::Reference< css::graphic::XPrimitive2D > Primitive2DReference
void setY(double fY)
const SfxPoolItem & Get(sal_uInt16 nWhich, bool bSrchInParent=true) const
virtual OutlinerParaObject * GetOutlinerParaObject() const override
Definition: svdotext.cxx:1371
#define SDRATTR_GRAFLUMINANCE
Definition: svddef.hxx:311
void SetLuminance(short nLuminancePercent)
const SfxItemSet & GetMergedItemSet() const
Definition: svdobj.cxx:1908
void NbcSetText(const OUString &rStr)
Definition: svdotext.cxx:207
Primitive2DReference createHiddenGeometryPrimitives2D(const basegfx::B2DHomMatrix &rMatrix)
#define F_PI18000
#define SDRATTR_GRAFTRANSPARENCE
Definition: svddef.hxx:314
basegfx::BColor getBColor() const
drawinglayer::primitive2d::Primitive2DContainer createVIP2DSForPresObj(const basegfx::B2DHomMatrix &rObjectMatrix, const drawinglayer::attribute::SdrLineFillShadowTextAttribute &rAttribute) const
drawinglayer::primitive2d::Primitive2DContainer createVIP2DSForDraft(const basegfx::B2DHomMatrix &rObjectMatrix, const drawinglayer::attribute::SdrLineFillShadowTextAttribute &rAttribute) const
#define SDRATTR_GRAFCONTRAST
Definition: svddef.hxx:312
long getHeight() const
B2DHomMatrix createShearXRotateTranslateB2DHomMatrix(double fShearX, double fRadiant, double fTranslateX, double fTranslateY)
const Size & GetSizePixel() const
long getWidth() const
sal_uInt8 GetTransparency() const
bool IsEmptyPresObj() const
Definition: svdobj.hxx:880