Flutter Engine
WordBreaker.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2015 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 /**
18  * A wrapper around ICU's line break iterator, that gives customized line
19  * break opportunities, as well as identifying words for the purpose of
20  * hyphenation.
21  */
22 
23 #ifndef MINIKIN_WORD_BREAKER_H
24 #define MINIKIN_WORD_BREAKER_H
25 
26 #include <memory>
27 #include "unicode/brkiter.h"
28 #include "utils/WindowsUtils.h"
29 
30 namespace minikin {
31 
32 class WordBreaker {
33  public:
35 
36  void setLocale(const icu::Locale& locale);
37 
38  void setText(const uint16_t* data, size_t size);
39 
40  // Advance iterator to next word break. Return offset, or -1 if EOT
41  ssize_t next();
42 
43  // Current offset of iterator, equal to 0 at BOT or last return from next()
44  ssize_t current() const;
45 
46  // After calling next(), wordStart() and wordEnd() are offsets defining the
47  // previous word. If wordEnd <= wordStart, it's not a word for the purpose of
48  // hyphenation.
49  ssize_t wordStart() const;
50 
51  ssize_t wordEnd() const;
52 
53  int breakBadness() const;
54 
55  void finish();
56 
57  private:
58  int32_t iteratorNext();
59  void detectEmailOrUrl();
60  ssize_t findNextBreakInEmailOrUrl();
61 
62  std::unique_ptr<icu::BreakIterator> mBreakIterator;
63  UText mUText = UTEXT_INITIALIZER;
64  const uint16_t* mText = nullptr;
65  size_t mTextSize;
66  ssize_t mLast;
67  ssize_t mCurrent;
68  bool mIteratorWasReset;
69 
70  // state for the email address / url detector
71  ssize_t mScanOffset;
72  bool mInEmailOrUrl;
73 };
74 
75 } // namespace minikin
76 
77 #endif // MINIKIN_WORD_BREAKER_H
void setLocale(const icu::Locale &locale)
constexpr std::size_t size(T(&array)[N])
Definition: size.h:13
ssize_t current() const
int breakBadness() const
ssize_t wordStart() const
void setText(const uint16_t *data, size_t size)
ssize_t wordEnd() const