1 /*
2 * Copyright (c) 2025 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 #include "../../dtk_test_ext.h"
16 #include "text/font_types.h"
17 #include "text/text_blob_builder.h"
18 #include "text/font_mgr.h"
19 #include "text/font.h"
20 #include "recording/mem_allocator.h"
21 #include "text/font_style_set.h"
22 #include "text/rs_xform.h"
23 #include "utils/point.h"
24 #include "utils/rect.h"
25 #include "text/typeface.h"
26 #include "text/font_style.h"
27 #include <sstream>
28
29 namespace OHOS {
30 namespace Rosen {
31
DrawTextBlob(std::vector<std::string> & texts,std::shared_ptr<Drawing::TextBlob> textBlob,Drawing::Font & font1,TestPlaybackCanvas * playbackCanvas)32 static void DrawTextBlob(std::vector<std::string>& texts, std::shared_ptr<Drawing::TextBlob> textBlob,
33 Drawing::Font& font1, TestPlaybackCanvas* playbackCanvas)
34 {
35 int line = 200;
36 int interval1 = 100;
37 int interval2 = 200;
38 int interval3 = 300;
39 int interval4 = 400;
40
41 for (auto text : texts) {
42 std::shared_ptr<Drawing::TextBlob> textinfo = Drawing::TextBlob::MakeFromText(text.c_str(), text.size(), font1);
43 Drawing::Brush brush;
44 playbackCanvas->AttachBrush(brush);
45 playbackCanvas->DrawTextBlob(textBlob.get(), interval2, line);
46 playbackCanvas->DrawTextBlob(textinfo.get(), interval2, line + interval1);
47 playbackCanvas->DetachBrush();
48 Drawing::Pen pen;
49 playbackCanvas->AttachPen(pen);
50 playbackCanvas->DrawTextBlob(textBlob.get(), interval2, line + interval2);
51 playbackCanvas->DrawTextBlob(textBlob.get(), interval2, line + interval3);
52 playbackCanvas->DetachPen();
53 line += interval4;
54 }
55 }
56
57 // 用例 Font_Scene_Transform_0215
58 DEF_DTK(Font_Scene_Transform_12, TestLevel::L2, 215)
59 {
60 // 1.创建typeface
61 std::shared_ptr<Drawing::FontMgr> fontMgr(Drawing::FontMgr::CreateDefaultFontMgr());
62 std::string familyName = "HMOS Color Emoji";
63 std::shared_ptr<Drawing::FontStyleSet> fontStyleSet(fontMgr->MatchFamily(familyName.c_str()));
64 auto typeface = std::shared_ptr<Drawing::Typeface>(fontStyleSet->MatchStyle({
65 Drawing::FontStyle::INVISIBLE_WEIGHT,
66 Drawing::FontStyle::ULTRA_CONDENSED_WIDTH,
67 Drawing::FontStyle::Slant::OBLIQUE_SLANT}));
68 // 2.组合typeface操作接口
69 std::string str = "CPAL";
70 reverse(str.begin(), str.end());
71 uint32_t tagid = *(reinterpret_cast<uint32_t*>(const_cast<char*>(str.c_str())));
72 std::string typefacestr = "GetTableSize = " + std::to_string(typeface->GetTableSize(tagid));
73 // 3.组合Font类接口,如果是操作类有返回值的接口,获取接口返回值加入vector容器
74 auto font = Drawing::Font(typeface, 50.f, 1.0f, 1.0f);
75 font.SetSubpixel(false);
76 font.SetBaselineSnap(true);
77 auto font1 = Drawing::Font(typeface, 50.f, 1.0f, 1.0f);
78 font1.SetSubpixel(font.IsSubpixel());
79 font1.SetBaselineSnap(font.IsBaselineSnap());
80
81 // 4.创建TextBlob
82 std::string textInfo = "";
83 std::shared_ptr<Drawing::TextBlob> textBlob = Drawing::TextBlob::MakeFromText(
84 textInfo.c_str(), textInfo.size(), font1, Drawing::TextEncoding::UTF8);
85
86 // 5.组合textBlob类接口,如果有返回值则获取上一步创建的textBlob返回值打印
87 Drawing::Paint paint;
88 paint.SetColor(0xFFFF0000);
89 float boundsx[] = {1, 2, 3};
90 int intercepts = textBlob->GetIntercepts(boundsx, nullptr, &paint);
91 std::string text2 = "intercepts = " + std::to_string(intercepts);
92
93 //6. 得到需要绘制的所有返回值text,全部适应固定的textBlob构造方式打印
94 std::vector<std::string> texts = {typefacestr, text2};
95
96 //7.调用ClipIRect截取(0,0,1000,1000)区域(缺省,默认INTERSECT、不抗锯齿)
97 auto rect1 = Drawing::RectI(0, 0, 1000, 1000);
98 playbackCanvas_->ClipIRect(rect1, Drawing::ClipOp::INTERSECT);
99
100 //8.调用Scale,Rotate,ConcatMatrix
101 playbackCanvas_->Rotate(45, 50, 50);
102 playbackCanvas_->Translate(400, 600);
103
104 //9.最终绘制
105 DrawTextBlob(texts, textBlob, font1, playbackCanvas_);
106 }
107
108 // 用例 Font_Scene_Transform_0216
109 DEF_DTK(Font_Scene_Transform_12, TestLevel::L2, 216)
110 {
111 // 1.创建typeface
112 std::shared_ptr<Drawing::FontMgr> fontMgr(Drawing::FontMgr::CreateDefaultFontMgr());
113 std::string familyName = "HMOS Color Emoji";
114 std::shared_ptr<Drawing::FontStyleSet> fontStyleSet(fontMgr->MatchFamily(familyName.c_str()));
115 auto typeface = std::shared_ptr<Drawing::Typeface>(fontStyleSet->MatchStyle({
116 Drawing::FontStyle::INVISIBLE_WEIGHT,
117 Drawing::FontStyle::ULTRA_CONDENSED_WIDTH,
118 Drawing::FontStyle::Slant::OBLIQUE_SLANT}));
119 // 2.组合typeface操作接口
120 auto data = typeface->Serialize();
121 uint32_t size = 10;
122 typeface->SetSize(size);
123 std::shared_ptr<Drawing::Typeface> typeface1 = Drawing::Typeface::Deserialize(data->GetData(), typeface->GetSize());
124 // 3.组合Font类接口,如果是操作类有返回值的接口,获取接口返回值加入vector容器
125 auto font = Drawing::Font(typeface1, 50.f, 1.0f, 1.0f);
126 font.SetSubpixel(false);
127 font.SetEmbolden(false);
128 auto font1 = Drawing::Font(typeface1, 50.f, 1.0f, 1.0f);
129 font1.SetSubpixel(font.IsSubpixel());
130 font1.SetEmbolden(font.IsEmbolden());
131
132 // 4.创建TextBlob
133 std::string textInfo = "";
134 std::shared_ptr<Drawing::TextBlob> textBlob = Drawing::TextBlob::MakeFromText(
135 textInfo.c_str(), textInfo.size(), font1, Drawing::TextEncoding::UTF8);
136
137 // 5.组合textBlob类接口,如果有返回值则获取上一步创建的textBlob返回值打印
138 auto rect = textBlob->Bounds();
139 playbackCanvas_->DrawRect(*rect);
140
141 //6. 得到需要绘制的所有返回值text,全部适应固定的textBlob构造方式打印
142 std::vector<std::string> texts = {};
143
144 //7.调用ClipIRect截取(0,0,1000,1000)区域(缺省,默认INTERSECT、不抗锯齿)
145
146 //8.调用Scale,Rotate,ConcatMatrix
147 Drawing::Matrix matrix;
148 playbackCanvas_->ConcatMatrix(matrix);
149
150 //9.最终绘制
151 DrawTextBlob(texts, textBlob, font1, playbackCanvas_);
152 }
153
154 // 用例 Font_Scene_Transform_0217
155 DEF_DTK(Font_Scene_Transform_12, TestLevel::L2, 217)
156 {
157 // 1.创建typeface
158 std::shared_ptr<Drawing::FontMgr> fontMgr(Drawing::FontMgr::CreateDefaultFontMgr());
159 std::string familyName = "HMOS Color Emoji";
160 std::shared_ptr<Drawing::FontStyleSet> fontStyleSet(fontMgr->MatchFamily(familyName.c_str()));
161 auto typeface = std::shared_ptr<Drawing::Typeface>(fontStyleSet->MatchStyle({
162 Drawing::FontStyle::INVISIBLE_WEIGHT,
163 Drawing::FontStyle::ULTRA_CONDENSED_WIDTH,
164 Drawing::FontStyle::Slant::OBLIQUE_SLANT}));
165 // 2.组合typeface操作接口
166 typeface->SetHash(100);
167 std::string typefacestr = "GetHash = " + std::to_string(typeface->GetHash());;
168 // 3.组合Font类接口,如果是操作类有返回值的接口,获取接口返回值加入vector容器
169 auto font1 = Drawing::Font(typeface, 50.f, 1.0f, 1.0f);
170 font1.SetSubpixel(false);
171 std::string text0 = "DDGR ddgr 鸿蒙 !@#¥%^&*; : , 。";
172 auto scalar = font1.UnicharToGlyph(0x44);
173 std::string text4 = "Glyphs = " + std::to_string(scalar);
174
175 // 4.创建TextBlob
176 std::string textInfo = "";
177 std::shared_ptr<Drawing::TextBlob> textBlob = Drawing::TextBlob::MakeFromText(
178 textInfo.c_str(), textInfo.size(), font1, Drawing::TextEncoding::UTF8);
179
180 // 5.组合textBlob类接口,如果有返回值则获取上一步创建的textBlob返回值打印
181 std::vector<Drawing::Point> points;
182 Drawing::TextBlob::GetDrawingPointsForTextBlob(textBlob.get(), points);
183 std::string text2 = "";
184 for (int i = 0; i < points.size(); i++) {
185 text2 += std::to_string(i) + "-- X:" + std::to_string(points[i].GetX())
186 + "Y:" + std::to_string(points[i].GetY());
187 }
188
189 //6. 得到需要绘制的所有返回值text,全部适应固定的textBlob构造方式打印
190 std::vector<std::string> texts = {typefacestr, text2, text4};
191
192 //7.调用ClipIRect截取(0,0,1000,1000)区域(缺省,默认INTERSECT、不抗锯齿)
193
194 //8.调用Scale,Rotate,ConcatMatrix
195 Drawing::Matrix matrix;
196 playbackCanvas_->ConcatMatrix(matrix);
197 playbackCanvas_->Scale(0.5, 0.5);
198
199 //9.最终绘制
200 DrawTextBlob(texts, textBlob, font1, playbackCanvas_);
201 }
202
203 // 用例 Font_Scene_Transform_0218
204 DEF_DTK(Font_Scene_Transform_12, TestLevel::L2, 218)
205 {
206 // 1.创建typeface
207 std::shared_ptr<Drawing::FontMgr> fontMgr(Drawing::FontMgr::CreateDefaultFontMgr());
208 std::string familyName = "HMOS Color Emoji";
209 std::shared_ptr<Drawing::FontStyleSet> fontStyleSet(fontMgr->MatchFamily(familyName.c_str()));
210 auto typeface = std::shared_ptr<Drawing::Typeface>(fontStyleSet->MatchStyle({
211 Drawing::FontStyle::INVISIBLE_WEIGHT,
212 Drawing::FontStyle::ULTRA_CONDENSED_WIDTH,
213 Drawing::FontStyle::Slant::OBLIQUE_SLANT}));
214 // 2.组合typeface操作接口
215 std::string typefacestr = "IsCustomTypeface = " + std::to_string(typeface->IsCustomTypeface());
216 // 3.组合Font类接口,如果是操作类有返回值的接口,获取接口返回值加入vector容器
217 auto font = Drawing::Font(typeface, 50.f, 1.0f, 1.0f);
218 font.SetSubpixel(false);
219 font.SetForceAutoHinting(false);
220 auto font1 = Drawing::Font(typeface, 50.f, 1.0f, 1.0f);
221 font1.SetSubpixel(font.IsSubpixel());
222 font1.SetForceAutoHinting(font.IsForceAutoHinting());
223
224 // 4.创建TextBlob
225 std::string textInfo = "harmony_os";
226 int cont = textInfo.size();
227 Drawing::Point p[cont];
228 for (int i = 0; i < cont; i++) {
229 p[i].SetX(-100 + 50 * i);
230 p[i].SetY(1000 - 50 * i);
231 }
232 std::shared_ptr<Drawing::TextBlob> textBlob = Drawing::TextBlob::MakeFromPosText(
233 textInfo.c_str(), 10, p, font1, Drawing::TextEncoding::UTF8);
234
235 // 5.组合textBlob类接口,如果有返回值则获取上一步创建的textBlob返回值打印
236 std::string text = "harmony_os";
237 std::vector<uint16_t> glyphid;
238 Drawing::TextBlob::GetDrawingGlyphIDforTextBlob(textBlob.get(), glyphid);
239 std::string text2 = "";
240 for (int row = 0; row < text.size() && row < glyphid.size(); row++) {
241 text2 += std::string(1, text[row]) + ":" + std::to_string(glyphid[row]);
242 }
243
244 //6. 得到需要绘制的所有返回值text,全部适应固定的textBlob构造方式打印
245 std::vector<std::string> texts = {typefacestr, text2};
246
247 //7.调用ClipIRect截取(0,0,1000,1000)区域(缺省,默认INTERSECT、不抗锯齿)
248 auto rrect = Drawing::RoundRect(Drawing::Rect(0, 0, 1000, 1000), 52, 52);
249 playbackCanvas_->ClipRoundRect(rrect);
250
251 //8.调用Scale,Rotate,ConcatMatrix
252 playbackCanvas_->Rotate(45, 50, 50);
253 playbackCanvas_->Scale(0.5, 0.5);
254 playbackCanvas_->Translate(400, 600);
255
256 //9.最终绘制
257 DrawTextBlob(texts, textBlob, font1, playbackCanvas_);
258 }
259
260 // 用例 Font_Scene_Transform_0219
261 DEF_DTK(Font_Scene_Transform_12, TestLevel::L2, 219)
262 {
263 // 1.创建typeface
264 std::shared_ptr<Drawing::FontMgr> fontMgr(Drawing::FontMgr::CreateDefaultFontMgr());
265 std::string familyName = "HMOS Color Emoji";
266 std::shared_ptr<Drawing::FontStyleSet> fontStyleSet(fontMgr->MatchFamily(familyName.c_str()));
267 auto typeface = std::shared_ptr<Drawing::Typeface>(fontStyleSet->MatchStyle({
268 Drawing::FontStyle::INVISIBLE_WEIGHT,
269 Drawing::FontStyle::ULTRA_CONDENSED_WIDTH,
270 Drawing::FontStyle::Slant::OBLIQUE_SLANT}));
271 // 2.组合typeface操作接口
272 auto familyNamex = typeface->GetFamilyName();
273 std::string typefacestr = "GetFamilyName = " + familyNamex;
274 // 3.组合Font类接口,如果是操作类有返回值的接口,获取接口返回值加入vector容器
275 auto font = Drawing::Font(typeface, 50.f, 1.0f, 1.0f);
276 font.SetSubpixel(false);
277 font.SetScaleX(1);
278 auto font1 = Drawing::Font(typeface, 50.f, font.GetScaleX(), 1.0f);
279 font1.SetSubpixel(font.IsSubpixel());
280
281 // 4.创建TextBlob
282 std::string textInfo = "harmony_os";
283 int cont = textInfo.size();
284 Drawing::Point p[cont];
285 for (int i = 0; i < cont; i++) {
286 p[i].SetX(-100 + 50 * i);
287 p[i].SetY(1000 - 50 * i);
288 }
289 std::shared_ptr<Drawing::TextBlob> textBlob = Drawing::TextBlob::MakeFromPosText(
290 textInfo.c_str(), 10, p, font1, Drawing::TextEncoding::UTF8);
291
292 // 5.组合textBlob类接口,如果有返回值则获取上一步创建的textBlob返回值打印
293 std::string textinfo1 = "Deserialize @Hello World";
294 Drawing::TextBlob::Context* Ctx = new (std::nothrow) Drawing::TextBlob::Context(typeface, false);
295 auto data2 = textBlob->Serialize(Ctx);
296 std::shared_ptr<Drawing::TextBlob> infoTextBlob2 =
297 Drawing::TextBlob::Deserialize(data2->GetData(), data2->GetSize(), Ctx);
298
299 //6. 得到需要绘制的所有返回值text,全部适应固定的textBlob构造方式打印
300 std::vector<std::string> texts = {typefacestr};
301
302 //7.调用ClipIRect截取(0,0,1000,1000)区域(缺省,默认INTERSECT、不抗锯齿)
303 auto rect1 = Drawing::RectI(0, 0, 1000, 1000);
304 playbackCanvas_->ClipIRect(rect1, Drawing::ClipOp::INTERSECT);
305
306 //8.调用Scale,Rotate,ConcatMatrix
307 Drawing::Matrix matrix;
308 playbackCanvas_->ConcatMatrix(matrix);
309 playbackCanvas_->Rotate(45, 50, 50);
310 playbackCanvas_->Scale(0.5, 0.5);
311 playbackCanvas_->Translate(400, 600);
312
313 //9.最终绘制
314 DrawTextBlob(texts, textBlob, font1, playbackCanvas_);
315 }
316
317 // 用例 Font_Scene_Transform_0220
318 DEF_DTK(Font_Scene_Transform_12, TestLevel::L2, 220)
319 {
320 // 1.创建typeface
321 std::shared_ptr<Drawing::FontMgr> fontMgr(Drawing::FontMgr::CreateDefaultFontMgr());
322 std::string familyName = "HMOS Color Emoji";
323 std::shared_ptr<Drawing::FontStyleSet> fontStyleSet(fontMgr->MatchFamily(familyName.c_str()));
324 auto typeface = std::shared_ptr<Drawing::Typeface>(fontStyleSet->MatchStyle({
325 Drawing::FontStyle::INVISIBLE_WEIGHT,
326 Drawing::FontStyle::ULTRA_CONDENSED_WIDTH,
327 Drawing::FontStyle::Slant::OBLIQUE_SLANT}));
328 // 2.组合typeface操作接口
329 auto data = typeface->Serialize();
330 uint32_t size = 10;
331 typeface->SetSize(size);
332 std::shared_ptr<Drawing::Typeface> typeface1 = Drawing::Typeface::Deserialize(data->GetData(), typeface->GetSize());
333 // 3.组合Font类接口,如果是操作类有返回值的接口,获取接口返回值加入vector容器
334 auto font1 = Drawing::Font(typeface1, 50.f, 1.0f, 1.0f);
335 font1.SetSubpixel(false);
336 std::string text0 = "DDGR ddgr 鸿蒙 !@#¥%^&*; : , 。";
337 int glyphCount = font1.CountText(text0.c_str(), text0.length(), Drawing::TextEncoding::UTF16);
338 uint16_t glyphs[glyphCount - 1];
339 int count = font1.TextToGlyphs(text0.c_str(), text0.length(), Drawing::TextEncoding::UTF16, glyphs, glyphCount + 1);
340 std::string text4 = "TextToGlyphs = " + std::to_string(count);
341
342 // 4.创建TextBlob
343 std::string textInfo = "harmony_os";
344 int cont = textInfo.size();
345 Drawing::Point p[cont];
346 for (int i = 0; i < cont; i++) {
347 p[i].SetX(-100 + 50 * i);
348 p[i].SetY(1000 - 50 * i);
349 }
350 std::shared_ptr<Drawing::TextBlob> textBlob = Drawing::TextBlob::MakeFromPosText(
351 textInfo.c_str(), 10, p, font1, Drawing::TextEncoding::UTF8);
352
353 // 5.组合textBlob类接口,如果有返回值则获取上一步创建的textBlob返回值打印
354 auto rect = textBlob->Bounds();
355 playbackCanvas_->DrawRect(*rect);
356
357 //6. 得到需要绘制的所有返回值text,全部适应固定的textBlob构造方式打印
358 std::vector<std::string> texts = {text4};
359
360 //7.调用ClipIRect截取(0,0,1000,1000)区域(缺省,默认INTERSECT、不抗锯齿)
361 auto rect1 = Drawing::RectI(0, 0, 1000, 1000);
362 playbackCanvas_->ClipIRect(rect1, Drawing::ClipOp::INTERSECT);
363
364 //8.调用Scale,Rotate,ConcatMatrix
365 Drawing::Matrix matrix;
366 playbackCanvas_->ConcatMatrix(matrix);
367 playbackCanvas_->Rotate(45, 50, 50);
368 playbackCanvas_->Translate(400, 600);
369
370 //9.最终绘制
371 DrawTextBlob(texts, textBlob, font1, playbackCanvas_);
372 }
373
374 // 用例 Font_Scene_Transform_0221
375 DEF_DTK(Font_Scene_Transform_12, TestLevel::L2, 221)
376 {
377 // 1.创建typeface
378 std::shared_ptr<Drawing::FontMgr> fontMgr(Drawing::FontMgr::CreateDefaultFontMgr());
379 std::string familyName = "HMOS Color Emoji";
380 std::shared_ptr<Drawing::FontStyleSet> fontStyleSet(fontMgr->MatchFamily(familyName.c_str()));
381 auto typeface = std::shared_ptr<Drawing::Typeface>(fontStyleSet->MatchStyle({
382 Drawing::FontStyle::INVISIBLE_WEIGHT,
383 Drawing::FontStyle::ULTRA_CONDENSED_WIDTH,
384 Drawing::FontStyle::Slant::OBLIQUE_SLANT}));
385 // 2.组合typeface操作接口
386 std::string typefacestr = "GetUnitsPerEm = " + std::to_string(typeface->GetUnitsPerEm());
387 // 3.组合Font类接口,如果是操作类有返回值的接口,获取接口返回值加入vector容器
388 auto font = Drawing::Font(typeface, 50.f, 1.0f, 1.0f);
389 font.SetSubpixel(false);
390 font.SetHinting(Drawing::FontHinting::NORMAL);
391 auto font1 = Drawing::Font(typeface, 50.f, 1.0f, 1.0f);
392 font1.SetSubpixel(font.IsSubpixel());
393 font1.SetHinting(font.GetHinting());
394
395 // 4.创建TextBlob
396 std::string textInfo = "harmony_os";
397 std::shared_ptr<Drawing::TextBlob> textBlob = Drawing::TextBlob::MakeFromString(
398 textInfo.c_str(), font1, Drawing::TextEncoding::UTF8);
399
400 // 5.组合textBlob类接口,如果有返回值则获取上一步创建的textBlob返回值打印
401 std::string text = "harmony_os";
402 std::vector<uint16_t> glyphid;
403 Drawing::TextBlob::GetDrawingGlyphIDforTextBlob(textBlob.get(), glyphid);
404 playbackCanvas_->Translate(200, 200);
405 for (int row = 0; row < text.size() && row < glyphid.size(); row++) {
406 auto path = Drawing::TextBlob::GetDrawingPathforTextBlob(glyphid[row], textBlob.get());
407 playbackCanvas_->DrawPath(path);
408 playbackCanvas_->Translate(0, 100);
409 }
410
411 //6. 得到需要绘制的所有返回值text,全部适应固定的textBlob构造方式打印
412 std::vector<std::string> texts = {typefacestr};
413
414 //7.调用ClipIRect截取(0,0,1000,1000)区域(缺省,默认INTERSECT、不抗锯齿)
415 Drawing::Region region;
416 region.SetRect(Drawing::RectI(100, 100, 500, 500));
417 playbackCanvas_->ClipRegion(region);
418
419 //8.调用Scale,Rotate,ConcatMatrix
420 Drawing::Matrix matrix;
421 playbackCanvas_->ConcatMatrix(matrix);
422 playbackCanvas_->Rotate(45, 50, 50);
423 playbackCanvas_->Scale(0.5, 0.5);
424
425 //9.最终绘制
426 DrawTextBlob(texts, textBlob, font1, playbackCanvas_);
427 }
428
429 // 用例 Font_Scene_Transform_0222
430 DEF_DTK(Font_Scene_Transform_12, TestLevel::L2, 222)
431 {
432 // 1.创建typeface
433 std::shared_ptr<Drawing::FontMgr> fontMgr(Drawing::FontMgr::CreateDefaultFontMgr());
434 std::string familyName = "HMOS Color Emoji";
435 std::shared_ptr<Drawing::FontStyleSet> fontStyleSet(fontMgr->MatchFamily(familyName.c_str()));
436 auto typeface = std::shared_ptr<Drawing::Typeface>(fontStyleSet->MatchStyle({
437 Drawing::FontStyle::INVISIBLE_WEIGHT,
438 Drawing::FontStyle::ULTRA_CONDENSED_WIDTH,
439 Drawing::FontStyle::Slant::OBLIQUE_SLANT}));
440 // 2.组合typeface操作接口
441 std::string typefacestr = "GetItalic = " + std::to_string(typeface->GetItalic());
442 // 3.组合Font类接口,如果是操作类有返回值的接口,获取接口返回值加入vector容器
443 auto font = Drawing::Font(typeface, 50.f, 1.0f, 1.0f);
444 font.SetSubpixel(false);
445 font.SetSkewX(1.0f);
446 auto font1 = Drawing::Font(typeface, 50.f, font.GetSkewX(), 1.0f);
447 font1.SetSubpixel(font.IsSubpixel());
448
449 // 4.创建TextBlob
450 std::string textInfo = "harmony_os";
451 std::shared_ptr<Drawing::TextBlob> textBlob = Drawing::TextBlob::MakeFromString(
452 textInfo.c_str(), font1, Drawing::TextEncoding::UTF8);
453
454 // 5.组合textBlob类接口,如果有返回值则获取上一步创建的textBlob返回值打印
455 std::string name1 = "HMOS Color Emoji";
456 std::string textInfo1 = "";
457 std::shared_ptr<Drawing::TextBlob> textBlob1 = Drawing::TextBlob::MakeFromText(textInfo1.c_str(),
458 textInfo1.size(), font1, Drawing::TextEncoding::UTF8);
459 if (textBlob1->IsEmoji()) {
460 playbackCanvas_->DrawBackground(0xFF0000FF);
461 } else {
462 playbackCanvas_->DrawBackground(0xFFFF0000);
463 }
464
465 //6. 得到需要绘制的所有返回值text,全部适应固定的textBlob构造方式打印
466 std::vector<std::string> texts = {typefacestr};
467
468 //7.调用ClipIRect截取(0,0,1000,1000)区域(缺省,默认INTERSECT、不抗锯齿)
469 auto rect1 = Drawing::Rect(0, 0, 600, 600);
470 playbackCanvas_->ClipRect(rect1);
471
472 //8.调用Scale,Rotate,ConcatMatrix
473 playbackCanvas_->Scale(0.5, 0.5);
474
475 //9.最终绘制
476 DrawTextBlob(texts, textBlob, font1, playbackCanvas_);
477 }
478
479 // 用例 Font_Scene_Transform_0223
480 DEF_DTK(Font_Scene_Transform_12, TestLevel::L2, 223)
481 {
482 // 1.创建typeface
483 std::shared_ptr<Drawing::FontMgr> fontMgr(Drawing::FontMgr::CreateDefaultFontMgr());
484 std::string familyName = "HMOS Color Emoji";
485 std::shared_ptr<Drawing::FontStyleSet> fontStyleSet(fontMgr->MatchFamily(familyName.c_str()));
486 auto typeface = std::shared_ptr<Drawing::Typeface>(fontStyleSet->MatchStyle({
487 Drawing::FontStyle::INVISIBLE_WEIGHT,
488 Drawing::FontStyle::ULTRA_CONDENSED_WIDTH,
489 Drawing::FontStyle::Slant::OBLIQUE_SLANT}));
490 // 2.组合typeface操作接口
491 auto familyNamex = typeface->GetFamilyName();
492 std::string typefacestr = "GetFamilyName = " + familyNamex;
493 // 3.组合Font类接口,如果是操作类有返回值的接口,获取接口返回值加入vector容器
494 auto font1 = Drawing::Font(typeface, 50.f, 1.0f, 1.0f);
495 font1.SetSubpixel(false);
496 std::string text0 = "DDGR ddgr 鸿蒙 !@#¥%^&*; : , 。";
497 Drawing::Rect bounds;
498 auto scalar = font1.MeasureText(text0.c_str(), text0.length(), Drawing::TextEncoding::UTF32, &bounds);
499 std::string text4 = "MeasureTextWidths = " + std::to_string(scalar);
500
501 // 4.创建TextBlob
502 std::string textInfo = "harmony_os";
503 std::shared_ptr<Drawing::TextBlob> textBlob = Drawing::TextBlob::MakeFromString(
504 textInfo.c_str(), font1, Drawing::TextEncoding::UTF8);
505
506 // 5.组合textBlob类接口,如果有返回值则获取上一步创建的textBlob返回值打印
507 std::string textinfo1 = "Deserialize @Hello World";
508 Drawing::TextBlob::Context* Ctx = new (std::nothrow) Drawing::TextBlob::Context(typeface, false);
509 auto data2 = textBlob->Serialize(Ctx);
510 std::shared_ptr<Drawing::TextBlob> infoTextBlob2 =
511 Drawing::TextBlob::Deserialize(data2->GetData(), data2->GetSize(), Ctx);
512
513 //6. 得到需要绘制的所有返回值text,全部适应固定的textBlob构造方式打印
514 std::vector<std::string> texts = {typefacestr, text4};
515
516 //7.调用ClipIRect截取(0,0,1000,1000)区域(缺省,默认INTERSECT、不抗锯齿)
517 auto rect1 = Drawing::Rect(0, 0, 600, 600);
518 playbackCanvas_->ClipRect(rect1);
519
520 //8.调用Scale,Rotate,ConcatMatrix
521 Drawing::Matrix matrix;
522 playbackCanvas_->ConcatMatrix(matrix);
523 playbackCanvas_->Rotate(45, 50, 50);
524 playbackCanvas_->Scale(0.5, 0.5);
525 playbackCanvas_->Translate(400, 600);
526
527 //9.最终绘制
528 DrawTextBlob(texts, textBlob, font1, playbackCanvas_);
529 }
530
531 // 用例 Font_Scene_Transform_0224
532 DEF_DTK(Font_Scene_Transform_12, TestLevel::L2, 224)
533 {
534 // 1.创建typeface
535 std::shared_ptr<Drawing::FontMgr> fontMgr(Drawing::FontMgr::CreateDefaultFontMgr());
536 std::string familyName = "HMOS Color Emoji";
537 std::shared_ptr<Drawing::FontStyleSet> fontStyleSet(fontMgr->MatchFamily(familyName.c_str()));
538 auto typeface = std::shared_ptr<Drawing::Typeface>(fontStyleSet->MatchStyle({
539 Drawing::FontStyle::INVISIBLE_WEIGHT,
540 Drawing::FontStyle::ULTRA_CONDENSED_WIDTH,
541 Drawing::FontStyle::Slant::OBLIQUE_SLANT}));
542 // 2.组合typeface操作接口
543 std::string typefacestr = "GetUniqueID = " + std::to_string(typeface->GetUniqueID());
544 // 3.组合Font类接口,如果是操作类有返回值的接口,获取接口返回值加入vector容器
545 auto font = Drawing::Font(typeface, 50.f, 1.0f, 1.0f);
546 font.SetSubpixel(false);
547 font.SetEmbeddedBitmaps(true);
548 auto font1 = Drawing::Font(typeface, 50.f, 1.0f, 1.0f);
549 font1.SetSubpixel(font.IsSubpixel());
550 font1.SetEmbeddedBitmaps(font.IsEmbeddedBitmaps());
551
552 // 4.创建TextBlob
553 std::string textInfo = "1111111111111111111111111111111111";
554 int maxGlyphCount = font1.CountText(textInfo.c_str(), textInfo.size(), Drawing::TextEncoding::UTF8);
555 Drawing::RSXform xform[maxGlyphCount];
556 for (int i = 0; i < maxGlyphCount; ++i) {
557 xform[i].cos_ = cos(10 * i) + 0.1 * i;
558 xform[i].sin_ = sin(10 * i);
559 xform[i].tx_ = 40 * i + 100;;
560 xform[i].ty_ = 100;
561 }
562 std::shared_ptr<Drawing::TextBlob> textBlob = Drawing::TextBlob::MakeFromRSXform(
563 textInfo.c_str(), textInfo.size(), &xform[0], font1, Drawing::TextEncoding::UTF8);
564
565 // 5.组合textBlob类接口,如果有返回值则获取上一步创建的textBlob返回值打印
566 std::vector<Drawing::Point> points;
567 Drawing::TextBlob::GetDrawingPointsForTextBlob(textBlob.get(), points);
568 std::string text2 = "";
569 for (int i = 0; i < points.size(); i++) {
570 text2 += std::to_string(i) + "-- X:" + std::to_string(points[i].GetX())
571 + "Y:" + std::to_string(points[i].GetY());
572 }
573
574 //6. 得到需要绘制的所有返回值text,全部适应固定的textBlob构造方式打印
575 std::vector<std::string> texts = {typefacestr, text2};
576
577 //7.调用ClipIRect截取(0,0,1000,1000)区域(缺省,默认INTERSECT、不抗锯齿)
578 Drawing::Path path;
579 path.AddOval({100, 100, 356, 356});
580 playbackCanvas_->ClipPath(path);
581
582 //8.调用Scale,Rotate,ConcatMatrix
583 playbackCanvas_->Rotate(45, 50, 50);
584
585 //9.最终绘制
586 DrawTextBlob(texts, textBlob, font1, playbackCanvas_);
587 }
588
589 // 用例 Font_Scene_Transform_0225
590 DEF_DTK(Font_Scene_Transform_12, TestLevel::L2, 225)
591 {
592 // 1.创建typeface
593 std::shared_ptr<Drawing::FontMgr> fontMgr(Drawing::FontMgr::CreateDefaultFontMgr());
594 std::string familyName = "HMOS Color Emoji";
595 std::shared_ptr<Drawing::FontStyleSet> fontStyleSet(fontMgr->MatchFamily(familyName.c_str()));
596 auto typeface = std::shared_ptr<Drawing::Typeface>(fontStyleSet->MatchStyle({
597 Drawing::FontStyle::INVISIBLE_WEIGHT,
598 Drawing::FontStyle::ULTRA_CONDENSED_WIDTH,
599 Drawing::FontStyle::Slant::OBLIQUE_SLANT}));
600 // 2.组合typeface操作接口
601 std::string typefacestr = "GetUniqueID = " + std::to_string(typeface->GetUniqueID());
602 // 3.组合Font类接口,如果是操作类有返回值的接口,获取接口返回值加入vector容器
603 auto font = Drawing::Font(typeface, 50.f, 1.0f, 1.0f);
604 font.SetSubpixel(false);
605 font.SetLinearMetrics(false);
606 auto font1 = Drawing::Font(typeface, 50.f, 1.0f, 1.0f);
607 font1.SetSubpixel(font.IsSubpixel());
608 font1.SetLinearMetrics(font.IsLinearMetrics());
609
610 // 4.创建TextBlob
611 std::string textInfo = "1111111111111111111111111111111111";
612 int maxGlyphCount = font1.CountText(textInfo.c_str(), textInfo.size(), Drawing::TextEncoding::UTF8);
613 Drawing::RSXform xform[maxGlyphCount];
614 for (int i = 0; i < maxGlyphCount; ++i) {
615 xform[i].cos_ = cos(10 * i) + 0.1 * i;
616 xform[i].sin_ = sin(10 * i);
617 xform[i].tx_ = 40 * i + 100;;
618 xform[i].ty_ = 100;
619 }
620 std::shared_ptr<Drawing::TextBlob> textBlob = Drawing::TextBlob::MakeFromRSXform(
621 textInfo.c_str(), textInfo.size(), &xform[0], font1, Drawing::TextEncoding::UTF8);
622
623 // 5.组合textBlob类接口,如果有返回值则获取上一步创建的textBlob返回值打印
624 Drawing::Paint paint;
625 paint.SetColor(0xFFFF0000);
626 float boundsx[] = {1, 2, 3};
627 int intercepts = textBlob->GetIntercepts(boundsx, nullptr, &paint);
628 std::string text2 = "intercepts = " + std::to_string(intercepts);
629
630 //6. 得到需要绘制的所有返回值text,全部适应固定的textBlob构造方式打印
631 std::vector<std::string> texts = {typefacestr, text2};
632
633 //7.调用ClipIRect截取(0,0,1000,1000)区域(缺省,默认INTERSECT、不抗锯齿)
634 Drawing::Path path;
635 path.AddOval({100, 100, 356, 356});
636 playbackCanvas_->ClipPath(path);
637
638 //8.调用Scale,Rotate,ConcatMatrix
639 playbackCanvas_->Scale(0.5, 0.5);
640 playbackCanvas_->Translate(400, 600);
641
642 //9.最终绘制
643 DrawTextBlob(texts, textBlob, font1, playbackCanvas_);
644 }
645
646 // 用例 Font_Scene_Transform_0227
647 DEF_DTK(Font_Scene_Transform_12, TestLevel::L2, 227)
648 {
649 // 1.创建typeface
650 std::shared_ptr<Drawing::FontMgr> fontMgr(Drawing::FontMgr::CreateDefaultFontMgr());
651 std::shared_ptr<Drawing::FontStyleSet> fontStyleSet(fontMgr->CreateStyleSet(0));
652 auto typeface = std::shared_ptr<Drawing::Typeface>(fontStyleSet->CreateTypeface(0));
653 // 2.组合typeface操作接口
654 std::string typefacestr = "GetItalic = " + std::to_string(typeface->GetItalic());
655 // 3.组合Font类接口,如果是操作类有返回值的接口,获取接口返回值加入vector容器
656 auto font = Drawing::Font(typeface, 50.f, 1.0f, 1.0f);
657 font.SetSubpixel(false);
658 font.SetEdging(Drawing::FontEdging::ANTI_ALIAS);
659 auto font1 = Drawing::Font(typeface, 50.f, 1.0f, 1.0f);
660 font1.SetSubpixel(font.IsSubpixel());
661 font1.SetEdging(font.GetEdging());
662
663 // 4.创建TextBlob
664 Drawing::TextBlobBuilder builder;
665 auto buffer = builder.AllocRunPos(font1, 20, nullptr);
666 for (int i = 0; i < 20; i++) {
667 buffer.glyphs[i] = font1.UnicharToGlyph(0x9088);
668 buffer.pos[i * 2] = 50.f * i;
669 buffer.pos[i * 2 + 1] = 0;
670 }
671 std::shared_ptr<Drawing::TextBlob> textBlob = builder.Make();
672
673 // 5.组合textBlob类接口,如果有返回值则获取上一步创建的textBlob返回值打印
674 std::vector<Drawing::Point> points;
675 Drawing::TextBlob::GetDrawingPointsForTextBlob(textBlob.get(), points);
676 std::string text2 = "";
677 for (int i = 0; i < points.size(); i++) {
678 text2 += std::to_string(i) + "-- X:" + std::to_string(points[i].GetX())
679 + "Y:" + std::to_string(points[i].GetY());
680 }
681
682 //6. 得到需要绘制的所有返回值text,全部适应固定的textBlob构造方式打印
683 std::vector<std::string> texts = {typefacestr, text2};
684
685 //7.调用ClipIRect截取(0,0,1000,1000)区域(缺省,默认INTERSECT、不抗锯齿)
686 Drawing::Region region;
687 region.SetRect(Drawing::RectI(100, 100, 500, 500));
688 playbackCanvas_->ClipRegion(region);
689
690 //8.调用Scale,Rotate,ConcatMatrix
691 playbackCanvas_->Rotate(45, 50, 50);
692 playbackCanvas_->Translate(400, 600);
693
694 //9.最终绘制
695 DrawTextBlob(texts, textBlob, font1, playbackCanvas_);
696 }
697
698 // 用例 Font_Scene_Transform_0228
699 DEF_DTK(Font_Scene_Transform_12, TestLevel::L2, 228)
700 {
701 // 1.创建typeface
702 std::shared_ptr<Drawing::FontMgr> fontMgr(Drawing::FontMgr::CreateDefaultFontMgr());
703 std::shared_ptr<Drawing::FontStyleSet> fontStyleSet(fontMgr->CreateStyleSet(0));
704 auto typeface = std::shared_ptr<Drawing::Typeface>(fontStyleSet->CreateTypeface(0));
705 // 2.组合typeface操作接口
706 std::string typefacestr = "GetUniqueID = " + std::to_string(typeface->GetUniqueID());
707 // 3.组合Font类接口,如果是操作类有返回值的接口,获取接口返回值加入vector容器
708 auto font = Drawing::Font(typeface, 50.f, 1.0f, 1.0f);
709 font.SetSubpixel(false);
710 font.SetSize(100.f);
711 auto font1 = Drawing::Font(typeface, font.GetSize(), 1.0f, 1.0f);
712 font1.SetSubpixel(font.IsSubpixel());
713
714 // 4.创建TextBlob
715 Drawing::TextBlobBuilder builder;
716 auto buffer = builder.AllocRunPos(font1, 20, nullptr);
717 for (int i = 0; i < 20; i++) {
718 buffer.glyphs[i] = font1.UnicharToGlyph(0x9088);
719 buffer.pos[i * 2] = 50.f * i;
720 buffer.pos[i * 2 + 1] = 0;
721 }
722 std::shared_ptr<Drawing::TextBlob> textBlob = builder.Make();
723
724 // 5.组合textBlob类接口,如果有返回值则获取上一步创建的textBlob返回值打印
725 auto rect = textBlob->Bounds();
726 playbackCanvas_->DrawRect(*rect);
727
728 //6. 得到需要绘制的所有返回值text,全部适应固定的textBlob构造方式打印
729 std::vector<std::string> texts = {typefacestr};
730
731 //7.调用ClipIRect截取(0,0,1000,1000)区域(缺省,默认INTERSECT、不抗锯齿)
732 Drawing::Path path;
733 path.AddOval({100, 100, 356, 356});
734 playbackCanvas_->ClipPath(path);
735
736 //8.调用Scale,Rotate,ConcatMatrix
737 Drawing::Matrix matrix;
738 playbackCanvas_->ConcatMatrix(matrix);
739 playbackCanvas_->Scale(0.5, 0.5);
740 playbackCanvas_->Translate(400, 600);
741
742 //9.最终绘制
743 DrawTextBlob(texts, textBlob, font1, playbackCanvas_);
744 }
745
746 // 用例 Font_Scene_Transform_0229
747 DEF_DTK(Font_Scene_Transform_12, TestLevel::L2, 229)
748 {
749 // 1.创建typeface
750 std::shared_ptr<Drawing::FontMgr> fontMgr(Drawing::FontMgr::CreateDefaultFontMgr());
751 std::shared_ptr<Drawing::FontStyleSet> fontStyleSet(fontMgr->CreateStyleSet(0));
752 auto typeface = std::shared_ptr<Drawing::Typeface>(fontStyleSet->CreateTypeface(0));
753 // 2.组合typeface操作接口
754 std::string typefacestr = "GetItalic = " + std::to_string(typeface->GetItalic());
755 // 3.组合Font类接口,如果是操作类有返回值的接口,获取接口返回值加入vector容器
756 auto font1 = Drawing::Font(typeface, 50.f, 1.0f, 1.0f);
757 font1.SetSubpixel(false);
758 std::string text0 = "DDGR ddgr 鸿蒙 !@#¥%^&*; : , 。";
759 int glyphCount = font1.CountText(text0.c_str(), text0.length(), Drawing::TextEncoding::UTF8);
760 float widths[glyphCount];
761 uint16_t glyphs[glyphCount];
762 font1.TextToGlyphs(text0.c_str(), text0.length(), Drawing::TextEncoding::UTF8, glyphs, glyphCount);
763 font1.GetWidths(glyphs, glyphCount, widths);
764 std::string text4 = "Getwidths = " + std::to_string(widths[0]);
765
766 // 4.创建TextBlob
767 Drawing::TextBlobBuilder builder;
768 auto buffer = builder.AllocRunPos(font1, 20, nullptr);
769 for (int i = 0; i < 20; i++) {
770 buffer.glyphs[i] = font1.UnicharToGlyph(0x9088);
771 buffer.pos[i * 2] = 50.f * i;
772 buffer.pos[i * 2 + 1] = 0;
773 }
774 std::shared_ptr<Drawing::TextBlob> textBlob = builder.Make();
775
776 // 5.组合textBlob类接口,如果有返回值则获取上一步创建的textBlob返回值打印
777 std::string textinfo1 = "Deserialize @Hello World";
778 Drawing::TextBlob::Context* Ctx = new (std::nothrow) Drawing::TextBlob::Context(typeface, false);
779 auto data2 = textBlob->Serialize(Ctx);
780 std::shared_ptr<Drawing::TextBlob> infoTextBlob2 =
781 Drawing::TextBlob::Deserialize(data2->GetData(), data2->GetSize(), Ctx);
782
783 //6. 得到需要绘制的所有返回值text,全部适应固定的textBlob构造方式打印
784 std::vector<std::string> texts = {typefacestr, text4};
785
786 //7.调用ClipIRect截取(0,0,1000,1000)区域(缺省,默认INTERSECT、不抗锯齿)
787 Drawing::Path path;
788 path.AddOval({100, 100, 356, 356});
789 playbackCanvas_->ClipPath(path);
790
791 //8.调用Scale,Rotate,ConcatMatrix
792 playbackCanvas_->Scale(0.5, 0.5);
793 playbackCanvas_->Translate(400, 600);
794
795 //9.最终绘制
796 DrawTextBlob(texts, textBlob, font1, playbackCanvas_);
797 }
798
799 // 用例 Font_Scene_Transform_0230
800 DEF_DTK(Font_Scene_Transform_12, TestLevel::L2, 230)
801 {
802 // 1.创建typeface
803 std::shared_ptr<Drawing::FontMgr> fontMgr(Drawing::FontMgr::CreateDefaultFontMgr());
804 std::shared_ptr<Drawing::FontStyleSet> fontStyleSet(fontMgr->CreateStyleSet(0));
805 auto typeface = std::shared_ptr<Drawing::Typeface>(fontStyleSet->CreateTypeface(0));
806 // 2.组合typeface操作接口
807 auto data = typeface->Serialize();
808 uint32_t size = 10;
809 typeface->SetSize(size);
810 std::shared_ptr<Drawing::Typeface> typeface1 = Drawing::Typeface::Deserialize(data->GetData(), typeface->GetSize());
811 // 3.组合Font类接口,如果是操作类有返回值的接口,获取接口返回值加入vector容器
812 auto font = Drawing::Font(typeface1, 50.f, 1.0f, 1.0f);
813 font.SetSubpixel(false);
814 font.SetBaselineSnap(true);
815 auto font1 = Drawing::Font(typeface1, 50.f, 1.0f, 1.0f);
816 font1.SetSubpixel(font.IsSubpixel());
817 font1.SetBaselineSnap(font.IsBaselineSnap());
818
819 // 4.创建TextBlob
820 Drawing::TextBlobBuilder builder;
821 auto buffer = builder.AllocRunRSXform(font1, 20);
822 for (int i = 0; i < 20; i++) {
823 buffer.glyphs[i] = font1.UnicharToGlyph(0x30);
824 buffer.pos[i * 4] = cos(i * 18);
825 buffer.pos[i * 4 + 1] = sin(18 * i);
826 buffer.pos[i * 4 + 2] = 100;
827 buffer.pos[i * 4 + 3] = 100;
828 }
829 std::shared_ptr<Drawing::TextBlob> textBlob = builder.Make();
830
831 // 5.组合textBlob类接口,如果有返回值则获取上一步创建的textBlob返回值打印
832 Drawing::Paint paint;
833 paint.SetColor(0xFFFF0000);
834 float boundsx[] = {1, 2, 3};
835 int intercepts = textBlob->GetIntercepts(boundsx, nullptr, &paint);
836 std::string text2 = "intercepts = " + std::to_string(intercepts);
837
838 //6. 得到需要绘制的所有返回值text,全部适应固定的textBlob构造方式打印
839 std::vector<std::string> texts = {text2};
840
841 //7.调用ClipIRect截取(0,0,1000,1000)区域(缺省,默认INTERSECT、不抗锯齿)
842 auto rrect = Drawing::RoundRect(Drawing::Rect(0, 0, 1000, 1000), 52, 52);
843 playbackCanvas_->ClipRoundRect(rrect);
844
845 //8.调用Scale,Rotate,ConcatMatrix
846 Drawing::Matrix matrix;
847 playbackCanvas_->ConcatMatrix(matrix);
848 playbackCanvas_->Translate(400, 600);
849
850 //9.最终绘制
851 DrawTextBlob(texts, textBlob, font1, playbackCanvas_);
852 }
853
854 // 用例 Font_Scene_Transform_0231
855 DEF_DTK(Font_Scene_Transform_12, TestLevel::L2, 231)
856 {
857 // 1.创建typeface
858 std::shared_ptr<Drawing::FontMgr> fontMgr(Drawing::FontMgr::CreateDefaultFontMgr());
859 std::shared_ptr<Drawing::FontStyleSet> fontStyleSet(fontMgr->CreateStyleSet(0));
860 auto typeface = std::shared_ptr<Drawing::Typeface>(fontStyleSet->CreateTypeface(0));
861 // 2.组合typeface操作接口
862 std::string typefacestr = "GetUnitsPerEm = " + std::to_string(typeface->GetUnitsPerEm());
863 // 3.组合Font类接口,如果是操作类有返回值的接口,获取接口返回值加入vector容器
864 auto font = Drawing::Font(typeface, 50.f, 1.0f, 1.0f);
865 font.SetSubpixel(false);
866 font.SetEmbolden(false);
867 auto font1 = Drawing::Font(typeface, 50.f, 1.0f, 1.0f);
868 font1.SetSubpixel(font.IsSubpixel());
869 font1.SetEmbolden(font.IsEmbolden());
870
871 // 4.创建TextBlob
872 Drawing::TextBlobBuilder builder;
873 auto buffer = builder.AllocRunRSXform(font1, 20);
874 for (int i = 0; i < 20; i++) {
875 buffer.glyphs[i] = font1.UnicharToGlyph(0x30);
876 buffer.pos[i * 4] = cos(i * 18);
877 buffer.pos[i * 4 + 1] = sin(18 * i);
878 buffer.pos[i * 4 + 2] = 100;
879 buffer.pos[i * 4 + 3] = 100;
880 }
881 std::shared_ptr<Drawing::TextBlob> textBlob = builder.Make();
882
883 // 5.组合textBlob类接口,如果有返回值则获取上一步创建的textBlob返回值打印
884 std::string textinfo1 = "Deserialize @Hello World";
885 Drawing::TextBlob::Context* Ctx = new (std::nothrow) Drawing::TextBlob::Context(typeface, false);
886 auto data2 = textBlob->Serialize(Ctx);
887 std::shared_ptr<Drawing::TextBlob> infoTextBlob2 =
888 Drawing::TextBlob::Deserialize(data2->GetData(), data2->GetSize(), Ctx);
889
890 //6. 得到需要绘制的所有返回值text,全部适应固定的textBlob构造方式打印
891 std::vector<std::string> texts = {typefacestr};
892
893 //7.调用ClipIRect截取(0,0,1000,1000)区域(缺省,默认INTERSECT、不抗锯齿)
894 auto rrect = Drawing::RoundRect(Drawing::Rect(0, 0, 1000, 1000), 52, 52);
895 playbackCanvas_->ClipRoundRect(rrect);
896
897 //8.调用Scale,Rotate,ConcatMatrix
898 playbackCanvas_->Scale(0.5, 0.5);
899
900 //9.最终绘制
901 DrawTextBlob(texts, textBlob, font1, playbackCanvas_);
902 }
903
904 // 用例 Font_Scene_Transform_0232
905 DEF_DTK(Font_Scene_Transform_12, TestLevel::L2, 232)
906 {
907 // 1.创建typeface
908 std::shared_ptr<Drawing::FontMgr> fontMgr(Drawing::FontMgr::CreateDefaultFontMgr());
909 std::shared_ptr<Drawing::FontStyleSet> fontStyleSet(fontMgr->CreateStyleSet(0));
910 auto typeface = std::shared_ptr<Drawing::Typeface>(fontStyleSet->CreateTypeface(0));
911 // 2.组合typeface操作接口
912 std::string typefacestr = "GetUniqueID = " + std::to_string(typeface->GetUniqueID());
913 // 3.组合Font类接口,如果是操作类有返回值的接口,获取接口返回值加入vector容器
914 auto font1 = Drawing::Font(typeface, 50.f, 1.0f, 1.0f);
915 font1.SetSubpixel(false);
916 std::string text0 = "DDGR ddgr 鸿蒙 !@#¥%^&*; : , 。";
917 auto scalar = font1.UnicharToGlyph(0x44);
918 std::string text4 = "Glyphs = " + std::to_string(scalar);
919
920 // 4.创建TextBlob
921 Drawing::TextBlobBuilder builder;
922 auto buffer = builder.AllocRunRSXform(font1, 20);
923 for (int i = 0; i < 20; i++) {
924 buffer.glyphs[i] = font1.UnicharToGlyph(0x30);
925 buffer.pos[i * 4] = cos(i * 18);
926 buffer.pos[i * 4 + 1] = sin(18 * i);
927 buffer.pos[i * 4 + 2] = 100;
928 buffer.pos[i * 4 + 3] = 100;
929 }
930 std::shared_ptr<Drawing::TextBlob> textBlob = builder.Make();
931
932 // 5.组合textBlob类接口,如果有返回值则获取上一步创建的textBlob返回值打印
933 std::string name1 = "HMOS Color Emoji";
934 std::string textInfo1 = "";
935 std::shared_ptr<Drawing::TextBlob> textBlob1 = Drawing::TextBlob::MakeFromText(textInfo1.c_str(),
936 textInfo1.size(), font1, Drawing::TextEncoding::UTF8);
937 if (textBlob1->IsEmoji()) {
938 playbackCanvas_->DrawBackground(0xFF0000FF);
939 } else {
940 playbackCanvas_->DrawBackground(0xFFFF0000);
941 }
942
943 //6. 得到需要绘制的所有返回值text,全部适应固定的textBlob构造方式打印
944 std::vector<std::string> texts = {typefacestr, text4};
945
946 //7.调用ClipIRect截取(0,0,1000,1000)区域(缺省,默认INTERSECT、不抗锯齿)
947 auto rrect = Drawing::RoundRect(Drawing::Rect(0, 0, 1000, 1000), 52, 52);
948 playbackCanvas_->ClipRoundRect(rrect);
949
950 //8.调用Scale,Rotate,ConcatMatrix
951 Drawing::Matrix matrix;
952 playbackCanvas_->ConcatMatrix(matrix);
953 playbackCanvas_->Rotate(45, 50, 50);
954 playbackCanvas_->Translate(400, 600);
955
956 //9.最终绘制
957 DrawTextBlob(texts, textBlob, font1, playbackCanvas_);
958 }
959
960 }
961 }
962