Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
DAY 013: _FONTWIDTH
#1
What is it?: _FontWidth is a nice simple function which tells us how wide the current font is for the screen we specify.  (Or by itself, for the current _DEST screen.)

How do we use it?: We simply just call it like any other function.  Something as simple as FW = _FontWidth will work just fine.

An example for people:

Code: (Select All)
Screen _NewImage(800, 600, 32)
Print "Default font size:"; _FontWidth, _FontHeight

f = _LoadFont("courbd.ttf", 16, "monospace")
_Font f
Locate 3: Print "Courier Bold, 16, monospace font size:"; _FontWidth; "    "; _FontHeight

f1 = _LoadFont("times.ttf", 20)
_Font f1
Locate 4: Print "Times New Roman, 20 font size:"; _FontWidth, _FontHeight


Copy.  Paste.  Compile.  Run the code above.  

Linux and Mac folks will probably get a whole bunch of zeros and maybe even a few error messages as no fonts are loaded.  You'll need to adjust the links to wherever your fonts are found on your system, and then you can get some sensible results out of the above code.

As for Windows users, your fonts are in a standard place usually: C:\Windows\Fonts\...  QB64-PE knows what folder to try and look for them at, so chances are everything will run as advertised for you guys, without any issues.



So what are the results once the proper fonts are found and pointed to?? 

Default Font Size: 8 wide, 16 high
Courier New, Bold, Font Size: 12 wide 16 high
Times New Roman Font Size: 0 wide 20 high

So, as everyone who's plotted text to the screen ever before should know, the standard font in qb64pe is 8x16 pixels in size.

The courier font, which I chose to load at size 16, happens to be 12 pixels wide and 16 pixels high.

The Times New Roman font however...  ?????

How the heck is it 0 pixels wide????

It's not.  Tongue

When _FontWidth reports a font width of 0, what it's telling you is, "That's not a monospaced font."  

Run the program once again.  Look at the Times New Roman text on the screen.  How many pixels wide is the "T"?  "How many pixels wide is the "i"?  Add the next two lines to the end of the program and run it once again, if you need to be convinced of the widths:

Code: (Select All)
Locate 5: Print "TTT"
Locate 6: Print "iiiiii"

Six "i" characters are about the same width as three "3" characters!  Times New Roman is a variable width font, and I didn't specify "monospace" when loading it, and as such it's impossible to say that the font's width is any set value.  It's 0 -- variable width -- based upon which character is printed to the screen.

And that's the basic rundown on _FontWidth -- it'll basically tell you the width of the font on the screen you specify, or else it'll tell you ZERO, if it's a variable-width font.  Wink
Reply
#2
Is this a keyword of today, tomorrow, or yesterday. I'm losing track. Someone please fix the friggin' Magic 8-bull, or stop feeding it Red Bull.

This is an extremely useful keyword along with _FONTHEIGHT for determining SCREEN size proportions in SCREEN 0. With them, you can size the width, height, and location of your program window in relation to your desktop and keep that ratio and placement relatively close if you change font sizes while running your app. The most recent app I've used this in is Sam Clip. I also coded a SCREEN _NEWIMAGE example for hanness, here...

https://qb64phoenix.com/forum/showthread.php?tid=337

Pete
If eggs are brain food, Biden has his scrambled.

Reply
#3
OFF TOPIC: I guess there's no other place to make such a proposal but... how about this one?
Reply




Users browsing this thread: 1 Guest(s)