PHP's overall drawing function-draw characters in Chinese ~~

Source: Internet
Author: User
Draw Chinese

Thought: finding the coordinates of the corresponding point connects all points as a Chinese character, such as an old dot matrix printer
To draw a Chinese character, we need to obtain information about all vertices of the Chinese character and depict them. The dot matrix font with Chinese characters is required for Chinese characters. The International Location Code is saved in the font. We can read Chinese character information from it (chs16.fon font file ).
First, we need to know that the Chinese character is composed of 16x16 points, and the position of a bit is 1; otherwise, it is 0, for example:
The C character information is as follows:
Row 3:00000000
Row 3: 1st
Row 3: 2nd
Row 3: 3rd
Row 3: 4th
Row 3: 5th
Row 3: 6th
Row 3: 7th
Use a double loop to locate the vertex and draw it out. 8 rows and 8 columns.
Chinese is represented by two ASCII codes with a high position of 1.

The conversion formula between the location code and the internal code is
[Location Code] + (10100000 10100000) = [Internal Code ]. That is:
Location Code 0 + (10100000) = internal code 0;
Location Code 1 + (10100000) = internal code 1;
In this way, the dot matrix data can be indexed and searched through the Chinese character "in-Memory code"-> "Location Code.
Since a Chinese character uses 32 bytes, And the GB-2312 location code table has 94 rows, 94 columns, as long as you know the character in the table is the first few, multiply by 32 on the line.
You can use the location code to find the number of the partition code in the partition code table, which is represented by the partition code)

Steps:

1. Use ord to obtain the inner code of a character, and then use the above conversion formula to obtain the location code (the first character of a Chinese character is the information of the area, and the second is the information of the BIT)
2. Identify the character position in the font and obtain the dot matrix information through the location code.
3. Put the dot matrix information into an array
4. If the value is 1, draw points at the corresponding position

The principle is so simple :)

Below is a sectionCode: Is someone else's code

/*************************************
* File name: 'draw1. 0. Inc. php
** Chinese dot matrix output Version 1.0
** Only simple operations are provided: Output a pure Chinese character string of the default size to the coordinate (0, 0) of the image.
** For more functions, see the next version.
*
****************************************/
Function draw ($ image, $ string, $ color)
{
$ Fp = fopen ("chs16.fon", "rb"); // read the dot matrix font chs16.fon in binary mode
If (! Feof ($ FP) // if the file pointer reaches the end of the file, exit and do not forget to close the file.
{
While ($ string) // when the string is not 0
{
$ QH = ord (substr ($ string, 0, 1)-0xa0;
$ Wh = ord (substr ($ string, 1, 2)-0xa0;
/* These two lines of code actually get a Chinese internal code. The partition code is computed. */
$ Offset = (94 * ($ qh-1) + ($ wh-1) * 32;
/* After the Location Value of the Chinese character is obtained, the offset is calculated. */
Fseek ($ FP, $ offset, seek_set );
/* In the font file $ FP, locate the file pointer to the offset. */
$ Buffer = preg_split ('//', fread ($ FP, 32),-1, preg_split_no_empty );
/* Fread ($ FP, 32); reads 32 bytes of data from $ FP, and then distributes the data to the array $ buffer through preg_split. Preg_split (); is a function that supports regular expression. */
For ($ I = 0; $ I <16; $ I ++) // Number of dot matrix rows: 16 columns should also be 16
For ($ J = 0; $ j <2; $ J ++) // because it is two bytes, You need to draw them one by one.
For ($ k = 0; $ k <8; $ K ++) // each byte has 8 points of data
If (ord ($ buffer [$ I * 2 + $ J])> (7-$ k) & 0x01 )) // if the value of this vertex is 1, output; otherwise, no
{
Imagesetpixel ($ image, $ x + 8 * $ J + $ K, $ I, $ color );
}
$ String = substr ($ string, 2); // Chinese is represented by two bytes. Therefore, after outputting a Chinese character, remove two bytes.
$ X = 24; // the output of a Chinese character ends. leave it blank and give the next Chinese character. Because the Chinese character is 16x16, it is enough to set the value of $ X to 16. But it's too crowded, isn't it?
}
}
Fclose ($ FP );
}

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.