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 "run_impl.h"
17 #include "drawing_painter_impl.h"
18 #include "typography_types.h"
19 #include "text/font.h"
20
21 namespace OHOS {
22 namespace Rosen {
23 namespace SPText {
24 namespace skt = skia::textlayout;
25
RunImpl(std::unique_ptr<skt::RunBase> runBase,const std::vector<PaintRecord> & paints)26 RunImpl::RunImpl(std::unique_ptr<skt::RunBase> runBase, const std::vector<PaintRecord>& paints)
27 : runBase_(std::move(runBase)), paints_(paints)
28 {}
29
GetFont() const30 Drawing::Font RunImpl::GetFont() const
31 {
32 if (!runBase_) {
33 return {};
34 }
35 return runBase_->font();
36 }
37
GetGlyphCount() const38 size_t RunImpl::GetGlyphCount() const
39 {
40 if (!runBase_) {
41 return 0;
42 }
43 return runBase_->size();
44 }
45
GetGlyphs() const46 std::vector<uint16_t> RunImpl::GetGlyphs() const
47 {
48 if (!runBase_) {
49 return {};
50 }
51 return runBase_->getGlyphs();
52 }
53
GetPositions()54 std::vector<Drawing::Point> RunImpl::GetPositions()
55 {
56 if (!runBase_) {
57 return {};
58 }
59 return runBase_->getPositions();
60 }
61
GetOffsets()62 std::vector<Drawing::Point> RunImpl::GetOffsets()
63 {
64 if (!runBase_) {
65 return {};
66 }
67 return runBase_->getOffsets();
68 }
69
Paint(Drawing::Canvas * canvas,double x,double y)70 void RunImpl::Paint(Drawing::Canvas* canvas, double x, double y)
71 {
72 if (!runBase_ || !canvas) {
73 return;
74 }
75 RSCanvasParagraphPainter painter(canvas, paints_);
76 runBase_->paint(&painter, x, y);
77 }
78
GetGlyphs(int64_t start,int64_t length) const79 std::vector<uint16_t> RunImpl::GetGlyphs(int64_t start, int64_t length) const
80 {
81 if (runBase_ == nullptr || start < 0 || length < 0) {
82 return {};
83 }
84 return runBase_->getGlyphs(start, length);
85 }
86
GetPositions(int64_t start,int64_t length) const87 std::vector<Drawing::Point> RunImpl::GetPositions(int64_t start, int64_t length) const
88 {
89 if (runBase_ == nullptr || start < 0 || length < 0) {
90 return {};
91 }
92 return runBase_->getPositions(start, length);
93 }
94
GetAdvances(uint32_t start,uint32_t length) const95 std::vector<Drawing::Point> RunImpl::GetAdvances(uint32_t start, uint32_t length) const
96 {
97 if (runBase_ == nullptr) {
98 return {};
99 }
100 return runBase_->getAdvances(start, length);
101 }
102
GetTextDirection() const103 TextDirection RunImpl::GetTextDirection() const
104 {
105 if (runBase_ == nullptr) {
106 return TextDirection::LTR;
107 }
108
109 if (runBase_->getTextDirection() == skia::textlayout::TextDirection::kRtl) {
110 return TextDirection::RTL;
111 } else {
112 return TextDirection::LTR;
113 }
114 }
115
GetStringRange(uint64_t * location,uint64_t * length) const116 void RunImpl::GetStringRange(uint64_t* location, uint64_t* length) const
117 {
118 if (location == nullptr || length == nullptr) {
119 return;
120 } else if (runBase_ == nullptr) {
121 *location = 0;
122 *length = 0;
123 return;
124 }
125 runBase_->getStringRange(location, length);
126 }
127
GetStringIndices(int64_t start,int64_t length) const128 std::vector<uint64_t> RunImpl::GetStringIndices(int64_t start, int64_t length) const
129 {
130 if (runBase_ == nullptr || start < 0 || length < 0) {
131 return {};
132 }
133 return runBase_->getStringIndices(start, length);
134 }
135
GetImageBounds() const136 Drawing::Rect RunImpl::GetImageBounds() const
137 {
138 if (runBase_ == nullptr) {
139 return {};
140 }
141 return runBase_->getImageBounds();
142 }
143
GetTypographicBounds(float * ascent,float * descent,float * leading) const144 float RunImpl::GetTypographicBounds(float* ascent, float* descent, float* leading) const
145 {
146 if (ascent == nullptr || descent == nullptr || leading == nullptr) {
147 return 0.0;
148 } else if (runBase_ == nullptr) {
149 *ascent = 0.0;
150 *descent = 0.0;
151 *leading = 0.0;
152 return 0.0;
153 }
154 return runBase_->getTypographicBounds(ascent, descent, leading);
155 }
156 } // namespace SPText
157 } // namespace Rosen
158 } // namespace OHOS
159