Exploration of FreeType Library

Source: Internet
Author: User
Tags api manual vector font

Text display depends on the font library. The general font library can be divided into dot matrix font, stroke font, and contour font.

Dot Matrix Font: The disadvantages are obvious. operations such as scaling and rotation are relatively complicated and the effects are not satisfactory. Most of them are used in the embedded industry (basically abandoned). Common formats include BDF, PCF, fnt, HbF, HZF, etc.

Stroke Font: not discussed.

Contour Font: The vector font. The font contour and filling are used to display the font. The advantage is obvious, rendering and scaling are easier, but the efficiency is relatively low (compared with embedded)

 

To put it simply, FreeType provides a solution for font fonts and supports operations such as text font rendering. It is mainly written in C and cross-platform, it is possible for many embedded systems that do not support the vector font format to use the embedded font, and the efficiency is not low.

The basic process is:
Load the font library file-> Search for the text index to be displayed-> rendering operations (if reversed)-> process as bitmap data-> display

FreeType http://freetype.sourceforge.net/index2.html

The following is an example of displaying a Chinese font in xp. Download the source code from the official website .. \ freetype-2.4.2 \ builds \ Win32 \ vc2008 \ open the project, compile as a link library, create vs2008 MFCProgramLoad the freetype242.lib library.

 

Simply consider, draw a Chinese character directly in the draw function of MFC. Easy to display and draw Chinese characters using GDI +. Therefore, perform initialization and other operations on GDI + (do not discuss the knowledge of GDI +. If you are not clear about it, leave a message or ask for the GDI + document, you can also search online)

Add header file declaration in view. h

 

# Include <ft2build. h> # include ft_freetype_h

 

 

Add member variables in view. h

 

 
Public: ft_library Library; ft_face face;

 

 

In the view. cpp constructor, add

 

 
// Initialize the database bool berror = ft_init_freetype (& library); If (! Berror) {// whether Initialization is successful} // load a font file. Here, berror = ft_new_face (library, "C :\\ WINDOWS \ fonts \ simhei. TTF ", 0, & face); If (berror = ft_err_unknown_file_format) {// indicates that this file can be opened and read, but this font format is not supported} else if (berror) {// other errors} // set to Unicode. The default value is ft_select_charmap (face, ft_encoding_unicode). // set the font character width and resolution to berror = ft_set_char_size (face, 0, 16*64,300,300 );

 

 

Add: ondraw (CDC * PDC)Code

 

 
Bool berror; wchar_t wchar = _ T ('bo'); // find the 'good' Character index ft_uint glyph_index = ft_get_char_index (face, wchar ); // load the font image to the font slot berror = ft_load_glyph (face, glyph_index, ft_load_default); // convert it to bitmap data if (face-> glyph-> Format! = Ft_glyph_format_bitmap) {// The second parameter is the rendering mode. Here the rendering is a bitmap (black and white Bitmap ), if it is ft_render_mode_normal, it is rendered as Level 1 grayscale graph berror = ft_render_glyph (face-> glyph, ft_render_mode_mono );}

 

 

Here we can use face-> glyph-> bitmap to obtain the bitmap data of the font "Bo". Bitmap stores information such as the width, height, color depth, and color palette of the bitmap, you can use GDI + to draw the image.

 

 // create a bitmap bitmapinfo BMP info = {0}; // initialize the bitmap structure BMP info. bmiheader. bisize = sizeof (bitmapinfoheader); BMP info. bmiheader. biwidth = face-> glyph-> bitmap. width; BMP info. bmiheader. biheight = face-> glyph-> bitmap. rows; BMP info. bmiheader. bibitcount = 1; // It depends on the rendering mode. For details, see the ft_bitmap section in the FreeType API manual. bmiheader. biclrimportant = 0; BMP info. bmiheader. biclrused = 0; BMP info. bmiheader. bicompression = Bi_rgb; BMP info. bmiheader. biplanes = 1; BMP info. bmiheader. bisizeimage = 0; // create a memory bitmap unsigned char * pvbits = new unsigned char [10000]; hbitmap = createdibsection (null, & BMP info, dib_rgb_colors, (void **) & pvbits, null, 0); int ilinebytes = (BMP info. bmiheader. biwidth + 7)/8; For (INT I = 0; I! = BMP info. bmiheader. biheight; ++ I) {memcpy (pvbits + I * ilinebytes, face-> glyph-> bitmap. buffer + I * ilinebytes, ilinebytes);} bitmap * pbitmap = bitmap: fromhbitmap (hbitmap, null); graphics graphic (PDC-> m_hdc); graphic. drawimage (pbitmap, point (20,150); 

 

 

This part of the code is not explained much. It only displays bitmap data. Here, face-> glyph-> bitmap is a bitmap without a color palette. It is derived from the ft_render_mode_mono rendering mode.

 

Show Preview

 
 
 
Here, the font inversion is related to the bitmap coordinate system, which can be simply processed.

Briefly describe the use process of FreeType. For more information about the Function Description and process, see "FreeType2 development basics". If you are interested in this document, please refer to it.

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.