Author Topic: _fontwidth & monospace  (Read 1715 times)

0 Members and 1 Guest are viewing this topic.

Offline TempodiBasic

  • Forum Resident
  • Posts: 1792
_fontwidth & monospace
« on: May 12, 2020, 07:32:50 pm »
Hi guys
I run in this issue
using a font monospace I get _FontWidth = 0 while the wiki says me that this case is the only exception to the result = 0....
What have I made wrong?
Thanks to take a look
here the code
Code: QB64: [Select]
  1. CONST white = _RGB32(255, 255, 255), Black = _RGB32(0, 0, 0), Green = _RGB32(0, 255, 0)
  2. SCREEN _NEWIMAGE(800, 600, 32)
  3. t = "text to attempt to write onscreen"
  4. f = _LOADFONT("cour.ttf", 22, "monospace")
  5. COLOR Green, white
  6. COLOR Black, Green
  7.  
and here the wiki
http://www.qb64.org/wiki/FONTWIDTH
Programming isn't difficult, only it's  consuming time and coffee

Offline Petr

  • Forum Resident
  • Posts: 1720
  • The best code is the DNA of the hops.
Re: _fontwidth & monospace
« Reply #1 on: May 13, 2020, 02:02:24 pm »
Hi. You can use _PRINTWIDTH (t$), this return complete t string width in this font.

Offline TempodiBasic

  • Forum Resident
  • Posts: 1792
Re: _fontwidth & monospace
« Reply #2 on: May 14, 2020, 03:39:14 am »
Thanks Petr
this info save my goal with a workaround

_PRINTWIDTH(t$)/LEN(t$) will give me the pixel for each monospace character.
Programming isn't difficult, only it's  consuming time and coffee

Offline TempodiBasic

  • Forum Resident
  • Posts: 1792
Re: _fontwidth & monospace
« Reply #3 on: May 14, 2020, 12:41:16 pm »
thanks again

now I can center the text ...

example/demo
Code: QB64: [Select]
  1. DIM f AS LONG, t AS STRING, Wfon AS INTEGER, Wfo AS INTEGER
  2. DIM Hfo AS INTEGER, Wscr AS LONG, Hscr AS LONG, Lstr AS LONG
  3. DIM Lstr2 AS LONG, X3 AS INTEGER, Y3 AS INTEGER
  4. CONST White = _RGB32(255, 255, 255), Black = _RGB32(0, 0, 0)
  5. CONST Red = _RGB32(255, 0, 0), Green = _RGB32(0, 255, 0)
  6.  
  7.  
  8. SCREEN _NEWIMAGE(800, 600, 32)
  9. t = "text to attempt to write onscreen" ' the text to write
  10. f = _LOADFONT("cour.ttf", 22, "monospace") ' the font used
  11. _FONT f ' loading the font for output
  12. Wscr = _WIDTH 'widht of screen
  13. Hscr = _HEIGHT ' height  of screen
  14. Wfo = _FONTWIDTH(f) ' width of font
  15. Lstr2 = _PRINTWIDTH(t) ' pixel width of lenght of string
  16. Wfon = Lstr2 / LEN(t) ' width of font calculate from _PrintWidth()
  17. Hfo = _FONTHEIGHT(f) ' height of font
  18. Lstr = Wfo * LEN(t) ' lenght of string calculated using _FontWidht
  19. PRINT f, Wfo, Hfo ' it shows _FontWidht 0 result
  20. PRINT f, Wfon, Hfo
  21. PRINT Wscr, Hscr, Lstr, Lstr2
  22. X1 = (Wscr / 2) - (Wfo * LEN(t) / 2)
  23. Y1 = 250
  24. X2 = (Wscr / 2) - (Wfon * LEN(t) / 2)
  25. Y2 = 350
  26. X3 = (Wscr - Lstr2) / 2
  27. Y3 = 420
  28. PRINT X1, Y1
  29. PRINT X2, Y2
  30. PRINT X3, Y3
  31.  
  32. LINE (Wscr / 2, 1)-(Wscr / 2, Hscr), Red
  33. COLOR Green, White
  34. PRINT "Text centered"
  35. PRINT " by _Fontwidth"
  36. COLOR Black, Green
  37. _PRINTSTRING (X1, Y1), t
  38. COLOR Green, White
  39. PRINT " by _printwidth() with same calculation"
  40. COLOR Black, Green
  41. _PRINTSTRING (X2, Y2), t
  42. COLOR Green, White
  43. PRINT " by _printwidth() "
  44. COLOR Black, Green
  45. _PRINTSTRING (X3, Y3), t
  46.  
Programming isn't difficult, only it's  consuming time and coffee

Offline moises1953

  • Newbie
  • Posts: 55
Re: _fontwidth & monospace
« Reply #4 on: May 15, 2020, 01:28:40 am »
Also you can use variables to loadfont:

Code: QB64: [Select]
  1. CONST white = _RGB32(255, 255, 255), Black = _RGB32(0, 0, 0), Green = _RGB32(0, 255, 0)
  2. SCREEN _NEWIMAGE(800, 600, 32)
  3. t = "text to attempt to write onscreen"
  4.  
  5. myfont$ = "cour.ttf"
  6. mysize% = 22
  7. style$ = "monospace"
  8. f = _LOADFONT(myfont$, mysize%, style$)
  9.  
  10. COLOR Green, white
  11. COLOR Black, Green
  12.  

Offline TempodiBasic

  • Forum Resident
  • Posts: 1792
Re: _fontwidth & monospace
« Reply #5 on: May 15, 2020, 07:13:03 am »
Thank Moises1953

strange glitch

Code: QB64: [Select]
  1. CONST white = _RGB32(255, 255, 255), Black = _RGB32(0, 0, 0), Green = _RGB32(0, 255, 0)
  2. SCREEN _NEWIMAGE(800, 600, 32)
  3. t = "text to attempt to write onscreen"
  4.  
  5. myfont$ = "cour.ttf"
  6. mysize% = 22
  7. style$ = "monospace"
  8. f1 = _LOADFONT("cour.ttf", 22, "monospace")
  9. f = _LOADFONT(myfont$, mysize%, style$)
  10.  
  11. COLOR Green, white
  12. LOCATE 1, 1
  13. COLOR Black, Green
  14.  
  15. LOCATE 10, 1
  16. COLOR Green, white
  17.  
  18. COLOR Black, Green
  19.  
  20.  
Programming isn't difficult, only it's  consuming time and coffee

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
Re: _fontwidth & monospace
« Reply #6 on: May 15, 2020, 11:23:19 am »
Interesting! I wonder if _LOADFONT changes or attempts to change the passed value (ie byref) in the mySize% spot.

Update: No, it ( _FONTWIDTH(f) ) just works better with a variable.
« Last Edit: May 15, 2020, 11:35:14 am by bplus »

Offline TempodiBasic

  • Forum Resident
  • Posts: 1792
Re: _fontwidth & monospace
« Reply #7 on: May 16, 2020, 09:18:19 am »
Surely better with variables!  :-)
Programming isn't difficult, only it's  consuming time and coffee