2007年11月9日 星期五

讀取漢字點陣數據

/**
* 讀取漢字點陣數據
*
* @author legend
* @link http://www.ugia.cn/?p=82
* @Copyright www.ugia.cn
*/

$str = "中華人民共和國";

$font_file_name = "mingliu12.fon"; // 點陣字庫文件名
$font_width = 12; // 單字寬度
$font_height = 12; // 單字高度
$start_offset = 0; // 偏移

$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)
{
// 先求區位碼,然後再計算其在區位碼二維表中的位置,進而得出此字符在文件中的偏移
$offset = ((ord($str{$i}) - 0xa1) * 94 + ord($str{$i + 1}) - 0xa1) * $offset_size;
$i ++;
}
else
{
$offset = (ord($str{$i}) + 156 - 1) * $offset_size;
}

// 讀取其點陣數據
fseek($fp, $start_offset + $offset, SEEK_SET);
$bindot = fread($fp, $offset_size);

for ($j = 0; $j < $offset_size; $j ++)
{
// 將二進制點陣數據轉化為字符串
$dot_string .= sprintf("%08b", ord($bindot{$j}));
}
}

fclose($fp);

echo $dot_string;
?>

沒有留言: