FreeType is a font service library that supports multiple fonts and provides efficient and high-quality text.
FreeType introduction see: http://blog.csdn.net/ganxingming/archive/2006/06/05/774796.aspx
1. Compile FreeType
Decompress the package and enter./include/FreeType. Copy the config file to the current folder and enter any name. Here I use custom_config
Enter./builds/Win32/VisualC
Here is the vs project, because FreeType is widely supported, there are many things we don't need, so we need to re-compile FreeType.
The folder we copied just now contains the custom FreeType file.
Open the project and add the folder you copied to the project, that is, custom_config
Then open ft2build. h
We can see that a file is contained,
# Include <FreeType/config/ftheader. h>
This file is the main file used to customize FreeType. What we need to do is to create our own custom file, replace it with this file, and change it
# Include <FreeType/custom_config/ftheader. h>
Custom_config is your own folder.
Then go to custom_config/ftheader. h
Modify these rows
# Define ft_config_config_h <FreeType/config/ftconfig. h>
# Define ft_config_standard_library_h <FreeType/config/ftstdlib. h>
# Define ft_config_options_h <FreeType/config/ftoption. h>
# Define ft_config_modules_h <FreeType/config/ftmodule. h>
Change
# Define ft_config_config_h <FreeType/custom_config/ftconfig. h>
# Define ft_config_standard_library_h <FreeType/custom_config/ftstdlib. h>
# Define ft_config_options_h <FreeType/custom_config/ftoption. h>
# Define ft_config_modules_h <FreeType/custom_config/ftmodule. h>
Go to custom_config/ftmodule. h
This file is registered by the FreeType module. I changed it
/* Ft_use_module (autofit_module_class )*/
Ft_use_module (tt_driver_class)
/* Ft_use_module (t1_driver_class )*/
/* Ft_use_module (cff_driver_class )*/
/* Ft_use_module (t1cid_driver_class )*/
/* Ft_use_module (pfr_driver_class )*/
/* Ft_use_module (t42_driver_class )*/
/* Ft_use_module (winfnt_driver_class )*/
/* Ft_use_module (pcf_driver_class )*/
/* Ft_use_module (psaux_module_class )*/
/* Ft_use_module (psnames_module_class )*/
/* Ft_use_module (pshinter_module_class )*/
/* Ft_use_module (ft_raster1_renderer_class )*/
Ft_use_module (sfnt_module_class)
Ft_use_module (ft_smooth_renderer_class)
/* Ft_use_module (ft_smooth_ LCD _renderer_class )*/
/* Ft_use_module (ft_smooth_lcdv_renderer_class )*/
/* Ft_use_module (bdf_driver_class )*/
Only three modules are left
Well, now the FreeType customization is complete, but there is still a problem. FreeType is a compiled static link library. We generally use a dynamic link library,
Therefore, you need to change it to a dynamic link library.
Go to custom_config/ftconfig. h and set
# Ifndef ft_export
# Ifdef _ cplusplus
# Define ft_export (x) extern "C" x
# Else
# Define ft_export (x) extern x
# Endif
Change
# Ifdef dll_export
# UNDEF dll_export
# Define dll_export _ declspec (dllexport)
# Else
# Define dll_export _ declspec (dllimport)
# Endif /*! Dll_export */
# Ifndef ft_export
# Ifdef _ cplusplus
# Define ft_export (x) extern "C" dll_export x
# Else
# Define ft_export (x) extern dll_export x
# Endif
Next, modify the project and enter the release multithreaded configuration of the project properties. We will compile the multi-threaded version.
Go to regular, and change the configuration type to dynamic library
Go to the C/C ++-> pre-processor and add dll_export to the pre-processor definition.
Now we can start compiling. The compiled directory is in objs/release_mt. Now you can copy
FreeType. dll, FreeType. Lib, and the entire include folder are included in our project.
2. Use FreeType
Please refer to the post here
Http://www.unixresources.net/lin... 0/59/21/592188 .html
It provides a detailed introduction.
Next, let me talk about my use of FreeType.
1. Get the correct position of the character.
First, ftface is created. We recommend that you use baselines as the baseline for drawing for FreeType. However, you usually set the position in the upper left corner of the character to start painting. Required
The distance from the baseline to the highest point of the character contour. This information is placed in
Ascender = ftface-> size-> metrics. ascender> 6; // The distance from the baseline to the highest point of the character contour, because it is a fixed point of 26.6,
Therefore, the integer part must be divided by 64.
Then the height of each character is different. The bitmap generated by FreeType is usually surrounded by characters. For example, the bitmap height of A and L is different.
Therefore, the offset width and height of each character must be obtained.
Bitmap_left = ftface-> glyph-> bitmap_left; // The distance between the character and the left boundary
Bitmap_top = ftface-> glyph-> bitmap_top; // The distance from the highest point of the character to the baseline
Now, we assume that we want to draw characters at (posx, Posy) and (charposx, charposy) indicate the correct position of the characters.
Charposx = posx + bitmap_left;
Charposy = posy + ascender-bitmap_top;
2, line spacing
Place the maximum character contour height
Ftface-> size-> metrics. Height // maximum height of the character contour, 26.6 points
However, I found that the height is too high, so I usually use it like this.
Descender = ftface-> size-> metrics. descender> 6; // The distance from the baseline to the lowest point of the character profile
Fontheight = (ftface-> size-> metrics. Height> 6) + ascender + descender)/2;
Then you can add a fixed height to fontheight, such as 1 or 2.
3, Character Color
FreeType generates an 8-bit grayscale image (there are others, but the 8-bit image looks better). The text is white and the background is black.
This is a problem. Generally, the font is black. If the font color is reversed, the font color turns black and white.
But what should we do if we need to add colors? This is why FreeType text is white, because it is an 8-bit grayscale image, so it is not black or white.
The color is a proportional factor. You only need to multiply it by the color you want to set and divide it by 256. For example, if the color of a certain point in the grayscale image is 156
The set color is RGB (127, 42,186), then the actual color is
(127*156> 8, 42*156> 8,186*156> 8)
Back to what we mentioned earlier, if we change the black to the white to the black, then the proportional factor will be reversed again, which is troublesome.
In addition, if the font color is set, the proportional factor of the grayscale image is lost and cannot be set to another color (it is necessary to obtain the grayscale image again ).
It takes too much time ).
The solution is to save the 8-bit grayscale image in the alpha channel. If you want to set the color, you can obtain the grayscale value from the alpha channel.
4. Make the characters look better.
The grayscale image is saved in the Alpha Channel. If your driver supports Alpha mixing, congratulations! The characters can be mixed with the background.
Yes, grayscale images also have alpha functions. If you want to have a better look, do alpha mixing.
My driver does not support alpha mixing, so I have to mix it myself.
This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/shtianhai/archive/2010/06/07/5652587.aspx