Julia Lavrova | 90787fe | 2020-07-20 17:32:03 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2020 Google Inc. |
| 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 | #ifndef SkUnicode_DEFINED |
| 8 | #define SkUnicode_DEFINED |
| 9 | |
| 10 | #include "include/core/SkTypes.h" |
| 11 | #include "src/core/SkSpan.h" |
Julia Lavrova | b6b7fff | 2020-09-11 13:59:49 +0000 | [diff] [blame] | 12 | #include "src/utils/SkUTF.h" |
Julia Lavrova | 90787fe | 2020-07-20 17:32:03 +0000 | [diff] [blame] | 13 | #include <vector> |
| 14 | |
| 15 | #if !defined(SKUNICODE_IMPLEMENTATION) |
| 16 | #define SKUNICODE_IMPLEMENTATION 0 |
| 17 | #endif |
| 18 | |
| 19 | #if !defined(SKUNICODE_API) |
| 20 | #if defined(SKSHAPER_DLL) |
| 21 | #if defined(_MSC_VER) |
| 22 | #if SKUNICODE_IMPLEMENTATION |
| 23 | #define SKUNICODE_API __declspec(dllexport) |
| 24 | #else |
| 25 | #define SKUNICODE_API __declspec(dllimport) |
| 26 | #endif |
| 27 | #else |
| 28 | #define SKUNICODE_API __attribute__((visibility("default"))) |
| 29 | #endif |
| 30 | #else |
| 31 | #define SKUNICODE_API |
| 32 | #endif |
| 33 | #endif |
| 34 | |
Julia Lavrova | 1798f4f | 2020-08-26 14:22:48 +0000 | [diff] [blame] | 35 | class SKUNICODE_API SkBidiIterator { |
| 36 | public: |
| 37 | typedef int32_t Position; |
| 38 | typedef uint8_t Level; |
| 39 | struct Region { |
| 40 | Region(Position start, Position end, Level level) |
| 41 | : start(start), end(end), level(level) { } |
| 42 | Position start; |
| 43 | Position end; |
| 44 | Level level; |
| 45 | }; |
| 46 | enum Direction { |
| 47 | kLTR, |
| 48 | kRTL, |
| 49 | }; |
Julia Lavrova | 6e51d92 | 2020-08-25 11:42:51 -0400 | [diff] [blame] | 50 | virtual ~SkBidiIterator() = default; |
Julia Lavrova | 1798f4f | 2020-08-26 14:22:48 +0000 | [diff] [blame] | 51 | virtual Position getLength() = 0; |
| 52 | virtual Level getLevelAt(Position) = 0; |
| 53 | static void ReorderVisual(const Level runLevels[], int levelsCount, int32_t logicalFromVisual[]); |
Julia Lavrova | 90787fe | 2020-07-20 17:32:03 +0000 | [diff] [blame] | 54 | }; |
| 55 | |
Julia Lavrova | 6e51d92 | 2020-08-25 11:42:51 -0400 | [diff] [blame] | 56 | class SKUNICODE_API SkBreakIterator { |
| 57 | public: |
| 58 | typedef int32_t Position; |
| 59 | typedef int32_t Status; |
| 60 | virtual ~SkBreakIterator() = default; |
| 61 | virtual Position first() = 0; |
| 62 | virtual Position current() = 0; |
| 63 | virtual Position next() = 0; |
| 64 | virtual Position preceding(Position offset) = 0; |
| 65 | virtual Position following(Position offset) = 0; |
| 66 | virtual Status status() = 0; |
| 67 | virtual bool isDone() = 0; |
| 68 | virtual bool setText(const char utftext8[], int utf8Units) = 0; |
| 69 | }; |
| 70 | |
Julia Lavrova | 36700ef | 2020-08-25 11:42:51 -0400 | [diff] [blame^] | 71 | class SKUNICODE_API SkScriptIterator { |
| 72 | public: |
| 73 | typedef uint32_t ScriptID; |
| 74 | virtual ~SkScriptIterator() = default; |
| 75 | virtual bool getScript(SkUnichar u, ScriptID* script) = 0; |
| 76 | }; |
| 77 | |
Julia Lavrova | 90787fe | 2020-07-20 17:32:03 +0000 | [diff] [blame] | 78 | class SKUNICODE_API SkUnicode { |
| 79 | public: |
Julia Lavrova | 90787fe | 2020-07-20 17:32:03 +0000 | [diff] [blame] | 80 | typedef uint32_t CombiningClass; |
| 81 | typedef uint32_t GeneralCategory; |
Julia Lavrova | 1798f4f | 2020-08-26 14:22:48 +0000 | [diff] [blame] | 82 | enum class TextDirection { |
| 83 | kLTR, |
| 84 | kRTL, |
| 85 | }; |
| 86 | typedef size_t Position; |
| 87 | typedef uint8_t BidiLevel; |
| 88 | struct BidiRegion { |
| 89 | BidiRegion(Position start, Position end, BidiLevel level) |
| 90 | : start(start), end(end), level(level) { } |
| 91 | Position start; |
| 92 | Position end; |
| 93 | BidiLevel level; |
| 94 | }; |
| 95 | enum class LineBreakType { |
| 96 | kSoftLineBreak, |
| 97 | kHardLineBreak |
| 98 | }; |
| 99 | |
Julia Lavrova | 6e51d92 | 2020-08-25 11:42:51 -0400 | [diff] [blame] | 100 | enum class BreakType { |
Julia Lavrova | 1798f4f | 2020-08-26 14:22:48 +0000 | [diff] [blame] | 101 | kWords, |
| 102 | kGraphemes, |
| 103 | kLines |
| 104 | }; |
| 105 | struct LineBreakBefore { |
| 106 | LineBreakBefore(Position pos, LineBreakType breakType) |
| 107 | : pos(pos), breakType(breakType) { } |
| 108 | Position pos; |
| 109 | LineBreakType breakType; |
| 110 | }; |
| 111 | |
Julia Lavrova | 90787fe | 2020-07-20 17:32:03 +0000 | [diff] [blame] | 112 | virtual ~SkUnicode() = default; |
Julia Lavrova | 1798f4f | 2020-08-26 14:22:48 +0000 | [diff] [blame] | 113 | |
Julia Lavrova | b6b7fff | 2020-09-11 13:59:49 +0000 | [diff] [blame] | 114 | virtual bool isControl(SkUnichar utf8) = 0; |
| 115 | virtual bool isWhitespace(SkUnichar utf8) = 0; |
| 116 | virtual SkString convertUtf16ToUtf8(const std::u16string& utf16) = 0; |
| 117 | |
Julia Lavrova | 6e51d92 | 2020-08-25 11:42:51 -0400 | [diff] [blame] | 118 | // Methods used in SkShaper |
Julia Lavrova | 1798f4f | 2020-08-26 14:22:48 +0000 | [diff] [blame] | 119 | virtual std::unique_ptr<SkBidiIterator> makeBidiIterator |
| 120 | (const uint16_t text[], int count, SkBidiIterator::Direction) = 0; |
| 121 | virtual std::unique_ptr<SkBidiIterator> makeBidiIterator |
| 122 | (const char text[], int count, SkBidiIterator::Direction) = 0; |
Julia Lavrova | 6e51d92 | 2020-08-25 11:42:51 -0400 | [diff] [blame] | 123 | virtual std::unique_ptr<SkBreakIterator> makeBreakIterator |
| 124 | (const char locale[], BreakType breakType) = 0; |
Julia Lavrova | 36700ef | 2020-08-25 11:42:51 -0400 | [diff] [blame^] | 125 | virtual std::unique_ptr<SkScriptIterator> makeScriptIterator() = 0; |
Julia Lavrova | 1798f4f | 2020-08-26 14:22:48 +0000 | [diff] [blame] | 126 | |
Julia Lavrova | 90787fe | 2020-07-20 17:32:03 +0000 | [diff] [blame] | 127 | // High level methods (that we actually use somewhere=SkParagraph) |
| 128 | virtual bool getBidiRegions |
Julia Lavrova | 1798f4f | 2020-08-26 14:22:48 +0000 | [diff] [blame] | 129 | (const char utf8[], int utf8Units, TextDirection dir, std::vector<BidiRegion>* results) = 0; |
Julia Lavrova | 90787fe | 2020-07-20 17:32:03 +0000 | [diff] [blame] | 130 | virtual bool getLineBreaks |
| 131 | (const char utf8[], int utf8Units, std::vector<LineBreakBefore>* results) = 0; |
| 132 | virtual bool getWords |
| 133 | (const char utf8[], int utf8Units, std::vector<Position>* results) = 0; |
| 134 | virtual bool getGraphemes |
| 135 | (const char utf8[], int utf8Units, std::vector<Position>* results) = 0; |
| 136 | virtual bool getWhitespaces |
| 137 | (const char utf8[], int utf8Units, std::vector<Position>* results) = 0; |
| 138 | |
| 139 | virtual void reorderVisual(const BidiLevel runLevels[], int levelsCount, int32_t logicalFromVisual[]) = 0; |
| 140 | |
| 141 | static std::unique_ptr<SkUnicode> Make(); |
| 142 | }; |
| 143 | |
Julia Lavrova | 90787fe | 2020-07-20 17:32:03 +0000 | [diff] [blame] | 144 | #endif // SkUnicode_DEFINED |