Author Topic: Program for neat display of long text  (Read 8014 times)

0 Members and 1 Guest are viewing this topic.

Offline Petr

  • Forum Resident
  • Posts: 1720
  • The best code is the DNA of the hops.
    • View Profile
Program for neat display of long text
« on: February 05, 2022, 09:46:30 am »
This program has one goal. Divide the text where it is expected, ie where there is a space in the text. It looks funny now, but - I went to it so badly again, I won't even describe it, you wouldn't want to believe it anyway...

It's not visible, but - I mean - so much work and so much nonsense. I spend three hours on it (due to a completely disastrous concept) until I finally left the computer, deleted it all, and finally did it completely differently. The original source was a nasty monster and it didn't work. This is short and functional. So if anyone wants to deal with it, it's available here.

Code: QB64: [Select]
  1. 'prikaz TEXT automaticky rozdeli a vycentruje na radky dlouhy text:
  2.  
  3.  
  4. T$ = "Toto je ukazka velice dlouheho textoveho retezce, ktery ma program za ukol rozdelit na nekolik kratsich retezcu a zobrazit je vycentrovane. Deleni je mozne jen v mezerach. For English-speaking friends, this notice in English: This program is designed to split extremely long text so that whole words appear on a line and do not break at the end of a line. Dividing is only possible in spaces between words."
  5.  
  6. Screen NewImage(1024, 768, 32)
  7. f = LoadFont("arial.ttf", 25, "MONOSPACE")
  8. Font f
  9. Text T$
  10.  
  11.  
  12. Sub Text (t As String)
  13.     'spocitam pocet znaku na obrazovku, podle toho rozsekam retezec (v kusu o te delce najdu prvni mezeru zprava)
  14.     TextMax = Width \ FontWidth
  15.     Tr = 1
  16.     t$ = t$ + " "
  17.     Do Until LastSpace = Len(t)
  18.         Word$ = Mid$(t, Tr, TextMax)
  19.         LastSpace = InStrRev(Word$, " ")
  20.         S$ = Mid$(Word$, 1, LastSpace)
  21.         Center = Width / 2 - PrintWidth(S$) / 2
  22.         PrintString (Center, y), S$
  23.         y = y + FontHeight
  24.         t$ = Mid$(t$, LastSpace + 1)
  25.     Loop
  26.  

Offline _vince

  • Seasoned Forum Regular
  • Posts: 422
    • View Profile
Re: Program for neat display of long text
« Reply #1 on: February 05, 2022, 09:52:18 am »
skvely!

Offline Petr

  • Forum Resident
  • Posts: 1720
  • The best code is the DNA of the hops.
    • View Profile
Re: Program for neat display of long text
« Reply #2 on: February 05, 2022, 09:53:54 am »

Offline Petr

  • Forum Resident
  • Posts: 1720
  • The best code is the DNA of the hops.
    • View Profile
Re: Program for neat display of long text
« Reply #3 on: February 05, 2022, 10:16:16 am »
Now is it centered also in Y axis:

Code: QB64: [Select]
  1. 'TEXT statement automaticelly center and divide long text string on the screen. Also in Y axis now.
  2.  
  3.  
  4. T$ = "For English-speaking friends, this notice in English: This program is designed to split extremely long text so that whole words appear on a line and do not break at the end of a line. Dividing is only possible in spaces between words."
  5.  
  6. Screen NewImage(1024, 768, 32)
  7. f = LoadFont("arial.ttf", 25, "MONOSPACE")
  8. Font f
  9. Text T$
  10.  
  11.  
  12. Sub Text (t As String)
  13.     TextMax = Width \ FontWidth
  14.     Rows = Len(t) \ TextMax
  15.     y = Height / 2 - (FontHeight * Rows / 2)
  16.     Tr = 1
  17.     t$ = t$ + " "
  18.     Do Until LastSpace = Len(t)
  19.         Word$ = Mid$(t, Tr, TextMax)
  20.         LastSpace = InStrRev(Word$, " ")
  21.         S$ = Mid$(Word$, 1, LastSpace)
  22.         Center = Width / 2 - PrintWidth(S$) / 2
  23.         PrintString (Center, y), S$
  24.         y = y + FontHeight
  25.         t$ = Mid$(t$, LastSpace + 1)
  26.     Loop
  27.  

Offline MasterGy

  • Seasoned Forum Regular
  • Posts: 327
  • people lie, math never lies
    • View Profile
Re: Program for neat display of long text
« Reply #4 on: February 05, 2022, 12:02:21 pm »
I tried this out of line thing too. I think it’s important to clarify the minimum distance between 2 words and be prepared to break the word if it doesn’t fit in the line.

Code: QB64: [Select]
  1. monx = 800: mony = 600 'window xy size
  2.  
  3. DIM SHARED txts(9), f 'txt-settings
  4.  
  5. txts(0) = 50 'text Y-start
  6. txts(1) = 100 'left column
  7. txts(2) = monx - txts(1) 'right column
  8. txts(3) = 25 'font height
  9. txts(4) = 20 'minimum space between words
  10. txts(5) = 30 'line spacing
  11.  
  12. SCREEN _NEWIMAGE(monx, mony, 32): f = _LOADFONT("arial.ttf", txts(3))
  13.  
  14.  
  15. txt "The tiger (Panthera tigris) is the largest living cat species and a member of the genus Panthera. It is most recognisable for its dark vertical stripes on orange fur with a white underside. An apex predator, it primarily preys on ungulates such as deer and wild boar. It is territorial and generally a solitary but social predator, requiring large contiguous areas of habitat, which support its requirements for prey and rearing of its offspring. Tiger cubs stay with their mother for about two years, then become independent and leave their mother's home range to establish their own."
  16.  
  17.  
  18.  
  19.  
  20. SUB txt (x$)
  21.     _FONT f
  22.     REDIM words$(999)
  23.     DO UNTIL ac > LEN(x$): ac = ac + 1
  24.         a$ = MID$(x$, ac, 1)
  25.         ac2 = ac
  26.         DO: ac2 = ac2 + 1: LOOP WHILE MID$(x$, ac2, 1) <> " " AND ac2 < LEN(x$)
  27.         a$ = MID$(x$, ac - 1, ac2 - ac + 1) '1 word
  28.         FOR af = 1 TO LEN(a$): af$ = MID$(a$, af, 1) 'if a word is longer than the line, break it
  29.             aw = aw + ABS(_PRINTWIDTH(words$(aw) + af$) > ABS(txts(1) - txts(2)))
  30.             words$(aw) = words$(aw) + af$
  31.         NEXT af
  32.         aw = aw + 1
  33.         ac = ac2 + 1
  34.     LOOP
  35.  
  36.     DO UNTIL start_word > aw - 1
  37.         IF words_lenght + _PRINTWIDTH(words$(start_word + words_c)) + txts(4) * words_c > ABS(txts(1) - txts(2)) THEN
  38.             lastx = txts(1)
  39.             words_space = (ABS(txts(1) - txts(2)) - words_lenght) / (words_c - 1)
  40.             y = txts(0) + txts(5) * act_row
  41.             FOR a = 0 TO words_c - 1
  42.                 x = lastx
  43.                 _PRINTSTRING (x, y), words$(a + start_word)
  44.                 lastx = x + words_space + _PRINTWIDTH(words$(a + start_word))
  45.             NEXT a
  46.             start_word = start_word + words_c
  47.             words_c = 0
  48.             act_row = act_row + 1
  49.             words_lenght = 0
  50.         ELSE
  51.             words_lenght = words_lenght + _PRINTWIDTH(words$(start_word + words_c))
  52.             words_c = words_c + 1
  53.         END IF
  54.     LOOP
  55.  
  56.  
  57.  
  58.  
  59.  
  60.  
  61.  

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: Program for neat display of long text
« Reply #5 on: February 05, 2022, 12:55:11 pm »
You might also try this little program:  https://qb64forum.alephc.xyz/index.php?topic=3592.msg129250#msg129250

Code: QB64: [Select]
  1. Screen _NewImage(640, 480, 32)
  2. ReDim foo(0) As String
  3.  
  4. text$ = "This is a bunch of foobar! "
  5. For i = 1 To 10
  6.     text$ = text$ + text$
  7.  
  8. Calculate_WordWrap text$, 300, foo()
  9. Line (301, 0)-(640, 480), &HFFFF0000, BF
  10. For i = 0 To 25
  11.     Print foo(i)
  12.  
  13.  
  14. _Delay .25
  15.  
  16.  
  17.  
  18. 'and now to try with a different font
  19. _Font _LoadFont("courbd.ttf", 12)
  20.  
  21. Print "NOTE:  The next test is with a non-monospaced font, so if your PC is like mine, expect your fans to kick in, CPU usage to skyrocket, and for this process to take a few seconds."
  22. Print "(We're talking 30+ seconds here, so don't think your PC has locked up if it takes that long, or longer, here.)"
  23. Print "NOTE:  This is with a NON-MONOSPACED font."
  24.  
  25. time## = Timer
  26.  
  27. Calculate_WordWrap text$, 300, foo()
  28. Line (301, 0)-(640, 480), &HFFFF0000, BF
  29. For i = 0 To 25
  30.     Print foo(i)
  31. Print Using "###.### seconds, to be exact."; Timer - time##
  32.  
  33.  
  34. _Delay .25
  35.  
  36.  
  37. 'and now to try with a different font
  38. _Font _LoadFont("courbd.ttf", 12, "monospace")
  39.  
  40. Print "NOTE:  This test is with the same font"
  41. Print "       set as MONOSPACE, just to test"
  42. Print "       the speed difference."
  43. time## = Timer
  44.  
  45. Calculate_WordWrap text$, 300, foo()
  46. Line (301, 0)-(640, 480), &HFFFF0000, BF
  47. For i = 0 To 25
  48.     Print foo(i)
  49. Print Using "###.### seconds, to be exact."; Timer - time##
  50.  
  51.  
  52.  
  53. Sub Calculate_WordWrap (temp$, wide%, result() As String)
  54.     Dim BreakPoint As String
  55.     BreakPoint = ",./- ;:!" 'I consider all these to be valid breakpoints.  If you want something else, change them.
  56.  
  57.     text$ = _Trim$(temp$)
  58.     Do
  59.         'first find the natural length of the line
  60.         For i = 1 To Len(text$)
  61.             p = _PrintWidth(Left$(text$, i))
  62.             If p > wide% Then Exit For
  63.         Next
  64.         lineend = i - 1
  65.         If lineend = 0 Then GoTo clean_exit
  66.         t$ = RTrim$(Left$(text$, lineend)) 'at most, our line can't be any longer than what fits the screen.
  67.         For i = lineend To 1 Step -1
  68.             If InStr(BreakPoint, Mid$(text$, i, 1)) Then lineend = i: Exit For
  69.         Next
  70.         result(count) = RTrim$(Left$(text$, lineend))
  71.         count = count + 1
  72.         If count > UBound(result) Then ReDim _Preserve result(count + 100) As String
  73.         text$ = LTrim$(Mid$(text$, lineend + 1))
  74.     Loop Until text$ = ""
  75.     clean_exit:
  76.     ReDim _Preserve result(count) As String
  77.  

Convert text to a word-wrapped array().  ;)
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Program for neat display of long text
« Reply #6 on: February 05, 2022, 01:49:47 pm »
Now is it centered also in Y axis:

Code: QB64: [Select]
  1. 'TEXT statement automaticelly center and divide long text string on the screen. Also in Y axis now.
  2.  
  3.  
  4. T$ = "For English-speaking friends, this notice in English: This program is designed to split extremely long text so that whole words appear on a line and do not break at the end of a line. Dividing is only possible in spaces between words."
  5.  
  6. Screen NewImage(1024, 768, 32)
  7. f = LoadFont("arial.ttf", 25, "MONOSPACE")
  8. Font f
  9. Text T$
  10.  
  11.  
  12. Sub Text (t As String)
  13.     TextMax = Width \ FontWidth
  14.     Rows = Len(t) \ TextMax
  15.     y = Height / 2 - (FontHeight * Rows / 2)
  16.     Tr = 1
  17.     t$ = t$ + " "
  18.     Do Until LastSpace = Len(t)
  19.         Word$ = Mid$(t, Tr, TextMax)
  20.         LastSpace = InStrRev(Word$, " ")
  21.         S$ = Mid$(Word$, 1, LastSpace)
  22.         Center = Width / 2 - PrintWidth(S$) / 2
  23.         PrintString (Center, y), S$
  24.         y = y + FontHeight
  25.         t$ = Mid$(t$, LastSpace + 1)
  26.     Loop
  27.  

@Petr I like the centering but your code fails with super long "words", fails even to include. This was MasterGy's point I think:
Code: QB64: [Select]
  1. 'TEXT statement automaticelly center and divide long text string on the screen. Also in Y axis now.
  2.  
  3.  
  4. T$ = "For English-speaking friends, this notice in English: This program is designed to split extremely long text so that whole words appear on a line and do not break at the end of a line. Dividing is only possible in spaces between words."
  5. T$ = T$ + " Lets add some really long strings like ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890-ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890"
  6. T$ = T$ + " Plus, this shorty."
  7. Screen NewImage(600, 720, 32)
  8. f = LoadFont("arial.ttf", 25, "MONOSPACE")
  9. Font f
  10. Text T$
  11.  
  12.  
  13. Sub Text (t As String)
  14.     TextMax = Width \ FontWidth
  15.     Rows = Len(t) \ TextMax
  16.     y = Height / 2 - (FontHeight * Rows / 2)
  17.     Tr = 1
  18.     t$ = t$ + " "
  19.     Do Until LastSpace = Len(t)
  20.         Word$ = Mid$(t, Tr, TextMax)
  21.         LastSpace = InStrRev(Word$, " ")
  22.         S$ = Mid$(Word$, 1, LastSpace)
  23.         Center = Width / 2 - PrintWidth(S$) / 2
  24.         PrintString (Center, y), S$
  25.         y = y + FontHeight
  26.         t$ = Mid$(t$, LastSpace + 1)
  27.     Loop
  28.  
  29.  
  30.  

It just quits??

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Program for neat display of long text
« Reply #7 on: February 05, 2022, 01:55:01 pm »
Steve's wordWrap holds up pretty well testing same string and font on same size screen, be nice to see centering of each line, not a hard mod after have the array of strings and could use Ubound of array to center height as well.
Code: QB64: [Select]
  1. Screen _NewImage(600, 720, 32)
  2. ReDim foo(0) As String
  3.  
  4. Text$ = "For English-speaking friends, this notice in English: This program is designed to split extremely long text so that whole words appear on a line and do not break at the end of a line. Dividing is only possible in spaces between words."
  5. Text$ = Text$ + " Lets add some really long strings like ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890"
  6. Text$ = Text$ + " Plus, this shorty."
  7. Calculate_WordWrap Text$, 600, foo()
  8. For i = 0 To UBound(foo)
  9.     Print foo(i)
  10.  
  11.  
  12. _Delay .25
  13.  
  14.  
  15.  
  16. 'and now to try with a different font
  17.  
  18. _Font _LoadFont("arial.ttf", 25, "MONOSPACE")
  19.  
  20. 'Print "NOTE:  The next test is with a non-monospaced font, so if your PC is like mine, expect your fans to kick in, CPU usage to skyrocket, and for this process to take a few seconds."
  21. 'Print "(We're talking 30+ seconds here, so don't think your PC has locked up if it takes that long, or longer, here.)"
  22. 'Print
  23. 'Print "NOTE:  This is with a NON-MONOSPACED font."
  24.  
  25. time## = Timer
  26.  
  27. Calculate_WordWrap Text$, 600, foo()
  28. 'Print UBound(foo)
  29. For i = 0 To UBound(foo)
  30.     Print foo(i)
  31. Print Using "###.### seconds, to be exact."; Timer - time##
  32.  
  33.  
  34. '_Delay .25
  35. '_KeyClear
  36.  
  37.  
  38. 'Cls
  39. ''and now to try with a different font
  40. '_Font _LoadFont("courbd.ttf", 12, "monospace")
  41.  
  42. 'Print "NOTE:  This test is with the same font"
  43. 'Print "       set as MONOSPACE, just to test"
  44. 'Print "       the speed difference."
  45. 'time## = Timer
  46.  
  47. 'Calculate_WordWrap Text$, 300, foo()
  48. 'Line (301, 0)-(640, 480), &HFFFF0000, BF
  49. 'Print UBound(foo)
  50. 'For i = 0 To 25
  51. '    Print foo(i)
  52. 'Next
  53. 'Print Using "###.### seconds, to be exact."; Timer - time##
  54.  
  55.  
  56.  
  57. Sub Calculate_WordWrap (temp$, wide%, result() As String)
  58.     Dim BreakPoint As String
  59.     BreakPoint = ",./- ;:!" 'I consider all these to be valid breakpoints.  If you want something else, change them.
  60.  
  61.     text$ = _Trim$(temp$)
  62.     Do
  63.         'first find the natural length of the line
  64.         For i = 1 To Len(text$)
  65.             p = _PrintWidth(Left$(text$, i))
  66.             If p > wide% Then Exit For
  67.         Next
  68.         lineend = i - 1
  69.         If lineend = 0 Then GoTo clean_exit
  70.         t$ = RTrim$(Left$(text$, lineend)) 'at most, our line can't be any longer than what fits the screen.
  71.         For i = lineend To 1 Step -1
  72.             If InStr(BreakPoint, Mid$(text$, i, 1)) Then lineend = i: Exit For
  73.         Next
  74.         result(count) = RTrim$(Left$(text$, lineend))
  75.         count = count + 1
  76.         If count > UBound(result) Then ReDim _Preserve result(count + 100) As String
  77.         text$ = LTrim$(Mid$(text$, lineend + 1))
  78.     Loop Until text$ = ""
  79.     clean_exit:
  80.     ReDim _Preserve result(count) As String
  81.  
  82.  
  83.  

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Program for neat display of long text
« Reply #8 on: February 05, 2022, 02:05:01 pm »
@MasterGy

I started a test of your code and was stuck trying to test same font and screen size as I did to the 2 others.
Seems to me a bad idea to do this part BEFORE You load the Font, what if Font Height > 25 but I like how everything would be controlled by screen size and Font dimensions, even the space between the strings.

EDIT: Strikeout section because I see what you did now with FontHeight, used it for loading the font, nice!

Oh wow! Newspaper style spacing between words:
 
Wrap MasterGY.PNG
« Last Edit: February 05, 2022, 02:20:39 pm by bplus »

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Program for neat display of long text
« Reply #9 on: February 05, 2022, 02:43:00 pm »
Oh here is my test code of MasterGy wrap trying out the margins:
Code: QB64: [Select]
  1. monx = 600: mony = 720 'window xy size
  2.  
  3. Dim Shared txts(9), f 'txt-settings
  4.  
  5.  
  6. txts(0) = 0 'text Y-start
  7. txts(1) = 50 'left column
  8. txts(2) = monx - txts(1) 'right column same margin as left
  9. txts(3) = 25 'font height
  10. txts(4) = 10 'minimum space between words
  11. txts(5) = 30 'line spacing
  12.  
  13. Screen _NewImage(monx, mony, 32):
  14. f = _LoadFont("arial.ttf", txts(3)) ' this is not going to be "MonoSpace" test
  15. Text$ = "For English-speaking friends, this notice in English: This program is designed to split extremely long text so that whole words appear on a line and do not break at the end of a line. Dividing is only possible in spaces between words."
  16. Text$ = Text$ + " Lets add some really long strings like ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890"
  17. Text$ = Text$ + " Plus, this shorty."
  18.  
  19. txt Text$
  20.  
  21.  
  22. Sub txt (x$)
  23.     _Font f
  24.     ReDim words$(999)
  25.     Do Until ac > Len(x$): ac = ac + 1
  26.         a$ = Mid$(x$, ac, 1)
  27.         ac2 = ac
  28.         Do: ac2 = ac2 + 1: Loop While Mid$(x$, ac2, 1) <> " " And ac2 < Len(x$)
  29.         a$ = Mid$(x$, ac - 1, ac2 - ac + 1) '1 word
  30.         For af = 1 To Len(a$): af$ = Mid$(a$, af, 1) 'if a word is longer than the line, break it
  31.             aw = aw + Abs(_PrintWidth(words$(aw) + af$) > Abs(txts(1) - txts(2)))
  32.             words$(aw) = words$(aw) + af$
  33.         Next af
  34.         aw = aw + 1
  35.         ac = ac2 + 1
  36.     Loop
  37.  
  38.     Do Until start_word > aw - 1
  39.         If words_lenght + _PrintWidth(words$(start_word + words_c)) + txts(4) * words_c > Abs(txts(1) - txts(2)) Then
  40.             lastx = txts(1)
  41.             words_space = (Abs(txts(1) - txts(2)) - words_lenght) / (words_c - 1)
  42.             y = txts(0) + txts(5) * act_row
  43.             For a = 0 To words_c - 1
  44.                 x = lastx
  45.                 _PrintString (x, y), words$(a + start_word)
  46.                 lastx = x + words_space + _PrintWidth(words$(a + start_word))
  47.             Next a
  48.             start_word = start_word + words_c
  49.             words_c = 0
  50.             act_row = act_row + 1
  51.             words_lenght = 0
  52.         Else
  53.             words_lenght = words_lenght + _PrintWidth(words$(start_word + words_c))
  54.             words_c = words_c + 1
  55.         End If
  56.     Loop
  57.  

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: Program for neat display of long text
« Reply #10 on: February 05, 2022, 07:17:28 pm »
Steve's wordWrap holds up pretty well testing same string and font on same size screen, be nice to see centering of each line, not a hard mod after have the array of strings and could use Ubound of array to center height as well.

That's one of the advantages to making it an array -- right, left, center justification is simple to do, as is scrollable text.   For example, have 25 lines of text, and only 10 lines of screen space to display them in??   No problem!  Set an input loop to use the arrow keys to increase/decrease a counter, and then display your lines starting from counter to counter + 10!

Many advantages to making the array first before displaying.  ;)
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline Petr

  • Forum Resident
  • Posts: 1720
  • The best code is the DNA of the hops.
    • View Profile
Re: Program for neat display of long text
« Reply #11 on: February 06, 2022, 03:53:53 am »
Thank you all for your many inspiring ideas and examples. The MasterGy solution is beautiful, the test with the Steve program took me 61 seconds, and to comment on BPlus - you're right, my program just didn't expect that there would be so many characters in the text without a space. I see that you can really improve a lot on this as well. The solution at the beginning of this thread is still sufficient for the purpose for which it was written. I will publish the exact use and the reason why I deal with it soon. It is again just one fragment from a larger program. Thank you all for your many inspiring suggestions!

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: Program for neat display of long text
« Reply #12 on: February 06, 2022, 08:50:14 am »
@Petr 61 seconds for a non-monospaced font.  Probably less than a second for a monospaced font.   It's a highlight of the difference in how _PRINTWIDTH calculates the two.

With monospaced fonts, printwidth can calculate width by character.  (Character count * fontwidth)  it's a quick and simple math calculation.  With non-monospaced fonts, the routine has to check and build width pixel by pixel as it goes. A "W" might spread and use 64 pixels, an "i" might use 8...  non-monospaced fonts are *much* slower to calculate print widths with, and that's something I wanted to emphasize with my demo.

I wouldn't plug in that little text to word-wrap array routine if I was using non-monospaced fonts, as I'd have to hunt for a faster, more efficient way to build and deal with those (perhaps an array with pre-calculated font character widths to work with?), but it'll work fine all the rest ofthe time.  ;)
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline _vince

  • Seasoned Forum Regular
  • Posts: 422
    • View Profile
Re: Program for neat display of long text
« Reply #13 on: February 06, 2022, 09:58:31 am »
With non-monospaced fonts, the routine has to check and build width pixel by pixel as it goes. A "W" might spread and use 64 pixels, an "i" might use 8...  non-monospaced fonts are *much* slower to calculate print widths with, and that's something I wanted to emphasize with my demo.

Is this a one dimensional form of OCR? the difference in aka screen 0 printing

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: Program for neat display of long text
« Reply #14 on: February 06, 2022, 10:45:27 am »
Is this a one dimensional form of OCR? the difference in aka screen 0 printing

Not really.  _PRINTWIDTH, from I remember, basically prints your text on a hidden blank screen 10,000 pixels wide and _FONTHEIGHT tall.  Then it starts at the 10,000th pixel and looks for a white pixel (text), instead of a black one (empty space).  When it first encounters the white pixel, it quits and returns that as a value for the printwidth.

It's a hack that works, but it's slow.  (Imagine a font 20 pixels tall, and your text is only 123 pixels in width in total...  That's 20 (pixels per vertical line) x 9877 (vertical lines) comparisons before it returns 123 as your solution.

Compared to a monospaced font which is (character count * fontwidth) in size. 

No real OCR going on anywhere; just a hunt for color on a blank background, but calculating non-monospaced text width is a slow command, as illustrated via my demo above.  If you need speed, you'll want to write your own custom width calculation routines.
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!