/src/libreoffice/svx/source/dialog/framelink.cxx
Line | Count | Source (jump to first uncovered line) |
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 <sal/config.h> |
21 | | |
22 | | #include <rtl/math.hxx> |
23 | | #include <svx/framelink.hxx> |
24 | | |
25 | | #include <editeng/borderline.hxx> |
26 | | #include <o3tl/hash_combine.hxx> |
27 | | |
28 | | |
29 | | using namespace ::com::sun::star; |
30 | | using namespace editeng; |
31 | | |
32 | | namespace svx::frame |
33 | | { |
34 | | |
35 | | Style::Style( double nP, double nD, double nS, SvxBorderLineStyle nType, double fScale ) |
36 | 0 | { |
37 | 0 | Clear(); |
38 | 0 | mnType = nType; |
39 | 0 | mfPatternScale = fScale; |
40 | 0 | Set( nP, nD, nS ); |
41 | 0 | } |
42 | | |
43 | | Style::Style( const Color& rColorPrim, const Color& rColorSecn, const Color& rColorGap, bool bUseGapColor, double nP, double nD, double nS, SvxBorderLineStyle nType, double fScale ) |
44 | 0 | { |
45 | 0 | Clear(); |
46 | 0 | mnType = nType; |
47 | 0 | mfPatternScale = fScale; |
48 | 0 | Set( rColorPrim, rColorSecn, rColorGap, bUseGapColor, nP, nD, nS ); |
49 | 0 | } |
50 | | |
51 | | Style::Style( const editeng::SvxBorderLine* pBorder, double fScale ) |
52 | 1.69M | { |
53 | 1.69M | Clear(); |
54 | 1.69M | if(nullptr != pBorder) |
55 | 2.50k | { |
56 | 2.50k | mfPatternScale = fScale; |
57 | 2.50k | Set( pBorder, fScale ); |
58 | 2.50k | } |
59 | 1.69M | } |
60 | | |
61 | | void Style::Clear() |
62 | 1.69M | { |
63 | 1.69M | maColorPrim = Color(); |
64 | 1.69M | maColorSecn = Color(); |
65 | 1.69M | maColorGap = Color(); |
66 | 1.69M | mbUseGapColor = false; |
67 | 1.69M | meRefMode = RefMode::Centered; |
68 | 1.69M | mfPrim = 0.0; |
69 | 1.69M | mfDist = 0.0; |
70 | 1.69M | mfSecn = 0.0; |
71 | 1.69M | mfPatternScale = 1.0; |
72 | 1.69M | mnType = SvxBorderLineStyle::SOLID; |
73 | 1.69M | mbWordTableCell = false; |
74 | 1.69M | } |
75 | | |
76 | | void Style::Set( double nP, double nD, double nS ) |
77 | 2.50k | { |
78 | | /* nP nD nS -> mfPrim mfDist mfSecn |
79 | | -------------------------------------- |
80 | | any any 0 nP 0 0 |
81 | | 0 any >0 nS 0 0 |
82 | | >0 0 >0 nP 0 0 |
83 | | >0 >0 >0 nP nD nS |
84 | | */ |
85 | 2.50k | mfPrim = rtl::math::round(nP ? nP : nS, 2); |
86 | 2.50k | mfDist = rtl::math::round((nP && nS) ? nD : 0, 2); |
87 | 2.50k | mfSecn = rtl::math::round((nP && nD) ? nS : 0, 2); |
88 | 2.50k | } |
89 | | |
90 | | void Style::Set( const Color& rColorPrim, const Color& rColorSecn, const Color& rColorGap, bool bUseGapColor, double nP, double nD, double nS ) |
91 | 0 | { |
92 | 0 | maColorPrim = rColorPrim; |
93 | 0 | maColorSecn = rColorSecn; |
94 | 0 | maColorGap = rColorGap; |
95 | 0 | mbUseGapColor = bUseGapColor; |
96 | 0 | Set( nP, nD, nS ); |
97 | 0 | } |
98 | | |
99 | | void Style::Set( const SvxBorderLine* pBorder, double fScale, sal_uInt16 nMaxWidth ) |
100 | 2.50k | { |
101 | 2.50k | if(nullptr == pBorder) |
102 | 0 | { |
103 | 0 | Clear(); |
104 | 0 | return; |
105 | 0 | } |
106 | | |
107 | 2.50k | maColorPrim = pBorder->GetColorOut(); |
108 | 2.50k | maColorSecn = pBorder->GetColorIn(); |
109 | 2.50k | maColorGap = pBorder->GetColorGap(); |
110 | 2.50k | mbUseGapColor = pBorder->HasGapColor(); |
111 | | |
112 | 2.50k | const sal_uInt16 nPrim(pBorder->GetOutWidth()); |
113 | 2.50k | const sal_uInt16 nDist(pBorder->GetDistance()); |
114 | 2.50k | const sal_uInt16 nSecn(pBorder->GetInWidth()); |
115 | | |
116 | 2.50k | mnType = pBorder->GetBorderLineStyle(); |
117 | 2.50k | mfPatternScale = fScale; |
118 | | |
119 | 2.50k | if( !nSecn ) // no or single frame border |
120 | 2.50k | { |
121 | 2.50k | Set( std::min<double>(nPrim * fScale, nMaxWidth), 0, 0 ); |
122 | 2.50k | } |
123 | 7 | else |
124 | 7 | { |
125 | 7 | Set(std::min<double>(nPrim * fScale, nMaxWidth), std::min<double>(nDist * fScale, nMaxWidth), std::min<double>(nSecn * fScale, nMaxWidth)); |
126 | | // Enlarge the style if distance is too small due to rounding losses. |
127 | 7 | double nPixWidth = std::min<double>((nPrim + nDist + nSecn) * fScale, nMaxWidth); |
128 | | |
129 | 7 | if( nPixWidth > GetWidth() ) |
130 | 0 | { |
131 | 0 | mfDist = nPixWidth - mfPrim - mfSecn; |
132 | 0 | } |
133 | | |
134 | | // Shrink the style if it is too thick for the control. |
135 | 7 | while( GetWidth() > nMaxWidth ) |
136 | 0 | { |
137 | | // First decrease space between lines. |
138 | 0 | if (mfDist) |
139 | 0 | { |
140 | 0 | --mfDist; |
141 | 0 | continue; |
142 | 0 | } |
143 | | |
144 | | // Still too thick? Decrease the line widths. |
145 | 0 | if (mfPrim != 0.0 && rtl::math::approxEqual(mfPrim, mfSecn)) |
146 | 0 | { |
147 | | // Both lines equal - decrease both to keep symmetry. |
148 | 0 | --mfPrim; |
149 | 0 | --mfSecn; |
150 | 0 | continue; |
151 | 0 | } |
152 | | |
153 | | // Decrease each line for itself |
154 | 0 | if (mfPrim) |
155 | 0 | --mfPrim; |
156 | |
|
157 | 0 | if ((GetWidth() > nMaxWidth) && mfSecn != 0.0) |
158 | 0 | --mfSecn; |
159 | 0 | } |
160 | 7 | } |
161 | 2.50k | } |
162 | | |
163 | | void Style::MirrorSelf() |
164 | 283 | { |
165 | 283 | if (mfSecn) |
166 | 0 | { |
167 | 0 | std::swap( mfPrim, mfSecn ); |
168 | | // also need to swap colors |
169 | 0 | std::swap( maColorPrim, maColorSecn ); |
170 | 0 | } |
171 | | |
172 | 283 | if( meRefMode != RefMode::Centered ) |
173 | 0 | { |
174 | 0 | meRefMode = (meRefMode == RefMode::Begin) ? RefMode::End : RefMode::Begin; |
175 | 0 | } |
176 | 283 | } |
177 | | |
178 | | bool Style::operator==( const Style& rOther) const |
179 | 0 | { |
180 | 0 | if (this == &rOther) |
181 | | // ptr compare (same instance) |
182 | 0 | return true; |
183 | | |
184 | 0 | return (Prim() == rOther.Prim() |
185 | 0 | && Dist() == rOther.Dist() |
186 | 0 | && Secn() == rOther.Secn() |
187 | 0 | && GetColorPrim() == rOther.GetColorPrim() |
188 | 0 | && GetColorSecn() == rOther.GetColorSecn() |
189 | 0 | && GetColorGap() == rOther.GetColorGap() |
190 | 0 | && GetRefMode() == rOther.GetRefMode() |
191 | 0 | && UseGapColor() == rOther.UseGapColor() |
192 | 0 | && Type() == rOther.Type()); |
193 | 0 | } |
194 | | |
195 | | size_t Style::hashCode() const |
196 | 0 | { |
197 | 0 | std::size_t seed = 0; |
198 | 0 | o3tl::hash_combine(seed, Prim()); |
199 | 0 | o3tl::hash_combine(seed, Dist()); |
200 | 0 | o3tl::hash_combine(seed, Secn()); |
201 | 0 | o3tl::hash_combine(seed, static_cast<sal_Int32>(GetColorPrim())); |
202 | 0 | o3tl::hash_combine(seed, static_cast<sal_Int32>(GetColorSecn())); |
203 | 0 | o3tl::hash_combine(seed, static_cast<sal_Int32>(GetColorGap())); |
204 | 0 | o3tl::hash_combine(seed, GetRefMode()); |
205 | 0 | o3tl::hash_combine(seed, UseGapColor()); |
206 | 0 | o3tl::hash_combine(seed, Type()); |
207 | 0 | return seed; |
208 | 0 | } |
209 | | |
210 | | |
211 | | namespace |
212 | | { |
213 | | /** |
214 | | * Gets the weight of rStyle, according to [MS-OI29500] v20171130, 2.1.168 Part 1 Section 17.4.66, |
215 | | * tcBorders (Table Cell Borders). |
216 | | */ |
217 | | double GetWordTableCellBorderWeight(const Style& rStyle) |
218 | 2.06k | { |
219 | 2.06k | double fWidth = rStyle.GetWidth(); |
220 | 2.06k | int nBorderNumber = 0; |
221 | | |
222 | | // See lcl_convertBorderStyleFromToken() in writerfilter/ and ConvertBorderStyleFromWord() in |
223 | | // editeng/, this is the opposite of the combination of those functions. |
224 | 2.06k | switch (rStyle.Type()) |
225 | 2.06k | { |
226 | 0 | case SvxBorderLineStyle::NONE: |
227 | 0 | return 0.0; |
228 | 0 | case SvxBorderLineStyle::DOTTED: |
229 | 0 | case SvxBorderLineStyle::DASHED: |
230 | 0 | return 1.0; |
231 | 2.06k | case SvxBorderLineStyle::SOLID: |
232 | | // single = 1 |
233 | | // thick = 2 |
234 | | // wave = 20 |
235 | 2.06k | nBorderNumber = 1; |
236 | 2.06k | break; |
237 | 0 | case SvxBorderLineStyle::DOUBLE: |
238 | 0 | case SvxBorderLineStyle::DOUBLE_THIN: |
239 | | // double = 3 |
240 | | // triple = 10 |
241 | | // doubleWave = 21 |
242 | | // dashDotStroked = 23 |
243 | 0 | nBorderNumber = 3; |
244 | 0 | break; |
245 | 0 | case SvxBorderLineStyle::DASH_DOT: |
246 | | // dotDash = 8 |
247 | 0 | nBorderNumber = 8; |
248 | 0 | break; |
249 | 0 | case SvxBorderLineStyle::DASH_DOT_DOT: |
250 | | // dotDotDash = 9 |
251 | 0 | nBorderNumber = 9; |
252 | 0 | break; |
253 | 0 | case SvxBorderLineStyle::THINTHICK_SMALLGAP: |
254 | | // thinThickSmallGap = 11 |
255 | 0 | nBorderNumber = 11; |
256 | 0 | break; |
257 | 0 | case SvxBorderLineStyle::THICKTHIN_SMALLGAP: |
258 | | // thickThinSmallGap = 12 |
259 | | // thinThickThinSmallGap = 13 |
260 | 0 | nBorderNumber = 12; |
261 | 0 | break; |
262 | 0 | case SvxBorderLineStyle::THINTHICK_MEDIUMGAP: |
263 | | // thinThickMediumGap = 14 |
264 | 0 | nBorderNumber = 14; |
265 | 0 | break; |
266 | 0 | case SvxBorderLineStyle::THICKTHIN_MEDIUMGAP: |
267 | | // thickThinMediumGap = 15 |
268 | | // thinThickThinMediumGap = 16 |
269 | 0 | nBorderNumber = 15; |
270 | 0 | break; |
271 | 0 | case SvxBorderLineStyle::THINTHICK_LARGEGAP: |
272 | | // thinThickLargeGap = 17 |
273 | 0 | nBorderNumber = 17; |
274 | 0 | break; |
275 | 0 | case SvxBorderLineStyle::THICKTHIN_LARGEGAP: |
276 | | // thickThinLargeGap = 18 |
277 | | // thinThickThinLargeGap = 19 |
278 | 0 | nBorderNumber = 18; |
279 | 0 | break; |
280 | 0 | case SvxBorderLineStyle::FINE_DASHED: |
281 | | // dashSmallGap = 22 |
282 | 0 | nBorderNumber = 22; |
283 | 0 | break; |
284 | 0 | case SvxBorderLineStyle::EMBOSSED: |
285 | | // threeDEmboss = 24 |
286 | 0 | nBorderNumber = 24; |
287 | 0 | break; |
288 | 0 | case SvxBorderLineStyle::ENGRAVED: |
289 | | // threeDEngrave = 25 |
290 | 0 | nBorderNumber = 25; |
291 | 0 | break; |
292 | 0 | case SvxBorderLineStyle::OUTSET: |
293 | | // outset = 26 |
294 | 0 | nBorderNumber = 25; |
295 | 0 | break; |
296 | 0 | case SvxBorderLineStyle::INSET: |
297 | | // inset = 27 |
298 | 0 | nBorderNumber = 27; |
299 | 0 | break; |
300 | 2.06k | } |
301 | | |
302 | 2.06k | return nBorderNumber * fWidth; |
303 | 2.06k | } |
304 | | } |
305 | | |
306 | | bool Style::operator<( const Style& rOther) const |
307 | 1.03k | { |
308 | 1.03k | if (mbWordTableCell) |
309 | 1.03k | { |
310 | | // The below code would first compare based on the border width, Word compares based on its |
311 | | // calculated weight, do that in the compat case. |
312 | 1.03k | double fLW = GetWordTableCellBorderWeight(*this); |
313 | 1.03k | double fRW = GetWordTableCellBorderWeight(rOther); |
314 | 1.03k | if (!rtl::math::approxEqual(fLW, fRW)) |
315 | 55 | { |
316 | 55 | return fLW < fRW; |
317 | 55 | } |
318 | 1.03k | } |
319 | | |
320 | | // different total widths -> this<rOther, if this is thinner |
321 | 975 | double nLW = GetWidth(); |
322 | 975 | double nRW = rOther.GetWidth(); |
323 | 975 | if( !rtl::math::approxEqual(nLW, nRW) ) return nLW < nRW; |
324 | | |
325 | | // one line double, the other single -> this<rOther, if this is single |
326 | 975 | if( (Secn() == 0) != (rOther.Secn() == 0) ) return Secn() == 0; |
327 | | |
328 | | // both lines double with different distances -> this<rOther, if distance of this greater |
329 | 975 | if( (Secn() && rOther.Secn()) && !rtl::math::approxEqual(Dist(), rOther.Dist()) ) return Dist() > rOther.Dist(); |
330 | | |
331 | | // both lines single and 1 unit thick, only one is dotted -> this<rOther, if this is dotted |
332 | 975 | if ((nLW == 1) && !Secn() && !rOther.Secn() && (Type() != rOther.Type())) return Type() > rOther.Type(); |
333 | | |
334 | | // seem to be equal |
335 | 975 | return false; |
336 | 975 | } |
337 | | } |
338 | | |
339 | | /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |