gosu 0.13.3 → 0.14.0.pre2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (80) hide show
  1. checksums.yaml +4 -4
  2. data/Gosu/Audio.hpp +15 -11
  3. data/Gosu/Font.hpp +24 -20
  4. data/Gosu/Fwd.hpp +1 -1
  5. data/Gosu/Graphics.hpp +8 -9
  6. data/Gosu/ImageData.hpp +1 -1
  7. data/Gosu/Input.hpp +1 -1
  8. data/Gosu/Math.hpp +0 -18
  9. data/Gosu/Text.hpp +22 -30
  10. data/Gosu/TextInput.hpp +13 -0
  11. data/Gosu/Utility.hpp +2 -0
  12. data/Gosu/Window.hpp +3 -3
  13. data/README.md +3 -4
  14. data/ext/gosu/extconf.rb +7 -9
  15. data/lib/gosu/swig_patches.rb +1 -4
  16. data/rdoc/gosu.rb +34 -9
  17. data/src/Audio.cpp +6 -6
  18. data/src/AudioImpl.cpp +2 -2
  19. data/src/Bitmap.cpp +1 -2
  20. data/src/BitmapIO.cpp +21 -2
  21. data/src/BlockAllocator.cpp +1 -1
  22. data/src/Channel.cpp +7 -1
  23. data/src/ClipRectStack.hpp +4 -1
  24. data/src/Color.cpp +2 -1
  25. data/src/DirectoriesWin.cpp +1 -1
  26. data/src/DrawOp.hpp +8 -4
  27. data/src/DrawOpQueue.hpp +13 -24
  28. data/src/FileUnix.cpp +3 -1
  29. data/src/Font.cpp +92 -96
  30. data/src/GosuGLView.cpp +59 -31
  31. data/src/GosuGLView.hpp +14 -0
  32. data/src/GosuViewController.cpp +21 -21
  33. data/src/{GosuViewController.h → GosuViewController.hpp} +2 -4
  34. data/src/Graphics.cpp +71 -38
  35. data/src/GraphicsImpl.hpp +12 -29
  36. data/src/Image.cpp +5 -7
  37. data/src/Input.cpp +7 -5
  38. data/src/InputUIKit.cpp +19 -37
  39. data/src/Macro.cpp +10 -2
  40. data/src/MarkupParser.cpp +241 -0
  41. data/src/MarkupParser.hpp +61 -0
  42. data/src/Math.cpp +1 -1
  43. data/src/OffScreenTarget.cpp +99 -0
  44. data/src/OffScreenTarget.hpp +23 -0
  45. data/src/OggFile.hpp +10 -0
  46. data/src/RenderState.hpp +0 -2
  47. data/src/Resolution.cpp +2 -2
  48. data/src/RubyGosu.cxx +457 -244
  49. data/src/TexChunk.cpp +8 -6
  50. data/src/Text.cpp +58 -345
  51. data/src/TextBuilder.cpp +138 -0
  52. data/src/TextBuilder.hpp +55 -0
  53. data/src/TextInput.cpp +27 -10
  54. data/src/Texture.cpp +22 -17
  55. data/src/Texture.hpp +19 -20
  56. data/src/TimingApple.cpp +5 -7
  57. data/src/TimingUnix.cpp +1 -4
  58. data/src/TimingWin.cpp +4 -1
  59. data/src/TrueTypeFont.cpp +282 -0
  60. data/src/TrueTypeFont.hpp +66 -0
  61. data/src/TrueTypeFontApple.cpp +65 -0
  62. data/src/TrueTypeFontUnix.cpp +91 -0
  63. data/src/TrueTypeFontWin.cpp +82 -0
  64. data/src/Utility.cpp +40 -0
  65. data/src/Window.cpp +7 -6
  66. data/src/WindowUIKit.cpp +9 -4
  67. data/src/stb_truetype.h +4589 -0
  68. data/src/utf8proc.c +755 -0
  69. data/src/utf8proc.h +699 -0
  70. data/src/utf8proc_data.h +14386 -0
  71. metadata +23 -16
  72. data/src/FormattedString.cpp +0 -237
  73. data/src/FormattedString.hpp +0 -47
  74. data/src/GosuAppDelegate.cpp +0 -30
  75. data/src/GosuAppDelegate.h +0 -8
  76. data/src/GosuGLView.h +0 -8
  77. data/src/TextApple.cpp +0 -212
  78. data/src/TextTTFWin.cpp +0 -197
  79. data/src/TextUnix.cpp +0 -280
  80. data/src/TextWin.cpp +0 -191
@@ -0,0 +1,755 @@
1
+ /* -*- mode: c; c-basic-offset: 2; tab-width: 2; indent-tabs-mode: nil -*- */
2
+ /*
3
+ * Copyright (c) 2015 Steven G. Johnson, Jiahao Chen, Peter Colberg, Tony Kelman, Scott P. Jones, and other contributors.
4
+ * Copyright (c) 2009 Public Software Group e. V., Berlin, Germany
5
+ *
6
+ * Permission is hereby granted, free of charge, to any person obtaining a
7
+ * copy of this software and associated documentation files (the "Software"),
8
+ * to deal in the Software without restriction, including without limitation
9
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10
+ * and/or sell copies of the Software, and to permit persons to whom the
11
+ * Software is furnished to do so, subject to the following conditions:
12
+ *
13
+ * The above copyright notice and this permission notice shall be included in
14
+ * all copies or substantial portions of the Software.
15
+ *
16
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22
+ * DEALINGS IN THE SOFTWARE.
23
+ */
24
+
25
+ /*
26
+ * This library contains derived data from a modified version of the
27
+ * Unicode data files.
28
+ *
29
+ * The original data files are available at
30
+ * http://www.unicode.org/Public/UNIDATA/
31
+ *
32
+ * Please notice the copyright statement in the file "utf8proc_data.c".
33
+ */
34
+
35
+
36
+ /*
37
+ * File name: utf8proc.c
38
+ *
39
+ * Description:
40
+ * Implementation of libutf8proc.
41
+ */
42
+
43
+
44
+ #include "utf8proc.h"
45
+ #include "utf8proc_data.h"
46
+
47
+
48
+ UTF8PROC_DLLEXPORT const utf8proc_int8_t utf8proc_utf8class[256] = {
49
+ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
50
+ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
51
+ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
52
+ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
53
+ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
54
+ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
55
+ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
56
+ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
57
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
58
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
59
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
60
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
61
+ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
62
+ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
63
+ 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
64
+ 4, 4, 4, 4, 4, 4, 4, 4, 0, 0, 0, 0, 0, 0, 0, 0 };
65
+
66
+ #define UTF8PROC_HANGUL_SBASE 0xAC00
67
+ #define UTF8PROC_HANGUL_LBASE 0x1100
68
+ #define UTF8PROC_HANGUL_VBASE 0x1161
69
+ #define UTF8PROC_HANGUL_TBASE 0x11A7
70
+ #define UTF8PROC_HANGUL_LCOUNT 19
71
+ #define UTF8PROC_HANGUL_VCOUNT 21
72
+ #define UTF8PROC_HANGUL_TCOUNT 28
73
+ #define UTF8PROC_HANGUL_NCOUNT 588
74
+ #define UTF8PROC_HANGUL_SCOUNT 11172
75
+ /* END is exclusive */
76
+ #define UTF8PROC_HANGUL_L_START 0x1100
77
+ #define UTF8PROC_HANGUL_L_END 0x115A
78
+ #define UTF8PROC_HANGUL_L_FILLER 0x115F
79
+ #define UTF8PROC_HANGUL_V_START 0x1160
80
+ #define UTF8PROC_HANGUL_V_END 0x11A3
81
+ #define UTF8PROC_HANGUL_T_START 0x11A8
82
+ #define UTF8PROC_HANGUL_T_END 0x11FA
83
+ #define UTF8PROC_HANGUL_S_START 0xAC00
84
+ #define UTF8PROC_HANGUL_S_END 0xD7A4
85
+
86
+ /* Should follow semantic-versioning rules (semver.org) based on API
87
+ compatibility. (Note that the shared-library version number will
88
+ be different, being based on ABI compatibility.): */
89
+ #define STRINGIZEx(x) #x
90
+ #define STRINGIZE(x) STRINGIZEx(x)
91
+ UTF8PROC_DLLEXPORT const char *utf8proc_version(void) {
92
+ return STRINGIZE(UTF8PROC_VERSION_MAJOR) "." STRINGIZE(UTF8PROC_VERSION_MINOR) "." STRINGIZE(UTF8PROC_VERSION_PATCH) "";
93
+ }
94
+
95
+ UTF8PROC_DLLEXPORT const char *utf8proc_errmsg(utf8proc_ssize_t errcode) {
96
+ switch (errcode) {
97
+ case UTF8PROC_ERROR_NOMEM:
98
+ return "Memory for processing UTF-8 data could not be allocated.";
99
+ case UTF8PROC_ERROR_OVERFLOW:
100
+ return "UTF-8 string is too long to be processed.";
101
+ case UTF8PROC_ERROR_INVALIDUTF8:
102
+ return "Invalid UTF-8 string";
103
+ case UTF8PROC_ERROR_NOTASSIGNED:
104
+ return "Unassigned Unicode code point found in UTF-8 string.";
105
+ case UTF8PROC_ERROR_INVALIDOPTS:
106
+ return "Invalid options for UTF-8 processing chosen.";
107
+ default:
108
+ return "An unknown error occurred while processing UTF-8 data.";
109
+ }
110
+ }
111
+
112
+ #define utf_cont(ch) (((ch) & 0xc0) == 0x80)
113
+ UTF8PROC_DLLEXPORT utf8proc_ssize_t utf8proc_iterate(
114
+ const utf8proc_uint8_t *str, utf8proc_ssize_t strlen, utf8proc_int32_t *dst
115
+ ) {
116
+ utf8proc_uint32_t uc;
117
+ const utf8proc_uint8_t *end;
118
+
119
+ *dst = -1;
120
+ if (!strlen) return 0;
121
+ end = str + ((strlen < 0) ? 4 : strlen);
122
+ uc = *str++;
123
+ if (uc < 0x80) {
124
+ *dst = uc;
125
+ return 1;
126
+ }
127
+ // Must be between 0xc2 and 0xf4 inclusive to be valid
128
+ if ((uc - 0xc2) > (0xf4-0xc2)) return UTF8PROC_ERROR_INVALIDUTF8;
129
+ if (uc < 0xe0) { // 2-byte sequence
130
+ // Must have valid continuation character
131
+ if (str >= end || !utf_cont(*str)) return UTF8PROC_ERROR_INVALIDUTF8;
132
+ *dst = ((uc & 0x1f)<<6) | (*str & 0x3f);
133
+ return 2;
134
+ }
135
+ if (uc < 0xf0) { // 3-byte sequence
136
+ if ((str + 1 >= end) || !utf_cont(*str) || !utf_cont(str[1]))
137
+ return UTF8PROC_ERROR_INVALIDUTF8;
138
+ // Check for surrogate chars
139
+ if (uc == 0xed && *str > 0x9f)
140
+ return UTF8PROC_ERROR_INVALIDUTF8;
141
+ uc = ((uc & 0xf)<<12) | ((*str & 0x3f)<<6) | (str[1] & 0x3f);
142
+ if (uc < 0x800)
143
+ return UTF8PROC_ERROR_INVALIDUTF8;
144
+ *dst = uc;
145
+ return 3;
146
+ }
147
+ // 4-byte sequence
148
+ // Must have 3 valid continuation characters
149
+ if ((str + 2 >= end) || !utf_cont(*str) || !utf_cont(str[1]) || !utf_cont(str[2]))
150
+ return UTF8PROC_ERROR_INVALIDUTF8;
151
+ // Make sure in correct range (0x10000 - 0x10ffff)
152
+ if (uc == 0xf0) {
153
+ if (*str < 0x90) return UTF8PROC_ERROR_INVALIDUTF8;
154
+ } else if (uc == 0xf4) {
155
+ if (*str > 0x8f) return UTF8PROC_ERROR_INVALIDUTF8;
156
+ }
157
+ *dst = ((uc & 7)<<18) | ((*str & 0x3f)<<12) | ((str[1] & 0x3f)<<6) | (str[2] & 0x3f);
158
+ return 4;
159
+ }
160
+
161
+ UTF8PROC_DLLEXPORT utf8proc_bool utf8proc_codepoint_valid(utf8proc_int32_t uc) {
162
+ return (((utf8proc_uint32_t)uc)-0xd800 > 0x07ff) && ((utf8proc_uint32_t)uc < 0x110000);
163
+ }
164
+
165
+ UTF8PROC_DLLEXPORT utf8proc_ssize_t utf8proc_encode_char(utf8proc_int32_t uc, utf8proc_uint8_t *dst) {
166
+ if (uc < 0x00) {
167
+ return 0;
168
+ } else if (uc < 0x80) {
169
+ dst[0] = (utf8proc_uint8_t) uc;
170
+ return 1;
171
+ } else if (uc < 0x800) {
172
+ dst[0] = (utf8proc_uint8_t)(0xC0 + (uc >> 6));
173
+ dst[1] = (utf8proc_uint8_t)(0x80 + (uc & 0x3F));
174
+ return 2;
175
+ // Note: we allow encoding 0xd800-0xdfff here, so as not to change
176
+ // the API, however, these are actually invalid in UTF-8
177
+ } else if (uc < 0x10000) {
178
+ dst[0] = (utf8proc_uint8_t)(0xE0 + (uc >> 12));
179
+ dst[1] = (utf8proc_uint8_t)(0x80 + ((uc >> 6) & 0x3F));
180
+ dst[2] = (utf8proc_uint8_t)(0x80 + (uc & 0x3F));
181
+ return 3;
182
+ } else if (uc < 0x110000) {
183
+ dst[0] = (utf8proc_uint8_t)(0xF0 + (uc >> 18));
184
+ dst[1] = (utf8proc_uint8_t)(0x80 + ((uc >> 12) & 0x3F));
185
+ dst[2] = (utf8proc_uint8_t)(0x80 + ((uc >> 6) & 0x3F));
186
+ dst[3] = (utf8proc_uint8_t)(0x80 + (uc & 0x3F));
187
+ return 4;
188
+ } else return 0;
189
+ }
190
+
191
+ /* internal "unsafe" version that does not check whether uc is in range */
192
+ static utf8proc_ssize_t unsafe_encode_char(utf8proc_int32_t uc, utf8proc_uint8_t *dst) {
193
+ if (uc < 0x00) {
194
+ return 0;
195
+ } else if (uc < 0x80) {
196
+ dst[0] = (utf8proc_uint8_t)uc;
197
+ return 1;
198
+ } else if (uc < 0x800) {
199
+ dst[0] = (utf8proc_uint8_t)(0xC0 + (uc >> 6));
200
+ dst[1] = (utf8proc_uint8_t)(0x80 + (uc & 0x3F));
201
+ return 2;
202
+ } else if (uc == 0xFFFF) {
203
+ dst[0] = (utf8proc_uint8_t)0xFF;
204
+ return 1;
205
+ } else if (uc == 0xFFFE) {
206
+ dst[0] = (utf8proc_uint8_t)0xFE;
207
+ return 1;
208
+ } else if (uc < 0x10000) {
209
+ dst[0] = (utf8proc_uint8_t)(0xE0 + (uc >> 12));
210
+ dst[1] = (utf8proc_uint8_t)(0x80 + ((uc >> 6) & 0x3F));
211
+ dst[2] = (utf8proc_uint8_t)(0x80 + (uc & 0x3F));
212
+ return 3;
213
+ } else if (uc < 0x110000) {
214
+ dst[0] = (utf8proc_uint8_t)(0xF0 + (uc >> 18));
215
+ dst[1] = (utf8proc_uint8_t)(0x80 + ((uc >> 12) & 0x3F));
216
+ dst[2] = (utf8proc_uint8_t)(0x80 + ((uc >> 6) & 0x3F));
217
+ dst[3] = (utf8proc_uint8_t)(0x80 + (uc & 0x3F));
218
+ return 4;
219
+ } else return 0;
220
+ }
221
+
222
+ /* internal "unsafe" version that does not check whether uc is in range */
223
+ static const utf8proc_property_t *unsafe_get_property(utf8proc_int32_t uc) {
224
+ /* ASSERT: uc >= 0 && uc < 0x110000 */
225
+ return utf8proc_properties + (
226
+ utf8proc_stage2table[
227
+ utf8proc_stage1table[uc >> 8] + (uc & 0xFF)
228
+ ]
229
+ );
230
+ }
231
+
232
+ UTF8PROC_DLLEXPORT const utf8proc_property_t *utf8proc_get_property(utf8proc_int32_t uc) {
233
+ return uc < 0 || uc >= 0x110000 ? utf8proc_properties : unsafe_get_property(uc);
234
+ }
235
+
236
+ /* return whether there is a grapheme break between boundclasses lbc and tbc
237
+ (according to the definition of extended grapheme clusters)
238
+
239
+ Rule numbering refers to TR29 Version 29 (Unicode 9.0.0):
240
+ http://www.unicode.org/reports/tr29/tr29-29.html
241
+
242
+ CAVEATS:
243
+ Please note that evaluation of GB10 (grapheme breaks between emoji zwj sequences)
244
+ and GB 12/13 (regional indicator code points) require knowledge of previous characters
245
+ and are thus not handled by this function. This may result in an incorrect break before
246
+ an E_Modifier class codepoint and an incorrectly missing break between two
247
+ REGIONAL_INDICATOR class code points if such support does not exist in the caller.
248
+
249
+ See the special support in grapheme_break_extended, for required bookkeeping by the caller.
250
+ */
251
+ static utf8proc_bool grapheme_break_simple(int lbc, int tbc) {
252
+ return
253
+ (lbc == UTF8PROC_BOUNDCLASS_START) ? true : // GB1
254
+ (lbc == UTF8PROC_BOUNDCLASS_CR && // GB3
255
+ tbc == UTF8PROC_BOUNDCLASS_LF) ? false : // ---
256
+ (lbc >= UTF8PROC_BOUNDCLASS_CR && lbc <= UTF8PROC_BOUNDCLASS_CONTROL) ? true : // GB4
257
+ (tbc >= UTF8PROC_BOUNDCLASS_CR && tbc <= UTF8PROC_BOUNDCLASS_CONTROL) ? true : // GB5
258
+ (lbc == UTF8PROC_BOUNDCLASS_L && // GB6
259
+ (tbc == UTF8PROC_BOUNDCLASS_L || // ---
260
+ tbc == UTF8PROC_BOUNDCLASS_V || // ---
261
+ tbc == UTF8PROC_BOUNDCLASS_LV || // ---
262
+ tbc == UTF8PROC_BOUNDCLASS_LVT)) ? false : // ---
263
+ ((lbc == UTF8PROC_BOUNDCLASS_LV || // GB7
264
+ lbc == UTF8PROC_BOUNDCLASS_V) && // ---
265
+ (tbc == UTF8PROC_BOUNDCLASS_V || // ---
266
+ tbc == UTF8PROC_BOUNDCLASS_T)) ? false : // ---
267
+ ((lbc == UTF8PROC_BOUNDCLASS_LVT || // GB8
268
+ lbc == UTF8PROC_BOUNDCLASS_T) && // ---
269
+ tbc == UTF8PROC_BOUNDCLASS_T) ? false : // ---
270
+ (tbc == UTF8PROC_BOUNDCLASS_EXTEND || // GB9
271
+ tbc == UTF8PROC_BOUNDCLASS_ZWJ || // ---
272
+ tbc == UTF8PROC_BOUNDCLASS_SPACINGMARK || // GB9a
273
+ lbc == UTF8PROC_BOUNDCLASS_PREPEND) ? false : // GB9b
274
+ ((lbc == UTF8PROC_BOUNDCLASS_E_BASE || // GB10 (requires additional handling below)
275
+ lbc == UTF8PROC_BOUNDCLASS_E_BASE_GAZ) && // ----
276
+ tbc == UTF8PROC_BOUNDCLASS_E_MODIFIER) ? false : // ----
277
+ (lbc == UTF8PROC_BOUNDCLASS_ZWJ && // GB11
278
+ (tbc == UTF8PROC_BOUNDCLASS_GLUE_AFTER_ZWJ || // ----
279
+ tbc == UTF8PROC_BOUNDCLASS_E_BASE_GAZ)) ? false : // ----
280
+ (lbc == UTF8PROC_BOUNDCLASS_REGIONAL_INDICATOR && // GB12/13 (requires additional handling below)
281
+ tbc == UTF8PROC_BOUNDCLASS_REGIONAL_INDICATOR) ? false : // ----
282
+ true; // GB999
283
+ }
284
+
285
+ static utf8proc_bool grapheme_break_extended(int lbc, int tbc, utf8proc_int32_t *state)
286
+ {
287
+ int lbc_override = lbc;
288
+ if (state && *state != UTF8PROC_BOUNDCLASS_START)
289
+ lbc_override = *state;
290
+ utf8proc_bool break_permitted = grapheme_break_simple(lbc_override, tbc);
291
+ if (state) {
292
+ // Special support for GB 12/13 made possible by GB999. After two RI
293
+ // class codepoints we want to force a break. Do this by resetting the
294
+ // second RI's bound class to UTF8PROC_BOUNDCLASS_OTHER, to force a break
295
+ // after that character according to GB999 (unless of course such a break is
296
+ // forbidden by a different rule such as GB9).
297
+ if (*state == tbc && tbc == UTF8PROC_BOUNDCLASS_REGIONAL_INDICATOR)
298
+ *state = UTF8PROC_BOUNDCLASS_OTHER;
299
+ // Special support for GB10. Fold any EXTEND codepoints into the previous
300
+ // boundclass if we're dealing with an emoji base boundclass.
301
+ else if ((*state == UTF8PROC_BOUNDCLASS_E_BASE ||
302
+ *state == UTF8PROC_BOUNDCLASS_E_BASE_GAZ) &&
303
+ tbc == UTF8PROC_BOUNDCLASS_EXTEND)
304
+ *state = UTF8PROC_BOUNDCLASS_E_BASE;
305
+ else
306
+ *state = tbc;
307
+ }
308
+ return break_permitted;
309
+ }
310
+
311
+ UTF8PROC_DLLEXPORT utf8proc_bool utf8proc_grapheme_break_stateful(
312
+ utf8proc_int32_t c1, utf8proc_int32_t c2, utf8proc_int32_t *state) {
313
+
314
+ return grapheme_break_extended(utf8proc_get_property(c1)->boundclass,
315
+ utf8proc_get_property(c2)->boundclass,
316
+ state);
317
+ }
318
+
319
+
320
+ UTF8PROC_DLLEXPORT utf8proc_bool utf8proc_grapheme_break(
321
+ utf8proc_int32_t c1, utf8proc_int32_t c2) {
322
+ return utf8proc_grapheme_break_stateful(c1, c2, NULL);
323
+ }
324
+
325
+ static utf8proc_int32_t seqindex_decode_entry(const utf8proc_uint16_t **entry)
326
+ {
327
+ utf8proc_int32_t entry_cp = **entry;
328
+ if ((entry_cp & 0xF800) == 0xD800) {
329
+ *entry = *entry + 1;
330
+ entry_cp = ((entry_cp & 0x03FF) << 10) | (**entry & 0x03FF);
331
+ entry_cp += 0x10000;
332
+ }
333
+ return entry_cp;
334
+ }
335
+
336
+ static utf8proc_int32_t seqindex_decode_index(const utf8proc_uint32_t seqindex)
337
+ {
338
+ const utf8proc_uint16_t *entry = &utf8proc_sequences[seqindex];
339
+ return seqindex_decode_entry(&entry);
340
+ }
341
+
342
+ static utf8proc_ssize_t seqindex_write_char_decomposed(utf8proc_uint16_t seqindex, utf8proc_int32_t *dst, utf8proc_ssize_t bufsize, utf8proc_option_t options, int *last_boundclass) {
343
+ utf8proc_ssize_t written = 0;
344
+ const utf8proc_uint16_t *entry = &utf8proc_sequences[seqindex & 0x1FFF];
345
+ int len = seqindex >> 13;
346
+ if (len >= 7) {
347
+ len = *entry;
348
+ entry++;
349
+ }
350
+ for (; len >= 0; entry++, len--) {
351
+ utf8proc_int32_t entry_cp = seqindex_decode_entry(&entry);
352
+
353
+ written += utf8proc_decompose_char(entry_cp, dst+written,
354
+ (bufsize > written) ? (bufsize - written) : 0, options,
355
+ last_boundclass);
356
+ if (written < 0) return UTF8PROC_ERROR_OVERFLOW;
357
+ }
358
+ return written;
359
+ }
360
+
361
+ UTF8PROC_DLLEXPORT utf8proc_int32_t utf8proc_tolower(utf8proc_int32_t c)
362
+ {
363
+ utf8proc_int32_t cl = utf8proc_get_property(c)->lowercase_seqindex;
364
+ return cl != UINT16_MAX ? seqindex_decode_index(cl) : c;
365
+ }
366
+
367
+ UTF8PROC_DLLEXPORT utf8proc_int32_t utf8proc_toupper(utf8proc_int32_t c)
368
+ {
369
+ utf8proc_int32_t cu = utf8proc_get_property(c)->uppercase_seqindex;
370
+ return cu != UINT16_MAX ? seqindex_decode_index(cu) : c;
371
+ }
372
+
373
+ UTF8PROC_DLLEXPORT utf8proc_int32_t utf8proc_totitle(utf8proc_int32_t c)
374
+ {
375
+ utf8proc_int32_t cu = utf8proc_get_property(c)->titlecase_seqindex;
376
+ return cu != UINT16_MAX ? seqindex_decode_index(cu) : c;
377
+ }
378
+
379
+ /* return a character width analogous to wcwidth (except portable and
380
+ hopefully less buggy than most system wcwidth functions). */
381
+ UTF8PROC_DLLEXPORT int utf8proc_charwidth(utf8proc_int32_t c) {
382
+ return utf8proc_get_property(c)->charwidth;
383
+ }
384
+
385
+ UTF8PROC_DLLEXPORT utf8proc_category_t utf8proc_category(utf8proc_int32_t c) {
386
+ return utf8proc_get_property(c)->category;
387
+ }
388
+
389
+ UTF8PROC_DLLEXPORT const char *utf8proc_category_string(utf8proc_int32_t c) {
390
+ static const char s[][3] = {"Cn","Lu","Ll","Lt","Lm","Lo","Mn","Mc","Me","Nd","Nl","No","Pc","Pd","Ps","Pe","Pi","Pf","Po","Sm","Sc","Sk","So","Zs","Zl","Zp","Cc","Cf","Cs","Co"};
391
+ return s[utf8proc_category(c)];
392
+ }
393
+
394
+ #define utf8proc_decompose_lump(replacement_uc) \
395
+ return utf8proc_decompose_char((replacement_uc), dst, bufsize, \
396
+ options & ~UTF8PROC_LUMP, last_boundclass)
397
+
398
+ UTF8PROC_DLLEXPORT utf8proc_ssize_t utf8proc_decompose_char(utf8proc_int32_t uc, utf8proc_int32_t *dst, utf8proc_ssize_t bufsize, utf8proc_option_t options, int *last_boundclass) {
399
+ const utf8proc_property_t *property;
400
+ utf8proc_propval_t category;
401
+ utf8proc_int32_t hangul_sindex;
402
+ if (uc < 0 || uc >= 0x110000) return UTF8PROC_ERROR_NOTASSIGNED;
403
+ property = unsafe_get_property(uc);
404
+ category = property->category;
405
+ hangul_sindex = uc - UTF8PROC_HANGUL_SBASE;
406
+ if (options & (UTF8PROC_COMPOSE|UTF8PROC_DECOMPOSE)) {
407
+ if (hangul_sindex >= 0 && hangul_sindex < UTF8PROC_HANGUL_SCOUNT) {
408
+ utf8proc_int32_t hangul_tindex;
409
+ if (bufsize >= 1) {
410
+ dst[0] = UTF8PROC_HANGUL_LBASE +
411
+ hangul_sindex / UTF8PROC_HANGUL_NCOUNT;
412
+ if (bufsize >= 2) dst[1] = UTF8PROC_HANGUL_VBASE +
413
+ (hangul_sindex % UTF8PROC_HANGUL_NCOUNT) / UTF8PROC_HANGUL_TCOUNT;
414
+ }
415
+ hangul_tindex = hangul_sindex % UTF8PROC_HANGUL_TCOUNT;
416
+ if (!hangul_tindex) return 2;
417
+ if (bufsize >= 3) dst[2] = UTF8PROC_HANGUL_TBASE + hangul_tindex;
418
+ return 3;
419
+ }
420
+ }
421
+ if (options & UTF8PROC_REJECTNA) {
422
+ if (!category) return UTF8PROC_ERROR_NOTASSIGNED;
423
+ }
424
+ if (options & UTF8PROC_IGNORE) {
425
+ if (property->ignorable) return 0;
426
+ }
427
+ if (options & UTF8PROC_LUMP) {
428
+ if (category == UTF8PROC_CATEGORY_ZS) utf8proc_decompose_lump(0x0020);
429
+ if (uc == 0x2018 || uc == 0x2019 || uc == 0x02BC || uc == 0x02C8)
430
+ utf8proc_decompose_lump(0x0027);
431
+ if (category == UTF8PROC_CATEGORY_PD || uc == 0x2212)
432
+ utf8proc_decompose_lump(0x002D);
433
+ if (uc == 0x2044 || uc == 0x2215) utf8proc_decompose_lump(0x002F);
434
+ if (uc == 0x2236) utf8proc_decompose_lump(0x003A);
435
+ if (uc == 0x2039 || uc == 0x2329 || uc == 0x3008)
436
+ utf8proc_decompose_lump(0x003C);
437
+ if (uc == 0x203A || uc == 0x232A || uc == 0x3009)
438
+ utf8proc_decompose_lump(0x003E);
439
+ if (uc == 0x2216) utf8proc_decompose_lump(0x005C);
440
+ if (uc == 0x02C4 || uc == 0x02C6 || uc == 0x2038 || uc == 0x2303)
441
+ utf8proc_decompose_lump(0x005E);
442
+ if (category == UTF8PROC_CATEGORY_PC || uc == 0x02CD)
443
+ utf8proc_decompose_lump(0x005F);
444
+ if (uc == 0x02CB) utf8proc_decompose_lump(0x0060);
445
+ if (uc == 0x2223) utf8proc_decompose_lump(0x007C);
446
+ if (uc == 0x223C) utf8proc_decompose_lump(0x007E);
447
+ if ((options & UTF8PROC_NLF2LS) && (options & UTF8PROC_NLF2PS)) {
448
+ if (category == UTF8PROC_CATEGORY_ZL ||
449
+ category == UTF8PROC_CATEGORY_ZP)
450
+ utf8proc_decompose_lump(0x000A);
451
+ }
452
+ }
453
+ if (options & UTF8PROC_STRIPMARK) {
454
+ if (category == UTF8PROC_CATEGORY_MN ||
455
+ category == UTF8PROC_CATEGORY_MC ||
456
+ category == UTF8PROC_CATEGORY_ME) return 0;
457
+ }
458
+ if (options & UTF8PROC_CASEFOLD) {
459
+ if (property->casefold_seqindex != UINT16_MAX) {
460
+ return seqindex_write_char_decomposed(property->casefold_seqindex, dst, bufsize, options, last_boundclass);
461
+ }
462
+ }
463
+ if (options & (UTF8PROC_COMPOSE|UTF8PROC_DECOMPOSE)) {
464
+ if (property->decomp_seqindex != UINT16_MAX &&
465
+ (!property->decomp_type || (options & UTF8PROC_COMPAT))) {
466
+ return seqindex_write_char_decomposed(property->decomp_seqindex, dst, bufsize, options, last_boundclass);
467
+ }
468
+ }
469
+ if (options & UTF8PROC_CHARBOUND) {
470
+ utf8proc_bool boundary;
471
+ int tbc = property->boundclass;
472
+ boundary = grapheme_break_extended(*last_boundclass, tbc, last_boundclass);
473
+ if (boundary) {
474
+ if (bufsize >= 1) dst[0] = 0xFFFF;
475
+ if (bufsize >= 2) dst[1] = uc;
476
+ return 2;
477
+ }
478
+ }
479
+ if (bufsize >= 1) *dst = uc;
480
+ return 1;
481
+ }
482
+
483
+ UTF8PROC_DLLEXPORT utf8proc_ssize_t utf8proc_decompose(
484
+ const utf8proc_uint8_t *str, utf8proc_ssize_t strlen,
485
+ utf8proc_int32_t *buffer, utf8proc_ssize_t bufsize, utf8proc_option_t options
486
+ ) {
487
+ return utf8proc_decompose_custom(str, strlen, buffer, bufsize, options, NULL, NULL);
488
+ }
489
+
490
+ UTF8PROC_DLLEXPORT utf8proc_ssize_t utf8proc_decompose_custom(
491
+ const utf8proc_uint8_t *str, utf8proc_ssize_t strlen,
492
+ utf8proc_int32_t *buffer, utf8proc_ssize_t bufsize, utf8proc_option_t options,
493
+ utf8proc_custom_func custom_func, void *custom_data
494
+ ) {
495
+ /* strlen will be ignored, if UTF8PROC_NULLTERM is set in options */
496
+ utf8proc_ssize_t wpos = 0;
497
+ if ((options & UTF8PROC_COMPOSE) && (options & UTF8PROC_DECOMPOSE))
498
+ return UTF8PROC_ERROR_INVALIDOPTS;
499
+ if ((options & UTF8PROC_STRIPMARK) &&
500
+ !(options & UTF8PROC_COMPOSE) && !(options & UTF8PROC_DECOMPOSE))
501
+ return UTF8PROC_ERROR_INVALIDOPTS;
502
+ {
503
+ utf8proc_int32_t uc;
504
+ utf8proc_ssize_t rpos = 0;
505
+ utf8proc_ssize_t decomp_result;
506
+ int boundclass = UTF8PROC_BOUNDCLASS_START;
507
+ while (1) {
508
+ if (options & UTF8PROC_NULLTERM) {
509
+ rpos += utf8proc_iterate(str + rpos, -1, &uc);
510
+ /* checking of return value is not necessary,
511
+ as 'uc' is < 0 in case of error */
512
+ if (uc < 0) return UTF8PROC_ERROR_INVALIDUTF8;
513
+ if (rpos < 0) return UTF8PROC_ERROR_OVERFLOW;
514
+ if (uc == 0) break;
515
+ } else {
516
+ if (rpos >= strlen) break;
517
+ rpos += utf8proc_iterate(str + rpos, strlen - rpos, &uc);
518
+ if (uc < 0) return UTF8PROC_ERROR_INVALIDUTF8;
519
+ }
520
+ if (custom_func != NULL) {
521
+ uc = custom_func(uc, custom_data); /* user-specified custom mapping */
522
+ }
523
+ decomp_result = utf8proc_decompose_char(
524
+ uc, buffer + wpos, (bufsize > wpos) ? (bufsize - wpos) : 0, options,
525
+ &boundclass
526
+ );
527
+ if (decomp_result < 0) return decomp_result;
528
+ wpos += decomp_result;
529
+ /* prohibiting integer overflows due to too long strings: */
530
+ if (wpos < 0 ||
531
+ wpos > (utf8proc_ssize_t)(SSIZE_MAX/sizeof(utf8proc_int32_t)/2))
532
+ return UTF8PROC_ERROR_OVERFLOW;
533
+ }
534
+ }
535
+ if ((options & (UTF8PROC_COMPOSE|UTF8PROC_DECOMPOSE)) && bufsize >= wpos) {
536
+ utf8proc_ssize_t pos = 0;
537
+ while (pos < wpos-1) {
538
+ utf8proc_int32_t uc1, uc2;
539
+ const utf8proc_property_t *property1, *property2;
540
+ uc1 = buffer[pos];
541
+ uc2 = buffer[pos+1];
542
+ property1 = unsafe_get_property(uc1);
543
+ property2 = unsafe_get_property(uc2);
544
+ if (property1->combining_class > property2->combining_class &&
545
+ property2->combining_class > 0) {
546
+ buffer[pos] = uc2;
547
+ buffer[pos+1] = uc1;
548
+ if (pos > 0) pos--; else pos++;
549
+ } else {
550
+ pos++;
551
+ }
552
+ }
553
+ }
554
+ return wpos;
555
+ }
556
+
557
+ UTF8PROC_DLLEXPORT utf8proc_ssize_t utf8proc_normalize_utf32(utf8proc_int32_t *buffer, utf8proc_ssize_t length, utf8proc_option_t options) {
558
+ /* UTF8PROC_NULLTERM option will be ignored, 'length' is never ignored */
559
+ if (options & (UTF8PROC_NLF2LS | UTF8PROC_NLF2PS | UTF8PROC_STRIPCC)) {
560
+ utf8proc_ssize_t rpos;
561
+ utf8proc_ssize_t wpos = 0;
562
+ utf8proc_int32_t uc;
563
+ for (rpos = 0; rpos < length; rpos++) {
564
+ uc = buffer[rpos];
565
+ if (uc == 0x000D && rpos < length-1 && buffer[rpos+1] == 0x000A) rpos++;
566
+ if (uc == 0x000A || uc == 0x000D || uc == 0x0085 ||
567
+ ((options & UTF8PROC_STRIPCC) && (uc == 0x000B || uc == 0x000C))) {
568
+ if (options & UTF8PROC_NLF2LS) {
569
+ if (options & UTF8PROC_NLF2PS) {
570
+ buffer[wpos++] = 0x000A;
571
+ } else {
572
+ buffer[wpos++] = 0x2028;
573
+ }
574
+ } else {
575
+ if (options & UTF8PROC_NLF2PS) {
576
+ buffer[wpos++] = 0x2029;
577
+ } else {
578
+ buffer[wpos++] = 0x0020;
579
+ }
580
+ }
581
+ } else if ((options & UTF8PROC_STRIPCC) &&
582
+ (uc < 0x0020 || (uc >= 0x007F && uc < 0x00A0))) {
583
+ if (uc == 0x0009) buffer[wpos++] = 0x0020;
584
+ } else {
585
+ buffer[wpos++] = uc;
586
+ }
587
+ }
588
+ length = wpos;
589
+ }
590
+ if (options & UTF8PROC_COMPOSE) {
591
+ utf8proc_int32_t *starter = NULL;
592
+ utf8proc_int32_t current_char;
593
+ const utf8proc_property_t *starter_property = NULL, *current_property;
594
+ utf8proc_propval_t max_combining_class = -1;
595
+ utf8proc_ssize_t rpos;
596
+ utf8proc_ssize_t wpos = 0;
597
+ utf8proc_int32_t composition;
598
+ for (rpos = 0; rpos < length; rpos++) {
599
+ current_char = buffer[rpos];
600
+ current_property = unsafe_get_property(current_char);
601
+ if (starter && current_property->combining_class > max_combining_class) {
602
+ /* combination perhaps possible */
603
+ utf8proc_int32_t hangul_lindex;
604
+ utf8proc_int32_t hangul_sindex;
605
+ hangul_lindex = *starter - UTF8PROC_HANGUL_LBASE;
606
+ if (hangul_lindex >= 0 && hangul_lindex < UTF8PROC_HANGUL_LCOUNT) {
607
+ utf8proc_int32_t hangul_vindex;
608
+ hangul_vindex = current_char - UTF8PROC_HANGUL_VBASE;
609
+ if (hangul_vindex >= 0 && hangul_vindex < UTF8PROC_HANGUL_VCOUNT) {
610
+ *starter = UTF8PROC_HANGUL_SBASE +
611
+ (hangul_lindex * UTF8PROC_HANGUL_VCOUNT + hangul_vindex) *
612
+ UTF8PROC_HANGUL_TCOUNT;
613
+ starter_property = NULL;
614
+ continue;
615
+ }
616
+ }
617
+ hangul_sindex = *starter - UTF8PROC_HANGUL_SBASE;
618
+ if (hangul_sindex >= 0 && hangul_sindex < UTF8PROC_HANGUL_SCOUNT &&
619
+ (hangul_sindex % UTF8PROC_HANGUL_TCOUNT) == 0) {
620
+ utf8proc_int32_t hangul_tindex;
621
+ hangul_tindex = current_char - UTF8PROC_HANGUL_TBASE;
622
+ if (hangul_tindex >= 0 && hangul_tindex < UTF8PROC_HANGUL_TCOUNT) {
623
+ *starter += hangul_tindex;
624
+ starter_property = NULL;
625
+ continue;
626
+ }
627
+ }
628
+ if (!starter_property) {
629
+ starter_property = unsafe_get_property(*starter);
630
+ }
631
+ if (starter_property->comb_index < 0x8000 &&
632
+ current_property->comb_index != UINT16_MAX &&
633
+ current_property->comb_index >= 0x8000) {
634
+ int sidx = starter_property->comb_index;
635
+ int idx = (current_property->comb_index & 0x3FFF) - utf8proc_combinations[sidx];
636
+ if (idx >= 0 && idx <= utf8proc_combinations[sidx + 1] ) {
637
+ idx += sidx + 2;
638
+ if (current_property->comb_index & 0x4000) {
639
+ composition = (utf8proc_combinations[idx] << 16) | utf8proc_combinations[idx+1];
640
+ } else
641
+ composition = utf8proc_combinations[idx];
642
+
643
+ if (composition > 0 && (!(options & UTF8PROC_STABLE) ||
644
+ !(unsafe_get_property(composition)->comp_exclusion))) {
645
+ *starter = composition;
646
+ starter_property = NULL;
647
+ continue;
648
+ }
649
+ }
650
+ }
651
+ }
652
+ buffer[wpos] = current_char;
653
+ if (current_property->combining_class) {
654
+ if (current_property->combining_class > max_combining_class) {
655
+ max_combining_class = current_property->combining_class;
656
+ }
657
+ } else {
658
+ starter = buffer + wpos;
659
+ starter_property = NULL;
660
+ max_combining_class = -1;
661
+ }
662
+ wpos++;
663
+ }
664
+ length = wpos;
665
+ }
666
+ return length;
667
+ }
668
+
669
+ UTF8PROC_DLLEXPORT utf8proc_ssize_t utf8proc_reencode(utf8proc_int32_t *buffer, utf8proc_ssize_t length, utf8proc_option_t options) {
670
+ /* UTF8PROC_NULLTERM option will be ignored, 'length' is never ignored
671
+ ASSERT: 'buffer' has one spare byte of free space at the end! */
672
+ length = utf8proc_normalize_utf32(buffer, length, options);
673
+ if (length < 0) return length;
674
+ {
675
+ utf8proc_ssize_t rpos, wpos = 0;
676
+ utf8proc_int32_t uc;
677
+ if (options & UTF8PROC_CHARBOUND) {
678
+ for (rpos = 0; rpos < length; rpos++) {
679
+ uc = buffer[rpos];
680
+ wpos += unsafe_encode_char(uc, ((utf8proc_uint8_t *)buffer) + wpos);
681
+ }
682
+ } else {
683
+ for (rpos = 0; rpos < length; rpos++) {
684
+ uc = buffer[rpos];
685
+ wpos += utf8proc_encode_char(uc, ((utf8proc_uint8_t *)buffer) + wpos);
686
+ }
687
+ }
688
+ ((utf8proc_uint8_t *)buffer)[wpos] = 0;
689
+ return wpos;
690
+ }
691
+ }
692
+
693
+ UTF8PROC_DLLEXPORT utf8proc_ssize_t utf8proc_map(
694
+ const utf8proc_uint8_t *str, utf8proc_ssize_t strlen, utf8proc_uint8_t **dstptr, utf8proc_option_t options
695
+ ) {
696
+ return utf8proc_map_custom(str, strlen, dstptr, options, NULL, NULL);
697
+ }
698
+
699
+ UTF8PROC_DLLEXPORT utf8proc_ssize_t utf8proc_map_custom(
700
+ const utf8proc_uint8_t *str, utf8proc_ssize_t strlen, utf8proc_uint8_t **dstptr, utf8proc_option_t options,
701
+ utf8proc_custom_func custom_func, void *custom_data
702
+ ) {
703
+ utf8proc_int32_t *buffer;
704
+ utf8proc_ssize_t result;
705
+ *dstptr = NULL;
706
+ result = utf8proc_decompose_custom(str, strlen, NULL, 0, options, custom_func, custom_data);
707
+ if (result < 0) return result;
708
+ buffer = (utf8proc_int32_t *) malloc(result * sizeof(utf8proc_int32_t) + 1);
709
+ if (!buffer) return UTF8PROC_ERROR_NOMEM;
710
+ result = utf8proc_decompose_custom(str, strlen, buffer, result, options, custom_func, custom_data);
711
+ if (result < 0) {
712
+ free(buffer);
713
+ return result;
714
+ }
715
+ result = utf8proc_reencode(buffer, result, options);
716
+ if (result < 0) {
717
+ free(buffer);
718
+ return result;
719
+ }
720
+ {
721
+ utf8proc_int32_t *newptr;
722
+ newptr = (utf8proc_int32_t *) realloc(buffer, (size_t)result+1);
723
+ if (newptr) buffer = newptr;
724
+ }
725
+ *dstptr = (utf8proc_uint8_t *)buffer;
726
+ return result;
727
+ }
728
+
729
+ UTF8PROC_DLLEXPORT utf8proc_uint8_t *utf8proc_NFD(const utf8proc_uint8_t *str) {
730
+ utf8proc_uint8_t *retval;
731
+ utf8proc_map(str, 0, &retval, UTF8PROC_NULLTERM | UTF8PROC_STABLE |
732
+ UTF8PROC_DECOMPOSE);
733
+ return retval;
734
+ }
735
+
736
+ UTF8PROC_DLLEXPORT utf8proc_uint8_t *utf8proc_NFC(const utf8proc_uint8_t *str) {
737
+ utf8proc_uint8_t *retval;
738
+ utf8proc_map(str, 0, &retval, UTF8PROC_NULLTERM | UTF8PROC_STABLE |
739
+ UTF8PROC_COMPOSE);
740
+ return retval;
741
+ }
742
+
743
+ UTF8PROC_DLLEXPORT utf8proc_uint8_t *utf8proc_NFKD(const utf8proc_uint8_t *str) {
744
+ utf8proc_uint8_t *retval;
745
+ utf8proc_map(str, 0, &retval, UTF8PROC_NULLTERM | UTF8PROC_STABLE |
746
+ UTF8PROC_DECOMPOSE | UTF8PROC_COMPAT);
747
+ return retval;
748
+ }
749
+
750
+ UTF8PROC_DLLEXPORT utf8proc_uint8_t *utf8proc_NFKC(const utf8proc_uint8_t *str) {
751
+ utf8proc_uint8_t *retval;
752
+ utf8proc_map(str, 0, &retval, UTF8PROC_NULLTERM | UTF8PROC_STABLE |
753
+ UTF8PROC_COMPOSE | UTF8PROC_COMPAT);
754
+ return retval;
755
+ }