PHP reads Chinese character dot matrix data

Source: Internet
Author: User
Tags character set ord php code php script
Chinese Character | data

Whim, want to imitate the LCD display effect, wrote a bitmap from the lattice of Chinese characters to extract the data of a section of PHP script.

Background knowledge:
Simplified Chinese GB Font (1981-year order, mainland China). 7,445 characters, of which 6,773 Chinese characters, including 3,755 first-level Chinese characters, two-grade Chinese characters 3,008. 2-byte (16-bit binary) encoding is used.

Location Code: GB GB2312 stipulates that all GB characters and symbols form a 94x94 matrix. In this phalanx, each row is called a "zone", and each column is called a "bit", so the Phalanx actually consists of a Chinese character set with 94 areas (0 1 to 94 respectively) and 94 digits in each region (the digits are 01 to 94 respectively). The area code of a Chinese character is composed of a simple combination of the region code and the digit number. In the location code of Chinese characters, the high two-bit is the area code, and the low two digit is the bit number. This shows that the location code and Chinese characters or symbols are one by one corresponding.

Inner code: Chinese characters in the code refers to the computer to represent the encoding of Chinese characters. The internal code is slightly different from the location code. Why not use location code directly as the code inside the computer? This is because the region code of Chinese characters and the range of bit code within 1 to 94, if the direct use of location code as the internal code, will be in conflict with the basic ASCII code. The inner code of Chinese characters is usually related to the computer system used. At present, for most computer systems in China, the inner code of a Chinese character occupies two bytes, called High byte and low byte respectively, and the relationship between the two-bit byte and location code is as follows: Inside code high = region code +a0h (h for hexadecimal) inner code low = bit code +A0H For example, Chinese characters "ah" The location code is "1601″ , the region code and the bit code are "1001H" in hexadecimal notation respectively, 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 Code: Returns a string consisting of 0 and 1.

<?php
/**
* Read Chinese character dot matrix data
*
* @author legend <legendsky@hotmail.com>
* @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 =  of ; //Word width
$font _height =  of ; //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 )
{
//Find the Location Code first, then calculate its position in the two-dimensional table of location code, and then get the deviation of this character in the file.
$offset = (( Ord ( $str { $i }) -  0xa1 ) *  the +  Ord ( $str { $i +  1 }) -  0xa1 ) *  $offset _size ;
$i ++;
}
Else
{
$offset = ( Ord ( $str { $i }) +  156 -  1 ) *  $offset _size ;
}

//read its dot matrix data
fseek ( $fp ,  $start _offset +  $offset ,  Seek_set );
$bindot =  fread ( $fp ,  $offset _size );

for ( $j =  0 ;  $j < $offset _size ;  $j ++)
{
//Convert binary lattice data to strings
$dot _string .=  sprintf ( "%08b" ,  Ord ( $bindot { $j }));
}
}

fclose ( $fp );

Echo $dot _string ;
?>

Lattice font and program packaging download:fontfun.rar
This includes two lattice font files: one for 16x16 Chs16.fon and the other for 12x12 Simsun12.fon with zero offsets.



Related Article

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.