xref: /trunk/main/svtools/source/urlobj/inetimg.cxx (revision 1ecadb572e7010ff3b3382ad9bf179dbc6efadbb)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_svtools.hxx"
30 #include <tools/debug.hxx>
31 #include <sot/formats.hxx>
32 #include <tools/stream.hxx>
33 
34 #include <svtools/inetimg.hxx>
35 
36 #define TOKEN_SEPARATOR '\001'
37 
38 sal_Bool INetImage::Write( SvStream& rOStm, sal_uLong nFormat ) const
39 {
40     sal_Bool bRet = sal_False;
41     switch( nFormat )
42     {
43     case SOT_FORMATSTR_ID_INET_IMAGE:
44         {
45             String sString;
46             (sString += aImageURL ) += TOKEN_SEPARATOR;
47             (sString += aTargetURL ) += TOKEN_SEPARATOR;
48             (sString += aTargetFrame ) += TOKEN_SEPARATOR;
49             (sString += aAlternateText ) += TOKEN_SEPARATOR;
50             sString += String::CreateFromInt32( aSizePixel.Width() );
51             sString += TOKEN_SEPARATOR;
52             sString += String::CreateFromInt32( aSizePixel.Height() );
53             ByteString sOut( sString, RTL_TEXTENCODING_UTF8 );
54 
55             rOStm.Write( sOut.GetBuffer(), sOut.Len() );
56             static const sal_Char aEndChar[2] = { 0 };
57             rOStm.Write( aEndChar, sizeof( aEndChar ));
58             bRet = 0 == rOStm.GetError();
59         }
60         break;
61 
62     case SOT_FORMATSTR_ID_NETSCAPE_IMAGE:
63         break;
64     }
65     return bRet;
66 }
67 
68 sal_Bool INetImage::Read( SvStream& rIStm, sal_uLong nFormat )
69 {
70     sal_Bool bRet = sal_False;
71     switch( nFormat )
72     {
73     case SOT_FORMATSTR_ID_INET_IMAGE:
74         {
75             String sINetImg;
76             rIStm.ReadCString( sINetImg, RTL_TEXTENCODING_UTF8 );
77             xub_StrLen nStart = 0;
78             aImageURL = sINetImg.GetToken( 0, TOKEN_SEPARATOR, nStart );
79             aTargetURL = sINetImg.GetToken( 0, TOKEN_SEPARATOR, nStart );
80             aTargetFrame = sINetImg.GetToken( 0, TOKEN_SEPARATOR, nStart );
81             aAlternateText = sINetImg.GetToken( 0, TOKEN_SEPARATOR, nStart );
82             aSizePixel.Width() = sINetImg.GetToken( 0, TOKEN_SEPARATOR,
83                                                     nStart ).ToInt32();
84             aSizePixel.Height() = sINetImg.GetToken( 0, TOKEN_SEPARATOR,
85                                                     nStart ).ToInt32();
86             bRet = 0 != sINetImg.Len();
87         }
88         break;
89 
90     case SOT_FORMATSTR_ID_NETSCAPE_IMAGE:
91         {
92 /*
93     --> structure size  MUST - alignment of 4!
94     int     iSize;              // size of all data, including variable length strings
95     sal_Bool    bIsMap;             // For server side maps
96     sal_Int32   iWidth;             // Fixed size data correspond to fields in LO_ImageDataStruct
97     sal_Int32   iHeight;            //   and EDT_ImageData
98     sal_Int32   iHSpace;
99     sal_Int32   iVSpace;
100     sal_Int32   iBorder;
101     int     iLowResOffset;      // Offsets into string_data. If 0, string is NULL (not used)
102     int     iAltOffset;         // (alternate text?)
103     int     iAnchorOffset;      // HREF in image
104     int     iExtraHTML_Offset;  // Extra HTML (stored in CImageElement)
105     sal_Char pImageURL[1];      // Append all variable-length strings starting here
106 */
107             rtl_TextEncoding eSysCSet = gsl_getSystemTextEncoding();
108             sal_Int32 nVal, nAnchorOffset, nAltOffset, nFilePos;
109             ByteString sData;
110 
111             nFilePos = rIStm.Tell();
112             // skip over iSize (int), bIsMao ( sal_Bool ) alignment of 4 !!!!
113             rIStm.SeekRel( 8 );
114             rIStm >> nVal;  aSizePixel.Width() = nVal;
115             rIStm >> nVal;  aSizePixel.Height() = nVal;
116             // skip over iHSpace, iVSpace, iBorder, iLowResOffset
117             rIStm.SeekRel( 3 * sizeof( sal_Int32 ) + sizeof( int ) );
118             rIStm >> nAltOffset;
119             rIStm >> nAnchorOffset;
120             // skip over iExtraHTML_Offset
121             rIStm.SeekRel( sizeof( int ) );
122 
123             rIStm.ReadCString( aImageURL, eSysCSet );
124             if( nAltOffset )
125             {
126                 rIStm.Seek( nFilePos + nAltOffset );
127                 rIStm.ReadCString( aAlternateText, eSysCSet );
128             }
129             else if( aAlternateText.Len() )
130                 aAlternateText.Erase();
131 
132             if( nAnchorOffset )
133             {
134                 rIStm.Seek( nFilePos + nAnchorOffset );
135                 rIStm.ReadCString( aTargetURL, eSysCSet );
136             }
137             else if( aTargetURL.Len() )
138                 aTargetURL.Erase();
139 
140             bRet = 0 == rIStm.GetError();
141         }
142         break;
143     }
144     return bRet;
145 }
146 
147