Subversion Repositories tpanel

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
4 andreas 1
/*
21 andreas 2
 * Copyright (C) 2020, 2021 by Andreas Theofilu <andreas@theosys.at>
4 andreas 3
 *
4
 * This program is free software; you can redistribute it and/or modify
5
 * it under the terms of the GNU General Public License as published by
6
 * the Free Software Foundation; either version 3 of the License, or
7
 * (at your option) any later version.
8
 *
9
 * This program is distributed in the hope that it will be useful,
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
 * GNU General Public License for more details.
13
 *
14
 * You should have received a copy of the GNU General Public License
15
 * along with this program; if not, write to the Free Software Foundation,
16
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
17
 */
18
 
19
#ifndef __TRESOURCES_H__
20
#define __TRESOURCES_H__
21
 
66 andreas 22
#include <deque>
87 andreas 23
#include <vector>
162 andreas 24
#include <string>
25
#include <iostream>
26
#include <stdexcept>
27
#include <type_traits>
28
#include <locale>
29
#include <codecvt>
30
#include <cstdint>
31
#include <clocale>
403 andreas 32
#include <iomanip>
405 andreas 33
#include <sstream>
418 andreas 34
#include <type_traits>
66 andreas 35
 
226 andreas 36
#ifdef __MACH__
37
#include <skia/core/SkImage.h>
38
#include <skia/core/SkString.h>
39
#include <skia/core/SkData.h>
365 andreas 40
#include <skia/core/SkColor.h>
226 andreas 41
#else
4 andreas 42
#include <include/core/SkImage.h>
43
#include <include/core/SkString.h>
44
#include <include/core/SkData.h>
365 andreas 45
#include <include/core/SkColor.h>
226 andreas 46
#endif
4 andreas 47
 
271 andreas 48
//#include "terror.h"
162 andreas 49
 
271 andreas 50
#ifndef ulong
51
typedef unsigned long ulong;
52
#endif
53
 
4 andreas 54
class SkBitmap;
55
class SkData;
56
class SkStreamAsset;
57 andreas 57
class SkFont;
4 andreas 58
class SkTypeface;
59
 
156 andreas 60
typedef enum _RESOURCE_TYPE
61
{
62
    RESTYPE_UNKNOWN,
63
    RESTYPE_IMAGE,
64
    RESTYPE_SYSIMAGE,
65
    RESTYPE_FONT,
66
    RESTYPE_SYSFONT,
67
    RESTYPE_CURSOR,
68
    RESTYPE_SYSCURSOR,
69
    RESTYPE_BORDER,
70
    RESTYPE_SYSBORDER,
71
    RESTYPE_SLIDER,
72
    RESTYPE_SYSSLIDER
73
}_RESOURCE_TYPE;
4 andreas 74
 
156 andreas 75
SkString GetResourcePath(const char* resource = "", _RESOURCE_TYPE rs = RESTYPE_IMAGE);
76
 
4 andreas 77
bool DecodeDataToBitmap(sk_sp<SkData> data, SkBitmap* dst);
78
 
156 andreas 79
sk_sp<SkData> GetResourceAsData(const char* resource, _RESOURCE_TYPE rs = RESTYPE_IMAGE);
4 andreas 80
 
81
inline bool GetResourceAsBitmap(const char* resource, SkBitmap* dst)
82
{
83
    return DecodeDataToBitmap(GetResourceAsData(resource), dst);
84
}
85
 
86
inline sk_sp<SkImage> GetResourceAsImage(const char* resource)
87
{
365 andreas 88
    return SkImages::DeferredFromEncodedData(GetResourceAsData(resource));
4 andreas 89
}
90
 
156 andreas 91
std::unique_ptr<SkStreamAsset> GetResourceAsStream(const char* resource, _RESOURCE_TYPE rs = RESTYPE_IMAGE);
4 andreas 92
 
163 andreas 93
sk_sp<SkTypeface> MakeResourceAsTypeface(const char* resource, int ttcIndex = 0, _RESOURCE_TYPE rs = RESTYPE_FONT);
4 andreas 94
 
6 andreas 95
sk_sp<SkData> readImage(const std::string& fname);
254 andreas 96
SkBitmap *allocPixels(int width, int height, SkBitmap *bm);
97
SkColor reverseColor(const SkColor& col);
6 andreas 98
 
417 andreas 99
std::string& toLower(std::string& str);
100
std::string& toUpper(std::string& str);
11 andreas 101
std::vector<std::string> StrSplit(const std::string& str, const std::string& seps, const bool trimEmpty=false);
102
std::string UTF8ToCp1250(const std::string& str);
103
std::string cp1250ToUTF8(const std::string& str);
104
std::string latin1ToUTF8(const std::string& str);
263 andreas 105
std::string intToString(int num);
268 andreas 106
std::string ReplaceString(const std::string subject, const std::string& search, const std::string& replace);
11 andreas 107
 
93 andreas 108
void *renew(char **mem, size_t old_size, size_t new_size);
409 andreas 109
std::vector<std::string> splitLine(const std::string& str, bool multiline=false);
57 andreas 110
std::vector<std::string> splitLine(const std::string& str, int width, int height, SkFont& font, SkPaint& paint);
60 andreas 111
bool isHex(int c);
112
int strCaseCompare(const std::string& str1, const std::string& str2);
63 andreas 113
std::string fillString(int c, int len);
67 andreas 114
bool isUTF8(const std::string& str);
115
size_t utf8Strlen(const std::string& str);
116
uint16_t getUint16(const unsigned char *p, bool big_endian=false);
117
uint32_t getUint32(const unsigned char *p, bool big_endian=false);
361 andreas 118
std::string UnicodeToUTF8(const std::wstring& ws);
71 andreas 119
bool endsWith (const std::string &src, const std::string &end);
120
bool startsWith (const std::string &src, const std::string &start);
121
std::string dirName (const std::string &path);
122
std::string baseName (const std::string &path);
97 andreas 123
char *strnstr(const char *haystack, const char *needle, size_t len);
104 andreas 124
std::string getCommand(const std::string& fullCmd);
222 andreas 125
bool StrContains(const std::string& str, const std::string& part);
233 andreas 126
bool isTrue(const std::string& value);
127
bool isFalse(const std::string& value);
128
bool isNumeric(const std::string& str, bool blank=false);
260 andreas 129
bool isBigEndian();
271 andreas 130
std::string handleToString(ulong handle);
297 andreas 131
ulong extractHandle(const std::string& obname);
431 andreas 132
uint32_t createButtonID(int type, int ap, int ad, int cp, int ch, int lp=-1, int lv=-1);
21 andreas 133
 
418 andreas 134
/**
135
 * @brief Converts a numeric value of any type into a string.
136
 * This template converts a numeric value of any type into a string with
137
 * hexadezimal representation. The numer of digits is always equal. If
138
 * necessary a 0 is prepended to the string.
139
 * If the type is double or float the result is undefined!
140
 *
141
 * @param i     A numeric value of any type.
142
 *
143
 * @return Returns the hexadezimal string representation of the numeric value.
144
 */
403 andreas 145
template<typename T>
146
inline std::string numberToHex(T i)
147
{
148
    std::stringstream stream;
149
    stream  << std::setfill ('0') << std::setw(sizeof(T)*2)
150
    << std::hex << i;
151
    return stream.str();
152
}
153
 
73 andreas 154
static inline std::string &ltrim(std::string &s)
155
{
156
    s.erase(s.begin(), std::find_if(s.begin(), s.end(), [](int c) {return !std::isspace(c);}));
157
    return s;
158
}
159
 
160
static inline std::string &rtrim(std::string &str)
161
{
162
    str.erase(str.find_last_not_of(" \n\r\t")+1);
163
    return str;
164
}
165
 
166
static inline std::string& trim(std::string& str) { return ltrim(rtrim(str)); }
167
 
162 andreas 168
#define ASSERT_MSG(cond, msg) { if (!(cond)) throw std::runtime_error("Assertion (" #cond ") failed at line " + std::to_string(__LINE__) + "! Msg: " + std::string(msg)); }
169
#define ASSERT(cond) ASSERT_MSG(cond, "")
170
 
171
class CharConvert
172
{
173
    public:
174
        template <typename U8StrT = std::string>
175
        inline static U8StrT Utf32To8(std::u32string const & s)
176
        {
177
            static_assert(sizeof(typename U8StrT::value_type) == 1, "Char byte-size should be 1 for UTF-8 strings!");
178
            typedef typename U8StrT::value_type VT;
179
            typedef uint8_t u8;
180
            U8StrT r;
181
 
182
            for (auto c: s)
183
            {
184
                size_t nby = c <= 0x7FU ? 1 : c <= 0x7FFU ? 2 : c <= 0xFFFFU ? 3 : c <= 0x1FFFFFU ? 4 : c <= 0x3FFFFFFU ? 5 : c <= 0x7FFFFFFFU ? 6 : 7;
185
                r.push_back(VT(
186
                    nby <= 1 ? u8(c) : (
187
                        (u8(0xFFU) << (8 - nby)) |
188
                        u8(c >> (6 * (nby - 1)))
189
                    )
190
                ));
191
 
192
                for (size_t i = 1; i < nby; ++i)
193
                    r.push_back(VT(u8(0x80U | (u8(0x3FU) & u8(c >> (6 * (nby - 1 - i)))))));
194
            }
195
 
196
            return r;
197
        }
198
 
199
        template <typename U8StrT>
200
        inline static std::u32string Utf8To32(U8StrT const & s)
201
        {
202
            static_assert(sizeof(typename U8StrT::value_type) == 1, "Char byte-size should be 1 for UTF-8 strings!");
203
            typedef uint8_t u8;
204
            std::u32string r;
205
            auto it = (u8 const *)s.c_str(), end = (u8 const *)(s.c_str() + s.length());
206
 
207
            while (it < end)
208
            {
209
                char32_t c = 0;
210
                if (*it <= 0x7FU)
211
                {
212
                    c = *it;
213
                    ++it;
214
                }
215
                else
216
                {
217
                    ASSERT((*it & 0xC0U) == 0xC0U);
218
                    size_t nby = 0;
219
 
220
                    for (u8 b = *it; (b & 0x80U) != 0; b <<= 1, ++nby) { (void)0; }
221
                    ASSERT(nby <= 7);
222
                    ASSERT((size_t)(end - it) >= nby);
223
                    c = *it & (u8(0xFFU) >> (nby + 1));
224
 
225
                    for (size_t i = 1; i < nby; ++i)
226
                    {
227
                        ASSERT((it[i] & 0xC0U) == 0x80U);
228
                        c = (c << 6) | (it[i] & 0x3FU);
229
                    }
230
 
231
                    it += nby;
232
                }
233
 
234
                r.push_back(c);
235
            }
236
 
237
            return r;
238
        }
239
 
240
 
241
        template <typename U16StrT = std::u16string>
242
        inline static U16StrT Utf32To16(std::u32string const & s)
243
        {
244
            static_assert(sizeof(typename U16StrT::value_type) == 2, "Char byte-size should be 2 for UTF-16 strings!");
245
            typedef typename U16StrT::value_type VT;
246
            typedef uint16_t u16;
247
            U16StrT r;
248
 
249
            for (auto c: s)
250
            {
251
                if (c <= 0xFFFFU)
252
                    r.push_back(VT(c));
253
                else
254
                {
255
                    ASSERT(c <= 0x10FFFFU);
256
                    c -= 0x10000U;
257
                    r.push_back(VT(u16(0xD800U | ((c >> 10) & 0x3FFU))));
258
                    r.push_back(VT(u16(0xDC00U | (c & 0x3FFU))));
259
                }
260
            }
261
 
262
            return r;
263
        }
264
 
265
        template <typename U16StrT>
266
        inline static std::u32string Utf16To32(U16StrT const & s)
267
        {
268
            static_assert(sizeof(typename U16StrT::value_type) == 2, "Char byte-size should be 2 for UTF-16 strings!");
269
            typedef uint16_t u16;
270
            std::u32string r;
271
            auto it = (u16 const *)s.c_str(), end = (u16 const *)(s.c_str() + s.length());
272
 
273
            while (it < end)
274
            {
275
                char32_t c = 0;
276
 
277
                if (*it < 0xD800U || *it > 0xDFFFU)
278
                {
279
                    c = *it;
280
                    ++it;
281
                }
282
                else if (*it >= 0xDC00U)
283
                {
284
                    ASSERT_MSG(false, "Unallowed UTF-16 sequence!");
285
                }
286
                else
287
                {
288
                    ASSERT(end - it >= 2);
289
                    c = (*it & 0x3FFU) << 10;
290
 
291
                    if ((it[1] < 0xDC00U) || (it[1] > 0xDFFFU))
292
                    {
293
                        ASSERT_MSG(false, "Unallowed UTF-16 sequence!");
294
                    }
295
                    else
296
                    {
297
                        c |= it[1] & 0x3FFU;
298
                        c += 0x10000U;
299
                    }
300
 
301
                    it += 2;
302
                }
303
 
304
                r.push_back(c);
305
            }
306
 
307
            return r;
308
        }
309
 
310
        template <typename StrT, size_t NumBytes = sizeof(typename StrT::value_type)> struct UtfHelper;
311
        template <typename StrT> struct UtfHelper<StrT, 1>
312
        {
313
            inline static std::u32string UtfTo32(StrT const & s) { return Utf8To32(s); }
314
            inline static StrT UtfFrom32(std::u32string const & s) { return Utf32To8<StrT>(s); }
315
        };
316
 
317
        template <typename StrT> struct UtfHelper<StrT, 2>
318
        {
319
            inline static std::u32string UtfTo32(StrT const & s) { return Utf16To32(s); }
320
            inline static StrT UtfFrom32(std::u32string const & s) { return Utf32To16<StrT>(s); }
321
        };
322
 
323
        template <typename StrT> struct UtfHelper<StrT, 4>
324
        {
325
            inline static std::u32string UtfTo32(StrT const & s)
326
            {
327
                return std::u32string((char32_t const *)(s.c_str()), (char32_t const *)(s.c_str() + s.length()));
328
            }
329
 
330
            inline static StrT UtfFrom32(std::u32string const & s)
331
            {
332
                return StrT((typename StrT::value_type const *)(s.c_str()),
333
                            (typename StrT::value_type const *)(s.c_str() + s.length()));
334
            }
335
        };
336
 
337
        template <typename StrT> inline static std::u32string UtfTo32(StrT const & s)
338
        {
339
            return UtfHelper<StrT>::UtfTo32(s);
340
        }
341
 
342
        template <typename StrT> inline static StrT UtfFrom32(std::u32string const & s)
343
        {
344
            return UtfHelper<StrT>::UtfFrom32(s);
345
        }
346
 
347
        template <typename StrToT, typename StrFromT> inline static StrToT UtfConv(StrFromT const & s)
348
        {
349
            return UtfFrom32<StrToT>(UtfTo32(s));
350
        }
351
 
352
    private:
353
        CharConvert() {}    // Never call this!
354
};
355
 
4 andreas 356
#endif  // __TRESOURCES_H__