Home
JAQForum Ver 24.01
Log In or Join  
Active Topics
Local Time 11:18 01 Aug 2025 Privacy Policy
Jump to

Notice. New forum software under development. It's going to miss a few functions and look a bit ugly for a while, but I'm working on it full time now as the old forum was too unstable. Couple days, all good. If you notice any issues, please contact me.

Forum Index : Microcontroller and PC projects : Question on Font defining

Author Message
William Leue
Guru

Joined: 03/07/2020
Location: United States
Posts: 405
Posted: 04:07pm 13 Mar 2021
Copy link to clipboard 
Print this post

The documentation on making fonts is a bit sparse IMHO.
We know this from the user manual:


DefineFont #n
' lots of 32-bit hex values
EndDefine Font


It looks as if the first hex value is used to define the number of words of memory used and the number of rows and columns in a character. But the usage does not seem to be be consistent in the extra fonts that are provided in the EmbeddedFonts folder.

By looking at the various fonts, I think the first 32-bit word is divided as follows:

Bits 0-7 : number of rows
Bits 15-8: number of columns
Bits 31-16: memory usage

For some of the example fonts, the value in bits 31-16 works out correctly to be the number of 16-bit words used in the font, but for others it does not.

Sorry if this has been covered before. Any clues?

Thanks!
-Bill
 
matherp
Guru

Joined: 11/12/2012
Location: United Kingdom
Posts: 10310
Posted: 04:23pm 13 Mar 2021
Copy link to clipboard 
Print this post

Bits 0-7 : number of rows
Bits 15-8: number of columns
Bits 16-23: font start ascii code - typically 0x20 = space
Bits 24-31: number of characters in font - typically 0x5F
 
William Leue
Guru

Joined: 03/07/2020
Location: United States
Posts: 405
Posted: 07:07pm 13 Mar 2021
Copy link to clipboard 
Print this post

Thanks, Peter!
-Bill
 
Geoffg

Guru

Joined: 06/06/2011
Location: Australia
Posts: 3292
Posted: 09:26pm 13 Mar 2021
Copy link to clipboard 
Print this post

This provides a full description: http://www.rinkydinkelectronics.com/h_utft_fonts_101.php

Geoff
Geoff Graham - http://geoffg.net
 
jirsoft

Guru

Joined: 18/09/2020
Location: Czech Republic
Posts: 533
Posted: 10:52pm 13 Mar 2021
Copy link to clipboard 
Print this post

Hi Bill,
for me was worst the rest, data payload (and I'm still hoping I have understand the format correctly, but somehow it works   ).

So maybe take a look into SimplEd, on line 1365 begins SUB importing font from file, on 1249 is export...
Jiri
Napoleon Commander and SimplEd for CMM2 (GitHub),  CMM2.fun
 
William Leue
Guru

Joined: 03/07/2020
Location: United States
Posts: 405
Posted: 02:53pm 14 Mar 2021
Copy link to clipboard 
Print this post

Jiri,

Yes, I did look at SimplEd. I was having a hard time understanding the code. (My stupidity, not your fault.) So I decided to write a little code myself, since this is the best way for me to understand something fully.

I have a font reader that sort-of works. I can successfully read and display fonts that are defined on a 16x16 grid, e.g. arial_bold.bas and swiss_721_outline.bas.

But things go wrong on differently sized grids, with the resulting bitmaps getting distorted.

To make it work for arial_bold and similar fonts, I had to use a weird byte-oriented mapping from the data hex to the position of pixels in the font. I still don't understand why this works, or why it doesn't work for various sizes.

So, for instance, in a 16x16 font, each row of a font character is mapped from the data 32-bit numbers like this:

   Column in Character     Bit Position
   -------------------     ------------
          1                     7
          2                     6
          3                     5
          4                     4
          5                     3
          6                     2
          7                     1
          8                     0
          9                     15
          10                    14
          11                    13
          12                    12
          13                    11
          14                    10
          15                    9
          16                    8

In other words, I have to read the data byte-wise and scan each byte from higher-order to lower-order bits.  This works fine for 16x16 fonts but fails for others.

Any clues?  Thanks!
-Bill
 
matherp
Guru

Joined: 11/12/2012
Location: United Kingdom
Posts: 10310
Posted: 03:56pm 14 Mar 2021
Copy link to clipboard 
Print this post

  Quote  Any clues?  Thanks!


Have a look at the MM.USER_BITMAP subroutine in this thread
 
William Leue
Guru

Joined: 03/07/2020
Location: United States
Posts: 405
Posted: 07:44pm 14 Mar 2021
Copy link to clipboard 
Print this post

Thanks, Peter! That pretty much aligns with what I've already figured out.
-Bill
 
erbp
Senior Member

Joined: 03/05/2016
Location: Australia
Posts: 195
Posted: 01:39am 15 Mar 2021
Copy link to clipboard 
Print this post

Hi Bill,

You might like to check out the FontView Utility that I wrote about 12 months ago - see this thread. The font decoding is able to display various sized fonts correctly. I do agree that the decoding from the Hex values is quite "weird". Hopefully you can follow what the program is doing - I think I included plenty of comments, but its a while since I looked at code.

Cheers,
Phil.
 
TassyJim

Guru

Joined: 07/08/2011
Location: Australia
Posts: 6283
Posted: 04:41am 15 Mar 2021
Copy link to clipboard 
Print this post

Things to remember with fonts.
The character width x height should be a multiple of 8 so that a character ends on a full byte.
The number of bytes will not necessarily be a multiple of 4 so it is unlikely to end at a 32 bit word boundary. That makes working on one character at a time tricky.

After you have worked out the fond size etc, you can calculate the number of bytes for each character.

In FontTweak, I combine all the hex data after the first word into one long string.
Next I change the byte order for each word and separate the individual bytes.
Then I store the required number of bytes for each character in an array (still as strings)

To show a character, I fill an array from the character's string of bytes.

Procedure.l fillArray(txt.s)
 Protected x.l, y.l, n.l ,b.l, num.l
 For n = 0 To fontW*fontH-1 Step 8
   
   thisByte = StringField(txt,n/8+1,",")
   thisByte = "$"+Mid(thisByte,3,2)
   num = Val(thisByte)
   thisByte = RSet(Bin(num,#PB_Byte ),8,"0")
   For b = 0 To 7
     x = (n+b) % fontW
     y = (n+b) / fontW
     If Mid(thisByte,b+1,1)="1"
       fontArray(x,y)=1
     Else
       fontArray(x,y)=0
     EndIf
   Next b
 Next n
 
EndProcedure

Finally, show the character at what ever scale is required.

I haven't tried to convert the PureBasic code into MMBasic.

I have gone the other way - created a font from an image.

It might be of help:
 CLS
 dim integer pixX = 100
 dim integer pixY = 100
 dim integer charCount  = 10
 dim integer charStart  = 48
 dim integer charHeight = 50
 dim integer charWidth  = 32
 dim integer ch,x,y,b
 ' print something to play with
 
 TEXT pixX+1,pixY,"0123456789",lt,9
 
 PRINT @(10,150), "" ' move the cursor to somewhere safe
 
 open "test.fnt" for output as #2
 ' print the header
 print #2, "DEFINEFONT #8"
 txt$ = HEX$(charCount,2)+HEX$(charStart,2)+HEX$(charHeight,2)+HEX$(charWidth,2)
 PRINT #2,txt$
 txt$ = ""
 for ch = 0 to 9
   FOR y = 0 TO charHeight-1
     FOR x = 0 TO charWidth -1
       IF PIXEL(pixX+ch*charWidth +x,pixY+y)= 0 THEN pxl = 0 ELSE pxl = 1
       outBuffer% = (outbuffer%<<1)+ pxl '<<7)
       IF b MOD 8 = 0 AND b <> 0  THEN ' we have filled the first output group
         chunk% = (chunk%>>8) + (outbuffer%<<24)
         outBuffer% = 0
       ENDIF
       IF b MOD 32 = 0 AND b <> 0 THEN ' we have a 'word'
         txt$ = txt$+ HEX$(chunk%,8)+" " ' fill the output line
         IF LEN(txt$) > 70 THEN
           PRINT #2,txt$
           txt$ = ""
         ENDIF
         chunk% = 0
       ENDIF
       b = b + 1
     NEXT x
   NEXT y
 next ch
 txt$ = txt$+ HEX$(chunk%,8)+" "
 
 PRINT #2,txt$
 print #2,"END DEFINEFONT"
 close #2
 
 load font "test.fnt"
 TEXT pixX+1,pixY+70,"0123456789",lt,8
 
END
 
DEFINEFONT #9
 0A303220
 00000000 00000000 00FEFF00 00FFFF01 80FFFF03 60FFFF01 F0FEFF0C F801001E
 F801003F F801003F F801003F F801003F F801003F F801003F F801003F F801003F
 F801003F F801003F F801003F F801003F F801003F 7800003E 18000038 08000020
 00000000 00000020 18000038 7800003E F801003F F801003F F801003F F801003F
 F801003F F801003F F801003F F801003F F801003F F801003F F801003F F801003F
 F801003F F801003F F000001E 60FEFF0C 00FFFF01 80FFFF03 00FFFF01 00FEFF00
 00000000 00000000 00000000 00000000 00000000 00000000 00000000 60000000
 F0000000 F8010000 F8010000 F8010000 F8010000 F8010000 F8010000 F8010000
 F8010000 F8010000 F8010000 F8010000 F8010000 F8010000 F8010000 78000000
 18000000 08000000 00000000 00000000 18000000 78000000 F8010000 F8010000
 F8010000 F8010000 F8010000 F8010000 F8010000 F8010000 F8010000 F8010000
 F8010000 F8010000 F8010000 F8010000 F0000000 60000000 00000000 00000000
 00000000 00000000 00000000 00000000 00000000 00000000 00FEFF00 00FFFF01
 80FFFF03 60FFFF01 F0FEFF00 F8010000 F8010000 F8010000 F8010000 F8010000
 F8010000 F8010000 F8010000 F8010000 F8010000 F8010000 F8010000 F8010000
 F8010000 78000000 18FEFF01 88FFFF03 E0FFFF0F C0FFFF27 00FFFF39 0000003E
 0000003F 0000003F 0000003F 0000003F 0000003F 0000003F 0000003F 0000003F
 0000003F 0000003F 0000003F 0000003F 0000003F 0000003F 0000001E 00FEFF0C
 00FFFF01 80FFFF03 00FFFF01 00FEFF00 00000000 00000000 00000000 00000000
 00FEFF00 00FFFF01 80FFFF03 60FFFF01 F0FEFF00 F8010000 F8010000 F8010000
 F8010000 F8010000 F8010000 F8010000 F8010000 F8010000 F8010000 F8010000
 F8010000 F8010000 F8010000 78000000 18FEFF01 88FFFF03 E0FFFF0F C0FFFF07
 18FFFF01 78000000 F8010000 F8010000 F8010000 F8010000 F8010000 F8010000
 F8010000 F8010000 F8010000 F8010000 F8010000 F8010000 F8010000 F8010000
 F0000000 60FEFF00 00FFFF01 80FFFF03 00FFFF01 00FEFF00 00000000 00000000
 00000000 00000000 00000000 00000000 00000000 60000000 F000000C F801001E
 F801003F F801003F F801003F F801003F F801003F F801003F F801003F F801003F
 F801003F F801003F F801003F F801003F F801003F 7800003E 18FEFF39 88FFFF23
 E0FFFF0F C0FFFF07 18FFFF01 78000000 F8010000 F8010000 F8010000 F8010000
 F8010000 F8010000 F8010000 F8010000 F8010000 F8010000 F8010000 F8010000
 F8010000 F8010000 F0000000 60000000 00000000 00000000 00000000 00000000
 00000000 00000000 00000000 00000000 00FEFF00 00FFFF01 80FFFF03 00FFFF01
 00FEFF0C 0000001E 0000003F 0000003F 0000003F 0000003F 0000003F 0000003F
 0000003F 0000003F 0000003F 0000003F 0000003F 0000003F 0000003F 0000003E
 00FEFF39 80FFFF23 E0FFFF0F C0FFFF07 18FFFF01 78000000 F8010000 F8010000
 F8010000 F8010000 F8010000 F8010000 F8010000 F8010000 F8010000 F8010000
 F8010000 F8010000 F8010000 F8010000 F0000000 60FEFF00 00FFFF01 80FFFF03
 00FFFF01 00FEFF00 00000000 00000000 00000000 00000000 00FEFF00 00FFFF01
 80FFFF03 00FFFF01 00FEFF0C 0000001E 0000003F 0000003F 0000003F 0000003F
 0000003F 0000003F 0000003F 0000003F 0000003F 0000003F 0000003F 0000003F
 0000003F 0000003E 00FEFF39 80FFFF23 E0FFFF0F C0FFFF27 18FFFF39 7800003E
 F801003F F801003F F801003F F801003F F801003F F801003F F801003F F801003F
 F801003F F801003F F801003F F801003F F801003F F801003F F000001E 60FEFF0C
 00FFFF01 80FFFF03 00FFFF01 00FEFF00 00000000 00000000 00000000 00000000
 00FEFF00 00FFFF01 80FFFF03 60FFFF01 F0FEFF00 F8010000 F8010000 F8010000
 F8010000 F8010000 F8010000 F8010000 F8010000 F8010000 F8010000 F8010000
 F8010000 F8010000 F8010000 78000000 18000000 08000000 00000000 00000000
 18000000 78000000 F8010000 F8010000 F8010000 F8010000 F8010000 F8010000
 F8010000 F8010000 F8010000 F8010000 F8010000 F8010000 F8010000 F8010000
 F0000000 60000000 00000000 00000000 00000000 00000000 00000000 00000000
 00000000 00000000 00FEFF00 00FFFF01 80FFFF03 60FFFF01 F0FEFF0C F801001E
 F801003F F801003F F801003F F801003F F801003F F801003F F801003F F801003F
 F801003F F801003F F801003F F801003F F801003F 7800003E 18FEFF39 88FFFF23
 E0FFFF0F C0FFFF27 18FFFF39 7800003E F801003F F801003F F801003F F801003F
 F801003F F801003F F801003F F801003F F801003F F801003F F801003F F801003F
 F801003F F801003F F000001E 60FEFF0C 00FFFF01 80FFFF03 00FFFF01 00FEFF00
 00000000 00000000 00000000 00000000 00FEFF00 00FFFF01 80FFFF03 60FFFF01
 F0FEFF0C F801001E F801003F F801003F F801003F F801003F F801003F F801003F
 F801003F F801003F F801003F F801003F F801003F F801003F F801003F 7800003E
 18FEFF39 88FFFF23 E0FFFF0F C0FFFF07 18FFFF01 78000000 F8010000 F8010000
 F8010000 F8010000 F8010000 F8010000 F8010000 F8010000 F8010000 F8010000
 F8010000 F8010000 F8010000 F8010000 F0000000 60FEFF00 00FFFF01 80FFFF03
 00FFFF01 00FEFF00 00000000 00000000 00000000
END DEFINEFONT



Jim
VK7JH
MMedit
 
William Leue
Guru

Joined: 03/07/2020
Location: United States
Posts: 405
Posted: 11:43am 16 Mar 2021
Copy link to clipboard 
Print this post

Phil said:

  Quote  Hi Bill,

You might like to check out the FontView Utility that I wrote about 12 months ago - see this thread. The font decoding is able to display various sized fonts correctly. I do agree that the decoding from the Hex values is quite "weird". Hopefully you can follow what the program is doing - I think I included plenty of comments, but its a while since I looked at code.

Cheers,
Phil.


Oh, thanks! I had totally forgotten about this.

-Bill
 
Print this page


To reply to this topic, you need to log in.

The Back Shed's forum code is written, and hosted, in Australia.
© JAQ Software 2025