[ZT] xft programming documentation

Source: Internet
Author: User
Tags new set

Http://www.linuxfans.org/nuke/modules.php? Name = News & file = article & OP = view & SID = 1981

Cjacker writes'
Xft Programming Technology

I. First, there are several points to note:
1. The font technology has become increasingly transparent to users. The two UIS and Their Derivative suites have completely encapsulated the font technology.
For example, gtk2 uses the pango font engine, and QT also encapsulates the xft and X core fonts. Therefore, you do not need to consider any font implementation issues when developing programs with gtk2 or QT or higher levels.
That is to say, if you want to use xft for application development, you must ensure that the UI suite you are using supports xft unless you are using xlib programming and processing fonts by yourself.

2. xft is responsible for user interfaces, FreeType and X rendering interface tasks, and no font rendering. We can understand that xft is a new set of font management tools and programming interfaces, fontconfig is responsible for xft configuration and can be understood as xft configuration tools, while FreeType and X are actually responsible for font rendering and font implementation. General applications, including xlib program development, there is no need to consider any font rendering issues.
Xft releases the relevant font application programming from xfontset,-MISC-song --------- and other complex fonts.

Ii. Data Types and functions:
Before starting, you must complete several assumptions:
Display * Dis and complete dis initialization.
Int iscreen = defaultscreen (DIS );
Window win =... defines a window.

Define the style xftpattern:
As the name implies, xft is a set of key-value pairs. Each key defines an attribute of the font. xftpattern is used to define a certain font.
Which of the following are commonly used?
Family string font family, which we often call helvetiva and dongwen-song.
Style string font style, such as bold, italic, and bold italic
Pixelsize double font size
Whether the antialias bool type is AA, that is, whether the edge is smooth.

Construct an empty xftpattern method:
Xftpattern * pattern;
Pattern = xftpatterncreate ();

Set attributes for pattern:
Because the attribute of xftpattern is a variety of key-value pairs, there are different attributes to add functions:
Processing string type, using xftpatternaddstring (pattern, "family", "dongwen-song ");
Define family as dongwen-song

Handle int/double type, use xftpatternaddinteger (pattern, "pixelsize", 48 );
Define the size of 48, or use xftpatternadddouble ();

Handle bool type, use xftpatternaddbool (pattern, "antialias", 1 );
Use AA smoothing.

In this way, the font definition can be basically completed.

Matching font xftfontmatch:
Xftresult result;
Xftpattern * xftfontmatch (display * Dis, int iscreen, xftpattern * pattern, xftresult * result );
Return the style closest to the User-Defined style. The user may wish to define the font style. For example, the font is not bold, but the user uses
Xftpatteraddstring (pattern, "style", "bold") defines the bold type. xftfontmatch can be used to check and filter the font style to obtain the legal style closest to the user's definition.

Generate xftfont:
Pointer to font
Xftfont * xftfontopenpattern (display * Dis, xftpattern * pattern)
In the above example, we can use
Xftresult result;
Xftfont * xftfont;
Xftfont = xftfontopenpattern (DIS, xftfontmatch (DIS, iscreen, pattern, & result );

Where to draw? Xftdraw:
Xft needs to know what it should use as the output target. xftdraw is an encapsulation of X-related functions,
Xftdraw * xftdrawcreate (display * Dis, drawable, visual * visual, colormap );

In this example, we can create xftdraw.
Xftdraw * xftdraw;
Xftdraw = xftdrawcreate (DIS, win, defaultvisual (DIS, defascreen screen (DIS), defacolorcolormap (DIS, defaultscreen (DIS )));
Clearly, we need to draw the image in the window created above.

Change draw's target
Void xftdrawchange (xftdraw * draw, drawable );
For example, if there is another win2, we can use
Xftdrawchange (xftdraw, win2) to determine the target win2.
What color is used to output xftcolor?
The color configuration is defined by the following structure:
Typedef struct {
Unsigned short red;
Unsigned short green;
Unsigned short blue;
Unsigned short Alpha;
} Xrendercolor;
Typedef struct _ xftcolor {
Unsigned long pixel;
Xrendercolor color;
} Xftcolor;
Use the xftcolorallocvalue function.

For example:
Xftcolor;
Xrendercolor rendercolor;

Rendercolor. Red = 0 xeeee;
Rendercolor. Green = 0 xaaaa;
Rendercolor. Blue = 0 xdddd;
Rendercolor. Alpha = 0 xFFFF;

Xftcolorallocvalue (DIS, defaultvisual (DIS, defaultscreen (DIS), defacolorcolormap (DIS, defaultscreen (DIS), & rendercolor, & xftcolor );

Output xftdrawstring ??? Xftdrawrect:
Xft has a total of four output text functions. The difference is that the string type is different.
Void
Xftdrawstring8 (xftdraw * D,
Xftcolor * color,
Xftfont * font,
Int X,
Int y,
Xftchar8 * string,
Int Len );

Void
Xftdrawstring16 (xftdraw * draw,
Xftcolor * color,
Xftfont * font,
Int X,
Int y,
Xftchar16 * string,
Int Len );

Void
Xftdrawstring32 (xftdraw * draw,
Xftcolor * color,
Xftfont * font,
Int X,
Int y,
Xftchar32 * string,
Int Len );

Void
Xftdrawstringutf8 (xftdraw * D,
Xftcolor * color,
Xftfont * font,
Int X,
Int y,
Xftchar8 * string,
Int Len );
It doesn't matter what you use in English. For Chinese, you can only use xftdrawstringutf8. This function also provides us with the possibility of simultaneously processing multiple languages.

Iconv_t convutf8;
Convutf8 = iconv_open ("UTF-8", "GBK ");

Char * STR = "test xft ";
Int L1, L2;
Char * pS;
Char Strout [100] = "";
L1 = strlen (STR );
L2 = 99;
PS = Strout;

L1 = iconv (convutf8, (char **) (& Str), & L1, & PS, & l2 );

Converts normal GBK text to utf8.

Xftdrawstringutf8 (xftdraw, & xftcolor, xftfont, 100,100, Strout, strlen (Strout ));
Use xftfont as the font to draw the xftcolor text at the position of X = 100 and Y = 100 in the target window.

Xftdrawrect (xftdraw, & xftcolor, 120,120,100,100 );
Draw a solid rectangle of xftcolor with a width of 120 and a height of 120 until x = 100 and Y = 100 in the target window.

Do not leave a tail:
Xftpatterndestroy (pattern );
Xftdrawdestroy (xftdraw );

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.