LibreOffice Module xmloff (master)  1
HatchStyle.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 <xmloff/HatchStyle.hxx>
21 
22 #include <com/sun/star/drawing/Hatch.hpp>
23 
24 #include <sax/tools/converter.hxx>
25 
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 <rtl/ustrbuf.hxx>
33 #include <rtl/ustring.hxx>
34 #include <sal/log.hxx>
35 #include <xmloff/xmltkmap.hxx>
36 #include <xmloff/xmlement.hxx>
37 
38 using namespace ::com::sun::star;
39 
40 using namespace ::xmloff::token;
41 
43 {
44  { XML_SINGLE, drawing::HatchStyle_SINGLE },
45  { XML_DOUBLE, drawing::HatchStyle_DOUBLE },
46  { XML_HATCHSTYLE_TRIPLE, drawing::HatchStyle_TRIPLE },
47  { XML_TOKEN_INVALID, drawing::HatchStyle(0) }
48 };
49 
50 // Import
51 
52 XMLHatchStyleImport::XMLHatchStyleImport( SvXMLImport& rImp )
53  : rImport(rImp)
54 {
55 }
56 
57 XMLHatchStyleImport::~XMLHatchStyleImport()
58 {
59 }
60 
61 void XMLHatchStyleImport::importXML(
62  const uno::Reference< xml::sax::XFastAttributeList >& xAttrList,
64  OUString& rStrName )
65 {
66  OUString aDisplayName;
67 
68  drawing::Hatch aHatch;
69  aHatch.Style = drawing::HatchStyle_SINGLE;
70  aHatch.Color = 0;
71  aHatch.Distance = 0;
72  aHatch.Angle = 0;
73 
74  SvXMLUnitConverter& rUnitConverter = rImport.GetMM100UnitConverter();
75 
76  for (auto &aIter : sax_fastparser::castToFastAttributeList( xAttrList ))
77  {
78  const OUString aStrValue = aIter.toString();
79  switch( aIter.getToken() )
80  {
81  case XML_ELEMENT(DRAW, XML_NAME):
82  case XML_ELEMENT(DRAW_OOO, XML_NAME):
83  rStrName = aStrValue;
84  break;
85  case XML_ELEMENT(DRAW, XML_DISPLAY_NAME):
86  case XML_ELEMENT(DRAW_OOO, XML_DISPLAY_NAME):
87  aDisplayName = aStrValue;
88  break;
89  case XML_ELEMENT(DRAW, XML_STYLE):
90  case XML_ELEMENT(DRAW_OOO, XML_STYLE):
91  SvXMLUnitConverter::convertEnum( aHatch.Style, aStrValue, pXML_HatchStyle_Enum );
92  break;
93  case XML_ELEMENT(DRAW, XML_COLOR):
94  case XML_ELEMENT(DRAW_OOO, XML_COLOR):
95  ::sax::Converter::convertColor(aHatch.Color, aStrValue);
96  break;
97  case XML_ELEMENT(DRAW, XML_DISTANCE):
98  case XML_ELEMENT(DRAW_OOO, XML_DISTANCE):
99  rUnitConverter.convertMeasureToCore(aHatch.Distance, aStrValue);
100  break;
101  case XML_ELEMENT(DRAW, XML_ROTATION):
102  case XML_ELEMENT(DRAW_OOO, XML_ROTATION):
103  {
104  sal_Int32 nValue;
105  if (::sax::Converter::convertNumber(nValue, aStrValue, 0, 3600))
106  aHatch.Angle = sal_Int16(nValue);
107  break;
108  }
109  default:
110  XMLOFF_WARN_UNKNOWN("xmloff.style", aIter);
111  }
112  }
113 
114  rValue <<= aHatch;
115 
116  if( !aDisplayName.isEmpty() )
117  {
118  rImport.AddStyleDisplayName( XmlStyleFamily::SD_HATCH_ID, rStrName,
119  aDisplayName );
120  rStrName = aDisplayName;
121  }
122 }
123 
124 // Export
125 
126 XMLHatchStyleExport::XMLHatchStyleExport( SvXMLExport& rExp )
127  : rExport(rExp)
128 {
129 }
130 
131 XMLHatchStyleExport::~XMLHatchStyleExport()
132 {
133 }
134 
135 void XMLHatchStyleExport::exportXML(
136  const OUString& rStrName,
137  const uno::Any& rValue )
138 {
139  drawing::Hatch aHatch;
140 
141  if( rStrName.isEmpty() )
142  return;
143 
144  if( !(rValue >>= aHatch) )
145  return;
146 
147  OUString aStrValue;
148  OUStringBuffer aOut;
149 
150  SvXMLUnitConverter& rUnitConverter =
152 
153  // Style
154  if( !SvXMLUnitConverter::convertEnum( aOut, aHatch.Style, pXML_HatchStyle_Enum ) )
155  return;
156 
157  // Name
158  bool bEncoded = false;
160  rExport.EncodeStyleName( rStrName,
161  &bEncoded ) );
162  if( bEncoded )
164  rStrName );
165 
166  aStrValue = aOut.makeStringAndClear();
168 
169  // Color
170  ::sax::Converter::convertColor(aOut, aHatch.Color);
171  aStrValue = aOut.makeStringAndClear();
173 
174  // Distance
175  rUnitConverter.convertMeasureToXML( aOut, aHatch.Distance );
176  aStrValue = aOut.makeStringAndClear();
178 
179  // Angle
180  rExport.AddAttribute( XML_NAMESPACE_DRAW, XML_ROTATION, OUString::number(aHatch.Angle) );
181 
182  // Do Write
184  true, false );
185 }
186 
187 /* 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
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
FastAttributeList & castToFastAttributeList(const css::uno::Reference< css::xml::sax::XFastAttributeList > &xAttrList)
css::uno::Any const & rValue
Definition: ImageStyle.hxx:38
#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
SvXMLEnumMapEntry< drawing::HatchStyle > const pXML_HatchStyle_Enum[]
Definition: HatchStyle.cxx:42
Map an XMLTokenEnum to an enum value.
Definition: ximpshap.hxx:40
static bool convertColor(sal_Int32 &rColor, const OUString &rValue)
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
sal_Int16 nValue
static bool convertNumber(sal_Int32 &rValue, std::u16string_view aString, sal_Int32 nMin=SAL_MIN_INT32, sal_Int32 nMax=SAL_MAX_INT32)