HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
iso_15924_impl.hpp
1// Copyright Take Vos 2021-2022.
2// Distributed under the Boost Software License, Version 1.0.
3// (See accompanying file LICENSE_1_0.txt or copy at https://www.boost.org/LICENSE_1_0.txt)
4
5#pragma once
6
7#include "iso_15924_intf.hpp"
8#include "../utility/utility.hpp"
9#include "../algorithm/module.hpp"
10#include "../macros.hpp"
11#include <array>
12
13hi_export_module(hikogui.i18n.iso_15924 : impl);
14
15namespace hi { inline namespace v1 {
16namespace detail {
17
19 fixed_string<4> code4;
20 fixed_string<4> code4_open_type;
21 uint16_t number;
22
23 constexpr iso_15924_info(char const (&code4)[5], char const (&code4_open_type)[5], uint16_t number) noexcept :
24 code4{to_title(fixed_string(code4))}, code4_open_type{code4_open_type}, number(number)
25 {
26 }
27
28 constexpr iso_15924_info() noexcept : iso_15924_info("zzzz", 999) {}
29
30 constexpr iso_15924_info(char const (&code4)[5], uint16_t number) noexcept : iso_15924_info(code4, code4, number) {}
31};
32
33[[nodiscard]] consteval auto iso_15924_infos_init() noexcept
34{
35 // We are using a c-style array to std::array conversion because
36 // compilers, tools and analysers do not handle large std::array constructors.
37
38 // clang-format off
39 constexpr iso_15924_info data[] = {
40 {"adlm", 166},
41 {"afak", 439},
42 {"aghb", 239},
43 {"ahom", 338},
44 {"arab", 160},
45 {"aran", 161},
46 {"armi", 124},
47 {"armn", 230},
48 {"avst", 134},
49 {"bali", 360},
50 {"bamu", 435},
51 {"bass", 259},
52 {"batk", 365},
53 {"beng", "bng2", 325},
54 {"bhks", 334},
55 {"blis", 550},
56 {"bopo", 285},
57 {"brah", 300},
58 {"brai", 570},
59 {"bugi", 367},
60 {"buhd", 372},
61 {"cakm", 349},
62 {"cans", 440},
63 {"cari", 201},
64 {"cham", 358},
65 {"cher", 445},
66 {"chrs", 109},
67 {"cirt", 291},
68 {"copt", 204},
69 {"cpmn", 402},
70 {"cprt", 403},
71 {"cyrl", 220},
72 {"cyrs", 221},
73 {"deva", "dev2", 315},
74 {"diak", 342},
75 {"dogr", 328},
76 {"dsrt", 250},
77 {"dupl", 755},
78 {"egyd", 70},
79 {"egyh", 60},
80 {"egyp", 50},
81 {"elba", 226},
82 {"elym", 128},
83 {"ethi", 430},
84 {"geok", 241},
85 {"geor", 240},
86 {"glag", 225},
87 {"gong", 312},
88 {"gonm", 313},
89 {"goth", 206},
90 {"gran", 343},
91 {"grek", 200},
92 {"gujr", "gjr2", 320},
93 {"guru", "gur2", 310},
94 {"hanb", 503},
95 {"hang", 286},
96 {"hani", 500},
97 {"hano", 371},
98 {"hans", 501},
99 {"hant", 502},
100 {"hatr", 127},
101 {"hebr", 125},
102 {"hira", 410},
103 {"hluw", 80},
104 {"hmng", 450},
105 {"hmnp", 451},
106 {"hrkt", 412},
107 {"hung", 176},
108 {"inds", 610},
109 {"ital", 210},
110 {"jamo", 284},
111 {"java", 361},
112 {"jpan", 413},
113 {"jurc", 510},
114 {"kali", 357},
115 {"kana", 411},
116 {"khar", 305},
117 {"khmr", 355},
118 {"khoj", 322},
119 {"kitl", 505},
120 {"kits", 288},
121 {"knda", "knd2", 345},
122 {"kore", 287},
123 {"kpel", 436},
124 {"kthi", 317},
125 {"lana", 351},
126 {"laoo", "lao ", 356},
127 {"latf", 217},
128 {"latg", 216},
129 {"latn", 215},
130 {"leke", 364},
131 {"lepc", 335},
132 {"limb", 336},
133 {"lina", 400},
134 {"linb", 401},
135 {"lisu", 399},
136 {"loma", 437},
137 {"lyci", 202},
138 {"lydi", 116},
139 {"mahj", 314},
140 {"maka", 366},
141 {"mand", 140},
142 {"mani", 139},
143 {"marc", 332},
144 {"maya", 90},
145 {"medf", 265},
146 {"mend", 438},
147 {"merc", 101},
148 {"mero", 100},
149 {"mlym", "mlm2", 347},
150 {"modi", 324},
151 {"mong", 145},
152 {"moon", 218},
153 {"mroo", 264},
154 {"mtei", 337},
155 {"mult", 323},
156 {"mymr", "mym2", 350},
157 {"nand", 311},
158 {"narb", 106},
159 {"nbat", 159},
160 {"newa", 333},
161 {"nkdb", 85},
162 {"nkgb", 420},
163 {"nkoo", "nko ", 165},
164 {"nshu", 499},
165 {"ogam", 212},
166 {"olck", 261},
167 {"orkh", 175},
168 {"orya", "ory2", 327},
169 {"osge", 219},
170 {"osma", 260},
171 {"ougr", 143},
172 {"palm", 126},
173 {"pauc", 263},
174 {"pcun", 15},
175 {"pelm", 16},
176 {"perm", 227},
177 {"phag", 331},
178 {"phli", 131},
179 {"phlp", 132},
180 {"phlv", 133},
181 {"phnx", 115},
182 {"plrd", 282},
183 {"piqd", 293},
184 {"prti", 130},
185 {"psin", 103},
186 {"qaaa", 900},
187 {"qabv", "byzm", 947}, // Open-type
188 {"qabw", "musc", 948}, // Open-type
189 {"qabx", 949},
190 {"ranj", 303},
191 {"rjng", 363},
192 {"rohg", 167},
193 {"roro", 620},
194 {"runr", 211},
195 {"samr", 123},
196 {"sara", 292},
197 {"sarb", 105},
198 {"saur", 344},
199 {"sgnw", 95},
200 {"shaw", 281},
201 {"shrd", 319},
202 {"shui", 530},
203 {"sidd", 302},
204 {"sind", 318},
205 {"sinh", 348},
206 {"sogd", 141},
207 {"sogo", 142},
208 {"sora", 398},
209 {"soyo", 329},
210 {"sund", 362},
211 {"sylo", 316},
212 {"syrc", 135},
213 {"syre", 138},
214 {"syrj", 137},
215 {"syrn", 136},
216 {"tagb", 373},
217 {"takr", 321},
218 {"tale", 353},
219 {"talu", 354},
220 {"taml", "tml2", 346},
221 {"tang", 520},
222 {"tavt", 359},
223 {"telu", "tel2", 340},
224 {"teng", 290},
225 {"tfng", 120},
226 {"tglg", 370},
227 {"thaa", 170},
228 {"thai", 352},
229 {"tibt", 330},
230 {"tirh", 326},
231 {"tnsa", 275},
232 {"toto", 294},
233 {"ugar", 40},
234 {"vaii", "vai ", 470},
235 {"visp", 280},
236 {"vith", 228},
237 {"wara", 262},
238 {"wcho", 283},
239 {"wole", 480},
240 {"xpeo", 30},
241 {"xsux", 20},
242 {"yezi", 192},
243 {"yiii", "yi ", 460},
244 {"zanb", 339},
245 {"zinh", 994},
246 {"zmth", "math", 995},
247 {"zsye", 993},
248 {"zsym", 996},
249 {"zxxx", 997},
250 {"zyyy", "DFLT", 998},
251 {"zzzz", 999}};
252 // clang-format on
253
254 constexpr auto data_size = sizeof(data) / sizeof(data[0]);
255
257
258 for (auto i = 0_uz; i != data_size; ++i) {
259 r[i] = data[i];
260 }
261
262 return r;
263}
264
265constexpr auto iso_15924_infos = iso_15924_infos_init();
266
267[[nodiscard]] consteval auto iso_15924_code4_by_number_init() noexcept
268{
269 auto r = std::array<fixed_string<4>, 1000>{};
270
271 for (hilet& info : iso_15924_infos) {
272 r[info.number] = info.code4;
273 }
274
275 return r;
276}
277
278[[nodiscard]] consteval auto iso_15924_code4_open_type_by_number_init() noexcept
279{
280 auto r = std::array<fixed_string<4>, 1000>{};
281
282 for (hilet& info : iso_15924_infos) {
283 r[info.number] = info.code4_open_type;
284 }
285
286 return r;
287}
288
289[[nodiscard]] consteval auto iso_15924_number_by_code4_init() noexcept
290{
291 constexpr size_t array_size = std::tuple_size_v<decltype(iso_15924_infos)>;
292 using record_type = std::pair<fixed_string<4>, uint16_t>;
293
295 for (auto i = 0_uz; i != iso_15924_infos.size(); ++i) {
296 r[i] = {iso_15924_infos[i].code4, iso_15924_infos[i].number};
297 }
298 std::sort(r.begin(), r.end(), [](hilet& a, hilet& b) {
299 return a.first < b.first;
300 });
301
302 return r;
303}
304
305constexpr auto iso_15924_code4_by_number = iso_15924_code4_by_number_init();
306constexpr auto iso_15924_code4_open_type_by_number = iso_15924_code4_open_type_by_number_init();
307constexpr auto iso_15924_number_by_code4 = iso_15924_number_by_code4_init();
308
309} // namespace detail
310
311constexpr iso_15924::iso_15924(std::string_view code4)
312{
313 if (code4.size() != 4) {
314 throw parse_error(std::format("Invalid script '{}'", code4));
315 }
316
317 hilet code4_ = to_title(code4);
318
319 hilet it = std::lower_bound(
320 detail::iso_15924_number_by_code4.begin(),
321 detail::iso_15924_number_by_code4.end(),
322 code4_,
323 [](hilet& item, hilet& value) {
324 return item.first < value;
325 });
326
327 if (it == detail::iso_15924_number_by_code4.end() or it->first != code4_) {
328 throw parse_error(std::format("Unknown script '{}'", code4));
329 }
330
331 _v = it->second;
332}
333
335{
336 hi_assert(_v < 1000);
337 return detail::iso_15924_code4_by_number[_v];
338}
339
341{
342 hi_assert(_v < 1000);
343 return detail::iso_15924_code4_open_type_by_number[_v];
344}
345
347{
348 switch (_v) {
349 case 50: // Hyro
350 case 105: // Sarb
351 case 106: // Narb
352 case 115: // Phnx
353 case 116: // Lydi
354 case 123: // Samr
355 case 124: // Armi
356 case 125: // Hebr
357 case 126: // Palm
358 case 127: // Hatr
359 case 130: // Prti
360 case 131: // Phli
361 case 132: // Phlp
362 case 133: // Phlv
363 case 134: // Evst
364 case 135: // Syrc
365 case 136: // Syrn
366 case 137: // Syrj
367 case 138: // Syre
368 case 140: // Mand
369 case 141: // Sogd
370 case 142: // Sogo
371 case 159: // Nbat
372 case 160: // Arab
373 case 161: // Aran
374 case 165: // Nkoo
375 case 166: // Adlm
376 case 167: // Rohg
377 case 170: // Thaa
378 case 175: // Orkh
379 case 176: // Hung
380 case 192: // Yezi
381 case 210: // Ital
382 case 305: // Khar
383 case 403: // Cprt
384 case 438: // Mend
385 case 495: // Ethi
386 case 610: // Inds
387 return false;
388 default:
389 return true;
390 }
391}
392}} // namespace hi::v1
@ end
Start from the end of the file.
@ begin
Start from the beginning of the file.
DOXYGEN BUG.
Definition algorithm.hpp:16
constexpr std::string to_title(std::string_view rhs) noexcept
Convert the current string to using title case.
Definition strings.hpp:159
geometry/margins.hpp
Definition lookahead_iterator.hpp:5
constexpr Out narrow_cast(In const &rhs) noexcept
Cast numeric values without loss of precision.
Definition cast.hpp:377
Definition iso_15924_impl.hpp:18
constexpr bool left_to_right() const noexcept
Is this script written left-to-right.
Definition iso_15924_impl.hpp:346
constexpr std::string code4_open_type() const noexcept
Get the 4-letter code used by open-type.
Definition iso_15924_impl.hpp:340
constexpr std::string code4() const noexcept
Get the iso-15924 4-letter code.
Definition iso_15924_impl.hpp:334
A string which may be used as a none-type template parameter.
Definition fixed_string.hpp:41
T lower_bound(T... args)
T size(T... args)
T sort(T... args)