LibreOffice Module vcl (master)  1
wmf.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 "emfwr.hxx"
21 #include "wmfwr.hxx"
22 #include <vcl/wmf.hxx>
23 #include <vcl/gdimetafiletools.hxx>
24 #include <vcl/graph.hxx>
25 
26 using namespace com::sun::star;
27 
28 bool ReadWindowMetafile( SvStream& rStream, GDIMetaFile& rMTF )
29 {
30  // tdf#111484 Use new method to import Metafile. Take current StreamPos
31  // into account (used by SwWW8ImplReader::ReadGrafFile and by
32  // SwWw6ReadMetaStream, so do *not* ignore. OTOH XclImpDrawing::ReadWmf
33  // is nice enough to copy to an own MemStream to avoid that indirect
34  // parameter passing...)
35  const sal_uInt32 nStreamStart(rStream.Tell());
36  const sal_uInt32 nStreamEnd(rStream.TellEnd());
37 
38  if (nStreamStart >= nStreamEnd)
39  {
40  return false;
41  }
42 
43  // Read binary data to mem array
44  const sal_uInt32 nStreamLength(nStreamEnd - nStreamStart);
45  VectorGraphicDataArray aNewData(nStreamLength);
46  rStream.ReadBytes(aNewData.begin(), nStreamLength);
47  rStream.Seek(nStreamStart);
48 
49  if (rStream.good())
50  {
51  // Throw into VectorGraphicData to get the import. Do not care
52  // too much for type, this will be checked there. Also no path
53  // needed, it is a temporary object
54  auto aVectorGraphicDataPtr =
55  std::make_shared<VectorGraphicData>(
56  aNewData,
58 
59  // create a Graphic and grep Metafile from it
60  const Graphic aGraphic(aVectorGraphicDataPtr);
61 
62  // get the Metafile from it, done
63  rMTF = aGraphic.GetGDIMetaFile();
64  return true;
65  }
66 
67  return rStream.good();
68 }
69 
70 bool ConvertGDIMetaFileToWMF( const GDIMetaFile & rMTF, SvStream & rTargetStream,
71  FilterConfigItem const * pConfigItem, bool bPlaceable)
72 {
73  WMFWriter aWMFWriter;
74  GDIMetaFile aGdiMetaFile(rMTF);
75 
76  if(usesClipActions(aGdiMetaFile))
77  {
78  // #i121267# It is necessary to prepare the metafile since the export does *not* support
79  // clip regions. This tooling method clips the geometry content of the metafile internally
80  // against its own clip regions, so that the export is safe to ignore clip regions
82  }
83 
84  bool bRet = aWMFWriter.WriteWMF(aGdiMetaFile, rTargetStream, pConfigItem, bPlaceable);
85  return bRet;
86 }
87 
88 bool ConvertGraphicToWMF(const Graphic& rGraphic, SvStream& rTargetStream,
89  FilterConfigItem const* pConfigItem, bool bPlaceable)
90 {
91  GfxLink aLink = rGraphic.GetGfxLink();
92  if (aLink.IsEMF() && aLink.GetData() && aLink.GetDataSize())
93  {
94  // This may be an EMF+ file, converting that to WMF is better done by re-parsing EMF+ as EMF
95  // and converting that to WMF.
96  uno::Sequence<sal_Int8> aData(reinterpret_cast<const sal_Int8*>(aLink.GetData()),
97  aLink.GetDataSize());
98  auto aVectorGraphicData
99  = std::make_shared<VectorGraphicData>(aData, VectorGraphicDataType::Emf);
100  aVectorGraphicData->setEnableEMFPlus(false);
101  Graphic aGraphic(aVectorGraphicData);
102  bool bRet = ConvertGDIMetaFileToWMF(aGraphic.GetGDIMetaFile(), rTargetStream, pConfigItem,
103  bPlaceable);
104  return bRet;
105  }
106 
107  bool bRet = ConvertGDIMetaFileToWMF(rGraphic.GetGDIMetaFile(), rTargetStream, pConfigItem,
108  bPlaceable);
109  return bRet;
110 }
111 
112 bool ConvertGDIMetaFileToEMF(const GDIMetaFile & rMTF, SvStream & rTargetStream)
113 {
114  EMFWriter aEMFWriter(rTargetStream);
115  GDIMetaFile aGdiMetaFile(rMTF);
116 
117  if(usesClipActions(aGdiMetaFile))
118  {
119  // #i121267# It is necessary to prepare the metafile since the export does *not* support
120  // clip regions. This tooling method clips the geometry content of the metafile internally
121  // against its own clip regions, so that the export is safe to ignore clip regions
123  }
124 
125  return aEMFWriter.WriteEMF(aGdiMetaFile);
126 }
127 
128 bool WriteWindowMetafileBits( SvStream& rStream, const GDIMetaFile& rMTF )
129 {
130  return WMFWriter().WriteWMF( rMTF, rStream, nullptr, false );
131 }
132 
133 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
bool WriteWMF(const GDIMetaFile &rMTF, SvStream &rTargetStream, FilterConfigItem const *pFilterConfigItem, bool bPlaceable)
Definition: wmfwr.cxx:1678
bool ReadWindowMetafile(SvStream &rStream, GDIMetaFile &rMTF)
Definition: wmf.cxx:28
virtual sal_uInt64 TellEnd()
bool ConvertGDIMetaFileToWMF(const GDIMetaFile &rMTF, SvStream &rTargetStream, FilterConfigItem const *pConfigItem, bool bPlaceable)
Definition: wmf.cxx:70
bool usesClipActions(const GDIMetaFile &rSource)
sal_uInt64 Seek(sal_uInt64 nPos)
bool WriteWindowMetafileBits(SvStream &rStream, const GDIMetaFile &rMTF)
Definition: wmf.cxx:128
bool ConvertGDIMetaFileToEMF(const GDIMetaFile &rMTF, SvStream &rTargetStream)
Definition: wmf.cxx:112
const GDIMetaFile & GetGDIMetaFile() const
Definition: graph.cxx:335
void clipMetafileContentAgainstOwnRegions(GDIMetaFile &rSource)
constexpr OUStringLiteral aData
bool ConvertGraphicToWMF(const Graphic &rGraphic, SvStream &rTargetStream, FilterConfigItem const *pConfigItem, bool bPlaceable)
Definition: wmf.cxx:88
bool WriteEMF(const GDIMetaFile &rMtf)
Definition: emfwr.cxx:239
GfxLink GetGfxLink() const
Definition: graph.cxx:503
std::size_t ReadBytes(void *pData, std::size_t nSize)
css::uno::Sequence< sal_Int8 > VectorGraphicDataArray
sal_uInt64 Tell() const
bool good() const