Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / third_party / skia / include / core / SkUtils.h
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 ///////////////////////////////////////////////////////////////////////////////
14
15 /** Similar to memset(), but it assigns a 16bit value into the buffer.
16     @param buffer   The memory to have value copied into it
17     @param value    The 16bit value to be copied into buffer
18     @param count    The number of times value should be copied into the buffer.
19 */
20 void sk_memset16(uint16_t dst[], uint16_t value, int count);
21 typedef void (*SkMemset16Proc)(uint16_t dst[], uint16_t value, int count);
22 SkMemset16Proc SkMemset16GetPlatformProc();
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 */
29 void sk_memset32(uint32_t dst[], uint32_t value, int count);
30 typedef void (*SkMemset32Proc)(uint32_t dst[], uint32_t value, int count);
31 SkMemset32Proc SkMemset32GetPlatformProc();
32
33 ///////////////////////////////////////////////////////////////////////////////
34
35 #define kMaxBytesInUTF8Sequence     4
36
37 #ifdef SK_DEBUG
38     int SkUTF8_LeadByteToCount(unsigned c);
39 #else
40     #define SkUTF8_LeadByteToCount(c)   ((((0xE5 << 24) >> ((unsigned)c >> 4 << 1)) & 3) + 1)
41 #endif
42
43 inline int SkUTF8_CountUTF8Bytes(const char utf8[]) {
44     SkASSERT(utf8);
45     return SkUTF8_LeadByteToCount(*(const uint8_t*)utf8);
46 }
47
48 int         SkUTF8_CountUnichars(const char utf8[]);
49 int         SkUTF8_CountUnichars(const char utf8[], size_t byteLength);
50 SkUnichar   SkUTF8_ToUnichar(const char utf8[]);
51 SkUnichar   SkUTF8_NextUnichar(const char**);
52 SkUnichar   SkUTF8_PrevUnichar(const char**);
53
54 /** Return the number of bytes need to convert a unichar
55     into a utf8 sequence. Will be 1..kMaxBytesInUTF8Sequence,
56     or 0 if uni is illegal.
57 */
58 size_t      SkUTF8_FromUnichar(SkUnichar uni, char utf8[] = NULL);
59
60 ///////////////////////////////////////////////////////////////////////////////
61
62 #define SkUTF16_IsHighSurrogate(c)  (((c) & 0xFC00) == 0xD800)
63 #define SkUTF16_IsLowSurrogate(c)   (((c) & 0xFC00) == 0xDC00)
64
65 int SkUTF16_CountUnichars(const uint16_t utf16[]);
66 int SkUTF16_CountUnichars(const uint16_t utf16[], int numberOf16BitValues);
67 // returns the current unichar and then moves past it (*p++)
68 SkUnichar SkUTF16_NextUnichar(const uint16_t**);
69 // this guy backs up to the previus unichar value, and returns it (*--p)
70 SkUnichar SkUTF16_PrevUnichar(const uint16_t**);
71 size_t SkUTF16_FromUnichar(SkUnichar uni, uint16_t utf16[] = NULL);
72
73 size_t SkUTF16_ToUTF8(const uint16_t utf16[], int numberOf16BitValues,
74                       char utf8[] = NULL);
75
76 inline bool SkUnichar_IsVariationSelector(SkUnichar uni) {
77 /*  The 'true' ranges are:
78  *      0x180B  <= uni <=  0x180D
79  *      0xFE00  <= uni <=  0xFE0F
80  *      0xE0100 <= uni <= 0xE01EF
81  */
82     if (uni < 0x180B || uni > 0xE01EF) {
83         return false;
84     }
85     if ((uni > 0x180D && uni < 0xFE00) || (uni > 0xFE0F && uni < 0xE0100)) {
86         return false;
87     }
88     return true;
89 }
90
91 ///////////////////////////////////////////////////////////////////////////////
92
93 class SkAutoTrace {
94 public:
95     /** NOTE: label contents are not copied, just the ptr is
96         retained, so DON'T DELETE IT.
97     */
98     SkAutoTrace(const char label[]) : fLabel(label) {
99         SkDebugf("--- trace: %s Enter\n", fLabel);
100     }
101     ~SkAutoTrace() {
102         SkDebugf("--- trace: %s Leave\n", fLabel);
103     }
104 private:
105     const char* fLabel;
106 };
107 #define SkAutoTrace(...) SK_REQUIRE_LOCAL_VAR(SkAutoTrace)
108
109 #endif