專案需求:輸入中文字串,輸出對應的點陣矩陣(0/1 字串)。本文基於 GB2312 區位碼與 12x12 點陣字庫(如 simsun12.fon)實作完整讀取邏輯。
區號 * 100 + 位號(如「啊」= 1601)高位 = 區號 + 0xA0、低位 = 位號 + 0xA0(「啊」= 0xB0A1)(區號-1)×94 + (位號-1) × 單字節點數<?php
$str = "中華人民共和國";
$font_file = "simsun12.fon"; // 12x12 點陣字庫
$font_w = 12;
$font_h = 12;
$start_offset = 0;
$fp = fopen($font_file, "rb");
$char_bytes = $font_w * $font_h / 8; // 12x12=144 bits = 18 bytes
$dot_string = "";
for ($i = 0; $i < strlen($str); $i++) {
$c = $str[$i];
if (ord($c) > 160) { // 双字节汉字高字节 >= 0xA1
$zone = ord($c) - 0xA1;
$pos = ord($str[++$i]) - 0xA1;
$offset = ($zone * 94 + $pos) * $char_bytes;
} else { // ASCII
$offset = (ord($c) + 156 - 1) * $char_bytes; // ASCII 區位映射
}
fseek($fp, $start_offset + $offset, SEEK_SET);
$bindot = fread($fp, $char_bytes);
// 位元轉 0/1 字串
for ($j = 0; $j < $char_bytes; $j++) {
$dot_string .= sprintf("%08b", ord($bindot[$j]));
}
}
fclose($fp);
echo $dot_string; // 連續 0/1 字串,每字 144 位
?>
sprintf("%08b", ...) 將每位元組展開為 8 位二進位字串(高位在前)DotFont 類別,支援多種字庫格式(HZK、HZK16、TTF 轉點陣)掌握區位碼與點陣字庫對應關係,即可在無 TTF 渲染環境下實現中文點陣顯示。