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