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 <o3tl/safeint.hxx>
16 #include <type_traits>
20 typename
std::enable_if
<
21 std::is_signed
<T
>::value
|| std::is_floating_point
<T
>::value
, long >::type
22 MinMax(T nVal
, long nMin
, long nMax
)
28 return static_cast<long>(nVal
);
40 typename
std::enable_if
<
41 std::is_unsigned
<T
>::value
, long >::type
42 MinMax(T nVal
, long nMin
, long nMax
)
51 if (nMin
< 0 || nVal
>= static_cast<unsigned long>(nMin
))
53 if (nVal
<= static_cast<unsigned long>(nMax
))
54 return static_cast<long>(nVal
);
65 inline sal_uInt32
AlignedWidth4Bytes(sal_uInt32 nWidthBits
)
67 if (nWidthBits
> SAL_MAX_UINT32
- 31)
68 nWidthBits
= SAL_MAX_UINT32
;
71 return (nWidthBits
>> 5) << 2;
74 inline long FRound( double fVal
)
76 return fVal
> 0.0 ? static_cast<long>( fVal
+ 0.5 ) : -static_cast<long>( -fVal
+ 0.5 );
79 //valid range: (-180,180]
81 [[nodiscard
]] inline typename
std::enable_if
<std::is_signed
<T
>::value
, T
>::type
91 //valid range: [0,360)
92 template <typename T
> [[nodiscard
]] inline T
NormAngle360(T angle
)
101 /** Convert 100th-mm to twips
103 A twip is 1/20 of a point, one inch is equal to 72 points, and
104 one inch is 2,540 100th-mm.
107 twips = n * 72 / 2,540 / 20
110 Adding 63 (half of 127) fixes truncation issues in int arithmetic.
112 This formula is (n>=0) ? (n*72+63) / 127 : (n*72-63) / 127
114 inline sal_Int64
sanitiseMm100ToTwip(sal_Int64 n
)
118 if (o3tl::checked_multiply
<sal_Int64
>(n
, 72, n
) || o3tl::checked_add
<sal_Int64
>(n
, 63, n
))
123 if (o3tl::checked_multiply
<sal_Int64
>(n
, 72, n
) || o3tl::checked_sub
<sal_Int64
>(n
, 63, n
))
126 return n
/ 127; // 127 is 2,540 100th-mm divided by 20pts
131 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */