在网上还真搜出一段好代码,从12-48点阵汉字库读取与显示全兼容,由网友毕元锋博客拷贝.
仅支持: ASC16 *16x8点阵西文字库 HZK16 *16x16点阵显示字库 HZK16F *16x16点阵繁体显示字库 ASC12 *12x6点阵西文字库 HZK12 *12x12点阵汉字库 HZK24T *24x24点阵中文符号字库 HZK24S *24x24点阵宋体汉字库 ASC24 *24x24点阵西文字库 HZK24F *24x24点阵仿宋体汉字库 HZK24H *24x24点阵黑体汉字库 HZK24K *24x24点阵楷体汉字库 ASC40 *40x20点阵西文字库 HZK40T *40x40点阵中文符号字库 HZK40S *40x40点阵宋体汉字库 ASC48 *48x48点阵西文字库 HZK48T *48x48点阵中文符号字库 HZK48S *48x48点阵宋体汉字库 -----------------------------------------------------PS御免----------------------------------------------------- 轮廓字体的读取还不会。 ASCPS *11种西文轮廓字库 HZKPST *中文符号轮廓字库 HZKPSSTJ*宋体汉字库 HZKPSFSJ*仿宋体汉字库 HZKPSHTJ*黑体汉字库 HZKPSKTJ*楷体汉字库
正文
//---------------------------------------------------------------------------
#include <vcl.h> #pragma hdrstop #include <stdio.h> #include "MainForm.h" //--------------------------------------------------------------------------- #pragma package(smart_init) #pragma resource "*.dfm" TForm1 *Form1; AnsiString __fastcall GetAppPath(void) { AnsiString s = ParamStr(0); int pos = 0; int count = s.Length(); for (int i = count; s.SubString(i,1) != ""; i--, pos = i); return s.SubString(1, pos); } //--------------------------------------------------------------------------- __fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner) { } //--------------------------------------------------------------------------- void __fastcall ShowHZ(char *S, int x, int y, int PixelStyle, TCanvas *Canvas) {
int PixelSize = PixelStyle; unsigned char Qu; unsigned char Wei; //char *S = STR.c_str(); AnsiString File1Name, File2Name, File3Name; FILE *iFileHandle1,*iFileHandle2,*iFileHandle3, *iTemp; const unsigned char Bit[8] = {128,64,32,16,8,4,2,1}; unsigned char AreaOffset; unsigned int HZKSize; unsigned char ASCSize; int innerX, innerY; int Len = StrLen(S); if (Len>=1) { switch (PixelSize) { case 12: HZKSize = 24; //汉字库字模大小24字节(12*16/8) ASCSize = 12; //ASC字库字模大小12字节 AreaOffset = 0;//16区以前不独立成字库 File1Name = GetAppPath()+"fontHZK12"; iFileHandle1 = fopen(File1Name.c_str(), "rb"); File2Name = GetAppPath()+"fontASC12"; iFileHandle2 = fopen(File2Name.c_str(), "rb"); break; case 16: HZKSize = 32; //汉字库字模大小32字节(12*16/8) ASCSize = 16; //ASC字库字模大小16字节 AreaOffset = 0;//16区以前不独立成字库 File1Name = GetAppPath()+"fontHZK16"; iFileHandle1 = fopen(File1Name.c_str(), "rb"); File2Name = GetAppPath()+"fontASC16"; iFileHandle2 = fopen(File2Name.c_str(), "rb"); break; case 24: HZKSize = 72; //汉字库字模大小32字节(24*24/8) ASCSize = 72; //ASC字库字模大小16字节(24*24/8) AreaOffset = 15;//16区以前独立成字库 File1Name = GetAppPath()+"fontHZK24F"; iFileHandle1 = fopen(File1Name.c_str(), "rb"); File2Name = GetAppPath()+"fontASC24"; iFileHandle2 = fopen(File2Name.c_str(), "rb"); File3Name = GetAppPath()+"fontHZK24T"; iFileHandle3 = fopen(File3Name.c_str(), "rb"); break; case 40: HZKSize = 200; //汉字库字模大小32字节(40*40/8) ASCSize = 120; //ASC字库字模大小16字节(40*24/8) AreaOffset = 15;//16区以前独立成字库 File1Name = GetAppPath()+"fontHZK40S"; iFileHandle1 = fopen(File1Name.c_str(), "rb"); File2Name = GetAppPath()+"fontASC40"; iFileHandle2 = fopen(File2Name.c_str(), "rb"); File3Name = GetAppPath()+"fontHZK40T"; iFileHandle3 = fopen(File3Name.c_str(), "rb"); break; case 48: HZKSize = 288; //汉字库字模大小32字节(48*48/8) ASCSize = 144; //ASC字库字模大小16字节(48*24/8) AreaOffset = 15;//16区以前独立成字库 File1Name = GetAppPath()+"fontHZK48S"; iFileHandle1 = fopen(File1Name.c_str(), "rb"); File2Name = GetAppPath()+"fontASC48"; iFileHandle2 = fopen(File2Name.c_str(), "rb"); File3Name = GetAppPath()+"fontHZK48T"; iFileHandle3 = fopen(File3Name.c_str(), "rb"); break; } int DataPos; unsigned char *LineData = new char[HZKSize];//最大可能分配文件块空间 unsigned char Area; //处理后的区 unsigned char Biti; //处理后的位 unsigned char C1, C2;//先后判断的2个字符 int Level; //字符集别:0,Ascii;1,符号(16区以前全角);2,汉字 bool ascii = true; //判断显示位置推动 for (int k = 0; k < Len;)//注意,这里没有k++,循环内调整循环变量 { //========================================================= //一下代码为判断文本级别 C1 = (unsigned char)(S[k]); k = k + 1; if (C1 < 160) Level = 0;//ASCII处理C1 else if (Len>=k+1) { C2 = (unsigned char)(S[k]); if (C2 < 160) Level = 0; //ASCII处理C1 else { if (C1 < 175) Level = 1;//符号处理[C1][C2] else Level = 2;//汉字处理[C1][C2] k = k + 1; } } //========================================================= switch (Level) { case 0: { if (k > 1) { if (ascii) x = x + PixelSize/2; else x = x + PixelSize; } ascii = true; switch (PixelSize) //根据字库特点,对变量进行合理约束 { case 16: Area = C1; break; case 24: Area = C1; if (C1 > 128) Area = 32; break; case 12: case 40: case 48: Area = C1 - 32; if (C1 >= 160) Area = 0; if (C1-32 < 0) Area = 0; break; } DataPos = Area * ASCSize * 1L; fseek(iFileHandle2, DataPos, 0); fread(LineData, ASCSize, 1, iFileHandle2); for (int i = 0; i < PixelSize; i++) { for (int j = 0; j < PixelSize / 2; j++) { innerX = x + j; innerY = y + i; switch (PixelSize) { case 12: case 16: case 40: case 48: if ((LineData[i*((PixelSize/2 + 7)/8)+(j/8)]&Bit[j%8])!=0) //+7***关键*** Canvas->Pixels[innerX][innerY] = clBlack; else Canvas->Pixels[innerX][innerY] = clWhite; break; case 24: if ((LineData[i*((PixelSize + 7)/8)+(j/8)]&Bit[j%8])!=0) //+7***关键*** Canvas->Pixels[innerX][innerY] = clBlack; else Canvas->Pixels[innerX][innerY] = clWhite; break; } } } } break; case 1: case 2: { if (k > 2) { if (ascii) x = x + PixelSize/2; else x = x + PixelSize; } ascii = false; Qu = C1-160; Wei =C2-160; if (Qu > 94) Qu = 94; if (Wei > 94) Wei = 94; if (Level == 2) { switch (PixelSize) { case 24: case 40: case 48: Area = Qu - AreaOffset;break; default:Area = Qu; } iTemp = iFileHandle1; } else { Area = Qu; switch (PixelSize) { case 24: case 40: case 48: iTemp = iFileHandle3; break; default: iTemp = iFileHandle1; } } Biti = Wei; //下面这行代码,计算位置***关键***,尤其是“L”的使用 DataPos = ((Area-1) * 94L + Biti - 1) * HZKSize * 1L; fseek(iTemp, DataPos, 0); fread(LineData, HZKSize, 1, iTemp); for (int i = 0; i < PixelSize; i++) { for (int j = 0; j < PixelSize; j++) { switch (PixelSize) { case 24: innerX = x + i; innerY = y + j; break; case 12: case 16: case 40: case 48: innerX = x + j; innerY = y + i; break; } if ((LineData[i*((PixelSize+7)/8)+(j/8)]&Bit[j%8])!=0) //+7***关键*** Canvas->Pixels[innerX][innerY] = clBlack; else Canvas->Pixels[innerX][innerY] = clWhite; } } } break; } } switch (PixelSize) { case 12: fclose(iFileHandle1); fclose(iFileHandle2); break; case 16: fclose(iFileHandle1); fclose(iFileHandle2); break; case 24: fclose(iFileHandle1); fclose(iFileHandle2); fclose(iFileHandle3); break; case 40: fclose(iFileHandle1); fclose(iFileHandle2); fclose(iFileHandle3); break; case 48: fclose(iFileHandle1); fclose(iFileHandle2); fclose(iFileHandle3); break; } delete []LineData; } }
void __fastcall TForm1::Button2Click(TObject *Sender) { Close(); } //---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender) { AnsiString S = "我a只想说de话,数学:春风冷~!"; ShowHZ(S.c_str(),0,0,24, Canvas); //汉字,x,y,大小(只能是12,16,24,40,48) } //---------------------------------------------------------------------------
void __fastcall TForm1::Button3Click(TObject *Sender) { AnsiString S = ","; unsigned char Qu, Wei; Qu = S[1]-160; Wei = S[2]-160; ShowMessage(S + "字
区:"+IntToStr(Qu)+"
位:"+IntToStr(Wei)); } //---------------------------------------------------------------------------
点阵字库和矢量字库以及源程序和目标程序可以到我的公共邮箱下载. 标题同. 下载附件即可,自行杀毒 (程序可执行文件上传的时候没有病毒,程序没有连接任何其他功能,只是显示汉字而已). 请不要修改密码. 请不要添加,删除或者修改邮件. 谢谢.. 进入方法(用户名和密码都是数字,不包含空格和其他符号): http://mail.163.com 用户名:7453991 密码:1993547 |