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 | }; |
| 50 | virtual ~SkBidiIterator() {} |
| 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 | |
| 56 | class SKUNICODE_API SkUnicode { |
| 57 | public: |
| 58 | typedef uint32_t ScriptID; |
| 59 | typedef uint32_t CombiningClass; |
| 60 | typedef uint32_t GeneralCategory; |
Julia Lavrova | 1798f4f | 2020-08-26 14:22:48 +0000 | [diff] [blame] | 61 | enum class TextDirection { |
| 62 | kLTR, |
| 63 | kRTL, |
| 64 | }; |
| 65 | typedef size_t Position; |
| 66 | typedef uint8_t BidiLevel; |
| 67 | struct BidiRegion { |
| 68 | BidiRegion(Position start, Position end, BidiLevel level) |
| 69 | : start(start), end(end), level(level) { } |
| 70 | Position start; |
| 71 | Position end; |
| 72 | BidiLevel level; |
| 73 | }; |
| 74 | enum class LineBreakType { |
| 75 | kSoftLineBreak, |
| 76 | kHardLineBreak |
| 77 | }; |
| 78 | |
| 79 | enum class UBreakType { |
| 80 | kWords, |
| 81 | kGraphemes, |
| 82 | kLines |
| 83 | }; |
| 84 | struct LineBreakBefore { |
| 85 | LineBreakBefore(Position pos, LineBreakType breakType) |
| 86 | : pos(pos), breakType(breakType) { } |
| 87 | Position pos; |
| 88 | LineBreakType breakType; |
| 89 | }; |
| 90 | |
Julia Lavrova | 90787fe | 2020-07-20 17:32:03 +0000 | [diff] [blame] | 91 | virtual ~SkUnicode() = default; |
Julia Lavrova | 1798f4f | 2020-08-26 14:22:48 +0000 | [diff] [blame] | 92 | |
Julia Lavrova | b6b7fff | 2020-09-11 13:59:49 +0000 | [diff] [blame^] | 93 | virtual bool isControl(SkUnichar utf8) = 0; |
| 94 | virtual bool isWhitespace(SkUnichar utf8) = 0; |
| 95 | virtual SkString convertUtf16ToUtf8(const std::u16string& utf16) = 0; |
| 96 | |
Julia Lavrova | 1798f4f | 2020-08-26 14:22:48 +0000 | [diff] [blame] | 97 | // Iterators (used in SkShaper) |
| 98 | virtual std::unique_ptr<SkBidiIterator> makeBidiIterator |
| 99 | (const uint16_t text[], int count, SkBidiIterator::Direction) = 0; |
| 100 | virtual std::unique_ptr<SkBidiIterator> makeBidiIterator |
| 101 | (const char text[], int count, SkBidiIterator::Direction) = 0; |
| 102 | |
Julia Lavrova | 90787fe | 2020-07-20 17:32:03 +0000 | [diff] [blame] | 103 | // High level methods (that we actually use somewhere=SkParagraph) |
| 104 | virtual bool getBidiRegions |
Julia Lavrova | 1798f4f | 2020-08-26 14:22:48 +0000 | [diff] [blame] | 105 | (const char utf8[], int utf8Units, TextDirection dir, std::vector<BidiRegion>* results) = 0; |
Julia Lavrova | 90787fe | 2020-07-20 17:32:03 +0000 | [diff] [blame] | 106 | virtual bool getLineBreaks |
| 107 | (const char utf8[], int utf8Units, std::vector<LineBreakBefore>* results) = 0; |
| 108 | virtual bool getWords |
| 109 | (const char utf8[], int utf8Units, std::vector<Position>* results) = 0; |
| 110 | virtual bool getGraphemes |
| 111 | (const char utf8[], int utf8Units, std::vector<Position>* results) = 0; |
| 112 | virtual bool getWhitespaces |
| 113 | (const char utf8[], int utf8Units, std::vector<Position>* results) = 0; |
| 114 | |
| 115 | virtual void reorderVisual(const BidiLevel runLevels[], int levelsCount, int32_t logicalFromVisual[]) = 0; |
| 116 | |
| 117 | static std::unique_ptr<SkUnicode> Make(); |
| 118 | }; |
| 119 | |
Julia Lavrova | 90787fe | 2020-07-20 17:32:03 +0000 | [diff] [blame] | 120 | #endif // SkUnicode_DEFINED |