blob: 0b5b36395a4a4a88afea9dc47bb1ecb782340abe [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"
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 Lavrova90787fe2020-07-20 17:32:03 +000034enum class UtfFormat {
35 kUTF8,
36 kUTF16
37};
38// Bidi
39typedef size_t Position;
40typedef uint8_t BidiLevel;
41enum class Direction {
42 kLTR,
43 kRTL,
44};
45struct 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
53enum class LineBreakType {
54 kSoftLineBreak,
55 kHardLineBreak
56};
57struct LineBreakBefore {
58 LineBreakBefore(Position pos, LineBreakType breakType)
59 : pos(pos), breakType(breakType) { }
60 Position pos;
61 LineBreakType breakType;
62};
63// Other breaks
64enum class UBreakType {
65 kWords,
66 kGraphemes,
67 kLines
68};
69struct Range {
70 Position start;
71 Position end;
72};
73
Julia Lavrova64e3d042020-08-06 14:25:52 -040074class SKUNICODE_API SkBidiIterator {
75public:
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 Lavrova90787fe2020-07-20 17:32:03 +000095class 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 Lavrova64e3d042020-08-06 14:25:52 -0400101
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 Lavrova90787fe2020-07-20 17:32:03 +0000108 // 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 Lavrova90787fe2020-07-20 17:32:03 +0000125#endif // SkUnicode_DEFINED