• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef ROSEN_MODULES_TEXGINE_SRC_CHAR_GROUPS_H
17 #define ROSEN_MODULES_TEXGINE_SRC_CHAR_GROUPS_H
18 
19 #include <array>
20 #include <cstdint>
21 #include <memory>
22 #include <ostream>
23 #include <vector>
24 
25 namespace OHOS {
26 namespace Rosen {
27 namespace TextEngine {
28 struct Glyph {
29     uint32_t codepoint;
30     double advanceX;
31     double advanceY;
32     double offsetX;
33     double offsetY;
34 };
35 
36 class Typeface;
37 struct CharGroup {
38     std::vector<uint16_t> chars;
39     std::vector<struct Glyph> glyphs;
40     std::shared_ptr<Typeface> typeface;
41     double visibleWidth = 0;
42     double invisibleWidth = 0;
43 
GetWidthCharGroup44     double GetWidth() const
45     {
46         return visibleWidth + invisibleWidth;
47     }
48 
GetHeightCharGroup49     double GetHeight() const
50     {
51         double maxAdvanceY = 0;
52         for (const auto &glyph : glyphs) {
53             maxAdvanceY = std::max(maxAdvanceY, glyph.advanceY);
54         }
55         return maxAdvanceY;
56     }
57 
CheckCodePointCharGroup58     bool CheckCodePoint()
59     {
60         if (!glyphs.size()) {
61             return false;
62         }
63         for (const auto& glyph : glyphs) {
64             if (!glyph.codepoint) {
65                 return false;
66             }
67         }
68         return true;
69     };
70 };
71 
72 struct IndexRange {
73     int start = 0;
74     int end = 0;
75 
76     bool operator==(IndexRange const &range) const
77     {
78         return range.start == start && range.end == end;
79     }
80 };
81 
82 std::ostream &operator<<(std::ostream &os, const struct IndexRange &range);
83 class CharGroups;
84 // 2 is the count of CharGroups in the return value
85 using CharGroupsPair = std::array<CharGroups, 2>;
86 class CharGroups {
87 public:
88     static CharGroups CreateEmpty();
89     static CharGroups CreateWithInvalidRange(IndexRange range);
90 
91     size_t GetNumberOfGlyph() const;
92     size_t GetNumberOfCharGroup() const;
93     const IndexRange &GetRange() const;
94     CharGroup &GetBack() const;
95     size_t GetSize() const;
96 
97     bool IsValid() const;
98     bool IsSameCharGroups(const CharGroups &right) const;
99     bool IsIntersect(const CharGroups &right) const;
100 
101     CharGroupsPair GetSplit(const int &index) const;
102     CharGroupsPair GetSplitAll(const int &index) const;
103     CharGroups GetSub(const int &start, const int &end) const;
104     CharGroups GetSubAll(const int &start, const int &end) const;
105     CharGroups GetSubFromU16RangeAll(const int &u16start, const int &u16end) const;
106     CharGroups GetIntersect(const CharGroups &right) const;
107     struct CharGroup &Get(const int32_t &index) const;
108     struct CharGroup &GetAll(const int32_t &index) const;
109 
110     std::vector<uint16_t> ToUTF16() const;
111     std::vector<uint16_t> ToUTF16All() const;
112 
113     std::vector<struct CharGroup>::iterator begin() const;
114     std::vector<struct CharGroup>::iterator end() const;
115 
116     CharGroups Clone() const;
117 
118     void Merge(const CharGroups &right);
119     void PushBack(const struct CharGroup &cg);
120     void ReverseAll();
121     bool operator==(CharGroups const &cgs) const
122     {
123         return IsSameCharGroups(cgs) && range_ == cgs.range_;
124     }
125 
126     bool CheckCodePoint();
127 private:
128     friend void ReportMemoryUsage(const std::string &member, const CharGroups &that, bool needThis);
129 
130     std::shared_ptr<std::vector<struct CharGroup>> pcgs_ = nullptr;
131     struct IndexRange range_ = {0, 0};
132 };
133 } // namespace TextEngine
134 } // namespace Rosen
135 } // namespace OHOS
136 
137 #endif // ROSEN_MODULES_TEXGINE_SRC_CHAR_GROUPS_H
138