LibreOffice Module xmloff (master)  1
DashStyle.cxx
Go to the documentation of this file.
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
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/.
8  *
9  * This file incorporates work covered by the following license notice:
10  *
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 .
18  */
19 
20 #include <com/sun/star/drawing/DashStyle.hpp>
21 #include <com/sun/star/drawing/LineDash.hpp>
22 
23 #include <sax/tools/converter.hxx>
24 
25 #include <xmloff/DashStyle.hxx>
26 #include <xmloff/namespacemap.hxx>
27 #include <xmloff/xmluconv.hxx>
28 #include <xmloff/xmlnamespace.hxx>
29 #include <xmloff/xmltoken.hxx>
30 #include <xmloff/xmlexp.hxx>
31 #include <xmloff/xmlimp.hxx>
32 #include <xmloff/xmlement.hxx>
33 #include <rtl/ustrbuf.hxx>
34 #include <rtl/ustring.hxx>
35 #include <sal/log.hxx>
36 #include <xmloff/xmltkmap.hxx>
37 
38 using namespace ::com::sun::star;
39 using namespace ::xmloff::token;
40 
42 {
43  { XML_RECT, drawing::DashStyle_RECT },
44  { XML_ROUND, drawing::DashStyle_ROUND },
45  { XML_RECT, drawing::DashStyle_RECTRELATIVE },
46  { XML_ROUND, drawing::DashStyle_ROUNDRELATIVE },
47  { XML_TOKEN_INVALID, drawing::DashStyle(0) }
48 };
49 
50 // Import
51 
52 XMLDashStyleImport::XMLDashStyleImport( SvXMLImport& rImp )
53  : rImport(rImp)
54 {
55 }
56 
57 XMLDashStyleImport::~XMLDashStyleImport()
58 {
59 }
60 
61 void XMLDashStyleImport::importXML(
62  const uno::Reference< xml::sax::XFastAttributeList >& xAttrList,
64  OUString& rStrName )
65 {
66  drawing::LineDash aLineDash;
67  aLineDash.Style = drawing::DashStyle_RECT;
68  aLineDash.Dots = 0;
69  aLineDash.DotLen = 0;
70  aLineDash.Dashes = 0;
71  aLineDash.DashLen = 0;
72  aLineDash.Distance = 20;
73  OUString aDisplayName;
74 
75  bool bIsRel = false;
76 
77  SvXMLUnitConverter& rUnitConverter = rImport.GetMM100UnitConverter();
78 
79  for (auto &aIter : sax_fastparser::castToFastAttributeList( xAttrList ))
80  {
81  const OUString aStrValue = aIter.toString();
82 
83  switch( aIter.getToken() )
84  {
85  case XML_ELEMENT(DRAW, XML_NAME):
86  case XML_ELEMENT(DRAW_OOO, XML_NAME):
87  {
88  rStrName = aStrValue;
89  }
90  break;
91  case XML_ELEMENT(DRAW, XML_DISPLAY_NAME):
92  case XML_ELEMENT(DRAW_OOO, XML_DISPLAY_NAME):
93  {
94  aDisplayName = aStrValue;
95  }
96  break;
97  case XML_ELEMENT(DRAW, XML_STYLE):
98  case XML_ELEMENT(DRAW_OOO, XML_STYLE):
99  {
100  SvXMLUnitConverter::convertEnum( aLineDash.Style, aStrValue, pXML_DashStyle_Enum );
101  }
102  break;
103  case XML_ELEMENT(DRAW, XML_DOTS1):
104  case XML_ELEMENT(DRAW_OOO, XML_DOTS1):
105  aLineDash.Dots = static_cast<sal_Int16>(aStrValue.toInt32());
106  break;
107 
108  case XML_ELEMENT(DRAW, XML_DOTS1_LENGTH):
109  case XML_ELEMENT(DRAW_OOO, XML_DOTS1_LENGTH):
110  {
111  if( aStrValue.indexOf( '%' ) != -1 ) // it's a percentage
112  {
113  bIsRel = true;
114  ::sax::Converter::convertPercent(aLineDash.DotLen, aStrValue);
115  }
116  else
117  {
118  rUnitConverter.convertMeasureToCore( aLineDash.DotLen,
119  aStrValue );
120  }
121  }
122  break;
123 
124  case XML_ELEMENT(DRAW, XML_DOTS2):
125  case XML_ELEMENT(DRAW_OOO, XML_DOTS2):
126  aLineDash.Dashes = static_cast<sal_Int16>(aStrValue.toInt32());
127  break;
128 
129  case XML_ELEMENT(DRAW, XML_DOTS2_LENGTH):
130  case XML_ELEMENT(DRAW_OOO, XML_DOTS2_LENGTH):
131  {
132  if( aStrValue.indexOf( '%' ) != -1 ) // it's a percentage
133  {
134  bIsRel = true;
135  ::sax::Converter::convertPercent(aLineDash.DashLen, aStrValue);
136  }
137  else
138  {
139  rUnitConverter.convertMeasureToCore( aLineDash.DashLen,
140  aStrValue );
141  }
142  }
143  break;
144 
145  case XML_ELEMENT(DRAW, XML_DISTANCE):
146  case XML_ELEMENT(DRAW_OOO, XML_DISTANCE):
147  {
148  if( aStrValue.indexOf( '%' ) != -1 ) // it's a percentage
149  {
150  bIsRel = true;
151  ::sax::Converter::convertPercent(aLineDash.Distance, aStrValue);
152  }
153  else
154  {
155  rUnitConverter.convertMeasureToCore( aLineDash.Distance,
156  aStrValue );
157  }
158  }
159  break;
160  default:
161  XMLOFF_WARN_UNKNOWN("xmloff.style", aIter);
162  }
163  }
164 
165  if( bIsRel )
166  aLineDash.Style = aLineDash.Style == drawing::DashStyle_RECT ? drawing::DashStyle_RECTRELATIVE : drawing::DashStyle_ROUNDRELATIVE;
167 
168  rValue <<= aLineDash;
169 
170  if( !aDisplayName.isEmpty() )
171  {
172  rImport.AddStyleDisplayName( XmlStyleFamily::SD_STROKE_DASH_ID,
173  rStrName, aDisplayName );
174  rStrName = aDisplayName;
175  }
176 }
177 
178 // Export
179 
180 XMLDashStyleExport::XMLDashStyleExport( SvXMLExport& rExp )
181  : rExport(rExp)
182 {
183 }
184 
185 XMLDashStyleExport::~XMLDashStyleExport()
186 {
187 }
188 
189 void XMLDashStyleExport::exportXML(
190  const OUString& rStrName,
191  const uno::Any& rValue )
192 {
193  SvXMLUnitConverter & rUnitConverter = rExport.GetMM100UnitConverter();
194 
195  drawing::LineDash aLineDash;
196 
197  if( rStrName.isEmpty() )
198  return;
199 
200  if( !(rValue >>= aLineDash) )
201  return;
202 
203  bool bIsRel = aLineDash.Style == drawing::DashStyle_RECTRELATIVE || aLineDash.Style == drawing::DashStyle_ROUNDRELATIVE;
204 
205  OUString aStrValue;
206  OUStringBuffer aOut;
207 
208  // Name
209  bool bEncoded = false;
211  rExport.EncodeStyleName( rStrName,
212  &bEncoded ) );
213  if( bEncoded )
215  rStrName );
216 
217  // Style
218  SvXMLUnitConverter::convertEnum( aOut, aLineDash.Style, pXML_DashStyle_Enum );
219  aStrValue = aOut.makeStringAndClear();
221 
222  // dots
223  if( aLineDash.Dots )
224  {
225  rExport.AddAttribute( XML_NAMESPACE_DRAW, XML_DOTS1, OUString::number( aLineDash.Dots ) );
226 
227  if( aLineDash.DotLen )
228  {
229  // dashes length
230  if( bIsRel )
231  {
232  ::sax::Converter::convertPercent(aOut, aLineDash.DotLen);
233  }
234  else
235  {
236  rUnitConverter.convertMeasureToXML( aOut,
237  aLineDash.DotLen );
238  }
239  aStrValue = aOut.makeStringAndClear();
241  }
242  }
243 
244  // dashes
245  if( aLineDash.Dashes )
246  {
247  rExport.AddAttribute( XML_NAMESPACE_DRAW, XML_DOTS2, OUString::number( aLineDash.Dashes ) );
248 
249  if( aLineDash.DashLen )
250  {
251  // dashes length
252  if( bIsRel )
253  {
254  ::sax::Converter::convertPercent(aOut, aLineDash.DashLen);
255  }
256  else
257  {
258  rUnitConverter.convertMeasureToXML( aOut,
259  aLineDash.DashLen );
260  }
261  aStrValue = aOut.makeStringAndClear();
263  }
264  }
265 
266  // distance
267  if( bIsRel )
268  {
269  ::sax::Converter::convertPercent( aOut, aLineDash.Distance );
270  }
271  else
272  {
273  rUnitConverter.convertMeasureToXML( aOut,
274  aLineDash.Distance );
275  }
276  aStrValue = aOut.makeStringAndClear();
278 
279  // do Write
282  true, false );
283 }
284 
285 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
static bool convertEnum(EnumT &rEnum, const OUString &rValue, const SvXMLEnumMapEntry< EnumT > *pMap)
convert string to enum using given enum map, if the enum is not found in the map, this method will re...
Definition: xmluconv.hxx:128
SvXMLEnumMapEntry< drawing::DashStyle > const pXML_DashStyle_Enum[]
Definition: DashStyle.cxx:41
the SvXMLTypeConverter converts values of various types from their internal representation to the tex...
Definition: xmluconv.hxx:77
const SvXMLUnitConverter & GetMM100UnitConverter() const
Definition: xmlexp.hxx:394
sal_Int32 toInt32(OUString const &rStr)
FastAttributeList & castToFastAttributeList(const css::uno::Reference< css::xml::sax::XFastAttributeList > &xAttrList)
css::uno::Any const & rValue
Definition: ImageStyle.hxx:38
bool convertMeasureToCore(sal_Int32 &rValue, const OUString &rString, sal_Int32 nMin=SAL_MIN_INT32, sal_Int32 nMax=SAL_MAX_INT32) const
convert string to measure with meCoreMeasureUnit, using optional min and max values ...
Definition: xmluconv.cxx:171
#define XMLOFF_WARN_UNKNOWN(area, rIter)
Definition: xmlictxt.hxx:131
void AddAttribute(sal_uInt16 nPrefix, const char *pName, const OUString &rValue)
Definition: xmlexp.cxx:909
constexpr sal_uInt16 XML_NAMESPACE_DRAW
OUString EncodeStyleName(const OUString &rName, bool *pEncoded=nullptr) const
Definition: xmlexp.cxx:1954
Map an XMLTokenEnum to an enum value.
Definition: ximpshap.hxx:40
Handling of tokens in XML:
#define XML_ELEMENT(prefix, name)
Definition: xmlimp.hxx:94
css::uno::Any const SvXMLExport & rExport
Definition: ImageStyle.hxx:38
void convertMeasureToXML(OUStringBuffer &rBuffer, sal_Int32 nMeasure) const
convert measure to string: from meCoreMeasureUnit to meXMLMeasureUnit
Definition: xmluconv.cxx:181
static bool convertPercent(sal_Int32 &rValue, const OUString &rString)