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 
18 #include "drawing_painter_impl.h"
19 #include "run_impl.h"
20 
21 namespace OHOS {
22 namespace Rosen {
23 namespace SPText {
24 namespace skt = skia::textlayout;
TextLineImpl(std::unique_ptr<skt::TextLineBase> textLineBase,const std::vector<PaintRecord> & paints)25 TextLineImpl::TextLineImpl(std::unique_ptr<skt::TextLineBase> textLineBase, const std::vector<PaintRecord>& paints)
26     : textLineBase_(std::move(textLineBase)), paints_(paints)
27 {}
28 
GetGlyphCount() const29 size_t TextLineImpl::GetGlyphCount() const
30 {
31     if (!textLineBase_) {
32         return 0;
33     }
34     return textLineBase_->getGlyphCount();
35 }
36 
GetGlyphRuns() const37 std::vector<std::unique_ptr<Run>> TextLineImpl::GetGlyphRuns() const
38 {
39     if (!textLineBase_) {
40         return {};
41     }
42 
43     std::vector<std::unique_ptr<skt::RunBase>> runBases = textLineBase_->getGlyphRuns();
44     std::vector<std::unique_ptr<SPText::Run>> runs;
45     for (std::unique_ptr<skt::RunBase>& runBase : runBases) {
46         std::unique_ptr<SPText::RunImpl> runImplPtr = std::make_unique<SPText::RunImpl>(std::move(runBase), paints_);
47         runs.emplace_back(std::move(runImplPtr));
48     }
49     return runs;
50 }
51 
GetTextRange() const52 Range<size_t> TextLineImpl::GetTextRange() const
53 {
54     if (!textLineBase_) {
55         Range<size_t> range(0, 0);
56         return range;
57     }
58     skt::SkRange<size_t> range = textLineBase_->getTextRange();
59     return Range<size_t>(range.start, range.end);
60 }
61 
Paint(Drawing::Canvas * canvas,double x,double y)62 void TextLineImpl::Paint(Drawing::Canvas* canvas, double x, double y)
63 {
64     if (!textLineBase_ || !canvas) {
65         return;
66     }
67     RSCanvasParagraphPainter painter(canvas, paints_);
68     textLineBase_->paint(&painter, x, y);
69 }
70 } // namespace SPText
71 } // namespace Rosen
72 } // namespace OHOS