summaryrefslogtreecommitdiff
path: root/chromium/third_party/blink/renderer/platform/fonts/utf16_ragel_iterator.h
blob: 1e8b1c225f797b384145eb8ab51302b1cb7d2b88 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef THIRD_PARTY_BLINK_RENDERER_PLATFORM_FONTS_UTF16_RAGEL_ITERATOR_H_
#define THIRD_PARTY_BLINK_RENDERER_PLATFORM_FONTS_UTF16_RAGEL_ITERATOR_H_

#include <unicode/uchar.h>

#include "base/check_op.h"
#include "third_party/blink/renderer/platform/platform_export.h"
#include "third_party/blink/renderer/platform/wtf/allocator/allocator.h"

namespace blink {

// UTF16RagelIterator is set up on top of a UTF-16 UChar* buffer iterating over
// a Blink internal text string and as such is used as an adapter between Blink
// strings and the Ragel-based emoji scanner. It supports forwarding and
// reversing using arithmetic operators. Dereferencing the iterator means
// retrieving a character class as defined in the Ragel grammar of
// third-party/emoji-segmenter. The dereferenced character category is cached
// since Ragel dereferences multiple times without moving the iterator's cursor.
class PLATFORM_EXPORT UTF16RagelIterator {
  DISALLOW_NEW();

 public:
  UTF16RagelIterator() : buffer_(nullptr), buffer_size_(0), cursor_(0) {}

  UTF16RagelIterator(const UChar* buffer,
                     unsigned buffer_size,
                     unsigned cursor = 0)
      : buffer_(buffer),
        buffer_size_(buffer_size),
        cursor_(cursor),
        cached_category_(kMaxEmojiScannerCategory) {
    UpdateCachedCategory();
  }

  UTF16RagelIterator end() {
    UTF16RagelIterator ret = *this;
    ret.cursor_ = buffer_size_;
    return ret;
  }

  UTF16RagelIterator& SetCursor(unsigned new_cursor);

  unsigned Cursor() { return cursor_; }

  UTF16RagelIterator& operator+=(int v) {
    if (v > 0) {
      U16_FWD_N(buffer_, cursor_, buffer_size_, v);
    } else if (v < 0) {
      U16_BACK_N(buffer_, 0, cursor_, -v);
    }
    UpdateCachedCategory();
    return *this;
  }

  UTF16RagelIterator& operator-=(int v) { return *this += -v; }

  UTF16RagelIterator operator+(int v) {
    UTF16RagelIterator ret = *this;
    return ret += v;
  }

  UTF16RagelIterator operator-(int v) { return *this + -v; }

  int operator-(const UTF16RagelIterator& other) {
    DCHECK_EQ(buffer_, other.buffer_);
    return cursor_ - other.cursor_;
  }

  UTF16RagelIterator& operator++() {
    DCHECK_LT(cursor_, buffer_size_);
    U16_FWD_1(buffer_, cursor_, buffer_size_);
    UpdateCachedCategory();
    return *this;
  }

  UTF16RagelIterator& operator--() {
    DCHECK_GT(cursor_, 0u);
    U16_BACK_1(buffer_, 0, cursor_);
    UpdateCachedCategory();
    return *this;
  }

  UTF16RagelIterator operator++(int) {
    UTF16RagelIterator ret = *this;
    ++(*this);
    return ret;
  }

  UTF16RagelIterator operator--(int) {
    UTF16RagelIterator ret = *this;
    --(*this);
    return ret;
  }

  UTF16RagelIterator operator=(int v) {
    // We need this integer assignment operator because Ragel has initialization
    // code for assigning 0 to ts, te.
    DCHECK_EQ(v, 0);
    UTF16RagelIterator ret = *this;
    ret.cursor_ = v;
    return ret;
  }

  UChar32 operator*() {
    CHECK(buffer_size_);
    return cached_category_;
  }

  bool operator==(const UTF16RagelIterator& other) const {
    return buffer_ == other.buffer_ && buffer_size_ == other.buffer_size_ &&
           cursor_ == other.cursor_;
  }

  bool operator!=(const UTF16RagelIterator& other) const {
    return !(*this == other);
  }

  // Must match the categories defined in third-party/emoji-segmenter/.
  // TODO(drott): Add static asserts once emoji-segmenter is imported to
  // third-party.
  enum EmojiScannerCharacterClass {
    EMOJI = 0,
    EMOJI_TEXT_PRESENTATION = 1,
    EMOJI_EMOJI_PRESENTATION = 2,
    EMOJI_MODIFIER_BASE = 3,
    EMOJI_MODIFIER = 4,
    EMOJI_VS_BASE = 5,
    REGIONAL_INDICATOR = 6,
    KEYCAP_BASE = 7,
    COMBINING_ENCLOSING_KEYCAP = 8,
    COMBINING_ENCLOSING_CIRCLE_BACKSLASH = 9,
    ZWJ = 10,
    VS15 = 11,
    VS16 = 12,
    TAG_BASE = 13,
    TAG_SEQUENCE = 14,
    TAG_TERM = 15,
    kMaxEmojiScannerCategory = 16
  };

 private:
  UChar32 Codepoint() const {
    DCHECK_GT(buffer_size_, 0u);
    UChar32 output;
    U16_GET(buffer_, 0, cursor_, buffer_size_, output);
    return output;
  }

  void UpdateCachedCategory();

  const UChar* buffer_;
  unsigned buffer_size_;
  unsigned cursor_;
  unsigned char cached_category_;
};

}  // namespace blink

#endif  // THIRD_PARTY_BLINK_RENDERER_PLATFORM_FONTS_UTF16_RAGEL_ITERATOR_H_