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
24
25#include <xmloff/DashStyle.hxx>
27#include <xmloff/xmluconv.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
38using namespace ::com::sun::star;
39using 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 : m_rImport(rImp)
54{
55}
56
58 const uno::Reference< xml::sax::XFastAttributeList >& xAttrList,
59 uno::Any& rValue,
60 OUString& rStrName )
61{
62 drawing::LineDash aLineDash;
63 aLineDash.Style = drawing::DashStyle_RECT;
64 aLineDash.Dots = 0;
65 aLineDash.DotLen = 0;
66 aLineDash.Dashes = 0;
67 aLineDash.DashLen = 0;
68 aLineDash.Distance = 20;
69 OUString aDisplayName;
70
71 bool bIsRel = false;
72
73 SvXMLUnitConverter& rUnitConverter = m_rImport.GetMM100UnitConverter();
74
75 for (auto &aIter : sax_fastparser::castToFastAttributeList( xAttrList ))
76 {
77 switch( aIter.getToken() )
78 {
80 case XML_ELEMENT(DRAW_OOO, XML_NAME):
81 {
82 rStrName = aIter.toString();
83 }
84 break;
86 case XML_ELEMENT(DRAW_OOO, XML_DISPLAY_NAME):
87 {
88 aDisplayName = aIter.toString();
89 }
90 break;
92 case XML_ELEMENT(DRAW_OOO, XML_STYLE):
93 {
94 SvXMLUnitConverter::convertEnum( aLineDash.Style, aIter.toView(), pXML_DashStyle_Enum );
95 }
96 break;
98 case XML_ELEMENT(DRAW_OOO, XML_DOTS1):
99 aLineDash.Dots = static_cast<sal_Int16>(aIter.toInt32());
100 break;
101
103 case XML_ELEMENT(DRAW_OOO, XML_DOTS1_LENGTH):
104 {
105 if( aIter.toView().find( '%' ) != std::string_view::npos ) // it's a percentage
106 {
107 bIsRel = true;
108 ::sax::Converter::convertPercent(aLineDash.DotLen, aIter.toView());
109 }
110 else
111 {
112 rUnitConverter.convertMeasureToCore( aLineDash.DotLen,
113 aIter.toView() );
114 }
115 }
116 break;
117
119 case XML_ELEMENT(DRAW_OOO, XML_DOTS2):
120 aLineDash.Dashes = static_cast<sal_Int16>(aIter.toInt32());
121 break;
122
124 case XML_ELEMENT(DRAW_OOO, XML_DOTS2_LENGTH):
125 {
126 if( aIter.toView().find( '%' ) != std::string_view::npos ) // it's a percentage
127 {
128 bIsRel = true;
129 ::sax::Converter::convertPercent(aLineDash.DashLen, aIter.toView());
130 }
131 else
132 {
133 rUnitConverter.convertMeasureToCore( aLineDash.DashLen,
134 aIter.toView() );
135 }
136 }
137 break;
138
140 case XML_ELEMENT(DRAW_OOO, XML_DISTANCE):
141 {
142 if( aIter.toView().find( '%' ) != std::string_view::npos ) // it's a percentage
143 {
144 bIsRel = true;
145 ::sax::Converter::convertPercent(aLineDash.Distance, aIter.toView());
146 }
147 else
148 {
149 rUnitConverter.convertMeasureToCore( aLineDash.Distance,
150 aIter.toView() );
151 }
152 }
153 break;
154 default:
155 XMLOFF_WARN_UNKNOWN("xmloff.style", aIter);
156 }
157 }
158
159 if( bIsRel )
160 aLineDash.Style = aLineDash.Style == drawing::DashStyle_RECT ? drawing::DashStyle_RECTRELATIVE : drawing::DashStyle_ROUNDRELATIVE;
161
162 rValue <<= aLineDash;
163
164 if( !aDisplayName.isEmpty() )
165 {
166 m_rImport.AddStyleDisplayName( XmlStyleFamily::SD_STROKE_DASH_ID,
167 rStrName, aDisplayName );
168 rStrName = aDisplayName;
169 }
170}
171
172// Export
173
175 : m_rExport(rExp)
176{
177}
178
180 const OUString& rStrName,
181 const uno::Any& rValue )
182{
184
185 drawing::LineDash aLineDash;
186
187 if( rStrName.isEmpty() )
188 return;
189
190 if( !(rValue >>= aLineDash) )
191 return;
192
193 bool bIsRel = aLineDash.Style == drawing::DashStyle_RECTRELATIVE || aLineDash.Style == drawing::DashStyle_ROUNDRELATIVE;
194
195 OUString aStrValue;
196 OUStringBuffer aOut;
197
198 // Name
199 bool bEncoded = false;
201 m_rExport.EncodeStyleName( rStrName,
202 &bEncoded ) );
203 if( bEncoded )
205 rStrName );
206
207 // Style
209 aStrValue = aOut.makeStringAndClear();
211
212 // dots
213 if( aLineDash.Dots )
214 {
215 m_rExport.AddAttribute( XML_NAMESPACE_DRAW, XML_DOTS1, OUString::number( aLineDash.Dots ) );
216
217 if( aLineDash.DotLen )
218 {
219 // dashes length
220 if( bIsRel )
221 {
222 ::sax::Converter::convertPercent(aOut, aLineDash.DotLen);
223 }
224 else
225 {
226 rUnitConverter.convertMeasureToXML( aOut,
227 aLineDash.DotLen );
228 }
229 aStrValue = aOut.makeStringAndClear();
231 }
232 }
233
234 // dashes
235 if( aLineDash.Dashes )
236 {
237 m_rExport.AddAttribute( XML_NAMESPACE_DRAW, XML_DOTS2, OUString::number( aLineDash.Dashes ) );
238
239 if( aLineDash.DashLen )
240 {
241 // dashes length
242 if( bIsRel )
243 {
244 ::sax::Converter::convertPercent(aOut, aLineDash.DashLen);
245 }
246 else
247 {
248 rUnitConverter.convertMeasureToXML( aOut,
249 aLineDash.DashLen );
250 }
251 aStrValue = aOut.makeStringAndClear();
253 }
254 }
255
256 // distance
257 if( bIsRel )
258 {
259 ::sax::Converter::convertPercent( aOut, aLineDash.Distance );
260 }
261 else
262 {
263 rUnitConverter.convertMeasureToXML( aOut,
264 aLineDash.Distance );
265 }
266 aStrValue = aOut.makeStringAndClear();
268
269 // do Write
272 true, false );
273}
274
275/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
SvXMLEnumMapEntry< drawing::DashStyle > const pXML_DashStyle_Enum[]
Definition: DashStyle.cxx:41
const SvXMLImport & m_rImport
void AddAttribute(sal_uInt16 nPrefix, const OUString &rName, const OUString &rValue)
Definition: xmlexp.cxx:907
OUString EncodeStyleName(const OUString &rName, bool *pEncoded=nullptr) const
Definition: xmlexp.cxx:1934
const SvXMLUnitConverter & GetMM100UnitConverter() const
Definition: xmlexp.hxx:391
the SvXMLTypeConverter converts values of various types from their internal representation to the tex...
Definition: xmluconv.hxx:83
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
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:188
void convertMeasureToXML(OUStringBuffer &rBuffer, sal_Int32 nMeasure) const
convert measure to string: from meCoreMeasureUnit to meXMLMeasureUnit
Definition: xmluconv.cxx:208
void exportXML(const OUString &rStrName, const css::uno::Any &rValue)
Definition: DashStyle.cxx:179
XMLDashStyleExport(SvXMLExport &rExport)
Definition: DashStyle.cxx:174
SvXMLExport & m_rExport
Definition: DashStyle.hxx:51
void importXML(const css::uno::Reference< css::xml::sax::XFastAttributeList > &xAttrList, css::uno::Any &rValue, OUString &rStrName)
Definition: DashStyle.cxx:57
SvXMLImport & m_rImport
Definition: DashStyle.hxx:37
XMLDashStyleImport(SvXMLImport &rImport)
Definition: DashStyle.cxx:52
static bool convertPercent(sal_Int32 &rValue, std::u16string_view rString)
DRAW
FastAttributeList & castToFastAttributeList(const css::uno::Reference< css::xml::sax::XFastAttributeList > &xAttrList)
Handling of tokens in XML:
#define XMLOFF_WARN_UNKNOWN(area, rIter)
Definition: xmlictxt.hxx:114
#define XML_ELEMENT(prefix, name)
Definition: xmlimp.hxx:97
constexpr sal_uInt16 XML_NAMESPACE_DRAW