/src/libreoffice/xmloff/source/style/HatchStyle.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 <xmloff/HatchStyle.hxx> |
21 | | |
22 | | #include <com/sun/star/drawing/Hatch.hpp> |
23 | | |
24 | | #include <sax/tools/converter.hxx> |
25 | | |
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 <rtl/ustrbuf.hxx> |
32 | | #include <rtl/ustring.hxx> |
33 | | #include <sal/log.hxx> |
34 | | #include <xmloff/xmlement.hxx> |
35 | | |
36 | | using namespace ::com::sun::star; |
37 | | |
38 | | using namespace ::xmloff::token; |
39 | | |
40 | | SvXMLEnumMapEntry<drawing::HatchStyle> const pXML_HatchStyle_Enum[] = |
41 | | { |
42 | | { XML_SINGLE, drawing::HatchStyle_SINGLE }, |
43 | | { XML_DOUBLE, drawing::HatchStyle_DOUBLE }, |
44 | | { XML_HATCHSTYLE_TRIPLE, drawing::HatchStyle_TRIPLE }, |
45 | | { XML_TOKEN_INVALID, drawing::HatchStyle(0) } |
46 | | }; |
47 | | |
48 | | // Import |
49 | | |
50 | | XMLHatchStyleImport::XMLHatchStyleImport( SvXMLImport& rImp ) |
51 | 0 | : m_rImport(rImp) |
52 | 0 | { |
53 | 0 | } |
54 | | |
55 | | void XMLHatchStyleImport::importXML( |
56 | | const uno::Reference< xml::sax::XFastAttributeList >& xAttrList, |
57 | | uno::Any& rValue, |
58 | | OUString& rStrName ) |
59 | 0 | { |
60 | 0 | OUString aDisplayName; |
61 | |
|
62 | 0 | drawing::Hatch aHatch; |
63 | 0 | aHatch.Style = drawing::HatchStyle_SINGLE; |
64 | 0 | aHatch.Color = 0; |
65 | 0 | aHatch.Distance = 0; |
66 | 0 | aHatch.Angle = 0; |
67 | |
|
68 | 0 | SvXMLUnitConverter& rUnitConverter = m_rImport.GetMM100UnitConverter(); |
69 | |
|
70 | 0 | for (auto &aIter : sax_fastparser::castToFastAttributeList( xAttrList )) |
71 | 0 | { |
72 | 0 | switch( aIter.getToken() ) |
73 | 0 | { |
74 | 0 | case XML_ELEMENT(DRAW, XML_NAME): |
75 | 0 | case XML_ELEMENT(DRAW_OOO, XML_NAME): |
76 | 0 | rStrName = aIter.toString(); |
77 | 0 | break; |
78 | 0 | case XML_ELEMENT(DRAW, XML_DISPLAY_NAME): |
79 | 0 | case XML_ELEMENT(DRAW_OOO, XML_DISPLAY_NAME): |
80 | 0 | aDisplayName = aIter.toString(); |
81 | 0 | break; |
82 | 0 | case XML_ELEMENT(DRAW, XML_STYLE): |
83 | 0 | case XML_ELEMENT(DRAW_OOO, XML_STYLE): |
84 | 0 | SvXMLUnitConverter::convertEnum( aHatch.Style, aIter.toView(), pXML_HatchStyle_Enum ); |
85 | 0 | break; |
86 | 0 | case XML_ELEMENT(DRAW, XML_COLOR): |
87 | 0 | case XML_ELEMENT(DRAW_OOO, XML_COLOR): |
88 | 0 | ::sax::Converter::convertColor(aHatch.Color, aIter.toView()); |
89 | 0 | break; |
90 | 0 | case XML_ELEMENT(DRAW, XML_DISTANCE): |
91 | 0 | case XML_ELEMENT(DRAW_OOO, XML_DISTANCE): |
92 | 0 | rUnitConverter.convertMeasureToCore(aHatch.Distance, aIter.toView()); |
93 | 0 | break; |
94 | 0 | case XML_ELEMENT(DRAW, XML_ROTATION): |
95 | 0 | case XML_ELEMENT(DRAW_OOO, XML_ROTATION): |
96 | 0 | { |
97 | | // tdf#161327. We keep reading unitless values as being in 1/10th of a degree for |
98 | | // backward compatibility for now. Values with unit are imported correctly. |
99 | | // For how to make it version-dependent see import of XML_GRADIENT_ANGLE, for example. |
100 | 0 | sal_Int16 nAngle; |
101 | 0 | bool const bRet |
102 | 0 | = ::sax::Converter::convert10thDegAngle(nAngle, aIter.toView(), true); |
103 | 0 | if (bRet) |
104 | 0 | { // limit to valid range [0..3600[ |
105 | 0 | nAngle = nAngle % 3600; |
106 | 0 | if (nAngle < 0) |
107 | 0 | nAngle += 3600; |
108 | 0 | aHatch.Angle = nAngle; |
109 | 0 | } |
110 | 0 | break; |
111 | 0 | } |
112 | 0 | default: |
113 | 0 | XMLOFF_WARN_UNKNOWN("xmloff.style", aIter); |
114 | 0 | } |
115 | 0 | } |
116 | | |
117 | 0 | rValue <<= aHatch; |
118 | |
|
119 | 0 | if( !aDisplayName.isEmpty() ) |
120 | 0 | { |
121 | 0 | m_rImport.AddStyleDisplayName( XmlStyleFamily::SD_HATCH_ID, rStrName, |
122 | 0 | aDisplayName ); |
123 | 0 | rStrName = aDisplayName; |
124 | 0 | } |
125 | 0 | } |
126 | | |
127 | | // Export |
128 | | |
129 | | XMLHatchStyleExport::XMLHatchStyleExport( SvXMLExport& rExp ) |
130 | 0 | : m_rExport(rExp) |
131 | 0 | { |
132 | 0 | } |
133 | | |
134 | | void XMLHatchStyleExport::exportXML( |
135 | | const OUString& rStrName, |
136 | | const uno::Any& rValue ) |
137 | 0 | { |
138 | 0 | drawing::Hatch aHatch; |
139 | |
|
140 | 0 | if( rStrName.isEmpty() ) |
141 | 0 | return; |
142 | | |
143 | 0 | if( !(rValue >>= aHatch) ) |
144 | 0 | return; |
145 | | |
146 | 0 | OUString aStrValue; |
147 | 0 | OUStringBuffer aOut; |
148 | |
|
149 | 0 | SvXMLUnitConverter& rUnitConverter = |
150 | 0 | m_rExport.GetMM100UnitConverter(); |
151 | | |
152 | | // Style |
153 | 0 | if( !SvXMLUnitConverter::convertEnum( aOut, aHatch.Style, pXML_HatchStyle_Enum ) ) |
154 | 0 | return; |
155 | | |
156 | | // Name |
157 | 0 | bool bEncoded = false; |
158 | 0 | m_rExport.AddAttribute( XML_NAMESPACE_DRAW, XML_NAME, |
159 | 0 | m_rExport.EncodeStyleName( rStrName, |
160 | 0 | &bEncoded ) ); |
161 | 0 | if( bEncoded ) |
162 | 0 | m_rExport.AddAttribute( XML_NAMESPACE_DRAW, XML_DISPLAY_NAME, |
163 | 0 | rStrName ); |
164 | |
|
165 | 0 | aStrValue = aOut.makeStringAndClear(); |
166 | 0 | m_rExport.AddAttribute( XML_NAMESPACE_DRAW, XML_STYLE, aStrValue ); |
167 | | |
168 | | // Color |
169 | 0 | ::sax::Converter::convertColor(aOut, aHatch.Color); |
170 | 0 | aStrValue = aOut.makeStringAndClear(); |
171 | 0 | m_rExport.AddAttribute( XML_NAMESPACE_DRAW, XML_COLOR, aStrValue ); |
172 | | |
173 | | // Distance |
174 | 0 | rUnitConverter.convertMeasureToXML( aOut, aHatch.Distance ); |
175 | 0 | aStrValue = aOut.makeStringAndClear(); |
176 | 0 | m_rExport.AddAttribute( XML_NAMESPACE_DRAW, XML_DISTANCE, aStrValue ); |
177 | | |
178 | | // Angle |
179 | | // tdf#161327. Start writing unit deg, when most users have a LO version, that can read angle |
180 | | // units. Write 1/10 of a degree for all versions for backward compatibility till then. |
181 | | // Adapt test when LO writes a new default ODF version. |
182 | 0 | SAL_WARN_IF( |
183 | 0 | SvtSaveOptions::ODFSaneDefaultVersion::ODFSVER_013_EXTENDED |
184 | 0 | < m_rExport.getSaneDefaultVersion(), |
185 | 0 | "xmloff.style", |
186 | 0 | "Check whether parameter isWrongOOo10thDegAngle can be false for newer LO version."); |
187 | 0 | ::sax::Converter::convert10thDegAngle(aOut, aHatch.Angle, true); |
188 | 0 | aStrValue = aOut.makeStringAndClear(); |
189 | 0 | m_rExport.AddAttribute(XML_NAMESPACE_DRAW, XML_ROTATION, aStrValue); |
190 | | |
191 | | // Do Write |
192 | 0 | SvXMLElementExport rElem( m_rExport, XML_NAMESPACE_DRAW, XML_HATCH, |
193 | 0 | true, false ); |
194 | 0 | } |
195 | | |
196 | | /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |