1 /*
2 * Copyright (c) 2022 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 "engine_adapter/skia_adapter/skia_canvas.h"
17 #include "rosen_text/properties/rosen_converter_txt.h"
18 #include "rosen_text/properties/typography_create_base.h"
19 #include "rosen_text/properties/typography_create_txt.h"
20 #include "rosen_text/properties/typography_txt.h"
21 #include "txt/paragraph_txt.h"
22
23 namespace rosen {
TypographyTxt()24 TypographyTxt::TypographyTxt()
25 {
26 paragraphTxt_ = std::make_unique<txt::ParagraphTxt>();
27 }
28
~TypographyTxt()29 TypographyTxt::~TypographyTxt()
30 {
31 }
32
Init(std::shared_ptr<TypographyCreateBase> typographyCreateBase)33 void TypographyTxt::Init(std::shared_ptr<TypographyCreateBase> typographyCreateBase)
34 {
35 const TypographyCreateTxt* typographyCreateTxt = static_cast<const TypographyCreateTxt*>(
36 typographyCreateBase.get());
37 paragraphTxt_ = typographyCreateTxt->GetParagraphBuilderTxt()->Build();
38 }
39
40
GetMaxWidth()41 double TypographyTxt::GetMaxWidth()
42 {
43 return paragraphTxt_->GetMaxWidth();
44 }
45
GetHeight()46 double TypographyTxt::GetHeight()
47 {
48 return paragraphTxt_->GetHeight();
49 }
50
GetLongestLine()51 double TypographyTxt::GetLongestLine()
52 {
53 return paragraphTxt_->GetLongestLine();
54 }
55
GetMinIntrinsicWidth()56 double TypographyTxt::GetMinIntrinsicWidth()
57 {
58 return paragraphTxt_->GetMinIntrinsicWidth();
59 }
60
GetMaxIntrinsicWidth()61 double TypographyTxt::GetMaxIntrinsicWidth()
62 {
63 return paragraphTxt_->GetMaxIntrinsicWidth();
64 }
65
GetAlphabeticBaseline()66 double TypographyTxt::GetAlphabeticBaseline()
67 {
68 return paragraphTxt_->GetAlphabeticBaseline();
69 }
70
GetIdeographicBaseline()71 double TypographyTxt::GetIdeographicBaseline()
72 {
73 return paragraphTxt_->GetIdeographicBaseline();
74 }
75
DidExceedMaxLines()76 bool TypographyTxt::DidExceedMaxLines()
77 {
78 return paragraphTxt_->DidExceedMaxLines();
79 }
80
Layout(double width)81 void TypographyTxt::Layout(double width)
82 {
83 paragraphTxt_->Layout(width);
84 }
85
Paint(Canvas * drawCanvas,double x,double y)86 void TypographyTxt::Paint(Canvas* drawCanvas, double x, double y)
87 {
88 std::shared_ptr<CoreCanvasImpl> coreCanvas = drawCanvas->GetCanvasData();
89 const SkiaCanvas* skiacavas = static_cast<const SkiaCanvas*>(coreCanvas.get());
90 SkCanvas *canvas = skiacavas->ExportSkCanvas();
91 paragraphTxt_->Paint(canvas, x, y);
92 }
93
GetRectsForRange(size_t start,size_t end,TypographyProperties::RectHeightStyle heightStyle,TypographyProperties::RectWidthStyle widthStyle)94 std::vector<TypographyProperties::TextBox> TypographyTxt::GetRectsForRange(
95 size_t start,
96 size_t end,
97 TypographyProperties::RectHeightStyle heightStyle,
98 TypographyProperties::RectWidthStyle widthStyle)
99 {
100 txt::Paragraph::RectHeightStyle txtHeightStyle = RosenConvertTxtRectHeightStyle(heightStyle);
101 txt::Paragraph::RectWidthStyle txtWidthStyle = RosenConvertTxtRectWidthStyle(widthStyle);
102 std::vector<txt::Paragraph::TextBox> txtTextBox =
103 paragraphTxt_->GetRectsForRange(start, end, txtHeightStyle, txtWidthStyle);
104 std::vector<TypographyProperties::TextBox> rosenTextBox;
105 for (auto& txtbox : txtTextBox) {
106 rosenTextBox.push_back(TxtConvertRosenTextBox(txtbox));
107 }
108 return rosenTextBox;
109 }
110
GetRectsForPlaceholders()111 std::vector<TypographyProperties::TextBox> TypographyTxt::GetRectsForPlaceholders()
112 {
113 std::vector<txt::Paragraph::TextBox> txtTextBox = paragraphTxt_->GetRectsForPlaceholders();
114 std::vector<TypographyProperties::TextBox> rosenTextBox;
115 for (auto& txtbox : txtTextBox) {
116 rosenTextBox.push_back(TxtConvertRosenTextBox(txtbox));
117 }
118 return rosenTextBox;
119 }
120
GetGlyphPositionAtCoordinate(double dx,double dy)121 TypographyProperties::PositionAndAffinity TypographyTxt::GetGlyphPositionAtCoordinate(double dx, double dy)
122 {
123 txt::Paragraph::PositionWithAffinity posAndAffinity = paragraphTxt_->GetGlyphPositionAtCoordinate(dx, dy);
124 return TxtConvertPosAndAffinity(posAndAffinity);
125 }
126
GetGlyphPositionAtCoordinateWithCluster(double dx,double dy)127 TypographyProperties::PositionAndAffinity TypographyTxt::GetGlyphPositionAtCoordinateWithCluster(double dx, double dy)
128 {
129 txt::Paragraph::PositionWithAffinity posAndAffinity =
130 #if defined(USE_CANVASKIT0310_SKIA) || defined(NEW_SKIA)
131 // new flutter libtxt not have GetGlyphPositionAtCoordinateWithCluster
132 paragraphTxt_->GetGlyphPositionAtCoordinate(dx, dy);
133 #else
134 paragraphTxt_->GetGlyphPositionAtCoordinateWithCluster(dx, dy);
135 #endif
136 return TxtConvertPosAndAffinity(posAndAffinity);
137 }
138
GetWordBoundary(size_t offset)139 TypographyProperties::Range<size_t> TypographyTxt::GetWordBoundary(size_t offset)
140 {
141 txt::Paragraph::Range<size_t> range = paragraphTxt_->GetWordBoundary(offset);
142 return TxtConvertRange(range);
143 }
144 } // namespace rosen
145