• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include <string>
17 #include <fstream>
18 #include "drawing_color.h"
19 #include "drawing_font.h"
20 #include "drawing_rect.h"
21 #include "drawing_font_collection.h"
22 #include "drawing_text_declaration.h"
23 #include "drawing_text_line.h"
24 #include "drawing_text_typography.h"
25 #include "gtest/gtest.h"
26 
27 #define NUM_1 1
28 #define NUM_2 2
29 #define NUM_3 3
30 #define NUM_4 4
31 #define NUM_5 5
32 #define NUM_6 6
33 #define NUM_7 7
34 #define NUM_10 10
35 #define NUM_20 20
36 #define NUM_600 600
37 #define NUM_700 700
38 
39 using namespace testing;
40 using namespace testing::ext;
41 
42 namespace OHOS {
43 namespace {
44     constexpr static float FLOAT_DATA_EPSILON = 1e-6f;
45 }
46 
47 class NativeTextLineTest : public testing::Test {
48 public:
SetUp()49     void SetUp() override
50     {
51         typoStyle_ = nullptr;
52         txtStyle_ = nullptr;
53         fontCollection_ = nullptr;
54         handler_ = nullptr;
55         typography_ = nullptr;
56     }
57 
TearDown()58     void TearDown() override
59     {
60         if (typography_ != nullptr) {
61             OH_Drawing_DestroyTypography(typography_);
62             typography_ = nullptr;
63         }
64         if (handler_ != nullptr) {
65             OH_Drawing_DestroyTypographyHandler(handler_);
66             handler_ = nullptr;
67         }
68         if (txtStyle_ != nullptr) {
69             OH_Drawing_DestroyTextStyle(txtStyle_);
70             txtStyle_ = nullptr;
71         }
72         if (typoStyle_ != nullptr) {
73             OH_Drawing_DestroyTypographyStyle(typoStyle_);
74             typoStyle_ = nullptr;
75         }
76         if (fontCollection_ != nullptr) {
77             OH_Drawing_DestroyFontCollection(fontCollection_);
78             fontCollection_ = nullptr;
79         }
80     }
81 
82     void PrepareCreateTextLine(const std::string& text);
83 
84 protected:
85     OH_Drawing_TypographyStyle* typoStyle_ = nullptr;
86     OH_Drawing_TextStyle* txtStyle_ = nullptr;
87     OH_Drawing_FontCollection* fontCollection_ = nullptr;
88     OH_Drawing_TypographyCreate* handler_ = nullptr;
89     OH_Drawing_Typography* typography_ = nullptr;
90 };
91 
PrepareCreateTextLine(const std::string & text)92 void NativeTextLineTest::PrepareCreateTextLine(const std::string& text)
93 {
94     double maxWidth = 500.0;
95     typoStyle_ = OH_Drawing_CreateTypographyStyle();
96     EXPECT_TRUE(typoStyle_ != nullptr);
97     txtStyle_ = OH_Drawing_CreateTextStyle();
98     EXPECT_TRUE(txtStyle_ != nullptr);
99     fontCollection_ = OH_Drawing_CreateFontCollection();
100     EXPECT_TRUE(fontCollection_ != nullptr);
101     handler_ = OH_Drawing_CreateTypographyHandler(typoStyle_, fontCollection_);
102     EXPECT_TRUE(handler_ != nullptr);
103     OH_Drawing_SetTextStyleColor(txtStyle_, OH_Drawing_ColorSetArgb(0xFF, 0x00, 0x00, 0x00));
104     double fontSize = 30;
105     OH_Drawing_SetTextStyleFontSize(txtStyle_, fontSize);
106     OH_Drawing_SetTextStyleFontWeight(txtStyle_, FONT_WEIGHT_400);
107     bool halfLeading = true;
108     OH_Drawing_SetTextStyleHalfLeading(txtStyle_, halfLeading);
109     const char* fontFamilies[] = {"Roboto"};
110     OH_Drawing_SetTextStyleFontFamilies(txtStyle_, NUM_1, fontFamilies);
111     OH_Drawing_TypographyHandlerPushTextStyle(handler_, txtStyle_);
112     OH_Drawing_TypographyHandlerAddText(handler_, text.c_str());
113     OH_Drawing_TypographyHandlerPopTextStyle(handler_);
114     typography_ = OH_Drawing_CreateTypography(handler_);
115     EXPECT_TRUE(typography_ != nullptr);
116     OH_Drawing_TypographyLayout(typography_, maxWidth);
117 }
118 
119 /*
120  * @tc.number: SUB_GRAPHIC_GRAPHIC_2D_TextLineTest_001
121  * @tc.name  : TextLineTest001
122  * @tc.desc  : test for the textLine GetGlyphCount
123  * @tc.size  : MediumTest
124  * @tc.type  : Function
125  * @tc.level : Level 0
126  */
127 HWTEST_F(NativeTextLineTest, TextLineTest001, Function | MediumTest | Level0)
128 {
129     PrepareCreateTextLine("Hello 测 World \n!@#$%^&*~(){}[] 123 4567890 - = ,. < >、/Drawing testlp 试 Drawing  ");
130     OH_Drawing_Array* textLines = OH_Drawing_TypographyGetTextLines(typography_);
131     size_t size = OH_Drawing_GetDrawingArraySize(textLines);
132     EXPECT_EQ(size, NUM_3);
133 
134     OH_Drawing_TextLine* textLine0 = OH_Drawing_GetTextLineByIndex(textLines, 0);
135     double data0 = 13;
136     double count0 = OH_Drawing_TextLineGetGlyphCount(textLine0);
137     EXPECT_EQ(count0, data0);
138 
139     OH_Drawing_TextLine* textLine1 = OH_Drawing_GetTextLineByIndex(textLines, NUM_1);
140     double data1 = 34;
141     double count1 = OH_Drawing_TextLineGetGlyphCount(textLine1);
142     EXPECT_EQ(count1, data1);
143 
144     OH_Drawing_TextLine* textLine2 = OH_Drawing_GetTextLineByIndex(textLines, NUM_2);
145     double data2 = 29;
146     double count2 = OH_Drawing_TextLineGetGlyphCount(textLine2);
147     EXPECT_EQ(count2, data2);
148 
149     OH_Drawing_DestroyTextLines(textLines);
150 }
151 
152 /*
153  * @tc.number: SUB_GRAPHIC_GRAPHIC_2D_TextLineTest_002
154  * @tc.name  : TextLineTest002
155  * @tc.desc  : test for the textLine GetGlyphCount
156  * @tc.size  : MediumTest
157  * @tc.type  : Function
158  * @tc.level : Level 0
159  */
160 HWTEST_F(NativeTextLineTest, TextLineTest002, Function | MediumTest | Level0)
161 {
162     PrepareCreateTextLine(
163         "Hello \t 中国 测 World \n !@#$%^&*~(){}[] 123 4567890 - = ,. < >、/ Drawing testlp 试 "
164         "Drawing \n\n   \u231A \u513B"
165         " \u00A9\uFE0F aaa clp11⌚��������‍����‍��‍��‍����مرحبا中国 测 World测试文本\n123");
166     OH_Drawing_Array* textLines = OH_Drawing_TypographyGetTextLines(typography_);
167     size_t size = OH_Drawing_GetDrawingArraySize(textLines);
168     EXPECT_EQ(size, NUM_7);
169     for (size_t index = 0; index < size; index++) {
170         OH_Drawing_TextLine* textLine = OH_Drawing_GetTextLineByIndex(textLines, index);
171         EXPECT_TRUE(textLine != nullptr);
172 
173         double count = OH_Drawing_TextLineGetGlyphCount(textLine);
174         if (index == NUM_3) {
175             EXPECT_EQ(count, 0);
176         } else if (index == NUM_6) {
177             EXPECT_EQ(count, NUM_3);
178         } else {
179             EXPECT_GE(count, NUM_10);
180         }
181     }
182     OH_Drawing_DestroyTextLines(textLines);
183 }
184 
185 /*
186  * @tc.number: SUB_GRAPHIC_GRAPHIC_2D_TextLineTest_003
187  * @tc.name  : TextLineTest003
188  * @tc.desc  : test for the textLine GetGlyphCount
189  * @tc.size  : MediumTest
190  * @tc.type  : Function
191  * @tc.level : Level 0
192  */
193 HWTEST_F(NativeTextLineTest, TextLineTest003, Function | MediumTest | Level0)
194 {
195     PrepareCreateTextLine("\n\n\n\n");
196     OH_Drawing_Array* textLines = OH_Drawing_TypographyGetTextLines(typography_);
197     size_t size = OH_Drawing_GetDrawingArraySize(textLines);
198     EXPECT_EQ(size, NUM_5);
199     for (size_t index = 0; index < size; index++) {
200         OH_Drawing_TextLine* textLine = OH_Drawing_GetTextLineByIndex(textLines, index);
201         EXPECT_TRUE(textLine != nullptr);
202 
203         double count = OH_Drawing_TextLineGetGlyphCount(textLine);
204         EXPECT_EQ(count, 0);
205     }
206     OH_Drawing_DestroyTextLines(textLines);
207 
208     double count = OH_Drawing_TextLineGetGlyphCount(nullptr);
209     EXPECT_EQ(count, 0);
210 }
211 
212 /*
213  * @tc.number: SUB_GRAPHIC_GRAPHIC_2D_TextLineTest_004
214  * @tc.name  : TextLineTest004
215  * @tc.desc  : test for the textLine GetTextRange
216  * @tc.size  : MediumTest
217  * @tc.type  : Function
218  * @tc.level : Level 0
219  */
220 HWTEST_F(NativeTextLineTest, TextLineTest004, Function | MediumTest | Level0)
221 {
222     PrepareCreateTextLine("Hello 测 World \n!@#$%^&*~(){}[] 123 4567890 - = ,. < >、/Drawing testlp 试 Drawing  ");
223     OH_Drawing_Array* textLines = OH_Drawing_TypographyGetTextLines(typography_);
224     size_t size = OH_Drawing_GetDrawingArraySize(textLines);
225     EXPECT_EQ(size, NUM_3);
226     size_t start = 0;
227     size_t end = 0;
228     size_t data1 = 16;
229     size_t data2 = 17;
230     size_t data3 = 51;
231     size_t data4 = 52;
232     size_t data5 = 87;
233     for (size_t index = 0; index < size; index++) {
234         OH_Drawing_TextLine* textLine = OH_Drawing_GetTextLineByIndex(textLines, index);
235         EXPECT_TRUE(textLine != nullptr);
236 
237         OH_Drawing_TextLineGetTextRange(textLine, &start, &end);
238         if (index == 0) {
239             EXPECT_EQ(start, 0);
240             EXPECT_EQ(end, data1);
241         }else if (index == NUM_1) {
242             EXPECT_EQ(start, data2);
243             EXPECT_EQ(end, data3);
244         }else {
245             EXPECT_EQ(start, data4);
246             EXPECT_EQ(end, data5);
247         }
248     }
249     OH_Drawing_DestroyTextLines(textLines);
250 }
251 
252 /*
253  * @tc.number: SUB_GRAPHIC_GRAPHIC_2D_TextLineTest_005
254  * @tc.name  : TextLineTest005
255  * @tc.desc  : test for the textLine GetTextRange
256  * @tc.size  : MediumTest
257  * @tc.type  : Function
258  * @tc.level : Level 0
259  */
260 HWTEST_F(NativeTextLineTest, TextLineTest005, Function | MediumTest | Level0)
261 {
262     PrepareCreateTextLine(
263         "Hello \t 中国 测 World \n !@#$%^&*~(){}[] 123 4567890 - = ,. < >、/ Drawing testlp 试 "
264         "Drawing \n\n   \u231A \u513B"
265         " \u00A9\uFE0F aaa clp11⌚��������‍����‍��‍��‍����مرحبا中国 测 World测试文本\n123");
266     OH_Drawing_Array* textLines = OH_Drawing_TypographyGetTextLines(typography_);
267     size_t size = OH_Drawing_GetDrawingArraySize(textLines);
268     EXPECT_EQ(size, NUM_7);
269 
270     size_t start = 0;
271     size_t end = 0;
272     std::vector<int32_t> startArr = {0, 26, 62, 98, 99, 176, 219};
273     std::vector<int32_t> endArr = {25, 61, 97, 98, 176, 218, 222};
274     for (size_t index = 0; index < size; index++) {
275         OH_Drawing_TextLine* textLine = OH_Drawing_GetTextLineByIndex(textLines, index);
276         EXPECT_TRUE(textLine != nullptr);
277 
278         OH_Drawing_TextLineGetTextRange(textLine, &start, &end);
279         EXPECT_EQ(start, startArr[index]);
280         EXPECT_EQ(end, endArr[index]);
281     }
282     OH_Drawing_DestroyTextLines(textLines);
283 }
284 
285 /*
286  * @tc.number: SUB_GRAPHIC_GRAPHIC_2D_TextLineTest_006
287  * @tc.name  : TextLineTest006
288  * @tc.desc  : test for the textLine GetTextRange
289  * @tc.size  : MediumTest
290  * @tc.type  : Function
291  * @tc.level : Level 0
292  */
293 HWTEST_F(NativeTextLineTest, TextLineTest006, Function | MediumTest | Level0)
294 {
295     PrepareCreateTextLine("\n");
296     OH_Drawing_Array* textLines = OH_Drawing_TypographyGetTextLines(typography_);
297     size_t size = OH_Drawing_GetDrawingArraySize(textLines);
298     EXPECT_EQ(size, NUM_2);
299 
300     size_t start = 0;
301     size_t end = 0;
302     for (size_t index = 0; index < size; index++) {
303         OH_Drawing_TextLine* textLine = OH_Drawing_GetTextLineByIndex(textLines, index);
304         EXPECT_TRUE(textLine != nullptr);
305 
306         OH_Drawing_TextLineGetTextRange(textLine, &start, &end);
307         EXPECT_EQ(start, 0);
308         EXPECT_EQ(end, NUM_1);
309     }
310     OH_Drawing_DestroyTextLines(textLines);
311 }
312 
313 /*
314  * @tc.number: SUB_GRAPHIC_GRAPHIC_2D_TextLineTest_007
315  * @tc.name  : TextLineTest007
316  * @tc.desc  : test for the textLine GetTextRange
317  * @tc.size  : MediumTest
318  * @tc.type  : Function
319  * @tc.level : Level 0
320  */
321 HWTEST_F(NativeTextLineTest, TextLineTest007, Function | MediumTest | Level0)
322 {
323     PrepareCreateTextLine("\n\n\n");
324     OH_Drawing_Array* textLines = OH_Drawing_TypographyGetTextLines(typography_);
325     size_t size = OH_Drawing_GetDrawingArraySize(textLines);
326     EXPECT_EQ(size, NUM_4);
327 
328     OH_Drawing_TextLine* textLine = textLine = OH_Drawing_GetTextLineByIndex(textLines, 0);
329     EXPECT_TRUE(textLine != nullptr);
330 
331     size_t start = 0;
332     OH_Drawing_TextLineGetTextRange(textLine, &start, nullptr);
333     EXPECT_EQ(start, 0);
334 
335     size_t end = 0;
336     OH_Drawing_TextLineGetTextRange(textLine, nullptr, &end);
337     EXPECT_EQ(end, 0);
338 
339     OH_Drawing_TextLineGetTextRange(textLine, nullptr, nullptr);
340     OH_Drawing_DestroyTextLines(textLines);
341 }
342 
343 /*
344  * @tc.number: SUB_GRAPHIC_GRAPHIC_2D_TextLineTest_008
345  * @tc.name  : TextLineTest008
346  * @tc.desc  : test for the textLine GetGlyphRuns
347  * @tc.size  : MediumTest
348  * @tc.type  : Function
349  * @tc.level : Level 0
350  */
351 HWTEST_F(NativeTextLineTest, TextLineTest008, Function | MediumTest | Level0)
352 {
353     PrepareCreateTextLine("Hello 测 World \n!@#$%^&*~(){}[] 123 4567890 - = ,. < >、/Drawing testlp 试 Drawing  ");
354     OH_Drawing_Array* textLines = OH_Drawing_TypographyGetTextLines(typography_);
355     size_t size = OH_Drawing_GetDrawingArraySize(textLines);
356     EXPECT_EQ(size, NUM_3);
357 
358     std::vector<int32_t> sizeArr = {4, 1, 6};
359     for (size_t index = 0; index < size; index++) {
360         OH_Drawing_TextLine* textLine = OH_Drawing_GetTextLineByIndex(textLines, index);
361         EXPECT_TRUE(textLine != nullptr);
362 
363         OH_Drawing_Array *runs = OH_Drawing_TextLineGetGlyphRuns(textLine);
364         EXPECT_TRUE(runs != nullptr);
365         size_t runsSize = OH_Drawing_GetDrawingArraySize(runs);
366         EXPECT_EQ(runsSize, sizeArr[index]);
367         OH_Drawing_DestroyRuns(runs);
368     }
369     OH_Drawing_DestroyTextLines(textLines);
370 }
371 
372 /*
373  * @tc.number: SUB_GRAPHIC_GRAPHIC_2D_TextLineTest_009
374  * @tc.name  : TextLineTest009
375  * @tc.desc  : test for the textLine GetGlyphRuns
376  * @tc.size  : MediumTest
377  * @tc.type  : Function
378  * @tc.level : Level 0
379  */
380 HWTEST_F(NativeTextLineTest, TextLineTest009, Function | MediumTest | Level0)
381 {
382     PrepareCreateTextLine(
383         "Hello \t 中国 测 World \n !@#$%^&*~(){}[] 123 4567890 - = ,. < >、/ Drawing testlp 试 "
384         "Drawing \n\n   \u231A \u513B"
385         " \u00A9\uFE0F aaa clp11⌚��������‍����‍��‍��‍����مرحبا中国 测 World测试文本\n123");
386     OH_Drawing_Array* textLines = OH_Drawing_TypographyGetTextLines(typography_);
387     size_t size = OH_Drawing_GetDrawingArraySize(textLines);
388     EXPECT_EQ(size, NUM_7);
389 
390     std::vector<int32_t> sizeArr = {6, 1, 6, 0, 7, 8, 1};
391     for (size_t index = 0; index < size; index++) {
392         OH_Drawing_TextLine* textLine = OH_Drawing_GetTextLineByIndex(textLines, index);
393         EXPECT_TRUE(textLine != nullptr);
394 
395         OH_Drawing_Array *runs = OH_Drawing_TextLineGetGlyphRuns(textLine);
396         size_t runsSize = OH_Drawing_GetDrawingArraySize(runs);
397         if (index == NUM_3) {
398             EXPECT_TRUE(runs == nullptr);
399             EXPECT_EQ(runsSize, 0);
400         } else if (index == NUM_1 || index == NUM_6) {
401             EXPECT_TRUE(runs != nullptr);
402             EXPECT_EQ(runsSize, NUM_1);
403         } else {
404             EXPECT_TRUE(runs != nullptr);
405             EXPECT_GE(runsSize, NUM_6);
406         }
407         EXPECT_EQ(runsSize, sizeArr[index]);
408         OH_Drawing_DestroyRuns(runs);
409     }
410     OH_Drawing_DestroyTextLines(textLines);
411 }
412 
413 /*
414  * @tc.number: SUB_GRAPHIC_GRAPHIC_2D_TextLineTest_010
415  * @tc.name  : TextLineTest010
416  * @tc.desc  : test for the textLine GetGlyphRuns
417  * @tc.size  : MediumTest
418  * @tc.type  : Function
419  * @tc.level : Level 0
420  */
421 HWTEST_F(NativeTextLineTest, TextLineTest010, Function | MediumTest | Level0)
422 {
423     PrepareCreateTextLine("\n\n\n");
424     OH_Drawing_Array* textLines = OH_Drawing_TypographyGetTextLines(typography_);
425     size_t size = OH_Drawing_GetDrawingArraySize(textLines);
426     EXPECT_EQ(size, NUM_4);
427 
428     for (size_t index = 0; index < size - NUM_1; index++) {
429         OH_Drawing_TextLine* textLine = OH_Drawing_GetTextLineByIndex(textLines, index);
430         EXPECT_TRUE(textLine != nullptr);
431 
432         OH_Drawing_Array *runs = OH_Drawing_TextLineGetGlyphRuns(textLine);
433         EXPECT_TRUE(runs == nullptr);
434         size_t runsSize = OH_Drawing_GetDrawingArraySize(runs);
435         EXPECT_EQ(runsSize, 0);
436         OH_Drawing_DestroyRuns(runs);
437     }
438     OH_Drawing_DestroyTextLines(textLines);
439 }
440 
441 /*
442  * @tc.number: SUB_GRAPHIC_GRAPHIC_2D_TextLineTest_011
443  * @tc.name  : TextLineTest011
444  * @tc.desc  : test for the textLine GetGlyphRuns
445  * @tc.size  : MediumTest
446  * @tc.type  : Function
447  * @tc.level : Level 0
448  */
449 HWTEST_F(NativeTextLineTest, TextLineTest011, Function | MediumTest | Level0)
450 {
451     PrepareCreateTextLine("\n\n");
452     OH_Drawing_Array* textLines = OH_Drawing_TypographyGetTextLines(typography_);
453     size_t size = OH_Drawing_GetDrawingArraySize(textLines);
454     EXPECT_EQ(size, NUM_3);
455 
456     OH_Drawing_TextLine* textLine = OH_Drawing_GetTextLineByIndex(textLines, 0);
457     EXPECT_TRUE(textLine != nullptr);
458 
459     OH_Drawing_Array *runs = OH_Drawing_TextLineGetGlyphRuns(textLine);
460     EXPECT_TRUE(runs == nullptr);
461 
462     runs = OH_Drawing_TextLineGetGlyphRuns(nullptr);
463     EXPECT_TRUE(runs == nullptr);
464 
465     OH_Drawing_DestroyTextLines(textLines);
466 }
467 
468 /*
469  * @tc.number: SUB_GRAPHIC_GRAPHIC_2D_TextLineTest_012
470  * @tc.name  : TextLineTest012
471  * @tc.desc  : test for the textLine GetTypographicBounds
472  * @tc.size  : MediumTest
473  * @tc.type  : Function
474  * @tc.level : Level 0
475  */
476 HWTEST_F(NativeTextLineTest, TextLineTest012, Function | MediumTest | Level0)
477 {
478     PrepareCreateTextLine("Hello 测 World \n!@#$%^&*~(){}[] 123 4567890 - = ,. < >、/Drawing testlp 试 Drawing  ");
479     OH_Drawing_Array* textLines = OH_Drawing_TypographyGetTextLines(typography_);
480     size_t size = OH_Drawing_GetDrawingArraySize(textLines);
481     EXPECT_EQ(size, NUM_3);
482     double ascent = 0.0;
483     double descent = 0.0;
484     double leading = 0.0;
485     std::vector<float> widthArr = {206.639786, 490.139404, 459.509460};
486     for (size_t index = 0; index < size; index++) {
487         OH_Drawing_TextLine* textLine = OH_Drawing_GetTextLineByIndex(textLines, index);
488         EXPECT_TRUE(textLine != nullptr);
489 
490         double width = OH_Drawing_TextLineGetTypographicBounds(textLine, &ascent, &descent, &leading);
491         EXPECT_NEAR(ascent, -27.84, FLOAT_DATA_EPSILON);
492         EXPECT_NEAR(descent, 7.32, FLOAT_DATA_EPSILON);
493         EXPECT_NEAR(leading, 0.0, FLOAT_DATA_EPSILON);
494         EXPECT_NEAR(width, widthArr[index], FLOAT_DATA_EPSILON);
495     }
496     OH_Drawing_DestroyTextLines(textLines);
497 }
498 
499 /*
500  * @tc.number: SUB_GRAPHIC_GRAPHIC_2D_TextLineTest_013
501  * @tc.name  : TextLineTest013
502  * @tc.desc  : test for the textLine GetTypographicBounds
503  * @tc.size  : MediumTest
504  * @tc.type  : Function
505  * @tc.level : Level 0
506  */
507 HWTEST_F(NativeTextLineTest, TextLineTest013, Function | MediumTest | Level0)
508 {
509     PrepareCreateTextLine(
510         "Hello \t 中国 测 World \n !@#$%^&*~(){}[] 123 4567890 - = ,. < >、/ Drawing testlp 试 "
511         "Drawing \n\n   \u231A \u513B"
512         " \u00A9\uFE0F aaa clp11⌚��������‍����‍��‍��‍����مرحبا中国 测 World测试文本\n123");
513     OH_Drawing_Array* textLines = OH_Drawing_TypographyGetTextLines(typography_);
514     size_t size = OH_Drawing_GetDrawingArraySize(textLines);
515     EXPECT_EQ(size, NUM_7);
516 
517     double ascent = 0.0;
518     double descent = 0.0;
519     double leading = 0.0;
520     std::vector<float> widthArr = {290.939697, 498.239380, 458.309509, 0.0, 497.952301, 409.497314, 51.300049};
521     for (size_t index = 0; index < size; index++) {
522         OH_Drawing_TextLine* textLine = OH_Drawing_GetTextLineByIndex(textLines, index);
523         EXPECT_TRUE(textLine != nullptr);
524 
525         double width = OH_Drawing_TextLineGetTypographicBounds(textLine, &ascent, &descent, &leading);
526         EXPECT_EQ(leading, 0);
527         if (index == NUM_4) {
528             EXPECT_NEAR(ascent, -27.84, FLOAT_DATA_EPSILON);
529             EXPECT_NEAR(descent, 7.431193, FLOAT_DATA_EPSILON);
530         } else if (index == NUM_5) {
531             EXPECT_NEAR(ascent, -35.369999, FLOAT_DATA_EPSILON);
532             EXPECT_NEAR(descent, 9.690001, FLOAT_DATA_EPSILON);
533         } else {
534             EXPECT_NEAR(ascent, -27.84, FLOAT_DATA_EPSILON);
535             EXPECT_NEAR(descent, 7.32, FLOAT_DATA_EPSILON);
536         }
537         EXPECT_NEAR(width, widthArr[index], FLOAT_DATA_EPSILON);
538     }
539     OH_Drawing_DestroyTextLines(textLines);
540 }
541 
542 /*
543  * @tc.number: SUB_GRAPHIC_GRAPHIC_2D_TextLineTest_014
544  * @tc.name  : TextLineTest014
545  * @tc.desc  : test for the textLine GetTypographicBounds
546  * @tc.size  : MediumTest
547  * @tc.type  : Function
548  * @tc.level : Level 0
549  */
550 HWTEST_F(NativeTextLineTest, TextLineTest014, Function | MediumTest | Level0)
551 {
552     PrepareCreateTextLine("\n\n\n\n");
553     OH_Drawing_Array* textLines = OH_Drawing_TypographyGetTextLines(typography_);
554     size_t size = OH_Drawing_GetDrawingArraySize(textLines);
555     EXPECT_EQ(size, NUM_5);
556 
557     double ascent = 0.0;
558     double descent = 0.0;
559     double leading = 0.0;
560     for (size_t index = 0; index < size; index++) {
561         OH_Drawing_TextLine* textLine = OH_Drawing_GetTextLineByIndex(textLines, index);
562         EXPECT_TRUE(textLine != nullptr);
563 
564         double width = OH_Drawing_TextLineGetTypographicBounds(textLine, &ascent, &descent, &leading);
565         EXPECT_NEAR(ascent, -27.84, FLOAT_DATA_EPSILON);
566         EXPECT_NEAR(descent, 7.32, FLOAT_DATA_EPSILON);
567         EXPECT_EQ(leading, 0);
568         EXPECT_EQ(width, 0);
569     }
570     OH_Drawing_DestroyTextLines(textLines);
571 }
572 
573 /*
574  * @tc.number: SUB_GRAPHIC_GRAPHIC_2D_TextLineTest_015
575  * @tc.name  : TextLineTest015
576  * @tc.desc  : test for the textLine GetTypographicBounds
577  * @tc.size  : MediumTest
578  * @tc.type  : Function
579  * @tc.level : Level 0
580  */
581 HWTEST_F(NativeTextLineTest, TextLineTest015, Function | MediumTest | Level0)
582 {
583     PrepareCreateTextLine("\n\n");
584     OH_Drawing_Array* textLines = OH_Drawing_TypographyGetTextLines(typography_);
585     size_t size = OH_Drawing_GetDrawingArraySize(textLines);
586     EXPECT_EQ(size, NUM_3);
587 
588     OH_Drawing_TextLine* textLine = textLine = OH_Drawing_GetTextLineByIndex(textLines, 0);
589     EXPECT_TRUE(textLine != nullptr);
590 
591     double ascent = 0.0;
592     double width = OH_Drawing_TextLineGetTypographicBounds(textLine, &ascent, nullptr, nullptr);
593     EXPECT_EQ(ascent, 0);
594     EXPECT_EQ(width, 0);
595 
596     double descent = 0.0;
597     width = OH_Drawing_TextLineGetTypographicBounds(textLine, nullptr, &descent, nullptr);
598     EXPECT_EQ(descent, 0);
599     EXPECT_EQ(width, 0);
600 
601     double leading = 0.0;
602     width = OH_Drawing_TextLineGetTypographicBounds(textLine, nullptr, nullptr, &leading);
603     EXPECT_EQ(leading, 0);
604     EXPECT_EQ(width, 0);
605 
606     width = OH_Drawing_TextLineGetTypographicBounds(textLine, nullptr, nullptr, nullptr);
607     EXPECT_EQ(width, 0);
608 
609     OH_Drawing_DestroyTextLines(textLines);
610 }
611 
612 /*
613  * @tc.number: SUB_GRAPHIC_GRAPHIC_2D_TextLineTest_016
614  * @tc.name  : TextLineTest016
615  * @tc.desc  : test for the textLine GetTrailingSpaceWidth
616  * @tc.size  : MediumTest
617  * @tc.type  : Function
618  * @tc.level : Level 0
619  */
620 HWTEST_F(NativeTextLineTest, TextLineTest016, Function | MediumTest | Level0)
621 {
622     PrepareCreateTextLine("Hello 测 World \n!@#$%^&*~(){}[] 123 4567890 - = ,. < >、/Drawing testlp 试 Drawing  ");
623     OH_Drawing_Array* textLines = OH_Drawing_TypographyGetTextLines(typography_);
624     size_t size = OH_Drawing_GetDrawingArraySize(textLines);
625     EXPECT_EQ(size, NUM_3);
626 
627     std::vector<float> widthArr = {8.099991, 8.099976, 16.199951};
628     for (size_t index = 0; index < size; index++) {
629         OH_Drawing_TextLine* textLine = OH_Drawing_GetTextLineByIndex(textLines, index);
630         EXPECT_TRUE(textLine != nullptr);
631 
632         double width = OH_Drawing_TextLineGetTrailingSpaceWidth(textLine);
633         EXPECT_NEAR(width, widthArr[index], FLOAT_DATA_EPSILON);
634     }
635     OH_Drawing_DestroyTextLines(textLines);
636 }
637 
638 /*
639  * @tc.number: SUB_GRAPHIC_GRAPHIC_2D_TextLineTest_017
640  * @tc.name  : TextLineTest017
641  * @tc.desc  : test for the textLine GetTrailingSpaceWidth
642  * @tc.size  : MediumTest
643  * @tc.type  : Function
644  * @tc.level : Level 0
645  */
646 HWTEST_F(NativeTextLineTest, TextLineTest017, Function | MediumTest | Level0)
647 {
648     PrepareCreateTextLine(
649         "Hello \t 中国 测 World \n !@#$%^&*~(){}[] 123 4567890 - = ,. < >、/ Drawing testlp 试 "
650         "Drawing \n\n   \u231A \u513B"
651         " \u00A9\uFE0F aaa clp11⌚��������‍����‍��‍��‍����مرحبا中国 测 World测试文本\n123");
652     OH_Drawing_Array* textLines = OH_Drawing_TypographyGetTextLines(typography_);
653     size_t size = OH_Drawing_GetDrawingArraySize(textLines);
654     EXPECT_EQ(size, NUM_7);
655 
656     for (size_t index = 0; index < size; index++) {
657         OH_Drawing_TextLine* textLine = OH_Drawing_GetTextLineByIndex(textLines, index);
658         EXPECT_TRUE(textLine != nullptr);
659 
660         double width = OH_Drawing_TextLineGetTrailingSpaceWidth(textLine);
661         if (index < NUM_3) {
662             EXPECT_NEAR(width, 8.099976, FLOAT_DATA_EPSILON);
663         } else {
664             EXPECT_EQ(width, 0.0);
665         }
666     }
667     OH_Drawing_DestroyTextLines(textLines);
668 }
669 
670 /*
671  * @tc.number: SUB_GRAPHIC_GRAPHIC_2D_TextLineTest_018
672  * @tc.name  : TextLineTest018
673  * @tc.desc  : test for the textLine GetTrailingSpaceWidth
674  * @tc.size  : MediumTest
675  * @tc.type  : Function
676  * @tc.level : Level 0
677  */
678 HWTEST_F(NativeTextLineTest, TextLineTest018, Function | MediumTest | Level0)
679 {
680     PrepareCreateTextLine("\n\n\n\n");
681     OH_Drawing_Array* textLines = OH_Drawing_TypographyGetTextLines(typography_);
682     size_t size = OH_Drawing_GetDrawingArraySize(textLines);
683     EXPECT_EQ(size, NUM_5);
684 
685     for (size_t index = 0; index < size; index++) {
686         OH_Drawing_TextLine* textLine = OH_Drawing_GetTextLineByIndex(textLines, index);
687         EXPECT_TRUE(textLine != nullptr);
688 
689         double width = OH_Drawing_TextLineGetTrailingSpaceWidth(textLine);
690         EXPECT_EQ(width, 0.0);
691     }
692     OH_Drawing_DestroyTextLines(textLines);
693 
694     double width = OH_Drawing_TextLineGetTrailingSpaceWidth(nullptr);
695     EXPECT_EQ(width, 0.0);
696 }
697 
698 /*
699  * @tc.number: SUB_GRAPHIC_GRAPHIC_2D_TextLineTest_019
700  * @tc.name  : TextLineTest019
701  * @tc.desc  : test for the textLine GetOffsetForStringIndex
702  * @tc.size  : MediumTest
703  * @tc.type  : Function
704  * @tc.level : Level 0
705  */
706 HWTEST_F(NativeTextLineTest, TextLineTest019, Function | MediumTest | Level0)
707 {
708     PrepareCreateTextLine("Hello 测 World \n!@#$%^&*~(){}[] 123 4567890 - = ,. < >、/Drawing testlp 试 Drawing  ");
709     OH_Drawing_Array* textLines = OH_Drawing_TypographyGetTextLines(typography_);
710     size_t size = OH_Drawing_GetDrawingArraySize(textLines);
711     EXPECT_EQ(size, NUM_3);
712 
713     const int maxCharacterNum = 88;
714     std::vector<float> offSetArr = {206.639786, 490.139404, 459.509460};
715     for (size_t index = 0; index < size; index++) {
716         OH_Drawing_TextLine* textLine = OH_Drawing_GetTextLineByIndex(textLines, index);
717         EXPECT_TRUE(textLine != nullptr);
718 
719         double offset = OH_Drawing_TextLineGetOffsetForStringIndex(textLine, 0);
720         EXPECT_EQ(offset, 0.0);
721         offset = OH_Drawing_TextLineGetOffsetForStringIndex(textLine, NUM_10);
722         if (index == 0) {
723             EXPECT_NEAR(offset, 161.939835, FLOAT_DATA_EPSILON);
724         } else {
725             EXPECT_EQ(offset, 0.0);
726         }
727         EXPECT_LE(offset, 500.0);
728         offset = OH_Drawing_TextLineGetOffsetForStringIndex(textLine, maxCharacterNum);
729         EXPECT_NEAR(offset, offSetArr[index], FLOAT_DATA_EPSILON);
730         offset = OH_Drawing_TextLineGetOffsetForStringIndex(textLine, -2);
731         EXPECT_EQ(offset, 0.0);
732     }
733     OH_Drawing_DestroyTextLines(textLines);
734 }
735 
736 /*
737  * @tc.number: SUB_GRAPHIC_GRAPHIC_2D_TextLineTest_020
738  * @tc.name  : TextLineTest020
739  * @tc.desc  : test for the textLine GetOffsetForStringIndex
740  * @tc.size  : MediumTest
741  * @tc.type  : Function
742  * @tc.level : Level 0
743  */
744 HWTEST_F(NativeTextLineTest, TextLineTest020, Function | MediumTest | Level0)
745 {
746     PrepareCreateTextLine(
747         "Hello \t 中国 测 World \n !@#$%^&*~(){}[] 123 4567890 - = ,. < >、/ Drawing testlp 试 "
748         "Drawing \n\n   \u231A \u513B"
749         " \u00A9\uFE0F aaa clp11⌚��������‍����‍��‍��‍����مرحبا中国 测 World测试文本\n123");
750     OH_Drawing_Array* textLines = OH_Drawing_TypographyGetTextLines(typography_);
751     size_t size = OH_Drawing_GetDrawingArraySize(textLines);
752     EXPECT_EQ(size, NUM_7);
753 
754     const int maxCharacterNum = 88;
755     std::vector<float> offSetArr = {290.939697, 498.239380, 458.309509};
756     for (size_t index = 0; index < size; index++) {
757         OH_Drawing_TextLine* textLine = OH_Drawing_GetTextLineByIndex(textLines, index);
758         EXPECT_TRUE(textLine != nullptr);
759 
760         double offset = OH_Drawing_TextLineGetOffsetForStringIndex(textLine, 0);
761         EXPECT_EQ(offset, 0.0);
762         offset = OH_Drawing_TextLineGetOffsetForStringIndex(textLine, NUM_10);
763         if (index == 0) {
764             EXPECT_NEAR(offset, 155.129852, FLOAT_DATA_EPSILON);
765         } else {
766             EXPECT_EQ(offset, 0.0);
767         }
768         EXPECT_LE(offset, 500.0);
769         offset = OH_Drawing_TextLineGetOffsetForStringIndex(textLine, maxCharacterNum);
770         if (index <= NUM_2) {
771             EXPECT_NEAR(offset, offSetArr[index], FLOAT_DATA_EPSILON);
772         }  else {
773             EXPECT_EQ(offset, 0.0);
774         }
775         EXPECT_LE(offset, 500.0);
776         offset = OH_Drawing_TextLineGetOffsetForStringIndex(textLine, -2);
777         EXPECT_EQ(offset, 0.0);
778     }
779     OH_Drawing_DestroyTextLines(textLines);
780 }
781 
782 /*
783  * @tc.number: SUB_GRAPHIC_GRAPHIC_2D_TextLineTest_021
784  * @tc.name  : TextLineTest021
785  * @tc.desc  : test for the textLine EnumerateCaretOffsets
786  * @tc.size  : MediumTest
787  * @tc.type  : Function
788  * @tc.level : Level 0
789  */
790 HWTEST_F(NativeTextLineTest, TextLineTest021, Function | MediumTest | Level0)
791 {
792     PrepareCreateTextLine("H测��مرحب\n!@#$%^&*~(){}[] 123 4567890 - = ,. < >、/Drawing testlp 试 Drawing  ");
793     OH_Drawing_Array* textLines = OH_Drawing_TypographyGetTextLines(typography_);
794     size_t size = OH_Drawing_GetDrawingArraySize(textLines);
795     EXPECT_EQ(size, NUM_3);
796     OH_Drawing_TextLine* textLine1 = OH_Drawing_GetTextLineByIndex(textLines, 0);
__anoncd2112550202(double offset, int32_t index, bool leadingEdge) 797     OH_Drawing_TextLineEnumerateCaretOffsets(textLine1, [](double offset, int32_t index, bool leadingEdge) {
798         static int offsetNum = 0;
799         int32_t data1 = 51;
800         int32_t data2 = 50;
801         if (index == 0 && leadingEdge) {
802             EXPECT_NEAR(offset, 0.0, FLOAT_DATA_EPSILON);
803         } else if (index == NUM_1 && leadingEdge) {
804             EXPECT_NEAR(offset, 22.349976, FLOAT_DATA_EPSILON);
805         } else if (index == NUM_2 && leadingEdge) {
806             EXPECT_NEAR(offset, 52.349945, FLOAT_DATA_EPSILON);
807         } else {
808             EXPECT_LE(offset, 500.0);
809         }
810         if (offsetNum++ % NUM_2 == 0) {
811             EXPECT_TRUE(leadingEdge);
812         } else {
813             EXPECT_FALSE(leadingEdge);
814         }
815         EXPECT_LE(index, data1);
816         return index > data2;
817     });
818     OH_Drawing_DestroyTextLines(textLines);
819 }
820 
821 /*
822  * @tc.number: SUB_GRAPHIC_GRAPHIC_2D_TextLineTest_022
823  * @tc.name  : TextLineTest022
824  * @tc.desc  : test for the textLine EnumerateCaretOffsets
825  * @tc.size  : MediumTest
826  * @tc.type  : Function
827  * @tc.level : Level 0
828  */
829 HWTEST_F(NativeTextLineTest, TextLineTest022, Function | MediumTest | Level0)
830 {
831     PrepareCreateTextLine(
832         "Hello \t 中国 测 World \n !@#$%^&*~(){}[] 123 4567890 - = ,. < >、/ Drawing testlp 试 "
833         "Drawing \n\n   \u231A \u513B"
834         " \u00A9\uFE0F aaa clp11⌚��������‍����‍��‍��‍����مرحبا中国 测 World测试文本\n123");
835     OH_Drawing_Array* textLines = OH_Drawing_TypographyGetTextLines(typography_);
836     size_t size = OH_Drawing_GetDrawingArraySize(textLines);
837     EXPECT_EQ(size, NUM_7);
838 
839     for (size_t index = 0; index < size; index++) {
840         OH_Drawing_TextLine* textLine = OH_Drawing_GetTextLineByIndex(textLines, index);
841         EXPECT_TRUE(textLine != nullptr);
842 
__anoncd2112550302(double offset, int32_t index, bool leadingEdge) 843         OH_Drawing_TextLineEnumerateCaretOffsets(textLine, [](double offset, int32_t index, bool leadingEdge) {
844             EXPECT_GE(index, 0);
845             EXPECT_EQ(offset, 0.0);
846             EXPECT_TRUE(leadingEdge);
847             return true;
848         });
849     }
850     OH_Drawing_DestroyTextLines(textLines);
851 }
852 
853 /*
854  * @tc.number: SUB_GRAPHIC_GRAPHIC_2D_TextLineTest_023
855  * @tc.name  : TextLineTest023
856  * @tc.desc  : test for the textLine EnumerateCaretOffsets
857  * @tc.size  : MediumTest
858  * @tc.type  : Function
859  * @tc.level : Level 0
860  */
861 HWTEST_F(NativeTextLineTest, TextLineTest023, Function | MediumTest | Level0)
862 {
863     PrepareCreateTextLine("\n\n");
864     OH_Drawing_Array* textLines = OH_Drawing_TypographyGetTextLines(typography_);
865     size_t size = OH_Drawing_GetDrawingArraySize(textLines);
866     EXPECT_EQ(size, NUM_3);
867 
868     OH_Drawing_TextLine* textLine = textLine = OH_Drawing_GetTextLineByIndex(textLines, 0);
869     EXPECT_TRUE(textLine != nullptr);
870     OH_Drawing_TextLineEnumerateCaretOffsets(textLine, nullptr);
__anoncd2112550402(double offset, int32_t index, bool leadingEdge) 871     OH_Drawing_TextLineEnumerateCaretOffsets(nullptr, [](double offset, int32_t index, bool leadingEdge) {
872             return false;
873         });
874     OH_Drawing_TextLineEnumerateCaretOffsets(nullptr, nullptr);
875     OH_Drawing_DestroyTextLines(textLines);
876 }
877 
878 /*
879  * @tc.number: SUB_GRAPHIC_GRAPHIC_2D_TextLineTest_024
880  * @tc.name  : TextLineTest024
881  * @tc.desc  : test for the textLine GetAlignmentOffset
882  * @tc.size  : MediumTest
883  * @tc.type  : Function
884  * @tc.level : Level 0
885  */
886 HWTEST_F(NativeTextLineTest, TextLineTest024, Function | MediumTest | Level0)
887 {
888     PrepareCreateTextLine(
889         "Hello \t 中国 测 World \n !@#$%^&*~(){}[] 123 4567890 - = ,. < >、/ Drawing testlp 试 "
890         "Drawing \n\n   \u231A \u513B"
891         " \u00A9\uFE0F aaa clp11⌚��������‍����‍��‍��‍����مرحبا中国 测 World测试文本\n123");
892     OH_Drawing_Array* textLines = OH_Drawing_TypographyGetTextLines(typography_);
893     size_t size = OH_Drawing_GetDrawingArraySize(textLines);
894     EXPECT_EQ(size, NUM_7);
895 
896     std::vector<float> offSetArr = {208.580139, 104.930298, 124.895233, 350.000000, 101.023849, 145.251343, 324.349976};
897     for (size_t index = 0; index < size; index++) {
898         OH_Drawing_TextLine* textLine = OH_Drawing_GetTextLineByIndex(textLines, index);
899         EXPECT_TRUE(textLine != nullptr);
900 
901         double offset = OH_Drawing_TextLineGetAlignmentOffset(textLine, 0.0, NUM_600);
902         EXPECT_EQ(offset, 0.0);
903         offset = OH_Drawing_TextLineGetAlignmentOffset(textLine, 0.5, NUM_700);
904         EXPECT_NEAR(offset, offSetArr[index], FLOAT_DATA_EPSILON);
905         offset = OH_Drawing_TextLineGetAlignmentOffset(textLine, -1.0, NUM_700);
906         EXPECT_EQ(offset, 0.0);
907         offset = OH_Drawing_TextLineGetAlignmentOffset(textLine, 2.0, NUM_20);
908         if (index == NUM_3) {
909             EXPECT_EQ(offset, 20.0);
910         } else {
911             EXPECT_EQ(offset, 0.0);
912         }
913     }
914     OH_Drawing_DestroyTextLines(textLines);
915 }
916 
917 /*
918  * @tc.number: SUB_GRAPHIC_GRAPHIC_2D_TextLineTest_025
919  * @tc.name  : TextLineTest025
920  * @tc.desc  : test for the textLine GetAlignmentOffset
921  * @tc.size  : MediumTest
922  * @tc.type  : Function
923  * @tc.level : Level 0
924  */
925 HWTEST_F(NativeTextLineTest, TextLineTest025, Function | MediumTest | Level0)
926 {
927     PrepareCreateTextLine("\n\n\n\n");
928     OH_Drawing_Array* textLines = OH_Drawing_TypographyGetTextLines(typography_);
929     size_t size = OH_Drawing_GetDrawingArraySize(textLines);
930     EXPECT_EQ(size, NUM_5);
931 
932     for (size_t index = 0; index < size; index++) {
933         OH_Drawing_TextLine* textLine = OH_Drawing_GetTextLineByIndex(textLines, index);
934         EXPECT_TRUE(textLine != nullptr);
935 
936         double offset = OH_Drawing_TextLineGetAlignmentOffset(textLine, 0.0, NUM_600);
937         EXPECT_EQ(offset, 0.0);
938         offset = OH_Drawing_TextLineGetAlignmentOffset(textLine, 0.5, NUM_700);
939         EXPECT_EQ(offset, 350.0);
940         offset = OH_Drawing_TextLineGetAlignmentOffset(textLine, -1.0, -700);
941         EXPECT_EQ(offset, 0.0);
942         offset = OH_Drawing_TextLineGetAlignmentOffset(textLine, 2.0, NUM_20);
943         EXPECT_EQ(offset, 20.0);
944     }
945     OH_Drawing_DestroyTextLines(textLines);
946 
947     double offset = OH_Drawing_TextLineGetAlignmentOffset(nullptr, 0.0, 0.0);
948     EXPECT_EQ(offset, 0.0);
949 }
950 }