• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024 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 #include "text_line_base.h"
17 #include "convert.h"
18 #include "run_impl.h"
19 
20 namespace OHOS {
21 namespace Rosen {
22 namespace AdapterTxt {
TextLineBaseImpl(std::unique_ptr<SPText::TextLineBase> textlinebase)23 TextLineBaseImpl::TextLineBaseImpl(std::unique_ptr<SPText::TextLineBase> textlinebase)
24     : textlinebase_(std::move(textlinebase))
25 {
26 }
27 
GetGlyphCount() const28 size_t TextLineBaseImpl::GetGlyphCount() const
29 {
30     if (!textlinebase_) {
31         return 0;
32     }
33     return textlinebase_->GetGlyphCount();
34 }
35 
GetGlyphRuns() const36 std::vector<std::unique_ptr<Run>> TextLineBaseImpl::GetGlyphRuns() const
37 {
38     if (!textlinebase_) {
39         return {};
40     }
41 
42     std::vector<std::unique_ptr<SPText::Run>> textRuns = textlinebase_->GetGlyphRuns();
43     std::vector<std::unique_ptr<Run>> runs;
44 
45     for (std::unique_ptr<SPText::Run>& textRun : textRuns) {
46         std::unique_ptr<RunImpl> runPtr = std::make_unique<RunImpl>(std::move(textRun));
47         runs.emplace_back(std::move(runPtr));
48     }
49     return runs;
50 }
51 
GetTextRange() const52 Boundary TextLineBaseImpl::GetTextRange() const
53 {
54     if (!textlinebase_) {
55         Boundary boundary(0, 0);
56         return boundary;
57     }
58 
59     return Convert(textlinebase_->GetTextRange());
60 }
61 
Paint(Drawing::Canvas * canvas,double x,double y)62 void TextLineBaseImpl::Paint(Drawing::Canvas *canvas, double x, double y)
63 {
64     if (!textlinebase_) {
65         return;
66     }
67     return textlinebase_->Paint(canvas, x, y);
68 }
69 
CreateTruncatedLine(double width,EllipsisModal ellipsisMode,const std::string & ellipsisStr) const70 std::unique_ptr<TextLineBase> TextLineBaseImpl::CreateTruncatedLine(double width, EllipsisModal ellipsisMode,
71     const std::string& ellipsisStr) const
72 {
73     if (!textlinebase_) {
74         return nullptr;
75     }
76 
77     std::unique_ptr<SPText::TextLineBase> textLine = textlinebase_->CreateTruncatedLine(width,
78         static_cast<OHOS::Rosen::SPText::EllipsisModal>(ellipsisMode), ellipsisStr);
79     if (textLine == nullptr) {
80         return nullptr;
81     }
82 
83     return std::make_unique<TextLineBaseImpl>(std::move(textLine));
84 }
85 
GetTypographicBounds(double * ascent,double * descent,double * leading) const86 double TextLineBaseImpl::GetTypographicBounds(double* ascent, double* descent, double* leading) const
87 {
88     if (!textlinebase_) {
89         return 0.0;
90     }
91     return textlinebase_->GetTypographicBounds(ascent, descent, leading);
92 }
93 
GetImageBounds() const94 Drawing::Rect TextLineBaseImpl::GetImageBounds() const
95 {
96     if (!textlinebase_) {
97         return {};
98     }
99     return textlinebase_->GetImageBounds();
100 }
101 
GetTrailingSpaceWidth() const102 double TextLineBaseImpl::GetTrailingSpaceWidth() const
103 {
104     if (!textlinebase_) {
105         return 0.0;
106     }
107     return textlinebase_->GetTrailingSpaceWidth();
108 }
109 
GetStringIndexForPosition(SkPoint point) const110 int32_t TextLineBaseImpl::GetStringIndexForPosition(SkPoint point) const
111 {
112     if (!textlinebase_) {
113         return 0;
114     }
115     return textlinebase_->GetStringIndexForPosition(point);
116 }
117 
GetOffsetForStringIndex(int32_t index) const118 double TextLineBaseImpl::GetOffsetForStringIndex(int32_t index) const
119 {
120     if (!textlinebase_) {
121         return 0.0;
122     }
123     return textlinebase_->GetOffsetForStringIndex(index);
124 }
125 
GetIndexAndOffsets(bool & isHardBreak) const126 std::map<int32_t, double> TextLineBaseImpl::GetIndexAndOffsets(bool& isHardBreak) const
127 {
128     if (!textlinebase_) {
129         return {};
130     }
131     return textlinebase_->GetIndexAndOffsets(isHardBreak);
132 }
133 
GetAlignmentOffset(double alignmentFactor,double alignmentWidth) const134 double TextLineBaseImpl::GetAlignmentOffset(double alignmentFactor, double alignmentWidth) const
135 {
136     if (!textlinebase_) {
137         return 0.0;
138     }
139     return textlinebase_->GetAlignmentOffset(alignmentFactor, alignmentWidth);
140 }
141 } // namespace AdapterTxt
142 } // namespace Rosen
143 } // namespace OHOS
144