fix baseline build (old cairo) - 'cairo_rectangle_int_t' does not name a type
[LibreOffice.git] / dtrans / source / win32 / dtobj / FmtFilter.cxx
blob406376289ba74ae33dfd33b5155b884d12ff9781
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <string.h>
22 #include "FmtFilter.hxx"
23 #include <osl/diagnose.h>
24 #include <comphelper/sequence.hxx>
26 #if defined _MSC_VER
27 #pragma warning(push,1)
28 #pragma warning(disable:4917)
29 #endif
30 #include <shobjidl.h>
31 #include <shlguid.h>
32 #include <objidl.h>
33 #include <shellapi.h>
34 #if defined _MSC_VER
35 #pragma warning(pop)
36 #endif
38 #include <string>
39 #include <sstream>
40 #include <vector>
41 #include <iomanip>
43 #include <systools/win32/comtools.hxx>
45 using namespace com::sun::star::uno;
47 #pragma pack(2)
48 struct METAFILEHEADER
50 DWORD key;
51 short hmf;
52 SMALL_RECT bbox;
53 WORD inch;
54 DWORD reserved;
55 WORD checksum;
57 #pragma pack()
59 // convert a windows metafile picture to a openoffice metafile picture
61 Sequence< sal_Int8 > SAL_CALL WinMFPictToOOMFPict( Sequence< sal_Int8 >& aMetaFilePict )
63 OSL_ASSERT( aMetaFilePict.getLength( ) == sizeof( METAFILEPICT ) );
65 Sequence< sal_Int8 > mfpictStream;
66 METAFILEPICT* pMFPict = reinterpret_cast< METAFILEPICT* >( aMetaFilePict.getArray( ) );
67 HMETAFILE hMf = pMFPict->hMF;
68 sal_uInt32 nCount = GetMetaFileBitsEx( hMf, 0, NULL );
70 if ( nCount > 0 )
72 mfpictStream.realloc( nCount + sizeof( METAFILEHEADER ) );
74 METAFILEHEADER* pMFHeader = reinterpret_cast< METAFILEHEADER* >( mfpictStream.getArray( ) );
75 SMALL_RECT aRect = { 0,
77 static_cast< short >( pMFPict->xExt ),
78 static_cast< short >( pMFPict->yExt ) };
79 USHORT nInch;
81 switch( pMFPict->mm )
83 case MM_TEXT:
84 nInch = 72;
85 break;
87 case MM_LOMETRIC:
88 nInch = 100;
89 break;
91 case MM_HIMETRIC:
92 nInch = 1000;
93 break;
95 case MM_LOENGLISH:
96 nInch = 254;
97 break;
99 case MM_HIENGLISH:
100 case MM_ISOTROPIC:
101 case MM_ANISOTROPIC:
102 nInch = 2540;
103 break;
105 case MM_TWIPS:
106 nInch = 1440;
107 break;
109 default:
110 nInch = 576;
113 pMFHeader->key = 0x9AC6CDD7L;
114 pMFHeader->hmf = 0;
115 pMFHeader->bbox = aRect;
116 pMFHeader->inch = nInch;
117 pMFHeader->reserved = 0;
118 pMFHeader->checksum = 0;
120 char* pMFBuff = reinterpret_cast< char* >( mfpictStream.getArray( ) );
122 nCount = GetMetaFileBitsEx( pMFPict->hMF, nCount, pMFBuff + sizeof( METAFILEHEADER ) );
123 OSL_ASSERT( nCount > 0 );
126 return mfpictStream;
129 // convert a windows enhanced metafile to a openoffice metafile
131 Sequence< sal_Int8 > SAL_CALL WinENHMFPictToOOMFPict( HENHMETAFILE hEnhMetaFile )
133 Sequence< sal_Int8 > aRet;
134 UINT nSize = 0;
136 if( hEnhMetaFile &&
137 ( ( nSize = GetEnhMetaFileBits( hEnhMetaFile, 0, NULL ) ) != 0 ) )
139 aRet.realloc( nSize );
141 if( GetEnhMetaFileBits( hEnhMetaFile, nSize, (unsigned char*) aRet.getArray() ) != nSize )
142 aRet.realloc( 0 );
145 return aRet;
148 // convert a openoffice metafile picture to a windows metafile picture
150 HMETAFILEPICT SAL_CALL OOMFPictToWinMFPict( Sequence< sal_Int8 >& aOOMetaFilePict )
152 HMETAFILEPICT hPict = NULL;
153 HMETAFILE hMtf = SetMetaFileBitsEx( aOOMetaFilePict.getLength(), (unsigned char*) aOOMetaFilePict.getConstArray() );
155 if( hMtf )
157 METAFILEPICT* pPict = (METAFILEPICT*) GlobalLock( hPict = GlobalAlloc( GHND, sizeof( METAFILEPICT ) ) );
159 pPict->mm = 8;
160 pPict->xExt = 0;
161 pPict->yExt = 0;
162 pPict->hMF = hMtf;
164 GlobalUnlock( hPict );
167 return hPict;
170 // convert a openoffice metafile picture to a windows enhanced metafile picture
172 HENHMETAFILE SAL_CALL OOMFPictToWinENHMFPict( Sequence< sal_Int8 >& aOOMetaFilePict )
174 HENHMETAFILE hEnhMtf = SetEnhMetaFileBits( aOOMetaFilePict.getLength(), (unsigned char*) aOOMetaFilePict.getConstArray() );
176 return hEnhMtf;
179 // convert a windows device independent bitmap into a openoffice bitmap
181 Sequence< sal_Int8 > SAL_CALL WinDIBToOOBMP( const Sequence< sal_Int8 >& aWinDIB )
183 OSL_ENSURE(aWinDIB.getLength() > sizeof(BITMAPINFOHEADER), "CF_DIBV5/CF_DIB too small (!)");
184 Sequence< sal_Int8 > ooBmpStream;
186 ooBmpStream.realloc(aWinDIB.getLength( ) + sizeof(BITMAPFILEHEADER));
187 const BITMAPINFOHEADER* pBmpInfoHdr = reinterpret_cast< const BITMAPINFOHEADER* >(aWinDIB.getConstArray());
188 BITMAPFILEHEADER* pBmpFileHdr = reinterpret_cast< BITMAPFILEHEADER* >(ooBmpStream.getArray());
189 const DWORD nSizeInfoOrV5(pBmpInfoHdr->biSize > sizeof(BITMAPINFOHEADER) ? sizeof(BITMAPV5HEADER) : sizeof(BITMAPINFOHEADER));
190 DWORD nOffset(sizeof(BITMAPFILEHEADER) + nSizeInfoOrV5);
192 memcpy(pBmpFileHdr + 1, pBmpInfoHdr, aWinDIB.getLength());
194 if(pBmpInfoHdr->biBitCount <= 8)
196 nOffset += (pBmpInfoHdr->biClrUsed ? pBmpInfoHdr->biClrUsed : (1 << pBmpInfoHdr->biBitCount)) << 2;
198 else if((BI_BITFIELDS == pBmpInfoHdr->biCompression ) && ((16 == pBmpInfoHdr->biBitCount ) || (32 == pBmpInfoHdr->biBitCount )))
200 nOffset += 12;
203 pBmpFileHdr->bfType = ('M' << 8) | 'B';
204 pBmpFileHdr->bfSize = 0; // maybe: nMemSize + sizeof(BITMAPFILEHEADER)
205 pBmpFileHdr->bfReserved1 = 0;
206 pBmpFileHdr->bfReserved2 = 0;
207 pBmpFileHdr->bfOffBits = nOffset;
209 return ooBmpStream;
212 // convert a openoffice bitmap into a windows device independent bitmap
214 Sequence< sal_Int8 > SAL_CALL OOBmpToWinDIB( Sequence< sal_Int8 >& aOOBmp )
216 Sequence< sal_Int8 > winDIBStream( aOOBmp.getLength( ) - sizeof( BITMAPFILEHEADER ) );
218 memcpy( winDIBStream.getArray( ),
219 aOOBmp.getArray( ) + sizeof( BITMAPFILEHEADER ),
220 aOOBmp.getLength( ) - sizeof( BITMAPFILEHEADER ) );
222 return winDIBStream;
225 // converts the openoffice text/html clipboard format to the HTML Format
226 // well known under MS Windows
227 // the MS HTML Format has a header before the real html data
229 // Version:1.0 Version number of the clipboard. Staring is 0.9
230 // StartHTML: Byte count from the beginning of the clipboard to the start
231 // of the context, or -1 if no context
232 // EndHTML: Byte count from the beginning of the clipboard to the end
233 // of the context, or -1 if no context
234 // StartFragment: Byte count from the beginning of the clipboard to the
235 // start of the fragment
236 // EndFragment: Byte count from the beginning of the clipboard to the
237 // end of the fragment
238 // StartSelection: Byte count from the beginning of the clipboard to the
239 // start of the selection
240 // EndSelection: Byte count from the beginning of the clipboard to the
241 // end of the selection
243 // StartSelection and EndSelection are optional
244 // The fragment should be preceded and followed by the HTML comments
245 // <!--StartFragment--> and <!--EndFragment--> (no space between !-- and the
246 // text
249 Sequence< sal_Int8 > SAL_CALL TextHtmlToHTMLFormat( Sequence< sal_Int8 >& aTextHtml )
251 OSL_ASSERT( aTextHtml.getLength( ) > 0 );
253 // check parameter
254 if ( !(aTextHtml.getLength( ) > 0) )
255 return Sequence< sal_Int8 >( );
257 // we create a buffer with the approximated size of
258 // the HTML Format header
259 char aHTMLFmtHdr[120];
261 memset( aHTMLFmtHdr, 0, sizeof( aHTMLFmtHdr ) );
263 // fill the buffer with dummy values to calc the
264 // exact length
266 wsprintf(
267 aHTMLFmtHdr,
268 "Version:1.0\nStartHTML:%010d\r\nnEndHTML:%010d\r\nStartFragment:%010\r\nnEndFragment:%010d\r\n", 0, 0, 0, 0 );
270 sal_uInt32 lHTMLFmtHdr = rtl_str_getLength( aHTMLFmtHdr );
272 // the office always writes the start
273 // and end html tag in upper cases and
274 // without spaces
275 // both tags don't allow parameters
276 OString startHtmlTag( "<HTML>" );
277 OString endHtmlTag( "</HTML>" );
279 // we don't include '>' into the search
280 // because the body tag allows parameters
281 // e.g. <BODY param>
282 // #92840#
283 OString startBodyTag( "<BODY" );
284 OString endBodyTag( "</BODY" );
286 OString textHtml(
287 reinterpret_cast< const sal_Char* >( aTextHtml.getConstArray( ) ),
288 aTextHtml.getLength( ) );
290 sal_Int32 nStartHtml = textHtml.indexOf( startHtmlTag );
291 sal_Int32 nEndHtml = textHtml.indexOf( endHtmlTag );
292 sal_Int32 nStartFrgmt = textHtml.indexOf( startBodyTag );
293 sal_Int32 nEndFrgmt = textHtml.indexOf( endBodyTag );
295 OSL_ASSERT( (nStartHtml >= 0) && (nEndHtml > nStartHtml) && (nStartFrgmt > nStartHtml) && (nEndFrgmt > nStartFrgmt) );
297 Sequence< sal_Int8 > aHTMLFmtSequence;
299 if ( (nStartHtml > -1) && (nEndHtml > -1) && (nStartFrgmt > -1) && (nEndFrgmt > -1) )
301 nStartHtml = nStartHtml + lHTMLFmtHdr - 1; // we start one before <HTML> Word 2000 does also so
302 nEndHtml = nEndHtml + lHTMLFmtHdr + endHtmlTag.getLength( ) + 1; // our SOffice 5.2 wants 2 behind </HTML>?
303 nStartFrgmt = nStartFrgmt + startBodyTag.getLength( ) + lHTMLFmtHdr; // after the <BODY> tag
304 nEndFrgmt = nEndFrgmt + lHTMLFmtHdr;
306 // fill the html header
307 memset( aHTMLFmtHdr, 0, sizeof( aHTMLFmtHdr ) );
309 wsprintf(
310 aHTMLFmtHdr,
311 "Version:1.0\nStartHTML:%010d\r\nEndHTML:%010d\r\nStartFragment:%010d\r\nEndFragment:%010d\r\n",
312 nStartHtml, nEndHtml, nStartFrgmt, nEndFrgmt );
314 // we add space for a trailing \0
315 aHTMLFmtSequence.realloc( lHTMLFmtHdr + aTextHtml.getLength( ) + 1 );
316 memset( aHTMLFmtSequence.getArray( ), 0, aHTMLFmtSequence.getLength( ) );
318 // copy the HTML Format header
319 memcpy(
320 static_cast< LPVOID >( aHTMLFmtSequence.getArray( ) ),
321 static_cast< LPVOID >( aHTMLFmtHdr ), lHTMLFmtHdr );
323 // concat the text/html
324 memcpy(
325 static_cast< LPVOID >( aHTMLFmtSequence.getArray( ) + lHTMLFmtHdr ),
326 static_cast< LPVOID >( aTextHtml.getArray( ) ),
327 aTextHtml.getLength( ) );
330 return aHTMLFmtSequence;
334 std::string GetHtmlFormatHeader(size_t startHtml, size_t endHtml, size_t startFragment, size_t endFragment)
336 std::ostringstream htmlHeader;
337 htmlHeader << "Version:1.0" << '\r' << '\n';
338 htmlHeader << "StartHTML:" << std::setw(10) << std::setfill('0') << std::dec << startHtml << '\r' << '\n';
339 htmlHeader << "EndHTML:" << std::setw(10) << std::setfill('0') << std::dec << endHtml << '\r' << '\n';
340 htmlHeader << "StartFragment:" << std::setw(10) << std::setfill('0') << std::dec << startFragment << '\r' << '\n';
341 htmlHeader << "EndFragment:" << std::setw(10) << std::setfill('0') << std::dec << endFragment << '\r' << '\n';
342 return htmlHeader.str();
345 // the case of these tags has to match what we output in our filters
346 // both tags don't allow parameters
347 const std::string TAG_HTML = std::string("<html>");
348 const std::string TAG_END_HTML = std::string("</html>");
350 // The body tag may have parameters so we need to search for the
351 // closing '>' manually e.g. <body param> #92840#
352 const std::string TAG_BODY = std::string("<body");
353 const std::string TAG_END_BODY = std::string("</body");
355 Sequence<sal_Int8> SAL_CALL TextHtmlToHTMLFormat(Sequence<sal_Int8>& aTextHtml)
357 OSL_ASSERT(aTextHtml.getLength() > 0);
359 if (!(aTextHtml.getLength() > 0))
360 return Sequence<sal_Int8>();
362 // fill the buffer with dummy values to calc the exact length
363 std::string dummyHtmlHeader = GetHtmlFormatHeader(0, 0, 0, 0);
364 size_t lHtmlFormatHeader = dummyHtmlHeader.length();
366 std::string textHtml(
367 reinterpret_cast<const sal_Char*>(aTextHtml.getConstArray()),
368 reinterpret_cast<const sal_Char*>(aTextHtml.getConstArray()) + aTextHtml.getLength());
370 std::string::size_type nStartHtml = textHtml.find(TAG_HTML) + lHtmlFormatHeader - 1; // we start one before '<HTML>' Word 2000 does also so
371 std::string::size_type nEndHtml = textHtml.find(TAG_END_HTML) + lHtmlFormatHeader + TAG_END_HTML.length() + 1; // our SOffice 5.2 wants 2 behind </HTML>?
373 // The body tag may have parameters so we need to search for the
374 // closing '>' manually e.g. <BODY param> #92840#
375 std::string::size_type nStartFragment = textHtml.find(">", textHtml.find(TAG_BODY)) + lHtmlFormatHeader + 1;
376 std::string::size_type nEndFragment = textHtml.find(TAG_END_BODY) + lHtmlFormatHeader;
378 std::string htmlFormat = GetHtmlFormatHeader(nStartHtml, nEndHtml, nStartFragment, nEndFragment);
379 htmlFormat += textHtml;
381 Sequence<sal_Int8> byteSequence(htmlFormat.length() + 1); // space the trailing '\0'
382 memset(byteSequence.getArray(), 0, byteSequence.getLength());
384 memcpy(
385 static_cast<void*>(byteSequence.getArray()),
386 static_cast<const void*>(htmlFormat.c_str()),
387 htmlFormat.length());
389 return byteSequence;
392 std::wstring getFileExtension(const std::wstring& aFilename)
394 std::wstring::size_type idx = aFilename.rfind(L".");
395 if ((idx != std::wstring::npos))
397 return std::wstring(aFilename, idx);
399 return std::wstring();
402 const std::wstring SHELL_LINK_FILE_EXTENSION = L".lnk";
404 bool isShellLink(const std::wstring& aFilename)
406 std::wstring ext = getFileExtension(aFilename);
407 return (_wcsicmp(ext.c_str(), SHELL_LINK_FILE_EXTENSION.c_str()) == 0);
410 /** Resolve a Windows Shell Link (lnk) file. If a resolution
411 is not possible simply return the provided name of the
412 lnk file. */
413 std::wstring getShellLinkTarget(const std::wstring& aLnkFile)
415 OSL_ASSERT(isShellLink(aLnkFile));
417 std::wstring target = aLnkFile;
421 sal::systools::COMReference<IShellLinkA> pIShellLink;
422 HRESULT hr = CoCreateInstance(
423 CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_IShellLink, reinterpret_cast<LPVOID*>(&pIShellLink));
424 if (FAILED(hr))
425 return target;
427 sal::systools::COMReference<IPersistFile> pIPersistFile =
428 pIShellLink.QueryInterface<IPersistFile>(IID_IPersistFile);
430 hr = pIPersistFile->Load(aLnkFile.c_str(), STGM_READ);
431 if (FAILED(hr))
432 return target;
434 hr = pIShellLink->Resolve(NULL, SLR_UPDATE | SLR_NO_UI);
435 if (FAILED(hr))
436 return target;
438 char pathA[MAX_PATH];
439 WIN32_FIND_DATA wfd;
440 hr = pIShellLink->GetPath(pathA, MAX_PATH, &wfd, SLGP_RAWPATH);
441 if (FAILED(hr))
442 return target;
444 wchar_t pathW[MAX_PATH];
445 MultiByteToWideChar(CP_ACP, 0, pathA, -1, pathW, MAX_PATH);
446 target = pathW;
448 catch(sal::systools::ComError& ex)
450 OSL_FAIL(ex.what());
451 (void)ex;
453 return target;
456 typedef std::vector<std::wstring> FileList_t;
457 typedef FileList_t::value_type FileList_ValueType_t;
458 typedef Sequence<sal_Int8> ByteSequence_t;
460 /* Calculate the size required for turning a string list into
461 a double '\0' terminated string buffer */
462 size_t CalcSizeForStringListBuffer(const FileList_t& fileList)
464 if ( fileList.empty() )
465 return 0;
467 size_t size = 1; // one for the very final '\0'
468 FileList_t::const_iterator iter_end = fileList.end();
469 for (FileList_t::const_iterator iter = fileList.begin(); iter != iter_end; ++iter)
471 size += iter->length() + 1; // length including terminating '\0'
473 return (size * sizeof(FileList_ValueType_t::value_type));
476 ByteSequence_t FileListToByteSequence(const FileList_t& fileList)
478 ByteSequence_t bseq;
479 size_t size = CalcSizeForStringListBuffer(fileList);
481 if (size > 0)
483 bseq.realloc(size);
484 wchar_t* p = reinterpret_cast<wchar_t*>(bseq.getArray());
485 ZeroMemory(p, size);
487 FileList_t::const_iterator iter;
488 FileList_t::const_iterator iter_end = fileList.end();
489 for (iter = fileList.begin(); iter != iter_end; ++iter)
491 wcsncpy(p, iter->c_str(), iter->length());
492 p += (iter->length() + 1);
495 return bseq;
498 ByteSequence_t CF_HDROPToFileList(HGLOBAL hGlobal)
500 UINT nFiles = DragQueryFileW((HDROP)hGlobal, 0xFFFFFFFF, NULL, 0);
501 FileList_t files;
503 for (UINT i = 0; i < nFiles; i++)
505 wchar_t buff[MAX_PATH];
506 /*UINT size =*/ DragQueryFileW((HDROP)hGlobal, i, buff, MAX_PATH);
507 std::wstring filename = buff;
508 if (isShellLink(filename))
509 filename = getShellLinkTarget(filename);
510 files.push_back(filename);
512 return FileListToByteSequence(files);
515 // convert a windows bitmap handle into a openoffice bitmap
517 Sequence< sal_Int8 > SAL_CALL WinBITMAPToOOBMP( HBITMAP aHBMP )
519 Sequence< sal_Int8 > ooBmpStream;
521 SIZE aBmpSize;
522 if( GetBitmapDimensionEx( aHBMP, &aBmpSize ) )
524 // fill bitmap info header
525 size_t nDataBytes = 4 * aBmpSize.cy * aBmpSize.cy;
526 Sequence< sal_Int8 > aBitmapStream(
527 sizeof(BITMAPINFO) +
528 nDataBytes
530 PBITMAPINFOHEADER pBmp = (PBITMAPINFOHEADER)aBitmapStream.getArray();
531 pBmp->biSize = sizeof( BITMAPINFOHEADER );
532 pBmp->biWidth = aBmpSize.cx;
533 pBmp->biHeight = aBmpSize.cy;
534 pBmp->biPlanes = 1;
535 pBmp->biBitCount = 32;
536 pBmp->biCompression = BI_RGB;
537 pBmp->biSizeImage = (DWORD)nDataBytes;
538 pBmp->biXPelsPerMeter = 1000;
539 pBmp->biYPelsPerMeter = 1000;
540 pBmp->biClrUsed = 0;
541 pBmp->biClrImportant = 0;
542 if( GetDIBits( 0, // DC, 0 is a default GC, basically that of the desktop
543 aHBMP,
544 0, aBmpSize.cy,
545 aBitmapStream.getArray() + sizeof(BITMAPINFO),
546 (LPBITMAPINFO)pBmp,
547 DIB_RGB_COLORS ) )
549 ooBmpStream = WinDIBToOOBMP( aBitmapStream );
553 return ooBmpStream;
556 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */