Use the TrueType font library in the VxWorks System

Source: Internet
Author: User
Tags printable characters

Summary: To display high-quality and flexible characters (including various deformation operations on the glyphs), WYSIWYG can be printed, the traditional method of using dot matrix font library cannot meet the requirements. There is a good method-using TrueType font library can solve the above two problems at the same time. this article introduces the basic principle of TrueType technology and how to use FreeType to apply TrueType Character Library in the Vxworks system. In practical application, this technology solves the above problems well, however, this technology can be easily transplanted to many other systems, such as Linux.
Keywords: VxWorks; TrueType; ugl font drive; print

Compared with the traditional dot matrix font, the TrueType font will at least bring about the following benefits: it can achieve stepless enlargement or reduction of characters with high quality, high-quality operations such as rotation and skew of characters (1) for easy "What you see is what you get ".
 
Figure 1 Application
Thanks to the above advantages, it is best to use the TrueType font library in many places that have special operation requirements on fonts. In Windows, the TrueType font library is used now.

L. Basics of TrueType font
1.1 about TrueType
The TrueType font is a contour font. In the true-type font, the font information is described by using a series of vertices. these points are connected either through a straight line segment or through a quadratic besell curve to form a glyph profile. figure 2 shows an example of the Chinese character "dry.
 
Figure 2 contour line example
Figure 2 the left-side figure is formed by directly connecting the "dry" point in the font, where the circle represents the verbose point: the small circle represents the straight line segment or the endpoint of the besell curve, A large circle indicates the control point of the besell curve.
The point processing is like this when the contour is formed: assume that the starting point P0 (x0, y0) (it must be the endpoint, that is, a small circle) is used, and find the next point P1 (x1, y1 ). if P1 is also the endpoint, use a straight line segment to connect P0 and P1, and then use P1 as the new start point to continue to connect the remaining points; otherwise, p is the control point of the beiser curve, and then find the next point P2 (X2, y2 ). if P2 is the endpoint, P0, P1, and P2 are connected using the besell curve, and P2 is used as the new start point to continue to connect the remaining points; otherwise, the next point P3 (X3, Y3) is found ), calculate the point PX of P2 and P3, and connect P0, P1, and PX with the besell curve. PX serves as the new start point to continue to connect the remaining points. figure 2 the graph on the right is the actual Shape Contour formed by adding the secondary besell curve.
After the contour is obtained, fill it with the desired character bitmap. The rest of the work is very simple. You can draw characters through hitting or textures.

1.2 introduction to FreeType
It is also difficult to use the TrueType font library: You need to understand the format of the TrueType font library. In this way, the font information can be extracted correctly, which is difficult and troublesome for most users. however, with FreeType, an open source shared software (which can be downloaded from the Internet), you do not have to do this yourself. freeType can be compiled and used on many platforms (such as Windows, Linux, and VxWorks ). using the APIS provided by FreeType, you can easily obtain the glyph information and form bitmap.

1.3 Use FreeType
FreeType provides a wide range of APIs. For more information, see the documentation.
The following describes how to use FreeType APIs to draw a character (only list function names ):
......
/* Initialize the FreeType library */
Ft_init_freetype;
/* Create a font, such as and */
Ft_new_face:
/* Specify the encoding of the Character Glyphs, such as Unicode and gb2312 * |
Ft_select charmap
......
/* Obtain the position of the font character in the font based on character encoding */
Ft_get_char_index:
/* Get the font Description Based on the position of the font, that is, the font contour */
Ft_load_glyph:
/* Fill the contour into a bitmap */
Ft_render_glyph:
/* Use a user-defined function to draw a bitmap */
Drawimage;
......
/* Undo font */
Ft_done_face:
/* Undo the FreeType library */
Ft_done_freetype;
......

Of course, the application in the actual system is not that simple, but the basic process is as follows.

2. ugl font drive

Ugl is the foundation of Zinc. First, add TrueType font support to the ugl font driver of VxWorks:
1) write the driver code according to the ugl font-driven interface standard of VxWorks.
2) Compile ugl using the command line. the ugl document describes how to compile ugl using line commands, and how to modify related files (such as uglnit) to add new font support. h) method.
The existing ugl font driver only provides the interface for horizontal character display, in order to be more flexible to display characters, you can add function pointer UGL-STATUS (* textdrawfree) in the driver interface ); to support printing, you can add a function pointer UGL-STATUS (* textprint ). of course, if you want to call these two functions, you have to go to ugl. h, and add the corresponding function definition to uglfont1.c.

Typedef struct ugl-font-driver
{
/* Original driver interface */
......
/* Newly added Driver Interface */
Ugl_status (* textdrawfree)
(Structu ug_lgc * PGC,
Const char * text,
Unsigned long length,
Const ugl_tt_param * PARAM );
Ugl_status (* textprint) (struct ugl_font_driver * pfontdriver,
Const char * pfacename.
File * OUTFILE,
Const char * text,
Int length,
Const ugl_tt_param * PARAM );
} UGL-FONT-DRIVER;
The structure uglrr_param in textdrawfree defines some parameters to be used, such as the size, foreground and background color, rotation angle, and tilt degree.
In addition, to increase the character display speed, you can use the following method:
1) Adjust the TrueType font to the memory. Use ft_new_memory_face to replace ft_new_face;
2) use the cache function provided by FreeType. For detailed usage, see the documentation provided by FreeType.
3) create a cache for fixed-size fonts (generally used on the interface, which requires high speed). The following is an example:

Assume that the cache structure is as follows:

Struct tt_cache _
{
/* Code_aray stores character encoding */
Unsigned long code_aray [tt_cache_num];
/* Cache_glyph stores the corresponding bitmap */
Struct tt_glyph_cache_glyph [tt_cache_num];
};
Tt_cache_num is the number of characters that need to be set up in the cache. Generally, the cache should contain ASCII printable characters, primary characters in Chinese characters, and some common symbols.
1) Suppose tt_cache 1> code_aray has stored the character encoding that requires the cache to be established. First, sort it (using the C-language qsort ):
Qsort (tt_cache-> code_array, tt_cache_num, sizeof (unsignedlong), compare );
2) generate the corresponding bitmap for all characters.
3) perform the following steps when you need to find the font characters during system operation:
① Use bsearch to search for character encoding in tt_cache 1> code_aray.
② If any, the corresponding bitmap is also found.
③ If not found, go to the font.
4) The cache must be destroyed when the font is destroyed.
Note that steps 1 and 2 are performed at system startup. If Step 2 is too long, skip step 2 ), you only need to change step 3 ):
① Use bsearch to search for character encoding in tt_cache 1> code_aray.
② If yes, check whether the corresponding bitmap exists. If yes, the corresponding bitmap is also found. If not, generate a bitmap based on the font and add it to the cache.
③ If not found, go to the font.
Practice has proved that this method is better.

3. Print

The following describes how to use the ugl font driver in the window system zinc to implement "WYSIWYG" in printing ".
The printing process in zinc is: generate a PS (postscript) file, and then send the PS file to the printer port for printing. So we will only discuss how to generate PS files.
Because the existing system zinc does not support Chinese character printing, it must return to ugl to draw Chinese characters by hitting. this is why the ugl_status (* textprint) function pointer must be added to implement the ugl font drive.
The parameter meaning in textprint is:
Pfacename, such as and;
OUTFILE points to the output target of the print: PS file.
Modify zafprinter in zinc and call the print function in ugl when you want to print characters. The print part in ugl is basically the same as that displayed on the screen. The difference is to display the screen (or memory bitmap) the hitting (or Paster) command is replaced by the hitting (or Paster) command for the writer in the PS file.
Since both the print and screen display have different output targets: the former is a display (if it is a memory bitmap) and the latter is a PS file. in this way, it is easy to implement wysisyg-as long as the coordinate unit is set to inch (or 1‰ of the inch. here is a tip. Because Chinese characters are printed by hitting, this will make the PS file relatively large. to reduce the number of PS files, you can draw a line to replace the continuous hitting. because the resolution of the printer is very high, the effect will be obvious: Generally, you can reduce the PS file to the original 1/5, which will greatly reduce the time to write the printer port.

4. Conclusion

The TrueType font library method in VxWorks has been successfully applied to VxWorks embedded Human geographic information systems and can be easily transplanted to many other systems.

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.