/src/libreoffice/sfx2/source/doc/graphhelp.cxx
Line | Count | Source (jump to first uncovered line) |
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 | | |
21 | | #ifdef _WIN32 |
22 | | #include <prewin.h> |
23 | | #include <postwin.h> |
24 | | #endif |
25 | | |
26 | | #include <com/sun/star/uno/Exception.hpp> |
27 | | #include <com/sun/star/graphic/GraphicProvider.hpp> |
28 | | #include <com/sun/star/graphic/XGraphicProvider.hpp> |
29 | | #include <com/sun/star/graphic/XGraphic.hpp> |
30 | | |
31 | | |
32 | | #include <vcl/gdimtf.hxx> |
33 | | #include <vcl/graph.hxx> |
34 | | #include <vcl/cvtgrf.hxx> |
35 | | #include <vcl/bitmapex.hxx> |
36 | | #include <vcl/graphicfilter.hxx> |
37 | | |
38 | | #include <tools/stream.hxx> |
39 | | #include <unotools/ucbstreamhelper.hxx> |
40 | | #include <comphelper/processfactory.hxx> |
41 | | #include <comphelper/propertyvalue.hxx> |
42 | | #include <o3tl/char16_t2wchar_t.hxx> |
43 | | #include <o3tl/string_view.hxx> |
44 | | |
45 | | #include "graphhelp.hxx" |
46 | | #include <bitmaps.hlst> |
47 | | |
48 | | #include <memory> |
49 | | |
50 | | #if defined _WIN32 |
51 | | #include <unotools/tempfile.hxx> |
52 | | #include <vcl/outdev.hxx> |
53 | | #endif |
54 | | |
55 | | using namespace css; |
56 | | |
57 | | std::unique_ptr<SvMemoryStream> GraphicHelper::getFormatStrFromGDI_Impl( const GDIMetaFile* pGDIMeta, ConvertDataFormat nFormat ) |
58 | 0 | { |
59 | 0 | std::unique_ptr<SvMemoryStream> pResult; |
60 | 0 | if ( pGDIMeta ) |
61 | 0 | { |
62 | 0 | std::unique_ptr<SvMemoryStream> pStream(new SvMemoryStream( 65535, 65535 )); |
63 | 0 | Graphic aGraph( *pGDIMeta ); |
64 | 0 | if ( GraphicConverter::Export( *pStream, aGraph, nFormat ) == ERRCODE_NONE ) |
65 | 0 | pResult = std::move(pStream); |
66 | 0 | } |
67 | |
|
68 | 0 | return pResult; |
69 | 0 | } |
70 | | |
71 | | |
72 | | // static |
73 | | void* GraphicHelper::getEnhMetaFileFromGDI_Impl( const GDIMetaFile* pGDIMeta ) |
74 | 0 | { |
75 | 0 | void* pResult = nullptr; |
76 | |
|
77 | | #ifdef _WIN32 |
78 | | if ( pGDIMeta ) |
79 | | { |
80 | | ::utl::TempFileNamed aTempFile( u"", true, u".emf" ); |
81 | | |
82 | | OUString aMetaFile = aTempFile.GetFileName(); |
83 | | OUString aMetaURL = aTempFile.GetURL(); |
84 | | |
85 | | std::unique_ptr<SvStream> pStream = ::utl::UcbStreamHelper::CreateStream( aMetaURL, StreamMode::STD_READWRITE ); |
86 | | if ( pStream ) |
87 | | { |
88 | | Graphic aGraph( *pGDIMeta ); |
89 | | ErrCode nFailed = GraphicConverter::Export( *pStream, aGraph, ConvertDataFormat::EMF ); |
90 | | pStream->Flush(); |
91 | | pStream.reset(); |
92 | | |
93 | | if ( !nFailed ) |
94 | | pResult = GetEnhMetaFileW( o3tl::toW(aMetaFile.getStr()) ); |
95 | | } |
96 | | } |
97 | | #else |
98 | 0 | (void)pGDIMeta; // unused |
99 | 0 | #endif |
100 | |
|
101 | 0 | return pResult; |
102 | 0 | } |
103 | | |
104 | | |
105 | | // static |
106 | | void* GraphicHelper::getWinMetaFileFromGDI_Impl( const GDIMetaFile* pGDIMeta, const Size& aMetaSize ) |
107 | 0 | { |
108 | 0 | void* pResult = nullptr; |
109 | |
|
110 | | #ifdef _WIN32 |
111 | | if ( pGDIMeta ) |
112 | | { |
113 | | SvMemoryStream pStream( 65535, 65535 ); |
114 | | Graphic aGraph( *pGDIMeta ); |
115 | | ErrCode nFailed = GraphicConverter::Export( pStream, aGraph, ConvertDataFormat::WMF ); |
116 | | pStream.Flush(); |
117 | | if ( !nFailed ) |
118 | | { |
119 | | sal_uInt64 nLength = pStream.TellEnd(); |
120 | | if ( nLength > 22 ) |
121 | | { |
122 | | HMETAFILE hMeta = SetMetaFileBitsEx( nLength - 22, |
123 | | static_cast< const unsigned char*>( pStream.GetData() ) + 22 ); |
124 | | |
125 | | if ( hMeta ) |
126 | | { |
127 | | HGLOBAL hMemory = GlobalAlloc( GMEM_DDESHARE | GMEM_MOVEABLE, sizeof( METAFILEPICT ) ); |
128 | | |
129 | | if ( hMemory ) |
130 | | { |
131 | | if (METAFILEPICT* pMF = static_cast<METAFILEPICT*>(GlobalLock(hMemory))) |
132 | | { |
133 | | pMF->hMF = hMeta; |
134 | | pMF->mm = MM_ANISOTROPIC; |
135 | | |
136 | | MapMode aWinMode( MapUnit::Map100thMM ); |
137 | | |
138 | | if ( aWinMode == pGDIMeta->GetPrefMapMode() ) |
139 | | { |
140 | | pMF->xExt = aMetaSize.Width(); |
141 | | pMF->yExt = aMetaSize.Height(); |
142 | | } |
143 | | else |
144 | | { |
145 | | Size aWinSize = OutputDevice::LogicToLogic( Size( aMetaSize.Width(), aMetaSize.Height() ), |
146 | | pGDIMeta->GetPrefMapMode(), |
147 | | aWinMode ); |
148 | | pMF->xExt = aWinSize.Width(); |
149 | | pMF->yExt = aWinSize.Height(); |
150 | | } |
151 | | |
152 | | GlobalUnlock(hMemory); |
153 | | } |
154 | | pResult = static_cast<void*>(hMemory); |
155 | | } |
156 | | else |
157 | | DeleteMetaFile( hMeta ); |
158 | | } |
159 | | } |
160 | | } |
161 | | } |
162 | | #else |
163 | 0 | (void)pGDIMeta; // unused |
164 | 0 | (void)aMetaSize; // unused |
165 | 0 | #endif |
166 | | |
167 | |
|
168 | 0 | return pResult; |
169 | 0 | } |
170 | | |
171 | | |
172 | | // static |
173 | | bool GraphicHelper::getThumbnailFormatFromBitmap_Impl(const BitmapEx& rBitmap, const uno::Reference<io::XStream>& xStream) |
174 | 0 | { |
175 | 0 | if (rBitmap.IsEmpty() || !xStream.is()) |
176 | 0 | return false; |
177 | | |
178 | 0 | std::unique_ptr<SvStream> pStream(utl::UcbStreamHelper::CreateStream(xStream)); |
179 | |
|
180 | 0 | if (pStream->GetError()) |
181 | 0 | return false; |
182 | | |
183 | 0 | BitmapEx bitmap(rBitmap); |
184 | 0 | bitmap.Convert(BmpConversion::N8BitColors); |
185 | |
|
186 | 0 | GraphicFilter& rFilter = GraphicFilter::GetGraphicFilter(); |
187 | |
|
188 | 0 | if (rFilter.compressAsPNG(bitmap, *pStream) != ERRCODE_NONE) |
189 | 0 | return false; |
190 | | |
191 | 0 | pStream->FlushBuffer(); |
192 | |
|
193 | 0 | return !pStream->GetError(); |
194 | 0 | } |
195 | | |
196 | | // static |
197 | | bool GraphicHelper::getThumbnailReplacement_Impl(std::u16string_view rResID, const uno::Reference< io::XStream >& xStream ) |
198 | 0 | { |
199 | 0 | bool bResult = false; |
200 | 0 | if (!rResID.empty() && xStream.is()) |
201 | 0 | { |
202 | 0 | const uno::Reference< uno::XComponentContext >& xContext = ::comphelper::getProcessComponentContext(); |
203 | 0 | try |
204 | 0 | { |
205 | 0 | uno::Reference< graphic::XGraphicProvider > xGraphProvider(graphic::GraphicProvider::create(xContext)); |
206 | 0 | const OUString aURL{OUString::Concat("private:graphicrepository/") + rResID}; |
207 | |
|
208 | 0 | uno::Sequence< beans::PropertyValue > aMediaProps{ comphelper::makePropertyValue(u"URL"_ustr, |
209 | 0 | aURL) }; |
210 | |
|
211 | 0 | uno::Reference< graphic::XGraphic > xGraphic = xGraphProvider->queryGraphic( aMediaProps ); |
212 | 0 | if ( xGraphic.is() ) |
213 | 0 | { |
214 | 0 | uno::Sequence< beans::PropertyValue > aStoreProps{ |
215 | 0 | comphelper::makePropertyValue(u"OutputStream"_ustr, xStream), |
216 | 0 | comphelper::makePropertyValue(u"MimeType"_ustr, u"image/png"_ustr) |
217 | 0 | }; |
218 | |
|
219 | 0 | xGraphProvider->storeGraphic( xGraphic, aStoreProps ); |
220 | 0 | bResult = true; |
221 | 0 | } |
222 | 0 | } |
223 | 0 | catch(const uno::Exception&) |
224 | 0 | { |
225 | 0 | } |
226 | 0 | } |
227 | |
|
228 | 0 | return bResult; |
229 | 0 | } |
230 | | |
231 | | // static |
232 | | OUString GraphicHelper::getThumbnailReplacementIDByFactoryName_Impl( std::u16string_view aFactoryShortName ) |
233 | 0 | { |
234 | 0 | OUString sResult; |
235 | |
|
236 | 0 | if ( aFactoryShortName == u"scalc" ) |
237 | 0 | { |
238 | 0 | sResult = BMP_128X128_CALC_DOC; |
239 | 0 | } |
240 | 0 | else if ( aFactoryShortName == u"sdraw" ) |
241 | 0 | { |
242 | 0 | sResult = BMP_128X128_DRAW_DOC; |
243 | 0 | } |
244 | 0 | else if ( aFactoryShortName == u"simpress" ) |
245 | 0 | { |
246 | 0 | sResult = BMP_128X128_IMPRESS_DOC; |
247 | 0 | } |
248 | 0 | else if ( aFactoryShortName == u"smath" ) |
249 | 0 | { |
250 | 0 | sResult = BMP_128X128_MATH_DOC; |
251 | 0 | } |
252 | 0 | else if ( aFactoryShortName == u"swriter" || o3tl::starts_with(aFactoryShortName, u"swriter/") ) |
253 | 0 | { |
254 | 0 | sResult = BMP_128X128_WRITER_DOC; |
255 | 0 | } |
256 | |
|
257 | 0 | return sResult; |
258 | 0 | } |
259 | | |
260 | | /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |