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
16 #include <securec.h>
17
18 #include "drawing_font_collection.h"
19 #include "drawing_rect.h"
20 #include "drawing_text_line.h"
21 #include "drawing_text_run.h"
22 #include "drawing_text_typography.h"
23 #include "gtest/gtest.h"
24
25 using namespace testing;
26 using namespace testing::ext;
27 namespace OHOS {
28 namespace {
29 constexpr static float FLOAT_DATA_EPSILON = 1e-6f;
30 const double DEFAULT_FONT_SIZE = 40;
31 const double DEFAULT_LAYOUT_WIDTH = 1000;
32 } // namespace
33 class NdkTypographyStyleTest : public testing::Test {
34 public:
35 void TearDown() override;
36 void PrepareWorkForTypographyStyleTest();
37 void PrepareWorkForAutoSpaceTest(std::string& text, double layoutWidth);
38
39 protected:
40 OH_Drawing_FontCollection* fontCollection_{nullptr};
41 OH_Drawing_TextStyle* txtStyle_{nullptr};
42 OH_Drawing_TypographyCreate* handler_{nullptr};
43 OH_Drawing_Typography* typography_{nullptr};
44 OH_Drawing_TypographyStyle* typoStyle_{nullptr};
45 OH_Drawing_TypographyCreate* handler2_{nullptr};
46 OH_Drawing_Typography* typography2_{nullptr};
47 OH_Drawing_TypographyStyle* typoStyle2_{nullptr};
48 };
49
PrepareWorkForTypographyStyleTest()50 void NdkTypographyStyleTest::PrepareWorkForTypographyStyleTest()
51 {
52 txtStyle_ = OH_Drawing_CreateTextStyle();
53 ASSERT_NE(txtStyle_, nullptr);
54 fontCollection_ = OH_Drawing_CreateSharedFontCollection();
55 ASSERT_NE(fontCollection_, nullptr);
56 OH_Drawing_SetTextStyleFontSize(txtStyle_, DEFAULT_FONT_SIZE);
57 }
PrepareWorkForAutoSpaceTest(std::string & text,double layoutWidth)58 void NdkTypographyStyleTest::PrepareWorkForAutoSpaceTest(std::string& text, double layoutWidth)
59 {
60 PrepareWorkForTypographyStyleTest();
61 // paragraph1 with autospace
62 typoStyle_ = OH_Drawing_CreateTypographyStyle();
63 ASSERT_NE(typoStyle_, nullptr);
64 OH_Drawing_SetTypographyTextAutoSpace(typoStyle_, true);
65 handler_ = OH_Drawing_CreateTypographyHandler(typoStyle_, fontCollection_);
66 ASSERT_NE(handler_, nullptr);
67 OH_Drawing_TypographyHandlerPushTextStyle(handler_, txtStyle_);
68 OH_Drawing_TypographyHandlerAddText(handler_, text.c_str());
69 typography_ = OH_Drawing_CreateTypography(handler_);
70 ASSERT_NE(typography_, nullptr);
71 OH_Drawing_TypographyLayout(typography_, layoutWidth);
72
73 // paragraph2 without autospace
74 typoStyle2_ = OH_Drawing_CreateTypographyStyle();
75 ASSERT_NE(typoStyle2_, nullptr);
76 OH_Drawing_SetTypographyTextAutoSpace(typoStyle2_, false);
77 handler2_ = OH_Drawing_CreateTypographyHandler(typoStyle2_, fontCollection_);
78 ASSERT_NE(handler2_, nullptr);
79 OH_Drawing_TypographyHandlerPushTextStyle(handler2_, txtStyle_);
80 OH_Drawing_TypographyHandlerAddText(handler2_, text.c_str());
81 typography2_ = OH_Drawing_CreateTypography(handler2_);
82 ASSERT_NE(typography2_, nullptr);
83 OH_Drawing_TypographyLayout(typography2_, DEFAULT_LAYOUT_WIDTH);
84 }
85
TearDown()86 void NdkTypographyStyleTest::TearDown()
87 {
88 if (fontCollection_ != nullptr) {
89 OH_Drawing_DestroyFontCollection(fontCollection_);
90 fontCollection_ = nullptr;
91 }
92 if (txtStyle_ != nullptr) {
93 OH_Drawing_DestroyTextStyle(txtStyle_);
94 txtStyle_ = nullptr;
95 }
96 if (handler_ != nullptr) {
97 OH_Drawing_DestroyTypographyHandler(handler_);
98 handler_ = nullptr;
99 }
100 if (typography_ != nullptr) {
101 OH_Drawing_DestroyTypography(typography_);
102 typography_ = nullptr;
103 }
104 if (typoStyle_ != nullptr) {
105 OH_Drawing_DestroyTypographyStyle(typoStyle_);
106 typoStyle_ = nullptr;
107 }
108 if (handler2_ != nullptr) {
109 OH_Drawing_DestroyTypographyHandler(handler2_);
110 handler2_ = nullptr;
111 }
112 if (typography2_ != nullptr) {
113 OH_Drawing_DestroyTypography(typography2_);
114 typography2_ = nullptr;
115 }
116 if (typoStyle2_ != nullptr) {
117 OH_Drawing_DestroyTypographyStyle(typoStyle2_);
118 typoStyle2_ = nullptr;
119 }
120 }
121
122 /*
123 * @tc.name: ParagraphTestGlyphPositionAtCoordinateWithCluster001
124 * @tc.desc: test for GlyphPositionAtCoordinate with dash
125 * @tc.type: FUNC
126 */
127 HWTEST_F(NdkTypographyStyleTest, ParagraphTestGlyphPositionAtCoordinateWithCluster001, TestSize.Level0)
128 {
129 std::string text3 = "————————";
130
131 double maxWidth3 = 1000.0;
132 OH_Drawing_TypographyStyle* typoStyle = OH_Drawing_CreateTypographyStyle();
133 OH_Drawing_TextStyle* txtStyle = OH_Drawing_CreateTextStyle();
134 OH_Drawing_FontCollection* fontCollection = OH_Drawing_GetFontCollectionGlobalInstance();
135
136 OH_Drawing_SetTextStyleFontSize(txtStyle, 30);
137 OH_Drawing_SetTextStyleColor(txtStyle, OH_Drawing_ColorSetArgb(0xFF, 0x00, 0x00, 0x00));
138 OH_Drawing_TypographyCreate* handler = OH_Drawing_CreateTypographyHandler(typoStyle, fontCollection);
139 OH_Drawing_TypographyHandlerPushTextStyle(handler, txtStyle);
140
141 OH_Drawing_TypographyHandlerAddText(handler, text3.c_str());
142 OH_Drawing_Typography* typography = OH_Drawing_CreateTypography(handler);
143 OH_Drawing_TypographyLayout(typography, maxWidth3);
144
145 float xCoords[] = { 0, 20, 30, 45, 60, 75, 100 };
146 int expectedPositions[] = { 0, 1, 1, 2, 2, 3, 3 };
147 int expectedAffinities[] = { 1, 0, 1, 0, 1, 0, 1 };
148 OH_Drawing_PositionAndAffinity* results[7];
149 for (int i = 0; i < 7; i++) {
150 results[i] = OH_Drawing_TypographyGetGlyphPositionAtCoordinateWithCluster(typography, xCoords[i], 0);
151 EXPECT_EQ(OH_Drawing_GetPositionFromPositionAndAffinity(results[i]), expectedPositions[i]);
152 EXPECT_EQ(OH_Drawing_GetAffinityFromPositionAndAffinity(results[i]), expectedAffinities[i]);
153 }
154
155 OH_Drawing_DestroyTypography(typography);
156 OH_Drawing_DestroyTypographyStyle(typoStyle);
157 OH_Drawing_DestroyTypographyHandler(handler);
158 OH_Drawing_DestroyTextStyle(txtStyle);
159 for (int i = 0; i < 7; i++) {
160 free(results[i]);
161 }
162 }
163
164 /*
165 * @tc.name: SetTypographyTextAutoSpaceTest001
166 * @tc.desc: test for set auto space when paragraph with single run
167 * @tc.type: FUNC
168 */
169 HWTEST_F(NdkTypographyStyleTest, SetTypographyTextAutoSpaceTest001, TestSize.Level0)
170 {
171 // Prepare Paragraph 1 and Paragraph 2, and turn on autospace and turn off autospace respectively.
172 std::string text = "SingRun©2002-2001";
173 PrepareWorkForAutoSpaceTest(text, DEFAULT_LAYOUT_WIDTH);
174
175 // paragraph1
176 double longestLineTrue = OH_Drawing_TypographyGetLongestLine(typography_);
177 double line1True = OH_Drawing_TypographyGetLineWidth(typography_, 0);
178 EXPECT_NEAR(longestLineTrue, line1True, FLOAT_DATA_EPSILON);
179
180 // paragraph2
181 double longestLineFalse = OH_Drawing_TypographyGetLongestLine(typography2_);
182 double line1False = OH_Drawing_TypographyGetLineWidth(typography2_, 0);
183 EXPECT_NEAR(longestLineFalse, line1False, FLOAT_DATA_EPSILON);
184
185 // compare paragraph1 and paragraph2
186 EXPECT_NEAR(longestLineTrue, longestLineFalse + DEFAULT_FONT_SIZE / 8 * 2, FLOAT_DATA_EPSILON);
187 EXPECT_NEAR(line1True, line1False + DEFAULT_FONT_SIZE / 8 * 2, FLOAT_DATA_EPSILON);
188 }
189
190 /*
191 * @tc.name: SetTypographyTextAutoSpaceTest002
192 * @tc.desc: test for set auto space when paragraph with single run and the layout width is at the boundary value.
193 * @tc.type: FUNC
194 */
195 HWTEST_F(NdkTypographyStyleTest, SetTypographyTextAutoSpaceTest002, TestSize.Level0)
196 {
197 // test boundary value:Use longestline without autospace as layout width when autospace enabled, line count + 1
198 // Prepare Paragraph 1 and Paragraph 2, and turn on autospace and turn off autospace respectively.
199 std::string text = "SingRun©2002-2001";
200 double layoutWidth = 388.319641; // boundary value
201 PrepareWorkForAutoSpaceTest(text, layoutWidth);
202
203 // paragraph1
204 double longestLineTrue = OH_Drawing_TypographyGetLongestLine(typography_);
205 double line1True = OH_Drawing_TypographyGetLineWidth(typography_, 0);
206 double line2True = OH_Drawing_TypographyGetLineWidth(typography_, 1);
207 size_t lineCount = OH_Drawing_TypographyGetLineCount(typography_);
208 EXPECT_NEAR(longestLineTrue, std::max(line1True, line2True), FLOAT_DATA_EPSILON);
209 EXPECT_GT(layoutWidth, longestLineTrue);
210 EXPECT_GT(layoutWidth, line1True);
211 EXPECT_GT(line2True, 0);
212
213 // paragraph2
214 double longestLineFalse2 = OH_Drawing_TypographyGetLongestLine(typography2_);
215 double line1False2 = OH_Drawing_TypographyGetLineWidth(typography2_, 0);
216 size_t lineCount2 = OH_Drawing_TypographyGetLineCount(typography2_);
217 EXPECT_NEAR(longestLineFalse2, line1False2, FLOAT_DATA_EPSILON);
218 EXPECT_NEAR(longestLineFalse2, layoutWidth, FLOAT_DATA_EPSILON);
219 EXPECT_NEAR(line1False2, layoutWidth, FLOAT_DATA_EPSILON);
220
221 // compare paragraph1 and paragraph2
222 EXPECT_GT(longestLineFalse2, longestLineTrue);
223 EXPECT_GT(line1False2, line1True);
224 EXPECT_EQ(lineCount, lineCount2 + 1);
225 }
226
227 /*
228 * @tc.name: SetTypographyTextAutoSpaceTest003
229 * @tc.desc: test for set auto space when paragraph with many lines
230 * @tc.type: FUNC
231 */
232 HWTEST_F(NdkTypographyStyleTest, SetTypographyTextAutoSpaceTest003, TestSize.Level0)
233 {
234 // Prepare Paragraph 1 and Paragraph 2, and turn on autospace and turn off autospace respectively.
235 std::string text = "嫌疑者X的牺牲\n版权所有©2002-2001华为技术有限公司保留一切权利\n卸载USB设备";
236 PrepareWorkForAutoSpaceTest(text, DEFAULT_LAYOUT_WIDTH);
237
238 // paragraph1
239 double longestLineTrue = OH_Drawing_TypographyGetLongestLine(typography_);
240 double line1True = OH_Drawing_TypographyGetLineWidth(typography_, 0);
241 double line2True = OH_Drawing_TypographyGetLineWidth(typography_, 1);
242 double line3True = OH_Drawing_TypographyGetLineWidth(typography_, 2);
243 EXPECT_NEAR(longestLineTrue, std::max(line1True, std::max(line2True, line3True)), FLOAT_DATA_EPSILON);
244
245 // paragraph2
246 double longestLineFalse = OH_Drawing_TypographyGetLongestLine(typography2_);
247 double line1False = OH_Drawing_TypographyGetLineWidth(typography2_, 0);
248 double line2False = OH_Drawing_TypographyGetLineWidth(typography2_, 1);
249 double line3False = OH_Drawing_TypographyGetLineWidth(typography2_, 2);
250 EXPECT_NEAR(longestLineFalse, std::max(line1False, std::max(line2False, line3False)), FLOAT_DATA_EPSILON);
251
252 // compare paragraph1 and paragraph2
253 EXPECT_NEAR(longestLineTrue, longestLineFalse + DEFAULT_FONT_SIZE / 8 * 3, FLOAT_DATA_EPSILON);
254 EXPECT_NEAR(line1True, line1False + DEFAULT_FONT_SIZE / 8 * 2, FLOAT_DATA_EPSILON);
255 EXPECT_NEAR(line2True, line2False + DEFAULT_FONT_SIZE / 8 * 3, FLOAT_DATA_EPSILON);
256 EXPECT_NEAR(line3True, line3False + DEFAULT_FONT_SIZE / 8 * 2, FLOAT_DATA_EPSILON);
257 }
258
259 /*
260 * @tc.name: SetTypographyTextAutoSpaceTest004
261 * @tc.desc: test for set auto space when paragraph is many lines and the layout width is at the boundary value.
262 * @tc.type: FUNC
263 */
264 HWTEST_F(NdkTypographyStyleTest, SetTypographyTextAutoSpaceTest004, TestSize.Level0)
265 {
266 // test boundary value:Use longestline without autospace as layout width when autospace enabled, line count + 1
267 // Prepare Paragraph 1 and Paragraph 2, and turn on autospace and turn off autospace respectively.
268 std::string text = "嫌疑者X的牺牲\n版权所有©2002-2001华为技术有限公司保留一切权利\n卸载USB设备";
269 double layoutWidth = 956.159546; // boundary value
270 PrepareWorkForAutoSpaceTest(text, layoutWidth);
271
272 // paragraph1
273 double longestLineTrue = OH_Drawing_TypographyGetLongestLine(typography_);
274 double line1True = OH_Drawing_TypographyGetLineWidth(typography_, 0);
275 double line2True = OH_Drawing_TypographyGetLineWidth(typography_, 1);
276 double line3True = OH_Drawing_TypographyGetLineWidth(typography_, 2);
277 double line4True = OH_Drawing_TypographyGetLineWidth(typography_, 3);
278 size_t lineCount = OH_Drawing_TypographyGetLineCount(typography_);
279 EXPECT_GT(layoutWidth, longestLineTrue);
280 EXPECT_GT(layoutWidth, line1True);
281 EXPECT_GT(line3True, 0);
282
283 // paragraph2
284 double longestLineFalse2 = OH_Drawing_TypographyGetLongestLine(typography2_);
285 double line1False2 = OH_Drawing_TypographyGetLineWidth(typography2_, 0);
286 double line2False2 = OH_Drawing_TypographyGetLineWidth(typography2_, 1);
287 double line3False2 = OH_Drawing_TypographyGetLineWidth(typography2_, 2);
288 size_t lineCount2 = OH_Drawing_TypographyGetLineCount(typography2_);
289 EXPECT_NEAR(longestLineFalse2, layoutWidth, FLOAT_DATA_EPSILON);
290 EXPECT_NEAR(line2False2, layoutWidth, FLOAT_DATA_EPSILON);
291
292 // compare paragraph1 and paragraph2
293 EXPECT_GT(longestLineFalse2, longestLineTrue);
294 EXPECT_GT(line1True, line1False2);
295 // The critical width value squeezed the second line down, so the characters are fewer, and the width is shorter.
296 EXPECT_GT(line2False2, line2True);
297 EXPECT_GT(line4True, line3False2);
298 EXPECT_EQ(lineCount, lineCount2 + 1);
299 }
300
301 /*
302 * @tc.name: SetTypographyTextAutoSpaceTest005
303 * @tc.desc: test for width from OH_Drawing_TextLineGetImageBounds when set auto space
304 * @tc.type: FUNC
305 */
306 HWTEST_F(NdkTypographyStyleTest, SetTypographyTextAutoSpaceTest005, TestSize.Level0)
307 {
308 // Prepare Paragraph 1 and Paragraph 2, and turn on autospace and turn off autospace respectively.
309 std::string text = "嫌疑者X的牺牲";
310 PrepareWorkForAutoSpaceTest(text, DEFAULT_LAYOUT_WIDTH);
311 OH_Drawing_Array* textLines = OH_Drawing_TypographyGetTextLines(typography_);
312 ASSERT_NE(textLines, nullptr);
313 OH_Drawing_Array* textLines2 = OH_Drawing_TypographyGetTextLines(typography2_);
314 ASSERT_NE(textLines2, nullptr);
315 OH_Drawing_TextLine* textLine = OH_Drawing_GetTextLineByIndex(textLines, 0);
316 ASSERT_NE(textLine, nullptr);
317 OH_Drawing_TextLine* textLine2 = OH_Drawing_GetTextLineByIndex(textLines2, 0);
318 ASSERT_NE(textLine2, nullptr);
319
320 // compare paragraph1 and paragraph2
321 OH_Drawing_Rect* rect = nullptr;
322 OH_Drawing_Rect* rect2 = nullptr;
323
324 rect = OH_Drawing_TextLineGetImageBounds(textLine);
325 rect2 = OH_Drawing_TextLineGetImageBounds(textLine2);
326 EXPECT_NEAR(OH_Drawing_RectGetLeft(rect), OH_Drawing_RectGetLeft(rect2), FLOAT_DATA_EPSILON);
327 EXPECT_NEAR(
328 OH_Drawing_RectGetRight(rect), OH_Drawing_RectGetRight(rect2) + DEFAULT_FONT_SIZE / 8 * 2, FLOAT_DATA_EPSILON);
329
330 OH_Drawing_DestroyTextLines(textLines);
331 OH_Drawing_DestroyTextLines(textLines2);
332 OH_Drawing_DestroyRunImageBounds(rect);
333 OH_Drawing_DestroyRunImageBounds(rect2);
334 }
335
336 /*
337 * @tc.name: SetTypographyTextAutoSpaceTest006
338 * @tc.desc: test for width from OH_Drawing_TextLineGetOffsetForStringIndex when set auto space
339 * @tc.type: FUNC
340 */
341 HWTEST_F(NdkTypographyStyleTest, SetTypographyTextAutoSpaceTest006, TestSize.Level0)
342 {
343 // Prepare Paragraph 1 and Paragraph 2, and turn on autospace and turn off autospace respectively.
344 std::string text = "嫌疑者X的牺牲";
345 PrepareWorkForAutoSpaceTest(text, DEFAULT_LAYOUT_WIDTH);
346 OH_Drawing_Array* textLines = OH_Drawing_TypographyGetTextLines(typography_);
347 ASSERT_NE(textLines, nullptr);
348 OH_Drawing_Array* textLines2 = OH_Drawing_TypographyGetTextLines(typography2_);
349 ASSERT_NE(textLines2, nullptr);
350 OH_Drawing_TextLine* textLine = OH_Drawing_GetTextLineByIndex(textLines, 0);
351 ASSERT_NE(textLine, nullptr);
352 OH_Drawing_TextLine* textLine2 = OH_Drawing_GetTextLineByIndex(textLines2, 0);
353 ASSERT_NE(textLine2, nullptr);
354
355 // compare paragraph1 and paragraph2
356 double textLineWidth3 = OH_Drawing_TextLineGetOffsetForStringIndex(textLine, 4) -
357 OH_Drawing_TextLineGetOffsetForStringIndex(textLine, 3);
358 double textLineWidth4 = OH_Drawing_TextLineGetOffsetForStringIndex(textLine, 5) -
359 OH_Drawing_TextLineGetOffsetForStringIndex(textLine, 4);
360 double textLine2Width3 = OH_Drawing_TextLineGetOffsetForStringIndex(textLine2, 4) -
361 OH_Drawing_TextLineGetOffsetForStringIndex(textLine2, 3);
362 double textLine2Width4 = OH_Drawing_TextLineGetOffsetForStringIndex(textLine2, 5) -
363 OH_Drawing_TextLineGetOffsetForStringIndex(textLine2, 4);
364 EXPECT_NEAR(textLineWidth3, textLine2Width3 + DEFAULT_FONT_SIZE / 8, FLOAT_DATA_EPSILON);
365 EXPECT_NEAR(textLineWidth4, textLine2Width4 + DEFAULT_FONT_SIZE / 8, FLOAT_DATA_EPSILON);
366
367 OH_Drawing_DestroyTextLines(textLines);
368 OH_Drawing_DestroyTextLines(textLines2);
369 }
370
371 /*
372 * @tc.name: SetTypographyTextAutoSpaceTest007
373 * @tc.desc: test for width from OH_Drawing_GetRunTypographicBounds when set auto space
374 * @tc.type: FUNC
375 */
376 HWTEST_F(NdkTypographyStyleTest, SetTypographyTextAutoSpaceTest007, TestSize.Level0)
377 {
378 // Prepare Paragraph 1 and Paragraph 2, and turn on autospace and turn off autospace respectively.
379 std::string text = "嫌疑者X的牺牲";
380 PrepareWorkForAutoSpaceTest(text, DEFAULT_LAYOUT_WIDTH);
381
382 // paragraph1
383 OH_Drawing_Array* textLines = OH_Drawing_TypographyGetTextLines(typography_);
384 ASSERT_NE(textLines, nullptr);
385 size_t size = OH_Drawing_GetDrawingArraySize(textLines);
386 ASSERT_GT(size, 0);
387 OH_Drawing_TextLine* textLine = OH_Drawing_GetTextLineByIndex(textLines, 0);
388 ASSERT_NE(textLine, nullptr);
389 OH_Drawing_Array* runs = OH_Drawing_TextLineGetGlyphRuns(textLine);
390 ASSERT_NE(runs, nullptr);
391 size_t runsSize = OH_Drawing_GetDrawingArraySize(runs);
392 ASSERT_GT(runsSize, 0);
393
394 // paragraph2
395 OH_Drawing_Array* textLines2 = OH_Drawing_TypographyGetTextLines(typography2_);
396 ASSERT_NE(textLines2, nullptr);
397 size_t size2 = OH_Drawing_GetDrawingArraySize(textLines2);
398 ASSERT_GT(size2, 0);
399 OH_Drawing_TextLine* textLine2 = OH_Drawing_GetTextLineByIndex(textLines2, 0);
400 ASSERT_NE(textLine2, nullptr);
401 OH_Drawing_Array* runs2 = OH_Drawing_TextLineGetGlyphRuns(textLine2);
402 ASSERT_NE(runs2, nullptr);
403 size_t runsSize2 = OH_Drawing_GetDrawingArraySize(runs2);
404 ASSERT_GT(runsSize2, 0);
405
406 // compare paragraph1 and paragraph2
407 std::vector<float> widthAddArr = { 0, DEFAULT_FONT_SIZE / 8, DEFAULT_FONT_SIZE / 8 };
408 for (int i = 0; i < runsSize; i++) {
409 OH_Drawing_Run* run = OH_Drawing_GetRunByIndex(runs, i);
410 OH_Drawing_Run* run2 = OH_Drawing_GetRunByIndex(runs2, i);
411 ASSERT_NE(run, nullptr);
412 ASSERT_NE(run2, nullptr);
413 float ascent = 0.0;
414 float descent = 0.0;
415 float leading = 0.0;
416 float width = OH_Drawing_GetRunTypographicBounds(run, &ascent, &descent, &leading);
417 float width2 = OH_Drawing_GetRunTypographicBounds(run2, &ascent, &descent, &leading);
418 EXPECT_NEAR(width, width2 + widthAddArr[i], FLOAT_DATA_EPSILON);
419 }
420 OH_Drawing_DestroyRuns(runs);
421 OH_Drawing_DestroyTextLines(textLines);
422 OH_Drawing_DestroyRuns(runs2);
423 OH_Drawing_DestroyTextLines(textLines2);
424 }
425
426 /*
427 * @tc.name: SetTypographyTextAutoSpaceTest008
428 * @tc.desc: test for width from OH_Drawing_GetRunImageBounds when set auto space
429 * @tc.type: FUNC
430 */
431 HWTEST_F(NdkTypographyStyleTest, SetTypographyTextAutoSpaceTest008, TestSize.Level0)
432 {
433 // Prepare Paragraph 1 and Paragraph 2, and turn on autospace and turn off autospace respectively.
434 std::string text = "嫌疑者X的牺牲";
435 PrepareWorkForAutoSpaceTest(text, DEFAULT_LAYOUT_WIDTH);
436
437 // paragraph1
438 OH_Drawing_Array* textLines = OH_Drawing_TypographyGetTextLines(typography_);
439 ASSERT_NE(textLines, nullptr);
440 size_t size = OH_Drawing_GetDrawingArraySize(textLines);
441 ASSERT_GT(size, 0);
442 OH_Drawing_TextLine* textLine = OH_Drawing_GetTextLineByIndex(textLines, 0);
443 ASSERT_NE(textLine, nullptr);
444 OH_Drawing_Array* runs = OH_Drawing_TextLineGetGlyphRuns(textLine);
445 ASSERT_NE(runs, nullptr);
446 size_t runsSize = OH_Drawing_GetDrawingArraySize(runs);
447 ASSERT_GT(runsSize, 0);
448
449 // paragraph2
450 OH_Drawing_Array* textLines2 = OH_Drawing_TypographyGetTextLines(typography2_);
451 ASSERT_NE(textLines2, nullptr);
452 size_t size2 = OH_Drawing_GetDrawingArraySize(textLines2);
453 ASSERT_GT(size2, 0);
454 OH_Drawing_TextLine* textLine2 = OH_Drawing_GetTextLineByIndex(textLines2, 0);
455 ASSERT_NE(textLine2, nullptr);
456 OH_Drawing_Array* runs2 = OH_Drawing_TextLineGetGlyphRuns(textLine2);
457 ASSERT_NE(runs2, nullptr);
458 size_t runsSize2 = OH_Drawing_GetDrawingArraySize(runs2);
459 ASSERT_GT(runsSize2, 0);
460
461 // compare paragraph1 and paragraph2
462 std::vector<float> leftAddArr = { 0, 0, DEFAULT_FONT_SIZE / 8 };
463 std::vector<float> rightAddArr = { 0, DEFAULT_FONT_SIZE / 8, DEFAULT_FONT_SIZE / 8 * 2 };
464 for (int i = 0; i < runsSize; i++) {
465 OH_Drawing_Run* run = OH_Drawing_GetRunByIndex(runs, i);
466 OH_Drawing_Run* run2 = OH_Drawing_GetRunByIndex(runs2, i);
467 ASSERT_NE(run, nullptr);
468 ASSERT_NE(run2, nullptr);
469 OH_Drawing_Rect* rect = OH_Drawing_GetRunImageBounds(run);
470 OH_Drawing_Rect* rect2 = OH_Drawing_GetRunImageBounds(run2);
471 // It will accumulate as the number of autospaces needs to be increased.
472 EXPECT_NEAR(OH_Drawing_RectGetLeft(rect), OH_Drawing_RectGetLeft(rect2) + leftAddArr[i], FLOAT_DATA_EPSILON);
473 EXPECT_NEAR(OH_Drawing_RectGetRight(rect), OH_Drawing_RectGetRight(rect2) + rightAddArr[i], FLOAT_DATA_EPSILON);
474 OH_Drawing_DestroyRunImageBounds(rect);
475 OH_Drawing_DestroyRunImageBounds(rect2);
476 }
477 OH_Drawing_DestroyRuns(runs);
478 OH_Drawing_DestroyTextLines(textLines);
479 OH_Drawing_DestroyRuns(runs2);
480 OH_Drawing_DestroyTextLines(textLines2);
481 }
482 } // namespace OHOS