1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
21 #include "xmloff/HatchStyle.hxx"
23 #include <com/sun/star/drawing/Hatch.hpp>
25 #include <sax/tools/converter.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>
38 using namespace ::com::sun::star
;
40 using namespace ::xmloff::token
;
42 enum SvXMLTokenMapAttrs
45 XML_TOK_HATCH_DISPLAY_NAME
,
48 XML_TOK_HATCH_DISTANCE
,
49 XML_TOK_HATCH_ROTATION
,
50 XML_TOK_TABSTOP_END
=XML_TOK_UNKNOWN
54 SvXMLEnumMapEntry
const pXML_HatchStyle_Enum
[] =
56 { XML_HATCHSTYLE_SINGLE
, drawing::HatchStyle_SINGLE
},
57 { XML_HATCHSTYLE_DOUBLE
, drawing::HatchStyle_DOUBLE
},
58 { XML_HATCHSTYLE_TRIPLE
, drawing::HatchStyle_TRIPLE
},
59 { XML_TOKEN_INVALID
, 0 }
63 //-------------------------------------------------------------
65 //-------------------------------------------------------------
67 XMLHatchStyleImport::XMLHatchStyleImport( SvXMLImport
& rImp
)
72 XMLHatchStyleImport::~XMLHatchStyleImport()
76 sal_Bool
XMLHatchStyleImport::importXML(
77 const uno::Reference
< xml::sax::XAttributeList
>& xAttrList
,
81 sal_Bool bRet
= sal_False
;
83 sal_Bool bHasName
= sal_False
;
84 sal_Bool bHasStyle
= sal_False
;
85 sal_Bool bHasColor
= sal_False
;
86 sal_Bool bHasDist
= sal_False
;
87 OUString aDisplayName
;
89 drawing::Hatch aHatch
;
90 aHatch
.Style
= drawing::HatchStyle_SINGLE
;
96 static SvXMLTokenMapEntry aHatchAttrTokenMap
[] =
98 { XML_NAMESPACE_DRAW
, XML_NAME
, XML_TOK_HATCH_NAME
},
99 { XML_NAMESPACE_DRAW
, XML_DISPLAY_NAME
, XML_TOK_HATCH_DISPLAY_NAME
},
100 { XML_NAMESPACE_DRAW
, XML_STYLE
, XML_TOK_HATCH_STYLE
},
101 { XML_NAMESPACE_DRAW
, XML_COLOR
, XML_TOK_HATCH_COLOR
},
102 { XML_NAMESPACE_DRAW
, XML_HATCH_DISTANCE
, XML_TOK_HATCH_DISTANCE
},
103 { XML_NAMESPACE_DRAW
, XML_ROTATION
, XML_TOK_HATCH_ROTATION
},
107 SvXMLTokenMap
aTokenMap( aHatchAttrTokenMap
);
108 SvXMLNamespaceMap rNamespaceMap
= rImport
.GetNamespaceMap();
109 SvXMLUnitConverter
& rUnitConverter
= rImport
.GetMM100UnitConverter();
111 sal_Int16 nAttrCount
= xAttrList
.is() ? xAttrList
->getLength() : 0;
112 for( sal_Int16 i
=0; i
< nAttrCount
; i
++ )
114 const OUString
& rFullAttrName
= xAttrList
->getNameByIndex( i
);
115 OUString aStrAttrName
;
116 sal_uInt16 nPrefix
= rNamespaceMap
.GetKeyByAttrName( rFullAttrName
, &aStrAttrName
);
117 const OUString
& rStrValue
= xAttrList
->getValueByIndex( i
);
119 switch( aTokenMap
.Get( nPrefix
, aStrAttrName
) )
121 case XML_TOK_HATCH_NAME
:
123 rStrName
= rStrValue
;
127 case XML_TOK_HATCH_DISPLAY_NAME
:
128 aDisplayName
= rStrValue
;
130 case XML_TOK_HATCH_STYLE
:
133 bHasStyle
= rUnitConverter
.convertEnum( eValue
, rStrValue
, pXML_HatchStyle_Enum
);
135 aHatch
.Style
= (drawing::HatchStyle
) eValue
;
138 case XML_TOK_HATCH_COLOR
:
140 bHasColor
= ::sax::Converter::convertColor(
141 aHatch
.Color
, rStrValue
);
144 case XML_TOK_HATCH_DISTANCE
:
145 bHasDist
= rUnitConverter
.convertMeasureToCore(
146 (sal_Int32
&)aHatch
.Distance
, rStrValue
);
148 case XML_TOK_HATCH_ROTATION
:
151 ::sax::Converter::convertNumber(nValue
, rStrValue
, 0, 3600);
152 aHatch
.Angle
= sal_Int16( nValue
);
157 DBG_WARNING( "Unknown token at import hatch style" )
164 if( !aDisplayName
.isEmpty() )
166 rImport
.AddStyleDisplayName( XML_STYLE_FAMILY_SD_HATCH_ID
, rStrName
,
168 rStrName
= aDisplayName
;
171 bRet
= bHasName
&& bHasStyle
&& bHasColor
&& bHasDist
;
179 //-------------------------------------------------------------
181 //-------------------------------------------------------------
183 XMLHatchStyleExport::XMLHatchStyleExport( SvXMLExport
& rExp
)
188 XMLHatchStyleExport::~XMLHatchStyleExport()
192 sal_Bool
XMLHatchStyleExport::exportXML(
193 const OUString
& rStrName
,
194 const uno::Any
& rValue
)
196 sal_Bool bRet
= sal_False
;
197 drawing::Hatch aHatch
;
199 if( !rStrName
.isEmpty() )
201 if( rValue
>>= aHatch
)
206 SvXMLUnitConverter
& rUnitConverter
=
207 rExport
.GetMM100UnitConverter();
210 if( !rUnitConverter
.convertEnum( aOut
, aHatch
.Style
, pXML_HatchStyle_Enum
) )
217 sal_Bool bEncoded
= sal_False
;
218 rExport
.AddAttribute( XML_NAMESPACE_DRAW
, XML_NAME
,
219 rExport
.EncodeStyleName( rStrName
,
222 rExport
.AddAttribute( XML_NAMESPACE_DRAW
, XML_DISPLAY_NAME
,
225 aStrValue
= aOut
.makeStringAndClear();
226 rExport
.AddAttribute( XML_NAMESPACE_DRAW
, XML_STYLE
, aStrValue
);
229 ::sax::Converter::convertColor(aOut
, aHatch
.Color
);
230 aStrValue
= aOut
.makeStringAndClear();
231 rExport
.AddAttribute( XML_NAMESPACE_DRAW
, XML_COLOR
, aStrValue
);
234 rUnitConverter
.convertMeasureToXML( aOut
, aHatch
.Distance
);
235 aStrValue
= aOut
.makeStringAndClear();
236 rExport
.AddAttribute( XML_NAMESPACE_DRAW
, XML_HATCH_DISTANCE
, aStrValue
);
239 ::sax::Converter::convertNumber(aOut
, sal_Int32(aHatch
.Angle
));
240 aStrValue
= aOut
.makeStringAndClear();
241 rExport
.AddAttribute( XML_NAMESPACE_DRAW
, XML_ROTATION
, aStrValue
);
244 SvXMLElementExport
rElem( rExport
, XML_NAMESPACE_DRAW
, XML_HATCH
,
245 sal_True
, sal_False
);
253 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */