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