LibreOffice Module shell (master) 1
thumbviewer.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#ifndef DONT_HAVE_GDIPLUS
21
22
23#include <global.hxx>
24
25#include <thumbviewer.hxx>
26#include <shlxthdl.hxx>
27#include <registry.hxx>
28#include <fileextensions.hxx>
29#include <config.hxx>
30#include <zipfile.hxx>
31#include <utilities.hxx>
32
33#include <resource.h>
34
35#include <stdio.h>
36#include <utility>
37#include <stdlib.h>
38
39#include <shellapi.h>
40
41#include <memory>
42
43namespace internal
44{
45 /* The signet.png used for thumbnails of signed documents
46 is contained as resource in this module, the resource
47 id is 2000 */
48 static void LoadSignetImageFromResource(ZipFile::ZipContentBuffer_t& buffer)
49 {
50 HRSRC hrc = FindResourceW(g_hModule, L"#2000", reinterpret_cast<LPWSTR>(RT_RCDATA));
51 DWORD size = SizeofResource(g_hModule, hrc);
52 HGLOBAL hglob = LoadResource(g_hModule, hrc);
53 char* data = static_cast<char*>(LockResource(hglob));
54 buffer = ZipFile::ZipContentBuffer_t(data, data + size);
55 }
56
57 static bool IsSignedDocument(const ZipFile* zipfile)
58 {
59 return zipfile->HasContent("META-INF/documentsignatures.xml");
60 }
61
62 static Gdiplus::Point CalcSignetPosition(
63 const Gdiplus::Rect& canvas, const Gdiplus::Rect& thumbnail_border, const Gdiplus::Rect& signet)
64 {
65 int x = 0;
66 int y = 0;
67 int hoffset = canvas.GetRight() - thumbnail_border.GetRight();
68 int voffset = canvas.GetBottom() - thumbnail_border.GetBottom();
69
70 if (hoffset > voffset)
71 {
72 x = thumbnail_border.GetRight() - signet.GetRight() + min(signet.GetRight() / 2, hoffset);
73 y = thumbnail_border.GetBottom() - signet.GetBottom();
74 }
75 else
76 {
77 x = thumbnail_border.GetRight() - signet.GetRight();
78 y = thumbnail_border.GetBottom() - signet.GetBottom() + min(signet.GetBottom() / 2, voffset);
79 }
80
81 return Gdiplus::Point(x,y);
82 }
83}
84
85namespace {
86
87Gdiplus::Rect CalcScaledAspectRatio(const Gdiplus::Rect& src, const Gdiplus::Rect& dest)
88{
89 Gdiplus::Rect result;
90 if (src.Width >= src.Height)
91 result = Gdiplus::Rect(0, 0, dest.Width, src.Height * dest.Width / src.Width);
92 else
93 result = Gdiplus::Rect(0, 0, src.Width * dest.Height / src.Height, dest.Height);
94
95 return result;
96}
97
98class StreamOnZipBuffer final : public IStream
99{
100public:
101 explicit StreamOnZipBuffer(const ZipFile::ZipContentBuffer_t& zip_buffer);
102
103 // IUnknown
104 virtual ULONG STDMETHODCALLTYPE AddRef() override;
105 virtual ULONG STDMETHODCALLTYPE Release() override;
106 virtual HRESULT STDMETHODCALLTYPE QueryInterface(REFIID riid, void __RPC_FAR *__RPC_FAR *ppvObject) override;
107
108 // IStream
109 virtual HRESULT STDMETHODCALLTYPE Read(void *pv, ULONG cb, ULONG *pcbRead) override;
110 virtual HRESULT STDMETHODCALLTYPE Write(void const *pv, ULONG cb, ULONG *pcbWritten) override;
111 virtual HRESULT STDMETHODCALLTYPE Seek(LARGE_INTEGER dlibMove, DWORD dwOrigin, ULARGE_INTEGER *plibNewPosition) override;
112 virtual HRESULT STDMETHODCALLTYPE SetSize(ULARGE_INTEGER libNewSize) override;
113 virtual HRESULT STDMETHODCALLTYPE CopyTo(IStream *pstm, ULARGE_INTEGER cb, ULARGE_INTEGER *pcbRead, ULARGE_INTEGER *pcbWritten) override;
114 virtual HRESULT STDMETHODCALLTYPE Commit(DWORD grfCommitFlags) override;
115 virtual HRESULT STDMETHODCALLTYPE Revert() override;
116 virtual HRESULT STDMETHODCALLTYPE LockRegion(ULARGE_INTEGER libOffset, ULARGE_INTEGER cb, DWORD dwLockType) override;
117 virtual HRESULT STDMETHODCALLTYPE UnlockRegion(ULARGE_INTEGER libOffset, ULARGE_INTEGER cb, DWORD dwLockType) override;
118 virtual HRESULT STDMETHODCALLTYPE Stat(STATSTG *pstatstg, DWORD grfStatFlag) override;
119 virtual HRESULT STDMETHODCALLTYPE Clone(IStream **ppstm) override;
120
121private:
122 LONG ref_count_;
123 const ZipFile::ZipContentBuffer_t& ref_zip_buffer_;
124 size_t pos_;
125};
126
127}
128
129StreamOnZipBuffer::StreamOnZipBuffer(const ZipFile::ZipContentBuffer_t& zip_buffer) :
130 ref_count_(1),
131 ref_zip_buffer_(zip_buffer),
132 pos_(0)
133{
134}
135
136// IUnknown methods
137
138ULONG STDMETHODCALLTYPE StreamOnZipBuffer::AddRef()
139{
140 return InterlockedIncrement(&ref_count_);
141}
142
143ULONG STDMETHODCALLTYPE StreamOnZipBuffer::Release()
144{
145 LONG refcnt = InterlockedDecrement(&ref_count_);
146
147 if (0 == ref_count_)
148 delete this;
149
150 return refcnt;
151}
152
153HRESULT STDMETHODCALLTYPE StreamOnZipBuffer::QueryInterface(REFIID riid, void __RPC_FAR *__RPC_FAR *ppvObject)
154{
155 *ppvObject = nullptr;
156 IUnknown* pUnk = nullptr;
157
158 if ((IID_IUnknown == riid) || (IID_IStream == riid))
159 {
160 pUnk = this;
161 pUnk->AddRef();
162 *ppvObject = pUnk;
163 return S_OK;
164 }
165 return E_NOINTERFACE;
166}
167
168HRESULT STDMETHODCALLTYPE StreamOnZipBuffer::Read(void *pv, ULONG cb, ULONG *pcbRead)
169{
170 if (pv == nullptr)
171 return STG_E_INVALIDPOINTER;
172
173 size_t size = ref_zip_buffer_.size();
174
175 if (pos_ > size)
176 return S_FALSE;
177
178 char* p = static_cast<char*>(pv);
179 ULONG read = 0;
180
181 for ( ;(pos_ < size) && (cb > 0); pos_++, cb--, read++)
182 *p++ = ref_zip_buffer_[pos_];
183
184 if (pcbRead)
185 *pcbRead = read;
186
187 return S_OK;
188}
189
190HRESULT STDMETHODCALLTYPE StreamOnZipBuffer::Seek(LARGE_INTEGER dlibMove, DWORD dwOrigin, ULARGE_INTEGER *)
191{
192 __int64 size = static_cast<__int64>(ref_zip_buffer_.size());
193 __int64 p = 0;
194
195 switch (dwOrigin)
196 {
197 case STREAM_SEEK_SET:
198 break;
199 case STREAM_SEEK_CUR:
200 p = static_cast<__int64>(pos_);
201 break;
202 case STREAM_SEEK_END:
203 p = size - 1;
204 break;
205 }
206
207 HRESULT hr = STG_E_INVALIDFUNCTION;
208
209 p += dlibMove.QuadPart;
210
211 if ( ( p >= 0 ) && (p < size) )
212 {
213 pos_ = static_cast<size_t>(p);
214 hr = S_OK;
215 }
216 return hr;
217}
218
219HRESULT STDMETHODCALLTYPE StreamOnZipBuffer::Stat(STATSTG *pstatstg, DWORD grfStatFlag)
220{
221 if (pstatstg == nullptr)
222 return STG_E_INVALIDPOINTER;
223
224 ZeroMemory(pstatstg, sizeof(STATSTG));
225
226 if (grfStatFlag == STATFLAG_DEFAULT)
227 {
228 size_t sz = 4 * sizeof(wchar_t);
229 wchar_t* name = static_cast<wchar_t*>(CoTaskMemAlloc(sz));
230 ZeroMemory(name, sz);
231 memcpy(name, L"png", 3 * sizeof(wchar_t));
232 pstatstg->pwcsName = name;
233 }
234
235 pstatstg->type = STGTY_LOCKBYTES;
236
237 ULARGE_INTEGER uli;
238 uli.LowPart = static_cast<DWORD>(ref_zip_buffer_.size());
239 uli.HighPart = 0;
240
241 pstatstg->cbSize = uli;
242
243 return S_OK;
244}
245
246HRESULT STDMETHODCALLTYPE StreamOnZipBuffer::Write(void const *, ULONG, ULONG *)
247{ return E_NOTIMPL; }
248
249HRESULT STDMETHODCALLTYPE StreamOnZipBuffer::SetSize(ULARGE_INTEGER)
250{ return E_NOTIMPL; }
251
252HRESULT STDMETHODCALLTYPE StreamOnZipBuffer::CopyTo(IStream *, ULARGE_INTEGER, ULARGE_INTEGER *, ULARGE_INTEGER *)
253{ return E_NOTIMPL; }
254
255HRESULT STDMETHODCALLTYPE StreamOnZipBuffer::Commit(DWORD)
256{ return E_NOTIMPL; }
257
258HRESULT STDMETHODCALLTYPE StreamOnZipBuffer::Revert()
259{ return E_NOTIMPL; }
260
261HRESULT STDMETHODCALLTYPE StreamOnZipBuffer::LockRegion(ULARGE_INTEGER, ULARGE_INTEGER, DWORD)
262{ return E_NOTIMPL; }
263
264HRESULT STDMETHODCALLTYPE StreamOnZipBuffer::UnlockRegion(ULARGE_INTEGER, ULARGE_INTEGER, DWORD)
265{ return E_NOTIMPL; }
266
267HRESULT STDMETHODCALLTYPE StreamOnZipBuffer::Clone(IStream **)
268{ return E_NOTIMPL; }
269
270
272 ref_count_(RefCnt)
273{
274 InterlockedIncrement(&g_DllRefCnt);
275
276 thumbnail_size_.cx = 0;
277 thumbnail_size_.cy = 0;
278
279 Gdiplus::GdiplusStartupInput gdiplusStartupInput;
280 Gdiplus::GdiplusStartup(&gdiplus_token_, &gdiplusStartupInput, nullptr);
281
282 ZipFile::ZipContentBuffer_t img_data;
284 IStream* stream = new StreamOnZipBuffer(img_data);
285 signet_ = new Gdiplus::Bitmap(stream, TRUE);
286 stream->Release();
287}
288
290{
291 delete signet_;
292 Gdiplus::GdiplusShutdown(gdiplus_token_);
293 InterlockedDecrement(&g_DllRefCnt);
294}
295
296// IUnknown methods
297
298HRESULT STDMETHODCALLTYPE CThumbviewer::QueryInterface(REFIID riid, void __RPC_FAR *__RPC_FAR *ppvObject)
299{
300 *ppvObject = nullptr;
301 IUnknown* pUnk = nullptr;
302
303 if ((IID_IUnknown == riid) || (IID_IPersistFile == riid))
304 {
305 pUnk = static_cast<IPersistFile*>(this);
306 pUnk->AddRef();
307 *ppvObject = pUnk;
308 return S_OK;
309 }
310 else if (IID_IExtractImage == riid)
311 {
312 pUnk = static_cast<IExtractImage*>(this);
313 pUnk->AddRef();
314 *ppvObject = pUnk;
315 return S_OK;
316 }
317 return E_NOINTERFACE;
318}
319
320ULONG STDMETHODCALLTYPE CThumbviewer::AddRef()
321{
322 return InterlockedIncrement(&ref_count_);
323}
324
325ULONG STDMETHODCALLTYPE CThumbviewer::Release()
326{
327 LONG refcnt = InterlockedDecrement(&ref_count_);
328
329 if (0 == ref_count_)
330 delete this;
331
332 return refcnt;
333}
334
335// IExtractImage2 methods
336
337const std::string THUMBNAIL_CONTENT = "Thumbnails/thumbnail.png";
338
339HRESULT STDMETHODCALLTYPE CThumbviewer::Extract(HBITMAP *phBmpImage)
340{
341 HRESULT hr = E_FAIL;
342
343 try
344 {
345 std::wstring fname = getShortPathName( filename_ );
346 std::unique_ptr<ZipFile> zipfile( new ZipFile( fname ) );
347
348 if (zipfile->HasContent(THUMBNAIL_CONTENT))
349 {
350 ZipFile::ZipContentBuffer_t thumbnail;
351 zipfile->GetUncompressedContent(THUMBNAIL_CONTENT, thumbnail);
352 IStream* stream = new StreamOnZipBuffer(thumbnail);
353
354 Gdiplus::Bitmap thumbnail_png(stream, TRUE);
355
356 if ((thumbnail_png.GetHeight() == 0) || (thumbnail_png.GetWidth() == 0))
357 {
358 stream->Release();
359 return E_FAIL;
360 }
361
362 HWND hwnd = GetDesktopWindow();
363 HDC hdc = GetDC(hwnd);
364 HDC memDC = CreateCompatibleDC(hdc);
365
366 if (memDC)
367 {
368 UINT offset = 3; // reserve a little border space
369
370 Gdiplus::Rect canvas(0, 0, thumbnail_size_.cx, thumbnail_size_.cy);
371 Gdiplus::Rect canvas_thumbnail(offset, offset, thumbnail_size_.cx - 2 * offset, thumbnail_size_.cy - 2 * offset);
372
373 Gdiplus::Rect scaledRect = CalcScaledAspectRatio(
374 Gdiplus::Rect(0, 0, thumbnail_png.GetWidth(), thumbnail_png.GetHeight()), canvas_thumbnail);
375
376 struct {
377 BITMAPINFOHEADER bi;
378 DWORD ct[256];
379 } dib;
380
381 ZeroMemory(&dib, sizeof(dib));
382
383 dib.bi.biSize = sizeof(BITMAPINFOHEADER);
384 dib.bi.biWidth = thumbnail_size_.cx;
385 dib.bi.biHeight = thumbnail_size_.cy;
386 dib.bi.biPlanes = 1;
387 dib.bi.biBitCount = static_cast<WORD>(color_depth_);
388 dib.bi.biCompression = BI_RGB;
389
390 LPVOID lpBits;
391 HBITMAP hMemBmp = CreateDIBSection(memDC, reinterpret_cast<LPBITMAPINFO>(&dib), DIB_RGB_COLORS, &lpBits, nullptr, 0);
392 HGDIOBJ hOldObj = SelectObject(memDC, hMemBmp);
393
394 Gdiplus::Graphics graphics(memDC);
395 Gdiplus::Pen blackPen(Gdiplus::Color(255, 0, 0, 0), 1);
396
397 Gdiplus::SolidBrush whiteBrush(Gdiplus::Color(255, 255, 255, 255));
398 graphics.FillRectangle(&whiteBrush, canvas);
399
400 scaledRect.X = (canvas.Width - scaledRect.Width) / 2;
401 scaledRect.Y = (canvas.Height - scaledRect.Height) / 2;
402
403 Gdiplus::Rect border_rect(scaledRect.X, scaledRect.Y, scaledRect.Width, scaledRect.Height);
404 graphics.DrawRectangle(&blackPen, border_rect);
405
406 scaledRect.X += 1;
407 scaledRect.Y += 1;
408 scaledRect.Width -= 1;
409 scaledRect.Height -= 1;
410
411 graphics.SetInterpolationMode(Gdiplus::InterpolationModeHighQualityBicubic);
412 Gdiplus::Status stat = graphics.DrawImage(
413 &thumbnail_png, scaledRect, 0 , 0,
414 thumbnail_png.GetWidth(), thumbnail_png.GetHeight(), Gdiplus::UnitPixel);
415
416 /* Add a signet sign to the thumbnail of signed documents */
417 if (internal::IsSignedDocument(zipfile.get()))
418 {
419 double SCALING_FACTOR = 0.6;
420 Gdiplus::Rect signet_scaled(
421 0, 0, static_cast<INT>(signet_->GetWidth() * SCALING_FACTOR), static_cast<INT>(signet_->GetHeight() * SCALING_FACTOR));
422 Gdiplus::Point pos_signet = internal::CalcSignetPosition(canvas_thumbnail, border_rect, signet_scaled);
423 Gdiplus::Rect dest(pos_signet.X, pos_signet.Y, signet_scaled.GetRight(), signet_scaled.GetBottom());
424
425 stat = graphics.DrawImage(
426 signet_, dest,
427 0, 0, signet_->GetWidth(), signet_->GetHeight(),
428 Gdiplus::UnitPixel);
429 }
430
431 if (stat == Gdiplus::Ok)
432 {
433 *phBmpImage = hMemBmp;
434 hr = NOERROR;
435 }
436
437 SelectObject(memDC, hOldObj);
438 DeleteDC(memDC);
439 }
440
441 ReleaseDC(hwnd, hdc);
442 stream->Release();
443 }
444 }
445 catch(std::exception&)
446 {
447 OutputDebugStringFormatW( L"CThumbviewer Extract ERROR!\n" );
448 hr = E_FAIL;
449 }
450 return hr;
451}
452
453HRESULT STDMETHODCALLTYPE CThumbviewer::GetLocation(
454 LPWSTR pszPathBuffer, DWORD cchMax, DWORD *pdwPriority, const SIZE *prgSize, DWORD dwRecClrDepth, DWORD *pdwFlags)
455{
456 if ((prgSize == nullptr) || (pdwFlags == nullptr) || ((*pdwFlags & IEIFLAG_ASYNC) && (pdwPriority == nullptr)))
457 return E_INVALIDARG;
458
459 thumbnail_size_ = *prgSize;
460 color_depth_ = dwRecClrDepth;
461
462 *pdwFlags = IEIFLAG_CACHE; // we don't cache the image
463
464 wcsncpy(pszPathBuffer, filename_.c_str(), cchMax);
465
466 return NOERROR;
467}
468
469// IPersist methods
470
471HRESULT STDMETHODCALLTYPE CThumbviewer::GetClassID(CLSID* pClassID)
472{
473 *pClassID = CLSID_THUMBVIEWER_HANDLER;
474 return S_OK;
475}
476
477// IPersistFile methods
478
479HRESULT STDMETHODCALLTYPE CThumbviewer::Load(LPCOLESTR pszFileName, DWORD)
480{
481 filename_ = pszFileName;
482 return S_OK;
483}
484
485HRESULT STDMETHODCALLTYPE CThumbviewer::IsDirty()
486{ return E_NOTIMPL; }
487
488HRESULT STDMETHODCALLTYPE CThumbviewer::Save(LPCOLESTR, BOOL)
489{ return E_NOTIMPL; }
490
491HRESULT STDMETHODCALLTYPE CThumbviewer::SaveCompleted(LPCOLESTR)
492{ return E_NOTIMPL; }
493
494HRESULT STDMETHODCALLTYPE CThumbviewer::GetCurFile(LPOLESTR __RPC_FAR*)
495{ return E_NOTIMPL; }
496
497
498#endif // DONT_HAVE_GDIPLUS
499
500/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
virtual HRESULT STDMETHODCALLTYPE GetCurFile(LPOLESTR __RPC_FAR *ppszFileName) override
virtual HRESULT STDMETHODCALLTYPE Extract(HBITMAP *phBmpImage) override
virtual HRESULT STDMETHODCALLTYPE Load(LPCOLESTR pszFileName, DWORD dwMode) override
CThumbviewer(LONG RefCnt=1)
virtual ULONG STDMETHODCALLTYPE AddRef() override
SIZE thumbnail_size_
Definition: thumbviewer.hxx:91
ULONG_PTR gdiplus_token_
Definition: thumbviewer.hxx:93
virtual HRESULT STDMETHODCALLTYPE Save(LPCOLESTR pszFileName, BOOL fRemember) override
virtual ULONG STDMETHODCALLTYPE Release() override
virtual HRESULT STDMETHODCALLTYPE QueryInterface(REFIID riid, void __RPC_FAR *__RPC_FAR *ppvObject) override
DWORD color_depth_
Definition: thumbviewer.hxx:92
virtual HRESULT STDMETHODCALLTYPE GetLocation(LPWSTR pszPathBuffer, DWORD cchMax, DWORD *pdwPriority, const SIZE *prgSize, DWORD dwRecClrDepth, DWORD *pdwFlags) override
virtual HRESULT STDMETHODCALLTYPE GetClassID(CLSID *pClassID) override
virtual HRESULT STDMETHODCALLTYPE IsDirty() override
virtual ~CThumbviewer()
virtual HRESULT STDMETHODCALLTYPE SaveCompleted(LPCOLESTR pszFileName) override
Gdiplus::Bitmap * signet_
Definition: thumbviewer.hxx:94
std::wstring filename_
Definition: thumbviewer.hxx:90
Reference< XOutputStream > stream
float y
float x
#define TRUE
const char * name
void * p
unsigned short WORD
Definition: lngconvex.cxx:25
size
static void LoadSignetImageFromResource(ZipFile::ZipContentBuffer_t &buffer)
Definition: thumbviewer.cxx:48
static Gdiplus::Point CalcSignetPosition(const Gdiplus::Rect &canvas, const Gdiplus::Rect &thumbnail_border, const Gdiplus::Rect &signet)
Definition: thumbviewer.cxx:62
static bool IsSignedDocument(const ZipFile *zipfile)
Definition: thumbviewer.cxx:57
css::uno::Reference< css::animations::XAnimationNode > Clone(const css::uno::Reference< css::animations::XAnimationNode > &xSourceNode, const SdPage *pSource=nullptr, const SdPage *pTarget=nullptr)
SwNodeOffset min(const SwNodeOffset &a, const SwNodeOffset &b)
static HINSTANCE g_hModule
Definition: propertyhdl.cxx:40
LONG g_DllRefCnt
Definition: propertyhdl.cxx:39
const wchar_t *typedef BOOL
LONG
const CLSID CLSID_THUMBVIEWER_HANDLER
Definition: shlxthdl.hxx:39
return hr
const std::string THUMBNAIL_CONTENT
Any result
std::wstring getShortPathName(const std::wstring &aLongName)
helper function to convert windows paths to short form.
Definition: utilities.cxx:122
void OutputDebugStringFormatW(LPCWSTR pFormat,...)
Definition: utilities.hxx:78