/src/libreoffice/xmloff/source/style/DashStyle.cxx
Line | Count | Source |
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/xmluconv.hxx> |
27 | | #include <xmloff/xmlnamespace.hxx> |
28 | | #include <xmloff/xmltoken.hxx> |
29 | | #include <xmloff/xmlexp.hxx> |
30 | | #include <xmloff/xmlimp.hxx> |
31 | | #include <xmloff/xmlement.hxx> |
32 | | #include <rtl/ustrbuf.hxx> |
33 | | #include <rtl/ustring.hxx> |
34 | | #include <sal/log.hxx> |
35 | | |
36 | | using namespace ::com::sun::star; |
37 | | using namespace ::xmloff::token; |
38 | | |
39 | | SvXMLEnumMapEntry<drawing::DashStyle> const pXML_DashStyle_Enum[] = |
40 | | { |
41 | | { XML_RECT, drawing::DashStyle_RECT }, |
42 | | { XML_ROUND, drawing::DashStyle_ROUND }, |
43 | | { XML_RECT, drawing::DashStyle_RECTRELATIVE }, |
44 | | { XML_ROUND, drawing::DashStyle_ROUNDRELATIVE }, |
45 | | { XML_TOKEN_INVALID, drawing::DashStyle(0) } |
46 | | }; |
47 | | |
48 | | // Import |
49 | | |
50 | | XMLDashStyleImport::XMLDashStyleImport( SvXMLImport& rImp ) |
51 | 235 | : m_rImport(rImp) |
52 | 235 | { |
53 | 235 | } |
54 | | |
55 | | void XMLDashStyleImport::importXML( |
56 | | const uno::Reference< xml::sax::XFastAttributeList >& xAttrList, |
57 | | uno::Any& rValue, |
58 | | OUString& rStrName ) |
59 | 235 | { |
60 | 235 | drawing::LineDash aLineDash; |
61 | 235 | aLineDash.Style = drawing::DashStyle_RECT; |
62 | 235 | aLineDash.Dots = 0; |
63 | 235 | aLineDash.DotLen = 0; |
64 | 235 | aLineDash.Dashes = 0; |
65 | 235 | aLineDash.DashLen = 0; |
66 | 235 | aLineDash.Distance = 20; |
67 | 235 | OUString aDisplayName; |
68 | | |
69 | 235 | bool bIsRel = false; |
70 | | |
71 | 235 | SvXMLUnitConverter& rUnitConverter = m_rImport.GetMM100UnitConverter(); |
72 | | |
73 | 235 | for (auto &aIter : sax_fastparser::castToFastAttributeList( xAttrList )) |
74 | 1.68k | { |
75 | 1.68k | switch( aIter.getToken() ) |
76 | 1.68k | { |
77 | 231 | case XML_ELEMENT(DRAW, XML_NAME): |
78 | 231 | case XML_ELEMENT(DRAW_OOO, XML_NAME): |
79 | 231 | { |
80 | 231 | rStrName = aIter.toString(); |
81 | 231 | } |
82 | 231 | break; |
83 | 221 | case XML_ELEMENT(DRAW, XML_DISPLAY_NAME): |
84 | 221 | case XML_ELEMENT(DRAW_OOO, XML_DISPLAY_NAME): |
85 | 221 | { |
86 | 221 | aDisplayName = aIter.toString(); |
87 | 221 | } |
88 | 221 | break; |
89 | 230 | case XML_ELEMENT(DRAW, XML_STYLE): |
90 | 230 | case XML_ELEMENT(DRAW_OOO, XML_STYLE): |
91 | 230 | { |
92 | 230 | SvXMLUnitConverter::convertEnum( aLineDash.Style, aIter.toView(), pXML_DashStyle_Enum ); |
93 | 230 | } |
94 | 230 | break; |
95 | 192 | case XML_ELEMENT(DRAW, XML_DOTS1): |
96 | 192 | case XML_ELEMENT(DRAW_OOO, XML_DOTS1): |
97 | 192 | aLineDash.Dots = static_cast<sal_Int16>(aIter.toInt32()); |
98 | 192 | break; |
99 | | |
100 | 188 | case XML_ELEMENT(DRAW, XML_DOTS1_LENGTH): |
101 | 188 | case XML_ELEMENT(DRAW_OOO, XML_DOTS1_LENGTH): |
102 | 188 | { |
103 | 188 | if( aIter.toView().find( '%' ) != std::string_view::npos ) // it's a percentage |
104 | 1 | { |
105 | 1 | bIsRel = true; |
106 | 1 | ::sax::Converter::convertPercent(aLineDash.DotLen, aIter.toView()); |
107 | 1 | } |
108 | 187 | else |
109 | 187 | { |
110 | 187 | rUnitConverter.convertMeasureToCore( aLineDash.DotLen, |
111 | 187 | aIter.toView() ); |
112 | 187 | } |
113 | 188 | } |
114 | 188 | break; |
115 | | |
116 | 201 | case XML_ELEMENT(DRAW, XML_DOTS2): |
117 | 201 | case XML_ELEMENT(DRAW_OOO, XML_DOTS2): |
118 | 201 | aLineDash.Dashes = static_cast<sal_Int16>(aIter.toInt32()); |
119 | 201 | break; |
120 | | |
121 | 184 | case XML_ELEMENT(DRAW, XML_DOTS2_LENGTH): |
122 | 184 | case XML_ELEMENT(DRAW_OOO, XML_DOTS2_LENGTH): |
123 | 184 | { |
124 | 184 | if( aIter.toView().find( '%' ) != std::string_view::npos ) // it's a percentage |
125 | 0 | { |
126 | 0 | bIsRel = true; |
127 | 0 | ::sax::Converter::convertPercent(aLineDash.DashLen, aIter.toView()); |
128 | 0 | } |
129 | 184 | else |
130 | 184 | { |
131 | 184 | rUnitConverter.convertMeasureToCore( aLineDash.DashLen, |
132 | 184 | aIter.toView() ); |
133 | 184 | } |
134 | 184 | } |
135 | 184 | break; |
136 | | |
137 | 220 | case XML_ELEMENT(DRAW, XML_DISTANCE): |
138 | 220 | case XML_ELEMENT(DRAW_OOO, XML_DISTANCE): |
139 | 220 | { |
140 | 220 | if( aIter.toView().find( '%' ) != std::string_view::npos ) // it's a percentage |
141 | 0 | { |
142 | 0 | bIsRel = true; |
143 | 0 | ::sax::Converter::convertPercent(aLineDash.Distance, aIter.toView()); |
144 | 0 | } |
145 | 220 | else |
146 | 220 | { |
147 | 220 | rUnitConverter.convertMeasureToCore( aLineDash.Distance, |
148 | 220 | aIter.toView() ); |
149 | 220 | } |
150 | 220 | } |
151 | 220 | break; |
152 | 18 | default: |
153 | 18 | XMLOFF_WARN_UNKNOWN("xmloff.style", aIter); |
154 | 1.68k | } |
155 | 1.68k | } |
156 | | |
157 | 235 | if( bIsRel ) |
158 | 1 | aLineDash.Style = aLineDash.Style == drawing::DashStyle_RECT ? drawing::DashStyle_RECTRELATIVE : drawing::DashStyle_ROUNDRELATIVE; |
159 | | |
160 | 235 | rValue <<= aLineDash; |
161 | | |
162 | 235 | if( !aDisplayName.isEmpty() ) |
163 | 221 | { |
164 | 221 | m_rImport.AddStyleDisplayName( XmlStyleFamily::SD_STROKE_DASH_ID, |
165 | 221 | rStrName, aDisplayName ); |
166 | 221 | rStrName = aDisplayName; |
167 | 221 | } |
168 | 235 | } |
169 | | |
170 | | // Export |
171 | | |
172 | | XMLDashStyleExport::XMLDashStyleExport( SvXMLExport& rExp ) |
173 | 0 | : m_rExport(rExp) |
174 | 0 | { |
175 | 0 | } |
176 | | |
177 | | void XMLDashStyleExport::exportXML( |
178 | | const OUString& rStrName, |
179 | | const uno::Any& rValue ) |
180 | 0 | { |
181 | 0 | SvXMLUnitConverter & rUnitConverter = m_rExport.GetMM100UnitConverter(); |
182 | |
|
183 | 0 | drawing::LineDash aLineDash; |
184 | |
|
185 | 0 | if( rStrName.isEmpty() ) |
186 | 0 | return; |
187 | | |
188 | 0 | if( !(rValue >>= aLineDash) ) |
189 | 0 | return; |
190 | | |
191 | 0 | bool bIsRel = aLineDash.Style == drawing::DashStyle_RECTRELATIVE || aLineDash.Style == drawing::DashStyle_ROUNDRELATIVE; |
192 | |
|
193 | 0 | OUString aStrValue; |
194 | 0 | OUStringBuffer aOut; |
195 | | |
196 | | // Name |
197 | 0 | bool bEncoded = false; |
198 | 0 | m_rExport.AddAttribute( XML_NAMESPACE_DRAW, XML_NAME, |
199 | 0 | m_rExport.EncodeStyleName( rStrName, |
200 | 0 | &bEncoded ) ); |
201 | 0 | if( bEncoded ) |
202 | 0 | m_rExport.AddAttribute( XML_NAMESPACE_DRAW, XML_DISPLAY_NAME, |
203 | 0 | rStrName ); |
204 | | |
205 | | // Style |
206 | 0 | SvXMLUnitConverter::convertEnum( aOut, aLineDash.Style, pXML_DashStyle_Enum ); |
207 | 0 | aStrValue = aOut.makeStringAndClear(); |
208 | 0 | m_rExport.AddAttribute( XML_NAMESPACE_DRAW, XML_STYLE, aStrValue ); |
209 | | |
210 | | // dots |
211 | 0 | if( aLineDash.Dots ) |
212 | 0 | { |
213 | 0 | m_rExport.AddAttribute( XML_NAMESPACE_DRAW, XML_DOTS1, OUString::number( aLineDash.Dots ) ); |
214 | |
|
215 | 0 | if( aLineDash.DotLen ) |
216 | 0 | { |
217 | | // dashes length |
218 | 0 | if( bIsRel ) |
219 | 0 | { |
220 | 0 | ::sax::Converter::convertPercent(aOut, aLineDash.DotLen); |
221 | 0 | } |
222 | 0 | else |
223 | 0 | { |
224 | 0 | rUnitConverter.convertMeasureToXML( aOut, |
225 | 0 | aLineDash.DotLen ); |
226 | 0 | } |
227 | 0 | aStrValue = aOut.makeStringAndClear(); |
228 | 0 | m_rExport.AddAttribute( XML_NAMESPACE_DRAW, XML_DOTS1_LENGTH, aStrValue ); |
229 | 0 | } |
230 | 0 | } |
231 | | |
232 | | // dashes |
233 | 0 | if( aLineDash.Dashes ) |
234 | 0 | { |
235 | 0 | m_rExport.AddAttribute( XML_NAMESPACE_DRAW, XML_DOTS2, OUString::number( aLineDash.Dashes ) ); |
236 | |
|
237 | 0 | if( aLineDash.DashLen ) |
238 | 0 | { |
239 | | // dashes length |
240 | 0 | if( bIsRel ) |
241 | 0 | { |
242 | 0 | ::sax::Converter::convertPercent(aOut, aLineDash.DashLen); |
243 | 0 | } |
244 | 0 | else |
245 | 0 | { |
246 | 0 | rUnitConverter.convertMeasureToXML( aOut, |
247 | 0 | aLineDash.DashLen ); |
248 | 0 | } |
249 | 0 | aStrValue = aOut.makeStringAndClear(); |
250 | 0 | m_rExport.AddAttribute( XML_NAMESPACE_DRAW, XML_DOTS2_LENGTH, aStrValue ); |
251 | 0 | } |
252 | 0 | } |
253 | | |
254 | | // distance |
255 | 0 | if( bIsRel ) |
256 | 0 | { |
257 | 0 | ::sax::Converter::convertPercent( aOut, aLineDash.Distance ); |
258 | 0 | } |
259 | 0 | else |
260 | 0 | { |
261 | 0 | rUnitConverter.convertMeasureToXML( aOut, |
262 | 0 | aLineDash.Distance ); |
263 | 0 | } |
264 | 0 | aStrValue = aOut.makeStringAndClear(); |
265 | 0 | m_rExport.AddAttribute( XML_NAMESPACE_DRAW, XML_DISTANCE, aStrValue ); |
266 | | |
267 | | // do Write |
268 | 0 | SvXMLElementExport rElem( m_rExport, |
269 | 0 | XML_NAMESPACE_DRAW, XML_STROKE_DASH, |
270 | 0 | true, false ); |
271 | 0 | } |
272 | | |
273 | | /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |