_php Tutorial of Reading Chinese character lattice data with PHP programming

Source: Internet
Author: User
Background knowledge:

Simplified Chinese National Standard Font (1981 years, mainland China). 7,445 characters, of which 6,773 Chinese characters, including a first-level kanji 3,755, two-level kanji 3,008. Encoded with 2-byte (16-bit binary).

Location Code: GB GB2312 provisions, all the national standard Chinese characters and symbols constitute a 94x94 matrix. In this matrix, each row is called a "zone", each column is called a "bit", so the square actually consists of a 94-zone (area code of 0 1 to 94), each region has 94 bits (01 to 94 of the number of digits) of the character set. The area code of the Chinese character is composed of a simple combination of the region code and the digit number. In the location code of Chinese characters, the height of two digits is the area code, and the lower two bits is the number. Thus, the location code and the Chinese characters or symbols are one by one corresponding.

Inner code: The inner code of Chinese characters refers to the encoding of Chinese characters in the computer. There is a slight difference between the in-machine code and the location code. Why not directly use the location code as the code inside the computer? This is because the region code and code of the Chinese characters in the range of 1 to 94, if directly with the location code for the in-machine code, it will conflict with the basic ASCII code. The inner code of Chinese characters is usually related to the computer system used. At present, for most of the domestic computer systems, a Chinese character in the internal code accounted for two bytes, respectively called high-order byte and low byte, and the relationship between the two-bit byte and location code is as follows: Inner code high = area code +a0h (h for hexadecimal) code low = bit code +A0H For example, the location code of "Ah" is "1601″ , the area code and the bit code are hexadecimal means "1001H", then its inner code is "b0a1h". Where b0h is the high byte of the inner code, A1H is the low byte of the inner code.

PHP (as the current mainstream development language) code: Returns a string consisting of 0 and 1.

/**
* Read Chinese character lattice data
*
* @author legend
* @link http://www.ugia.cn/?p=82
* @Copyright www.ugia.cn
*/

$str = "People's Republic of China";

$font _file_name = "Simsun12.fon"; Bitmap font file name
$font _width = 12; Word width
$font _height = 12; Word height
$start _offset = 0; Offset

$fp = fopen ($font _file_name, "RB");

$offset _size = $font _width * $font _HEIGHT/8;
$string _size = $font _width * $font _height;
$dot _string = "";

for ($i = 0; $i strlen ($STR); $i + +)
{
if (Ord ($str {$i}) > 160)
{
The location code is first obtained, then the position in the two-dimensional table of location code is calculated, and then the offset of this character in the file is obtained.
$offset = ((Ord ($str {$i})-0XA1) * 94 + ord ($str {$i + 1})-0xa1) * $offset _size;
$i + +;
}
Else
{
$offset = (Ord ($str {$i}) + 156-1) * $offset _size;
}

Reading its lattice data
Fseek ($fp, $start _offset + $offset, seek_set);

http://www.bkjia.com/PHPjc/508698.html www.bkjia.com true http://www.bkjia.com/PHPjc/508698.html techarticle background knowledge: Simplified Chinese national Standard Font (1981 years, mainland China). 7,445 characters, of which 6,773 Chinese characters, including a first-level kanji 3,755, two-level kanji 3,008. Takes 2 bytes (16 bits ...

  • 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.