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