LibreOffice Module svx (master)  1
xattrbmp.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 <com/sun/star/awt/XBitmap.hpp>
21 #include <com/sun/star/graphic/XGraphic.hpp>
22 
24 #include <tools/debug.hxx>
25 #include <vcl/virdev.hxx>
26 #include <vcl/bitmapex.hxx>
27 #include <svl/style.hxx>
28 #include <editeng/memberids.h>
29 #include <svx/strings.hrc>
30 #include <svx/xtable.hxx>
31 #include <svx/xdef.hxx>
32 #include <svx/unomid.hxx>
33 #include <svx/unoapi.hxx>
34 #include <svx/svdmodel.hxx>
35 #include <svx/xbitmap.hxx>
36 #include <svx/xbtmpit.hxx>
37 #include <com/sun/star/beans/PropertyValue.hpp>
38 #include <vcl/BitmapTools.hxx>
39 #include <vcl/GraphicLoader.hxx>
40 
41 #include <libxml/xmlwriter.h>
42 
43 using namespace ::com::sun::star;
44 
45 XOBitmap::XOBitmap( const BitmapEx& rBmp ) :
46  xGraphicObject (new GraphicObject(rBmp)),
47  bGraphicDirty ( false )
48 {
49 }
50 
52 {
53 }
54 
56 {
58 }
59 
61 {
62  if( bGraphicDirty )
63  const_cast<XOBitmap*>(this)->Array2Bitmap();
64 
65  return *xGraphicObject;
66 }
67 
69 {
71  bool bPixelColor = false;
72  const BitmapEx aBitmap( GetBitmap() );
73  const sal_Int32 nLines = 8; // type dependent
74 
75  if( !pPixelArray )
76  pPixelArray.reset( new sal_uInt16[ nLines * nLines ] );
77 
78  pVDev->SetOutputSizePixel( aBitmap.GetSizePixel() );
79  pVDev->DrawBitmapEx( Point(), aBitmap );
80  aPixelColor = aBckgrColor = pVDev->GetPixel( Point() );
81 
82  // create array and determine foreground and background color
83  for (sal_Int32 i = 0; i < nLines; ++i)
84  {
85  for (sal_Int32 j = 0; j < nLines; ++j)
86  {
87  if ( pVDev->GetPixel( Point( j, i ) ) == aBckgrColor )
88  pPixelArray[ j + i * nLines ] = 0;
89  else
90  {
91  pPixelArray[ j + i * nLines ] = 1;
92  if( !bPixelColor )
93  {
94  aPixelColor = pVDev->GetPixel( Point( j, i ) );
95  bPixelColor = true;
96  }
97  }
98  }
99  }
100 }
101 
104 {
105  if (!pPixelArray)
106  return;
107 
109  const sal_Int32 nLines = 8; // type dependent
110 
111  pVDev->SetOutputSizePixel( Size( nLines, nLines ) );
112 
113  // create bitmap
114  for (sal_Int32 i = 0; i < nLines; ++i)
115  {
116  for (sal_Int32 j = 0; j < nLines; ++j)
117  {
118  if( pPixelArray[ j + i * nLines ] == 0 )
119  pVDev->DrawPixel( Point( j, i ), aBckgrColor );
120  else
121  pVDev->DrawPixel( Point( j, i ), aPixelColor );
122  }
123  }
124 
125  xGraphicObject.reset(new GraphicObject(pVDev->GetBitmapEx(Point(), Size(nLines, nLines))));
126  bGraphicDirty = false;
127 }
128 
129 
131 
132 XFillBitmapItem::XFillBitmapItem(const OUString& rName, const GraphicObject& rGraphicObject)
133 : NameOrIndex(XATTR_FILLBITMAP, rName),
134  maGraphicObject(rGraphicObject)
135 {
136 }
137 
139 : NameOrIndex(rItem),
140  maGraphicObject(rItem.maGraphicObject)
141 {
142 }
143 
146  , maGraphicObject(rGraphicObject)
147 {
148 }
149 
151 {
152  return new XFillBitmapItem(*this);
153 }
154 
155 bool XFillBitmapItem::operator==(const SfxPoolItem& rItem) const
156 {
157  return (NameOrIndex::operator==(rItem)
158  && maGraphicObject == static_cast<const XFillBitmapItem&>(rItem).maGraphicObject);
159 }
160 
161 
163 {
164  Color aBack, aFront;
165  return vcl::bitmap::isHistorical8x8(GetGraphicObject().GetGraphic().GetBitmapEx(), aBack, aFront);
166 }
167 
169  SfxItemPresentation /*ePres*/,
170  MapUnit /*eCoreUnit*/,
171  MapUnit /*ePresUnit*/,
172  OUString& rText,
173  const IntlWrapper&) const
174 {
175  rText += GetName();
176  return true;
177 }
178 
179 bool XFillBitmapItem::QueryValue(css::uno::Any& rVal, sal_uInt8 nMemberId) const
180 {
181  nMemberId &= ~CONVERT_TWIPS;
182 
183  // needed for MID_NAME
184  OUString aApiName;
185  // needed for complete item (MID 0)
186  OUString aInternalName;
187 
188  css::uno::Reference< css::awt::XBitmap > xBmp;
189 
190  if( nMemberId == MID_NAME )
191  {
192  aApiName = SvxUnogetApiNameForItem(Which(), GetName());
193  }
194  else if( nMemberId == 0 )
195  {
196  aInternalName = GetName();
197  }
198 
199  if (nMemberId == MID_BITMAP ||
200  nMemberId == 0)
201  {
202  xBmp.set(GetGraphicObject().GetGraphic().GetXGraphic(), uno::UNO_QUERY);
203  }
204 
205  if( nMemberId == MID_NAME )
206  rVal <<= aApiName;
207  else if( nMemberId == MID_BITMAP )
208  rVal <<= xBmp;
209  else
210  {
211  // member-id 0 => complete item (e.g. for toolbars)
212  DBG_ASSERT( nMemberId == 0, "invalid member-id" );
213  uno::Sequence< beans::PropertyValue > aPropSeq{
214  comphelper::makePropertyValue("Name", aInternalName),
215  comphelper::makePropertyValue("Bitmap", xBmp)
216  };
217 
218  rVal <<= aPropSeq;
219  }
220 
221  return true;
222 }
223 
224 bool XFillBitmapItem::PutValue( const css::uno::Any& rVal, sal_uInt8 nMemberId )
225 {
226  nMemberId &= ~CONVERT_TWIPS;
227 
228  OUString aName;
229  OUString aURL;
230  css::uno::Reference< css::awt::XBitmap > xBmp;
231  css::uno::Reference< css::graphic::XGraphic > xGraphic;
232 
233  bool bSetURL = false;
234  bool bSetName = false;
235  bool bSetBitmap = false;
236 
237  if( nMemberId == MID_NAME )
238  bSetName = (rVal >>= aName);
239  else if( nMemberId == MID_BITMAP )
240  {
241  if (rVal.has<OUString>())
242  {
243  bSetURL = true;
244  aURL = rVal.get<OUString>();
245  }
246  else if (rVal.has<uno::Reference<awt::XBitmap>>())
247  {
248  bSetBitmap = true;
249  xBmp = rVal.get<uno::Reference<awt::XBitmap>>();
250  }
251  else if (rVal.has<uno::Reference<graphic::XGraphic>>())
252  {
253  bSetBitmap = true;
254  xGraphic = rVal.get<uno::Reference<graphic::XGraphic>>();
255  }
256  }
257  else
258  {
259  DBG_ASSERT( nMemberId == 0, "invalid member-id" );
260  uno::Sequence< beans::PropertyValue > aPropSeq;
261  if( rVal >>= aPropSeq )
262  {
263  for ( const auto& rProp : std::as_const(aPropSeq) )
264  {
265  if ( rProp.Name == "Name" )
266  bSetName = (rProp.Value >>= aName);
267  else if ( rProp.Name == "Bitmap" )
268  bSetBitmap = (rProp.Value >>= xBmp);
269  else if ( rProp.Name == "FillBitmapURL" )
270  bSetURL = (rProp.Value >>= aURL);
271  }
272  }
273  }
274 
275  if( bSetName )
276  {
277  SetName( aName );
278  }
279  if (bSetURL && !aURL.isEmpty())
280  {
281  Graphic aGraphic = vcl::graphic::loadFromURL(aURL);
282  if (!aGraphic.IsNone())
283  {
285  }
286  }
287  else if( bSetBitmap )
288  {
289  if (xBmp.is())
290  {
291  xGraphic.set(xBmp, uno::UNO_QUERY);
292  }
293  if (xGraphic.is())
294  {
295  maGraphicObject.SetGraphic(xGraphic);
296  }
297  }
298 
299  return (bSetURL || bSetName || bSetBitmap);
300 }
301 
303 {
304  const GraphicObject& aGraphicObjectA(static_cast<const XFillBitmapItem*>(p1)->GetGraphicObject());
305  const GraphicObject& aGraphicObjectB(static_cast<const XFillBitmapItem*>(p2)->GetGraphicObject());
306 
307  return aGraphicObjectA == aGraphicObjectB;
308 }
309 
310 std::unique_ptr<XFillBitmapItem> XFillBitmapItem::checkForUniqueItem( SdrModel* pModel ) const
311 {
312  if( pModel )
313  {
315  if(isPattern())
316  aListType = XPropertyListType::Pattern;
317  const OUString aUniqueName = NameOrIndex::CheckNamedItem(
318  this, XATTR_FILLBITMAP, &pModel->GetItemPool(),
319  XFillBitmapItem::CompareValueFunc, RID_SVXSTR_BMP21,
320  pModel->GetPropertyList( aListType ) );
321 
322  // if the given name is not valid, replace it!
323  if( aUniqueName != GetName() )
324  {
325  return std::make_unique<XFillBitmapItem>(aUniqueName, maGraphicObject);
326  }
327  }
328 
329  return nullptr;
330 }
331 
333 {
334  (void)xmlTextWriterStartElement(pWriter, BAD_CAST("XFillBitmapItem"));
335  (void)xmlTextWriterWriteAttribute(pWriter, BAD_CAST("whichId"), BAD_CAST(OString::number(Which()).getStr()));
336 
337  NameOrIndex::dumpAsXml(pWriter);
338 
339  (void)xmlTextWriterEndElement(pWriter);
340 }
341 
342 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
BitmapEx GetBitmapEx(BitmapEx const &rBitmapEx, DrawModeFlags nDrawMode)
~XOBitmap()
Definition: xattrbmp.cxx:51
virtual bool QueryValue(css::uno::Any &rVal, sal_uInt8 nMemberId=0) const override
Definition: xattrbmp.cxx:179
URL aURL
static bool CompareValueFunc(const NameOrIndex *p1, const NameOrIndex *p2)
Definition: xattrbmp.cxx:302
Color aBckgrColor
Definition: xbitmap.hxx:34
const XPropertyListRef & GetPropertyList(XPropertyListType t) const
Definition: svdmodel.hxx:527
struct _xmlTextWriter * xmlTextWriterPtr
bool IsNone() const
#define MID_NAME
css::beans::PropertyValue makePropertyValue(const OUString &rName, T &&rValue)
std::unique_ptr< GraphicObject > xGraphicObject
Definition: xbitmap.hxx:31
XOBitmap(const XOBitmap &rXBmp)=delete
OUString SvxUnogetApiNameForItem(const sal_uInt16 nWhich, const OUString &rInternalName)
if the given name is a predefined name for the current language it is replaced by the corresponding a...
Definition: unoprov.cxx:2010
bool bGraphicDirty
Definition: xbitmap.hxx:35
Graphic loadFromURL(OUString const &rURL, weld::Window *pParentWin)
void Bitmap2Array()
Definition: xattrbmp.cxx:68
virtual void dumpAsXml(xmlTextWriterPtr pWriter) const override
Definition: xattrbmp.cxx:332
Color aPixelColor
Definition: xbitmap.hxx:33
const GraphicObject & GetGraphicObject() const
Definition: xbtmpit.hxx:56
SfxItemPresentation
XPropertyListType
Definition: xtable.hxx:131
virtual bool GetPresentation(SfxItemPresentation ePres, MapUnit eCoreMetric, MapUnit ePresMetric, OUString &rText, const IntlWrapper &) const override
Definition: xattrbmp.cxx:168
#define DBG_ASSERT(sCon, aError)
int i
void dumpAsXml(xmlTextWriterPtr pWriter) const override
Definition: xattr.cxx:251
constexpr TypedWhichId< XFillBitmapItem > XATTR_FILLBITMAP(XATTR_FILL_FIRST+4)
GraphicObject maGraphicObject
Definition: xbtmpit.hxx:34
bool isHistorical8x8(const BitmapEx &rBitmapEx, Color &o_rBack, Color &o_rFront)
std::unique_ptr< XFillBitmapItem > checkForUniqueItem(SdrModel *pModel) const
Definition: xattrbmp.cxx:310
BitmapEx GetBitmapEx(const GraphicConversionParameters &rParameters=GraphicConversionParameters()) const
std::unique_ptr< sal_uInt16[]> pPixelArray
Definition: xbitmap.hxx:32
virtual bool operator==(const SfxPoolItem &rItem) const override
Definition: xattrbmp.cxx:155
static SfxPoolItem * CreateDefault()
Definition: xattrbmp.cxx:130
void Array2Bitmap()
convert array, fore- and background color into a bitmap
Definition: xattrbmp.cxx:103
bool isPattern() const
Definition: xattrbmp.cxx:162
virtual XFillBitmapItem * Clone(SfxItemPool *pPool=nullptr) const override
Definition: xattrbmp.cxx:150
virtual bool PutValue(const css::uno::Any &rVal, sal_uInt8 nMemberId) override
Definition: xattrbmp.cxx:224
BitmapEx GetBitmap() const
Definition: xattrbmp.cxx:55
void SetName(const OUString &rName)
Definition: xit.hxx:54
const Graphic & GetGraphic() const
unsigned char sal_uInt8
void SetGraphic(const Graphic &rGraphic)
OUString aName
const GraphicObject & GetGraphicObject() const
Definition: xattrbmp.cxx:60
css::uno::Reference< css::graphic::XGraphic > GetXGraphic() const
OUString const & GetName() const
Definition: xit.hxx:53
MapUnit
const SfxItemPool & GetItemPool() const
Definition: svdmodel.hxx:317
const Size & GetSizePixel() const
static OUString CheckNamedItem(const NameOrIndex *pCheckItem, const sal_uInt16 nWhich, const SfxItemPool *pPool1, SvxCompareValueFunc pCompareValueFunc, TranslateId pPrefixResId, const XPropertyListRef &pDefaults)
this static checks if the given NameOrIndex item has a unique name for its value. ...
Definition: xattr.cxx:131
sal_uInt16 Which() const
#define MID_BITMAP
Definition: unomid.hxx:46
bool m_bDetectedRangeSegmentation false
#define CONVERT_TWIPS
typedef void(CALLTYPE *GetFuncDataPtr)(sal_uInt16 &nNo