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/RunBaseImpl.h"
17
18 namespace skia {
19 namespace textlayout {
RunBaseImpl(sk_sp<SkTextBlob> blob,SkPoint offset,ParagraphPainter::SkPaintOrID paint,bool clippingNeeded,SkRect clipRect,const Run * visitorRun,size_t visitorPos,size_t visitorSize)20 RunBaseImpl::RunBaseImpl(
21 #ifndef USE_SKIA_TXT
22 sk_sp<SkTextBlob> blob,
23 #else
24 std::shared_ptr<RSTextBlob> blob,
25 #endif
26 SkPoint offset,
27 ParagraphPainter::SkPaintOrID paint,
28 bool clippingNeeded,
29 SkRect clipRect,
30 const Run* visitorRun,
31 size_t visitorPos,
32 size_t visitorSize)
33 : fBlob(blob),
34 fOffset(offset),
35 fClippingNeeded(clippingNeeded),
36 fClipRect(clipRect),
37 fVisitorRun(visitorRun),
38 fVisitorPos(visitorPos),
39 fVisitorSize(visitorSize)
40 {
41 if (std::holds_alternative<SkPaint>(paint)) {
42 fPaint = std::get<SkPaint>(paint);
43 } else if (std::holds_alternative<ParagraphPainter::PaintID>(paint)) {
44 fPaint = std::get<ParagraphPainter::PaintID>(paint);
45 }
46
47 }
48
49 #ifndef USE_SKIA_TXT
font() const50 const SkFont& RunBaseImpl::font() const
51 #else
52 const RSFont& RunBaseImpl::font() const
53 #endif
54 {
55 if (!fVisitorRun) {
56 return {};
57 }
58 return fVisitorRun->font();
59 }
60
size() const61 size_t RunBaseImpl::size() const
62 {
63 return fVisitorSize;
64 }
65
getGlyphs() const66 std::vector<uint16_t> RunBaseImpl::getGlyphs() const
67 {
68 if (!fVisitorRun) {
69 return {};
70 }
71 SkSpan<const SkGlyphID> glyphIDSpan = fVisitorRun->glyphs();
72 SkSpan<const SkGlyphID> runGlyphIDSpan = glyphIDSpan.subspan(fVisitorPos, fVisitorSize);
73 return std::vector<uint16_t>(runGlyphIDSpan.begin(), runGlyphIDSpan.end());
74 }
75
getPositions() const76 std::vector<RSPoint> RunBaseImpl::getPositions() const
77 {
78 if (!fVisitorRun) {
79 return {};
80 }
81 SkSpan<const SkPoint> positionSpan = fVisitorRun->positions();
82 SkSpan<const SkPoint> runPositionSpan = positionSpan.subspan(fVisitorPos, fVisitorSize);
83 std::vector<RSPoint> positions;
84 for (size_t i = 0; i < runPositionSpan.size(); i++) {
85 RSPoint point(runPositionSpan[i].fX, runPositionSpan[i].fY);
86 positions.emplace_back(point);
87 }
88
89 return positions;
90
91 }
92
getOffsets() const93 std::vector<RSPoint> RunBaseImpl::getOffsets() const
94 {
95 if (!fVisitorRun) {
96 return {};
97 }
98 SkSpan<const SkPoint> offsetSpan = fVisitorRun->offsets();
99 SkSpan<const SkPoint> runOffsetSpan = offsetSpan.subspan(fVisitorPos, fVisitorSize);
100 std::vector<RSPoint> offsets;
101 for (size_t i = 0; i < runOffsetSpan.size(); i++) {
102 RSPoint point(runOffsetSpan[i].fX, runOffsetSpan[i].fY);
103 offsets.emplace_back(point);
104 }
105
106 return offsets;
107
108 }
109
paint(ParagraphPainter * painter,SkScalar x,SkScalar y)110 void RunBaseImpl::paint(ParagraphPainter* painter, SkScalar x, SkScalar y)
111 {
112 if (!painter) {
113 return;
114 }
115 if (fClippingNeeded) {
116 painter->save();
117 painter->clipRect(fClipRect.makeOffset(x, y));
118 }
119 painter->drawTextBlob(fBlob, x + fOffset.x(), y + fOffset.y(), fPaint);
120 if (fClippingNeeded) {
121 painter->restore();
122 }
123 }
124
getVisitorPos() const125 size_t RunBaseImpl::getVisitorPos() const
126 {
127 return fVisitorPos;
128 }
129
getVisitorSize() const130 size_t RunBaseImpl::getVisitorSize() const
131 {
132 return fVisitorSize;
133 }
134
135 } // namespace textlayout
136 } // namespace skia