[Home]  [Edit this page]  [Recent Changes]  [Special Pages]  [Help
usefont-pascal
The way I implemented fonts in my programs was I created a picture - an array of numbers denoting colours1 - of all the letters I would use. I then made some procedures that when I told it what letter to draw, it would look at the picture, find the letter and then draw it. The font I made looked like this:
           ABCDEFGHIJK9
           LMNOPQRSTUV7
           WXYZ01234568
Each letter being 4 by 6 pixels. I could Have drawn it bigger, but that is the size I wanted for my program. The next step was to get my program to look at the picture and know where every letter was. What I did was, I made a variable called CMAP (short for Character Map) that contained the string
' ABCDEFGHIJK9LMNOPQRSTUV7WXYZ01234568'
. If you look again at how the picture looked, you will notice that there are 12 characters on a line, but if you stick them all in a row, they look like my CMAP variable. So what I did was, I made my DrawLetter Procedure find the position of the letter in CMAP and then find the row (position DIV 12 - integer division) and the letter in the row (position Mod 12 - remainder) and then told it to start drawing at Row * 6 (Each letter is 6 pixels high) and column * 4 (Width = 4). For example, the number one is at position 31 in CMAP. Since CMAP contains ' ' in it, we need to minus 1. It is therefore at row: 30 Div 12 = 2 and column: 30 mod 12 = 6. Look at the grid of letters and you will see it is actually there!!! So let us apply all this to some code:
{We want to draw it at any XY on the screen with any font that is 4X6 and 
 is based on our CMAP}
Procedure WriteLetter4X6(X,Y : Integer; Lett : Char;Var Fnt : Pic);
{Type PIC is similar to type icon, just bigger - therefore, passed by 
 reference}
Var i, j, Temp, Row, Column : Integer; 
Begin
 Lett := Upcase(Lett);
 If Lett <> ' ' then 
 Row := (Pos(Lett,CMAP)-1) DIV 12; 
 Column := (Pos(Lett,CMAP)-1) MOD 12;
 For i := 0 to 3 do
   For j := 0 to 5 Do
     If Fnt[(X+Column*4)+i,(Y+Row*6)+j] <> 0 Then {0 = transparency colour 
                                                   of font}
              PutPixel(X+i,Y+j, Fnt[(X+Column*4)+i,(Y+Row*6)+j],VGA);
End;


And now that we have something to draw a letter, we can also draw a string. Look at the following procedure:
Procedure WriteString(x,y : Integer; MyString : String; Var Fnt : Pic);
Var i : Integer;
Begin
 For i := 1 to Length(MyString) do 
{Notice that it moves 4 pixels to the left between every leter => (i-1)*4}
  WriteLetter4X6(X+(i-1)*4,Y,MyString[i],Fnt);
End;



Notes

1) An example of what the letter 'A' looks like in the font is:
(0,0,0,0)
(0,0,1,0)
(0,1,0,1)
(0,1,1,1)
(0,1,0,1)
(0,1,0,1)
But that is what it looks like wen I extract it. In the font itself, it is saved next to the letter 'B' - as is shown in CMAP. Here is what it looks like:
(0,0,0,0,0,0,0,0)
(0,0,1,0,0,1,1,0)
(0,1,0,1,0,1,0,1)
(0,1,1,1,0,1,1,0)
(0,1,0,1,0,1,0,1)
(0,1,0,1,0,1,1,0)
This is a 8 by 6 block - two letters next to each other my type 'pic' is a 48 by 18 block - allowing the amount of letters in CMAP to be drawn. I made a small program that allowed me to draw these letters with the mouse onto a grid. I made it save it to a file and then read when I needed to access my fonts. I passed it to my write letter procedure to draw my letters. You will notice that both the letters are padded by '0' both on top and to the left - This is in order that I can write a string of letters and they wont run into each other.

last edited (September 28, 2005) by Relman, Number of views: 3391, Current Rev: 3 (Diff)

[Edit this page]  [Page history]  [What links here]  [Discuss this topic]  [Printer Friendly]  

Members

Username:

Password:


Register
Forgot Password?




Programmers Heaven - for .NET, Java, C/C++ and WEB Developers!
© 1996-2008 Community Networks Ltd. All rights reserved. Reproduction in whole or in part, in any form or medium without express written permission is prohibited. Violators of this policy may be subject to legal action. Please read Terms Of Use and Privacy Statement for more information. Development by Tore Nestenius at .NET Consultant - Synchron Data.