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