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