LibreOffice Module vcl (master)  1
gfxlink.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 <sal/log.hxx>
21 #include <tools/stream.hxx>
22 #include <tools/vcompat.hxx>
23 #include <vcl/graph.hxx>
24 #include <vcl/gfxlink.hxx>
25 #include <vcl/graphicfilter.hxx>
26 #include <memory>
27 
30  , mnUserId(0)
31  , mnSwapInDataSize(0)
32  , mbPrefMapModeValid(false)
33  , mbPrefSizeValid(false)
34 {
35 }
36 
37 GfxLink::GfxLink(std::unique_ptr<sal_uInt8[]> pBuf, sal_uInt32 nSize, GfxLinkType nType)
38  : meType(nType)
39  , mnUserId(0)
40  , mpSwapInData(std::shared_ptr<sal_uInt8>(pBuf.release(), pBuf.get_deleter())) // std::move(pBuf) does not compile on Jenkins MacOSX (24 May 2016)
41  , mnSwapInDataSize(nSize)
42  , mbPrefMapModeValid(false)
43  , mbPrefSizeValid(false)
44 {
45  SAL_WARN_IF(mpSwapInData == nullptr || mnSwapInDataSize <= 0, "vcl",
46  "GfxLink::GfxLink(): empty/NULL buffer given");
47 }
48 
49 bool GfxLink::operator==( const GfxLink& rGfxLink ) const
50 {
51  bool bIsEqual = false;
52 
53  if ( ( mnSwapInDataSize == rGfxLink.mnSwapInDataSize ) && ( meType == rGfxLink.meType ) )
54  {
55  const sal_uInt8* pSource = GetData();
56  const sal_uInt8* pDest = rGfxLink.GetData();
57  sal_uInt32 nSourceSize = GetDataSize();
58  sal_uInt32 nDestSize = rGfxLink.GetDataSize();
59  if ( pSource && pDest && ( nSourceSize == nDestSize ) )
60  {
61  bIsEqual = memcmp( pSource, pDest, nSourceSize ) == 0;
62  }
63  else if ( ( pSource == nullptr ) && ( pDest == nullptr ) )
64  bIsEqual = true;
65  }
66  return bIsEqual;
67 }
68 
69 
70 bool GfxLink::IsNative() const
71 {
73 }
74 
75 
77 {
78  return mpSwapInData.get();
79 }
80 
81 
82 void GfxLink::SetPrefSize( const Size& rPrefSize )
83 {
84  maPrefSize = rPrefSize;
85  mbPrefSizeValid = true;
86 }
87 
88 
89 void GfxLink::SetPrefMapMode( const MapMode& rPrefMapMode )
90 {
91  maPrefMapMode = rPrefMapMode;
92  mbPrefMapModeValid = true;
93 }
94 
95 
96 bool GfxLink::LoadNative( Graphic& rGraphic )
97 {
98  bool bRet = false;
99 
100  if( IsNative() && mnSwapInDataSize )
101  {
102  const sal_uInt8* pData = GetData();
103  if (pData)
104  {
105  SvMemoryStream aMemoryStream(const_cast<sal_uInt8*>(pData), mnSwapInDataSize, StreamMode::READ | StreamMode::WRITE);
106  OUString aShortName;
107 
108  switch (meType)
109  {
110  case GfxLinkType::NativeGif: aShortName = GIF_SHORTNAME; break;
111  case GfxLinkType::NativeJpg: aShortName = JPG_SHORTNAME; break;
112  case GfxLinkType::NativePng: aShortName = PNG_SHORTNAME; break;
113  case GfxLinkType::NativeTif: aShortName = TIF_SHORTNAME; break;
114  case GfxLinkType::NativeWmf: aShortName = WMF_SHORTNAME; break;
115  case GfxLinkType::NativeMet: aShortName = MET_SHORTNAME; break;
116  case GfxLinkType::NativePct: aShortName = PCT_SHORTNAME; break;
117  case GfxLinkType::NativeSvg: aShortName = SVG_SHORTNAME; break;
118  case GfxLinkType::NativeBmp: aShortName = BMP_SHORTNAME; break;
119  case GfxLinkType::NativePdf: aShortName = PDF_SHORTNAME; break;
120  default: break;
121  }
122  if (!aShortName.isEmpty())
123  {
125  sal_uInt16 nFormat = rFilter.GetImportFormatNumberForShortName(aShortName);
126  ErrCode nResult = rFilter.ImportGraphic(rGraphic, OUString(), aMemoryStream, nFormat);
127  if (nResult == ERRCODE_NONE)
128  bRet = true;
129  }
130  }
131  }
132 
133  return bRet;
134 }
135 
136 bool GfxLink::ExportNative( SvStream& rOStream ) const
137 {
138  if( GetDataSize() )
139  {
140  auto pData = GetSwapInData();
141  if (pData)
142  rOStream.WriteBytes( pData.get(), mnSwapInDataSize );
143  }
144 
145  return ( rOStream.GetError() == ERRCODE_NONE );
146 }
147 
148 std::shared_ptr<sal_uInt8> GfxLink::GetSwapInData() const
149 {
150  return mpSwapInData;
151 }
152 
153 bool GfxLink::IsEMF() const
154 {
155  const sal_uInt8* pGraphicAry = GetData();
156  if ((GetType() == GfxLinkType::NativeWmf) && pGraphicAry && (GetDataSize() > 0x2c))
157  {
158  // check the magic number
159  if ((pGraphicAry[0x28] == 0x20) && (pGraphicAry[0x29] == 0x45)
160  && (pGraphicAry[0x2a] == 0x4d) && (pGraphicAry[0x2b] == 0x46))
161  {
162  //emf detected
163  return true;
164  }
165  }
166  return false;
167 }
168 
169 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
ErrCode ImportGraphic(Graphic &rGraphic, const INetURLObject &rPath, sal_uInt16 nFormat=GRFILTER_FORMAT_DONTKNOW, sal_uInt16 *pDeterminedFormat=nullptr, GraphicFilterImportFlags nImportFlags=GraphicFilterImportFlags::NONE)
std::unique_ptr< ContentProperties > pData
Class to import and export graphic formats.
NONE
#define TIF_SHORTNAME
#define JPG_SHORTNAME
ErrCode GetError() const
#define PNG_SHORTNAME
#define BMP_SHORTNAME
std::size_t WriteBytes(const void *pData, std::size_t nSize)
sal_uInt16 GetImportFormatNumberForShortName(const OUString &rShortName)
#define PDF_SHORTNAME
#define SAL_WARN_IF(condition, area, stream)
#define ERRCODE_NONE
Definition: errcode.hxx:198
unsigned char sal_uInt8
static GraphicFilter & GetGraphicFilter()
#define MET_SHORTNAME
#define SVG_SHORTNAME
#define GIF_SHORTNAME
RedlineType meType
#define PCT_SHORTNAME
#define WMF_SHORTNAME