1/*
2 * Copyright (c) 2022 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 "gtest/gtest.h"
17#include "drawing_bitmap.h"
18#include "drawing_brush.h"
19#include "drawing_canvas.h"
20#include "drawing_color.h"
21#include "drawing_font.h"
22#include "drawing_font_collection.h"
23#include "drawing_path.h"
24#include "drawing_pen.h"
25#include "drawing_text_declaration.h"
26#include "drawing_text_typography.h"
27#ifndef USE_GRAPHIC_TEXT_GINE
28#include "rosen_text/ui/typography.h"
29#include "rosen_text/ui/typography_create.h"
30#else
31#include "rosen_text/typography.h"
32#include "rosen_text/typography_create.h"
33#endif
34
35#include <string>
36#include <fstream>
37
38#ifndef USE_GRAPHIC_TEXT_GINE
39using namespace rosen;
40#else
41using namespace OHOS::Rosen;
42#endif
43using namespace testing;
44using namespace testing::ext;
45
46namespace OHOS {
47class OHDrawingTypographyLargeValueTest : public testing::Test {
48};
49
50/*
51 * @tc.name: OHDrawingTypographyLargeValueTest016
52 * @tc.desc: test for typography
53 * @tc.type: FUNC
54 */
55HWTEST_F(OHDrawingTypographyLargeValueTest, OHDrawingTypographyLargeValueTest016, TestSize.Level1)
56{
57    OH_Drawing_TypographyStyle* typoStyle = OH_Drawing_CreateTypographyStyle();
58    OH_Drawing_TextStyle* txtStyle = OH_Drawing_CreateTextStyle();
59    OH_Drawing_TypographyCreate* handler = OH_Drawing_CreateTypographyHandler(typoStyle,
60        OH_Drawing_CreateFontCollection());
61    EXPECT_TRUE(handler != nullptr);
62
63    OH_Drawing_SetTextStyleColor(txtStyle, OH_Drawing_ColorSetArgb(0xFF, 0x00, 0x00, 0x00));
64    double fontSize = 30;
65    OH_Drawing_SetTextStyleFontSize(txtStyle, fontSize);
66    OH_Drawing_SetTextStyleFontWeight(txtStyle, FONT_WEIGHT_400);
67    OH_Drawing_SetTextStyleBaseLine(txtStyle, TEXT_BASELINE_ALPHABETIC);
68    const char* fontFamilies[] = {"Roboto"};
69    OH_Drawing_SetTextStyleFontFamilies(txtStyle, 1, fontFamilies);
70    OH_Drawing_TypographyHandlerPushTextStyle(handler, txtStyle);
71
72    const char* text = "OpenHarmony\n";
73    OH_Drawing_TypographyHandlerAddText(handler, text);
74    OH_Drawing_TypographyHandlerPopTextStyle(handler);
75
76    OH_Drawing_Typography* typography = OH_Drawing_CreateTypography(handler);
77    const float indents[] = {1.2, 3.4};
78    OH_Drawing_TypographySetIndents(typography, 2, indents);
79    float indent = 3.4;
80    EXPECT_EQ(indent, OH_Drawing_TypographyGetIndentsWithIndex(typography, 1));
81    double maxWidth = 2160.0;
82    OH_Drawing_TypographyLayout(typography, maxWidth);
83    EXPECT_EQ(maxWidth, OH_Drawing_TypographyGetMaxWidth(typography));
84    double position[2] = {10.0, 15.0};
85    OH_Drawing_Bitmap* cBitmap = OH_Drawing_BitmapCreate();
86    OH_Drawing_BitmapFormat cFormat {COLOR_FORMAT_RGBA_8888, ALPHA_FORMAT_OPAQUE};
87    uint32_t width = 20;
88    uint32_t height = 40;
89    OH_Drawing_BitmapBuild(cBitmap, width, height, &cFormat);
90    EXPECT_EQ(width, OH_Drawing_BitmapGetWidth(cBitmap));
91    EXPECT_EQ(height, OH_Drawing_BitmapGetHeight(cBitmap));
92
93    OH_Drawing_Canvas* cCanvas = OH_Drawing_CanvasCreate();
94    OH_Drawing_CanvasBind(cCanvas, cBitmap);
95    OH_Drawing_CanvasClear(cCanvas, OH_Drawing_ColorSetArgb(0xFF, 0xFF, 0xFF, 0xFF));
96
97    EXPECT_EQ(OH_Drawing_TypographyGetHeight(typography) != 0.0, true);
98    EXPECT_EQ(OH_Drawing_TypographyGetLongestLine(typography) != 0.0, true);
99    EXPECT_EQ(OH_Drawing_TypographyGetMinIntrinsicWidth(typography) <=
100        OH_Drawing_TypographyGetMaxIntrinsicWidth(typography), true);
101    EXPECT_EQ(OH_Drawing_TypographyGetAlphabeticBaseline(typography) != 0.0, true);
102    EXPECT_EQ(OH_Drawing_TypographyGetIdeographicBaseline(typography) != 0.0, true);
103    OH_Drawing_TypographyPaint(typography, cCanvas, position[0], position[1]);
104    OH_Drawing_DestroyTypography(typography);
105    OH_Drawing_DestroyTypographyHandler(handler);
106}
107
108/*
109 * @tc.name: OHDrawingTypographyLargeValueTest026
110 * @tc.desc: test for typography and txtStyle
111 * @tc.type: FUNC
112 */
113HWTEST_F(OHDrawingTypographyLargeValueTest, OHDrawingTypographyLargeValueTest026, TestSize.Level1)
114{
115    OH_Drawing_TypographyStyle* typoStyle = OH_Drawing_CreateTypographyStyle();
116    OH_Drawing_TextStyle* txtStyle = OH_Drawing_CreateTextStyle();
117    OH_Drawing_TypographyCreate* handler = OH_Drawing_CreateTypographyHandler(typoStyle,
118        OH_Drawing_CreateFontCollection());
119    EXPECT_TRUE(handler != nullptr);
120    OH_Drawing_SetTextStyleColor(txtStyle, OH_Drawing_ColorSetArgb(0xFF, 0x00, 0x00, 0x00));
121    double fontSize = 30;
122    OH_Drawing_SetTextStyleFontSize(txtStyle, fontSize);
123    OH_Drawing_SetTextStyleFontWeight(txtStyle, FONT_WEIGHT_400);
124    bool halfLeading = true;
125    OH_Drawing_SetTextStyleHalfLeading(txtStyle, halfLeading);
126    const char* fontFamilies[] = {"Roboto"};
127    OH_Drawing_SetTextStyleFontFamilies(txtStyle, 1, fontFamilies);
128    OH_Drawing_TypographyHandlerPushTextStyle(handler, txtStyle);
129    const char* text = "OpenHarmony\n";
130    OH_Drawing_TypographyHandlerAddText(handler, text);
131    OH_Drawing_PlaceholderSpan placeholderSpan = {20, 40,
132        ALIGNMENT_OFFSET_AT_BASELINE, TEXT_BASELINE_ALPHABETIC, 10};
133    OH_Drawing_TypographyHandlerAddPlaceholder(handler, &placeholderSpan);
134    OH_Drawing_TypographyHandlerPopTextStyle(handler);
135    OH_Drawing_Typography* typography = OH_Drawing_CreateTypography(handler);
136    double maxWidth = 2160.0;
137    OH_Drawing_TypographyLayout(typography, maxWidth);
138    double position[2] = {10.0, 15.0};
139    OH_Drawing_Bitmap* cBitmap = OH_Drawing_BitmapCreate();
140    OH_Drawing_BitmapFormat cFormat {COLOR_FORMAT_RGBA_8888, ALPHA_FORMAT_OPAQUE};
141    uint32_t width = 20;
142    uint32_t height = 40;
143    OH_Drawing_BitmapBuild(cBitmap, width, height, &cFormat);
144    OH_Drawing_Canvas* cCanvas = OH_Drawing_CanvasCreate();
145    OH_Drawing_CanvasBind(cCanvas, cBitmap);
146    OH_Drawing_CanvasClear(cCanvas, OH_Drawing_ColorSetArgb(0xFF, 0xFF, 0xFF, 0xFF));
147    EXPECT_EQ(OH_Drawing_TypographyDidExceedMaxLines(typography) != true, true);
148    OH_Drawing_RectHeightStyle heightStyle = RECT_HEIGHT_STYLE_TIGHT;
149    OH_Drawing_RectWidthStyle widthStyle = RECT_WIDTH_STYLE_TIGHT;
150    EXPECT_EQ(OH_Drawing_TypographyGetRectsForRange(typography, 1, 2, heightStyle, widthStyle) != nullptr, true);
151    EXPECT_EQ(OH_Drawing_TypographyGetRectsForPlaceholders(typography) != nullptr, true);
152    EXPECT_EQ(OH_Drawing_TypographyGetGlyphPositionAtCoordinate(typography, 1, 0) != nullptr, true);
153    EXPECT_EQ(OH_Drawing_TypographyGetGlyphPositionAtCoordinateWithCluster(typography, 1, 0) != nullptr, true);
154    EXPECT_EQ(OH_Drawing_TypographyGetWordBoundary(typography, 1) != nullptr, true);
155    EXPECT_EQ(OH_Drawing_TypographyGetLineTextRange(typography, 1, true) != nullptr, true);
156    EXPECT_EQ(OH_Drawing_TypographyGetLineCount(typography) != 0, true);
157    OH_Drawing_TypographyPaint(typography, cCanvas, position[0], position[1]);
158    OH_Drawing_DestroyTypography(typography);
159    OH_Drawing_DestroyTypographyHandler(handler);
160}
161
162/*
163 * @tc.name: OHDrawingTypographyLargeValueTest027
164 * @tc.desc: test for getting line info for text typography
165 * @tc.type: FUNC
166 */
167HWTEST_F(OHDrawingTypographyLargeValueTest, OHDrawingTypographyLargeValueTest027, TestSize.Level1)
168{
169    OH_Drawing_TypographyStyle* typoStyle = OH_Drawing_CreateTypographyStyle();
170    OH_Drawing_TextStyle* txtStyle = OH_Drawing_CreateTextStyle();
171    OH_Drawing_TypographyCreate* handler = OH_Drawing_CreateTypographyHandler(typoStyle,
172        OH_Drawing_CreateFontCollection());
173    EXPECT_TRUE(handler != nullptr);
174    OH_Drawing_SetTextStyleColor(txtStyle, OH_Drawing_ColorSetArgb(0xFF, 0x00, 0x00, 0x00));
175    double fontSize = 30;
176    OH_Drawing_SetTextStyleFontSize(txtStyle, fontSize);
177    OH_Drawing_SetTextStyleFontWeight(txtStyle, FONT_WEIGHT_400);
178    bool halfLeading = true;
179    OH_Drawing_SetTextStyleHalfLeading(txtStyle, halfLeading);
180    const char* fontFamilies[] = {"Roboto"};
181    OH_Drawing_SetTextStyleFontFamilies(txtStyle, 1, fontFamilies);
182    OH_Drawing_TypographyHandlerPushTextStyle(handler, txtStyle);
183    const char* text = "OpenHarmony\n";
184    OH_Drawing_TypographyHandlerAddText(handler, text);
185    OH_Drawing_TypographyHandlerPopTextStyle(handler);
186    OH_Drawing_Typography* typography = OH_Drawing_CreateTypography(handler);
187    double maxWidth = 2160.0;
188    OH_Drawing_TypographyLayout(typography, maxWidth);
189    double position[2] = {10.0, 15.0};
190    OH_Drawing_Bitmap* cBitmap = OH_Drawing_BitmapCreate();
191    OH_Drawing_BitmapFormat cFormat {COLOR_FORMAT_RGBA_8888, ALPHA_FORMAT_OPAQUE};
192    uint32_t width = 20;
193    uint32_t height = 40;
194    OH_Drawing_BitmapBuild(cBitmap, width, height, &cFormat);
195    OH_Drawing_Canvas* cCanvas = OH_Drawing_CanvasCreate();
196    OH_Drawing_CanvasBind(cCanvas, cBitmap);
197    OH_Drawing_CanvasClear(cCanvas, OH_Drawing_ColorSetArgb(0xFF, 0xFF, 0xFF, 0xFF));
198    OH_Drawing_TypographyPaint(typography, cCanvas, position[0], position[1]);
199    int lineNum = 0;
200    bool oneLine = true;
201    bool includeWhitespace = true;
202    OH_Drawing_LineMetrics lineMetrics;
203    EXPECT_EQ(OH_Drawing_TypographyGetLineInfo(typography, lineNum, oneLine, includeWhitespace, nullptr), false);
204    EXPECT_EQ(OH_Drawing_TypographyGetLineInfo(typography, -1, oneLine, includeWhitespace, &lineMetrics), false);
205    EXPECT_EQ(OH_Drawing_TypographyGetLineInfo(typography, lineNum, oneLine, includeWhitespace, &lineMetrics), true);
206    OH_Drawing_DestroyTypography(typography);
207    OH_Drawing_DestroyTypographyHandler(handler);
208}
209
210/*
211 * @tc.name: OHDrawingTypographyLargeValueTest042
212 * @tc.desc: test for text shadow for textstyle
213 * @tc.type: FUNC
214 */
215HWTEST_F(OHDrawingTypographyLargeValueTest, OHDrawingTypographyLargeValueTest042, TestSize.Level1)
216{
217    OH_Drawing_TypographyStyle* typoStyle = OH_Drawing_CreateTypographyStyle();
218    OH_Drawing_TextStyle* txtStyle = OH_Drawing_CreateTextStyle();
219    OH_Drawing_TypographyCreate* handler = OH_Drawing_CreateTypographyHandler(typoStyle,
220        OH_Drawing_CreateFontCollection());
221    EXPECT_TRUE(handler != nullptr);
222    OH_Drawing_SetTextStyleColor(txtStyle, OH_Drawing_ColorSetArgb(0xFF, 0x00, 0x00, 0x00));
223    double fontSize = 30;
224    OH_Drawing_SetTextStyleFontSize(txtStyle, fontSize);
225    OH_Drawing_SetTextStyleFontWeight(txtStyle, FONT_WEIGHT_400);
226    bool halfLeading = true;
227    OH_Drawing_SetTextStyleHalfLeading(txtStyle, halfLeading);
228    const char* fontFamilies[] = {"Roboto"};
229    OH_Drawing_SetTextStyleFontFamilies(txtStyle, 1, fontFamilies);
230    OH_Drawing_TypographyHandlerPushTextStyle(handler, txtStyle);
231    const char* text = "OpenHarmony\n";
232    OH_Drawing_TypographyHandlerAddText(handler, text);
233    OH_Drawing_TypographyHandlerPopTextStyle(handler);
234    OH_Drawing_Typography* typography = OH_Drawing_CreateTypography(handler);
235    double maxWidth = 2160.0;
236    OH_Drawing_TypographyLayout(typography, maxWidth);
237    double position[2] = {10.0, 15.0};
238    OH_Drawing_Bitmap* cBitmap = OH_Drawing_BitmapCreate();
239    OH_Drawing_BitmapFormat cFormat {COLOR_FORMAT_RGBA_8888, ALPHA_FORMAT_OPAQUE};
240    uint32_t width = 20;
241    uint32_t height = 40;
242    OH_Drawing_BitmapBuild(cBitmap, width, height, &cFormat);
243    OH_Drawing_Canvas* cCanvas = OH_Drawing_CanvasCreate();
244    OH_Drawing_CanvasBind(cCanvas, cBitmap);
245    OH_Drawing_CanvasClear(cCanvas, OH_Drawing_ColorSetArgb(0xFF, 0xFF, 0xFF, 0xFF));
246    EXPECT_EQ(OH_Drawing_TextStyleGetShadows(txtStyle) != nullptr, true);
247    OH_Drawing_TextStyleClearShadows(txtStyle);
248    OH_Drawing_TextShadow* textshadows = OH_Drawing_TextStyleGetShadows(txtStyle);
249    OH_Drawing_DestroyTextShadows(textshadows);
250    OH_Drawing_DestroyTextShadows(nullptr);
251    OH_Drawing_TextStyleAddShadow(txtStyle, nullptr);
252    OH_Drawing_TextStyleAddShadow(txtStyle, OH_Drawing_CreateTextShadow());
253    EXPECT_EQ(OH_Drawing_TextStyleGetShadowWithIndex(txtStyle, 0) != nullptr, true);
254    EXPECT_EQ(OH_Drawing_TextStyleGetShadowWithIndex(txtStyle, 10000000) == nullptr, true);
255    EXPECT_EQ(OH_Drawing_TextStyleGetShadowWithIndex(nullptr, 0) == nullptr, true);
256    EXPECT_EQ(OH_Drawing_TextStyleGetShadowCount(txtStyle), 1);
257    EXPECT_EQ(OH_Drawing_TextStyleGetShadowCount(nullptr), 0);
258    OH_Drawing_TypographyPaint(typography, cCanvas, position[0], position[1]);
259    OH_Drawing_DestroyTypography(typography);
260    OH_Drawing_DestroyTypographyHandler(handler);
261}
262
263/*
264 * @tc.name: OHDrawingTypographyLargeValueTestWithIndent
265 * @tc.desc: test for typography
266 * @tc.type: FUNC
267 */
268HWTEST_F(OHDrawingTypographyLargeValueTest, OHDrawingTypographyLargeValueTestWithIndent, TestSize.Level1)
269{
270    OH_Drawing_TypographyStyle* typoStyle = OH_Drawing_CreateTypographyStyle();
271    OH_Drawing_TextStyle* txtStyle = OH_Drawing_CreateTextStyle();
272    OH_Drawing_TypographyCreate* handler = OH_Drawing_CreateTypographyHandler(typoStyle,
273        OH_Drawing_CreateFontCollection());
274    EXPECT_TRUE(handler != nullptr);
275
276    OH_Drawing_SetTextStyleColor(txtStyle, OH_Drawing_ColorSetArgb(0xFF, 0x00, 0x00, 0x00));
277    double fontSize = 30;
278    OH_Drawing_SetTextStyleFontSize(txtStyle, fontSize);
279    OH_Drawing_SetTextStyleFontWeight(txtStyle, FONT_WEIGHT_400);
280    OH_Drawing_SetTextStyleBaseLine(txtStyle, TEXT_BASELINE_ALPHABETIC);
281    const char* fontFamilies[] = {"Roboto"};
282    OH_Drawing_SetTextStyleFontFamilies(txtStyle, 1, fontFamilies);
283    OH_Drawing_TypographyHandlerPushTextStyle(handler, txtStyle);
284
285    const char* text = "OpenHarmony\n";
286    OH_Drawing_TypographyHandlerAddText(handler, text);
287    OH_Drawing_TypographyHandlerPopTextStyle(handler);
288    OH_Drawing_Typography* typography = OH_Drawing_CreateTypography(handler);
289    const float indents[] = {1.2, 3.4};
290    OH_Drawing_TypographySetIndents(typography, 2, indents);
291    float indent = 3.4;
292    EXPECT_EQ(indent, OH_Drawing_TypographyGetIndentsWithIndex(typography, 1));
293    double maxWidth = 2160.0;
294    OH_Drawing_TypographyLayout(typography, maxWidth);
295    EXPECT_EQ(maxWidth, OH_Drawing_TypographyGetMaxWidth(typography));
296    double position[2] = {10.0, 15.0};
297    OH_Drawing_Bitmap* cBitmap = OH_Drawing_BitmapCreate();
298    OH_Drawing_BitmapFormat cFormat {COLOR_FORMAT_RGBA_8888, ALPHA_FORMAT_OPAQUE};
299    uint32_t width = 20;
300    uint32_t height = 40;
301    OH_Drawing_BitmapBuild(cBitmap, width, height, &cFormat);
302    EXPECT_EQ(width, OH_Drawing_BitmapGetWidth(cBitmap));
303    EXPECT_EQ(height, OH_Drawing_BitmapGetHeight(cBitmap));
304
305    OH_Drawing_Canvas* cCanvas = OH_Drawing_CanvasCreate();
306    OH_Drawing_CanvasBind(cCanvas, cBitmap);
307    OH_Drawing_CanvasClear(cCanvas, OH_Drawing_ColorSetArgb(0xFF, 0xFF, 0xFF, 0xFF));
308
309    EXPECT_EQ(OH_Drawing_TypographyGetHeight(typography) != 0.0, true);
310    EXPECT_EQ(OH_Drawing_TypographyGetLongestLineWithIndent(typography) != 0.0, true);
311    EXPECT_EQ(OH_Drawing_TypographyGetMinIntrinsicWidth(typography) <=
312        OH_Drawing_TypographyGetMaxIntrinsicWidth(typography), true);
313    EXPECT_EQ(OH_Drawing_TypographyGetAlphabeticBaseline(typography) != 0.0, true);
314    EXPECT_EQ(OH_Drawing_TypographyGetIdeographicBaseline(typography) != 0.0, true);
315    OH_Drawing_TypographyPaint(typography, cCanvas, position[0], position[1]);
316    OH_Drawing_DestroyTypography(typography);
317    OH_Drawing_DestroyTypographyHandler(handler);
318}
319}