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