Introduction to the implementation of skia font drawing

Skia uses a technique called "text manipulation" to implement font drawing. This technology includes multiple components such as font library, text path and glyph object, which allows developers to present characters or text content in the form of paths.

step

The specific implementation process is as follows:

1. Get the font object (SkTypeface) for drawing text.
Obtaining font objects can be achieved through the static methods createFromFile(), createFromStream(), createFromData(), etc. of the SkTypeface class, or by specifying the font family (family name), font style (style) or font name (font name), etc. create.

2. Create a text brush (SkPaint) based on the font object.
A text brush (SkPaint) is an object used to describe the parameters of drawing text, which includes attributes such as font information, font size, color, bold, and italic. These parameters can be set through the constructor of SkPaint or the set*() method.

3. Convert the text content into a text path (SkPath).
In Skia, text content is stored through character sets, so before drawing, characters need to be converted into corresponding text path (SkPath) objects. This process can be realized through SkPath's addText() method.

4. Draw text on the Canvas according to the text path and text brush.
The final step is to combine the text path with the text brush and draw the text on the Canvas. You can use the drawPath() method or drawTextOnPath() method of Canvas to accomplish this process.

sktypeface

SkTypeface is a class used in Skia to represent fonts and implement text drawing. It encapsulates font information and font library management. In Skia, use SkTypeface to support fonts of different types and formats, such as TrueType, OpenType, Type 1, CFF and other formats, and support multiple language environments and character sets. SkTypeface saves font strokes (outline), bitmaps (bitmaps) or vector outlines (vector outlines) and other representations, and text can be drawn by drawing paths or rendering bitmaps.

The SkTypeface class provides a variety of constructors to create font objects, such as according to the font file path, font data block, font family name, font style, etc. When creating a SkTypeface object, you need to select appropriate parameters to set, so as to realize the control of attributes such as different font styles, sizes, and colors. In addition to creating SkTypeface objects, Skia also supports direct use of system built-in fonts, such as using fonts in the system font library or directly using SFUI default fonts.

This article ends.

Guess you like

Origin blog.csdn.net/henysugar/article/details/130699881