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 "modules/skparagraph/src/TextLineBaseImpl.h"
17
18 namespace skia {
19 namespace textlayout {
20 #ifdef OHOS_SUPPORT
TextLineBaseImpl(std::unique_ptr<TextLine> visitorTextLine)21 TextLineBaseImpl::TextLineBaseImpl(std::unique_ptr<TextLine> visitorTextLine)
22 : fVisitorTextLine(std::move(visitorTextLine))
23 #else
24 TextLineBaseImpl::TextLineBaseImpl(TextLine* visitorTextLine) : fVisitorTextLine(visitorTextLine)
25 #endif
26 {
27 }
28
getGlyphCount() const29 size_t TextLineBaseImpl::getGlyphCount() const
30 {
31 if (!fVisitorTextLine) {
32 return 0;
33 }
34 fVisitorTextLine->ensureTextBlobCachePopulated();
35 return fVisitorTextLine->getGlyphCount();
36 }
37
getGlyphRuns() const38 std::vector<std::unique_ptr<RunBase>> TextLineBaseImpl::getGlyphRuns() const
39 {
40 if (!fVisitorTextLine) {
41 return {};
42 }
43 fVisitorTextLine->ensureTextBlobCachePopulated();
44 return fVisitorTextLine->getGlyphRuns();
45 }
46
getTextRange() const47 SkRange<size_t> TextLineBaseImpl::getTextRange() const
48 {
49 if (!fVisitorTextLine) {
50 return {};
51 }
52 return fVisitorTextLine->text();
53 }
54
paint(ParagraphPainter* painter, SkScalar x, SkScalar y)55 void TextLineBaseImpl::paint(ParagraphPainter* painter, SkScalar x, SkScalar y)
56 {
57 if (!fVisitorTextLine || !painter) {
58 return;
59 }
60 return fVisitorTextLine->paint(painter, x, y);
61 }
62
63 #ifdef OHOS_SUPPORT
createTruncatedLine(double width, EllipsisModal ellipsisMode, const std::string& ellipsisStr)64 std::unique_ptr<TextLineBase> TextLineBaseImpl::createTruncatedLine(double width, EllipsisModal ellipsisMode,
65 const std::string& ellipsisStr)
66 {
67 if (!fVisitorTextLine) {
68 return nullptr;
69 }
70
71 return fVisitorTextLine->createTruncatedLine(width, ellipsisMode, ellipsisStr);
72 }
73
getTypographicBounds(double* ascent, double* descent, double* leading) const74 double TextLineBaseImpl::getTypographicBounds(double* ascent, double* descent, double* leading) const
75 {
76 if (!fVisitorTextLine) {
77 return 0.0;
78 }
79
80 return fVisitorTextLine->getTypographicBounds(ascent, descent, leading);
81 }
82
getImageBounds() const83 RSRect TextLineBaseImpl::getImageBounds() const
84 {
85 if (!fVisitorTextLine) {
86 return {};
87 }
88
89 return fVisitorTextLine->getImageBounds();
90 }
91
getTrailingSpaceWidth() const92 double TextLineBaseImpl::getTrailingSpaceWidth() const
93 {
94 if (!fVisitorTextLine) {
95 return 0.0;
96 }
97
98 return fVisitorTextLine->getTrailingSpaceWidth();
99 }
100
getStringIndexForPosition(SkPoint point) const101 int32_t TextLineBaseImpl::getStringIndexForPosition(SkPoint point) const
102 {
103 if (!fVisitorTextLine) {
104 return 0;
105 }
106
107 return fVisitorTextLine->getStringIndexForPosition(point);
108 }
109
getOffsetForStringIndex(int32_t index) const110 double TextLineBaseImpl::getOffsetForStringIndex(int32_t index) const
111 {
112 if (!fVisitorTextLine) {
113 return 0.0;
114 }
115
116 return fVisitorTextLine->getOffsetForStringIndex(index);
117 }
118
getIndexAndOffsets(bool& isHardBreak) const119 std::map<int32_t, double> TextLineBaseImpl::getIndexAndOffsets(bool& isHardBreak) const
120 {
121 if (!fVisitorTextLine) {
122 return {};
123 }
124
125 return fVisitorTextLine->getIndexAndOffsets(isHardBreak);
126 }
127
getAlignmentOffset(double alignmentFactor, double alignmentWidth) const128 double TextLineBaseImpl::getAlignmentOffset(double alignmentFactor, double alignmentWidth) const
129 {
130 if (!fVisitorTextLine) {
131 return 0.0;
132 }
133
134 return fVisitorTextLine->getAlignmentOffset(alignmentFactor, alignmentWidth);
135 }
136 #endif
137 } // namespace textlayout
138 } // namespace skia