1 /*
2 * Copyright 2006 The Android Open Source Project
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8 #ifndef SkUtils_DEFINED
9 #define SkUtils_DEFINED
10
11 #include "SkTypes.h"
12 #include "SkMath.h"
13
14 /** Similar to memset(), but it assigns a 16, 32, or 64-bit value into the buffer.
15 @param buffer The memory to have value copied into it
16 @param value The value to be copied into buffer
17 @param count The number of times value should be copied into the buffer.
18 */
19 void sk_memset16(uint16_t buffer[], uint16_t value, int count);
20 void sk_memset32(uint32_t buffer[], uint32_t value, int count);
21 void sk_memset64(uint64_t buffer[], uint64_t value, int count);
22 ///////////////////////////////////////////////////////////////////////////////
23
24 #define kMaxBytesInUTF8Sequence 4
25
26 #ifdef SK_DEBUG
27 int SkUTF8_LeadByteToCount(unsigned c);
28 #else
29 #define SkUTF8_LeadByteToCount(c) ((((0xE5 << 24) >> ((unsigned)c >> 4 << 1)) & 3) + 1)
30 #endif
31
SkUTF8_CountUTF8Bytes(const char utf8[])32 inline int SkUTF8_CountUTF8Bytes(const char utf8[]) {
33 SkASSERT(utf8);
34 return SkUTF8_LeadByteToCount(*(const uint8_t*)utf8);
35 }
36
37 int SkUTF8_CountUnichars(const char utf8[]);
38
39 /** This function is safe: invalid UTF8 sequences will return -1; */
40 int SkUTF8_CountUnicharsWithError(const char utf8[], size_t byteLength);
41
42 /** This function is safe: invalid UTF8 sequences will return 0; */
SkUTF8_CountUnichars(const char utf8[],size_t byteLength)43 inline int SkUTF8_CountUnichars(const char utf8[], size_t byteLength) {
44 return SkClampPos(SkUTF8_CountUnicharsWithError(utf8, byteLength));
45 }
46
47 /** This function is safe: invalid UTF8 sequences will return -1
48 * When -1 is returned, ptr is unchanged.
49 * Precondition: *ptr < end;
50 */
51 SkUnichar SkUTF8_NextUnicharWithError(const char** ptr, const char* end);
52
53 /** this version replaces invalid utf-8 sequences with code point U+FFFD. */
SkUTF8_NextUnichar(const char ** ptr,const char * end)54 inline SkUnichar SkUTF8_NextUnichar(const char** ptr, const char* end) {
55 SkUnichar val = SkUTF8_NextUnicharWithError(ptr, end);
56 if (val < 0) {
57 *ptr = end;
58 return 0xFFFD; // REPLACEMENT CHARACTER
59 }
60 return val;
61 }
62
63 SkUnichar SkUTF8_ToUnichar(const char utf8[]);
64 SkUnichar SkUTF8_NextUnichar(const char**);
65 SkUnichar SkUTF8_PrevUnichar(const char**);
66
67 /** Return the number of bytes need to convert a unichar
68 into a utf8 sequence. Will be 1..kMaxBytesInUTF8Sequence,
69 or 0 if uni is illegal.
70 */
71 size_t SkUTF8_FromUnichar(SkUnichar uni, char utf8[] = NULL);
72
73 ///////////////////////////////////////////////////////////////////////////////
74
75 #define SkUTF16_IsHighSurrogate(c) (((c) & 0xFC00) == 0xD800)
76 #define SkUTF16_IsLowSurrogate(c) (((c) & 0xFC00) == 0xDC00)
77
78 int SkUTF16_CountUnichars(const uint16_t utf16[]);
79 int SkUTF16_CountUnichars(const uint16_t utf16[], int numberOf16BitValues);
80 // returns the current unichar and then moves past it (*p++)
81 SkUnichar SkUTF16_NextUnichar(const uint16_t**);
82 // this guy backs up to the previus unichar value, and returns it (*--p)
83 SkUnichar SkUTF16_PrevUnichar(const uint16_t**);
84 size_t SkUTF16_FromUnichar(SkUnichar uni, uint16_t utf16[] = NULL);
85
86 size_t SkUTF16_ToUTF8(const uint16_t utf16[], int numberOf16BitValues,
87 char utf8[] = NULL);
88
SkUnichar_IsVariationSelector(SkUnichar uni)89 inline bool SkUnichar_IsVariationSelector(SkUnichar uni) {
90 /* The 'true' ranges are:
91 * 0x180B <= uni <= 0x180D
92 * 0xFE00 <= uni <= 0xFE0F
93 * 0xE0100 <= uni <= 0xE01EF
94 */
95 if (uni < 0x180B || uni > 0xE01EF) {
96 return false;
97 }
98 if ((uni > 0x180D && uni < 0xFE00) || (uni > 0xFE0F && uni < 0xE0100)) {
99 return false;
100 }
101 return true;
102 }
103 #endif
104