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 * This file incorporates work covered by the following license notice:
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 .
20 #ifndef INCLUDED_OOX_HELPER_HELPER_HXX
21 #define INCLUDED_OOX_HELPER_HELPER_HXX
23 #include <sal/config.h>
29 #include <osl/endian.h>
30 #include <rtl/math.hxx>
31 #include <rtl/string.hxx>
32 #include <rtl/textenc.h>
33 #include <sal/macros.h>
34 #include <sal/types.h>
38 // Helper macros ==============================================================
40 /** Expands to the 'index'-th element of a STATIC data array, or to 'def', if
41 'index' is out of the array limits. */
42 #define STATIC_ARRAY_SELECT( array, index, def ) \
43 ((static_cast<size_t>(index) < SAL_N_ELEMENTS(array)) ? ((array)[static_cast<size_t>(index)]) : (def))
45 /** Convert an OUString to an ASCII C string. Use for debug purposes only. */
46 #define OUSTRING_TO_CSTR( str ) \
47 OUStringToOString( str, RTL_TEXTENCODING_ASCII_US ).getStr()
49 // Common constants ===========================================================
51 const sal_uInt8 WINDOWS_CHARSET_ANSI
= 0;
52 const sal_uInt8 WINDOWS_CHARSET_DEFAULT
= 1;
53 const sal_uInt8 WINDOWS_CHARSET_SYMBOL
= 2;
54 const sal_uInt8 WINDOWS_CHARSET_APPLE_ROMAN
= 77;
55 const sal_uInt8 WINDOWS_CHARSET_SHIFTJIS
= 128;
56 const sal_uInt8 WINDOWS_CHARSET_HANGEUL
= 129;
57 const sal_uInt8 WINDOWS_CHARSET_JOHAB
= 130;
58 const sal_uInt8 WINDOWS_CHARSET_GB2312
= 134;
59 const sal_uInt8 WINDOWS_CHARSET_BIG5
= 136;
60 const sal_uInt8 WINDOWS_CHARSET_GREEK
= 161;
61 const sal_uInt8 WINDOWS_CHARSET_TURKISH
= 162;
62 const sal_uInt8 WINDOWS_CHARSET_VIETNAMESE
= 163;
63 const sal_uInt8 WINDOWS_CHARSET_HEBREW
= 177;
64 const sal_uInt8 WINDOWS_CHARSET_ARABIC
= 178;
65 const sal_uInt8 WINDOWS_CHARSET_BALTIC
= 186;
66 const sal_uInt8 WINDOWS_CHARSET_RUSSIAN
= 204;
67 const sal_uInt8 WINDOWS_CHARSET_THAI
= 222;
68 const sal_uInt8 WINDOWS_CHARSET_EASTERN
= 238;
69 const sal_uInt8 WINDOWS_CHARSET_OEM
= 255;
72 const sal_Int32 API_RGB_TRANSPARENT
= -1; ///< Transparent color for API calls.
73 const sal_uInt32 UNSIGNED_RGB_TRANSPARENT
= static_cast<sal_uInt32
>(-1); ///< Transparent color for unsigned int32 places.
74 const sal_Int32 API_RGB_BLACK
= 0x000000; ///< Black color for API calls.
75 const sal_Int32 API_RGB_GRAY
= 0x808080; ///< Gray color for API calls.
76 const sal_Int32 API_RGB_WHITE
= 0xFFFFFF; ///< White color for API calls.
78 const sal_Int16 API_LINE_SOLID
= 0;
79 const sal_Int16 API_LINE_DOTTED
= 1;
80 const sal_Int16 API_LINE_DASHED
= 2;
81 const sal_Int16 API_FINE_LINE_DASHED
= 14;
83 const sal_Int16 API_LINE_NONE
= 0;
84 const sal_Int16 API_LINE_HAIR
= 2;
85 const sal_Int16 API_LINE_THIN
= 35;
86 const sal_Int16 API_LINE_MEDIUM
= 88;
87 const sal_Int16 API_LINE_THICK
= 141;
89 const sal_Int16 API_ESCAPE_NONE
= 0; ///< No escapement.
90 const sal_Int16 API_ESCAPE_SUPERSCRIPT
= 101; ///< Superscript: raise characters automatically (magic value 101).
91 const sal_Int16 API_ESCAPE_SUBSCRIPT
= -101; ///< Subscript: lower characters automatically (magic value -101).
93 const sal_Int8 API_ESCAPEHEIGHT_NONE
= 100; ///< Relative character height if not escaped.
94 const sal_Int8 API_ESCAPEHEIGHT_DEFAULT
= 58; ///< Relative character height if escaped.
97 // Limitate values ------------------------------------------------------------
99 template< typename ReturnType
, typename Type
>
100 inline ReturnType
getLimitedValue( Type nValue
, Type nMin
, Type nMax
)
102 return static_cast< ReturnType
>( ::std::min( ::std::max( nValue
, nMin
), nMax
) );
105 template< typename ReturnType
, typename Type
>
106 inline ReturnType
getIntervalValue( Type nValue
, Type nBegin
, Type nEnd
)
108 static_assert(::std::numeric_limits
< Type
>::is_integer
, "is integer");
109 Type nInterval
= nEnd
- nBegin
;
110 Type nCount
= (nValue
< nBegin
) ? -((nBegin
- nValue
- 1) / nInterval
+ 1) : ((nValue
- nBegin
) / nInterval
);
111 return static_cast< ReturnType
>( nValue
- nCount
* nInterval
);
114 template< typename ReturnType
>
115 inline ReturnType
getDoubleIntervalValue( double fValue
, double fBegin
, double fEnd
)
117 double fInterval
= fEnd
- fBegin
;
118 double fCount
= (fValue
< fBegin
) ? -(::rtl::math::approxFloor( (fBegin
- fValue
- 1.0) / fInterval
) + 1.0) : ::rtl::math::approxFloor( (fValue
- fBegin
) / fInterval
);
119 return static_cast< ReturnType
>( fValue
- fCount
* fInterval
);
122 // Read from bitfields --------------------------------------------------------
124 /** Returns true, if at least one of the bits set in nMask is set in nBitField. */
125 template< typename Type
>
126 inline bool getFlag( Type nBitField
, Type nMask
)
128 return (nBitField
& nMask
) != 0;
131 /** Returns nSet, if at least one bit of nMask is set in nBitField, otherwise nUnset. */
132 template< typename ReturnType
, typename Type
>
133 inline ReturnType
getFlagValue( Type nBitField
, Type nMask
, ReturnType nSet
, ReturnType nUnset
)
135 return getFlag( nBitField
, nMask
) ? nSet
: nUnset
;
138 /** Extracts a value from a bit field.
140 Returns the data fragment from nBitField, that starts at bit nStartBit
141 (0-based, bit 0 is rightmost) with the width of nBitCount. The returned
142 value will be right-aligned (normalized).
143 For instance: extractValue<T>(0x4321,8,4) returns 3 (value in bits 8-11).
145 template< typename ReturnType
, typename Type
>
146 inline ReturnType
extractValue( Type nBitField
, sal_uInt8 nStartBit
, sal_uInt8 nBitCount
)
148 sal_uInt64 nMask
= 1; nMask
<<= nBitCount
; --nMask
;
149 return static_cast< ReturnType
>( nMask
& (nBitField
>> nStartBit
) );
152 // Write to bitfields ---------------------------------------------------------
154 /** Sets or clears (according to bSet) all set bits of nMask in ornBitField. */
155 template< typename Type
>
156 inline void setFlag( Type
& ornBitField
, Type nMask
, bool bSet
= true )
158 if( bSet
) ornBitField
|= nMask
; else ornBitField
&= ~nMask
;
162 /** Optional value, similar to ::boost::optional<>, with convenience accessors.
164 template< typename Type
>
168 OptValue() : maValue(), mbHasValue( false ) {}
169 explicit OptValue( const Type
& rValue
) : maValue( rValue
), mbHasValue( true ) {}
170 explicit OptValue( bool bHasValue
, const Type
& rValue
) : maValue( rValue
), mbHasValue( bHasValue
) {}
172 bool has() const { return mbHasValue
; }
173 bool operator!() const { return !mbHasValue
; }
174 bool differsFrom( const Type
& rValue
) const { return mbHasValue
&& (maValue
!= rValue
); }
176 const Type
& get() const { return maValue
; }
177 const Type
& get( const Type
& rDefValue
) const { return mbHasValue
? maValue
: rDefValue
; }
179 void set( const Type
& rValue
) { maValue
= rValue
; mbHasValue
= true; }
180 Type
& use() { mbHasValue
= true; return maValue
; }
182 OptValue
& operator=( const Type
& rValue
) { set( rValue
); return *this; }
183 bool operator==( const OptValue
& rValue
) const {
184 return ( ( !mbHasValue
&& rValue
.mbHasValue
== false ) ||
185 ( mbHasValue
== rValue
.mbHasValue
&& maValue
== rValue
.maValue
) );
187 void assignIfUsed( const OptValue
& rValue
) { if( rValue
.mbHasValue
) set( rValue
.maValue
); }
195 /** Provides platform independent functions to convert from or to little-endian
196 byte order, e.g. for reading data from or writing data to memory or a
199 On big-endian platforms, the byte order in the passed values is swapped,
200 this can be used for converting big-endian to and from little-endian data.
202 On little-endian platforms, the conversion functions are implemented empty,
203 thus compilers should completely optimize away the function call.
205 class ByteOrderConverter
209 static void convertLittleEndian( sal_Int8
& ) {} // present for usage in templates
210 static void convertLittleEndian( sal_uInt8
& ) {} // present for usage in templates
211 static void convertLittleEndian( char16_t
& rnValue
) { swap2( reinterpret_cast< sal_uInt8
* >( &rnValue
) ); }
212 static void convertLittleEndian( sal_Int16
& rnValue
) { swap2( reinterpret_cast< sal_uInt8
* >( &rnValue
) ); }
213 static void convertLittleEndian( sal_uInt16
& rnValue
) { swap2( reinterpret_cast< sal_uInt8
* >( &rnValue
) ); }
214 static void convertLittleEndian( sal_Int32
& rnValue
) { swap4( reinterpret_cast< sal_uInt8
* >( &rnValue
) ); }
215 static void convertLittleEndian( sal_uInt32
& rnValue
) { swap4( reinterpret_cast< sal_uInt8
* >( &rnValue
) ); }
216 static void convertLittleEndian( sal_Int64
& rnValue
) { swap8( reinterpret_cast< sal_uInt8
* >( &rnValue
) ); }
217 static void convertLittleEndian( sal_uInt64
& rnValue
) { swap8( reinterpret_cast< sal_uInt8
* >( &rnValue
) ); }
218 static void convertLittleEndian( float& rfValue
) { swap4( reinterpret_cast< sal_uInt8
* >( &rfValue
) ); }
219 static void convertLittleEndian( double& rfValue
) { swap8( reinterpret_cast< sal_uInt8
* >( &rfValue
) ); }
221 template< typename Type
>
222 inline static void convertLittleEndianArray( Type
* pnArray
, size_t nElemCount
);
224 static void convertLittleEndianArray( sal_Int8
*, size_t ) {}
225 static void convertLittleEndianArray( sal_uInt8
*, size_t ) {}
228 template< typename Type
>
229 static void convertLittleEndian( Type
& ) {}
231 template< typename Type
>
232 static void convertLittleEndianArray( Type
*, size_t ) {}
236 /** Writes a value to memory, while converting it to little-endian.
237 @param pDstBuffer The memory buffer to write the value to.
238 @param nValue The value to be written to memory in little-endian.
240 template< typename Type
>
241 inline static void writeLittleEndian( void* pDstBuffer
, Type nValue
);
245 inline static void swap2( sal_uInt8
* pnData
);
246 inline static void swap4( sal_uInt8
* pnData
);
247 inline static void swap8( sal_uInt8
* pnData
);
252 template< typename Type
>
253 inline void ByteOrderConverter::writeLittleEndian( void* pDstBuffer
, Type nValue
)
255 convertLittleEndian( nValue
);
256 memcpy( pDstBuffer
, &nValue
, sizeof( Type
) );
260 template< typename Type
>
261 inline void ByteOrderConverter::convertLittleEndianArray( Type
* pnArray
, size_t nElemCount
)
263 for( Type
* pnArrayEnd
= pnArray
+ nElemCount
; pnArray
!= pnArrayEnd
; ++pnArray
)
264 convertLittleEndian( *pnArray
);
267 inline void ByteOrderConverter::swap2( sal_uInt8
* pnData
)
269 ::std::swap( pnData
[ 0 ], pnData
[ 1 ] );
272 inline void ByteOrderConverter::swap4( sal_uInt8
* pnData
)
274 ::std::swap( pnData
[ 0 ], pnData
[ 3 ] );
275 ::std::swap( pnData
[ 1 ], pnData
[ 2 ] );
278 inline void ByteOrderConverter::swap8( sal_uInt8
* pnData
)
280 ::std::swap( pnData
[ 0 ], pnData
[ 7 ] );
281 ::std::swap( pnData
[ 1 ], pnData
[ 6 ] );
282 ::std::swap( pnData
[ 2 ], pnData
[ 5 ] );
283 ::std::swap( pnData
[ 3 ], pnData
[ 4 ] );
292 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */