In Delphi, a function that specifies the size font and reads the bitmap information of the font

Source: Internet
Author: User
Tags ord save file first row

In order to control the hardware, it is necessary to convert the vector characters into the lattice information into the EPROM or display on the LCD screen, so with Delphi wrote the following functions, you can specify a Chinese character (two characters) into the lattice information to save to the file, each point corresponds to a bit, there is text information that bit is 1, otherwise 0.

At present, the function can generate the specified size characters and read the matrix font information to save to the file.

such as Converttomatrix (' North '), 6,18, ' Font.dat ') will generate 12*18 lattice file Font.dat, which preserves the Chinese character ' north ' of the font. File format is from top to bottom, after the column, the following figure, the first row to save 00 00, the second row is 90 00 (all 16, the rest of the line analogy)

Convert a Chinese character to a dot-matrix information text is a Chinese character, Chrwidth is a character wide, the Chinese character is two characters, all if you want to generate a width is 12 of the Chinese characters are chrwidth for the 6,chrwidth is currently 8, Because most hardware uses the dot matrix information is 16 below Chrheight is the Chinese character high, Savefilename is saves the Chinese character lattice information the file name.

function Converttomatrix (Text:pchar;
Chrwidth,chrheight:byte; Savefilename:pchar): Bool;
Type
Pbitmapinfo=^tbitmapinfo;
Var
Tempbmp:tbitmap;
Lpvsbits,lpvdbits:pchar;
Doffset,soffset:integer;
DC:HDC;
Thefont:hfont;
Bmiinfo:pbitmapinfo;
Ds:tdibsection;
Bmibuf:array[0..2047]of byte;
i,j:integer;//Cycle Control
wdata:word;//Save dot matrix information for each line of the font, up to 16 digits, less than 16 digits ignoring excess highs
Memorystream:tmemorystream;
Begin
More than one word exits
If Length (Text) >2 Then
Begin
ShowMessage (' Please convert a Chinese character! ');
Result:=false;
Exit;
End
Whether the parameters are reasonable or not
if (chrwidth=0) or (chrheight=0) or (Savefilename = ') Then
Begin
ShowMessage (' parameter Error! ');
Result:=false;
Exit;
End
Create a Stream
Memorystream:=tmemorystream.create;
Create temporary files
Tempbmp:=tbitmap.create;
Set to 256 colors
Tempbmp.pixelformat:= Pf8bit;
Set the width of the chart
Tempbmp.width:=chrwidth * Length (Text);
Set the height of the diagram
Tempbmp.height:= Chrheight;
Get BMP File hDC
Dc:=tempbmp.canvas.handle;
Creating logical Fonts
Thefont: = CreateFont (chrheight,chrwidth, 0, 0, 400, 0, 0, 0,
Gb2312_charset, Out_default_precis, Clip_default_precis,
Default_quality, Default_pitch OR ff_script, ' SCRIPT ');
Specify font to DC
SelectObject (Dc,thefont);
Writes the specified string
TextOut (Dc,0,0,pchar (text), Length (text));
Free logical fonts
DeleteObject (Thefont);
Get BMP information to Lpvsbits
Bmiinfo:=pbitmapinfo (@BMIbuf);
Allocating memory
Lpvsbits:=allocmem (Tempbmp.width*tempbmp.height);
Lpvdbits:=allocmem (Tempbmp.width*tempbmp.height);
Create a program screen compatible DC
DC: = CreateCompatibleDC (0);
Returns the specified BMP information to be saved in the DS
GetObject (Tempbmp.handle, SizeOf (DS), @DS);
Read header information
BMIINFO.BMIHEADER:=DS.DSBMIH;
Read into the DIB
GetDIBits (DC, Tempbmp.handle, 0, Ds.dsbmih.biheight,lpvsbits,
bmiinfo^, dib_rgb_colors);
Inverted image
For i:=0 to Tempbmp.height-1 do
Begin
Soffset:=i*tempbmp.width;
doffset:= (tempbmp.height-i-1) *tempbmp.width;
CopyMemory (Lpvdbits+doffset,lpvsbits+soffset,tempbmp.width);
End
Save File
For i:=0 to Tempbmp.height-1 do
Begin
wdata:=0;
For j:=0 to Tempbmp.width-1 do
Begin
ShowMessage (IntToStr (ord (lpvdbits+i*tempbmp.width+j) ^));
If Ord ((lpvdbits+i*tempbmp.width+j) ^) =0 Then
Begin
wdata:= (wdata SHL 1) or 1;
End
Else
Begin
wdata:= (wdata SHL 1) or 0;
End
End
Memorystream.write (wdata,sizeof (wdata));
End
Memorystream.savetofile (Savefilename);
Memorystream.free;
Tempbmp.savetofile (' temp.bmp ') can be deleted, save ' temp.bmp ' for the purpose of the file is only for contrast viewing
Tempbmp.savetofile (' temp.bmp ');
Tempbmp.free;
Result:=true;
End

Attached: This article is all original content, if you use in the program made changes please send a point to the author webmaster@daheng-image.com; Please specify the author thermometer and email, thank you.

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.