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