1# Class (Font) 2 3<!--Kit: ArkGraphics 2D--> 4<!--Subsystem: Graphics--> 5<!--Owner: @hangmengxin--> 6<!--Designer: @wangyanglan--> 7<!--Tester: @nobuggers--> 8<!--Adviser: @ge-yafang--> 9 10> **说明:** 11> 12> - 本模块首批接口从API version 11开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。 13> 14> - 本模块使用屏幕物理像素单位px。 15> 16> - 本模块为单线程模型策略,需要调用方自行管理线程安全和上下文状态的切换。 17 18描述字型绘制时所使用的属性,如大小、字体等。 19 20## 导入模块 21 22```ts 23import { drawing } from '@kit.ArkGraphics2D'; 24``` 25 26## isSubpixel<sup>12+</sup> 27 28isSubpixel(): boolean 29 30获取字型是否使用次像素渲染。 31 32**系统能力:** SystemCapability.Graphics.Drawing 33 34**返回值:** 35 36| 类型 | 说明 | 37| ------ | -------------------- | 38| boolean | 返回字型是否使用次像素渲染的结果,true表示使用,false表示不使用。 | 39 40**示例:** 41 42```ts 43import {drawing} from '@kit.ArkGraphics2D'; 44 45let font: drawing.Font = new drawing.Font(); 46font.enableSubpixel(true) 47console.info("values=" + font.isSubpixel()); 48``` 49 50## isLinearMetrics<sup>12+</sup> 51 52isLinearMetrics(): boolean 53 54获取字型是否可以线性缩放。 55 56**系统能力:** SystemCapability.Graphics.Drawing 57 58**返回值:** 59 60| 类型 | 说明 | 61| ------ | -------------------- | 62| boolean | 返回字型是否可线性缩放的结果,true表示可线性缩放,false表示不可线性缩放。 | 63 64**示例:** 65 66```ts 67import {drawing} from '@kit.ArkGraphics2D'; 68 69let font: drawing.Font = new drawing.Font(); 70font.enableLinearMetrics(true) 71console.info("values=" + font.isLinearMetrics()); 72``` 73 74## getSkewX<sup>12+</sup> 75 76getSkewX(): number 77 78获取字型在x轴方向上的倾斜度。 79 80**系统能力:** SystemCapability.Graphics.Drawing 81 82**返回值:** 83 84| 类型 | 说明 | 85| ------ | -------------------- | 86| number | 返回字型在x轴方向上的倾斜度。 | 87 88**示例:** 89 90```ts 91import {drawing} from '@kit.ArkGraphics2D'; 92 93let font: drawing.Font = new drawing.Font(); 94font.setSkewX(-1) 95console.info("values=" + font.getSkewX()); 96``` 97 98## isEmbolden<sup>12+</sup> 99 100isEmbolden(): boolean 101 102获取字型是否设置了粗体效果。 103 104**系统能力:** SystemCapability.Graphics.Drawing 105 106**返回值:** 107 108| 类型 | 说明 | 109| ------ | -------------------- | 110| boolean | 返回字型是否设置粗体效果的结果,true表示设置了粗体效果,false表示未设置粗体效果。 | 111 112**示例:** 113 114```ts 115import {drawing} from '@kit.ArkGraphics2D'; 116 117let font: drawing.Font = new drawing.Font(); 118font.enableEmbolden(true); 119console.info("values=" + font.isEmbolden()); 120``` 121 122## getScaleX<sup>12+</sup> 123 124getScaleX(): number 125 126获取字型在x轴方向上的缩放比例。 127 128**系统能力:** SystemCapability.Graphics.Drawing 129 130**返回值:** 131 132| 类型 | 说明 | 133| ------ | -------------------- | 134| number | 返回字型在x轴方向上的缩放比例。 | 135 136**示例:** 137 138```ts 139import {drawing} from '@kit.ArkGraphics2D'; 140 141let font: drawing.Font = new drawing.Font(); 142font.setScaleX(2); 143console.info("values=" + font.getScaleX()); 144``` 145 146## getHinting<sup>12+</sup> 147 148getHinting(): FontHinting 149 150获取字型轮廓效果。 151 152**系统能力:** SystemCapability.Graphics.Drawing 153 154**返回值:** 155 156| 类型 | 说明 | 157| ------ | -------------------- | 158| [FontHinting](arkts-apis-graphics-drawing-e.md#fonthinting12) | 返回字型轮廓效果。 | 159 160**示例:** 161 162```ts 163import {drawing} from '@kit.ArkGraphics2D'; 164 165let font: drawing.Font = new drawing.Font(); 166console.info("values=" + font.getHinting()); 167``` 168 169## getEdging<sup>12+</sup> 170 171getEdging(): FontEdging 172 173获取字型边缘效果。 174 175**系统能力:** SystemCapability.Graphics.Drawing 176 177**返回值:** 178 179| 类型 | 说明 | 180| ------ | -------------------- | 181| [FontEdging](arkts-apis-graphics-drawing-e.md#fontedging12) | 返回字型边缘效果。 | 182 183**示例:** 184 185```ts 186import {drawing} from '@kit.ArkGraphics2D'; 187 188let font: drawing.Font = new drawing.Font(); 189console.info("values=" + font.getEdging()); 190``` 191 192## enableSubpixel 193 194enableSubpixel(isSubpixel: boolean): void 195 196使能字型亚像素级别的文字绘制,显示效果平滑。 197 198**系统能力:** SystemCapability.Graphics.Drawing 199 200**参数:** 201 202| 参数名 | 类型 | 必填 | 说明 | 203| ---------- | ------- | ---- | ------------------------------------------------------------ | 204| isSubpixel | boolean | 是 | 表示是否使能字型亚像素级别的文字绘制。true表示使能,false表示不使能。 | 205 206**错误码:** 207 208以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 209 210| 错误码ID | 错误信息 | 211| ------- | --------------------------------------------| 212| 401 | Parameter error.Possible causes:1.Mandatory parameters are left unspecified;2.Incorrect parameter types. | 213 214**示例:** 215 216```ts 217import { drawing } from '@kit.ArkGraphics2D'; 218 219let font = new drawing.Font(); 220font.enableSubpixel(true); 221``` 222 223## enableEmbolden 224 225enableEmbolden(isEmbolden: boolean): void 226 227使能字型粗体。 228 229**系统能力:** SystemCapability.Graphics.Drawing 230 231**参数:** 232 233| 参数名 | 类型 | 必填 | 说明 | 234| ---------- | ------- | ---- | ----------------------------------------------------- | 235| isEmbolden | boolean | 是 | 表示是否使能字型粗体。true表示使能,false表示不使能。 | 236 237**错误码:** 238 239以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 240 241| 错误码ID | 错误信息 | 242| ------- | --------------------------------------------| 243| 401 | Parameter error.Possible causes:1.Mandatory parameters are left unspecified;2.Incorrect parameter types. | 244 245**示例:** 246 247```ts 248import { drawing } from '@kit.ArkGraphics2D'; 249 250let font = new drawing.Font(); 251font.enableEmbolden(true); 252``` 253 254## enableLinearMetrics 255 256enableLinearMetrics(isLinearMetrics: boolean): void 257 258使能字型的线性缩放。 259 260**系统能力:** SystemCapability.Graphics.Drawing 261 262**参数:** 263 264| 参数名 | 类型 | 必填 | 说明 | 265| --------------- | ------- | ---- | ----------------------------------------------------------- | 266| isLinearMetrics | boolean | 是 | 表示是否使能字型的线性缩放。true表示使能,false表示不使能。 | 267 268**错误码:** 269 270以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 271 272| 错误码ID | 错误信息 | 273| ------- | --------------------------------------------| 274| 401 | Parameter error.Possible causes:1.Mandatory parameters are left unspecified;2.Incorrect parameter types. | 275 276**示例:** 277 278```ts 279import { drawing } from '@kit.ArkGraphics2D'; 280 281let font = new drawing.Font(); 282font.enableLinearMetrics(true); 283``` 284 285## setSize 286 287setSize(textSize: number): void 288 289设置字型大小。 290 291**系统能力:** SystemCapability.Graphics.Drawing 292 293**参数:** 294 295| 参数名 | 类型 | 必填 | 说明 | 296| -------- | ------ | ---- | ---------------- | 297| textSize | number | 是 | 字型大小,该参数为浮点数,为负数时字型大小会被置为0。字型大小为0时,绘制的文字不会显示。| 298 299**错误码:** 300 301以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 302 303| 错误码ID | 错误信息 | 304| ------- | --------------------------------------------| 305| 401 | Parameter error.Possible causes:1.Mandatory parameters are left unspecified;2.Incorrect parameter types;3.Parameter verification failed. | 306 307**示例:** 308 309```ts 310import { drawing } from '@kit.ArkGraphics2D'; 311 312let font = new drawing.Font(); 313font.setSize(5); 314``` 315 316## getSize 317 318getSize(): number 319 320获取字型大小。 321 322**系统能力:** SystemCapability.Graphics.Drawing 323 324**返回值:** 325 326| 类型 | 说明 | 327| ------ | ---------------- | 328| number | 字型大小,浮点数。 | 329 330**示例:** 331 332```ts 333import { drawing } from '@kit.ArkGraphics2D'; 334 335let font = new drawing.Font(); 336font.setSize(5); 337let fontSize = font.getSize(); 338``` 339 340## setTypeface 341 342setTypeface(typeface: Typeface): void 343 344为字型设置字体样式(包括字体名称、粗细、斜体等属性)。 345 346**系统能力:** SystemCapability.Graphics.Drawing 347 348**参数:** 349 350| 参数名 | 类型 | 必填 | 说明 | 351| -------- | --------------------- | ---- | ------ | 352| typeface | [Typeface](arkts-apis-graphics-drawing-Typeface.md) | 是 | 字体样式,包括字体名称、粗细、斜体等属性。 | 353 354**错误码:** 355 356以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 357 358| 错误码ID | 错误信息 | 359| ------- | --------------------------------------------| 360| 401 | Parameter error.Possible causes:1.Mandatory parameters are left unspecified;2.Incorrect parameter types. | 361 362**示例:** 363 364```ts 365import { drawing } from '@kit.ArkGraphics2D'; 366 367let font = new drawing.Font(); 368font.setTypeface(new drawing.Typeface()); 369``` 370 371## getTypeface 372 373getTypeface(): Typeface 374 375获取字体。 376 377**系统能力:** SystemCapability.Graphics.Drawing 378 379**返回值:** 380 381| 类型 | 说明 | 382| --------------------- | ------ | 383| [Typeface](arkts-apis-graphics-drawing-Typeface.md) | 字体。 | 384 385**示例:** 386 387```ts 388import { drawing } from '@kit.ArkGraphics2D'; 389 390let font = new drawing.Font(); 391let typeface = font.getTypeface(); 392``` 393 394## getMetrics 395 396getMetrics(): FontMetrics 397 398获取与字体关联的FontMetrics属性。 399 400**系统能力:** SystemCapability.Graphics.Drawing 401 402**返回值:** 403 404| 类型 | 说明 | 405| --------------------------- | ----------------- | 406| [FontMetrics](arkts-apis-graphics-drawing-i.md#fontmetrics) | FontMetrics属性。 | 407 408**示例:** 409 410```ts 411import { drawing } from '@kit.ArkGraphics2D'; 412 413let font = new drawing.Font(); 414let metrics = font.getMetrics(); 415``` 416 417## measureText 418 419measureText(text: string, encoding: TextEncoding): number 420 421测量文本的宽度。 422 423> **说明:** 424> 425> 此接口用于测量原始字符串的文本宽度,若想测量排版后的文本宽度,建议使用[measure.measureText](../apis-arkui/arkts-apis-uicontext-measureutils.md#measuretext12)替代。 426 427**系统能力:** SystemCapability.Graphics.Drawing 428 429**参数:** 430 431| 参数名 | 类型 | 必填 | 说明 | 432| -------- | ----------------------------- | ---- | ---------- | 433| text | string | 是 | 文本内容。 | 434| encoding | [TextEncoding](arkts-apis-graphics-drawing-e.md#textencoding) | 是 | 编码格式。 | 435 436**返回值:** 437 438| 类型 | 说明 | 439| ------ | ---------------- | 440| number | 文本的宽度,浮点数。 | 441 442**错误码:** 443 444以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 445 446| 错误码ID | 错误信息 | 447| ------- | --------------------------------------------| 448| 401 | Parameter error.Possible causes:1.Mandatory parameters are left unspecified;2.Incorrect parameter types. | 449 450**示例:** 451 452```ts 453import { drawing } from '@kit.ArkGraphics2D'; 454 455let font = new drawing.Font(); 456font.measureText("drawing", drawing.TextEncoding.TEXT_ENCODING_UTF8); 457``` 458 459## measureSingleCharacter<sup>12+</sup> 460 461measureSingleCharacter(text: string): number 462 463测量单个字符的宽度。当前字型中的字体不支持待测量字符时,退化到使用系统字体测量字符宽度。 464 465**系统能力:** SystemCapability.Graphics.Drawing 466 467**参数** 468 469| 参数名 | 类型 | 必填 | 说明 | 470| ------ | ------------------- | ---- | ----------- | 471| text | string | 是 | 待测量的单个字符,字符串的长度必须为1。 | 472 473**返回值:** 474 475| 类型 | 说明 | 476| ------ | ---------------- | 477| number | 字符的宽度,浮点数。 | 478 479**错误码:** 480 481以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 482 483| 错误码ID | 错误信息 | 484| ------- | --------------------------------------------| 485| 401 | Parameter error.Possible causes:1.Mandatory parameters are left unspecified;2.Incorrect parameter types;3.Parameter verification failed. | 486 487**示例:** 488 489```ts 490import { RenderNode } from '@kit.ArkUI'; 491import { drawing } from '@kit.ArkGraphics2D'; 492 493class DrawingRenderNode extends RenderNode { 494 draw(context : DrawContext) { 495 const canvas = context.canvas; 496 const font = new drawing.Font(); 497 font.setSize(20); 498 let width = font.measureSingleCharacter("你"); 499 } 500} 501``` 502 503## measureSingleCharacterWithFeatures<sup>20+</sup> 504 505measureSingleCharacterWithFeatures(text: string, features: Array\<FontFeature\>): number 506 507测量单个字符的宽度,字符带有字体特征。当前字型中的字体不支持待测量字符时,退化到使用系统字体测量字符宽度。 508 509**系统能力:** SystemCapability.Graphics.Drawing 510 511**参数** 512 513| 参数名 | 类型 | 必填 | 说明 | 514| ------ | ------------------- | ---- | ----------- | 515| text | string | 是 | 待测量的单个字符。字符串长度必须为1。 | 516| features | Array\<[FontFeature](arkts-apis-graphics-drawing-i.md#fontfeature20)\> | 是 | 字体特征对象数组。参数为空数组时使用TTF(TrueType Font)文件中预设的字体特征。| 517 518**返回值:** 519 520| 类型 | 说明 | 521| ------ | ---------------- | 522| number | 字符的宽度,浮点数,单位为px。 | 523 524**错误码:** 525 526以下错误码的详细介绍请参见[图形绘制与显示错误码](../apis-arkgraphics2d/errorcode-drawing.md)。 527 528| 错误码ID | 错误信息 | 529| ------- | --------------------------------------------| 530| 25900001 | Parameter error. Possible causes: Incorrect parameter range. | 531 532**示例:** 533 534```ts 535import { RenderNode } from '@kit.ArkUI'; 536import { drawing } from '@kit.ArkGraphics2D'; 537 538class DrawingRenderNode extends RenderNode { 539 draw(context : DrawContext) { 540 const font = new drawing.Font(); 541 font.setSize(20); 542 let fontFeatures : Array<drawing.FontFeature> = []; 543 fontFeatures.push({name: 'calt', value: 0}); 544 let width = font.measureSingleCharacterWithFeatures("你", fontFeatures); 545 } 546} 547``` 548 549## setScaleX<sup>12+</sup> 550 551setScaleX(scaleX: number): void 552 553设置字型对象在x轴上的缩放比例。 554 555**系统能力:** SystemCapability.Graphics.Drawing 556 557**参数:** 558 559| 参数名 | 类型 | 必填 | 说明 | 560| -------- | ----------------------------- | ---- | ---------- | 561| scaleX | number | 是 | 文本在x轴上的缩放比例,该参数为浮点数。 | 562 563**错误码:** 564 565以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 566 567| 错误码ID | 错误信息 | 568| ------- | --------------------------------------------| 569| 401 | Parameter error.Possible causes:1.Mandatory parameters are left unspecified;2.Incorrect parameter types. | 570 571**示例:** 572 573```ts 574import { RenderNode } from '@kit.ArkUI'; 575import { common2D, drawing } from '@kit.ArkGraphics2D'; 576 577class DrawingRenderNode extends RenderNode { 578 draw(context : DrawContext) { 579 const canvas = context.canvas; 580 const pen = new drawing.Pen(); 581 pen.setStrokeWidth(5); 582 pen.setColor({alpha: 255, red: 255, green: 0, blue: 0}); 583 canvas.attachPen(pen); 584 let font = new drawing.Font(); 585 font.setSize(100); 586 font.setScaleX(2); 587 const textBlob = drawing.TextBlob.makeFromString("hello", font, drawing.TextEncoding.TEXT_ENCODING_UTF8); 588 canvas.drawTextBlob(textBlob, 200, 200); 589 } 590} 591``` 592 593## setSkewX<sup>12+</sup> 594 595setSkewX(skewX: number): void 596 597设置字型对象在x轴上的倾斜比例。 598 599**系统能力:** SystemCapability.Graphics.Drawing 600 601**参数:** 602 603| 参数名 | 类型 | 必填 | 说明 | 604| -------- | ----------------------------- | ---- | ---------- | 605| skewX | number | 是 | 文本在x轴上的倾斜比例,正数表示往左边倾斜,负数表示往右边倾斜,该参数为浮点数。 | 606 607**错误码:** 608 609以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 610 611| 错误码ID | 错误信息 | 612| ------- | --------------------------------------------| 613| 401 | Parameter error.Possible causes:1.Mandatory parameters are left unspecified;2.Incorrect parameter types. | 614 615**示例:** 616 617```ts 618import { RenderNode } from '@kit.ArkUI'; 619import { common2D, drawing } from '@kit.ArkGraphics2D'; 620 621class DrawingRenderNode extends RenderNode { 622 draw(context : DrawContext) { 623 const canvas = context.canvas; 624 const pen = new drawing.Pen(); 625 pen.setStrokeWidth(5); 626 pen.setColor({alpha: 255, red: 255, green: 0, blue: 0}); 627 canvas.attachPen(pen); 628 let font = new drawing.Font(); 629 font.setSize(100); 630 font.setSkewX(1); 631 const textBlob = drawing.TextBlob.makeFromString("hello", font, drawing.TextEncoding.TEXT_ENCODING_UTF8); 632 canvas.drawTextBlob(textBlob, 200, 200); 633 } 634} 635``` 636 637## setEdging<sup>12+</sup> 638 639setEdging(edging: FontEdging): void 640 641设置字型边缘效果。 642 643**系统能力:** SystemCapability.Graphics.Drawing 644 645**参数:** 646 647| 参数名 | 类型 | 必填 | 说明 | 648| -------- | ----------------------------- | ---- | ---------- | 649| edging | [FontEdging](arkts-apis-graphics-drawing-e.md#fontedging12) | 是 | 字型边缘效果。 | 650 651**错误码:** 652 653以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 654 655| 错误码ID | 错误信息 | 656| ------- | --------------------------------------------| 657| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified;2. Incorrect parameter types; 3. Parameter verification failed. | 658 659**示例:** 660 661```ts 662import { drawing } from '@kit.ArkGraphics2D'; 663 664let font = new drawing.Font(); 665font.setEdging(drawing.FontEdging.SUBPIXEL_ANTI_ALIAS); 666``` 667 668## setHinting<sup>12+</sup> 669 670setHinting(hinting: FontHinting): void 671 672设置字型轮廓效果。 673 674**系统能力:** SystemCapability.Graphics.Drawing 675 676**参数:** 677 678| 参数名 | 类型 | 必填 | 说明 | 679| -------- | ----------------------------- | ---- | ---------- | 680| hinting | [FontHinting](arkts-apis-graphics-drawing-e.md#fonthinting12) | 是 | 字型轮廓效果。 | 681 682**错误码:** 683 684以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 685 686| 错误码ID | 错误信息 | 687| ------- | --------------------------------------------| 688| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified;2. Incorrect parameter types; 3. Parameter verification failed. | 689 690**示例:** 691 692```ts 693import { drawing } from '@kit.ArkGraphics2D'; 694 695let font = new drawing.Font(); 696font.setHinting(drawing.FontHinting.FULL); 697``` 698 699## countText<sup>12+</sup> 700 701countText(text: string): number 702 703获取文本所表示的字符数量。 704 705**系统能力:** SystemCapability.Graphics.Drawing 706 707**参数:** 708 709| 参数名 | 类型 | 必填 | 说明 | 710| -------- | ----------------------------- | ---- | ---------- | 711| text | string | 是 | 文本内容。 | 712 713**返回值:** 714 715| 类型 | 说明 | 716| ------ | ---------------- | 717| number | 返回文本所表示的字符数量,整数。 | 718 719**错误码:** 720 721以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 722 723| 错误码ID | 错误信息 | 724| ------- | --------------------------------------------| 725| 401 | Parameter error.Possible causes:1.Mandatory parameters are left unspecified;2.Incorrect parameter types. | 726 727**示例:** 728 729```ts 730import { drawing } from '@kit.ArkGraphics2D'; 731 732let font = new drawing.Font(); 733let resultNumber: number = font.countText('ABCDE'); 734console.info("count text number: " + resultNumber); 735``` 736 737## setBaselineSnap<sup>12+</sup> 738 739setBaselineSnap(isBaselineSnap: boolean): void 740 741当前画布矩阵轴对齐时,设置字型基线是否与像素对齐。 742 743**系统能力:** SystemCapability.Graphics.Drawing 744 745**参数:** 746 747| 参数名 | 类型 | 必填 | 说明 | 748| --------------- | ------- | ---- | ---------------------------------------- | 749| isBaselineSnap | boolean | 是 | 指示字型基线是否和像素对齐,true表示对齐,false表示不对齐。 | 750 751**错误码:** 752 753以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 754 755| 错误码ID | 错误信息 | 756| ------- | --------------------------------------------| 757| 401 | Parameter error.Possible causes:1.Mandatory parameters are left unspecified;2.Incorrect parameter types. | 758 759**示例:** 760 761```ts 762import { drawing } from '@kit.ArkGraphics2D'; 763 764let font : drawing.Font = new drawing.Font(); 765font.setBaselineSnap(true); 766console.info("drawing font isBaselineSnap: " + font.isBaselineSnap()); 767``` 768 769## isBaselineSnap()<sup>12+</sup> 770 771isBaselineSnap(): boolean 772 773当前画布矩阵轴对齐时,获取字型基线是否与像素对齐的结果。 774 775**系统能力:** SystemCapability.Graphics.Drawing 776 777**返回值:** 778 779| 类型 | 说明 | 780| ------ | ---------------- | 781| boolean | 返回字型基线是否与像素对齐,true为对齐,false为没有对齐。 | 782 783**示例:** 784 785```ts 786import { drawing } from '@kit.ArkGraphics2D'; 787 788let font : drawing.Font = new drawing.Font(); 789font.setTypeface(new drawing.Typeface()); 790font.setBaselineSnap(true); 791console.info("drawing font isBaselineSnap: " + font.isBaselineSnap()); 792``` 793 794## setEmbeddedBitmaps<sup>12+</sup> 795 796setEmbeddedBitmaps(isEmbeddedBitmaps: boolean): void 797 798设置字型是否转换成位图处理。 799 800**系统能力:** SystemCapability.Graphics.Drawing 801 802**参数:** 803 804| 参数名 | 类型 | 必填 | 说明 | 805| -------- | ------ | ---- | ---------------- | 806| isEmbeddedBitmaps | boolean | 是 | 设置字型是否转换成位图处理,true表示转换成位图处理,false表示不转换成位图处理。 | 807 808**错误码:** 809 810以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 811 812| 错误码ID | 错误信息 | 813| ------- | --------------------------------------------| 814| 401 | Parameter error.Possible causes:1.Mandatory parameters are left unspecified;2.Incorrect parameter types. | 815 816**示例:** 817 818```ts 819import { drawing } from '@kit.ArkGraphics2D'; 820 821let font : drawing.Font = new drawing.Font(); 822font.setTypeface(new drawing.Typeface()); 823font.setEmbeddedBitmaps(false); 824console.info("draw isEmbeddedBitmaps: " + font.isEmbeddedBitmaps()); 825``` 826 827## isEmbeddedBitmaps()<sup>12+</sup> 828 829isEmbeddedBitmaps(): boolean 830 831获取字型是否转换成位图处理的结果。 832 833**系统能力:** SystemCapability.Graphics.Drawing 834 835**返回值:** 836 837| 类型 | 说明 | 838| ------ | ---------------- | 839| boolean | 返回字型是否转换成位图处理结果,true表示转换成位图处理,false表示不转换成位图处理。 | 840 841**示例:** 842 843```ts 844import { drawing } from '@kit.ArkGraphics2D'; 845 846let font : drawing.Font = new drawing.Font(); 847font.setTypeface(new drawing.Typeface()); 848font.setEmbeddedBitmaps(true); 849console.info("draw isEmbeddedBitmaps: " + font.isEmbeddedBitmaps()); 850``` 851 852## setForceAutoHinting<sup>12+</sup> 853 854setForceAutoHinting(isForceAutoHinting: boolean): void 855 856设置是否自动调整字型轮廓。 857 858**系统能力:** SystemCapability.Graphics.Drawing 859 860**参数:** 861 862| 参数名 | 类型 | 必填 | 说明 | 863| -------- | ------ | ---- | ---------------- | 864| isForceAutoHinting | boolean | 是 | 是否自动调整字型轮廓,true为自动调整,false为不自动调整。 | 865 866**错误码:** 867 868以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 869 870| 错误码ID | 错误信息 | 871| ------- | --------------------------------------------| 872| 401 | Parameter error.Possible causes:1.Mandatory parameters are left unspecified;2.Incorrect parameter types. | 873 874**示例:** 875 876```ts 877import { drawing } from '@kit.ArkGraphics2D'; 878 879let font : drawing.Font = new drawing.Font(); 880font.setTypeface(new drawing.Typeface()); 881font.setForceAutoHinting(false); 882console.info("drawing isForceAutoHinting: " + font.isForceAutoHinting()); 883``` 884 885## isForceAutoHinting<sup>12+</sup> 886 887isForceAutoHinting(): boolean 888 889获取字型轮廓是否自动调整的结果。 890 891**系统能力:** SystemCapability.Graphics.Drawing 892 893**返回值:** 894 895| 类型 | 说明 | 896| ------ | ---------------- | 897| boolean | 返回字型轮廓是否自动调整,true为自动调整,false为不自动调整。 | 898 899**示例:** 900 901```ts 902import { drawing } from '@kit.ArkGraphics2D'; 903 904let font : drawing.Font = new drawing.Font(); 905font.setTypeface(new drawing.Typeface()); 906font.setForceAutoHinting(false); 907console.info("drawing isForceAutoHinting: " + font.isForceAutoHinting()); 908``` 909 910## getWidths<sup>12+</sup> 911 912getWidths(glyphs: Array\<number>): Array\<number> 913 914获取字形数组中每个字形对应的宽度。 915 916**系统能力:** SystemCapability.Graphics.Drawing 917 918**参数:** 919 920| 参数名 | 类型 | 必填 | 说明 | 921| -------- | --------------------- | ---- | ------ | 922| glyphs | Array\<number> | 是 | 字形索引数组,可由[textToGlyphs](#texttoglyphs12)生成。 | 923 924**返回值:** 925 926| 类型 | 说明 | 927| ------ | ---------------- | 928| Array\<number> | 返回字形宽度数组。 | 929 930**错误码:** 931 932以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 933 934| 错误码ID | 错误信息 | 935| ------- | --------------------------------------------| 936| 401 | Parameter error.Possible causes:1.Mandatory parameters are left unspecified;2.Incorrect parameter types. | 937 938**示例:** 939 940```ts 941import { drawing } from '@kit.ArkGraphics2D'; 942 943let font: drawing.Font = new drawing.Font(); 944let text: string = 'hello world'; 945let glyphs: number[] = font.textToGlyphs(text); 946let fontWidths: Array<number> = font.getWidths(glyphs); 947for (let index = 0; index < fontWidths.length; index++) { 948 console.info("get fontWidths[", index, "]:", fontWidths[index]); 949} 950``` 951 952## textToGlyphs<sup>12+</sup> 953 954textToGlyphs(text: string, glyphCount?: number): Array\<number> 955 956将文本转换为字形索引。 957 958**系统能力:** SystemCapability.Graphics.Drawing 959 960**参数:** 961 962| 参数名 | 类型 | 必填 | 说明 | 963| -------- | ----------------------------- | ---- | ---------- | 964| text | string | 是 | 文本字符串。 | 965| glyphCount | number | 否 | 文本表示的字符数量,必须与[countText](#counttext12)获取的值相等,默认为text的字符数量,该参数为整数。 | 966 967**返回值:** 968 969| 类型 | 说明 | 970| ------ | ---------------- | 971| Array\<number> | 返回转换得到的字形索引数组。 | 972 973**错误码:** 974 975以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 976 977| 错误码ID | 错误信息 | 978| ------- | --------------------------------------------| 979| 401 | Parameter error.Possible causes:1.Mandatory parameters are left unspecified;2.Incorrect parameter types. | 980 981**示例:** 982 983```ts 984import { drawing } from '@kit.ArkGraphics2D'; 985 986let font : drawing.Font = new drawing.Font(); 987let text : string = 'hello world'; 988let glyphs : number[] = font.textToGlyphs(text); 989console.info("drawing text toglyphs OnTestFunction num = " + glyphs.length ); 990``` 991 992## getBounds<sup>18+</sup> 993 994getBounds(glyphs: Array\<number>): Array\<common2D.Rect> 995 996获取字形数组中每个字形的边界矩形。 997 998**系统能力:** SystemCapability.Graphics.Drawing 999 1000**参数:** 1001 1002| 参数名 | 类型 | 必填 | 说明 | 1003| -------- | --------------------- | ---- | ------ | 1004| glyphs | Array\<number> | 是 | 字形索引数组,可由[textToGlyphs](#texttoglyphs12)生成。 | 1005 1006**返回值:** 1007 1008| 类型 | 说明 | 1009| ------ | ---------------- | 1010| Array\<[common2D.Rect](js-apis-graphics-common2D.md#rect)> | 返回字形边界矩形数组。 | 1011 1012**示例:** 1013 1014```ts 1015import { common2D, drawing } from '@kit.ArkGraphics2D'; 1016 1017let font: drawing.Font = new drawing.Font(); 1018let text: string = 'hello world'; 1019let glyphs: number[] = font.textToGlyphs(text); 1020let fontBounds: Array<common2D.Rect> = font.getBounds(glyphs); 1021for (let index = 0; index < fontBounds.length; index++) { 1022 console.info("get fontWidths[", index, "] left:", fontBounds[index].left, " top:", fontBounds[index].top, 1023 " right:", fontBounds[index].right, " bottom:", fontBounds[index].bottom); 1024} 1025``` 1026 1027## getTextPath<sup>18+</sup> 1028 1029getTextPath(text: string, byteLength: number, x: number, y: number): Path 1030 1031获取文字的轮廓路径。 1032 1033**系统能力:** SystemCapability.Graphics.Drawing 1034 1035**参数:** 1036 1037| 参数名 | 类型 | 必填 | 说明 | 1038| ------ | ------------------------------------------------ | ---- | ---------------------- | 1039| text | string | 是 | 表示存储UTF-8 文本编码的字符。| 1040|byteLength| number | 是 | 表示要获取对应文本路径的字节长度,按传入的字节长度和实际的文本字节大小之间的最小值来获取对应的文本路径。| 1041| x | number | 是 | 表示文本在绘图区域内以原点为起始位置的X坐标。| 1042| y | number | 是 | 表示文本在绘图区域内以原点为起始位置的Y坐标。| 1043 1044**返回值:** 1045 1046| 类型 | 说明 | 1047| ------ | ---------------- | 1048| [Path](arkts-apis-graphics-drawing-Path.md) | 返回获取到的文本的路径轮廓。 | 1049 1050**错误码:** 1051 1052以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 1053 1054| 错误码ID | 错误信息 | 1055| ------- | --------------------------------------------| 1056| 401 | Parameter error.Possible causes:1.Mandatory parameters are left unspecified;2.Incorrect parameter types;3.Parameter verification failed. | 1057 1058**示例:** 1059 1060```ts 1061import { drawing } from '@kit.ArkGraphics2D'; 1062import { buffer } from '@kit.ArkTS'; 1063import { RenderNode } from '@kit.ArkUI'; 1064 1065class DrawingRenderNode extends RenderNode { 1066 draw(context : DrawContext) { 1067 const canvas = context.canvas; 1068 let font = new drawing.Font(); 1069 font.setSize(50); 1070 let myString: string = "你好, HarmonyOS"; 1071 let length: number = myString.length; 1072 let path = font.getTextPath(myString, length, 0, 100); 1073 canvas.drawPath(path); 1074 } 1075} 1076``` 1077 1078## createPathForGlyph<sup>18+</sup> 1079 1080createPathForGlyph(index: number): Path 1081 1082获取指定字形的路径轮廓。 1083 1084**系统能力:** SystemCapability.Graphics.Drawing 1085 1086**参数:** 1087 1088| 参数名 | 类型 | 必填 | 说明 | 1089| -------- | --------------------- | ---- | ------ | 1090| index | number | 是 | 字形索引。 | 1091 1092**返回值:** 1093 1094| 类型 | 说明 | 1095| ------ | ---------------- | 1096| [Path](arkts-apis-graphics-drawing-Path.md) | 返回指定字形的路径轮廓。 | 1097 1098**示例:** 1099 1100```ts 1101import { FrameNode, NodeController, RenderNode } from '@kit.ArkUI'; 1102import { drawing } from '@kit.ArkGraphics2D'; 1103 1104class DrawingRenderNode extends RenderNode { 1105 draw(context : DrawContext) { 1106 const canvas = context.canvas; 1107 let font = new drawing.Font(); 1108 font.setSize(50) 1109 let text: string = '你好'; 1110 let glyphs: number[] = font.textToGlyphs(text); 1111 for (let index = 0; index < glyphs.length; index++) { 1112 let path: drawing.Path = font.createPathForGlyph(glyphs[index]) 1113 canvas.drawPath(path) 1114 } 1115 } 1116} 1117``` 1118 1119## setThemeFontFollowed<sup>15+</sup> 1120 1121setThemeFontFollowed(followed: boolean): void 1122 1123设置字型中的字体是否跟随主题字体。设置跟随主题字体后,若系统启用主题字体并且字型未被设置字体,字型会使用该主题字体。 1124 1125**系统能力:** SystemCapability.Graphics.Drawing 1126 1127**参数:** 1128 1129| 参数名 | 类型 | 必填 | 说明 | 1130| -------- | ------ | ---- | ---------------- | 1131| followed | boolean | 是 | 字型中的字体是否跟随主题字体,true表示跟随主题字体,false表示不跟随主题字体。 | 1132 1133**错误码:** 1134 1135以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 1136 1137| 错误码ID | 错误信息 | 1138| ------- | --------------------------------------------| 1139| 401 | Parameter error.Possible causes:1.Mandatory parameters are left unspecified;2.Incorrect parameter types. | 1140 1141**示例:** 1142 1143```ts 1144import { drawing } from '@kit.ArkGraphics2D'; 1145 1146let font : drawing.Font = new drawing.Font(); 1147font.setThemeFontFollowed(true); 1148console.info("font is theme font followed: " + font.isThemeFontFollowed()); 1149``` 1150 1151## isThemeFontFollowed()<sup>15+</sup> 1152 1153isThemeFontFollowed(): boolean 1154 1155获取字型中的字体是否跟随主题字体。默认不跟随。 1156 1157**系统能力:** SystemCapability.Graphics.Drawing 1158 1159**返回值:** 1160 1161| 类型 | 说明 | 1162| ------ | ---------------- | 1163| boolean | 返回字型中的字体是否跟随主题字体的结果,true表示跟随主题字体,false表示不跟随主题字体。 | 1164 1165**示例:** 1166 1167```ts 1168import { drawing } from '@kit.ArkGraphics2D'; 1169 1170let font : drawing.Font = new drawing.Font(); 1171font.setThemeFontFollowed(true); 1172console.info("font is theme font followed: " + font.isThemeFontFollowed()); 1173```