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" |
| 12 | #include <vector> |
| 13 | |
| 14 | #if !defined(SKUNICODE_IMPLEMENTATION) |
| 15 | #define SKUNICODE_IMPLEMENTATION 0 |
| 16 | #endif |
| 17 | |
| 18 | #if !defined(SKUNICODE_API) |
| 19 | #if defined(SKSHAPER_DLL) |
| 20 | #if defined(_MSC_VER) |
| 21 | #if SKUNICODE_IMPLEMENTATION |
| 22 | #define SKUNICODE_API __declspec(dllexport) |
| 23 | #else |
| 24 | #define SKUNICODE_API __declspec(dllimport) |
| 25 | #endif |
| 26 | #else |
| 27 | #define SKUNICODE_API __attribute__((visibility("default"))) |
| 28 | #endif |
| 29 | #else |
| 30 | #define SKUNICODE_API |
| 31 | #endif |
| 32 | #endif |
| 33 | |
Julia Lavrova | 90787fe | 2020-07-20 17:32:03 +0000 | [diff] [blame] | 34 | enum class UtfFormat { |
| 35 | kUTF8, |
| 36 | kUTF16 |
| 37 | }; |
| 38 | // Bidi |
| 39 | typedef size_t Position; |
| 40 | typedef uint8_t BidiLevel; |
| 41 | enum class Direction { |
| 42 | kLTR, |
| 43 | kRTL, |
| 44 | }; |
| 45 | struct BidiRegion { |
| 46 | BidiRegion(Position start, Position end, BidiLevel level) |
| 47 | : start(start), end(end), level(level) { } |
| 48 | Position start; |
| 49 | Position end; |
| 50 | BidiLevel level; |
| 51 | }; |
| 52 | // LineBreaks |
| 53 | enum class LineBreakType { |
| 54 | kSoftLineBreak, |
| 55 | kHardLineBreak |
| 56 | }; |
| 57 | struct LineBreakBefore { |
| 58 | LineBreakBefore(Position pos, LineBreakType breakType) |
| 59 | : pos(pos), breakType(breakType) { } |
| 60 | Position pos; |
| 61 | LineBreakType breakType; |
| 62 | }; |
| 63 | // Other breaks |
| 64 | enum class UBreakType { |
| 65 | kWords, |
| 66 | kGraphemes, |
| 67 | kLines |
| 68 | }; |
| 69 | struct Range { |
| 70 | Position start; |
| 71 | Position end; |
| 72 | }; |
| 73 | |
Julia Lavrova | 64e3d04 | 2020-08-06 14:25:52 -0400 | [diff] [blame] | 74 | class SKUNICODE_API SkBidiIterator { |
| 75 | public: |
| 76 | typedef int32_t Position; |
| 77 | typedef uint8_t Level; |
| 78 | struct Region { |
| 79 | Region(Position start, Position end, Level level) |
| 80 | : start(start), end(end), level(level) { } |
| 81 | Position start; |
| 82 | Position end; |
| 83 | Level level; |
| 84 | }; |
| 85 | enum Direction { |
| 86 | kLTR, |
| 87 | kRTL, |
| 88 | }; |
| 89 | virtual ~SkBidiIterator() {} |
| 90 | virtual Position getLength() = 0; |
| 91 | virtual Level getLevelAt(Position) = 0; |
| 92 | static void ReorderVisual(const Level runLevels[], int levelsCount, int32_t logicalFromVisual[]); |
| 93 | }; |
| 94 | |
Julia Lavrova | 90787fe | 2020-07-20 17:32:03 +0000 | [diff] [blame] | 95 | class SKUNICODE_API SkUnicode { |
| 96 | public: |
| 97 | typedef uint32_t ScriptID; |
| 98 | typedef uint32_t CombiningClass; |
| 99 | typedef uint32_t GeneralCategory; |
| 100 | virtual ~SkUnicode() = default; |
Julia Lavrova | 64e3d04 | 2020-08-06 14:25:52 -0400 | [diff] [blame] | 101 | |
| 102 | // Iterators (used in SkShaper) |
| 103 | virtual std::unique_ptr<SkBidiIterator> makeBidiIterator |
| 104 | (const uint16_t text[], int count, SkBidiIterator::Direction) = 0; |
| 105 | virtual std::unique_ptr<SkBidiIterator> makeBidiIterator |
| 106 | (const char text[], int count, SkBidiIterator::Direction) = 0; |
| 107 | |
Julia Lavrova | 90787fe | 2020-07-20 17:32:03 +0000 | [diff] [blame] | 108 | // High level methods (that we actually use somewhere=SkParagraph) |
| 109 | virtual bool getBidiRegions |
| 110 | (const char utf8[], int utf8Units, Direction dir, std::vector<BidiRegion>* results) = 0; |
| 111 | virtual bool getLineBreaks |
| 112 | (const char utf8[], int utf8Units, std::vector<LineBreakBefore>* results) = 0; |
| 113 | virtual bool getWords |
| 114 | (const char utf8[], int utf8Units, std::vector<Position>* results) = 0; |
| 115 | virtual bool getGraphemes |
| 116 | (const char utf8[], int utf8Units, std::vector<Position>* results) = 0; |
| 117 | virtual bool getWhitespaces |
| 118 | (const char utf8[], int utf8Units, std::vector<Position>* results) = 0; |
| 119 | |
| 120 | virtual void reorderVisual(const BidiLevel runLevels[], int levelsCount, int32_t logicalFromVisual[]) = 0; |
| 121 | |
| 122 | static std::unique_ptr<SkUnicode> Make(); |
| 123 | }; |
| 124 | |
Julia Lavrova | 90787fe | 2020-07-20 17:32:03 +0000 | [diff] [blame] | 125 | #endif // SkUnicode_DEFINED |