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 
53  : rImport(rImp)
54 {
55 }
56 
58 {
59 }
60 
62  const uno::Reference< xml::sax::XFastAttributeList >& xAttrList,
63  uno::Any& rValue,
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 
78 
79  for (auto &aIter : sax_fastparser::castToFastAttributeList( xAttrList ))
80  {
81  switch( aIter.getToken() )
82  {
83  case XML_ELEMENT(DRAW, XML_NAME):
84  case XML_ELEMENT(DRAW_OOO, XML_NAME):
85  {
86  rStrName = aIter.toString();
87  }
88  break;
90  case XML_ELEMENT(DRAW_OOO, XML_DISPLAY_NAME):
91  {
92  aDisplayName = aIter.toString();
93  }
94  break;
95  case XML_ELEMENT(DRAW, XML_STYLE):
96  case XML_ELEMENT(DRAW_OOO, XML_STYLE):
97  {
98  SvXMLUnitConverter::convertEnum( aLineDash.Style, aIter.toView(), pXML_DashStyle_Enum );
99  }
100  break;
101  case XML_ELEMENT(DRAW, XML_DOTS1):
102  case XML_ELEMENT(DRAW_OOO, XML_DOTS1):
103  aLineDash.Dots = static_cast<sal_Int16>(aIter.toInt32());
104  break;
105 
107  case XML_ELEMENT(DRAW_OOO, XML_DOTS1_LENGTH):
108  {
109  if( aIter.toView().find( '%' ) != std::string_view::npos ) // it's a percentage
110  {
111  bIsRel = true;
112  ::sax::Converter::convertPercent(aLineDash.DotLen, aIter.toView());
113  }
114  else
115  {
116  rUnitConverter.convertMeasureToCore( aLineDash.DotLen,
117  aIter.toView() );
118  }
119  }
120  break;
121 
122  case XML_ELEMENT(DRAW, XML_DOTS2):
123  case XML_ELEMENT(DRAW_OOO, XML_DOTS2):
124  aLineDash.Dashes = static_cast<sal_Int16>(aIter.toInt32());
125  break;
126 
128  case XML_ELEMENT(DRAW_OOO, XML_DOTS2_LENGTH):
129  {
130  if( aIter.toView().find( '%' ) != std::string_view::npos ) // it's a percentage
131  {
132  bIsRel = true;
133  ::sax::Converter::convertPercent(aLineDash.DashLen, aIter.toView());
134  }
135  else
136  {
137  rUnitConverter.convertMeasureToCore( aLineDash.DashLen,
138  aIter.toView() );
139  }
140  }
141  break;
142 
144  case XML_ELEMENT(DRAW_OOO, XML_DISTANCE):
145  {
146  if( aIter.toView().find( '%' ) != std::string_view::npos ) // it's a percentage
147  {
148  bIsRel = true;
149  ::sax::Converter::convertPercent(aLineDash.Distance, aIter.toView());
150  }
151  else
152  {
153  rUnitConverter.convertMeasureToCore( aLineDash.Distance,
154  aIter.toView() );
155  }
156  }
157  break;
158  default:
159  XMLOFF_WARN_UNKNOWN("xmloff.style", aIter);
160  }
161  }
162 
163  if( bIsRel )
164  aLineDash.Style = aLineDash.Style == drawing::DashStyle_RECT ? drawing::DashStyle_RECTRELATIVE : drawing::DashStyle_ROUNDRELATIVE;
165 
166  rValue <<= aLineDash;
167 
168  if( !aDisplayName.isEmpty() )
169  {
171  rStrName, aDisplayName );
172  rStrName = aDisplayName;
173  }
174 }
175 
176 // Export
177 
179  : rExport(rExp)
180 {
181 }
182 
184 {
185 }
186 
188  const OUString& rStrName,
189  const uno::Any& rValue )
190 {
191  SvXMLUnitConverter & rUnitConverter = rExport.GetMM100UnitConverter();
192 
193  drawing::LineDash aLineDash;
194 
195  if( rStrName.isEmpty() )
196  return;
197 
198  if( !(rValue >>= aLineDash) )
199  return;
200 
201  bool bIsRel = aLineDash.Style == drawing::DashStyle_RECTRELATIVE || aLineDash.Style == drawing::DashStyle_ROUNDRELATIVE;
202 
203  OUString aStrValue;
204  OUStringBuffer aOut;
205 
206  // Name
207  bool bEncoded = false;
209  rExport.EncodeStyleName( rStrName,
210  &bEncoded ) );
211  if( bEncoded )
213  rStrName );
214 
215  // Style
216  SvXMLUnitConverter::convertEnum( aOut, aLineDash.Style, pXML_DashStyle_Enum );
217  aStrValue = aOut.makeStringAndClear();
219 
220  // dots
221  if( aLineDash.Dots )
222  {
223  rExport.AddAttribute( XML_NAMESPACE_DRAW, XML_DOTS1, OUString::number( aLineDash.Dots ) );
224 
225  if( aLineDash.DotLen )
226  {
227  // dashes length
228  if( bIsRel )
229  {
230  ::sax::Converter::convertPercent(aOut, aLineDash.DotLen);
231  }
232  else
233  {
234  rUnitConverter.convertMeasureToXML( aOut,
235  aLineDash.DotLen );
236  }
237  aStrValue = aOut.makeStringAndClear();
239  }
240  }
241 
242  // dashes
243  if( aLineDash.Dashes )
244  {
245  rExport.AddAttribute( XML_NAMESPACE_DRAW, XML_DOTS2, OUString::number( aLineDash.Dashes ) );
246 
247  if( aLineDash.DashLen )
248  {
249  // dashes length
250  if( bIsRel )
251  {
252  ::sax::Converter::convertPercent(aOut, aLineDash.DashLen);
253  }
254  else
255  {
256  rUnitConverter.convertMeasureToXML( aOut,
257  aLineDash.DashLen );
258  }
259  aStrValue = aOut.makeStringAndClear();
261  }
262  }
263 
264  // distance
265  if( bIsRel )
266  {
267  ::sax::Converter::convertPercent( aOut, aLineDash.Distance );
268  }
269  else
270  {
271  rUnitConverter.convertMeasureToXML( aOut,
272  aLineDash.Distance );
273  }
274  aStrValue = aOut.makeStringAndClear();
276 
277  // do Write
280  true, false );
281 }
282 
283 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
bool convertMeasureToCore(sal_Int32 &rValue, std::u16string_view 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:186
void exportXML(const OUString &rStrName, const css::uno::Any &rValue)
Definition: DashStyle.cxx:187
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:82
const SvXMLUnitConverter & GetMM100UnitConverter() const
Definition: xmlexp.hxx:400
FastAttributeList & castToFastAttributeList(const css::uno::Reference< css::xml::sax::XFastAttributeList > &xAttrList)
void importXML(const css::uno::Reference< css::xml::sax::XFastAttributeList > &xAttrList, css::uno::Any &rValue, OUString &rStrName)
Definition: DashStyle.cxx:61
SvXMLExport & rExport
Definition: DashStyle.hxx:53
DRAW
#define XMLOFF_WARN_UNKNOWN(area, rIter)
Definition: xmlictxt.hxx:113
void AddAttribute(sal_uInt16 nPrefix, const char *pName, const OUString &rValue)
Definition: xmlexp.cxx:938
constexpr sal_uInt16 XML_NAMESPACE_DRAW
static bool convertPercent(sal_Int32 &rValue, std::u16string_view rString)
OUString EncodeStyleName(const OUString &rName, bool *pEncoded=nullptr) const
Definition: xmlexp.cxx:1981
Map an XMLTokenEnum to an enum value.
Definition: ximpshap.hxx:39
XMLDashStyleImport(SvXMLImport &rImport)
Definition: DashStyle.cxx:52
Handling of tokens in XML:
#define XML_ELEMENT(prefix, name)
Definition: xmlimp.hxx:97
XMLDashStyleExport(SvXMLExport &rExport)
Definition: DashStyle.cxx:178
static bool convertEnum(EnumT &rEnum, std::u16string_view 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:145
const SvXMLUnitConverter & GetMM100UnitConverter() const
Definition: xmlimp.hxx:400
void AddStyleDisplayName(XmlStyleFamily nFamily, const OUString &rName, const OUString &rDisplayName)
Definition: xmlimp.cxx:1375
SvXMLImport & rImport
Definition: DashStyle.hxx:38
void convertMeasureToXML(OUStringBuffer &rBuffer, sal_Int32 nMeasure) const
convert measure to string: from meCoreMeasureUnit to meXMLMeasureUnit
Definition: xmluconv.cxx:206