1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 .
22 #include "FmtFilter.hxx"
23 #include <osl/diagnose.h>
24 #include <comphelper/sequence.hxx>
27 #pragma warning(push,1)
28 #pragma warning(disable:4917)
43 #include <systools/win32/comtools.hxx>
45 using namespace com::sun::star::uno
;
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
);
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
) };
113 pMFHeader
->key
= 0x9AC6CDD7L
;
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 );
129 // convert a windows enhanced metafile to a openoffice metafile
131 Sequence
< sal_Int8
> SAL_CALL
WinENHMFPictToOOMFPict( HENHMETAFILE hEnhMetaFile
)
133 Sequence
< sal_Int8
> aRet
;
137 ( ( nSize
= GetEnhMetaFileBits( hEnhMetaFile
, 0, NULL
) ) != 0 ) )
139 aRet
.realloc( nSize
);
141 if( GetEnhMetaFileBits( hEnhMetaFile
, nSize
, (unsigned char*) aRet
.getArray() ) != nSize
)
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() );
157 METAFILEPICT
* pPict
= (METAFILEPICT
*) GlobalLock( hPict
= GlobalAlloc( GHND
, sizeof( METAFILEPICT
) ) );
164 GlobalUnlock( 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() );
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
)))
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
;
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
) );
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
249 Sequence< sal_Int8 > SAL_CALL TextHtmlToHTMLFormat( Sequence< sal_Int8 >& aTextHtml )
251 OSL_ASSERT( aTextHtml.getLength( ) > 0 );
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
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
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
283 OString startBodyTag( "<BODY" );
284 OString endBodyTag( "</BODY" );
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 ) );
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
320 static_cast< LPVOID >( aHTMLFmtSequence.getArray( ) ),
321 static_cast< LPVOID >( aHTMLFmtHdr ), lHTMLFmtHdr );
323 // concat the text/html
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());
385 static_cast<void*>(byteSequence
.getArray()),
386 static_cast<const void*>(htmlFormat
.c_str()),
387 htmlFormat
.length());
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
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
));
427 sal::systools::COMReference
<IPersistFile
> pIPersistFile
=
428 pIShellLink
.QueryInterface
<IPersistFile
>(IID_IPersistFile
);
430 hr
= pIPersistFile
->Load(aLnkFile
.c_str(), STGM_READ
);
434 hr
= pIShellLink
->Resolve(NULL
, SLR_UPDATE
| SLR_NO_UI
);
438 char pathA
[MAX_PATH
];
440 hr
= pIShellLink
->GetPath(pathA
, MAX_PATH
, &wfd
, SLGP_RAWPATH
);
444 wchar_t pathW
[MAX_PATH
];
445 MultiByteToWideChar(CP_ACP
, 0, pathA
, -1, pathW
, MAX_PATH
);
448 catch(sal::systools::ComError
& ex
)
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() )
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
)
479 size_t size
= CalcSizeForStringListBuffer(fileList
);
484 wchar_t* p
= reinterpret_cast<wchar_t*>(bseq
.getArray());
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);
498 ByteSequence_t
CF_HDROPToFileList(HGLOBAL hGlobal
)
500 UINT nFiles
= DragQueryFileW((HDROP
)hGlobal
, 0xFFFFFFFF, NULL
, 0);
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
;
522 if( GetBitmapDimensionEx( aHBMP
, &aBmpSize
) )
524 // fill bitmap info header
525 size_t nDataBytes
= 4 * aBmpSize
.cy
* aBmpSize
.cy
;
526 Sequence
< sal_Int8
> aBitmapStream(
530 PBITMAPINFOHEADER pBmp
= (PBITMAPINFOHEADER
)aBitmapStream
.getArray();
531 pBmp
->biSize
= sizeof( BITMAPINFOHEADER
);
532 pBmp
->biWidth
= aBmpSize
.cx
;
533 pBmp
->biHeight
= aBmpSize
.cy
;
535 pBmp
->biBitCount
= 32;
536 pBmp
->biCompression
= BI_RGB
;
537 pBmp
->biSizeImage
= (DWORD
)nDataBytes
;
538 pBmp
->biXPelsPerMeter
= 1000;
539 pBmp
->biYPelsPerMeter
= 1000;
541 pBmp
->biClrImportant
= 0;
542 if( GetDIBits( 0, // DC, 0 is a default GC, basically that of the desktop
545 aBitmapStream
.getArray() + sizeof(BITMAPINFO
),
549 ooBmpStream
= WinDIBToOOBMP( aBitmapStream
);
556 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */