• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# 复杂文本绘制与显示(C/C++)
2<!--Kit: ArkGraphics 2D-->
3<!--Subsystem: Graphics-->
4<!--Owner: @oh_wangxk; @gmiao522; @Lem0nC-->
5<!--Designer: @liumingxiang-->
6<!--Tester: @yhl0101-->
7<!--Adviser: @ge-yafang-->
8在进行文本绘制时,可以通过选择合适的字体、大小和颜色完成简单文本的绘制与显示;此外,还支持通过设置其他丰富的样式、语言、段落等进行复杂文本的绘制。
9
10复杂文本绘制主要包含以下几个场景:
11
12- 多语言文本绘制与显示
13
14- 多行文本绘制与显示
15
16- 多样式文本绘制与显示
17
18- 样式的拷贝、绘制与显示
19
20
21## 多语言文本绘制与显示
22
23多语言支持是全球化应用的基础。多语言文本绘制需要支持不同语言的字符集及其独特的显示需求,例如右到左语言(如阿拉伯语)或竖排文本(如中文)。开发者需要理解不同语言的渲染特性,确保文本的正确显示。
24
25在多语言文本使用的场景下,主要通过指定TextStyle文本样式中的**locale**字段来实现,可直接通过locale字段的值优先匹配对应字体,跳过遍历列表匹配字体的过程,从而降低匹配时间和内存使用。
26
27### 接口说明
28
29| 接口定义 | 描述 |
30| -------- | -------- |
31| void OH_Drawing_SetTypographyTextLocale(OH_Drawing_TypographyStyle\* style, const char\* locale) | 设置指定排版样式的语言环境。 |
32
33
34### 开发步骤
35
36画布Canvas对象具体可见[画布的获取与绘制结果的显示](canvas-get-result-draw-c.md)。
37
38```c++
39// 创建一个 TypographyStyle,创建 TypographyCreate 时需要使用
40OH_Drawing_TypographyStyle *typoStyle = OH_Drawing_CreateTypographyStyle();
41// 设置文本对齐方式为居中
42OH_Drawing_SetTypographyTextAlign(typoStyle, TEXT_ALIGN_CENTER);
43// 设置 locale 为中文
44OH_Drawing_SetTypographyTextLocale(typoStyle, "zh-Hans");
45
46// 设置文字颜色、大小、字重,不设置 TextStyle 会使用 TypographyStyle 中的默认 TextStyle
47OH_Drawing_TextStyle *txtStyle = OH_Drawing_CreateTextStyle();
48OH_Drawing_SetTextStyleColor(txtStyle, OH_Drawing_ColorSetArgb(0xFF, 0x00, 0x00, 0x00));
49OH_Drawing_SetTextStyleFontSize(txtStyle, 100);
50OH_Drawing_SetTextStyleFontWeight(txtStyle, FONT_WEIGHT_400);
51
52// 创建 FontCollection,FontCollection 用于管理字体匹配逻辑
53OH_Drawing_FontCollection *fc = OH_Drawing_CreateSharedFontCollection();
54// 使用 FontCollection 和 之前创建的 TypographyStyle 创建 TypographyCreate。TypographyCreate 用于创建 Typography
55OH_Drawing_TypographyCreate *handler = OH_Drawing_CreateTypographyHandler(typoStyle, fc);
56
57// 将之前创建的 TextStyle 加入 handler 中
58OH_Drawing_TypographyHandlerPushTextStyle(handler, txtStyle);
59// 设置文本内容,并将文本添加到 handler 中
60const char *text = "你好,中文\n";
61OH_Drawing_TypographyHandlerAddText(handler, text);
62
63// 通过 handler 创建一个 Typography
64OH_Drawing_Typography *typography = OH_Drawing_CreateTypography(handler);
65// 设置排版宽度
66double layoutWidth = 1310;
67OH_Drawing_TypographyLayout(typography, layoutWidth);
68// 设置文本在画布上绘制的起始位置
69double position[2] = {0, 1140};
70// 将文本绘制到画布上
71OH_Drawing_TypographyPaint(typography, canvas, position[0], position[1]);
72
73// 释放内存
74OH_Drawing_DestroyTypographyStyle(typoStyle);
75OH_Drawing_DestroyTextStyle(txtStyle);
76OH_Drawing_DestroyFontCollection(fc);
77OH_Drawing_DestroyTypographyHandler(handler);
78OH_Drawing_DestroyTypography(typography);
79```
80
81
82### 效果展示
83
84![zh-cn_image_0000002246563765](figures/zh-cn_image_0000002246563765.png)
85
86
87## 多行文本绘制与显示
88
89多行文本相对于单行文本比较复杂,一般针对多行文本,需要进行文本排版、断词策略设置、文本对齐方式、最大行数限制等,主要通过设置段落样式实现。
90
91
92### 接口说明
93
94| 接口定义 | 描述 |
95| -------- | -------- |
96| void OH_Drawing_SetTypographyTextAlign(OH_Drawing_TypographyStyle\* style, int align) | 设置文本对齐方式。 |
97| void OH_Drawing_SetTypographyTextWordBreakType(OH_Drawing_TypographyStyle\* style, int wordBreakType) | 设置单词的断词方式。 |
98| void OH_Drawing_SetTypographyTextMaxLines(OH_Drawing_TypographyStyle\* style, int lineNumber) | 设置文本最大行数。 |
99
100
101### 开发步骤
102
103```c++
104// 设置排版宽度
105double layoutWidth = 800;
106// 创建 FontCollection,FontCollection 用于管理字体匹配逻辑
107OH_Drawing_FontCollection *fc = OH_Drawing_CreateSharedFontCollection();
108
109// 设置文字颜色、大小、字重,不设置 TextStyle 会使用 TypographyStyle 中的默认 TextStyle
110OH_Drawing_TextStyle *txtStyle = OH_Drawing_CreateTextStyle();
111OH_Drawing_SetTextStyleColor(txtStyle, OH_Drawing_ColorSetArgb(0xFF, 0x00, 0x00, 0x00));
112OH_Drawing_SetTextStyleFontSize(txtStyle, 50);
113OH_Drawing_SetTextStyleFontWeight(txtStyle, FONT_WEIGHT_400);
114// 当断词策略为WORD_BREAK_TYPE_BREAK_HYPHEN时,需要为段落设置语言偏好,段落会在不同语言偏好下呈现不同的文本断词效果
115// OH_Drawing_SetTextStyleLocale(txtStyle, "en-gb");
116
117// 设置文本内容
118const char *text =
119    "Nunc quis augue viverra, venenatis arcu eu, gravida odio. Integer posuere nisi quis ex pretium, a dapibus "
120    "nisl gravida. Mauris lacinia accumsan enim, non tempus ligula. Mauris iaculis dui eu nisi tristique, in porta "
121    "urna varius. Orci varius natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Mauris "
122    "congue nibh mi, vel ultrices ex volutpat et. Aliquam consectetur odio in libero tristique, a mattis ex "
123    "mollis. Praesent et nisl iaculis, facilisis metus nec, faucibus lacus. Duis nec dolor at nibh eleifend "
124    "tempus. Nunc et enim interdum, commodo eros ac, pretium sapien. Pellentesque laoreet orci a nunc pharetra "
125    "pharetra.";
126
127
128// 创建一个断词策略为 BREAK_ALL 的 TypographyStyle
129OH_Drawing_TypographyStyle *typoStyle = OH_Drawing_CreateTypographyStyle();
130// 设置文本对齐方式为居中
131OH_Drawing_SetTypographyTextAlign(typoStyle, TEXT_ALIGN_CENTER);
132// 设置断词策略为 WORD_BREAK_TYPE_BREAK_ALL
133OH_Drawing_SetTypographyTextWordBreakType(typoStyle, WORD_BREAK_TYPE_BREAK_ALL);
134// 设置最大行数为 10,行数大于 10 的部分不显示
135OH_Drawing_SetTypographyTextMaxLines(typoStyle, 10);
136
137// 使用之前创建的 FontCollection 和 TypographyStyle 创建 TypographyCreate。TypographyCreate 用于创建 Typography
138OH_Drawing_TypographyCreate *handler = OH_Drawing_CreateTypographyHandler(typoStyle, fc);
139// 将之前创建的 TextStyle 加入 handler
140OH_Drawing_TypographyHandlerPushTextStyle(handler, txtStyle);
141// 将文本添加到 handler 中
142OH_Drawing_TypographyHandlerAddText(handler, text);
143
144OH_Drawing_Typography *typography = OH_Drawing_CreateTypography(handler);
145OH_Drawing_TypographyLayout(typography, layoutWidth);
146// 设置文本在画布上绘制的起始位置
147double positionBreakAll[2] = {0, 0};
148// 将文本绘制到画布上
149OH_Drawing_TypographyPaint(typography, canvas, positionBreakAll[0], positionBreakAll[1]);
150
151// 创建一个断词策略为 BREAK_WORD 的 TypographyStyle
152// OH_Drawing_TypographyStyle *typoStyle = OH_Drawing_CreateTypographyStyle();
153// OH_Drawing_SetTypographyTextAlign(typoStyle, TEXT_ALIGN_CENTER);
154// OH_Drawing_SetTypographyTextWordBreakType(typoStyle, WORD_BREAK_TYPE_BREAK_WORD);
155// OH_Drawing_TypographyCreate *handler = OH_Drawing_CreateTypographyHandler(typoStyle, fc);
156// OH_Drawing_TypographyHandlerPushTextStyle(handler, txtStyle);
157// OH_Drawing_TypographyHandlerAddText(handler, text);
158// OH_Drawing_Typography *typography = OH_Drawing_CreateTypography(handler);
159// OH_Drawing_TypographyLayout(typography, layoutWidth);
160// double positionBreakWord[2] = {0, 100};
161// OH_Drawing_TypographyPaint(typography, canvas, positionBreakWord[0], positionBreakWord[1]);
162
163// 创建一个断词策略为 BREAK_HYPHEN 的 TypographyStyle
164// OH_Drawing_TypographyStyle *typoStyle = OH_Drawing_CreateTypographyStyle();
165// OH_Drawing_SetTypographyTextStyle(typoStyle, txtStyle);
166// OH_Drawing_SetTypographyTextAlign(typoStyle, TEXT_ALIGN_LEFT);
167// OH_Drawing_SetTypographyTextWordBreakType(typoStyle, WORD_BREAK_TYPE_BREAK_HYPHEN);
168// OH_Drawing_TypographyCreate *handler = OH_Drawing_CreateTypographyHandler(typoStyle, fc);
169// OH_Drawing_TypographyHandlerPushTextStyle(handler, txtStyle);
170// OH_Drawing_TypographyHandlerAddText(handler, text);
171// OH_Drawing_Typography *typography = OH_Drawing_CreateTypography(handler);
172// OH_Drawing_TypographyLayout(typography, layoutWidth);
173// double positionBreakWord[2] = {0, 100};
174// OH_Drawing_TypographyPaint(typography, canvas, positionBreakWord[0], positionBreakWord[1]);
175
176// 释放内存
177OH_Drawing_DestroyFontCollection(fc);
178OH_Drawing_DestroyTextStyle(txtStyle);
179OH_Drawing_DestroyTypographyStyle(typoStyle);
180OH_Drawing_DestroyTypographyHandler(handler);
181OH_Drawing_DestroyTypography(typography);
182```
183
184
185### 效果展示
186
187| BREAK_ALL | BREAK_WORD |
188| -------- | -------- |
189| ![ndk_word_break_all.jpg](figures/ndk_word_break_all.jpg) | ![ndk_word_break_word.jpg](figures/ndk_word_break_word.jpg) |
190
191| BREAK_HYPHEN(locale:未设置) | BREAK_HYPHEN(locale:en-gb) | BREAK_HYPHEN(locale:en-us) |
192| -------- | -------- |-------- |
193| ![ndk_word_break_hyphen_locale_undefined.jpg](figures/ndk_word_break_hyphen_locale_undefined.jpg) | ![ndk_word_break_hyphen_local_en-gb.jpg](figures/ndk_word_break_hyphen_local_en-gb.jpg) | ![ndk_word_break_hyphen_local_en-us.jpg](figures/ndk_word_break_hyphen_local_en-us.jpg) |
194
195## 多样式文本绘制与显示
196
197除基本文字、排版属性之外,针对应用中不同文本的设计,开发者可能需要设置使用不同的绘制样式或能力,以凸显对应文本的独特表现或风格,此时可以结合使用多种绘制样式进行文本的渲染。
198
199当前支持的多样式绘制及各绘制样式侧重效果如下:
200
201- **装饰线样式绘制:** 主要通过不同的线条样式对文本进行装饰,可以使文本更加突出,富有表现力。
202
203- **字体特性绘制:** 主要通过字体的变化,包括粗细、斜体等特性来改变文本的外观,增强文本的可读性和美观性。
204
205- **可变字体绘制:** 对应提供文本在不同的显示环境和设备上灵活调整的能力,可满足更为精细的视觉效果。
206
207- **文本阴影绘制:** 主要通过在文本周围添加阴影效果,以提升文本的层次感和立体感,从而使文本更具吸引力。
208
209- **占位符绘制:** 可以在不确定文本内容时保持文本布局的稳定性,使得文本显示更为流畅和自然。
210
211- **自动间距绘制:** 可以在一些字符混排切换的地方自动添加额外间距,提升阅读体验。
212
213- **渐变色绘制:** 可以为文字提供颜色渐变效果,增强文字表现力。
214
215- **垂直对齐:** 调整文本在垂直方向排版位置,提升排版质量。
216
217- **上下标:** 可以将任意字符处理成上标或下标,更精准表达文本含义。
218
219- **高对比度文字绘制:** 主要通过将深色文字变黑、浅色文字变白,增强文本的对比效果。
220
221### 装饰线
222
223**装饰线**是指在文本上方、下方或中间添加的装饰性线条,当前支持上划线、下划线、删除线。
224
225可以通过添加文本装饰线,提升文本的视觉效果和可读性。
226
227使用装饰线需要初始化装饰线样式对象,并添加到文本样式中,从而在文本绘制时生效。
228
229
230| 接口定义 | 描述 |
231| -------- | -------- |
232| void OH_Drawing_SetTextStyleDecoration(OH_Drawing_TextStyle\* style, int decoration) | 设置指定文本样式中的装饰线类型,只能设置一个装饰线类型,添加多个需要使用OH_Drawing_AddTextStyleDecoration。 |
233| void OH_Drawing_SetTextStyleDecorationStyle(OH_Drawing_TextStyle\* style, int decorationStyle) | 设置指定文本样式中的装饰线样式。 |
234| void OH_Drawing_SetTextStyleDecorationColor(OH_Drawing_TextStyle\* style, uint32_t color) | 设置指定文本样式中的装饰线颜色。 |
235
236
237示例及示意效果如下所示:
238
239```c++
240// 创建一个TypographyStyle创建Typography时需要使用
241OH_Drawing_TypographyStyle *typoStyle = OH_Drawing_CreateTypographyStyle();
242// 设置文本对齐方式为居中
243OH_Drawing_SetTypographyTextAlign(typoStyle, TEXT_ALIGN_CENTER);
244// 设置文本内容
245const char *text = "Hello World Drawing\n";
246
247// 设置文字颜色、大小、字重,不设置 TextStyle 会使用 TypographyStyle 中的默认 TextStyle
248OH_Drawing_TextStyle *txtStyleWithDeco = OH_Drawing_CreateTextStyle();
249OH_Drawing_SetTextStyleColor(txtStyleWithDeco, OH_Drawing_ColorSetArgb(0xFF, 0x00, 0x00, 0x00));
250OH_Drawing_SetTextStyleFontSize(txtStyleWithDeco, 100);
251OH_Drawing_SetTextStyleFontWeight(txtStyleWithDeco, FONT_WEIGHT_400);
252// 设置装饰线为 LINE_THROUGH
253OH_Drawing_SetTextStyleDecoration(txtStyleWithDeco, TEXT_DECORATION_LINE_THROUGH);
254// 设置装饰线样式为 WAVY
255OH_Drawing_SetTextStyleDecorationStyle(txtStyleWithDeco, TEXT_DECORATION_STYLE_WAVY);
256// 设置装饰线颜色
257OH_Drawing_SetTextStyleDecorationColor(txtStyleWithDeco, OH_Drawing_ColorSetArgb(0xFF, 0x6F, 0xFF, 0xFF));
258
259// 创建一个不带装饰线的 TextStyle 用于对比
260OH_Drawing_TextStyle *txtStyleNoDeco = OH_Drawing_CreateTextStyle();
261// 设置文字颜色、大小、字重,不设置 TextStyle 会使用 TypographyStyle 中的默认 TextStyle
262OH_Drawing_SetTextStyleColor(txtStyleNoDeco, OH_Drawing_ColorSetArgb(0xFF, 0x00, 0x00, 0x00));
263OH_Drawing_SetTextStyleFontSize(txtStyleNoDeco, 100);
264OH_Drawing_SetTextStyleFontWeight(txtStyleNoDeco, FONT_WEIGHT_400);
265
266// 创建 FontCollection,FontCollection 用于管理字体匹配逻辑
267OH_Drawing_FontCollection *fc = OH_Drawing_CreateSharedFontCollection();
268// 使用 FontCollection 和 之前创建的 TypographyStyle 创建 TypographyCreate。TypographyCreate 用于创建 Typography
269OH_Drawing_TypographyCreate *handler = OH_Drawing_CreateTypographyHandler(typoStyle, fc);
270
271// 加入带有装饰线的文本样式
272OH_Drawing_TypographyHandlerPushTextStyle(handler, txtStyleWithDeco);
273// 将文本添加到 handler 中
274OH_Drawing_TypographyHandlerAddText(handler, text);
275
276// 后续加入的不带装饰线的文本样式
277OH_Drawing_TypographyHandlerPushTextStyle(handler, txtStyleNoDeco);
278// 将文本添加到 handler 中
279OH_Drawing_TypographyHandlerAddText(handler, text);
280
281OH_Drawing_Typography *typography = OH_Drawing_CreateTypography(handler);
282// 设置排版宽度
283double layoutWidth = 1310;
284OH_Drawing_TypographyLayout(typography, layoutWidth);
285// 设置文本在画布上绘制的起始位置
286double position[2] = {0, 1140};
287// 将文本绘制到画布上
288OH_Drawing_TypographyPaint(typography, canvas, position[0], position[1]);
289
290// 释放内存
291OH_Drawing_DestroyTypographyStyle(typoStyle);
292OH_Drawing_DestroyTextStyle(txtStyleWithDeco);
293OH_Drawing_DestroyTextStyle(txtStyleNoDeco);
294OH_Drawing_DestroyFontCollection(fc);
295OH_Drawing_DestroyTypographyHandler(handler);
296OH_Drawing_DestroyTypography(typography);
297```
298
299
300![zh-cn_image_0000002211603604](figures/zh-cn_image_0000002211603604.png)
301
302
303### 字体特征
304
305**字体特征**绘制专注于在文本渲染过程中对字体特性(如粗体、斜体、字体变种等)的处理,允许字体在不同的排版场景下表现出不同的效果,可用于增强文本的表现力,使其更符合设计和阅读需求。
306
307常见的**FontFeature**包含有liga、frac、case等,需要对应的ttf文件支持才能正常使能。
308
309
310| 接口定义 | 描述 |
311| -------- | -------- |
312| void OH_Drawing_TextStyleAddFontFeature(OH_Drawing_TextStyle\* style, const char\* tag, int value) | 设置文本样式中指定字体特征是否启用。 |
313
314
315示例及示意效果如下所示:
316
317
318```c++
319// 创建一个 TypographyStyle,创建 TypographyCreate 时需要使用
320OH_Drawing_TypographyStyle *typoStyle = OH_Drawing_CreateTypographyStyle();
321// 设置文本对齐方式为居中
322OH_Drawing_SetTypographyTextAlign(typoStyle, TEXT_ALIGN_CENTER);
323// 设置文本内容
324const char *text = "1/2 1/3 1/4\n";
325
326// 设置文字颜色、大小、字重,不设置TextStyle无法绘制出文本
327OH_Drawing_TextStyle *txtStyleWithFeature = OH_Drawing_CreateTextStyle();
328OH_Drawing_SetTextStyleColor(txtStyleWithFeature, OH_Drawing_ColorSetArgb(0xFF, 0x00, 0x00, 0x00));
329OH_Drawing_SetTextStyleFontSize(txtStyleWithFeature, 100);
330OH_Drawing_SetTextStyleFontWeight(txtStyleWithFeature, FONT_WEIGHT_900);
331// 设置启用frac font feature,此功能将斜线分隔的数字替换为普通(对角线)分数。
332OH_Drawing_TextStyleAddFontFeature(txtStyleWithFeature, "frac", 1);
333
334// 创建一个不带字体特征的 TextStyle 用于对比
335OH_Drawing_TextStyle *txtStyleNoFeature = OH_Drawing_CreateTextStyle();
336// 设置文字颜色、大小、字重。不设置 TextStyle 无法绘制出文本
337OH_Drawing_SetTextStyleColor(txtStyleNoFeature, OH_Drawing_ColorSetArgb(0xFF, 0x00, 0x00, 0x00));
338OH_Drawing_SetTextStyleFontSize(txtStyleNoFeature, 100);
339OH_Drawing_SetTextStyleFontWeight(txtStyleNoFeature, FONT_WEIGHT_900);
340
341// 创建 FontCollection,FontCollection 用于管理字体匹配逻辑
342OH_Drawing_FontCollection *fc = OH_Drawing_CreateSharedFontCollection();
343// 使用 FontCollection 和 之前创建的 TypographyStyle 创建 TypographyCreate。TypographyCreate 用于创建 Typography
344OH_Drawing_TypographyCreate *handler = OH_Drawing_CreateTypographyHandler(typoStyle, fc);
345
346// 加入带有字体特征的文本样式
347OH_Drawing_TypographyHandlerPushTextStyle(handler, txtStyleWithFeature);
348// 将文本添加到 handler 中
349OH_Drawing_TypographyHandlerAddText(handler, text);
350// 弹出之前创建的 TextStyle
351OH_Drawing_TypographyHandlerPopTextStyle(handler);
352
353// 后续加入的不带字体特征的文本样式
354OH_Drawing_TypographyHandlerPushTextStyle(handler, txtStyleNoFeature);
355// 将文本添加到 handler 中
356OH_Drawing_TypographyHandlerAddText(handler, text);
357// 弹出之前创建的 TextStyle
358OH_Drawing_TypographyHandlerPopTextStyle(handler);
359
360OH_Drawing_Typography *typography = OH_Drawing_CreateTypography(handler);
361// 设置排版宽度
362double layoutWidth = 1310;
363OH_Drawing_TypographyLayout(typography, layoutWidth);
364// 设置文本在画布上绘制的起始位置
365double position[2] = {0, 1140};
366// 将文本绘制到画布上
367OH_Drawing_TypographyPaint(typography, canvas, position[0], position[1]);
368
369// 释放内存
370OH_Drawing_DestroyTypographyStyle(typoStyle);
371OH_Drawing_DestroyTextStyle(txtStyleWithFeature);
372OH_Drawing_DestroyTextStyle(txtStyleNoFeature);
373OH_Drawing_DestroyFontCollection(fc);
374OH_Drawing_DestroyTypographyHandler(handler);
375OH_Drawing_DestroyTypography(typography);
376```
377
378
379![zh-cn_image_0000002246603641](figures/zh-cn_image_0000002246603641.png)
380
381
382### 可变字体
383
384**可变字体**是一种在一个字体文件中包含多个字形变体的字体格式,允许在一个字体文件内灵活地调整字体的各种属性(如字重、字宽、斜体等)。
385
386与传统字体文件(每种变体需要一个独立的文件)不同,可变字体在一个字体文件中包含多个变体轴,可通过使用可变字体实现文本渲染绘制时的平滑过渡。
387
388
389| 接口定义 | 描述 |
390| -------- | -------- |
391| void OH_Drawing_TextStyleAddFontVariation(OH_Drawing_TextStyle\* style, const char\* axis, const float value) | 添加可变字体属性。对应的字体文件(.ttf文件)需要支持可变调节,此接口才能生效。当对应的字体不支持可变调节时,此接口调用不生效。 |
392
393
394示例及示意效果如下所示:
395
396
397```c++
398// 创建一个 TypographyStyle 创建 Typography 时需要使用
399OH_Drawing_TypographyStyle *typoStyle = OH_Drawing_CreateTypographyStyle();
400// 设置文本对齐方式为居中
401OH_Drawing_SetTypographyTextAlign(typoStyle, TEXT_ALIGN_CENTER);
402// 设置文字内容
403const char *text = "Hello World Drawing\n";
404
405OH_Drawing_TextStyle *txtStyleWithVar = OH_Drawing_CreateTextStyle();
406// 设置可变字体的字重,在字体文件支持的情况下,还可以设置"slnt", "wdth"
407OH_Drawing_TextStyleAddFontVariation(txtStyleWithVar, "wght", 800);
408// 设置文字颜色、大小、字重,不设置 TextStyle 会使用 TypographyStyle 中的默认 TextStyle
409OH_Drawing_SetTextStyleColor(txtStyleWithVar, OH_Drawing_ColorSetArgb(0xFF, 0x00, 0x00, 0x00));
410OH_Drawing_SetTextStyleFontSize(txtStyleWithVar, 100);
411// 此处设置字重不生效,将被可变字体的字重覆盖
412OH_Drawing_SetTextStyleFontWeight(txtStyleWithVar, FONT_WEIGHT_400);
413
414// 创建一个不带可变字体的 TextStyle 用于对比
415OH_Drawing_TextStyle *txtStyleNoVar = OH_Drawing_CreateTextStyle();
416// 设置文字颜色、大小、字重,不设置 TextStyle 会使用 TypographyStyle 中的默认 TextStyle
417OH_Drawing_SetTextStyleColor(txtStyleNoVar, OH_Drawing_ColorSetArgb(0xFF, 0x00, 0x00, 0x00));
418OH_Drawing_SetTextStyleFontSize(txtStyleNoVar, 100);
419OH_Drawing_SetTextStyleFontWeight(txtStyleNoVar, FONT_WEIGHT_400);
420
421// 创建 FontCollection,FontCollection 用于管理字体匹配逻辑
422OH_Drawing_FontCollection *fc = OH_Drawing_CreateSharedFontCollection();
423// 使用 FontCollection 和 之前创建的 TypographyStyle 创建 TypographyCreate。TypographyCreate 用于创建 Typography
424OH_Drawing_TypographyCreate *handler = OH_Drawing_CreateTypographyHandler(typoStyle, fc);
425
426// 加入带有可变字体的文本样式
427OH_Drawing_TypographyHandlerPushTextStyle(handler, txtStyleWithVar);
428// 将文本添加到 handler 中
429OH_Drawing_TypographyHandlerAddText(handler, text);
430// 弹出之前创建的 TextStyle
431OH_Drawing_TypographyHandlerPopTextStyle(handler);
432
433// 后续加入的不带可变字体的文本样式
434OH_Drawing_TypographyHandlerPushTextStyle(handler, txtStyleNoVar);
435// 将文本添加到 handler 中
436OH_Drawing_TypographyHandlerAddText(handler, text);
437// 弹出之前创建的 TextStyle
438OH_Drawing_TypographyHandlerPopTextStyle(handler);
439
440OH_Drawing_Typography *typography = OH_Drawing_CreateTypography(handler);
441// 设置排版宽度
442double layoutWidth = 1310;
443OH_Drawing_TypographyLayout(typography, layoutWidth);
444// 设置文本在画布上绘制的起始位置
445double position[2] = {0, 1140};
446// 将文本绘制到画布上
447OH_Drawing_TypographyPaint(typography, canvas, position[0], position[1]);
448
449// 释放内存
450OH_Drawing_DestroyTypographyStyle(typoStyle);
451OH_Drawing_DestroyTextStyle(txtStyleWithVar);
452OH_Drawing_DestroyTextStyle(txtStyleNoVar);
453OH_Drawing_DestroyFontCollection(fc);
454OH_Drawing_DestroyTypographyHandler(handler);
455OH_Drawing_DestroyTypography(typography);
456```
457
458
459![zh-cn_image_0000002211443824](figures/zh-cn_image_0000002211443824.png)
460
461
462### 文本阴影
463
464**文本阴影**为文本提供了深度感,使得文本在背景上更具立体感。通常用于提升文本的视觉吸引力或增强可读性,尤其是在色彩对比度较低的场景下。
465
466其中,TextShadow有三个属性,分别为阴影颜色color、阴影基于当前文本的偏移位置point、阴影半径blurRadius。
467
468使用阴影效果需要在文本样式中设置对应的阴影效果数组,从而在文本绘制时生效。
469
470
471| 接口定义 | 描述 |
472| -------- | -------- |
473| OH_Drawing_Point\* OH_Drawing_PointCreate(float x, float y) | 用于创建一个坐标点对象。 |
474| OH_Drawing_TextShadow\* OH_Drawing_CreateTextShadow(void) | 创建指向字体阴影对象的指针。 |
475| void OH_Drawing_SetTextShadow(OH_Drawing_TextShadow\* shadow, uint32_t color, OH_Drawing_Point\* offset, double blurRadius) | 设置字体阴影对象的参数。 |
476| void OH_Drawing_TextStyleAddShadow(OH_Drawing_TextStyle\* style, const OH_Drawing_TextShadow\* shadow) | 字体阴影容器中添加字体阴影元素。 |
477| void OH_Drawing_DestroyTextShadow(OH_Drawing_TextShadow\* shadow) | 释放被字体阴影对象占据的内存。 |
478
479
480示例及示意效果如下所示:
481
482
483```c++
484// 创建一个 TypographyStyle 创建 Typography 时需要使用
485OH_Drawing_TypographyStyle *typoStyle = OH_Drawing_CreateTypographyStyle();
486// 设置文本对齐方式为居中
487OH_Drawing_SetTypographyTextAlign(typoStyle, TEXT_ALIGN_CENTER);
488// 设置文本内容
489const char *text = "Hello World Drawing\n";
490
491// 设置文字颜色、大小、字重,不设置 TextStyle 会使用 TypographyStyle 中的默认 TextStyle
492OH_Drawing_TextStyle *txtStyleWithShadow = OH_Drawing_CreateTextStyle();
493OH_Drawing_SetTextStyleColor(txtStyleWithShadow, OH_Drawing_ColorSetArgb(0xFF, 0x00, 0x00, 0x00));
494OH_Drawing_SetTextStyleFontSize(txtStyleWithShadow, 100);
495OH_Drawing_SetTextStyleFontWeight(txtStyleWithShadow, FONT_WEIGHT_400);
496// 设置阴影偏移量
497OH_Drawing_Point *offset = OH_Drawing_PointCreate(1, 1);
498OH_Drawing_TextShadow *shadow = OH_Drawing_CreateTextShadow();
499// 为 TextShadow 设置样式
500OH_Drawing_SetTextShadow(shadow, OH_Drawing_ColorSetArgb(0xFF, 0x00, 0x00, 0x00), offset, 10);
501// 将 TextShadow 加入 TextStyle
502OH_Drawing_TextStyleAddShadow(txtStyleWithShadow, shadow);
503
504// 创建一个不带阴影的 TextStyle 用于对比
505OH_Drawing_TextStyle *txtStyleNoShadow = OH_Drawing_CreateTextStyle();
506// 设置文字颜色、大小、字重,不设置 TextStyle 会使用 TypographyStyle 中的默认 TextStyle
507OH_Drawing_SetTextStyleColor(txtStyleNoShadow, OH_Drawing_ColorSetArgb(0xFF, 0x00, 0x00, 0x00));
508OH_Drawing_SetTextStyleFontSize(txtStyleNoShadow, 100);
509OH_Drawing_SetTextStyleFontWeight(txtStyleNoShadow, FONT_WEIGHT_400);
510
511// 创建 FontCollection,FontCollection 用于管理字体匹配逻辑
512OH_Drawing_FontCollection *fc = OH_Drawing_CreateSharedFontCollection();
513// 使用 FontCollection 和 之前创建的 TypographyStyle 创建 TypographyCreate。TypographyCreate 用于创建 Typography
514OH_Drawing_TypographyCreate *handler = OH_Drawing_CreateTypographyHandler(typoStyle, fc);
515
516// 加入带有阴影的文本样式
517OH_Drawing_TypographyHandlerPushTextStyle(handler, txtStyleWithShadow);
518// 将文本添加到 handler 中
519OH_Drawing_TypographyHandlerAddText(handler, text);
520
521// 后续加入的不带阴影的文本样式
522OH_Drawing_TypographyHandlerPushTextStyle(handler, txtStyleNoShadow);
523// 将文本添加到 handler 中
524OH_Drawing_TypographyHandlerAddText(handler, text);
525
526OH_Drawing_Typography *typography = OH_Drawing_CreateTypography(handler);
527// 设置排版宽度
528double layoutWidth = 1310;
529OH_Drawing_TypographyLayout(typography, layoutWidth);
530// 设置文本在画布上绘制的起始位置
531double position[2] = {0, 1140};
532// 将文本绘制到画布上
533OH_Drawing_TypographyPaint(typography, canvas, position[0], position[1]);
534
535// 释放内存
536OH_Drawing_DestroyTypographyStyle(typoStyle);
537OH_Drawing_DestroyTextStyle(txtStyleWithShadow);
538OH_Drawing_DestroyPoint(offset);
539OH_Drawing_DestroyTextShadow(shadow);
540OH_Drawing_DestroyTextStyle(txtStyleNoShadow);
541OH_Drawing_DestroyFontCollection(fc);
542OH_Drawing_DestroyTypographyHandler(handler);
543OH_Drawing_DestroyTypography(typography);
544```
545
546
547![zh-cn_image_0000002246563769](figures/zh-cn_image_0000002246563769.png)
548
549
550### 占位符
551
552占位符绘制用于处理文本中占位符符号的渲染。
553
554占位符也是用来实现图文混排的关键,是指在实际图像或内容注册之前,用来预先提供或替代某个位置的视觉元素。
555
556
557| 接口定义 | 描述 |
558| -------- | -------- |
559| void OH_Drawing_TypographyHandlerAddPlaceholder(OH_Drawing_TypographyCreate\* handler, OH_Drawing_PlaceholderSpan\* span) | 设置占位符。 |
560
561
562示例及示意效果如下所示:
563
564
565```c++
566// 设置排版宽度
567double layoutWidth = 1310;
568// 创建 FontCollection,FontCollection 用于管理字体匹配逻辑
569OH_Drawing_FontCollection *fc = OH_Drawing_CreateSharedFontCollection();
570
571// 设置文字颜色、大小、字重,不设置 TextStyle 会使用 TypographyStyle 中的默认 TextStyle
572OH_Drawing_TextStyle *txtStyle = OH_Drawing_CreateTextStyle();
573OH_Drawing_SetTextStyleColor(txtStyle, OH_Drawing_ColorSetArgb(0xFF, 0x00, 0x00, 0x00));
574OH_Drawing_SetTextStyleFontSize(txtStyle, 100);
575OH_Drawing_SetTextStyleFontWeight(txtStyle, FONT_WEIGHT_400);
576
577// 设置文本内容
578const char *text = "Hello World Drawing\n";
579
580// 创建一个 TypographyStyle 创建 Typography 时需要使用
581OH_Drawing_TypographyStyle *typoStyle = OH_Drawing_CreateTypographyStyle();
582// 设置文本对齐方式为居中
583OH_Drawing_SetTypographyTextAlign(typoStyle, TEXT_ALIGN_CENTER);
584
585// 使用 FontCollection 和 之前创建的 TypographyStyle 创建 TypographyCreate。TypographyCreate 用于创建 Typography
586OH_Drawing_TypographyCreate *handlerWithPlaceholder = OH_Drawing_CreateTypographyHandler(typoStyle, fc);
587// 创建一个 placeholder,并且初始化其成员变量
588OH_Drawing_PlaceholderSpan placeholder;
589placeholder.width = 200.0;
590placeholder.height = 200.0;
591placeholder.alignment = ALIGNMENT_ABOVE_BASELINE; // 基线对齐策略
592placeholder.baseline = TEXT_BASELINE_ALPHABETIC;                  // 使用的文本基线类型
593placeholder.baselineOffset = 0.0; // 相比基线的偏移量。只有对齐策略是 OFFSET_AT_BASELINE 时生效
594
595// 将 placeholder 放在开头
596OH_Drawing_TypographyHandlerAddPlaceholder(handlerWithPlaceholder, &placeholder);
597
598// 将之前创建的 TextStyle 加入 handler
599OH_Drawing_TypographyHandlerPushTextStyle(handlerWithPlaceholder, txtStyle);
600// 将文本添加到 handler 中
601OH_Drawing_TypographyHandlerAddText(handlerWithPlaceholder, text);
602
603OH_Drawing_Typography *typographyWithPlaceholder = OH_Drawing_CreateTypography(handlerWithPlaceholder);
604OH_Drawing_TypographyLayout(typographyWithPlaceholder, layoutWidth);
605// 设置文本在画布上绘制的起始位置
606double positionBreakAll[2] = {0, 0};
607// 将文本绘制到画布上
608OH_Drawing_TypographyPaint(typographyWithPlaceholder, canvas, positionBreakAll[0], positionBreakAll[1]);
609
610// 创建 OH_Drawing_TypographyCreate
611OH_Drawing_TypographyCreate *handlerNoPlaceholder = OH_Drawing_CreateTypographyHandler(typoStyle, fc);
612// 将之前创建的 TextStyle 加入 handler
613OH_Drawing_TypographyHandlerPushTextStyle(handlerNoPlaceholder, txtStyle);
614// 将文本添加到 handler 中
615OH_Drawing_TypographyHandlerAddText(handlerNoPlaceholder, text);
616
617OH_Drawing_Typography *typographyNoPlaceholder = OH_Drawing_CreateTypography(handlerNoPlaceholder);
618OH_Drawing_TypographyLayout(typographyNoPlaceholder, layoutWidth);
619// 设置文本在画布上绘制的起始位置
620double positionBreakWord[2] = {0, 1140};
621// 将文本绘制到画布上
622OH_Drawing_TypographyPaint(typographyNoPlaceholder, canvas, positionBreakWord[0], positionBreakWord[1]);
623
624// 释放内存
625OH_Drawing_DestroyFontCollection(fc);
626OH_Drawing_DestroyTextStyle(txtStyle);
627OH_Drawing_DestroyTypographyStyle(typoStyle);
628OH_Drawing_DestroyTypographyHandler(handlerWithPlaceholder);
629OH_Drawing_DestroyTypographyHandler(handlerNoPlaceholder);
630OH_Drawing_DestroyTypography(typographyWithPlaceholder);
631OH_Drawing_DestroyTypography(typographyNoPlaceholder);
632```
633
634
635![zh-cn_image_0000002211443820](figures/zh-cn_image_0000002211443820.png)
636
637
638### 自动间距
639
640使能自动间距,则会在文本排版时自动调整CJK(中文字符、日文字符、韩文字符)与西文(拉丁字母、西里尔字母、希腊字母)、CJK与数字、CJK与版权符号、版权符号与数字、版权符号与西文之间的间距。例如,在中英文混排场景中,使能自动间距即可在中英文切换的地方自动添加额外间距,提升阅读体验。
641
642
643| 接口定义 | 描述 |
644| -------- | -------- |
645| void OH_Drawing_SetTypographyTextAutoSpace(OH_Drawing_TypographyStyle \*style, bool enableAutoSpace) |设置文本排版时是否使能自动间距。默认不使能自动间距,一旦使能则会自动调整CJK(中文字符、日文字符、韩文字符)与西文(拉丁字母、西里尔字母、希腊字母)、CJK与数字、CJK与版权符号、版权符号与数字、版权符号与西文之间的间距。 |
646
647
648示例及示意效果如下所示:
649
650
651```c++
652// 创建一个TypographyStyle创建Typography时需要使用
653OH_Drawing_TypographyStyle *typoStyle = OH_Drawing_CreateTypographyStyle();
654// 设置使能自动间距,默认为false
655OH_Drawing_SetTypographyTextAutoSpace(typoStyle, true);
656// 设置文字内容
657const char *text = "test测试©test©测试。";
658
659OH_Drawing_TextStyle *txtStyle = OH_Drawing_CreateTextStyle();
660// 设置文字颜色、大小、字重,不设置TextStyle会使用TypographyStyle中的默认TextStyle
661OH_Drawing_SetTextStyleColor(txtStyle, OH_Drawing_ColorSetArgb(0xFF, 0x00, 0x00, 0x00));
662OH_Drawing_SetTextStyleFontSize(txtStyle, 100);
663
664// 创建FontCollection,FontCollection用于管理字体匹配逻辑
665OH_Drawing_FontCollection *fc = OH_Drawing_CreateSharedFontCollection();
666// 使用FontCollection和之前创建的TypographyStyle创建TypographyCreate。TypographyCreate用于创建Typography
667OH_Drawing_TypographyCreate *handler = OH_Drawing_CreateTypographyHandler(typoStyle, fc);
668
669// 将文本样式添加到handler中
670OH_Drawing_TypographyHandlerPushTextStyle(handler, txtStyle);
671// 将文本添加到handler中
672OH_Drawing_TypographyHandlerAddText(handler, text);
673// 创建段落
674OH_Drawing_Typography *typography = OH_Drawing_CreateTypography(handler);
675// 设置排版宽度
676double layoutWidth = 1310;
677// 将段落按照排版宽度进行排版
678OH_Drawing_TypographyLayout(typography, layoutWidth);
679// 设置文本在画布上绘制的起始位置
680double position[2] = {0, 1140};
681// 将文本绘制到画布上
682OH_Drawing_TypographyPaint(typography, canvas, position[0], position[1]);
683
684// 释放内存
685OH_Drawing_DestroyTypographyStyle(typoStyle);
686OH_Drawing_DestroyTextStyle(txtStyle);
687OH_Drawing_DestroyFontCollection(fc);
688OH_Drawing_DestroyTypographyHandler(handler);
689OH_Drawing_DestroyTypography(typography);
690```
691
692| 段落样式设置(自动间距) | 示意效果 |
693| -------- | -------- |
694| 不使能自动间距 | ![zh-cn_image_autoSpace_1](figures/zh-cn_image_autoSpace_1.png) |
695| 使能自动间距 | ![zh-cn_image_autoSpace_2](figures/zh-cn_image_autoSpace_2.png) |
696
697### 渐变色
698
699**渐变色**是一种在文字设计中广泛应用的视觉效果,通过在文字的不同部分应用不同的颜色,从而创造出从一种颜色平滑过渡到另一种颜色的效果。可以通过着色器实现文字渐变的效果,着色器的更多介绍请参考[着色器效果](complex-drawing-effect-c.md#着色器效果)。
700
701
702| 接口定义 | 描述 |
703| -------- | -------- |
704| void OH_Drawing_SetTextStyleForegroundBrush(OH_Drawing_TextStyle\* style, OH_Drawing_Brush* foregroundBrush) | 添加前景画刷,渐变着色器属性依附于前景画刷中。 |
705
706
707示例及效果如下所示:
708```c++
709OH_Drawing_TypographyStyle *typoStyle = OH_Drawing_CreateTypographyStyle();
710OH_Drawing_TextStyle *txtStyle = OH_Drawing_CreateTextStyle();
711// 设置文字大小
712OH_Drawing_SetTextStyleFontSize(txtStyle, 100);
713// 创建着色器对象,并设置颜色、变化起始点与结束点
714OH_Drawing_Point *startPt = OH_Drawing_PointCreate(0, 0);
715OH_Drawing_Point *endPt = OH_Drawing_PointCreate(900, 900);
716uint32_t colors[] = {0xFFFFFF00, 0xFFFF0000, 0xFF0000FF};
717float pos[] = {0.0f, 0.5f, 1.0f};
718OH_Drawing_ShaderEffect *colorShaderEffect =
719    OH_Drawing_ShaderEffectCreateLinearGradient(startPt, endPt, colors, pos, 3, OH_Drawing_TileMode::CLAMP);
720// 创建画刷对象,并将着色器添加到画刷
721OH_Drawing_Brush* brush = OH_Drawing_BrushCreate();
722OH_Drawing_BrushSetShaderEffect(brush, colorShaderEffect);
723// 将画刷添加到文本样式中
724OH_Drawing_SetTextStyleForegroundBrush(txtStyle, brush);
725// 创建排版对象,并绘制
726OH_Drawing_FontCollection *fc = OH_Drawing_CreateSharedFontCollection();
727OH_Drawing_TypographyCreate *handler = OH_Drawing_CreateTypographyHandler(typoStyle, fc);
728OH_Drawing_TypographyHandlerPushTextStyle(handler, txtStyle);
729const char *text = "Hello World";
730OH_Drawing_TypographyHandlerAddText(handler, text);
731OH_Drawing_Typography *typography = OH_Drawing_CreateTypography(handler);
732OH_Drawing_TypographyLayout(typography, 1000);
733OH_Drawing_TypographyPaint(typography, canvas, 0, 0);
734
735// 释放对象
736OH_Drawing_DestroyFontCollection(fc);
737OH_Drawing_ShaderEffectDestroy(colorShaderEffect);
738OH_Drawing_BrushDestroy(brush);
739OH_Drawing_DestroyTextStyle(txtStyle);
740OH_Drawing_DestroyTypographyStyle(typoStyle);
741OH_Drawing_DestroyTypographyHandler(handler);
742OH_Drawing_DestroyTypography(typography);
743```
744
745![zh-cn_image_gradient_c](figures/zh-cn_image_gradient_c.png)
746
747### 垂直对齐
748
749**垂直对齐**用于调整文本在一行中垂直方向的排版位置。开启行高缩放或行内存在不同字号文本混排时使能垂直对齐,可以让文本实现顶部对齐、居中对齐、底部对齐或基线对齐(默认)。
750
751| 接口定义 | 描述 |
752| -------- | -------- |
753| void OH_Drawing_SetTypographyVerticalAlignment(OH_Drawing_TypographyStyle* style, OH_Drawing_TextVerticalAlignment align) | 设置文本垂直方向排版方式。|
754
755示例及效果如下所示:
756```c++
757OH_Drawing_TypographyStyle *typoStyle = OH_Drawing_CreateTypographyStyle();
758OH_Drawing_TextStyle *txtStyle = OH_Drawing_CreateTextStyle();
759// 设置垂直对齐方式
760OH_Drawing_SetTypographyVerticalAlignment(typoStyle, OH_Drawing_TextVerticalAlignment::TEXT_VERTICAL_ALIGNMENT_CENTER);
761// 设置文字大小
762OH_Drawing_SetTextStyleFontSize(txtStyle, 30);
763// 设置文字颜色
764OH_Drawing_SetTextStyleColor(txtStyle, OH_Drawing_ColorSetArgb(0xFF, 0x00, 0x00, 0x00));
765// 创建排版对象,并绘制
766OH_Drawing_FontCollection *fc = OH_Drawing_CreateSharedFontCollection();
767OH_Drawing_TypographyCreate *handler = OH_Drawing_CreateTypographyHandler(typoStyle, fc);
768OH_Drawing_TypographyHandlerPushTextStyle(handler, txtStyle);
769const char *text = "VerticalAlignment-center";
770OH_Drawing_TypographyHandlerAddText(handler, text);
771OH_Drawing_Typography *typography = OH_Drawing_CreateTypography(handler);
772OH_Drawing_TypographyLayout(typography, 1000);
773OH_Drawing_TypographyPaint(typography, canvas, 0, 0);
774
775// 释放对象
776OH_Drawing_DestroyFontCollection(fc);
777OH_Drawing_DestroyTextStyle(txtStyle);
778OH_Drawing_DestroyTypographyStyle(typoStyle);
779OH_Drawing_DestroyTypographyHandler(handler);
780OH_Drawing_DestroyTypography(typography);
781```
782
783![zh-cn_image_complexArkTsDemo2_2](figures/en_image_verticalAlignment_center.jpg)
784
785### 上下标
786
787**上下标**能将文本作为上标或下标参与排版。一般用于数学公式、化学式等场景。
788
789| 接口定义 | 描述 |
790| -------- | -------- |
791| void OH_Drawing_SetTextStyleBadgeType(OH_Drawing_TextStyle* style, OH_Drawing_TextBadgeType textBadgeType) | 使能上下标样式。|
792
793示例及效果如下所示:
794```c++
795OH_Drawing_TypographyStyle *typoStyle = OH_Drawing_CreateTypographyStyle();
796OH_Drawing_TextStyle *txtStyle = OH_Drawing_CreateTextStyle();
797OH_Drawing_TextStyle *badgeTxtStyle = OH_Drawing_CreateTextStyle();
798// 设置文字大小
799OH_Drawing_SetTextStyleFontSize(txtStyle, 30);
800OH_Drawing_SetTextStyleFontSize(badgeTxtStyle, 30);
801// 设置文字颜色
802OH_Drawing_SetTextStyleColor(txtStyle, OH_Drawing_ColorSetArgb(0xFF, 0x00, 0x00, 0x00));
803OH_Drawing_SetTextStyleColor(badgeTxtStyle, OH_Drawing_ColorSetArgb(0xFF, 0x00, 0x00, 0x00));
804// 使能文本上标
805OH_Drawing_SetTextStyleBadgeType(badgeTxtStyle, OH_Drawing_TextBadgeType::TEXT_SUPERSCRIPT);
806// 创建排版对象,并绘制
807OH_Drawing_FontCollection *fc = OH_Drawing_CreateSharedFontCollection();
808OH_Drawing_TypographyCreate *handler = OH_Drawing_CreateTypographyHandler(typoStyle, fc);
809OH_Drawing_TypographyHandlerPushTextStyle(handler, txtStyle);
810const char *text = "Mass-energy equivalence: E=mc";
811OH_Drawing_TypographyHandlerAddText(handler, text);
812OH_Drawing_TypographyHandlerPushTextStyle(handler, badgeTxtStyle);
813const char *badgeText = "2";
814OH_Drawing_TypographyHandlerAddText(handler, badgeText);
815OH_Drawing_Typography *typography = OH_Drawing_CreateTypography(handler);
816OH_Drawing_TypographyLayout(typography, 1000);
817OH_Drawing_TypographyPaint(typography, canvas, 0, 0);
818
819// 释放对象
820OH_Drawing_DestroyFontCollection(fc);
821OH_Drawing_DestroyTextStyle(txtStyle);
822OH_Drawing_DestroyTextStyle(badgeTxtStyle);
823OH_Drawing_DestroyTypographyStyle(typoStyle);
824OH_Drawing_DestroyTypographyHandler(handler);
825OH_Drawing_DestroyTypography(typography);
826```
827
828![zh-cn_image_complexArkTsDemo2_2](figures/en_image_superscript.jpg)
829
830### 高对比度
831
832高对比度可将深色文字变黑、浅色文字变白。开发者可选择开启或关闭应用的高对比度文字渲染,或遵循系统设置中的高对比度文字配置。
833
834
835| 接口定义 | 描述 |
836| -------- | -------- |
837| void OH_Drawing_SetTextHighContrast(OH_Drawing_TextHighContrast action) | 设置文字渲染高对比度模式。模式具体可参考[OH_Drawing_TextHighContrast](../reference/apis-arkgraphics2d/capi-drawing-text-global-h.md#oh_drawing_texthighcontrast)。 |
838
839
840示例及示意效果如下所示:
841
842
843```c++
844// 开启APP的文字渲染高对比模式,该模式的优先级要高于系统设置中的高对比度文字配置
845OH_Drawing_SetTextHighContrast(TEXT_APP_ENABLE_HIGH_CONTRAST);
846// 创建一个 TypographyStyle,创建 Typography 时需要使用
847OH_Drawing_TypographyStyle *typoStyle = OH_Drawing_CreateTypographyStyle();
848
849// 设置文字颜色、大小,不设置 TextStyle 会使用 TypographyStyle 中的默认 TextStyle
850OH_Drawing_TextStyle *txtStyle = OH_Drawing_CreateTextStyle();
851OH_Drawing_SetTextStyleColor(txtStyle, OH_Drawing_ColorSetArgb(0xFF, 0x6F, 0xFF, 0xFF));
852OH_Drawing_SetTextStyleFontSize(txtStyle, 100);
853
854// 创建 FontCollection,FontCollection 用于管理字体匹配逻辑
855OH_Drawing_FontCollection *fc = OH_Drawing_CreateSharedFontCollection();
856// 使用 FontCollection 和 之前创建的 TypographyStyle 创建 TypographyCreate
857OH_Drawing_TypographyCreate *handler = OH_Drawing_CreateTypographyHandler(typoStyle, fc);
858
859// 将之前创建的 TextStyle 加入 handler 中
860OH_Drawing_TypographyHandlerPushTextStyle(handler, txtStyle);
861// 设置文本内容,并将文本添加到 handler 中
862const char *text = "Hello World Drawing\n";
863OH_Drawing_TypographyHandlerAddText(handler, text);
864
865OH_Drawing_Typography *typography = OH_Drawing_CreateTypography(handler);
866OH_Drawing_TypographyLayout(typography, 1250);
867// 将文本绘制到画布上
868OH_Drawing_TypographyPaint(typography, canvas, 10, 800);
869
870// 释放内存
871OH_Drawing_DestroyTypographyStyle(typoStyle);
872OH_Drawing_DestroyTextStyle(txtStyle);
873OH_Drawing_DestroyFontCollection(fc);
874OH_Drawing_DestroyTypographyHandler(handler);
875OH_Drawing_DestroyTypography(typography);
876```
877
878| 高对比度设置 | 示意效果 |
879| -------- | -------- |
880| 不开启高对比度 | ![zh-cn_image_highContrast_1](figures/zh-cn_image_highContrast_1.png) |
881| 开启高对比度 | ![zh-cn_image_highContrast_2](figures/zh-cn_image_highContrast_2.png) |
882
883## 样式的拷贝、绘制与显示
884支持拷贝文本样式、段落样式、阴影样式,以便快速复制相关样式作用到不同文字上。
885
886| 接口定义 | 描述 |
887| -------- | -------- |
888| OH_Drawing_TypographyStyle\* OH_Drawing_CopyTypographyStyle(OH_Drawing_TypographyStyle\* style) | 创建一个段落样式的对象副本,用于拷贝一个已有的段落样式对象。 |
889| OH_Drawing_TextStyle* OH_Drawing_CopyTextStyle(OH_Drawing_TextStyle* style) | 创建一个文本样式的对象副本,用于拷贝一个已有的文本样式对象。 |
890| OH_Drawing_TextShadow* OH_Drawing_CopyTextShadow(OH_Drawing_TextShadow* shadow) | 创建一个文本阴影的对象副本,用于拷贝一个已有的文本阴影对象。 |
891
892示例及示意效果如下所示:
893
894```c++
895// 创建一个TypographyStyle,其中创建Typography时需要使用
896OH_Drawing_TypographyStyle *typoStyle = OH_Drawing_CreateTypographyStyle();
897// 配置段落样式包括:使能自动间距、最大行数、省略号样式、省略号文本、对齐方式
898// 使能自动间距
899OH_Drawing_SetTypographyTextAutoSpace(typoStyle, true);
900// 设置段落最大行数为3行
901OH_Drawing_SetTypographyTextMaxLines(typoStyle, 3);
902// 设置省略号模式为尾部省略号
903OH_Drawing_SetTypographyTextEllipsisModal(typoStyle, ELLIPSIS_MODAL_TAIL);
904// 设置省略号文本
905OH_Drawing_SetTypographyTextEllipsis(typoStyle, "...");
906// 设置对齐方式为居中对齐
907OH_Drawing_SetTypographyTextAlign(typoStyle, TEXT_ALIGN_CENTER);
908
909OH_Drawing_TextStyle *txtStyle = OH_Drawing_CreateTextStyle();
910// 设置文字颜色、大小、字重,不设置TextStyle会使用TypographyStyle中的默认TextStyle
911OH_Drawing_SetTextStyleColor(txtStyle, OH_Drawing_ColorSetArgb(0xFF, 0x00, 0x00, 0x00));
912OH_Drawing_SetTextStyleFontSize(txtStyle, 100);
913// 设置文本的装饰线
914// 添加下划线
915OH_Drawing_SetTextStyleDecoration(txtStyle, TEXT_DECORATION_UNDERLINE);
916// 设置装饰线样式为波浪线样式
917OH_Drawing_SetTextStyleDecorationStyle(txtStyle, ARKUI_TEXT_DECORATION_STYLE_WAVY);
918// 设置下划线粗细
919OH_Drawing_SetTextStyleDecorationThicknessScale(txtStyle, 1);
920// 设置下划线颜色为蓝色
921OH_Drawing_SetTextStyleDecorationColor(txtStyle, OH_Drawing_ColorSetArgb(0xFF, 0x00, 0x00, 0xFF));
922
923// 设置阴影的颜色、偏移量、模糊半径
924// 创建阴影对象
925OH_Drawing_TextShadow *shadow = OH_Drawing_CreateTextShadow();
926// 设置阴影偏移量
927OH_Drawing_Point *offset = OH_Drawing_PointCreate(5, 5);
928// 定义阴影模糊半径
929double blurRadius = 4;
930OH_Drawing_SetTextShadow(shadow, OH_Drawing_ColorSetArgb(0xFF, 0xFF, 0x00, 0xFF), offset, blurRadius);
931
932// 拷贝阴影对象
933OH_Drawing_TextShadow *shadowCopy = OH_Drawing_CopyTextShadow(shadow);
934// 将拷贝出的阴影添加到文本样式中
935OH_Drawing_TextStyleAddShadow(txtStyle, shadowCopy);
936
937// 创建FontCollection,FontCollection用于管理字体匹配逻辑
938OH_Drawing_FontCollection *fc = OH_Drawing_CreateSharedFontCollection();
939
940// 使用FontCollection和之前创建的TypographyStyle创建TypographyCreate。TypographyCreate用于创建Typography
941OH_Drawing_TypographyCreate *handler = OH_Drawing_CreateTypographyHandler(typoStyle, fc);
942// 将段落一文本样式添加到handler中
943OH_Drawing_TypographyHandlerPushTextStyle(handler, txtStyle);
944// 将段落一文本添加到handler中
945const char *text = "The text style, paragraph style, and text shadow of the copied text will be exactly the same as those of the original text.";
946OH_Drawing_TypographyHandlerAddText(handler, text);
947// 创建段落一,并将段落一按照排版宽度进行排版
948OH_Drawing_Typography *typography = OH_Drawing_CreateTypography(handler);
949double layoutWidth = 1200;
950OH_Drawing_TypographyLayout(typography, layoutWidth);
951// 将段落一文本绘制到画布上
952double position[2] = {0, 500.0};
953OH_Drawing_TypographyPaint(typography, canvas, position[0], position[1]);
954
955// 生成第二段文本,其中,文本样式和段落样式均由第一段文本拷贝而来
956// 复制文本样式
957OH_Drawing_TextStyle *textsyleCopy = OH_Drawing_CopyTextStyle(txtStyle);
958// 复制段落样式
959OH_Drawing_TypographyStyle *typographyStyleCopy = OH_Drawing_CopyTypographyStyle(typoStyle);
960
961// 使用复制的样式创建段落二,后续可以观察段落一和段落二是否绘制效果一致
962OH_Drawing_TypographyCreate *handlerCopy = OH_Drawing_CreateTypographyHandler(typographyStyleCopy, fc);
963OH_Drawing_TypographyHandlerPushTextStyle(handlerCopy, textsyleCopy);
964OH_Drawing_TypographyHandlerAddText(handlerCopy, text);
965OH_Drawing_Typography *typographyCopy = OH_Drawing_CreateTypography(handlerCopy);
966OH_Drawing_TypographyLayout(typographyCopy, layoutWidth);
967// 将段落二文本绘制到画布上
968double positionCopy[2] = {0, 1200.0};
969OH_Drawing_TypographyPaint(typographyCopy, canvas, positionCopy[0], positionCopy[1]);
970
971// 释放内存
972OH_Drawing_DestroyFontCollection(fc);
973OH_Drawing_DestroyTypographyStyle(typoStyle);
974OH_Drawing_DestroyTextStyle(txtStyle);
975OH_Drawing_DestroyTypographyHandler(handler);
976OH_Drawing_DestroyTypography(typography);
977// 拷贝的段落样式也需要释放内存
978OH_Drawing_DestroyTypographyStyle(typographyStyleCopy);
979// 拷贝的文本样式也需要释放内存
980OH_Drawing_DestroyTextStyle(textsyleCopy);
981OH_Drawing_DestroyTypographyHandler(handlerCopy);
982OH_Drawing_DestroyTypography(typographyCopy);
983```
984
985![zh-cn_image_styleCopy_1](figures/zh-cn_image_styleCopy_1.png)