• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024 Huawei Device Co., Ltd.. All rights reserved.
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 SKPARAGRAPH_SRC_TEXT_TAB_ALIGN_H
17 #define SKPARAGRAPH_SRC_TEXT_TAB_ALIGN_H
18 
19 #ifdef ENABLE_TEXT_ENHANCE
20 
21 #include <string>
22 #include "include/ParagraphStyle.h"
23 #include "include/core/SkSpan.h"
24 #include "modules/skparagraph/src/TextLine.h"
25 #include "modules/skparagraph/src/TextWrapper.h"
26 
27 namespace skia {
28 namespace textlayout {
29 class TextTabAlign {
30 public:
TextTabAlign(const TextTabs & other)31     explicit TextTabAlign(const TextTabs& other) : fTabAlignMode(other.alignment), fTabPosition(other.location) {}
32     void init(SkScalar maxWidth, Cluster* endOfClusters);
33 
processTab(TextWrapper::TextStretch & words,TextWrapper::TextStretch & clusters,Cluster * currentCluster,SkScalar totalFakeSpacing)34     bool processTab(TextWrapper::TextStretch& words, TextWrapper::TextStretch& clusters, Cluster* currentCluster,
35         SkScalar totalFakeSpacing)
36     {
37         if ((fTextTabFuncs != nullptr) && (this->fTextTabFuncs->processTabFunc != nullptr) &&
38             (currentCluster != nullptr)) {
39             return (this->*(fTextTabFuncs->processTabFunc))(words, clusters, currentCluster, totalFakeSpacing);
40         }
41         return false;
42     }
43 
processEndofWord(TextWrapper::TextStretch & words,TextWrapper::TextStretch & clusters,Cluster * currentCluster,SkScalar totalFakeSpacing)44     bool processEndofWord(TextWrapper::TextStretch& words, TextWrapper::TextStretch& clusters, Cluster* currentCluster,
45         SkScalar totalFakeSpacing)
46     {
47         if ((fTextTabFuncs != nullptr) && (this->fTextTabFuncs->processEndofWordFunc != nullptr) &&
48             (currentCluster != nullptr)) {
49             return (this->*(fTextTabFuncs->processEndofWordFunc))(words, clusters, currentCluster, totalFakeSpacing);
50         }
51         return false;
52     }
53 
processEndofLine(TextWrapper::TextStretch & words,TextWrapper::TextStretch & clusters,Cluster * currentCluster,SkScalar totalFakeSpacing)54     bool processEndofLine(TextWrapper::TextStretch& words, TextWrapper::TextStretch& clusters, Cluster* currentCluster,
55         SkScalar totalFakeSpacing)
56     {
57         if ((fTextTabFuncs != nullptr) && (this->fTextTabFuncs->processEndofLineFunc != nullptr) &&
58             (currentCluster != nullptr)) {
59             return (this->*(fTextTabFuncs->processEndofLineFunc))(words, clusters, currentCluster, totalFakeSpacing);
60         }
61         return false;
62     }
63 
processCluster(TextWrapper::TextStretch & words,TextWrapper::TextStretch & clusters,Cluster * currentCluster,SkScalar totalFakeSpacing)64     bool processCluster(TextWrapper::TextStretch& words, TextWrapper::TextStretch& clusters, Cluster* currentCluster,
65         SkScalar totalFakeSpacing)
66     {
67         if ((fTextTabFuncs != nullptr) && (this->fTextTabFuncs->processClusterFunc != nullptr) &&
68             (currentCluster != nullptr)) {
69             return (this->*(fTextTabFuncs->processClusterFunc))(words, clusters, currentCluster, totalFakeSpacing);
70         }
71         return false;
72     }
73 
74 private:
75     void expendTabCluster(SkScalar width);
76 
77     bool leftAlignProcessTab(TextWrapper::TextStretch& words, TextWrapper::TextStretch& clusters,
78         Cluster* currentCluster, SkScalar totalFakeSpacing);
79     bool leftAlignProcessEndofWord(TextWrapper::TextStretch& words, TextWrapper::TextStretch& clusters,
80         Cluster* currentCluster, SkScalar totalFakeSpacing);
81     bool leftAlignProcessEndofLine(TextWrapper::TextStretch& words, TextWrapper::TextStretch& clusters,
82         Cluster* currentCluster, SkScalar totalFakeSpacing);
83     bool leftAlignProcessCluster(TextWrapper::TextStretch& words, TextWrapper::TextStretch& clusters,
84         Cluster* currentCluster, SkScalar totalFakeSpacing);
85 
86     void rightAlignProcessTabBlockEnd(TextWrapper::TextStretch& words, TextWrapper::TextStretch& clusters);
87     bool rightAlignProcessTab(TextWrapper::TextStretch& words, TextWrapper::TextStretch& clusters,
88         Cluster* currentCluster, SkScalar totalFakeSpacing);
89     bool rightAlignProcessEndofWord(TextWrapper::TextStretch& words, TextWrapper::TextStretch& clusters,
90         Cluster* currentCluster, SkScalar totalFakeSpacing);
91     bool rightAlignProcessEndofLine(TextWrapper::TextStretch& words, TextWrapper::TextStretch& clusters,
92         Cluster* currentCluster, SkScalar totalFakeSpacing);
93     bool rightAlignProcessCluster(TextWrapper::TextStretch& words, TextWrapper::TextStretch& clusters,
94         Cluster* currentCluster, SkScalar totalFakeSpacing);
95 
96     bool centerAlignProcessTabBlockEnd(TextWrapper::TextStretch& words, TextWrapper::TextStretch& clusters);
97     bool centerAlignProcessTab(TextWrapper::TextStretch& words, TextWrapper::TextStretch& clusters,
98         Cluster* currentCluster, SkScalar totalFakeSpacing);
99     bool centerAlignProcessEndofWord(TextWrapper::TextStretch& words, TextWrapper::TextStretch& clusters,
100         Cluster* currentCluster, SkScalar totalFakeSpacing);
101     bool centerAlignProcessEndofLine(TextWrapper::TextStretch& words, TextWrapper::TextStretch& clusters,
102         Cluster* currentCluster, SkScalar totalFakeSpacing);
103     bool centerAlignProcessCluster(TextWrapper::TextStretch& words, TextWrapper::TextStretch& clusters,
104         Cluster* currentCluster, SkScalar totalFakeSpacing);
105 
106     TextAlign fTabAlignMode;
107     SkScalar fTabPosition{0.0};
108     bool fAlreadyInTab{false};
109     SkScalar fTabStartPos{0.0};
110     SkScalar fTabEndPos{0.0};
111     SkScalar fTabShift{0.0};
112     int fTabIndex{0};
113     int fMaxTabIndex{0};
114     Cluster* fTabBlockEnd{nullptr};
115     Cluster* fEndOfClusters{nullptr};
116     SkScalar fMaxWidth{0.0f};
117     Cluster* fTabCluster{nullptr};
118 
119     using TextTabFunc = bool (TextTabAlign::*)(TextWrapper::TextStretch&, TextWrapper::TextStretch&,
120         Cluster*, SkScalar);
121     struct TextTabFuncs {
122         TextTabFunc processTabFunc;
123         TextTabFunc processEndofWordFunc;
124         TextTabFunc processEndofLineFunc;
125         TextTabFunc processClusterFunc;
126     };
127     constexpr static size_t textAlignCount = static_cast<size_t>(TextAlign::kCenter) + 1;
128     static TextTabFuncs fTextTabFuncsTable[textAlignCount];
129     TextTabFuncs* fTextTabFuncs{nullptr};
130 };
131 constexpr uint32_t POSITION_COEFFICIENT = 2;
132 }  // namespace textlayout
133 }  // namespace skia
134 
135 #endif // ENABLE_TEXT_ENHANCE
136 #endif  // SKPARAGRAPH_SRC_TEXT_TAB_ALIGN_H