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>
66 andreas 32
 
226 andreas 33
#ifdef __MACH__
34
#include <skia/core/SkImage.h>
35
#include <skia/core/SkString.h>
36
#include <skia/core/SkData.h>
365 andreas 37
#include <skia/core/SkColor.h>
226 andreas 38
#else
4 andreas 39
#include <include/core/SkImage.h>
40
#include <include/core/SkString.h>
41
#include <include/core/SkData.h>
365 andreas 42
#include <include/core/SkColor.h>
226 andreas 43
#endif
4 andreas 44
 
271 andreas 45
//#include "terror.h"
162 andreas 46
 
271 andreas 47
#ifndef ulong
48
typedef unsigned long ulong;
49
#endif
50
 
4 andreas 51
class SkBitmap;
52
class SkData;
53
class SkStreamAsset;
57 andreas 54
class SkFont;
4 andreas 55
class SkTypeface;
56
 
156 andreas 57
typedef enum _RESOURCE_TYPE
58
{
59
    RESTYPE_UNKNOWN,
60
    RESTYPE_IMAGE,
61
    RESTYPE_SYSIMAGE,
62
    RESTYPE_FONT,
63
    RESTYPE_SYSFONT,
64
    RESTYPE_CURSOR,
65
    RESTYPE_SYSCURSOR,
66
    RESTYPE_BORDER,
67
    RESTYPE_SYSBORDER,
68
    RESTYPE_SLIDER,
69
    RESTYPE_SYSSLIDER
70
}_RESOURCE_TYPE;
4 andreas 71
 
156 andreas 72
SkString GetResourcePath(const char* resource = "", _RESOURCE_TYPE rs = RESTYPE_IMAGE);
73
 
4 andreas 74
bool DecodeDataToBitmap(sk_sp<SkData> data, SkBitmap* dst);
75
 
156 andreas 76
sk_sp<SkData> GetResourceAsData(const char* resource, _RESOURCE_TYPE rs = RESTYPE_IMAGE);
4 andreas 77
 
78
inline bool GetResourceAsBitmap(const char* resource, SkBitmap* dst)
79
{
80
    return DecodeDataToBitmap(GetResourceAsData(resource), dst);
81
}
82
 
83
inline sk_sp<SkImage> GetResourceAsImage(const char* resource)
84
{
365 andreas 85
    return SkImages::DeferredFromEncodedData(GetResourceAsData(resource));
4 andreas 86
}
87
 
156 andreas 88
std::unique_ptr<SkStreamAsset> GetResourceAsStream(const char* resource, _RESOURCE_TYPE rs = RESTYPE_IMAGE);
4 andreas 89
 
163 andreas 90
sk_sp<SkTypeface> MakeResourceAsTypeface(const char* resource, int ttcIndex = 0, _RESOURCE_TYPE rs = RESTYPE_FONT);
4 andreas 91
 
6 andreas 92
sk_sp<SkData> readImage(const std::string& fname);
254 andreas 93
SkBitmap *allocPixels(int width, int height, SkBitmap *bm);
94
SkColor reverseColor(const SkColor& col);
6 andreas 95
 
21 andreas 96
std::string toLower(std::string& str);
97
std::string toUpper(std::string& str);
11 andreas 98
std::vector<std::string> StrSplit(const std::string& str, const std::string& seps, const bool trimEmpty=false);
99
std::string UTF8ToCp1250(const std::string& str);
100
std::string cp1250ToUTF8(const std::string& str);
101
std::string latin1ToUTF8(const std::string& str);
263 andreas 102
std::string intToString(int num);
268 andreas 103
std::string ReplaceString(const std::string subject, const std::string& search, const std::string& replace);
11 andreas 104
 
93 andreas 105
void *renew(char **mem, size_t old_size, size_t new_size);
54 andreas 106
std::vector<std::string> splitLine(const std::string& str);
57 andreas 107
std::vector<std::string> splitLine(const std::string& str, int width, int height, SkFont& font, SkPaint& paint);
60 andreas 108
bool isHex(int c);
109
int strCaseCompare(const std::string& str1, const std::string& str2);
63 andreas 110
std::string fillString(int c, int len);
67 andreas 111
bool isUTF8(const std::string& str);
112
size_t utf8Strlen(const std::string& str);
113
uint16_t getUint16(const unsigned char *p, bool big_endian=false);
114
uint32_t getUint32(const unsigned char *p, bool big_endian=false);
361 andreas 115
std::string UnicodeToUTF8(const std::wstring& ws);
71 andreas 116
bool endsWith (const std::string &src, const std::string &end);
117
bool startsWith (const std::string &src, const std::string &start);
118
std::string dirName (const std::string &path);
119
std::string baseName (const std::string &path);
97 andreas 120
char *strnstr(const char *haystack, const char *needle, size_t len);
104 andreas 121
std::string getCommand(const std::string& fullCmd);
222 andreas 122
bool StrContains(const std::string& str, const std::string& part);
233 andreas 123
bool isTrue(const std::string& value);
124
bool isFalse(const std::string& value);
125
bool isNumeric(const std::string& str, bool blank=false);
260 andreas 126
bool isBigEndian();
271 andreas 127
std::string handleToString(ulong handle);
297 andreas 128
ulong extractHandle(const std::string& obname);
21 andreas 129
 
73 andreas 130
static inline std::string &ltrim(std::string &s)
131
{
132
    s.erase(s.begin(), std::find_if(s.begin(), s.end(), [](int c) {return !std::isspace(c);}));
133
    return s;
134
}
135
 
136
static inline std::string &rtrim(std::string &str)
137
{
138
    str.erase(str.find_last_not_of(" \n\r\t")+1);
139
    return str;
140
}
141
 
142
static inline std::string& trim(std::string& str) { return ltrim(rtrim(str)); }
143
 
162 andreas 144
#define ASSERT_MSG(cond, msg) { if (!(cond)) throw std::runtime_error("Assertion (" #cond ") failed at line " + std::to_string(__LINE__) + "! Msg: " + std::string(msg)); }
145
#define ASSERT(cond) ASSERT_MSG(cond, "")
146
 
147
class CharConvert
148
{
149
    public:
150
        template <typename U8StrT = std::string>
151
        inline static U8StrT Utf32To8(std::u32string const & s)
152
        {
153
            static_assert(sizeof(typename U8StrT::value_type) == 1, "Char byte-size should be 1 for UTF-8 strings!");
154
            typedef typename U8StrT::value_type VT;
155
            typedef uint8_t u8;
156
            U8StrT r;
157
 
158
            for (auto c: s)
159
            {
160
                size_t nby = c <= 0x7FU ? 1 : c <= 0x7FFU ? 2 : c <= 0xFFFFU ? 3 : c <= 0x1FFFFFU ? 4 : c <= 0x3FFFFFFU ? 5 : c <= 0x7FFFFFFFU ? 6 : 7;
161
                r.push_back(VT(
162
                    nby <= 1 ? u8(c) : (
163
                        (u8(0xFFU) << (8 - nby)) |
164
                        u8(c >> (6 * (nby - 1)))
165
                    )
166
                ));
167
 
168
                for (size_t i = 1; i < nby; ++i)
169
                    r.push_back(VT(u8(0x80U | (u8(0x3FU) & u8(c >> (6 * (nby - 1 - i)))))));
170
            }
171
 
172
            return r;
173
        }
174
 
175
        template <typename U8StrT>
176
        inline static std::u32string Utf8To32(U8StrT const & s)
177
        {
178
            static_assert(sizeof(typename U8StrT::value_type) == 1, "Char byte-size should be 1 for UTF-8 strings!");
179
            typedef uint8_t u8;
180
            std::u32string r;
181
            auto it = (u8 const *)s.c_str(), end = (u8 const *)(s.c_str() + s.length());
182
 
183
            while (it < end)
184
            {
185
                char32_t c = 0;
186
                if (*it <= 0x7FU)
187
                {
188
                    c = *it;
189
                    ++it;
190
                }
191
                else
192
                {
193
                    ASSERT((*it & 0xC0U) == 0xC0U);
194
                    size_t nby = 0;
195
 
196
                    for (u8 b = *it; (b & 0x80U) != 0; b <<= 1, ++nby) { (void)0; }
197
                    ASSERT(nby <= 7);
198
                    ASSERT((size_t)(end - it) >= nby);
199
                    c = *it & (u8(0xFFU) >> (nby + 1));
200
 
201
                    for (size_t i = 1; i < nby; ++i)
202
                    {
203
                        ASSERT((it[i] & 0xC0U) == 0x80U);
204
                        c = (c << 6) | (it[i] & 0x3FU);
205
                    }
206
 
207
                    it += nby;
208
                }
209
 
210
                r.push_back(c);
211
            }
212
 
213
            return r;
214
        }
215
 
216
 
217
        template <typename U16StrT = std::u16string>
218
        inline static U16StrT Utf32To16(std::u32string const & s)
219
        {
220
            static_assert(sizeof(typename U16StrT::value_type) == 2, "Char byte-size should be 2 for UTF-16 strings!");
221
            typedef typename U16StrT::value_type VT;
222
            typedef uint16_t u16;
223
            U16StrT r;
224
 
225
            for (auto c: s)
226
            {
227
                if (c <= 0xFFFFU)
228
                    r.push_back(VT(c));
229
                else
230
                {
231
                    ASSERT(c <= 0x10FFFFU);
232
                    c -= 0x10000U;
233
                    r.push_back(VT(u16(0xD800U | ((c >> 10) & 0x3FFU))));
234
                    r.push_back(VT(u16(0xDC00U | (c & 0x3FFU))));
235
                }
236
            }
237
 
238
            return r;
239
        }
240
 
241
        template <typename U16StrT>
242
        inline static std::u32string Utf16To32(U16StrT const & s)
243
        {
244
            static_assert(sizeof(typename U16StrT::value_type) == 2, "Char byte-size should be 2 for UTF-16 strings!");
245
            typedef uint16_t u16;
246
            std::u32string r;
247
            auto it = (u16 const *)s.c_str(), end = (u16 const *)(s.c_str() + s.length());
248
 
249
            while (it < end)
250
            {
251
                char32_t c = 0;
252
 
253
                if (*it < 0xD800U || *it > 0xDFFFU)
254
                {
255
                    c = *it;
256
                    ++it;
257
                }
258
                else if (*it >= 0xDC00U)
259
                {
260
                    ASSERT_MSG(false, "Unallowed UTF-16 sequence!");
261
                }
262
                else
263
                {
264
                    ASSERT(end - it >= 2);
265
                    c = (*it & 0x3FFU) << 10;
266
 
267
                    if ((it[1] < 0xDC00U) || (it[1] > 0xDFFFU))
268
                    {
269
                        ASSERT_MSG(false, "Unallowed UTF-16 sequence!");
270
                    }
271
                    else
272
                    {
273
                        c |= it[1] & 0x3FFU;
274
                        c += 0x10000U;
275
                    }
276
 
277
                    it += 2;
278
                }
279
 
280
                r.push_back(c);
281
            }
282
 
283
            return r;
284
        }
285
 
286
        template <typename StrT, size_t NumBytes = sizeof(typename StrT::value_type)> struct UtfHelper;
287
        template <typename StrT> struct UtfHelper<StrT, 1>
288
        {
289
            inline static std::u32string UtfTo32(StrT const & s) { return Utf8To32(s); }
290
            inline static StrT UtfFrom32(std::u32string const & s) { return Utf32To8<StrT>(s); }
291
        };
292
 
293
        template <typename StrT> struct UtfHelper<StrT, 2>
294
        {
295
            inline static std::u32string UtfTo32(StrT const & s) { return Utf16To32(s); }
296
            inline static StrT UtfFrom32(std::u32string const & s) { return Utf32To16<StrT>(s); }
297
        };
298
 
299
        template <typename StrT> struct UtfHelper<StrT, 4>
300
        {
301
            inline static std::u32string UtfTo32(StrT const & s)
302
            {
303
                return std::u32string((char32_t const *)(s.c_str()), (char32_t const *)(s.c_str() + s.length()));
304
            }
305
 
306
            inline static StrT UtfFrom32(std::u32string const & s)
307
            {
308
                return StrT((typename StrT::value_type const *)(s.c_str()),
309
                            (typename StrT::value_type const *)(s.c_str() + s.length()));
310
            }
311
        };
312
 
313
        template <typename StrT> inline static std::u32string UtfTo32(StrT const & s)
314
        {
315
            return UtfHelper<StrT>::UtfTo32(s);
316
        }
317
 
318
        template <typename StrT> inline static StrT UtfFrom32(std::u32string const & s)
319
        {
320
            return UtfHelper<StrT>::UtfFrom32(s);
321
        }
322
 
323
        template <typename StrToT, typename StrFromT> inline static StrToT UtfConv(StrFromT const & s)
324
        {
325
            return UtfFrom32<StrToT>(UtfTo32(s));
326
        }
327
 
328
    private:
329
        CharConvert() {}    // Never call this!
330
};
331
 
4 andreas 332
#endif  // __TRESOURCES_H__