/src/libreoffice/xmloff/source/style/shadwhdl.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 "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> |
28 | | #include <sax/tools/converter.hxx> |
29 | | #include <xmloff/xmluconv.hxx> |
30 | | #include <xmloff/xmltoken.hxx> |
31 | | |
32 | | using namespace ::com::sun::star; |
33 | | using namespace ::xmloff::token; |
34 | | |
35 | | |
36 | | |
37 | | |
38 | | XMLShadowPropHdl::~XMLShadowPropHdl() |
39 | 221k | { |
40 | | // nothing to do |
41 | 221k | } |
42 | | |
43 | | bool XMLShadowPropHdl::importXML( const OUString& rStrImpValue, uno::Any& rValue, const SvXMLUnitConverter& rUnitConverter ) const |
44 | 2.34k | { |
45 | 2.34k | bool bRet = false; |
46 | 2.34k | table::ShadowFormat aShadow; |
47 | 2.34k | aShadow.Location = table::ShadowLocation_BOTTOM_RIGHT; |
48 | | |
49 | 2.34k | bool bColorFound = false; |
50 | 2.34k | bool bOffsetFound = false; |
51 | 2.34k | SvXMLTokenEnumerator aTokenEnum( rStrImpValue ); |
52 | 2.34k | Color aColor( 128,128, 128 ); |
53 | 2.34k | std::u16string_view aToken; |
54 | | |
55 | 2.35k | while( aTokenEnum.getNextToken( aToken ) ) |
56 | 2.34k | { |
57 | 2.34k | if( IsXMLToken( aToken, XML_NONE ) ) |
58 | 2.33k | { |
59 | 2.33k | aShadow.Location = table::ShadowLocation_NONE; |
60 | 2.33k | bRet = true; |
61 | 2.33k | break; |
62 | 2.33k | } |
63 | 8 | else if( !bColorFound && aToken.substr(0,1) == u"#" ) |
64 | 0 | { |
65 | 0 | bRet = ::sax::Converter::convertColor( aColor, aToken ); |
66 | 0 | if( !bRet ) |
67 | 0 | return false; |
68 | 0 | bColorFound = true; |
69 | 0 | } |
70 | 8 | else if( !bOffsetFound ) |
71 | 8 | { |
72 | 8 | sal_Int32 nX = 0, nY = 0; |
73 | | |
74 | 8 | bRet = rUnitConverter.convertMeasureToCore( nX, aToken ); |
75 | 8 | if( bRet && aTokenEnum.getNextToken( aToken ) ) |
76 | 1 | bRet = rUnitConverter.convertMeasureToCore( nY, aToken ); |
77 | | |
78 | 8 | if( bRet ) |
79 | 2 | { |
80 | 2 | if( nX < 0 ) |
81 | 0 | { |
82 | 0 | if( nY < 0 ) |
83 | 0 | aShadow.Location = table::ShadowLocation_TOP_LEFT; |
84 | 0 | else |
85 | 0 | aShadow.Location = table::ShadowLocation_BOTTOM_LEFT; |
86 | 0 | } |
87 | 2 | else |
88 | 2 | { |
89 | 2 | if( nY < 0 ) |
90 | 0 | aShadow.Location = table::ShadowLocation_TOP_RIGHT; |
91 | 2 | else |
92 | 2 | aShadow.Location = table::ShadowLocation_BOTTOM_RIGHT; |
93 | 2 | } |
94 | | |
95 | 2 | if (nX < 0) |
96 | 0 | nX = o3tl::saturating_toggle_sign(nX); |
97 | 2 | if (nY < 0) |
98 | 0 | nY = o3tl::saturating_toggle_sign(nY); |
99 | | |
100 | 2 | sal_Int32 nWidth; |
101 | 2 | bRet = !o3tl::checked_add(nX, nY, nWidth); |
102 | 2 | if (bRet) |
103 | 2 | aShadow.ShadowWidth = sal::static_int_cast<sal_Int16>(nWidth >> 1); |
104 | 2 | } |
105 | 8 | } |
106 | 2.34k | } |
107 | | |
108 | 2.34k | if( bRet && ( bColorFound || bOffsetFound ) ) |
109 | 0 | { |
110 | 0 | aShadow.IsTransparent = aColor.IsTransparent(); |
111 | 0 | aShadow.Color = sal_Int32(aColor); |
112 | 0 | bRet = true; |
113 | 0 | } |
114 | | |
115 | 2.34k | rValue <<= aShadow; |
116 | | |
117 | 2.34k | return bRet; |
118 | 2.34k | } |
119 | | |
120 | | bool XMLShadowPropHdl::exportXML( OUString& rStrExpValue, const uno::Any& rValue, const SvXMLUnitConverter& rUnitConverter ) const |
121 | 0 | { |
122 | 0 | bool bRet = false; |
123 | 0 | table::ShadowFormat aShadow; |
124 | |
|
125 | 0 | if( rValue >>= aShadow ) |
126 | 0 | { |
127 | 0 | sal_Int32 nX = 1, nY = 1; |
128 | |
|
129 | 0 | switch( aShadow.Location ) |
130 | 0 | { |
131 | 0 | case table::ShadowLocation_TOP_LEFT: |
132 | 0 | nX = -1; |
133 | 0 | nY = -1; |
134 | 0 | break; |
135 | 0 | case table::ShadowLocation_TOP_RIGHT: |
136 | 0 | nY = -1; |
137 | 0 | break; |
138 | 0 | case table::ShadowLocation_BOTTOM_LEFT: |
139 | 0 | nX = -1; |
140 | 0 | break; |
141 | 0 | case table::ShadowLocation_BOTTOM_RIGHT: |
142 | 0 | break; |
143 | 0 | case table::ShadowLocation_NONE: |
144 | 0 | default: |
145 | 0 | rStrExpValue = GetXMLToken(XML_NONE); |
146 | 0 | return true; |
147 | 0 | } |
148 | | |
149 | 0 | nX *= aShadow.ShadowWidth; |
150 | 0 | nY *= aShadow.ShadowWidth; |
151 | |
|
152 | 0 | OUStringBuffer aOut; |
153 | 0 | ::sax::Converter::convertColor( aOut, aShadow.Color ); |
154 | 0 | aOut.append( ' ' ); |
155 | 0 | rUnitConverter.convertMeasureToXML( aOut, nX ); |
156 | 0 | aOut.append( ' ' ); |
157 | 0 | rUnitConverter.convertMeasureToXML( aOut, nY ); |
158 | |
|
159 | 0 | rStrExpValue = aOut.makeStringAndClear(); |
160 | |
|
161 | 0 | bRet = true; |
162 | 0 | } |
163 | | |
164 | 0 | return bRet; |
165 | 0 | } |
166 | | |
167 | | /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |