blob: 3bbb02ed064eb692f08b50e2c2fa8890cced458f [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 Lavrova1798f4f2020-08-26 14:22:48 +000034class SKUNICODE_API SkBidiIterator {
35public:
36 typedef int32_t Position;
37 typedef uint8_t Level;
38 struct Region {
39 Region(Position start, Position end, Level level)
40 : start(start), end(end), level(level) { }
41 Position start;
42 Position end;
43 Level level;
44 };
45 enum Direction {
46 kLTR,
47 kRTL,
48 };
49 virtual ~SkBidiIterator() {}
50 virtual Position getLength() = 0;
51 virtual Level getLevelAt(Position) = 0;
52 static void ReorderVisual(const Level runLevels[], int levelsCount, int32_t logicalFromVisual[]);
Julia Lavrova90787fe2020-07-20 17:32:03 +000053};
54
55class SKUNICODE_API SkUnicode {
56 public:
57 typedef uint32_t ScriptID;
58 typedef uint32_t CombiningClass;
59 typedef uint32_t GeneralCategory;
Julia Lavrova1798f4f2020-08-26 14:22:48 +000060 enum class TextDirection {
61 kLTR,
62 kRTL,
63 };
64 typedef size_t Position;
65 typedef uint8_t BidiLevel;
66 struct BidiRegion {
67 BidiRegion(Position start, Position end, BidiLevel level)
68 : start(start), end(end), level(level) { }
69 Position start;
70 Position end;
71 BidiLevel level;
72 };
73 enum class LineBreakType {
74 kSoftLineBreak,
75 kHardLineBreak
76 };
77
78 enum class UBreakType {
79 kWords,
80 kGraphemes,
81 kLines
82 };
83 struct LineBreakBefore {
84 LineBreakBefore(Position pos, LineBreakType breakType)
85 : pos(pos), breakType(breakType) { }
86 Position pos;
87 LineBreakType breakType;
88 };
89
Julia Lavrova90787fe2020-07-20 17:32:03 +000090 virtual ~SkUnicode() = default;
Julia Lavrova1798f4f2020-08-26 14:22:48 +000091
92 // Iterators (used in SkShaper)
93 virtual std::unique_ptr<SkBidiIterator> makeBidiIterator
94 (const uint16_t text[], int count, SkBidiIterator::Direction) = 0;
95 virtual std::unique_ptr<SkBidiIterator> makeBidiIterator
96 (const char text[], int count, SkBidiIterator::Direction) = 0;
97
Julia Lavrova90787fe2020-07-20 17:32:03 +000098 // High level methods (that we actually use somewhere=SkParagraph)
99 virtual bool getBidiRegions
Julia Lavrova1798f4f2020-08-26 14:22:48 +0000100 (const char utf8[], int utf8Units, TextDirection dir, std::vector<BidiRegion>* results) = 0;
Julia Lavrova90787fe2020-07-20 17:32:03 +0000101 virtual bool getLineBreaks
102 (const char utf8[], int utf8Units, std::vector<LineBreakBefore>* results) = 0;
103 virtual bool getWords
104 (const char utf8[], int utf8Units, std::vector<Position>* results) = 0;
105 virtual bool getGraphemes
106 (const char utf8[], int utf8Units, std::vector<Position>* results) = 0;
107 virtual bool getWhitespaces
108 (const char utf8[], int utf8Units, std::vector<Position>* results) = 0;
109
110 virtual void reorderVisual(const BidiLevel runLevels[], int levelsCount, int32_t logicalFromVisual[]) = 0;
111
112 static std::unique_ptr<SkUnicode> Make();
113};
114
Julia Lavrova90787fe2020-07-20 17:32:03 +0000115#endif // SkUnicode_DEFINED