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);
|
71 |
andreas |
113 |
bool endsWith (const std::string &src, const std::string &end);
|
|
|
114 |
bool startsWith (const std::string &src, const std::string &start);
|
|
|
115 |
std::string dirName (const std::string &path);
|
|
|
116 |
std::string baseName (const std::string &path);
|
97 |
andreas |
117 |
char *strnstr(const char *haystack, const char *needle, size_t len);
|
104 |
andreas |
118 |
std::string getCommand(const std::string& fullCmd);
|
222 |
andreas |
119 |
bool StrContains(const std::string& str, const std::string& part);
|
233 |
andreas |
120 |
bool isTrue(const std::string& value);
|
|
|
121 |
bool isFalse(const std::string& value);
|
|
|
122 |
bool isNumeric(const std::string& str, bool blank=false);
|
260 |
andreas |
123 |
bool isBigEndian();
|
271 |
andreas |
124 |
std::string handleToString(ulong handle);
|
297 |
andreas |
125 |
ulong extractHandle(const std::string& obname);
|
21 |
andreas |
126 |
|
73 |
andreas |
127 |
static inline std::string <rim(std::string &s)
|
|
|
128 |
{
|
|
|
129 |
s.erase(s.begin(), std::find_if(s.begin(), s.end(), [](int c) {return !std::isspace(c);}));
|
|
|
130 |
return s;
|
|
|
131 |
}
|
|
|
132 |
|
|
|
133 |
static inline std::string &rtrim(std::string &str)
|
|
|
134 |
{
|
|
|
135 |
str.erase(str.find_last_not_of(" \n\r\t")+1);
|
|
|
136 |
return str;
|
|
|
137 |
}
|
|
|
138 |
|
|
|
139 |
static inline std::string& trim(std::string& str) { return ltrim(rtrim(str)); }
|
|
|
140 |
|
162 |
andreas |
141 |
#define ASSERT_MSG(cond, msg) { if (!(cond)) throw std::runtime_error("Assertion (" #cond ") failed at line " + std::to_string(__LINE__) + "! Msg: " + std::string(msg)); }
|
|
|
142 |
#define ASSERT(cond) ASSERT_MSG(cond, "")
|
|
|
143 |
|
|
|
144 |
class CharConvert
|
|
|
145 |
{
|
|
|
146 |
public:
|
|
|
147 |
template <typename U8StrT = std::string>
|
|
|
148 |
inline static U8StrT Utf32To8(std::u32string const & s)
|
|
|
149 |
{
|
|
|
150 |
static_assert(sizeof(typename U8StrT::value_type) == 1, "Char byte-size should be 1 for UTF-8 strings!");
|
|
|
151 |
typedef typename U8StrT::value_type VT;
|
|
|
152 |
typedef uint8_t u8;
|
|
|
153 |
U8StrT r;
|
|
|
154 |
|
|
|
155 |
for (auto c: s)
|
|
|
156 |
{
|
|
|
157 |
size_t nby = c <= 0x7FU ? 1 : c <= 0x7FFU ? 2 : c <= 0xFFFFU ? 3 : c <= 0x1FFFFFU ? 4 : c <= 0x3FFFFFFU ? 5 : c <= 0x7FFFFFFFU ? 6 : 7;
|
|
|
158 |
r.push_back(VT(
|
|
|
159 |
nby <= 1 ? u8(c) : (
|
|
|
160 |
(u8(0xFFU) << (8 - nby)) |
|
|
|
161 |
u8(c >> (6 * (nby - 1)))
|
|
|
162 |
)
|
|
|
163 |
));
|
|
|
164 |
|
|
|
165 |
for (size_t i = 1; i < nby; ++i)
|
|
|
166 |
r.push_back(VT(u8(0x80U | (u8(0x3FU) & u8(c >> (6 * (nby - 1 - i)))))));
|
|
|
167 |
}
|
|
|
168 |
|
|
|
169 |
return r;
|
|
|
170 |
}
|
|
|
171 |
|
|
|
172 |
template <typename U8StrT>
|
|
|
173 |
inline static std::u32string Utf8To32(U8StrT const & s)
|
|
|
174 |
{
|
|
|
175 |
static_assert(sizeof(typename U8StrT::value_type) == 1, "Char byte-size should be 1 for UTF-8 strings!");
|
|
|
176 |
typedef uint8_t u8;
|
|
|
177 |
std::u32string r;
|
|
|
178 |
auto it = (u8 const *)s.c_str(), end = (u8 const *)(s.c_str() + s.length());
|
|
|
179 |
|
|
|
180 |
while (it < end)
|
|
|
181 |
{
|
|
|
182 |
char32_t c = 0;
|
|
|
183 |
if (*it <= 0x7FU)
|
|
|
184 |
{
|
|
|
185 |
c = *it;
|
|
|
186 |
++it;
|
|
|
187 |
}
|
|
|
188 |
else
|
|
|
189 |
{
|
|
|
190 |
ASSERT((*it & 0xC0U) == 0xC0U);
|
|
|
191 |
size_t nby = 0;
|
|
|
192 |
|
|
|
193 |
for (u8 b = *it; (b & 0x80U) != 0; b <<= 1, ++nby) { (void)0; }
|
|
|
194 |
ASSERT(nby <= 7);
|
|
|
195 |
ASSERT((size_t)(end - it) >= nby);
|
|
|
196 |
c = *it & (u8(0xFFU) >> (nby + 1));
|
|
|
197 |
|
|
|
198 |
for (size_t i = 1; i < nby; ++i)
|
|
|
199 |
{
|
|
|
200 |
ASSERT((it[i] & 0xC0U) == 0x80U);
|
|
|
201 |
c = (c << 6) | (it[i] & 0x3FU);
|
|
|
202 |
}
|
|
|
203 |
|
|
|
204 |
it += nby;
|
|
|
205 |
}
|
|
|
206 |
|
|
|
207 |
r.push_back(c);
|
|
|
208 |
}
|
|
|
209 |
|
|
|
210 |
return r;
|
|
|
211 |
}
|
|
|
212 |
|
|
|
213 |
|
|
|
214 |
template <typename U16StrT = std::u16string>
|
|
|
215 |
inline static U16StrT Utf32To16(std::u32string const & s)
|
|
|
216 |
{
|
|
|
217 |
static_assert(sizeof(typename U16StrT::value_type) == 2, "Char byte-size should be 2 for UTF-16 strings!");
|
|
|
218 |
typedef typename U16StrT::value_type VT;
|
|
|
219 |
typedef uint16_t u16;
|
|
|
220 |
U16StrT r;
|
|
|
221 |
|
|
|
222 |
for (auto c: s)
|
|
|
223 |
{
|
|
|
224 |
if (c <= 0xFFFFU)
|
|
|
225 |
r.push_back(VT(c));
|
|
|
226 |
else
|
|
|
227 |
{
|
|
|
228 |
ASSERT(c <= 0x10FFFFU);
|
|
|
229 |
c -= 0x10000U;
|
|
|
230 |
r.push_back(VT(u16(0xD800U | ((c >> 10) & 0x3FFU))));
|
|
|
231 |
r.push_back(VT(u16(0xDC00U | (c & 0x3FFU))));
|
|
|
232 |
}
|
|
|
233 |
}
|
|
|
234 |
|
|
|
235 |
return r;
|
|
|
236 |
}
|
|
|
237 |
|
|
|
238 |
template <typename U16StrT>
|
|
|
239 |
inline static std::u32string Utf16To32(U16StrT const & s)
|
|
|
240 |
{
|
|
|
241 |
static_assert(sizeof(typename U16StrT::value_type) == 2, "Char byte-size should be 2 for UTF-16 strings!");
|
|
|
242 |
typedef uint16_t u16;
|
|
|
243 |
std::u32string r;
|
|
|
244 |
auto it = (u16 const *)s.c_str(), end = (u16 const *)(s.c_str() + s.length());
|
|
|
245 |
|
|
|
246 |
while (it < end)
|
|
|
247 |
{
|
|
|
248 |
char32_t c = 0;
|
|
|
249 |
|
|
|
250 |
if (*it < 0xD800U || *it > 0xDFFFU)
|
|
|
251 |
{
|
|
|
252 |
c = *it;
|
|
|
253 |
++it;
|
|
|
254 |
}
|
|
|
255 |
else if (*it >= 0xDC00U)
|
|
|
256 |
{
|
|
|
257 |
ASSERT_MSG(false, "Unallowed UTF-16 sequence!");
|
|
|
258 |
}
|
|
|
259 |
else
|
|
|
260 |
{
|
|
|
261 |
ASSERT(end - it >= 2);
|
|
|
262 |
c = (*it & 0x3FFU) << 10;
|
|
|
263 |
|
|
|
264 |
if ((it[1] < 0xDC00U) || (it[1] > 0xDFFFU))
|
|
|
265 |
{
|
|
|
266 |
ASSERT_MSG(false, "Unallowed UTF-16 sequence!");
|
|
|
267 |
}
|
|
|
268 |
else
|
|
|
269 |
{
|
|
|
270 |
c |= it[1] & 0x3FFU;
|
|
|
271 |
c += 0x10000U;
|
|
|
272 |
}
|
|
|
273 |
|
|
|
274 |
it += 2;
|
|
|
275 |
}
|
|
|
276 |
|
|
|
277 |
r.push_back(c);
|
|
|
278 |
}
|
|
|
279 |
|
|
|
280 |
return r;
|
|
|
281 |
}
|
|
|
282 |
|
|
|
283 |
template <typename StrT, size_t NumBytes = sizeof(typename StrT::value_type)> struct UtfHelper;
|
|
|
284 |
template <typename StrT> struct UtfHelper<StrT, 1>
|
|
|
285 |
{
|
|
|
286 |
inline static std::u32string UtfTo32(StrT const & s) { return Utf8To32(s); }
|
|
|
287 |
inline static StrT UtfFrom32(std::u32string const & s) { return Utf32To8<StrT>(s); }
|
|
|
288 |
};
|
|
|
289 |
|
|
|
290 |
template <typename StrT> struct UtfHelper<StrT, 2>
|
|
|
291 |
{
|
|
|
292 |
inline static std::u32string UtfTo32(StrT const & s) { return Utf16To32(s); }
|
|
|
293 |
inline static StrT UtfFrom32(std::u32string const & s) { return Utf32To16<StrT>(s); }
|
|
|
294 |
};
|
|
|
295 |
|
|
|
296 |
template <typename StrT> struct UtfHelper<StrT, 4>
|
|
|
297 |
{
|
|
|
298 |
inline static std::u32string UtfTo32(StrT const & s)
|
|
|
299 |
{
|
|
|
300 |
return std::u32string((char32_t const *)(s.c_str()), (char32_t const *)(s.c_str() + s.length()));
|
|
|
301 |
}
|
|
|
302 |
|
|
|
303 |
inline static StrT UtfFrom32(std::u32string const & s)
|
|
|
304 |
{
|
|
|
305 |
return StrT((typename StrT::value_type const *)(s.c_str()),
|
|
|
306 |
(typename StrT::value_type const *)(s.c_str() + s.length()));
|
|
|
307 |
}
|
|
|
308 |
};
|
|
|
309 |
|
|
|
310 |
template <typename StrT> inline static std::u32string UtfTo32(StrT const & s)
|
|
|
311 |
{
|
|
|
312 |
return UtfHelper<StrT>::UtfTo32(s);
|
|
|
313 |
}
|
|
|
314 |
|
|
|
315 |
template <typename StrT> inline static StrT UtfFrom32(std::u32string const & s)
|
|
|
316 |
{
|
|
|
317 |
return UtfHelper<StrT>::UtfFrom32(s);
|
|
|
318 |
}
|
|
|
319 |
|
|
|
320 |
template <typename StrToT, typename StrFromT> inline static StrToT UtfConv(StrFromT const & s)
|
|
|
321 |
{
|
|
|
322 |
return UtfFrom32<StrToT>(UtfTo32(s));
|
|
|
323 |
}
|
|
|
324 |
|
|
|
325 |
private:
|
|
|
326 |
CharConvert() {} // Never call this!
|
|
|
327 |
};
|
|
|
328 |
|
4 |
andreas |
329 |
#endif // __TRESOURCES_H__
|