xref: /trunk/main/xmloff/source/style/ImageStyle.cxx (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
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_xmloff.hxx"
30 #include "xmloff/ImageStyle.hxx"
31 #include <com/sun/star/awt/XBitmap.hpp>
32 #include <xmloff/attrlist.hxx>
33 #include <xmloff/nmspmap.hxx>
34 #include <xmloff/xmluconv.hxx>
35 #include"xmloff/xmlnmspe.hxx"
36 #include <xmloff/xmltoken.hxx>
37 #include <xmloff/xmlexp.hxx>
38 #include <xmloff/xmlimp.hxx>
39 #include <rtl/ustrbuf.hxx>
40 #include <rtl/ustring.hxx>
41 #include <tools/debug.hxx>
42 #include <xmloff/xmltkmap.hxx>
43 
44 using namespace ::com::sun::star;
45 using ::rtl::OUString;
46 using ::rtl::OUStringBuffer;
47 
48 using namespace ::xmloff::token;
49 
50 enum SvXMLTokenMapAttrs
51 {
52     XML_TOK_IMAGE_NAME,
53     XML_TOK_IMAGE_DISPLAY_NAME,
54     XML_TOK_IMAGE_URL,
55     XML_TOK_IMAGE_TYPE,
56     XML_TOK_IMAGE_SHOW,
57     XML_TOK_IMAGE_ACTUATE,
58     /* XML_TOK_IMAGE_SIZEW,
59        XML_TOK_IMAGE_SIZEH,*/
60     XML_TOK_TABSTOP_END=XML_TOK_UNKNOWN
61 };
62 
63 
64 XMLImageStyle::XMLImageStyle()
65 {
66 }
67 
68 XMLImageStyle::~XMLImageStyle()
69 {
70 }
71 
72 #ifndef SVX_LIGHT
73 
74 sal_Bool XMLImageStyle::exportXML( const OUString& rStrName, const ::com::sun::star::uno::Any& rValue, SvXMLExport& rExport )
75 {
76     return ImpExportXML( rStrName, rValue, rExport );
77 }
78 
79 sal_Bool XMLImageStyle::ImpExportXML( const OUString& rStrName, const uno::Any& rValue, SvXMLExport& rExport )
80 {
81     sal_Bool bRet = sal_False;
82 
83     OUString sImageURL;
84 
85     if( rStrName.getLength() )
86     {
87         if( rValue >>= sImageURL )
88         {
89             OUString aStrValue;
90             OUStringBuffer aOut;
91 
92             // Name
93             sal_Bool bEncoded = sal_False;
94             rExport.AddAttribute( XML_NAMESPACE_DRAW, XML_NAME,
95                                   rExport.EncodeStyleName( rStrName,
96                                                            &bEncoded ) );
97             if( bEncoded )
98                 rExport.AddAttribute( XML_NAMESPACE_DRAW, XML_DISPLAY_NAME,
99                                       rStrName );
100 
101             // uri
102             const OUString aStr( rExport.AddEmbeddedGraphicObject( sImageURL ) );
103             if( aStr.getLength() )
104             {
105                 rExport.AddAttribute( XML_NAMESPACE_XLINK, XML_HREF, aStr );
106                 rExport.AddAttribute( XML_NAMESPACE_XLINK, XML_TYPE, XML_SIMPLE );
107                 rExport.AddAttribute( XML_NAMESPACE_XLINK, XML_SHOW, XML_EMBED );
108                 rExport.AddAttribute( XML_NAMESPACE_XLINK, XML_ACTUATE, XML_ONLOAD );
109             }
110 /*
111             // size
112             awt::Size aSize = xBitmap->getSize();
113 
114             rUnitConverter.convertNumber( aOut, aSize.Width );
115             aStrValue = aOut.makeStringAndClear();
116             AddAttribute( XML_NAMESPACE_SVG, XML_WIDTH, aStrValue );
117 
118             rUnitConverter.convertNumber( aOut, aSize.Height );
119             aStrValue = aOut.makeStringAndClear();
120             AddAttribute( XML_NAMESPACE_SVG, XML_HEIGHT, aStrValue );
121 */
122             // Do Write
123             SvXMLElementExport aElem( rExport, XML_NAMESPACE_DRAW, XML_FILL_IMAGE, sal_True, sal_True );
124 
125             if( sImageURL.getLength() )
126             {
127                 // optional office:binary-data
128                 rExport.AddEmbeddedGraphicObjectAsBase64( sImageURL );
129             }
130         }
131     }
132 
133     return bRet;
134 }
135 
136 #endif // #ifndef SVX_LIGHT
137 
138 sal_Bool XMLImageStyle::importXML( const uno::Reference< xml::sax::XAttributeList >& xAttrList, uno::Any& rValue, OUString& rStrName, SvXMLImport& rImport )
139 {
140     return ImpImportXML( xAttrList, rValue, rStrName, rImport );
141 }
142 
143 sal_Bool XMLImageStyle::ImpImportXML( const uno::Reference< xml::sax::XAttributeList >& xAttrList,
144                                       uno::Any& rValue, OUString& rStrName,
145                                       SvXMLImport& rImport )
146 {
147     sal_Bool bRet     = sal_False;
148     sal_Bool bHasHRef = sal_False;
149     sal_Bool bHasName = sal_False;
150     OUString aStrURL;
151     OUString aDisplayName;
152 
153     {
154         static __FAR_DATA SvXMLTokenMapEntry aHatchAttrTokenMap[] =
155 {
156     { XML_NAMESPACE_DRAW, XML_NAME, XML_TOK_IMAGE_NAME },
157     { XML_NAMESPACE_DRAW, XML_DISPLAY_NAME, XML_TOK_IMAGE_DISPLAY_NAME },
158     { XML_NAMESPACE_XLINK, XML_HREF, XML_TOK_IMAGE_URL },
159     { XML_NAMESPACE_XLINK, XML_TYPE, XML_TOK_IMAGE_TYPE },
160     { XML_NAMESPACE_XLINK, XML_SHOW, XML_TOK_IMAGE_SHOW },
161     { XML_NAMESPACE_XLINK, XML_ACTUATE, XML_TOK_IMAGE_ACTUATE },
162     /*{ XML_NAMESPACE_XLINK, XML_HREF, XML_TOK_IMAGE_URL },
163     { XML_NAMESPACE_XLINK, XML_HREF, XML_TOK_IMAGE_URL },*/
164     XML_TOKEN_MAP_END
165 };
166 
167     SvXMLTokenMap aTokenMap( aHatchAttrTokenMap );
168 
169     sal_Int16 nAttrCount = xAttrList.is() ? xAttrList->getLength() : 0;
170     for( sal_Int16 i=0; i < nAttrCount; i++ )
171     {
172         const OUString& rFullAttrName = xAttrList->getNameByIndex( i );
173         OUString aStrAttrName;
174         sal_uInt16 nPrefix = rImport.GetNamespaceMap().GetKeyByAttrName( rFullAttrName, &aStrAttrName );
175         const OUString& rStrValue = xAttrList->getValueByIndex( i );
176 
177         switch( aTokenMap.Get( nPrefix, aStrAttrName ) )
178         {
179             case XML_TOK_IMAGE_NAME:
180                 {
181                     rStrName = rStrValue;
182                     bHasName = sal_True;
183                 }
184                 break;
185             case XML_TOK_IMAGE_DISPLAY_NAME:
186                 {
187                     aDisplayName = rStrValue;
188                 }
189                 break;
190             case XML_TOK_IMAGE_URL:
191                 {
192                     aStrURL = rImport.ResolveGraphicObjectURL( rStrValue, sal_False );
193                     bHasHRef = sal_True;
194                 }
195                 break;
196             case XML_TOK_IMAGE_TYPE:
197                 // ignore
198                 break;
199             case XML_TOK_IMAGE_SHOW:
200                 // ignore
201                 break;
202             case XML_TOK_IMAGE_ACTUATE:
203                 // ignore
204                 break;
205             default:
206                 DBG_WARNING( "Unknown token at import fill bitmap style" )
207                 ;
208         }
209     }
210 
211     rValue <<= aStrURL;
212 
213     if( aDisplayName.getLength() )
214     {
215         rImport.AddStyleDisplayName( XML_STYLE_FAMILY_SD_FILL_IMAGE_ID,
216                                      rStrName, aDisplayName );
217         rStrName = aDisplayName;
218     }
219 
220     bRet = bHasName && bHasHRef;
221 
222     }
223 
224     return bRet;
225 }
226