Display usdos Chinese Character Library in C Language

Source: Internet
Author: User
Recently, I found a new C language document, which is deeply C Language Is infatuated with the underlying operation features ~. In this regard, the most classic book is the "C advanced practicality" published by Tsinghua University Press. Program Design (Wang Shiyuan): This is the best book I have ever written in the field of advanced C language applications, it is a pity that this book is out of print now (probably because of the rapid technological development and updates) and cannot be purchased online at the bookstore. I remember that in my undergraduate course, I often read this book by my classmates. I love it, and the knowledge in it is very attractive. Even today, I still feel that it is not outdated, but I have an endless aftertaste. The font library files and operating systems mentioned here are already at the antique level and may be hard to find now .... This application is rarely studied in the present era, but I think it is still in use in the dot matrix Chinese Character Display Screen of embedded systems such as single chip microcomputer.
Here I refer to some materials that show chinese character libraries in C language. In the early stages of computer development, in order to support the display of Chinese characters, the Chinese character national standard (GB) Code represented by two bytes was invented in China. According to this encoding rule, Chinese characters were divided into 94 areas, 94 Chinese Characters in each region. The positions of Chinese characters in the region are represented by bits. The two bytes represent the region code and BITs respectively. To distinguish ASCII codes, the first part of each byte is set to 1. There are also specific methods to distinguish between network transmission. These details are not described here. In international encoding, Chinese characters are allocated to area 16th (starting area 0x0 f ). To display Chinese characters, you must support Chinese Character Font files. In the DOS era, the usdos system emerged, including hzk16 (16x16) and hzk24 (24x24) font files, essentially, a Chinese character is a binary image (bpp = 1). Therefore, it is essentially a graphical font (non-vector). For example, hzk16, each character is 16*16 pixels, each pixel occupies 1 byte (1/8 bytes), so each Chinese Character occupies 16*16/8 = 32 bytes/Chinese Character in the file. Hzk24 each Chinese Character occupies 24*24/8 = 72 Bytes/Chinese character. Because these conventions are used, no descriptive information is attached to the file header in the file, and all of them are closely arranged pixel-shaped lattice, the file starts from the first byte and ends with the last character. The file has no suffix. The glyphs are closely arranged in files, but the two important issues that need to be noted are:
(1) Scanning sequence:
Hzk16 is a row-based scan, while hzk24 is a column-based scan.
Therefore, assume that a glyph is read to a byte []. The Byte distribution is divided into row-based scan and column-based scan. For example, in hzk16, scan by row and line, that is, the first byte [0], byte [1] is the first row, and the second byte is the second row... Hzk24 uses column-based scanning for printer output. Because the printer has a vertical 24-pin, each time a row of Chinese characters can be printed one by one, in order to cooperate with the printer, hzk24 uses column-based scanning. As shown in: (the number in the figure indicates the index of this Byte in byte .)

The following figure shows a 24x24 pixel column-scanned Chinese character. How to use a byte [] array to describe it: in the figure, byte [0], byte [1], and byte [2] are marked by color filling.

(2) Positioning of fonts in font files:
Note that the locations in hzk16 and hzk24 are different. This is because Chinese characters have different starting areas in the file. Hzk16 has 16th Chinese Character start areas, while hzk24 directly starts from the first partition. Some special symbols and letters are stored in the first 15 shards of hzk16 (see the following section ). Therefore, the partition where the Chinese characters are located starts from the 15th partition, while hzk24 does not contain the preceding special characters. The first partition is the Chinese character area "ah", which starts from the 0th partition. Note that the [Bytes/Chinese character] values of the two are different. Offset indicates the file offset address calculated from the file header. Code [2] indicates the man code.
The code is represented by an internal code. For example, the inner code of the first Chinese character "ah" in the Chinese character table is {0xb0, 0xa1}; the area code is obtained from the first byte of the Chinese character code, obtain the bid from the second byte. Subtract 0xa1 from them and convert them to the region and Location Numbers we need. That is, it is converted to the area code {0x0f, 0x00}, indicating that the word "ah" is located in Area 15th, and the bit number is 0.
For hzk16, 94 indicates the number of Chinese Characters in each area.
Unsigned long offset = ( (Code [0]-0xa1) * 94 + (code [1]-0xa1) * 32l; // 32 is the number of bytes occupied by each Chinese Character
For hzk24:
Unsigned long offset = ( (Code [0]-0xa1-15) * 94 + (code [1]-0xa1) * 72l; // 72 is the number of bytes occupied by each Chinese Character
Note that in hzk24, because the first partition is a Chinese character area, the area code 0x0 f is subtracted.

In this way, we can see that Code (The following code comes from C advanced application design .) Hzk16 code for displaying Chinese Characters
/* A Chinese character is displayed at the x0 and Y0 locations on the screen. */
Void Dishz ( Int X0, Int Y0, Char Code [], Int Color)
{
Unsigned Char Mask [] = { 0x80 , 0x40 , 0x20 , 0x10 , 0x08 , 0x04 , 0x02 , 0x01 };
/* Block arrays of each line of the dashboard */
Int I, j, X, Y, Pos;
Char Mat [ 32 ];
/* The following function is used to read the corresponding bytes from the file to the mat array */
Get_hz (Code, mat );
Y = Y0;
For (I = 0 ; I < 16 ; ++ I)
{
X = X0;
Pos = 2 * I;
For (J = 0 ; J < 16 ; ++ J)
{
/* Blocks A single digit of the Chinese character model. That is, if a vertex is 1, it is displayed. Otherwise, it is not displayed. */
If (Mask [J % 8 ] & Mat [POS + J / 8 ]) ! = Null)
Putpixel (X, Y, color );
++ X;
}
++ Y;
}
}

Note that because each Chinese Character occupies 2 bytes, we must increase the pointer of the string by 2 each time. For example:
Char * s = "this is a Chinese character string ";
While (* s)
{
Dishz (x0, y0, S, lightblue );
X0 + = 16;
S + = 2; // notice: Not s ++
}
One digit of each number in the preceding modulo array is 1, so that bitwise AND operation can be used to determine whether a digit in a byte is 1 or 0, if the value is 1, the foreground color of Chinese characters is displayed. The above code is used for hzk16, which is displayed in the order of row scanning. The code displayed by column is similar in principle and is not listed.
The loading mode mainly uses the above file positioning to read a certain number of bytes. We will not list them here.
The following are some of the first special characters in the hzk16 font (including Chess pawns, tabs, full-angle letters and numbers, etc.): (32 characters in each line, I marked a red dot in the upper left corner of each character ).

The above uses the TC screenshot code in "display BMP with C language" (I write the screenshot code to cpyscr. put the H file in the include folder, so you only need to use # include "cpyscr. H "to directly use the screenshot function). Note that the number in the first line represents the number of the character (note that this number is not an ascii code !!!), Not the file address !.
It is a Chinese character displayed with hzk24f (). It is output in the original size (24x24). A large word is used to increase the length of the font side to 2 times the length of the side (48x48) because it is a bitmap font, after expansion, the bitmap will be scaled out of the teeth:

:

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.