xref: /aoo42x/main/xmloff/source/style/HatchStyle.cxx (revision 63bba73c)
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/HatchStyle.hxx"
27 #include <com/sun/star/drawing/Hatch.hpp>
28 #include <xmloff/nmspmap.hxx>
29 #include <xmloff/xmluconv.hxx>
30 #include "xmloff/xmlnmspe.hxx"
31 #include <xmloff/xmltoken.hxx>
32 #include <xmloff/xmlexp.hxx>
33 #include <xmloff/xmlimp.hxx>
34 #include <rtl/ustrbuf.hxx>
35 #include <rtl/ustring.hxx>
36 #include <tools/debug.hxx>
37 #include <xmloff/xmltkmap.hxx>
38 
39 using namespace ::com::sun::star;
40 using ::rtl::OUString;
41 using ::rtl::OUStringBuffer;
42 
43 using namespace ::xmloff::token;
44 
45 enum SvXMLTokenMapAttrs
46 {
47 	XML_TOK_HATCH_NAME,
48 	XML_TOK_HATCH_DISPLAY_NAME,
49 	XML_TOK_HATCH_STYLE,
50 	XML_TOK_HATCH_COLOR,
51 	XML_TOK_HATCH_DISTANCE,
52 	XML_TOK_HATCH_ROTATION,
53 	XML_TOK_TABSTOP_END=XML_TOK_UNKNOWN
54 };
55 
56 
57 SvXMLEnumMapEntry __READONLY_DATA pXML_HatchStyle_Enum[] =
58 {
59 	{ XML_HATCHSTYLE_SINGLE,	drawing::HatchStyle_SINGLE },
60 	{ XML_HATCHSTYLE_DOUBLE,	drawing::HatchStyle_DOUBLE },
61 	{ XML_HATCHSTYLE_TRIPLE,	drawing::HatchStyle_TRIPLE },
62 	{ XML_TOKEN_INVALID, 0 }
63 };
64 
65 
66 //-------------------------------------------------------------
67 // Import
68 //-------------------------------------------------------------
69 
XMLHatchStyleImport(SvXMLImport & rImp)70 XMLHatchStyleImport::XMLHatchStyleImport( SvXMLImport& rImp )
71     : rImport(rImp)
72 {
73 }
74 
~XMLHatchStyleImport()75 XMLHatchStyleImport::~XMLHatchStyleImport()
76 {
77 }
78 
importXML(const uno::Reference<xml::sax::XAttributeList> & xAttrList,uno::Any & rValue,OUString & rStrName)79 sal_Bool XMLHatchStyleImport::importXML(
80     const uno::Reference< xml::sax::XAttributeList >& xAttrList,
81     uno::Any& rValue,
82     OUString& rStrName )
83 {
84 	sal_Bool bRet = sal_False;
85 
86 	sal_Bool bHasName  = sal_False;
87 	sal_Bool bHasStyle = sal_False;
88 	sal_Bool bHasColor = sal_False;
89 	sal_Bool bHasDist  = sal_False;
90 	OUString aDisplayName;
91 
92 	drawing::Hatch aHatch;
93 	aHatch.Style = drawing::HatchStyle_SINGLE;
94 	aHatch.Color = 0;
95 	aHatch.Distance = 0;
96 	aHatch.Angle = 0;
97 
98     {
99         static __FAR_DATA SvXMLTokenMapEntry aHatchAttrTokenMap[] =
100 {
101 	{ XML_NAMESPACE_DRAW, XML_NAME, XML_TOK_HATCH_NAME },
102 	{ XML_NAMESPACE_DRAW, XML_DISPLAY_NAME, XML_TOK_HATCH_DISPLAY_NAME },
103 	{ XML_NAMESPACE_DRAW, XML_STYLE, XML_TOK_HATCH_STYLE },
104 	{ XML_NAMESPACE_DRAW, XML_COLOR, XML_TOK_HATCH_COLOR },
105 	{ XML_NAMESPACE_DRAW, XML_HATCH_DISTANCE, XML_TOK_HATCH_DISTANCE },
106 	{ XML_NAMESPACE_DRAW, XML_ROTATION, XML_TOK_HATCH_ROTATION },
107 	XML_TOKEN_MAP_END
108 };
109 
110 	SvXMLTokenMap aTokenMap( aHatchAttrTokenMap );
111     SvXMLNamespaceMap rNamespaceMap = rImport.GetNamespaceMap();
112     SvXMLUnitConverter& rUnitConverter = rImport.GetMM100UnitConverter();
113 
114 	sal_Int16 nAttrCount = xAttrList.is() ? xAttrList->getLength() : 0;
115 	for( sal_Int16 i=0; i < nAttrCount; i++ )
116 	{
117 		const OUString& rFullAttrName = xAttrList->getNameByIndex( i );
118 		OUString aStrAttrName;
119 		sal_uInt16 nPrefix = rNamespaceMap.GetKeyByAttrName( rFullAttrName, &aStrAttrName );
120 		const OUString& rStrValue = xAttrList->getValueByIndex( i );
121 
122 		switch( aTokenMap.Get( nPrefix, aStrAttrName ) )
123 		{
124 			case XML_TOK_HATCH_NAME:
125 				{
126 					rStrName = rStrValue;
127 					bHasName = sal_True;
128 				}
129 				break;
130 			case XML_TOK_HATCH_DISPLAY_NAME:
131 				aDisplayName = rStrValue;
132 				break;
133 			case XML_TOK_HATCH_STYLE:
134 				{
135 					sal_uInt16 eValue;
136                     bHasStyle = rUnitConverter.convertEnum( eValue, rStrValue, pXML_HatchStyle_Enum );
137 					if( bHasStyle )
138 						aHatch.Style = (drawing::HatchStyle) eValue;
139 				}
140 				break;
141 			case XML_TOK_HATCH_COLOR:
142 				{
143 					Color aColor;
144                     bHasColor = rUnitConverter.convertColor( aColor, rStrValue );
145 					if( bHasColor )
146 						aHatch.Color = (sal_Int32)( aColor.GetColor() );
147 				}
148 				break;
149 			case XML_TOK_HATCH_DISTANCE:
150 				bHasDist = rUnitConverter.convertMeasure( (sal_Int32&)aHatch.Distance, rStrValue );
151 				break;
152 			case XML_TOK_HATCH_ROTATION:
153 				{
154 					sal_Int32 nValue;
155 					rUnitConverter.convertNumber( nValue, rStrValue, 0, 3600 );
156 					aHatch.Angle = sal_Int16( nValue );
157 				}
158 				break;
159 
160 			default:
161 				DBG_WARNING( "Unknown token at import hatch style" )
162 				;
163 		}
164 	}
165 
166 	rValue <<= aHatch;
167 
168 	if( aDisplayName.getLength() )
169 	{
170 		rImport.AddStyleDisplayName( XML_STYLE_FAMILY_SD_HATCH_ID, rStrName,
171 									 aDisplayName );
172 		rStrName = aDisplayName;
173 	}
174 
175 	bRet = bHasName && bHasStyle && bHasColor && bHasDist;
176 
177     }
178 
179 	return bRet;
180 }
181 
182 
183 //-------------------------------------------------------------
184 // Export
185 //-------------------------------------------------------------
186 
187 #ifndef SVX_LIGHT
188 
XMLHatchStyleExport(SvXMLExport & rExp)189 XMLHatchStyleExport::XMLHatchStyleExport( SvXMLExport& rExp )
190     : rExport(rExp)
191 {
192 }
193 
~XMLHatchStyleExport()194 XMLHatchStyleExport::~XMLHatchStyleExport()
195 {
196 }
197 
exportXML(const OUString & rStrName,const uno::Any & rValue)198 sal_Bool XMLHatchStyleExport::exportXML(
199     const OUString& rStrName,
200     const uno::Any& rValue )
201 {
202 	sal_Bool bRet = sal_False;
203 	drawing::Hatch aHatch;
204 
205 	if( rStrName.getLength() )
206 	{
207 		if( rValue >>= aHatch )
208 		{
209 			OUString aStrValue;
210 			OUStringBuffer aOut;
211 
212             SvXMLUnitConverter& rUnitConverter =
213                 rExport.GetMM100UnitConverter();
214 
215 			// Style
216 			if( !rUnitConverter.convertEnum( aOut, aHatch.Style, pXML_HatchStyle_Enum ) )
217 			{
218 				bRet = sal_False;
219 			}
220 			else
221 			{
222 				// Name
223 				sal_Bool bEncoded = sal_False;
224 				rExport.AddAttribute( XML_NAMESPACE_DRAW, XML_NAME,
225 									  rExport.EncodeStyleName( rStrName,
226 										 					   &bEncoded ) );
227 				if( bEncoded )
228 					rExport.AddAttribute( XML_NAMESPACE_DRAW, XML_DISPLAY_NAME,
229 									  	  rStrName );
230 
231 				aStrValue = aOut.makeStringAndClear();
232 				rExport.AddAttribute( XML_NAMESPACE_DRAW, XML_STYLE, aStrValue );
233 
234 				// Color
235 				rUnitConverter.convertColor( aOut, Color( aHatch.Color ) );
236 				aStrValue = aOut.makeStringAndClear();
237 				rExport.AddAttribute( XML_NAMESPACE_DRAW, XML_COLOR, aStrValue );
238 
239 				// Distance
240 				rUnitConverter.convertMeasure( aOut, aHatch.Distance );
241 				aStrValue = aOut.makeStringAndClear();
242 				rExport.AddAttribute( XML_NAMESPACE_DRAW, XML_HATCH_DISTANCE, aStrValue );
243 
244 				// Angle
245 				rUnitConverter.convertNumber( aOut, sal_Int32( aHatch.Angle ) );
246 				aStrValue = aOut.makeStringAndClear();
247 				rExport.AddAttribute( XML_NAMESPACE_DRAW, XML_ROTATION, aStrValue );
248 
249 				// Do Write
250 				SvXMLElementExport rElem( rExport, XML_NAMESPACE_DRAW, XML_HATCH,
251 										  sal_True, sal_False );
252 			}
253 		}
254 	}
255 
256 	return bRet;
257 }
258 
259 #endif // #ifndef SVX_LIGHT
260