1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 #ifndef INCLUDED_TOOLS_HELPERS_HXX
10 #define INCLUDED_TOOLS_HELPERS_HXX
12 #include <sal/config.h>
13 #include <sal/types.h>
14 #include <tools/long.hxx>
15 #include <o3tl/safeint.hxx>
17 #include <type_traits>
21 typename
std::enable_if
<
22 std::is_signed
<T
>::value
|| std::is_floating_point
<T
>::value
, long >::type
23 MinMax(T nVal
, tools::Long nMin
, tools::Long nMax
)
29 return static_cast<tools::Long
>(nVal
);
41 typename
std::enable_if
<
42 std::is_unsigned
<T
>::value
, long >::type
43 MinMax(T nVal
, tools::Long nMin
, tools::Long nMax
)
52 if (nMin
< 0 || nVal
>= static_cast<unsigned long>(nMin
))
54 if (nVal
<= static_cast<unsigned long>(nMax
))
55 return static_cast<tools::Long
>(nVal
);
66 inline sal_uInt32
AlignedWidth4Bytes(sal_uInt32 nWidthBits
)
68 if (nWidthBits
> SAL_MAX_UINT32
- 31)
69 nWidthBits
= SAL_MAX_UINT32
;
72 return (nWidthBits
>> 5) << 2;
75 inline tools::Long
FRound( double fVal
)
77 return fVal
> 0.0 ? static_cast<tools::Long
>( fVal
+ 0.5 ) : -static_cast<tools::Long
>( -fVal
+ 0.5 );
80 //valid range: (-180,180]
82 [[nodiscard
]] inline typename
std::enable_if
<std::is_signed
<T
>::value
, T
>::type
92 //valid range: [0,360)
93 template <typename T
> [[nodiscard
]] inline T
NormAngle360(T angle
)
102 /** Convert 100th-mm to twips
104 A twip is 1/20 of a point, one inch is equal to 72 points, and
105 one inch is 2,540 100th-mm.
108 twips = n * 72 / 2,540 / 20
111 Adding 63 (half of 127) fixes truncation issues in int arithmetic.
113 This formula is (n>=0) ? (n*72+63) / 127 : (n*72-63) / 127
115 inline sal_Int64
sanitiseMm100ToTwip(sal_Int64 n
)
119 if (o3tl::checked_multiply
<sal_Int64
>(n
, 72, n
) || o3tl::checked_add
<sal_Int64
>(n
, 63, n
))
124 if (o3tl::checked_multiply
<sal_Int64
>(n
, 72, n
) || o3tl::checked_sub
<sal_Int64
>(n
, 63, n
))
127 return n
/ 127; // 127 is 2,540 100th-mm divided by 20pts
132 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */