LibreOffice Module xmloff (master) 1
shadwhdl.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 "shadwhdl.hxx"
21#include <com/sun/star/uno/Any.hxx>
22#include <rtl/ustrbuf.hxx>
23
24
25#include <com/sun/star/table/ShadowFormat.hpp>
26#include <o3tl/safeint.hxx>
27#include <tools/color.hxx>
29#include <xmloff/xmluconv.hxx>
30#include <xmloff/xmltoken.hxx>
31
32using namespace ::com::sun::star;
33using namespace ::xmloff::token;
34
35
36
37
39{
40 // nothing to do
41}
42
43bool XMLShadowPropHdl::importXML( const OUString& rStrImpValue, uno::Any& rValue, const SvXMLUnitConverter& rUnitConverter ) const
44{
45 bool bRet = false;
46 table::ShadowFormat aShadow;
47 aShadow.Location = table::ShadowLocation_BOTTOM_RIGHT;
48
49 bool bColorFound = false;
50 bool bOffsetFound = false;
51 SvXMLTokenEnumerator aTokenEnum( rStrImpValue );
52 Color aColor( 128,128, 128 );
53 std::u16string_view aToken;
54
55 while( aTokenEnum.getNextToken( aToken ) )
56 {
57 if( IsXMLToken( aToken, XML_NONE ) )
58 {
59 aShadow.Location = table::ShadowLocation_NONE;
60 bRet = true;
61 break;
62 }
63 else if( !bColorFound && aToken.substr(0,1) == u"#" )
64 {
65 bRet = ::sax::Converter::convertColor( aColor, aToken );
66 if( !bRet )
67 return false;
68 bColorFound = true;
69 }
70 else if( !bOffsetFound )
71 {
72 sal_Int32 nX = 0, nY = 0;
73
74 bRet = rUnitConverter.convertMeasureToCore( nX, aToken );
75 if( bRet && aTokenEnum.getNextToken( aToken ) )
76 bRet = rUnitConverter.convertMeasureToCore( nY, aToken );
77
78 if( bRet )
79 {
80 if( nX < 0 )
81 {
82 if( nY < 0 )
83 aShadow.Location = table::ShadowLocation_TOP_LEFT;
84 else
85 aShadow.Location = table::ShadowLocation_BOTTOM_LEFT;
86 }
87 else
88 {
89 if( nY < 0 )
90 aShadow.Location = table::ShadowLocation_TOP_RIGHT;
91 else
92 aShadow.Location = table::ShadowLocation_BOTTOM_RIGHT;
93 }
94
95 if (nX < 0)
97 if (nY < 0)
99
100 sal_Int32 nWidth;
101 bRet = !o3tl::checked_add(nX, nY, nWidth);
102 if (bRet)
103 aShadow.ShadowWidth = sal::static_int_cast<sal_Int16>(nWidth >> 1);
104 }
105 }
106 }
107
108 if( bRet && ( bColorFound || bOffsetFound ) )
109 {
110 aShadow.IsTransparent = aColor.IsTransparent();
111 aShadow.Color = sal_Int32(aColor);
112 bRet = true;
113 }
114
115 rValue <<= aShadow;
116
117 return bRet;
118}
119
120bool XMLShadowPropHdl::exportXML( OUString& rStrExpValue, const uno::Any& rValue, const SvXMLUnitConverter& rUnitConverter ) const
121{
122 bool bRet = false;
123 table::ShadowFormat aShadow;
124
125 if( rValue >>= aShadow )
126 {
127 sal_Int32 nX = 1, nY = 1;
128
129 switch( aShadow.Location )
130 {
131 case table::ShadowLocation_TOP_LEFT:
132 nX = -1;
133 nY = -1;
134 break;
135 case table::ShadowLocation_TOP_RIGHT:
136 nY = -1;
137 break;
138 case table::ShadowLocation_BOTTOM_LEFT:
139 nX = -1;
140 break;
141 case table::ShadowLocation_BOTTOM_RIGHT:
142 break;
143 case table::ShadowLocation_NONE:
144 default:
145 rStrExpValue = GetXMLToken(XML_NONE);
146 return true;
147 }
148
149 nX *= aShadow.ShadowWidth;
150 nY *= aShadow.ShadowWidth;
151
152 OUStringBuffer aOut;
153 ::sax::Converter::convertColor( aOut, aShadow.Color );
154 aOut.append( ' ' );
155 rUnitConverter.convertMeasureToXML( aOut, nX );
156 aOut.append( ' ' );
157 rUnitConverter.convertMeasureToXML( aOut, nY );
158
159 rStrExpValue = aOut.makeStringAndClear();
160
161 bRet = true;
162 }
163
164 return bRet;
165}
166
167/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
bool IsTransparent() const
bool getNextToken(std::u16string_view &rToken)
Definition: xmluconv.cxx:529
the SvXMLTypeConverter converts values of various types from their internal representation to the tex...
Definition: xmluconv.hxx:83
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
virtual bool exportXML(OUString &rStrExpValue, const css::uno::Any &rValue, const SvXMLUnitConverter &rUnitConverter) const override
Exports the given value according to the XML-data-type corresponding to the derived class.
Definition: shadwhdl.cxx:120
virtual ~XMLShadowPropHdl() override
Definition: shadwhdl.cxx:38
virtual bool importXML(const OUString &rStrImpValue, css::uno::Any &rValue, const SvXMLUnitConverter &rUnitConverter) const override
Imports the given value according to the XML-data-type corresponding to the derived class.
Definition: shadwhdl.cxx:43
static bool convertColor(sal_Int32 &rColor, std::u16string_view rValue)
float u
std::enable_if< std::is_signed< T >::value, bool >::type checked_add(T a, T b, T &result)
std::enable_if< std::is_signed< T >::value, T >::type saturating_toggle_sign(T a)
Handling of tokens in XML:
bool IsXMLToken(std::u16string_view rString, enum XMLTokenEnum eToken)
compare eToken to the string
Definition: xmltoken.cxx:3597
const OUString & GetXMLToken(enum XMLTokenEnum eToken)
return the OUString representation for eToken
Definition: xmltoken.cxx:3541