bump product version to 4.1.6.2
[LibreOffice.git] / xmloff / source / style / HatchStyle.cxx
blob5d852c952d9db5ab8b43925d281e1da9c296bf71
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 .
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
44 XML_TOK_HATCH_NAME,
45 XML_TOK_HATCH_DISPLAY_NAME,
46 XML_TOK_HATCH_STYLE,
47 XML_TOK_HATCH_COLOR,
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 //-------------------------------------------------------------
64 // Import
65 //-------------------------------------------------------------
67 XMLHatchStyleImport::XMLHatchStyleImport( SvXMLImport& rImp )
68 : rImport(rImp)
72 XMLHatchStyleImport::~XMLHatchStyleImport()
76 sal_Bool XMLHatchStyleImport::importXML(
77 const uno::Reference< xml::sax::XAttributeList >& xAttrList,
78 uno::Any& rValue,
79 OUString& rStrName )
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;
91 aHatch.Color = 0;
92 aHatch.Distance = 0;
93 aHatch.Angle = 0;
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 },
104 XML_TOKEN_MAP_END
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;
124 bHasName = sal_True;
126 break;
127 case XML_TOK_HATCH_DISPLAY_NAME:
128 aDisplayName = rStrValue;
129 break;
130 case XML_TOK_HATCH_STYLE:
132 sal_uInt16 eValue;
133 bHasStyle = rUnitConverter.convertEnum( eValue, rStrValue, pXML_HatchStyle_Enum );
134 if( bHasStyle )
135 aHatch.Style = (drawing::HatchStyle) eValue;
137 break;
138 case XML_TOK_HATCH_COLOR:
140 bHasColor = ::sax::Converter::convertColor(
141 aHatch.Color, rStrValue);
143 break;
144 case XML_TOK_HATCH_DISTANCE:
145 bHasDist = rUnitConverter.convertMeasureToCore(
146 (sal_Int32&)aHatch.Distance, rStrValue );
147 break;
148 case XML_TOK_HATCH_ROTATION:
150 sal_Int32 nValue;
151 ::sax::Converter::convertNumber(nValue, rStrValue, 0, 3600);
152 aHatch.Angle = sal_Int16( nValue );
154 break;
156 default:
157 DBG_WARNING( "Unknown token at import hatch style" )
162 rValue <<= aHatch;
164 if( !aDisplayName.isEmpty() )
166 rImport.AddStyleDisplayName( XML_STYLE_FAMILY_SD_HATCH_ID, rStrName,
167 aDisplayName );
168 rStrName = aDisplayName;
171 bRet = bHasName && bHasStyle && bHasColor && bHasDist;
175 return bRet;
179 //-------------------------------------------------------------
180 // Export
181 //-------------------------------------------------------------
183 XMLHatchStyleExport::XMLHatchStyleExport( SvXMLExport& rExp )
184 : rExport(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 )
203 OUString aStrValue;
204 OUStringBuffer aOut;
206 SvXMLUnitConverter& rUnitConverter =
207 rExport.GetMM100UnitConverter();
209 // Style
210 if( !rUnitConverter.convertEnum( aOut, aHatch.Style, pXML_HatchStyle_Enum ) )
212 bRet = sal_False;
214 else
216 // Name
217 sal_Bool bEncoded = sal_False;
218 rExport.AddAttribute( XML_NAMESPACE_DRAW, XML_NAME,
219 rExport.EncodeStyleName( rStrName,
220 &bEncoded ) );
221 if( bEncoded )
222 rExport.AddAttribute( XML_NAMESPACE_DRAW, XML_DISPLAY_NAME,
223 rStrName );
225 aStrValue = aOut.makeStringAndClear();
226 rExport.AddAttribute( XML_NAMESPACE_DRAW, XML_STYLE, aStrValue );
228 // Color
229 ::sax::Converter::convertColor(aOut, aHatch.Color);
230 aStrValue = aOut.makeStringAndClear();
231 rExport.AddAttribute( XML_NAMESPACE_DRAW, XML_COLOR, aStrValue );
233 // Distance
234 rUnitConverter.convertMeasureToXML( aOut, aHatch.Distance );
235 aStrValue = aOut.makeStringAndClear();
236 rExport.AddAttribute( XML_NAMESPACE_DRAW, XML_HATCH_DISTANCE, aStrValue );
238 // Angle
239 ::sax::Converter::convertNumber(aOut, sal_Int32(aHatch.Angle));
240 aStrValue = aOut.makeStringAndClear();
241 rExport.AddAttribute( XML_NAMESPACE_DRAW, XML_ROTATION, aStrValue );
243 // Do Write
244 SvXMLElementExport rElem( rExport, XML_NAMESPACE_DRAW, XML_HATCH,
245 sal_True, sal_False );
250 return bRet;
253 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */