Update rive-cpp to 2.0 version
[platform/core/uifw/rive-tizen.git] / submodule / skia / src / utils / SkUTF.h
1 // Copyright 2018 Google LLC.
2 // Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
3 #ifndef SkUTF_DEFINED
4 #define SkUTF_DEFINED
5
6 #include "include/core/SkTypes.h"
7
8 typedef int32_t SkUnichar;
9
10 namespace SkUTF {
11
12 /** Given a sequence of UTF-8 bytes, return the number of unicode codepoints.
13     If the sequence is invalid UTF-8, return -1.
14 */
15 SK_SPI int CountUTF8(const char* utf8, size_t byteLength);
16
17 /** Given a sequence of aligned UTF-16 characters in machine-endian form,
18     return the number of unicode codepoints.  If the sequence is invalid
19     UTF-16, return -1.
20 */
21 SK_SPI int CountUTF16(const uint16_t* utf16, size_t byteLength);
22
23 /** Given a sequence of aligned UTF-32 characters in machine-endian form,
24     return the number of unicode codepoints.  If the sequence is invalid
25     UTF-32, return -1.
26 */
27 SK_SPI int CountUTF32(const int32_t* utf32, size_t byteLength);
28
29 /** Given a sequence of UTF-8 bytes, return the first unicode codepoint.
30     The pointer will be incremented to point at the next codepoint's start.  If
31     invalid UTF-8 is encountered, set *ptr to end and return -1.
32 */
33 SK_SPI SkUnichar NextUTF8(const char** ptr, const char* end);
34
35 /** Given a sequence of aligned UTF-16 characters in machine-endian form,
36     return the first unicode codepoint.  The pointer will be incremented to
37     point at the next codepoint's start.  If invalid UTF-16 is encountered,
38     set *ptr to end and return -1.
39 */
40 SK_SPI SkUnichar NextUTF16(const uint16_t** ptr, const uint16_t* end);
41
42 /** Given a sequence of aligned UTF-32 characters in machine-endian form,
43     return the first unicode codepoint.  The pointer will be incremented to
44     point at the next codepoint's start.  If invalid UTF-32 is encountered,
45     set *ptr to end and return -1.
46 */
47 SK_SPI SkUnichar NextUTF32(const int32_t** ptr, const int32_t* end);
48
49 constexpr unsigned kMaxBytesInUTF8Sequence = 4;
50
51 /** Convert the unicode codepoint into UTF-8.  If `utf8` is non-null, place the
52     result in that array.  Return the number of bytes in the result.  If `utf8`
53     is null, simply return the number of bytes that would be used.  For invalid
54     unicode codepoints, return 0.
55 */
56 SK_SPI size_t ToUTF8(SkUnichar uni, char utf8[kMaxBytesInUTF8Sequence] = nullptr);
57
58 /** Convert the unicode codepoint into UTF-16.  If `utf16` is non-null, place
59     the result in that array.  Return the number of UTF-16 code units in the
60     result (1 or 2).  If `utf16` is null, simply return the number of code
61     units that would be used.  For invalid unicode codepoints, return 0.
62 */
63 SK_SPI size_t ToUTF16(SkUnichar uni, uint16_t utf16[2] = nullptr);
64
65 /** Returns the number of resulting UTF16 values needed to convert the src utf8 sequence.
66  *  If dst is not null, it is filled with the corresponding values up to its capacity.
67  *  If there is an error, -1 is returned and the dst[] buffer is undefined.
68  */
69 SK_SPI int UTF8ToUTF16(uint16_t dst[], int dstCapacity, const char src[], size_t srcByteLength);
70
71 /** Returns the number of resulting UTF8 values needed to convert the src utf16 sequence.
72  *  If dst is not null, it is filled with the corresponding values up to its capacity.
73  *  If there is an error, -1 is returned and the dst[] buffer is undefined.
74  */
75 SK_SPI int UTF16ToUTF8(char dst[], int dstCapacity, const uint16_t src[], size_t srcLength);
76
77 /**
78  * Given a UTF-16 code point, returns true iff it is a leading surrogate.
79  * https://unicode.org/faq/utf_bom.html#utf16-2
80  */
81 static inline bool IsLeadingSurrogateUTF16(uint16_t c) { return ((c) & 0xFC00) == 0xD800; }
82
83 /**
84  * Given a UTF-16 code point, returns true iff it is a trailing surrogate.
85  * https://unicode.org/faq/utf_bom.html#utf16-2
86  */
87 static inline bool IsTrailingSurrogateUTF16(uint16_t c) { return ((c) & 0xFC00) == 0xDC00; }
88
89
90 }  // namespace SkUTF
91
92 #endif  // SkUTF_DEFINED