• 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 #include "text_line_impl.h"
17 #include "drawing_painter_impl.h"
18 #include "run_impl.h"
19 
20 namespace OHOS {
21 namespace Rosen {
22 namespace SPText {
23 namespace skt = skia::textlayout;
TextLineImpl(std::unique_ptr<skt::TextLineBase> textLineBase,const std::vector<PaintRecord> & paints)24 TextLineImpl::TextLineImpl(std::unique_ptr<skt::TextLineBase> textLineBase, const std::vector<PaintRecord>& paints)
25     : textLineBase_(std::move(textLineBase)), paints_(paints)
26 {}
27 
GetGlyphCount() const28 size_t TextLineImpl::GetGlyphCount() const
29 {
30     if (!textLineBase_) {
31         return 0;
32     }
33     return textLineBase_->getGlyphCount();
34 }
35 
GetGlyphRuns() const36 std::vector<std::unique_ptr<Run>> TextLineImpl::GetGlyphRuns() const
37 {
38     if (!textLineBase_) {
39         return {};
40     }
41 
42     std::vector<std::unique_ptr<skt::RunBase>> runBases = textLineBase_->getGlyphRuns();
43     std::vector<std::unique_ptr<SPText::Run>> runs;
44     for (std::unique_ptr<skt::RunBase>& runBase : runBases) {
45         std::unique_ptr<SPText::RunImpl> runImplPtr = std::make_unique<SPText::RunImpl>(std::move(runBase), paints_);
46         runs.emplace_back(std::move(runImplPtr));
47     }
48     return runs;
49 }
50 
GetTextRange() const51 Range<size_t> TextLineImpl::GetTextRange() const
52 {
53     if (!textLineBase_) {
54         Range<size_t> range(0, 0);
55         return range;
56     }
57     skt::SkRange<size_t> range = textLineBase_->getTextRange();
58     return Range<size_t>(range.start, range.end);
59 }
60 
Paint(Drawing::Canvas * canvas,double x,double y)61 void TextLineImpl::Paint(Drawing::Canvas* canvas, double x, double y)
62 {
63     if (!textLineBase_ || !canvas) {
64         return;
65     }
66     RSCanvasParagraphPainter painter(canvas, paints_);
67     textLineBase_->paint(&painter, x, y);
68 }
69 
CreateTruncatedLine(double width,EllipsisModal ellipsisMode,const std::string & ellipsisStr) const70 std::unique_ptr<TextLineBase> TextLineImpl::CreateTruncatedLine(double width, EllipsisModal ellipsisMode,
71     const std::string& ellipsisStr) const
72 {
73     if (!textLineBase_) {
74         return nullptr;
75     }
76 
77     std::unique_ptr<skt::TextLineBase> textLine = textLineBase_->createTruncatedLine(width,
78         static_cast<skt::EllipsisModal>(ellipsisMode), ellipsisStr);
79     if (textLine == nullptr) {
80         return nullptr;
81     }
82 
83     return std::make_unique<SPText::TextLineImpl>(std::move(textLine), paints_);
84 }
85 
GetTypographicBounds(double * ascent,double * descent,double * leading) const86 double TextLineImpl::GetTypographicBounds(double* ascent, double* descent, double* leading) const
87 {
88     if (!textLineBase_) {
89         return 0.0;
90     }
91 
92     return textLineBase_->getTypographicBounds(ascent, descent, leading);
93 }
94 
GetImageBounds() const95 Drawing::Rect TextLineImpl::GetImageBounds() const
96 {
97     if (!textLineBase_) {
98         return {};
99     }
100 
101     return textLineBase_->getImageBounds();
102 }
103 
GetTrailingSpaceWidth() const104 double TextLineImpl::GetTrailingSpaceWidth() const
105 {
106     if (!textLineBase_) {
107         return 0.0;
108     }
109 
110     return textLineBase_->getTrailingSpaceWidth();
111 }
112 
GetStringIndexForPosition(SkPoint point) const113 int32_t TextLineImpl::GetStringIndexForPosition(SkPoint point) const
114 {
115     if (!textLineBase_) {
116         return 0;
117     }
118     return textLineBase_->getStringIndexForPosition(point);
119 }
120 
GetOffsetForStringIndex(int32_t index) const121 double TextLineImpl::GetOffsetForStringIndex(int32_t index) const
122 {
123     if (!textLineBase_) {
124         return 0.0;
125     }
126 
127     return textLineBase_->getOffsetForStringIndex(index);
128 }
129 
GetIndexAndOffsets(bool & isHardBreak) const130 std::map<int32_t, double> TextLineImpl::GetIndexAndOffsets(bool& isHardBreak) const
131 {
132     if (!textLineBase_) {
133         return {};
134     }
135 
136     return textLineBase_->getIndexAndOffsets(isHardBreak);
137 }
138 
GetAlignmentOffset(double alignmentFactor,double alignmentWidth) const139 double TextLineImpl::GetAlignmentOffset(double alignmentFactor, double alignmentWidth) const
140 {
141     if (!textLineBase_) {
142         return 0.0;
143     }
144 
145     return textLineBase_->getAlignmentOffset(alignmentFactor, alignmentWidth);
146 }
147 } // namespace SPText
148 } // namespace Rosen
149 } // namespace OHOS