1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: DashStyle.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_xmloff.hxx"
33 #include <com/sun/star/drawing/DashStyle.hpp>
34 #include <com/sun/star/drawing/LineDash.hpp>
35 #include "DashStyle.hxx"
36 #include <xmloff/attrlist.hxx>
37 #include <xmloff/nmspmap.hxx>
38 #include <xmloff/xmluconv.hxx>
39 #include "xmlnmspe.hxx"
40 #include <xmloff/xmltoken.hxx>
41 #include <xmloff/xmlexp.hxx>
42 #include <xmloff/xmlimp.hxx>
43 #include <rtl/ustrbuf.hxx>
44 #include <rtl/ustring.hxx>
45 #include <tools/debug.hxx>
46 #include <xmloff/xmltkmap.hxx>
48 using namespace ::com::sun::star
;
49 using ::rtl::OUString
;
50 using ::rtl::OUStringBuffer
;
52 using namespace ::xmloff::token
;
54 enum SvXMLTokenMapAttrs
57 XML_TOK_DASH_DISPLAY_NAME
,
60 XML_TOK_DASH_DOTS1LEN
,
62 XML_TOK_DASH_DOTS2LEN
,
63 XML_TOK_DASH_DISTANCE
,
64 XML_TOK_DASH_END
=XML_TOK_UNKNOWN
67 static __FAR_DATA SvXMLTokenMapEntry aDashStyleAttrTokenMap
[] =
69 { XML_NAMESPACE_DRAW
, XML_NAME
, XML_TOK_DASH_NAME
},
70 { XML_NAMESPACE_DRAW
, XML_DISPLAY_NAME
, XML_TOK_DASH_DISPLAY_NAME
},
71 { XML_NAMESPACE_DRAW
, XML_STYLE
, XML_TOK_DASH_STYLE
},
72 { XML_NAMESPACE_DRAW
, XML_DOTS1
, XML_TOK_DASH_DOTS1
},
73 { XML_NAMESPACE_DRAW
, XML_DOTS1_LENGTH
, XML_TOK_DASH_DOTS1LEN
},
74 { XML_NAMESPACE_DRAW
, XML_DOTS2
, XML_TOK_DASH_DOTS2
},
75 { XML_NAMESPACE_DRAW
, XML_DOTS2_LENGTH
, XML_TOK_DASH_DOTS2LEN
},
76 { XML_NAMESPACE_DRAW
, XML_DISTANCE
, XML_TOK_DASH_DISTANCE
},
80 SvXMLEnumMapEntry __READONLY_DATA pXML_DashStyle_Enum
[] =
82 { XML_RECT
, drawing::DashStyle_RECT
},
83 { XML_ROUND
, drawing::DashStyle_ROUND
},
84 { XML_RECT
, drawing::DashStyle_RECTRELATIVE
},
85 { XML_ROUND
, drawing::DashStyle_ROUNDRELATIVE
},
86 { XML_TOKEN_INVALID
, 0 }
89 //-------------------------------------------------------------
91 //-------------------------------------------------------------
93 XMLDashStyleImport::XMLDashStyleImport( SvXMLImport
& rImp
)
98 XMLDashStyleImport::~XMLDashStyleImport()
102 sal_Bool
XMLDashStyleImport::importXML(
103 const uno::Reference
< xml::sax::XAttributeList
>& xAttrList
,
107 drawing::LineDash aLineDash
;
108 aLineDash
.Style
= drawing::DashStyle_RECT
;
110 aLineDash
.DotLen
= 0;
111 aLineDash
.Dashes
= 0;
112 aLineDash
.DashLen
= 0;
113 aLineDash
.Distance
= 20;
114 OUString aDisplayName
;
116 sal_Bool bIsRel
= sal_False
;
118 SvXMLNamespaceMap
& rNamespaceMap
= rImport
.GetNamespaceMap();
119 SvXMLUnitConverter
& rUnitConverter
= rImport
.GetMM100UnitConverter();
121 SvXMLTokenMap
aTokenMap( aDashStyleAttrTokenMap
);
123 sal_Int16 nAttrCount
= xAttrList
.is() ? xAttrList
->getLength() : 0;
124 for( sal_Int16 i
=0; i
< nAttrCount
; i
++ )
126 const OUString
& rFullAttrName
= xAttrList
->getNameByIndex( i
);
127 OUString aStrAttrName
;
128 sal_uInt16 nPrefix
= rNamespaceMap
.GetKeyByAttrName( rFullAttrName
, &aStrAttrName
);
129 const OUString
& rStrValue
= xAttrList
->getValueByIndex( i
);
131 switch( aTokenMap
.Get( nPrefix
, aStrAttrName
) )
133 case XML_TOK_DASH_NAME
:
135 rStrName
= rStrValue
;
138 case XML_TOK_DASH_DISPLAY_NAME
:
140 aDisplayName
= rStrValue
;
143 case XML_TOK_DASH_STYLE
:
146 if( rUnitConverter
.convertEnum( eValue
, rStrValue
, pXML_DashStyle_Enum
) )
148 aLineDash
.Style
= (drawing::DashStyle
) eValue
;
152 case XML_TOK_DASH_DOTS1
:
153 aLineDash
.Dots
= (sal_Int16
)rStrValue
.toInt32();
156 case XML_TOK_DASH_DOTS1LEN
:
158 if( rStrValue
.indexOf( sal_Unicode('%') ) != -1 ) // its a percentage
161 rUnitConverter
.convertPercent( aLineDash
.DotLen
, rStrValue
);
165 rUnitConverter
.convertMeasure( aLineDash
.DotLen
, rStrValue
);
170 case XML_TOK_DASH_DOTS2
:
171 aLineDash
.Dashes
= (sal_Int16
)rStrValue
.toInt32();
174 case XML_TOK_DASH_DOTS2LEN
:
176 if( rStrValue
.indexOf( sal_Unicode('%') ) != -1 ) // its a percentage
179 rUnitConverter
.convertPercent( aLineDash
.DashLen
, rStrValue
);
183 rUnitConverter
.convertMeasure( aLineDash
.DashLen
, rStrValue
);
188 case XML_TOK_DASH_DISTANCE
:
190 if( rStrValue
.indexOf( sal_Unicode('%') ) != -1 ) // its a percentage
193 rUnitConverter
.convertPercent( aLineDash
.Distance
, rStrValue
);
197 rUnitConverter
.convertMeasure( aLineDash
.Distance
, rStrValue
);
202 DBG_WARNING( "Unknown token at import gradient style" );
207 aLineDash
.Style
= aLineDash
.Style
== drawing::DashStyle_RECT
? drawing::DashStyle_RECTRELATIVE
: drawing::DashStyle_ROUNDRELATIVE
;
209 rValue
<<= aLineDash
;
211 if( aDisplayName
.getLength() )
213 rImport
.AddStyleDisplayName( XML_STYLE_FAMILY_SD_STROKE_DASH_ID
,
214 rStrName
, aDisplayName
);
215 rStrName
= aDisplayName
;
222 //-------------------------------------------------------------
224 //-------------------------------------------------------------
228 XMLDashStyleExport::XMLDashStyleExport( SvXMLExport
& rExp
)
233 XMLDashStyleExport::~XMLDashStyleExport()
237 sal_Bool
XMLDashStyleExport::exportXML(
238 const OUString
& rStrName
,
239 const uno::Any
& rValue
)
241 sal_Bool bRet
= sal_False
;
243 SvXMLUnitConverter rUnitConverter
= rExport
.GetMM100UnitConverter();
245 drawing::LineDash aLineDash
;
247 if( rStrName
.getLength() )
249 if( rValue
>>= aLineDash
)
251 sal_Bool bIsRel
= aLineDash
.Style
== drawing::DashStyle_RECTRELATIVE
|| aLineDash
.Style
== drawing::DashStyle_ROUNDRELATIVE
;
257 sal_Bool bEncoded
= sal_False
;
258 rExport
.AddAttribute( XML_NAMESPACE_DRAW
, XML_NAME
,
259 rExport
.EncodeStyleName( rStrName
,
262 rExport
.AddAttribute( XML_NAMESPACE_DRAW
, XML_DISPLAY_NAME
,
266 rUnitConverter
.convertEnum( aOut
, aLineDash
.Style
, pXML_DashStyle_Enum
);
267 aStrValue
= aOut
.makeStringAndClear();
268 rExport
.AddAttribute( XML_NAMESPACE_DRAW
, XML_STYLE
, aStrValue
);
274 rExport
.AddAttribute( XML_NAMESPACE_DRAW
, XML_DOTS1
, OUString::valueOf( (sal_Int32
)aLineDash
.Dots
) );
276 if( aLineDash
.DotLen
)
281 rUnitConverter
.convertPercent( aOut
, aLineDash
.DotLen
);
285 rUnitConverter
.convertMeasure( aOut
, aLineDash
.DotLen
);
287 aStrValue
= aOut
.makeStringAndClear();
288 rExport
.AddAttribute( XML_NAMESPACE_DRAW
, XML_DOTS1_LENGTH
, aStrValue
);
293 if( aLineDash
.Dashes
)
295 rExport
.AddAttribute( XML_NAMESPACE_DRAW
, XML_DOTS2
, OUString::valueOf( (sal_Int32
)aLineDash
.Dashes
) );
297 if( aLineDash
.DashLen
)
302 rUnitConverter
.convertPercent( aOut
, aLineDash
.DashLen
);
306 rUnitConverter
.convertMeasure( aOut
, aLineDash
.DashLen
);
308 aStrValue
= aOut
.makeStringAndClear();
309 rExport
.AddAttribute( XML_NAMESPACE_DRAW
, XML_DOTS2_LENGTH
, aStrValue
);
316 rUnitConverter
.convertPercent( aOut
, aLineDash
.Distance
);
320 rUnitConverter
.convertMeasure( aOut
, aLineDash
.Distance
);
322 aStrValue
= aOut
.makeStringAndClear();
323 rExport
.AddAttribute( XML_NAMESPACE_DRAW
, XML_DISTANCE
, aStrValue
);
327 SvXMLElementExport
rElem( rExport
,
328 XML_NAMESPACE_DRAW
, XML_STROKE_DASH
,
329 sal_True
, sal_False
);
335 #endif // #ifndef SVX_LIGHT