xref: /trunk/main/xmloff/source/style/XMLBackgroundImageExport.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 <com/sun/star/style/GraphicLocation.hpp>
31 
32 #include <xmloff/xmlnmspe.hxx>
33 #include <xmloff/xmltoken.hxx>
34 #include <rtl/ustrbuf.hxx>
35 #include <xmloff/xmlexp.hxx>
36 #include "XMLBackgroundImageExport.hxx"
37 #include <xmloff/xmluconv.hxx>
38 
39 using ::rtl::OUString;
40 using ::rtl::OUStringBuffer;
41 
42 using namespace ::com::sun::star;
43 using namespace ::com::sun::star::uno;
44 using namespace ::com::sun::star::style;
45 using namespace ::xmloff::token;
46 
47 XMLBackgroundImageExport::XMLBackgroundImageExport( SvXMLExport& rExp ) :
48     rExport( rExp )
49 {
50 }
51 
52 XMLBackgroundImageExport::~XMLBackgroundImageExport()
53 {
54 }
55 
56 void XMLBackgroundImageExport::exportXML( const Any& rURL,
57             const Any *pPos,
58             const Any *pFilter,
59             const Any *pTransparency,
60             sal_uInt16 nPrefix,
61             const ::rtl::OUString& rLocalName )
62 {
63     GraphicLocation ePos;
64     if( !(pPos && ((*pPos) >>= ePos)) )
65         ePos = GraphicLocation_AREA;
66 
67     OUString sURL;
68     rURL >>= sURL;
69     if( sURL.getLength() && GraphicLocation_NONE != ePos )
70     {
71         OUString sTempURL( GetExport().AddEmbeddedGraphicObject( sURL ) );
72         if( sTempURL.getLength() )
73         {
74             GetExport().AddAttribute( XML_NAMESPACE_XLINK, XML_HREF, sTempURL );
75             GetExport().AddAttribute( XML_NAMESPACE_XLINK, XML_TYPE,
76                                       XML_SIMPLE );
77             GetExport().AddAttribute( XML_NAMESPACE_XLINK, XML_ACTUATE,
78                                       XML_ONLOAD );
79         }
80 
81         OUStringBuffer aOut;
82         switch( ePos )
83         {
84         case GraphicLocation_LEFT_TOP:
85         case GraphicLocation_MIDDLE_TOP:
86         case GraphicLocation_RIGHT_TOP:
87             aOut.append( GetXMLToken(XML_TOP) );
88             break;
89         case GraphicLocation_LEFT_MIDDLE:
90         case GraphicLocation_MIDDLE_MIDDLE:
91         case GraphicLocation_RIGHT_MIDDLE:
92             aOut.append( GetXMLToken(XML_CENTER) );
93             break;
94         case GraphicLocation_LEFT_BOTTOM:
95         case GraphicLocation_MIDDLE_BOTTOM:
96         case GraphicLocation_RIGHT_BOTTOM:
97             aOut.append( GetXMLToken(XML_BOTTOM) );
98             break;
99         default:
100             break;
101         }
102 
103         if( aOut.getLength() )
104         {
105             aOut.append( sal_Unicode( ' ' ) );
106 
107             switch( ePos )
108             {
109             case GraphicLocation_LEFT_TOP:
110             case GraphicLocation_LEFT_BOTTOM:
111             case GraphicLocation_LEFT_MIDDLE:
112                 aOut.append( GetXMLToken(XML_LEFT) );
113                 break;
114             case GraphicLocation_MIDDLE_TOP:
115             case GraphicLocation_MIDDLE_MIDDLE:
116             case GraphicLocation_MIDDLE_BOTTOM:
117                 aOut.append( GetXMLToken(XML_CENTER) );
118                 break;
119             case GraphicLocation_RIGHT_MIDDLE:
120             case GraphicLocation_RIGHT_TOP:
121             case GraphicLocation_RIGHT_BOTTOM:
122                 aOut.append( GetXMLToken(XML_RIGHT) );
123                 break;
124             default:
125                 break;
126             }
127         }
128         if( aOut.getLength() )
129             GetExport().AddAttribute( XML_NAMESPACE_STYLE,
130                                   XML_POSITION, aOut.makeStringAndClear() );
131 
132         if( GraphicLocation_AREA == ePos )
133         {
134             aOut.append( GetXMLToken(XML_BACKGROUND_STRETCH) );
135         }
136         else if( GraphicLocation_NONE != ePos && GraphicLocation_TILED != ePos  )
137         {
138             aOut.append( GetXMLToken(XML_BACKGROUND_NO_REPEAT) );
139         }
140         if( aOut.getLength() )
141             GetExport().AddAttribute( XML_NAMESPACE_STYLE, XML_REPEAT,
142                           aOut.makeStringAndClear() );
143 
144         if( pFilter )
145         {
146             OUString sFilter;
147             (*pFilter) >>= sFilter;
148             if( sFilter.getLength() )
149                 GetExport().AddAttribute( XML_NAMESPACE_STYLE, XML_FILTER_NAME,
150                                           sFilter );
151         }
152 
153         if( pTransparency )
154         {
155             sal_Int8 nTransparency = sal_Int8();
156             if( (*pTransparency) >>= nTransparency )
157             {
158                 OUStringBuffer aTransOut;
159                 SvXMLUnitConverter::convertPercent( aTransOut, 100-nTransparency );
160                 GetExport().AddAttribute( XML_NAMESPACE_DRAW, XML_OPACITY,
161                                           aTransOut.makeStringAndClear() );
162             }
163         }
164     }
165 
166     {
167         SvXMLElementExport aElem( GetExport(), nPrefix, rLocalName, sal_True, sal_True );
168         if( sURL.getLength() && GraphicLocation_NONE != ePos )
169         {
170             // optional office:binary-data
171             GetExport().AddEmbeddedGraphicObjectAsBase64( sURL );
172         }
173     }
174 }
175