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 
13 /** Similar to memset(), but it assigns a 16bit value into the buffer.
14     @param buffer   The memory to have value copied into it
15     @param value    The 16bit value to be copied into buffer
16     @param count    The number of times value should be copied into the buffer.
17 */
sk_memset16(uint16_t buffer[],uint16_t value,int count)18 static inline void sk_memset16(uint16_t buffer[], uint16_t value, int count) {
19     for (int i = 0; i < count; i++) {
20         buffer[i] = value;
21     }
22 }
23 
24 /** Similar to memset(), but it assigns a 32bit value into the buffer.
25     @param buffer   The memory to have value copied into it
26     @param value    The 32bit value to be copied into buffer
27     @param count    The number of times value should be copied into the buffer.
28 */
sk_memset32(uint32_t buffer[],uint32_t value,int count)29 static inline void sk_memset32(uint32_t buffer[], uint32_t value, int count) {
30     for (int i = 0; i < count; i++) {
31         buffer[i] = value;
32     }
33 }
34 
35 ///////////////////////////////////////////////////////////////////////////////
36 
37 #define kMaxBytesInUTF8Sequence     4
38 
39 #ifdef SK_DEBUG
40     int SkUTF8_LeadByteToCount(unsigned c);
41 #else
42     #define SkUTF8_LeadByteToCount(c)   ((((0xE5 << 24) >> ((unsigned)c >> 4 << 1)) & 3) + 1)
43 #endif
44 
SkUTF8_CountUTF8Bytes(const char utf8[])45 inline int SkUTF8_CountUTF8Bytes(const char utf8[]) {
46     SkASSERT(utf8);
47     return SkUTF8_LeadByteToCount(*(const uint8_t*)utf8);
48 }
49 
50 int         SkUTF8_CountUnichars(const char utf8[]);
51 int         SkUTF8_CountUnichars(const char utf8[], size_t byteLength);
52 SkUnichar   SkUTF8_ToUnichar(const char utf8[]);
53 SkUnichar   SkUTF8_NextUnichar(const char**);
54 SkUnichar   SkUTF8_PrevUnichar(const char**);
55 
56 /** Return the number of bytes need to convert a unichar
57     into a utf8 sequence. Will be 1..kMaxBytesInUTF8Sequence,
58     or 0 if uni is illegal.
59 */
60 size_t      SkUTF8_FromUnichar(SkUnichar uni, char utf8[] = NULL);
61 
62 ///////////////////////////////////////////////////////////////////////////////
63 
64 #define SkUTF16_IsHighSurrogate(c)  (((c) & 0xFC00) == 0xD800)
65 #define SkUTF16_IsLowSurrogate(c)   (((c) & 0xFC00) == 0xDC00)
66 
67 int SkUTF16_CountUnichars(const uint16_t utf16[]);
68 int SkUTF16_CountUnichars(const uint16_t utf16[], int numberOf16BitValues);
69 // returns the current unichar and then moves past it (*p++)
70 SkUnichar SkUTF16_NextUnichar(const uint16_t**);
71 // this guy backs up to the previus unichar value, and returns it (*--p)
72 SkUnichar SkUTF16_PrevUnichar(const uint16_t**);
73 size_t SkUTF16_FromUnichar(SkUnichar uni, uint16_t utf16[] = NULL);
74 
75 size_t SkUTF16_ToUTF8(const uint16_t utf16[], int numberOf16BitValues,
76                       char utf8[] = NULL);
77 
SkUnichar_IsVariationSelector(SkUnichar uni)78 inline bool SkUnichar_IsVariationSelector(SkUnichar uni) {
79 /*  The 'true' ranges are:
80  *      0x180B  <= uni <=  0x180D
81  *      0xFE00  <= uni <=  0xFE0F
82  *      0xE0100 <= uni <= 0xE01EF
83  */
84     if (uni < 0x180B || uni > 0xE01EF) {
85         return false;
86     }
87     if ((uni > 0x180D && uni < 0xFE00) || (uni > 0xFE0F && uni < 0xE0100)) {
88         return false;
89     }
90     return true;
91 }
92 
93 #endif
94