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