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