1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_xmloff.hxx"
26 #include <com/sun/star/style/GraphicLocation.hpp>
27 
28 #include <xmloff/xmlnmspe.hxx>
29 #include <xmloff/xmltoken.hxx>
30 #include <rtl/ustrbuf.hxx>
31 #include <xmloff/xmlexp.hxx>
32 #include "XMLBackgroundImageExport.hxx"
33 #include <xmloff/xmluconv.hxx>
34 
35 using ::rtl::OUString;
36 using ::rtl::OUStringBuffer;
37 
38 using namespace ::com::sun::star;
39 using namespace ::com::sun::star::uno;
40 using namespace ::com::sun::star::style;
41 using namespace ::xmloff::token;
42 
XMLBackgroundImageExport(SvXMLExport & rExp)43 XMLBackgroundImageExport::XMLBackgroundImageExport( SvXMLExport& rExp ) :
44 	rExport( rExp )
45 {
46 }
47 
~XMLBackgroundImageExport()48 XMLBackgroundImageExport::~XMLBackgroundImageExport()
49 {
50 }
51 
exportXML(const Any & rURL,const Any * pPos,const Any * pFilter,const Any * pTransparency,sal_uInt16 nPrefix,const::rtl::OUString & rLocalName)52 void XMLBackgroundImageExport::exportXML( const Any& rURL,
53 			const Any *pPos,
54 			const Any *pFilter,
55             const Any *pTransparency,
56 			sal_uInt16 nPrefix,
57 			const ::rtl::OUString& rLocalName )
58 {
59 	GraphicLocation ePos;
60 	if( !(pPos && ((*pPos) >>= ePos)) )
61 		ePos = GraphicLocation_AREA;
62 
63 	OUString sURL;
64 	rURL >>= sURL;
65 	if( sURL.getLength() && GraphicLocation_NONE != ePos )
66 	{
67 		OUString sTempURL( GetExport().AddEmbeddedGraphicObject( sURL ) );
68 		if( sTempURL.getLength() )
69 		{
70 			GetExport().AddAttribute( XML_NAMESPACE_XLINK, XML_HREF, sTempURL );
71 			GetExport().AddAttribute( XML_NAMESPACE_XLINK, XML_TYPE,
72                                       XML_SIMPLE );
73 			GetExport().AddAttribute( XML_NAMESPACE_XLINK, XML_ACTUATE,
74                                       XML_ONLOAD );
75 		}
76 
77 		OUStringBuffer aOut;
78 		switch( ePos )
79 		{
80 		case GraphicLocation_LEFT_TOP:
81 		case GraphicLocation_MIDDLE_TOP:
82 		case GraphicLocation_RIGHT_TOP:
83 			aOut.append( GetXMLToken(XML_TOP) );
84 			break;
85 		case GraphicLocation_LEFT_MIDDLE:
86 		case GraphicLocation_MIDDLE_MIDDLE:
87 		case GraphicLocation_RIGHT_MIDDLE:
88 			aOut.append( GetXMLToken(XML_CENTER) );
89 			break;
90 		case GraphicLocation_LEFT_BOTTOM:
91 		case GraphicLocation_MIDDLE_BOTTOM:
92 		case GraphicLocation_RIGHT_BOTTOM:
93 			aOut.append( GetXMLToken(XML_BOTTOM) );
94 			break;
95 		default:
96 			break;
97 		}
98 
99 		if( aOut.getLength() )
100 		{
101 			aOut.append( sal_Unicode( ' ' ) );
102 
103 			switch( ePos )
104 			{
105 			case GraphicLocation_LEFT_TOP:
106 			case GraphicLocation_LEFT_BOTTOM:
107 			case GraphicLocation_LEFT_MIDDLE:
108 				aOut.append( GetXMLToken(XML_LEFT) );
109 				break;
110 			case GraphicLocation_MIDDLE_TOP:
111 			case GraphicLocation_MIDDLE_MIDDLE:
112 			case GraphicLocation_MIDDLE_BOTTOM:
113 				aOut.append( GetXMLToken(XML_CENTER) );
114 				break;
115 			case GraphicLocation_RIGHT_MIDDLE:
116 			case GraphicLocation_RIGHT_TOP:
117 			case GraphicLocation_RIGHT_BOTTOM:
118 				aOut.append( GetXMLToken(XML_RIGHT) );
119 				break;
120 			default:
121 				break;
122 			}
123 		}
124 		if( aOut.getLength() )
125 			GetExport().AddAttribute( XML_NAMESPACE_STYLE,
126 								  XML_POSITION, aOut.makeStringAndClear() );
127 
128 		if( GraphicLocation_AREA == ePos )
129 		{
130 			aOut.append( GetXMLToken(XML_BACKGROUND_STRETCH) );
131 		}
132 		else if( GraphicLocation_NONE != ePos && GraphicLocation_TILED != ePos  )
133 		{
134 			aOut.append( GetXMLToken(XML_BACKGROUND_NO_REPEAT) );
135 		}
136 		if( aOut.getLength() )
137 			GetExport().AddAttribute( XML_NAMESPACE_STYLE, XML_REPEAT,
138 						  aOut.makeStringAndClear() );
139 
140 		if( pFilter )
141 		{
142 			OUString sFilter;
143 			(*pFilter) >>= sFilter;
144 			if( sFilter.getLength() )
145 				GetExport().AddAttribute( XML_NAMESPACE_STYLE, XML_FILTER_NAME,
146 										  sFilter );
147 		}
148 
149         if( pTransparency )
150         {
151             sal_Int8 nTransparency = sal_Int8();
152             if( (*pTransparency) >>= nTransparency )
153             {
154                 OUStringBuffer aTransOut;
155                 SvXMLUnitConverter::convertPercent( aTransOut, 100-nTransparency );
156                 GetExport().AddAttribute( XML_NAMESPACE_DRAW, XML_OPACITY,
157                                           aTransOut.makeStringAndClear() );
158             }
159         }
160 	}
161 
162 	{
163 		SvXMLElementExport aElem( GetExport(), nPrefix, rLocalName, sal_True, sal_True );
164 		if( sURL.getLength() && GraphicLocation_NONE != ePos )
165 		{
166 			// optional office:binary-data
167 			GetExport().AddEmbeddedGraphicObjectAsBase64( sURL );
168 		}
169 	}
170 }
171