bump product version to 5.0.4.1
[LibreOffice.git] / xmloff / source / style / HatchStyle.cxx
blob3dbbb45467a1fe134e1fcaa3cf337f1fafb3e422
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 .
20 #include <xmloff/HatchStyle.hxx>
22 #include <com/sun/star/drawing/Hatch.hpp>
24 #include <sax/tools/converter.hxx>
26 #include <xmloff/nmspmap.hxx>
27 #include <xmloff/xmluconv.hxx>
28 #include <xmloff/xmlnmspe.hxx>
29 #include <xmloff/xmltoken.hxx>
30 #include <xmloff/xmlexp.hxx>
31 #include <xmloff/xmlimp.hxx>
32 #include <rtl/ustrbuf.hxx>
33 #include <rtl/ustring.hxx>
34 #include <tools/debug.hxx>
35 #include <xmloff/xmltkmap.hxx>
37 using namespace ::com::sun::star;
39 using namespace ::xmloff::token;
41 enum SvXMLTokenMapAttrs
43 XML_TOK_HATCH_NAME,
44 XML_TOK_HATCH_DISPLAY_NAME,
45 XML_TOK_HATCH_STYLE,
46 XML_TOK_HATCH_COLOR,
47 XML_TOK_HATCH_DISTANCE,
48 XML_TOK_HATCH_ROTATION,
49 XML_TOK_TABSTOP_END=XML_TOK_UNKNOWN
52 SvXMLEnumMapEntry const pXML_HatchStyle_Enum[] =
54 { XML_HATCHSTYLE_SINGLE, drawing::HatchStyle_SINGLE },
55 { XML_HATCHSTYLE_DOUBLE, drawing::HatchStyle_DOUBLE },
56 { XML_HATCHSTYLE_TRIPLE, drawing::HatchStyle_TRIPLE },
57 { XML_TOKEN_INVALID, 0 }
60 // Import
62 XMLHatchStyleImport::XMLHatchStyleImport( SvXMLImport& rImp )
63 : rImport(rImp)
67 XMLHatchStyleImport::~XMLHatchStyleImport()
71 bool XMLHatchStyleImport::importXML(
72 const uno::Reference< xml::sax::XAttributeList >& xAttrList,
73 uno::Any& rValue,
74 OUString& rStrName )
76 static const SvXMLTokenMapEntry aHatchAttrTokenMap[] =
78 { XML_NAMESPACE_DRAW, XML_NAME, XML_TOK_HATCH_NAME },
79 { XML_NAMESPACE_DRAW, XML_DISPLAY_NAME, XML_TOK_HATCH_DISPLAY_NAME },
80 { XML_NAMESPACE_DRAW, XML_STYLE, XML_TOK_HATCH_STYLE },
81 { XML_NAMESPACE_DRAW, XML_COLOR, XML_TOK_HATCH_COLOR },
82 { XML_NAMESPACE_DRAW, XML_HATCH_DISTANCE, XML_TOK_HATCH_DISTANCE },
83 { XML_NAMESPACE_DRAW, XML_ROTATION, XML_TOK_HATCH_ROTATION },
84 XML_TOKEN_MAP_END
87 bool bHasName = false;
88 bool bHasStyle = false;
89 bool bHasColor = false;
90 bool bHasDist = false;
91 OUString aDisplayName;
93 drawing::Hatch aHatch;
94 aHatch.Style = drawing::HatchStyle_SINGLE;
95 aHatch.Color = 0;
96 aHatch.Distance = 0;
97 aHatch.Angle = 0;
99 SvXMLTokenMap aTokenMap( aHatchAttrTokenMap );
100 SvXMLNamespaceMap rNamespaceMap = rImport.GetNamespaceMap();
101 SvXMLUnitConverter& rUnitConverter = rImport.GetMM100UnitConverter();
103 sal_Int16 nAttrCount = xAttrList.is() ? xAttrList->getLength() : 0;
104 for( sal_Int16 i=0; i < nAttrCount; i++ )
106 const OUString& rFullAttrName = xAttrList->getNameByIndex( i );
107 OUString aStrAttrName;
108 sal_uInt16 nPrefix = rNamespaceMap.GetKeyByAttrName( rFullAttrName, &aStrAttrName );
109 const OUString& rStrValue = xAttrList->getValueByIndex( i );
111 switch( aTokenMap.Get( nPrefix, aStrAttrName ) )
113 case XML_TOK_HATCH_NAME:
115 rStrName = rStrValue;
116 bHasName = true;
118 break;
119 case XML_TOK_HATCH_DISPLAY_NAME:
120 aDisplayName = rStrValue;
121 break;
122 case XML_TOK_HATCH_STYLE:
124 sal_uInt16 eValue;
125 bHasStyle = SvXMLUnitConverter::convertEnum( eValue, rStrValue, pXML_HatchStyle_Enum );
126 if( bHasStyle )
127 aHatch.Style = (drawing::HatchStyle) eValue;
129 break;
130 case XML_TOK_HATCH_COLOR:
132 bHasColor = ::sax::Converter::convertColor(
133 aHatch.Color, rStrValue);
135 break;
136 case XML_TOK_HATCH_DISTANCE:
137 bHasDist = rUnitConverter.convertMeasureToCore(
138 (sal_Int32&)aHatch.Distance, rStrValue );
139 break;
140 case XML_TOK_HATCH_ROTATION:
142 sal_Int32 nValue;
143 ::sax::Converter::convertNumber(nValue, rStrValue, 0, 3600);
144 aHatch.Angle = sal_Int16( nValue );
146 break;
148 default:
149 SAL_INFO("xmloff.style", "Unknown token at import hatch style");
153 rValue <<= aHatch;
155 if( !aDisplayName.isEmpty() )
157 rImport.AddStyleDisplayName( XML_STYLE_FAMILY_SD_HATCH_ID, rStrName,
158 aDisplayName );
159 rStrName = aDisplayName;
162 bool bRet = bHasName && bHasStyle && bHasColor && bHasDist;
164 return bRet;
167 // Export
169 XMLHatchStyleExport::XMLHatchStyleExport( SvXMLExport& rExp )
170 : rExport(rExp)
174 XMLHatchStyleExport::~XMLHatchStyleExport()
178 bool XMLHatchStyleExport::exportXML(
179 const OUString& rStrName,
180 const uno::Any& rValue )
182 bool bRet = false;
183 drawing::Hatch aHatch;
185 if( !rStrName.isEmpty() )
187 if( rValue >>= aHatch )
189 OUString aStrValue;
190 OUStringBuffer aOut;
192 SvXMLUnitConverter& rUnitConverter =
193 rExport.GetMM100UnitConverter();
195 // Style
196 if( !SvXMLUnitConverter::convertEnum( aOut, aHatch.Style, pXML_HatchStyle_Enum ) )
198 bRet = false;
200 else
202 // Name
203 bool bEncoded = false;
204 rExport.AddAttribute( XML_NAMESPACE_DRAW, XML_NAME,
205 rExport.EncodeStyleName( rStrName,
206 &bEncoded ) );
207 if( bEncoded )
208 rExport.AddAttribute( XML_NAMESPACE_DRAW, XML_DISPLAY_NAME,
209 rStrName );
211 aStrValue = aOut.makeStringAndClear();
212 rExport.AddAttribute( XML_NAMESPACE_DRAW, XML_STYLE, aStrValue );
214 // Color
215 ::sax::Converter::convertColor(aOut, aHatch.Color);
216 aStrValue = aOut.makeStringAndClear();
217 rExport.AddAttribute( XML_NAMESPACE_DRAW, XML_COLOR, aStrValue );
219 // Distance
220 rUnitConverter.convertMeasureToXML( aOut, aHatch.Distance );
221 aStrValue = aOut.makeStringAndClear();
222 rExport.AddAttribute( XML_NAMESPACE_DRAW, XML_HATCH_DISTANCE, aStrValue );
224 // Angle
225 ::sax::Converter::convertNumber(aOut, sal_Int32(aHatch.Angle));
226 aStrValue = aOut.makeStringAndClear();
227 rExport.AddAttribute( XML_NAMESPACE_DRAW, XML_ROTATION, aStrValue );
229 // Do Write
230 SvXMLElementExport rElem( rExport, XML_NAMESPACE_DRAW, XML_HATCH,
231 true, false );
236 return bRet;
239 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */