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 <osl/thread.h>
21 #include <sot/formats.hxx>
22 #include <tools/stream.hxx>
23 
24 #include <vcl/inetimg.hxx>
25 
26 static const sal_Unicode TOKEN_SEPARATOR = '\001';
27 
Write(SvStream & rOStm,SotClipboardFormatId nFormat) const28 void INetImage::Write( SvStream& rOStm, SotClipboardFormatId nFormat ) const
29 {
30     switch( nFormat )
31     {
32     case SotClipboardFormatId::INET_IMAGE:
33         {
34             OUString sString(
35                 aImageURL + OUStringChar(TOKEN_SEPARATOR) + aTargetURL
36                 + OUStringChar(TOKEN_SEPARATOR) + aTargetFrame
37                 + OUStringChar(TOKEN_SEPARATOR) /* + aAlternateText */
38                 + OUStringChar(TOKEN_SEPARATOR)
39                 + OUString::number(aSizePixel.Width())
40                 + OUStringChar(TOKEN_SEPARATOR)
41                 + OUString::number(aSizePixel.Height()));
42 
43             OString sOut(OUStringToOString(sString,
44                 RTL_TEXTENCODING_UTF8));
45 
46             rOStm.WriteBytes(sOut.getStr(), sOut.getLength());
47             static const sal_Char aEndChar[2] = { 0 };
48             rOStm.WriteBytes(aEndChar, sizeof(aEndChar));
49         }
50         break;
51 
52     case SotClipboardFormatId::NETSCAPE_IMAGE:
53         break;
54     default: break;
55     }
56 }
57 
Read(SvStream & rIStm,SotClipboardFormatId nFormat)58 bool INetImage::Read( SvStream& rIStm, SotClipboardFormatId nFormat )
59 {
60     bool bRet = false;
61     switch( nFormat )
62     {
63     case SotClipboardFormatId::INET_IMAGE:
64         {
65             OUString sINetImg = read_zeroTerminated_uInt8s_ToOUString(rIStm, RTL_TEXTENCODING_UTF8);
66             sal_Int32 nStart = 0;
67             aImageURL = sINetImg.getToken( 0, TOKEN_SEPARATOR, nStart );
68             aTargetURL = sINetImg.getToken( 0, TOKEN_SEPARATOR, nStart );
69             aTargetFrame = sINetImg.getToken( 0, TOKEN_SEPARATOR, nStart );
70             /*aAlternateText =*/ sINetImg.getToken( 0, TOKEN_SEPARATOR, nStart );
71             aSizePixel.setWidth( sINetImg.getToken( 0, TOKEN_SEPARATOR,
72                                                     nStart ).toInt32() );
73             aSizePixel.setHeight( sINetImg.getToken( 0, TOKEN_SEPARATOR,
74                                                     nStart ).toInt32() );
75             bRet = !sINetImg.isEmpty();
76         }
77         break;
78 
79     case SotClipboardFormatId::NETSCAPE_IMAGE:
80         {
81 /*
82     --> structure size  MUST - alignment of 4!
83     int     iSize;              // size of all data, including variable length strings
84     sal_Bool    bIsMap;             // For server side maps
85     sal_Int32   iWidth;             // Fixed size data correspond to fields in LO_ImageDataStruct
86     sal_Int32   iHeight;            //   and EDT_ImageData
87     sal_Int32   iHSpace;
88     sal_Int32   iVSpace;
89     sal_Int32   iBorder;
90     int     iLowResOffset;      // Offsets into string_data. If 0, string is NULL (not used)
91     int     iAltOffset;         // (alternate text?)
92     int     iAnchorOffset;      // HREF in image
93     int     iExtraHTML_Offset;  // Extra HTML (stored in CImageElement)
94     sal_Char pImageURL[1];      // Append all variable-length strings starting here
95 */
96             rtl_TextEncoding eSysCSet = osl_getThreadTextEncoding();
97             sal_Int32 nVal, nAnchorOffset, nAltOffset;
98             sal_uInt64 nFilePos;
99 
100             nFilePos = rIStm.Tell();
101             // skip over iSize (int), bIsMao ( sal_Bool ) alignment of 4 !!!!
102             rIStm.SeekRel( 8 );
103             rIStm.ReadInt32( nVal );  aSizePixel.setWidth( nVal );
104             rIStm.ReadInt32( nVal );  aSizePixel.setHeight( nVal );
105             // skip over iHSpace, iVSpace, iBorder, iLowResOffset
106             rIStm.SeekRel( 3 * sizeof( sal_Int32 ) + sizeof( int ) );
107             rIStm.ReadInt32( nAltOffset );
108             rIStm.ReadInt32( nAnchorOffset );
109             // skip over iExtraHTML_Offset
110             rIStm.SeekRel( sizeof( int ) );
111 
112             aImageURL = read_zeroTerminated_uInt8s_ToOUString(rIStm, eSysCSet);
113             if( nAltOffset )
114             {
115                 rIStm.Seek( nFilePos + nAltOffset );
116                 /*aAlternateText =*/ read_zeroTerminated_uInt8s_ToOUString(rIStm, eSysCSet);
117             }
118 
119             if( nAnchorOffset )
120             {
121                 rIStm.Seek( nFilePos + nAnchorOffset );
122                 aTargetURL = read_zeroTerminated_uInt8s_ToOUString(rIStm, eSysCSet);
123             }
124             else if( !aTargetURL.isEmpty() )
125                 aTargetURL.clear();
126 
127             bRet = ERRCODE_NONE == rIStm.GetError();
128         }
129         break;
130     default: break;
131     }
132     return bRet;
133 }
134 
135 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
136