blob: 07b09049af561ab4f7ec0f5e8c132266f1decc6a [file] [log] [blame]
Julia Lavrova90787fe2020-07-20 17:32:03 +00001/*
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 Lavrovab6b7fff2020-09-11 13:59:49 +000012#include "src/utils/SkUTF.h"
Julia Lavrova90787fe2020-07-20 17:32:03 +000013#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 Lavrova1798f4f2020-08-26 14:22:48 +000035class SKUNICODE_API SkBidiIterator {
36public:
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 Lavrova6e51d922020-08-25 11:42:51 -040050 virtual ~SkBidiIterator() = default;
Julia Lavrova1798f4f2020-08-26 14:22:48 +000051 virtual Position getLength() = 0;
52 virtual Level getLevelAt(Position) = 0;
53 static void ReorderVisual(const Level runLevels[], int levelsCount, int32_t logicalFromVisual[]);
Julia Lavrova90787fe2020-07-20 17:32:03 +000054};
55
Julia Lavrova6e51d922020-08-25 11:42:51 -040056class SKUNICODE_API SkBreakIterator {
57public:
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 Lavrova90787fe2020-07-20 17:32:03 +000071class SKUNICODE_API SkUnicode {
72 public:
73 typedef uint32_t ScriptID;
74 typedef uint32_t CombiningClass;
75 typedef uint32_t GeneralCategory;
Julia Lavrova1798f4f2020-08-26 14:22:48 +000076 enum class TextDirection {
77 kLTR,
78 kRTL,
79 };
80 typedef size_t Position;
81 typedef uint8_t BidiLevel;
82 struct BidiRegion {
83 BidiRegion(Position start, Position end, BidiLevel level)
84 : start(start), end(end), level(level) { }
85 Position start;
86 Position end;
87 BidiLevel level;
88 };
89 enum class LineBreakType {
90 kSoftLineBreak,
91 kHardLineBreak
92 };
93
Julia Lavrova6e51d922020-08-25 11:42:51 -040094 enum class BreakType {
Julia Lavrova1798f4f2020-08-26 14:22:48 +000095 kWords,
96 kGraphemes,
97 kLines
98 };
99 struct LineBreakBefore {
100 LineBreakBefore(Position pos, LineBreakType breakType)
101 : pos(pos), breakType(breakType) { }
102 Position pos;
103 LineBreakType breakType;
104 };
105
Julia Lavrova90787fe2020-07-20 17:32:03 +0000106 virtual ~SkUnicode() = default;
Julia Lavrova1798f4f2020-08-26 14:22:48 +0000107
Julia Lavrovab6b7fff2020-09-11 13:59:49 +0000108 virtual bool isControl(SkUnichar utf8) = 0;
109 virtual bool isWhitespace(SkUnichar utf8) = 0;
110 virtual SkString convertUtf16ToUtf8(const std::u16string& utf16) = 0;
111
Julia Lavrova6e51d922020-08-25 11:42:51 -0400112 // Methods used in SkShaper
Julia Lavrova1798f4f2020-08-26 14:22:48 +0000113 virtual std::unique_ptr<SkBidiIterator> makeBidiIterator
114 (const uint16_t text[], int count, SkBidiIterator::Direction) = 0;
115 virtual std::unique_ptr<SkBidiIterator> makeBidiIterator
116 (const char text[], int count, SkBidiIterator::Direction) = 0;
Julia Lavrova6e51d922020-08-25 11:42:51 -0400117 virtual std::unique_ptr<SkBreakIterator> makeBreakIterator
118 (const char locale[], BreakType breakType) = 0;
Julia Lavrova1798f4f2020-08-26 14:22:48 +0000119
Julia Lavrova90787fe2020-07-20 17:32:03 +0000120 // High level methods (that we actually use somewhere=SkParagraph)
121 virtual bool getBidiRegions
Julia Lavrova1798f4f2020-08-26 14:22:48 +0000122 (const char utf8[], int utf8Units, TextDirection dir, std::vector<BidiRegion>* results) = 0;
Julia Lavrova90787fe2020-07-20 17:32:03 +0000123 virtual bool getLineBreaks
124 (const char utf8[], int utf8Units, std::vector<LineBreakBefore>* results) = 0;
125 virtual bool getWords
126 (const char utf8[], int utf8Units, std::vector<Position>* results) = 0;
127 virtual bool getGraphemes
128 (const char utf8[], int utf8Units, std::vector<Position>* results) = 0;
129 virtual bool getWhitespaces
130 (const char utf8[], int utf8Units, std::vector<Position>* results) = 0;
131
132 virtual void reorderVisual(const BidiLevel runLevels[], int levelsCount, int32_t logicalFromVisual[]) = 0;
133
134 static std::unique_ptr<SkUnicode> Make();
135};
136
Julia Lavrova90787fe2020-07-20 17:32:03 +0000137#endif // SkUnicode_DEFINED