Current location - Health Preservation Learning Network - Slimming men and women - Ttf font file editing
Ttf font file editing
The extraction (conversion) of Windows vector fonts requires the display of Chinese characters in many occasions, such as the electronic display board for bus stop reporting and the display of various commodity information in shopping malls. Windows provides a rich font library. How to use these fonts to display Chinese characters is a problem that needs to be solved. Windows supports GDI fonts and device fonts. GDI fonts are stored in hard disk files, while device fonts are inherent in output devices. GDI fonts are divided into three types: dot matrix fonts, stroke fonts and TrueType fonts. The fonts of dot matrix fonts can be obtained directly from font files, and the latter two are vector fonts, so their fonts cannot be obtained directly. Therefore, it is necessary to rasterize stroke fonts and TrueType fonts to obtain the required fonts.

Usually, the information display of electronic systems uses the dot matrix font of 16× 16(32 bytes). For example, Chs 16.fon under Win98 is the font file of 16× 16(32 bytes). The method of extracting fonts from it is: the internal code of Chinese characters is two bytes, which are set as A and B, respectively. The size of A should be between 0xa 1 and 0xfe, its area code is qu=a-0xa0, its bit code is wei=b-0xa0, and the position of Chinese characters in the font file is offset = ((qu-1) × 90.

This paper mainly introduces the method of extracting fonts from vector fonts of Windows. This method has been successfully applied to the design of single chip microcomputer system and solved the problem of Chinese character display. In practical application, Win32 API function can be directly called to convert the Chinese character text to bitmap, so as to realize the dot matrix of Chinese characters and extract fonts.

1 font settings

You need to set the font first. Win32 SDK provides a general font selection dialog box, just call the ChooseFont function, and its return value is a Boolean value. Specifically defined as boolchoosefont (lpchoosefont lpcf). After calling this function, the font selection dialog box pops up, and you can select the required parameters such as font, font style and font size. After selection, if you click OK in the font selection dialog box, the function returns a non-zero value; If you click the Cancel key, the function will return a value of zero. Before calling this function, you need to define two variables:

Select font cf;

LOGFONT logfont

CHOOSEFONT is a structure with more than ten fields, which contains all kinds of information used by the ChooseFont function to initialize the font selection dialog box. LOGFONT is also a structure, which contains 14 fields and defines various attributes of fonts. Click OK to return the selected font information through the LOGFONT structure. The returned font information is stored in the LOGFONT structure specified by the lpLogFont field of the CHOOSEFONT structure.

The following is the code to call the ChooseFont function:

//initialize CHOOSEFONT

cf . l structsize = sizeof(choose font);

Cf.hwndOwner = hwnd// Handle of the current window.

cf.hDC = NULL

Cf.lpLogFont = & amplogfont///The font information returned by the system is saved here.

cf . ipointsize = 0;

CF . Flags = CF _ INITTOLOGFONTSTRUCT | CF _

Screen font | CF _ EFFECTS

cf . RGB colors = 0;

cf . lcustdata = 0;

cf.lpfnHook = NULL

cf.lpTemplateName = NULL

cf.hInstance = NULL

cf.lpszStyle = NULL

cf . nfonttype = 0;

cf . NSI Zemin = 0;

cf . nsizemax = 0;

& cf; select a font; //After calling this function, the general dialog box for font selection pops up.

If you choose a font (&; Cf) function returns a non-zero value, then the font has been selected. The selected font is saved in the logfont variable. The next thing to do is to create the selected logical font. You can call the CreateFontIndirect function to create a logical font. The CreateFontIndirect function accepts a pointer to the LOGFONT structure, specifically defined as hfont createfontindirect (constlogfont? Lplf)。 The code is as follows:

HFONT hNewFont = CreateFontIndirect(& amp; log font);

At this point, the font creation is completed. By directly calling the SeletObject function, the created logical font can be selected into the device description table. In bitmap conversion, the SelectObject function will be used to select hNewFont into the memory device description table. However, it should be noted that before the end of the program, the DeleteObject(hNewFont) function must be called to release the font handle to avoid memory leakage. The following describes the specific implementation process of converting text into bitmap.

2 bitmap conversion

Here is an example of extracting "ting" font. First, you need to define the following variables:

Static WCHAR Chinese character [] = "ting";

Static HBITMAP hBitmap

Static int cxBitmap

Static HDC hdc

PAINTSTRUCT ps

Size;

CxBitmap and cybermap are the sizes of bitmaps to be created, which are consistent with the text size obtained by GetTextExtentPoint32 function, that is, the size of the word "Ting" here.

The following is the specific method of converting Chinese character text into bitmap, which is generally handled in WM_PAINT message.

Case WM_PAINT:

hdc = begin paint(hwnd & amp; PS); //Get the device handle of the current window.

hdc mem = CreateCompatileDC(hdc); //Create a memory device description table.

SelectObject(hdcMem,hNewFont); //Select the created font into the memory device description table.

Getextensionpoint 32w (hdcmem, Hanzi, 1,&);

//Get the size of the text to be displayed.

cxBitmap = size.cx

cyBitmap = size.cy

hbit map = createcompatiletibbitmap(hdc,cxBitmap,

cyBitmap); //Create a bitmap handle

SelectObject(hdcMem,hbit map); //Select the bitmap to the memory device description table.

TextOutW(hdcMem, 0,0, Chinese character,1); //Draw Chinese characters on the bitmap of the memory device description table.

BitBlt(hdc,0,0,cxBitmap,cyBitmap,hdcMem,0,0,SRC-

Copy); //Display the bitmap in the client area of the window and observe the display effect.

At this point, the dot matrix process of Chinese characters is completed, and then the font is extracted.

3 extract fonts

The GetPixel function is used to extract fonts and is defined as COLORREF GetPixel(HDC hdc, int nXPos, int nYPos). This function returns a value of type COLORREF, that is, the RGB value of the point specified by nXPos and nYPos. The size of the bitmap was determined in the last article. In this range, every pixel is scanned once, and bitmap codes are generated according to the returned RGB values. Because Windows vector font has gray scale, it is necessary to choose an appropriate RGB value to judge whether this point is valid or not. The RGB value of white is FFFFFFH, the dark gray is 808080H, and the black is 000000H. You can choose dark gray as the judgment basis. This point is considered valid when the return value of the function is less than 808080H. The following is the function of extracting fonts, which is stored in bytes and scanned from the first point of the first line:

Jing Mozi [2048]; //There are lattice codes in this array.

void GetZimo(HDC hdc,int nXPos,int nYPos)

{

Int Hang, Lie//Hang is the number of rows scanned.

int temp,I,j,g;

Hang = nYPos

Lie = nXPos

if(Lie % 8==0 ){

Lie = Lie/8; //The width of the bitmap is an integer multiple of 8, so

//Only Lie/8 bytes are needed to store fonts.

temp = 0;

}

Otherwise {

temp = Lie % 8;

Lie = Lie/8+ 1; //The width of the bitmap is not an integer multiple of 8.

//So only Lie/8+ 1 byte is needed to store fonts.

}

Memset (Mozi, 0, 2048); //Set the font array to all zeros.

for(I = 0; I< hang up; i++){

for(j = 0; J< lies; j++){

If ((temp! = 0)& amp; & amp(j==Lie- 1) ){

for(k = 0; K< temperature; k++){

g=(int)GetPixel(hdc,j*8+k,I);

if(g & lt; 0x00808080)

Mozi [i*Lie+j]+= (unsigned

char)pow(2,7-k);

}

}

Otherwise {

for(k = 0; k & lt8; k++){

g=(int)GetPixel(hdc,j*8+k,I);

if(g & lt; 0x00808080)

Mozi [i*Lie+j]+= (unsigned

char)pow(2,7-k);

}

}

}

}

}

In the WM_PAINT message, call GetZimo(hdcMem, cxBit-

Map, cyBitmap) can get the font of Chinese characters. At the end of the program, you must also do some finishing touches:

delete object(hbit map); //The device description must be published after use.

//table and bitmap handles to avoid memory leaks

delete object(hNewFont);

DeleteDC(hdc mem);

Top coat (hwnd &; PS);

Returns 0; //WM_PAINT message is processed and returned.

4 output results

Taking the word "Ting" as an example, after the font selection dialog box pops up, the font is selected as a new style, the font is selected as normal, and the font size is selected as small two. Hang=24, Lie=3, and 72 bytes are stored. The fonts are 00H, 00H, 00H, 00H, 00H, 03H, 00H, 04H, 0 1H, 80H, 04H, 0 1H, 0CH, 04H, 3EH, F0H, 0CH, 00H, 7h. 18H, 8FH, F0H, 10H, 90H, 04H, 1 h, BFH, FEH, 1 1H, 20H, 04H, 3/. From cxBitmap and cybermap, we can know that the lattice size of "Ting" is 24×24.

5 concluding remarks

Using the method of text to bitmap, we can extract fonts of various fonts from the rich font library of Windows, which is no longer limited to a single font, thus enriching the design of electronic display system. If you extract fonts from dot matrix fonts, there are some shortcomings. The most important thing is that there are too few fonts to choose from. In addition, the method introduced in this paper can extract simple image lattice, which enriches the design of electronic display system.

This is someone else's space, but I can't understand it. I hope it helps you.