Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
5 : * This Source Code Form is subject to the terms of the Mozilla Public
6 : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
11 : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : * contributor license agreements. See the NOTICE file distributed
13 : * with this work for additional information regarding copyright
14 : * ownership. The ASF licenses this file to you under the Apache
15 : * License, Version 2.0 (the "License"); you may not use this file
16 : * except in compliance with the License. You may obtain a copy of
17 : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : */
19 :
20 : #include <com/sun/star/drawing/DashStyle.hpp>
21 : #include <com/sun/star/drawing/LineDash.hpp>
22 :
23 : #include <sax/tools/converter.hxx>
24 :
25 : #include <xmloff/DashStyle.hxx>
26 : #include <xmloff/attrlist.hxx>
27 : #include <xmloff/nmspmap.hxx>
28 : #include <xmloff/xmluconv.hxx>
29 : #include <xmloff/xmlnmspe.hxx>
30 : #include <xmloff/xmltoken.hxx>
31 : #include <xmloff/xmlexp.hxx>
32 : #include <xmloff/xmlimp.hxx>
33 : #include <rtl/ustrbuf.hxx>
34 : #include <rtl/ustring.hxx>
35 : #include <tools/debug.hxx>
36 : #include <xmloff/xmltkmap.hxx>
37 :
38 : using namespace ::com::sun::star;
39 :
40 : using namespace ::xmloff::token;
41 :
42 : enum SvXMLTokenMapAttrs
43 : {
44 : XML_TOK_DASH_NAME,
45 : XML_TOK_DASH_DISPLAY_NAME,
46 : XML_TOK_DASH_STYLE,
47 : XML_TOK_DASH_DOTS1,
48 : XML_TOK_DASH_DOTS1LEN,
49 : XML_TOK_DASH_DOTS2,
50 : XML_TOK_DASH_DOTS2LEN,
51 : XML_TOK_DASH_DISTANCE,
52 : XML_TOK_DASH_END=XML_TOK_UNKNOWN
53 : };
54 :
55 : static SvXMLTokenMapEntry aDashStyleAttrTokenMap[] =
56 : {
57 : { XML_NAMESPACE_DRAW, XML_NAME, XML_TOK_DASH_NAME },
58 : { XML_NAMESPACE_DRAW, XML_DISPLAY_NAME, XML_TOK_DASH_DISPLAY_NAME },
59 : { XML_NAMESPACE_DRAW, XML_STYLE, XML_TOK_DASH_STYLE },
60 : { XML_NAMESPACE_DRAW, XML_DOTS1, XML_TOK_DASH_DOTS1 },
61 : { XML_NAMESPACE_DRAW, XML_DOTS1_LENGTH, XML_TOK_DASH_DOTS1LEN },
62 : { XML_NAMESPACE_DRAW, XML_DOTS2, XML_TOK_DASH_DOTS2 },
63 : { XML_NAMESPACE_DRAW, XML_DOTS2_LENGTH, XML_TOK_DASH_DOTS2LEN },
64 : { XML_NAMESPACE_DRAW, XML_DISTANCE, XML_TOK_DASH_DISTANCE },
65 : XML_TOKEN_MAP_END
66 : };
67 :
68 : SvXMLEnumMapEntry const pXML_DashStyle_Enum[] =
69 : {
70 : { XML_RECT, drawing::DashStyle_RECT },
71 : { XML_ROUND, drawing::DashStyle_ROUND },
72 : { XML_RECT, drawing::DashStyle_RECTRELATIVE },
73 : { XML_ROUND, drawing::DashStyle_ROUNDRELATIVE },
74 : { XML_TOKEN_INVALID, 0 }
75 : };
76 :
77 : // Import
78 :
79 0 : XMLDashStyleImport::XMLDashStyleImport( SvXMLImport& rImp )
80 0 : : rImport(rImp)
81 : {
82 0 : }
83 :
84 0 : XMLDashStyleImport::~XMLDashStyleImport()
85 : {
86 0 : }
87 :
88 0 : void XMLDashStyleImport::importXML(
89 : const uno::Reference< xml::sax::XAttributeList >& xAttrList,
90 : uno::Any& rValue,
91 : OUString& rStrName )
92 : {
93 0 : drawing::LineDash aLineDash;
94 0 : aLineDash.Style = drawing::DashStyle_RECT;
95 0 : aLineDash.Dots = 0;
96 0 : aLineDash.DotLen = 0;
97 0 : aLineDash.Dashes = 0;
98 0 : aLineDash.DashLen = 0;
99 0 : aLineDash.Distance = 20;
100 0 : OUString aDisplayName;
101 :
102 0 : bool bIsRel = false;
103 :
104 0 : SvXMLNamespaceMap& rNamespaceMap = rImport.GetNamespaceMap();
105 0 : SvXMLUnitConverter& rUnitConverter = rImport.GetMM100UnitConverter();
106 :
107 0 : SvXMLTokenMap aTokenMap( aDashStyleAttrTokenMap );
108 :
109 0 : sal_Int16 nAttrCount = xAttrList.is() ? xAttrList->getLength() : 0;
110 0 : for( sal_Int16 i=0; i < nAttrCount; i++ )
111 : {
112 0 : const OUString& rFullAttrName = xAttrList->getNameByIndex( i );
113 0 : OUString aStrAttrName;
114 0 : sal_uInt16 nPrefix = rNamespaceMap.GetKeyByAttrName( rFullAttrName, &aStrAttrName );
115 0 : const OUString& rStrValue = xAttrList->getValueByIndex( i );
116 :
117 0 : switch( aTokenMap.Get( nPrefix, aStrAttrName ) )
118 : {
119 : case XML_TOK_DASH_NAME:
120 : {
121 0 : rStrName = rStrValue;
122 : }
123 0 : break;
124 : case XML_TOK_DASH_DISPLAY_NAME:
125 : {
126 0 : aDisplayName = rStrValue;
127 : }
128 0 : break;
129 : case XML_TOK_DASH_STYLE:
130 : {
131 : sal_uInt16 eValue;
132 0 : if( rUnitConverter.convertEnum( eValue, rStrValue, pXML_DashStyle_Enum ) )
133 : {
134 0 : aLineDash.Style = (drawing::DashStyle) eValue;
135 : }
136 : }
137 0 : break;
138 : case XML_TOK_DASH_DOTS1:
139 0 : aLineDash.Dots = (sal_Int16)rStrValue.toInt32();
140 0 : break;
141 :
142 : case XML_TOK_DASH_DOTS1LEN:
143 : {
144 0 : if( rStrValue.indexOf( '%' ) != -1 ) // it's a percentage
145 : {
146 0 : bIsRel = true;
147 0 : ::sax::Converter::convertPercent(aLineDash.DotLen, rStrValue);
148 : }
149 : else
150 : {
151 : rUnitConverter.convertMeasureToCore( aLineDash.DotLen,
152 0 : rStrValue );
153 : }
154 : }
155 0 : break;
156 :
157 : case XML_TOK_DASH_DOTS2:
158 0 : aLineDash.Dashes = (sal_Int16)rStrValue.toInt32();
159 0 : break;
160 :
161 : case XML_TOK_DASH_DOTS2LEN:
162 : {
163 0 : if( rStrValue.indexOf( '%' ) != -1 ) // it's a percentage
164 : {
165 0 : bIsRel = true;
166 0 : ::sax::Converter::convertPercent(aLineDash.DashLen, rStrValue);
167 : }
168 : else
169 : {
170 : rUnitConverter.convertMeasureToCore( aLineDash.DashLen,
171 0 : rStrValue );
172 : }
173 : }
174 0 : break;
175 :
176 : case XML_TOK_DASH_DISTANCE:
177 : {
178 0 : if( rStrValue.indexOf( '%' ) != -1 ) // it's a percentage
179 : {
180 0 : bIsRel = true;
181 0 : ::sax::Converter::convertPercent(aLineDash.Distance, rStrValue);
182 : }
183 : else
184 : {
185 : rUnitConverter.convertMeasureToCore( aLineDash.Distance,
186 0 : rStrValue );
187 : }
188 : }
189 0 : break;
190 : default:
191 : DBG_WARNING( "Unknown token at import gradient style" );
192 : }
193 0 : }
194 :
195 0 : if( bIsRel )
196 0 : aLineDash.Style = aLineDash.Style == drawing::DashStyle_RECT ? drawing::DashStyle_RECTRELATIVE : drawing::DashStyle_ROUNDRELATIVE;
197 :
198 0 : rValue <<= aLineDash;
199 :
200 0 : if( !aDisplayName.isEmpty() )
201 : {
202 : rImport.AddStyleDisplayName( XML_STYLE_FAMILY_SD_STROKE_DASH_ID,
203 0 : rStrName, aDisplayName );
204 0 : rStrName = aDisplayName;
205 0 : }
206 0 : }
207 :
208 : // Export
209 :
210 0 : XMLDashStyleExport::XMLDashStyleExport( SvXMLExport& rExp )
211 0 : : rExport(rExp)
212 : {
213 0 : }
214 :
215 0 : XMLDashStyleExport::~XMLDashStyleExport()
216 : {
217 0 : }
218 :
219 0 : bool XMLDashStyleExport::exportXML(
220 : const OUString& rStrName,
221 : const uno::Any& rValue )
222 : {
223 0 : bool bRet = false;
224 :
225 0 : SvXMLUnitConverter & rUnitConverter = rExport.GetMM100UnitConverter();
226 :
227 0 : drawing::LineDash aLineDash;
228 :
229 0 : if( !rStrName.isEmpty() )
230 : {
231 0 : if( rValue >>= aLineDash )
232 : {
233 0 : bool bIsRel = aLineDash.Style == drawing::DashStyle_RECTRELATIVE || aLineDash.Style == drawing::DashStyle_ROUNDRELATIVE;
234 :
235 0 : OUString aStrValue;
236 0 : OUStringBuffer aOut;
237 :
238 : // Name
239 0 : bool bEncoded = false;
240 : rExport.AddAttribute( XML_NAMESPACE_DRAW, XML_NAME,
241 : rExport.EncodeStyleName( rStrName,
242 0 : &bEncoded ) );
243 0 : if( bEncoded )
244 : rExport.AddAttribute( XML_NAMESPACE_DRAW, XML_DISPLAY_NAME,
245 0 : rStrName );
246 :
247 : // Style
248 0 : rUnitConverter.convertEnum( aOut, aLineDash.Style, pXML_DashStyle_Enum );
249 0 : aStrValue = aOut.makeStringAndClear();
250 0 : rExport.AddAttribute( XML_NAMESPACE_DRAW, XML_STYLE, aStrValue );
251 :
252 : // dots
253 0 : if( aLineDash.Dots )
254 : {
255 0 : rExport.AddAttribute( XML_NAMESPACE_DRAW, XML_DOTS1, OUString::number( aLineDash.Dots ) );
256 :
257 0 : if( aLineDash.DotLen )
258 : {
259 : // dashes length
260 0 : if( bIsRel )
261 : {
262 0 : ::sax::Converter::convertPercent(aOut, aLineDash.DotLen);
263 : }
264 : else
265 : {
266 : rUnitConverter.convertMeasureToXML( aOut,
267 0 : aLineDash.DotLen );
268 : }
269 0 : aStrValue = aOut.makeStringAndClear();
270 0 : rExport.AddAttribute( XML_NAMESPACE_DRAW, XML_DOTS1_LENGTH, aStrValue );
271 : }
272 : }
273 :
274 : // dashes
275 0 : if( aLineDash.Dashes )
276 : {
277 0 : rExport.AddAttribute( XML_NAMESPACE_DRAW, XML_DOTS2, OUString::number( aLineDash.Dashes ) );
278 :
279 0 : if( aLineDash.DashLen )
280 : {
281 : // dashes length
282 0 : if( bIsRel )
283 : {
284 0 : ::sax::Converter::convertPercent(aOut, aLineDash.DashLen);
285 : }
286 : else
287 : {
288 : rUnitConverter.convertMeasureToXML( aOut,
289 0 : aLineDash.DashLen );
290 : }
291 0 : aStrValue = aOut.makeStringAndClear();
292 0 : rExport.AddAttribute( XML_NAMESPACE_DRAW, XML_DOTS2_LENGTH, aStrValue );
293 : }
294 : }
295 :
296 : // distance
297 0 : if( bIsRel )
298 : {
299 0 : ::sax::Converter::convertPercent( aOut, aLineDash.Distance );
300 : }
301 : else
302 : {
303 : rUnitConverter.convertMeasureToXML( aOut,
304 0 : aLineDash.Distance );
305 : }
306 0 : aStrValue = aOut.makeStringAndClear();
307 0 : rExport.AddAttribute( XML_NAMESPACE_DRAW, XML_DISTANCE, aStrValue );
308 :
309 : // do Write
310 : SvXMLElementExport rElem( rExport,
311 : XML_NAMESPACE_DRAW, XML_STROKE_DASH,
312 0 : true, false );
313 : }
314 : }
315 0 : return bRet;
316 : }
317 :
318 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|