Simple use of FreeType2: platform-independent TrueType font display.

Source: Internet
Author: User

Simple use of FreeType2:
FreeType2 is a simple cross-platform font drawing engine. It currently supports TrueType type1 Type2 and other font formats. However, it does not seem to support OpenType.
FreeType is widely used. The famous ftgl is FreeType. It can draw Vector Fonts efficiently in OpenGL.
I have never used ftgl because I don't want to use ftgl without knowing how to use FreeType.

After one night's readingCode(My code reading capability is poor). I finally learned how to use FreeType2. But it is simple to use. I still don't know how to set attributes such as bold itainly. It is mainly a simple demonstration. I will prepare a perfect font engine later.
The following is a brief introduction.

First of all, it includes the header file. The header file should include:
# Include [ft2build. H]
# Include ft_freetype_h
I don't know why. It's like this.
The following is the FT2 initialization code. And the code for drawing and releasing.>
Note that the draw code here accepts Unicode characters, indicating that you can draw the code as old as this way.
Ft2_obj font;
Font. INIT ("simsun. TTF", 32 );
Wchat_t ptext [] = l "pan lilang is a wild boar ";
For (INT n = 0; n <wcslen (ptext); N ++)
{
Font. drawaunicode (ptext [N];
}
Font. Free ();

// The following code is ft2_obj.
// Refer to Lesson 43 of nehe.
Class ft2_obj
{
Ft_library library;
Int h;
Ft_face face;

Public:
Void Init (const char * fname, unsigned int H );
Void free ();
Void drawaunicode (wchar_t ch)
};
Void ft2_obj: Init (const char * fname, unsigned int H)
{
This-> H = h;

// Initialize the FreeType library ..
If (ft_init_freetype (& library ))
Throw STD: runtime_error ("ft_init_freetype failed ");

// Load a font and use the default face, which is generally regualer.
If (ft_new_face (library, fname, 0, & face ))
Throw STD: runtime_error ("ft_new_face failed (there is probably a problem with your font file )");

// The size must be multiplied by 64. This is a rule. Just do it.
Ft_set_char_size (face, H <6, H <6, 96, 96 );
Ft_matrix matrix;/* transformation matrix */
Ft_uint glyph_index;
Ft_vector pen;
// Set a rotation matrix for it
Float angle =-20/180. * 3.14;
Matrix. xx = (ft_fixed) (COS (angle) * 0x0000l );
Matrix. XY = (ft_fixed) (-sin (angle) * 0x0000l );
Matrix. YX = (ft_fixed) (sin (angle) * 0x0000l );
Matrix. yy = (ft_fixed) (COS (angle) * 0x0000l );

Ft_set_transform (face, & matrix, & pen );
}.

/*
draw operation.
*/
void ft2_obj: drawaunicode (wchar_t ch)
{< br> If (ft_load_glyph (face, ft_get_char_index (face, CH), ft_load_default ))
throw STD: runtime_error ("ft_load_glyph failed");

// Obtain the Template
Ft_glyph glyph;
If (ft_get_glyph (face-> glyph, & glyph ))
Throw STD: runtime_error ("ft_get_glyph failed ");

// Convert to a bitmap
Ft_render_glyph (face-> glyph, ft_render_mode_normal );
Ft_glyph_to_bitmap (& glyph, ft_render_mode_normal, 0, 1 );
Ft_bitmapglyph bitmap_glyph = (ft_bitmapglyph) glyph;

// Obtain bitmap data
Ft_bitmap & bitmap = bitmap_glyph-> bitmap;

// Copy the bitmap data to the data area defined by the user so that you can draw the desired data.
Int width = bitmap. width;
Int Height = bitmap. Rows;

Usigned char * expanded_data = new usigned char [3 * width * Height];

For (Int J = 0; j
<Height; j ++)
{
For (INT I = 0; I <width; I ++)
{
Expanded_data [3 * (I + (height-j-1) * width)] =
Expanded_data [3 * (I + (height-j-1) * width) + 1] =
Expanded_data [3 * (I + (height-j-1) * width) + 2] =
(I> = bitmap. Width | j> = bitmap. Rows )?
0: bitmap. Buffer [I + bitmap. Width * j];

}
}

}
}

Void ft2_obj: Free ()
{
Ft_done_face (FACE );
Ft_done_freetype (library );
}

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.