reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1 | /* |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 2 | * Copyright 2006 The Android Open Source Project |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 3 | * |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #ifndef SkUtils_DEFINED |
| 9 | #define SkUtils_DEFINED |
| 10 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 11 | #include "include/core/SkFontTypes.h" |
| 12 | #include "src/core/SkOpts.h" |
| 13 | #include "src/utils/SkUTF.h" |
mtklein | bdb34d0 | 2015-07-31 14:02:36 -0700 | [diff] [blame] | 14 | |
Mike Klein | 57bc1e7 | 2017-01-04 14:44:20 -0500 | [diff] [blame] | 15 | /** Similar to memset(), but it assigns a 16, 32, or 64-bit value into the buffer. |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 16 | @param buffer The memory to have value copied into it |
Mike Klein | 57bc1e7 | 2017-01-04 14:44:20 -0500 | [diff] [blame] | 17 | @param value The value to be copied into buffer |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 18 | @param count The number of times value should be copied into the buffer. |
| 19 | */ |
Mike Klein | 87db001 | 2017-05-23 12:20:38 -0400 | [diff] [blame] | 20 | static inline void sk_memset16(uint16_t buffer[], uint16_t value, int count) { |
| 21 | SkOpts::memset16(buffer, value, count); |
| 22 | } |
| 23 | static inline void sk_memset32(uint32_t buffer[], uint32_t value, int count) { |
| 24 | SkOpts::memset32(buffer, value, count); |
| 25 | } |
| 26 | static inline void sk_memset64(uint64_t buffer[], uint64_t value, int count) { |
| 27 | SkOpts::memset64(buffer, value, count); |
| 28 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 29 | |
| 30 | /////////////////////////////////////////////////////////////////////////////// |
| 31 | |
Hal Canary | f107a2f | 2018-07-25 16:52:48 -0400 | [diff] [blame] | 32 | // Unlike the functions in SkUTF.h, these two functions do not take an array |
| 33 | // length parameter. When possible, use SkUTF::NextUTF{8,16} instead. |
| 34 | SkUnichar SkUTF8_NextUnichar(const char**); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 35 | SkUnichar SkUTF16_NextUnichar(const uint16_t**); |
Hal Canary | c9b6dda | 2018-06-19 09:53:26 -0400 | [diff] [blame] | 36 | |
Hal Canary | f107a2f | 2018-07-25 16:52:48 -0400 | [diff] [blame] | 37 | /////////////////////////////////////////////////////////////////////////////// |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 38 | |
Ben Wagner | ad031f5 | 2018-08-20 13:45:57 -0400 | [diff] [blame] | 39 | static inline bool SkUTF16_IsLeadingSurrogate(uint16_t c) { return ((c) & 0xFC00) == 0xD800; } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 40 | |
Ben Wagner | ad031f5 | 2018-08-20 13:45:57 -0400 | [diff] [blame] | 41 | static inline bool SkUTF16_IsTrailingSurrogate (uint16_t c) { return ((c) & 0xFC00) == 0xDC00; } |
Hal Canary | f107a2f | 2018-07-25 16:52:48 -0400 | [diff] [blame] | 42 | |
| 43 | /////////////////////////////////////////////////////////////////////////////// |
| 44 | |
Mike Reed | 64670cb | 2019-04-16 11:37:38 -0700 | [diff] [blame] | 45 | static inline int SkUTFN_CountUnichars(SkTextEncoding enc, const void* utfN, size_t bytes) { |
Hal Canary | f107a2f | 2018-07-25 16:52:48 -0400 | [diff] [blame] | 46 | switch (enc) { |
Mike Reed | 64670cb | 2019-04-16 11:37:38 -0700 | [diff] [blame] | 47 | case SkTextEncoding::kUTF8: return SkUTF::CountUTF8((const char*)utfN, bytes); |
| 48 | case SkTextEncoding::kUTF16: return SkUTF::CountUTF16((const uint16_t*)utfN, bytes); |
| 49 | case SkTextEncoding::kUTF32: return SkUTF::CountUTF32((const int32_t*)utfN, bytes); |
Hal Canary | f107a2f | 2018-07-25 16:52:48 -0400 | [diff] [blame] | 50 | default: SkDEBUGFAIL("unknown text encoding"); return -1; |
reed@google.com | 419f433 | 2011-12-21 15:21:32 +0000 | [diff] [blame] | 51 | } |
reed@google.com | 419f433 | 2011-12-21 15:21:32 +0000 | [diff] [blame] | 52 | } |
Hal Canary | d6e6e66 | 2017-06-17 10:38:13 -0400 | [diff] [blame] | 53 | |
Mike Reed | 64670cb | 2019-04-16 11:37:38 -0700 | [diff] [blame] | 54 | static inline SkUnichar SkUTFN_Next(SkTextEncoding enc, const void** ptr, const void* stop) { |
Hal Canary | f107a2f | 2018-07-25 16:52:48 -0400 | [diff] [blame] | 55 | switch (enc) { |
Mike Reed | 64670cb | 2019-04-16 11:37:38 -0700 | [diff] [blame] | 56 | case SkTextEncoding::kUTF8: |
Hal Canary | f107a2f | 2018-07-25 16:52:48 -0400 | [diff] [blame] | 57 | return SkUTF::NextUTF8((const char**)ptr, (const char*)stop); |
Mike Reed | 64670cb | 2019-04-16 11:37:38 -0700 | [diff] [blame] | 58 | case SkTextEncoding::kUTF16: |
Hal Canary | f107a2f | 2018-07-25 16:52:48 -0400 | [diff] [blame] | 59 | return SkUTF::NextUTF16((const uint16_t**)ptr, (const uint16_t*)stop); |
Mike Reed | 64670cb | 2019-04-16 11:37:38 -0700 | [diff] [blame] | 60 | case SkTextEncoding::kUTF32: |
Hal Canary | f107a2f | 2018-07-25 16:52:48 -0400 | [diff] [blame] | 61 | return SkUTF::NextUTF32((const int32_t**)ptr, (const int32_t*)stop); |
| 62 | default: SkDEBUGFAIL("unknown text encoding"); return -1; |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | /////////////////////////////////////////////////////////////////////////////// |
| 67 | |
Hal Canary | d6e6e66 | 2017-06-17 10:38:13 -0400 | [diff] [blame] | 68 | namespace SkHexadecimalDigits { |
| 69 | extern const char gUpper[16]; // 0-9A-F |
| 70 | extern const char gLower[16]; // 0-9a-f |
| 71 | } |
| 72 | |
Mike Klein | 7a177b4 | 2019-06-17 17:17:47 -0500 | [diff] [blame] | 73 | /////////////////////////////////////////////////////////////////////////////// |
| 74 | |
| 75 | // If T is an 8-byte GCC or Clang vector extension type, it would naturally |
| 76 | // pass or return in the MMX mm0 register on 32-bit x86 builds. This has the |
| 77 | // fun side effect of clobbering any state in the x87 st0 register. (There is |
| 78 | // no ABI governing who should preserve mm?/st? registers, so no one does!) |
| 79 | // |
| 80 | // We force-inline sk_unaligned_load() and sk_unaligned_store() to avoid that, |
| 81 | // making them safe to use for all types on all platforms, thus solving the |
| 82 | // problem once and for all! |
| 83 | |
| 84 | template <typename T, typename P> |
| 85 | static SK_ALWAYS_INLINE T sk_unaligned_load(const P* ptr) { |
| 86 | // TODO: static_assert desirable things about T here so as not to be totally abused. |
| 87 | T val; |
| 88 | memcpy(&val, ptr, sizeof(val)); |
| 89 | return val; |
| 90 | } |
| 91 | |
| 92 | template <typename T, typename P> |
| 93 | static SK_ALWAYS_INLINE void sk_unaligned_store(P* ptr, T val) { |
| 94 | // TODO: ditto |
| 95 | memcpy(ptr, &val, sizeof(val)); |
| 96 | } |
| 97 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 98 | #endif |