1 /*
2 * Copyright (c) 2023 Huawei Device Co., Ltd.
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_shaper.h"
17
18 #include "measurer.h"
19 #include "texgine_font_manager.h"
20 #include "texgine_exception.h"
21 #include "texgine_font.h"
22 #include "texgine_text_blob_builder.h"
23 #include "texgine/typography_types.h"
24 #include "texgine/utils/exlog.h"
25 #ifdef LOGGER_ENABLE_SCOPE
26 #include "texgine/utils/trace.h"
27 #endif
28
29 namespace OHOS {
30 namespace Rosen {
31 namespace TextEngine {
32 #define DOUBLE 2
33
PartFontPropertySet(TexgineFont & font,const CharGroup & cg) const34 void TextShaper::PartFontPropertySet(TexgineFont& font, const CharGroup &cg) const
35 {
36 font.SetEdging(TexgineFont::FontEdging::ANTIALIAS);
37 font.SetSubpixel(true);
38 font.SetHinting(TexgineFont::TexgineFontHinting::SLIGHT);
39 if (cg.typeface->Get()->DetectRawInformation() || cg.typeface->DetectionItalic()) {
40 font.SetSkewX();
41 }
42 if (cg.typeface->DetectionFakeBold()) {
43 font.SetBold();
44 }
45 font.SetTypeface(cg.typeface->Get());
46 }
47
FilterTextSpan(std::shared_ptr<TextSpan> ts)48 int TextShaper::FilterTextSpan(std::shared_ptr<TextSpan> ts)
49 {
50 if (!ts->cgs_.IsValid()) {
51 LOGEX_FUNC_LINE(ERROR) << "cgs is inValid";
52 return 1;
53 }
54
55 if (ts->cgs_.GetSize() <= 0) {
56 LOGEX_FUNC_LINE(ERROR) << "cgs have no cg";
57 return 1;
58 }
59
60 if (ts->cgs_.Get(0).typeface == nullptr) {
61 LOGEX_FUNC_LINE(ERROR) << "first cgs have no typeface";
62 return 1;
63 }
64
65 return 0; // Shape successed
66 }
67
Shape(const VariantSpan & span,const TypographyStyle & ys,const std::shared_ptr<FontProviders> & fontProviders)68 int TextShaper::Shape(const VariantSpan &span, const TypographyStyle &ys,
69 const std::shared_ptr<FontProviders> &fontProviders)
70 {
71 #ifdef LOGGER_ENABLE_SCOPE
72 ScopedTrace scope("TextShaper::ShapeLineSpans");
73 #endif
74 LOGSCOPED(sl, LOGEX_FUNC_LINE_DEBUG(), "TextShaper::ShapeLineSpans");
75 if (span == nullptr) {
76 LOGEX_FUNC_LINE(ERROR) << "span is nullptr";
77 return 1; // Shape failed
78 }
79
80 if (span.TryToAnySpan() != nullptr) {
81 return 0; // Shape successed
82 }
83
84 auto xs = span.GetTextStyle();
85 std::shared_ptr<TextSpan> ts = span.TryToTextSpan();
86 if (ts->cgs_.JudgeOnlyHardBreak()) {
87 xs = ys.ConvertToTextStyle();
88 }
89
90 auto ret = DoShape(ts, xs, ys, fontProviders);
91 if (ret) {
92 LOGEX_FUNC_LINE(ERROR) << "DoShape failed";
93 return 1;
94 }
95
96 if (FilterTextSpan(ts)) {
97 return 1; // Shape failed
98 }
99
100 TexgineFont font;
101 font.SetSize(xs.fontSize);
102 font.GetMetrics(ts->tmetrics_);
103
104 auto blob = GenerateTextBlob(font, ts->cgs_, ts->width_, ts->glyphWidths_);
105 if (blob == nullptr) {
106 LOGEX_FUNC_LINE(ERROR) << "Generate text blob is failed";
107 return 1;
108 }
109
110 ts->textBlob_ = blob;
111 return 0;
112 }
113
DoShape(std::shared_ptr<TextSpan> & span,const TextStyle & xs,const TypographyStyle & ys,const std::shared_ptr<FontProviders> & fontProviders)114 int TextShaper::DoShape(std::shared_ptr<TextSpan> &span, const TextStyle &xs,
115 const TypographyStyle &ys, const std::shared_ptr<FontProviders> &fontProviders)
116 {
117 if (fontProviders == nullptr || span == nullptr) {
118 LOGEX_FUNC_LINE(ERROR) << "providers or span is nullptr";
119 return 1;
120 }
121
122 auto families = xs.fontFamilies;
123 if (families.empty()) {
124 families = ys.fontFamilies;
125 }
126
127 auto fontCollection = fontProviders->GenerateFontCollection(families);
128 if (fontCollection == nullptr) {
129 LOGEX_FUNC_LINE(ERROR) << "fontCollection is nullptr";
130 return 1;
131 }
132
133 std::vector<uint16_t> u16vect = span->cgs_.ToUTF16();
134 span->u16vect_ = u16vect;
135 auto measurer = Measurer::Create(span->u16vect_, *fontCollection);
136 if (measurer == nullptr) {
137 return 1;
138 }
139 measurer->SetLocale(xs.locale);
140 measurer->SetRTL(span->rtl_);
141 FontStyles style(xs.fontWeight, xs.fontStyle);
142 measurer->SetFontStyle(style);
143 measurer->SetSize(xs.fontSize);
144 measurer->SetFontFeatures(xs.fontFeature);
145 measurer->SetRange(0, span->u16vect_.size());
146 measurer->SetSpacing(xs.letterSpacing, xs.wordSpacing);
147 if (measurer->Measure(span->cgs_)) {
148 LOGEX_FUNC_LINE(ERROR) << "Measurer::Measure failed";
149 return 1;
150 }
151
152 return 0;
153 }
154
GenerateTextBlob(TexgineFont & font,const CharGroups & cgs,double & spanWidth,std::vector<double> & glyphWidths) const155 std::shared_ptr<TexgineTextBlob> TextShaper::GenerateTextBlob(TexgineFont &font, const CharGroups &cgs,
156 double &spanWidth, std::vector<double> &glyphWidths) const
157 {
158 TexgineTextBlobBuilder builder;
159 auto offset = 0.0;
160 for (const auto &cg : cgs) {
161 PartFontPropertySet(font, cg);
162
163 glyphWidths.push_back(cg.GetWidth());
164 auto drawingOffset = offset;
165 offset += cg.GetWidth();
166
167 const auto& runBuffer = builder.AllocRunPos(font, cg.glyphs.size());
168 auto index = 0;
169 for (const auto &[cp, ax, ay, ox, oy] : cg.glyphs) {
170 runBuffer.glyphs[index] = cp;
171 runBuffer.pos[index * DOUBLE] = drawingOffset + ox;
172 runBuffer.pos[index * DOUBLE + 1] = ay - oy;
173 index++;
174 drawingOffset += ax;
175 }
176 }
177
178 spanWidth = offset;
179 return builder.Make();
180 }
181 } // namespace TextEngine
182 } // namespace Rosen
183 } // namespace OHOS
184