xref: /trunk/main/xmloff/source/style/HatchStyle.cxx (revision 1ecadb572e7010ff3b3382ad9bf179dbc6efadbb)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_xmloff.hxx"
30 #include "xmloff/HatchStyle.hxx"
31 #include <com/sun/star/drawing/Hatch.hpp>
32 #include <xmloff/nmspmap.hxx>
33 #include <xmloff/xmluconv.hxx>
34 #include "xmloff/xmlnmspe.hxx"
35 #include <xmloff/xmltoken.hxx>
36 #include <xmloff/xmlexp.hxx>
37 #include <xmloff/xmlimp.hxx>
38 #include <rtl/ustrbuf.hxx>
39 #include <rtl/ustring.hxx>
40 #include <tools/debug.hxx>
41 #include <xmloff/xmltkmap.hxx>
42 
43 using namespace ::com::sun::star;
44 using ::rtl::OUString;
45 using ::rtl::OUStringBuffer;
46 
47 using namespace ::xmloff::token;
48 
49 enum SvXMLTokenMapAttrs
50 {
51     XML_TOK_HATCH_NAME,
52     XML_TOK_HATCH_DISPLAY_NAME,
53     XML_TOK_HATCH_STYLE,
54     XML_TOK_HATCH_COLOR,
55     XML_TOK_HATCH_DISTANCE,
56     XML_TOK_HATCH_ROTATION,
57     XML_TOK_TABSTOP_END=XML_TOK_UNKNOWN
58 };
59 
60 
61 SvXMLEnumMapEntry __READONLY_DATA pXML_HatchStyle_Enum[] =
62 {
63     { XML_HATCHSTYLE_SINGLE,    drawing::HatchStyle_SINGLE },
64     { XML_HATCHSTYLE_DOUBLE,    drawing::HatchStyle_DOUBLE },
65     { XML_HATCHSTYLE_TRIPLE,    drawing::HatchStyle_TRIPLE },
66     { XML_TOKEN_INVALID, 0 }
67 };
68 
69 
70 //-------------------------------------------------------------
71 // Import
72 //-------------------------------------------------------------
73 
74 XMLHatchStyleImport::XMLHatchStyleImport( SvXMLImport& rImp )
75     : rImport(rImp)
76 {
77 }
78 
79 XMLHatchStyleImport::~XMLHatchStyleImport()
80 {
81 }
82 
83 sal_Bool XMLHatchStyleImport::importXML(
84     const uno::Reference< xml::sax::XAttributeList >& xAttrList,
85     uno::Any& rValue,
86     OUString& rStrName )
87 {
88     sal_Bool bRet = sal_False;
89 
90     sal_Bool bHasName  = sal_False;
91     sal_Bool bHasStyle = sal_False;
92     sal_Bool bHasColor = sal_False;
93     sal_Bool bHasDist  = sal_False;
94     OUString aDisplayName;
95 
96     drawing::Hatch aHatch;
97     aHatch.Style = drawing::HatchStyle_SINGLE;
98     aHatch.Color = 0;
99     aHatch.Distance = 0;
100     aHatch.Angle = 0;
101 
102     {
103         static __FAR_DATA SvXMLTokenMapEntry aHatchAttrTokenMap[] =
104 {
105     { XML_NAMESPACE_DRAW, XML_NAME, XML_TOK_HATCH_NAME },
106     { XML_NAMESPACE_DRAW, XML_DISPLAY_NAME, XML_TOK_HATCH_DISPLAY_NAME },
107     { XML_NAMESPACE_DRAW, XML_STYLE, XML_TOK_HATCH_STYLE },
108     { XML_NAMESPACE_DRAW, XML_COLOR, XML_TOK_HATCH_COLOR },
109     { XML_NAMESPACE_DRAW, XML_HATCH_DISTANCE, XML_TOK_HATCH_DISTANCE },
110     { XML_NAMESPACE_DRAW, XML_ROTATION, XML_TOK_HATCH_ROTATION },
111     XML_TOKEN_MAP_END
112 };
113 
114     SvXMLTokenMap aTokenMap( aHatchAttrTokenMap );
115     SvXMLNamespaceMap rNamespaceMap = rImport.GetNamespaceMap();
116     SvXMLUnitConverter& rUnitConverter = rImport.GetMM100UnitConverter();
117 
118     sal_Int16 nAttrCount = xAttrList.is() ? xAttrList->getLength() : 0;
119     for( sal_Int16 i=0; i < nAttrCount; i++ )
120     {
121         const OUString& rFullAttrName = xAttrList->getNameByIndex( i );
122         OUString aStrAttrName;
123         sal_uInt16 nPrefix = rNamespaceMap.GetKeyByAttrName( rFullAttrName, &aStrAttrName );
124         const OUString& rStrValue = xAttrList->getValueByIndex( i );
125 
126         switch( aTokenMap.Get( nPrefix, aStrAttrName ) )
127         {
128             case XML_TOK_HATCH_NAME:
129                 {
130                     rStrName = rStrValue;
131                     bHasName = sal_True;
132                 }
133                 break;
134             case XML_TOK_HATCH_DISPLAY_NAME:
135                 aDisplayName = rStrValue;
136                 break;
137             case XML_TOK_HATCH_STYLE:
138                 {
139                     sal_uInt16 eValue;
140                     bHasStyle = rUnitConverter.convertEnum( eValue, rStrValue, pXML_HatchStyle_Enum );
141                     if( bHasStyle )
142                         aHatch.Style = (drawing::HatchStyle) eValue;
143                 }
144                 break;
145             case XML_TOK_HATCH_COLOR:
146                 {
147                     Color aColor;
148                     bHasColor = rUnitConverter.convertColor( aColor, rStrValue );
149                     if( bHasColor )
150                         aHatch.Color = (sal_Int32)( aColor.GetColor() );
151                 }
152                 break;
153             case XML_TOK_HATCH_DISTANCE:
154                 bHasDist = rUnitConverter.convertMeasure( (sal_Int32&)aHatch.Distance, rStrValue );
155                 break;
156             case XML_TOK_HATCH_ROTATION:
157                 {
158                     sal_Int32 nValue;
159                     rUnitConverter.convertNumber( nValue, rStrValue, 0, 3600 );
160                     aHatch.Angle = sal_Int16( nValue );
161                 }
162                 break;
163 
164             default:
165                 DBG_WARNING( "Unknown token at import hatch style" )
166                 ;
167         }
168     }
169 
170     rValue <<= aHatch;
171 
172     if( aDisplayName.getLength() )
173     {
174         rImport.AddStyleDisplayName( XML_STYLE_FAMILY_SD_HATCH_ID, rStrName,
175                                      aDisplayName );
176         rStrName = aDisplayName;
177     }
178 
179     bRet = bHasName && bHasStyle && bHasColor && bHasDist;
180 
181     }
182 
183     return bRet;
184 }
185 
186 
187 //-------------------------------------------------------------
188 // Export
189 //-------------------------------------------------------------
190 
191 #ifndef SVX_LIGHT
192 
193 XMLHatchStyleExport::XMLHatchStyleExport( SvXMLExport& rExp )
194     : rExport(rExp)
195 {
196 }
197 
198 XMLHatchStyleExport::~XMLHatchStyleExport()
199 {
200 }
201 
202 sal_Bool XMLHatchStyleExport::exportXML(
203     const OUString& rStrName,
204     const uno::Any& rValue )
205 {
206     sal_Bool bRet = sal_False;
207     drawing::Hatch aHatch;
208 
209     if( rStrName.getLength() )
210     {
211         if( rValue >>= aHatch )
212         {
213             OUString aStrValue;
214             OUStringBuffer aOut;
215 
216             SvXMLUnitConverter& rUnitConverter =
217                 rExport.GetMM100UnitConverter();
218 
219             // Style
220             if( !rUnitConverter.convertEnum( aOut, aHatch.Style, pXML_HatchStyle_Enum ) )
221             {
222                 bRet = sal_False;
223             }
224             else
225             {
226                 // Name
227                 sal_Bool bEncoded = sal_False;
228                 rExport.AddAttribute( XML_NAMESPACE_DRAW, XML_NAME,
229                                       rExport.EncodeStyleName( rStrName,
230                                                                &bEncoded ) );
231                 if( bEncoded )
232                     rExport.AddAttribute( XML_NAMESPACE_DRAW, XML_DISPLAY_NAME,
233                                           rStrName );
234 
235                 aStrValue = aOut.makeStringAndClear();
236                 rExport.AddAttribute( XML_NAMESPACE_DRAW, XML_STYLE, aStrValue );
237 
238                 // Color
239                 rUnitConverter.convertColor( aOut, Color( aHatch.Color ) );
240                 aStrValue = aOut.makeStringAndClear();
241                 rExport.AddAttribute( XML_NAMESPACE_DRAW, XML_COLOR, aStrValue );
242 
243                 // Distance
244                 rUnitConverter.convertMeasure( aOut, aHatch.Distance );
245                 aStrValue = aOut.makeStringAndClear();
246                 rExport.AddAttribute( XML_NAMESPACE_DRAW, XML_HATCH_DISTANCE, aStrValue );
247 
248                 // Angle
249                 rUnitConverter.convertNumber( aOut, sal_Int32( aHatch.Angle ) );
250                 aStrValue = aOut.makeStringAndClear();
251                 rExport.AddAttribute( XML_NAMESPACE_DRAW, XML_ROTATION, aStrValue );
252 
253                 // Do Write
254                 SvXMLElementExport rElem( rExport, XML_NAMESPACE_DRAW, XML_HATCH,
255                                           sal_True, sal_False );
256             }
257         }
258     }
259 
260     return bRet;
261 }
262 
263 #endif // #ifndef SVX_LIGHT
264