Author Topic: Simple page formatting  (Read 3230 times)

0 Members and 1 Guest are viewing this topic.

Offline Richard Frost

  • Seasoned Forum Regular
  • Posts: 316
  • Needle nardle noo. - Peter Sellers
    • View Profile
Simple page formatting
« on: November 05, 2020, 11:15:54 pm »
'Ave ye ever wanted to plunk a page of text to the screen, yet be able to edit it
ad infinitum, or change the font, without having to monkey with the formatting? 
Here's a simple way.  It's also the heart of a book reader.

Code: QB64: [Select]
  1. DEFLNG A-Z
  2. SCREEN _NEWIMAGE(480, 600, 32) '                                                   whatever screen you like
  3.  
  4. f = _LOADFONT("liberati.ttf", 14, "BOLD") '                                        whatever font you like, or none
  5. _FONT f '                                                                          use the font, or not
  6.  
  7. xmar = 20: ymar = 20 '                                                             margins, vary to suit taste/requirements
  8. xmin = xmar: xmax = _WIDTH - xmar
  9. ymin = ymar: ymax = _HEIGHT - ymar
  10. x = xmin: y = ymin
  11.  
  12.     READ t$
  13.     IF t$ = "end" THEN EXIT DO
  14.     DO
  15.         p = INSTR(t$, " ") '                                                       position of next space in line
  16.         IF p = 0 THEN p = LEN(t$) '                                                no space, take whatever is remaining
  17.         IF p = 0 THEN EXIT DO '                                                    nothing to see here, people, move along
  18.         w$ = LEFT$(t$, p) '                                                        word
  19.         t$ = RIGHT$(t$, LEN(t$) - LEN(w$)) '                                       remove word from line
  20.         nl = 0: IF RIGHT$(w$, 2) = "/p" THEN nl = 1: w$ = LEFT$(w$, LEN(w$) - 2) ' detect new paragraph, and strip command from word
  21.         w$ = RTRIM$(w$) + " " '                                                    because there's no space between lines in the data statements
  22.         IF (x + _PRINTWIDTH(w$)) > xmax THEN x = xmin: y = y + 16 '                won't fit, start new line
  23.         _PRINTSTRING (x, y), w$ '                                                  show word
  24.         x = x + _PRINTWIDTH(w$) '                                                  git along, little doggy
  25.         IF nl THEN x = xmin: y = y + 32 '                                          new paragraph
  26.     LOOP UNTIL y > ymax '                                                          ran out of space on the page
  27.  
  28. DATA "Chess Information/p"
  29. DATA ""
  30. DATA "Move a piece by clicking on it, then click the destination square, or move the"
  31. DATA "mouse cursor off the board and use the arrow keys and Enter.  The program will"
  32. DATA "not allow illegal moves.  If you change your mind about moving a piece after"
  33. DATA "having selected it, move it to an illegal spot or press Esc./p"
  34. DATA ""
  35. DATA "If you quit the program by accident, the game may be resumed by selecting File"
  36. DATA "Playback in the Setup menu and choose the highest numbered .alg file.  Games"
  37. DATA "are saved with the filenames chNNNNNN.alg, with N being a number in sequence./p"
  38. DATA ""
  39. DATA "Selet options by letter or mouse click.  Castling and en passant are supported./p"
  40. DATA ""
  41. DATA "If the clock color is set to Thermal (the default), the colors indicate CPU"
  42. DATA "load - green normal, yellow >74%, and red >94%.  Delays are forced at red, but"
  43. DATA "it may not be enough to prevent your computer from going into thermal shutdown "
  44. DATA "(rebooting).  I suggest you do not run this program while web browsing and"
  45. DATA "playing music videos. There's also a thermometer option, but it's ugly./p"
  46. DATA ""
  47. DATA "The plasma effect can be restarted with g (graphics), accentuated with ', or"
  48. DATA "turned off in Setup. PgUp/PgDn controls the sound volume./p"
  49. DATA ""
  50. DATA "Exit the program by clicking the local X at top right or the game will not be"
  51. DATA "saved.  More information, extra commands, and keyboard shortcuts may be"
  52. DATA "found by looking at the source code./p"
  53. DATA ""
  54. DATA "Esc: Return to game"
  55.  
  56. DATA "end"
  57.  
« Last Edit: November 05, 2020, 11:17:18 pm by Richard Frost »
It works better if you plug it in.

Offline Richard Frost

  • Seasoned Forum Regular
  • Posts: 316
  • Needle nardle noo. - Peter Sellers
    • View Profile
Re: Simple page formatting
« Reply #1 on: November 05, 2020, 11:38:52 pm »
Er - the 16 should be (_FONTHEIGHT+2) and the 32 should be ((_FONTHEIGHT+2)*2).

Anyway, see how little code it takes to reformat text. 
It works better if you plug it in.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Simple page formatting
« Reply #2 on: November 05, 2020, 11:40:19 pm »
Quote
Anyway, see how little code it takes to reformat text.
 

Yeah, that's pretty compact!

Offline Petr

  • Forum Resident
  • Posts: 1720
  • The best code is the DNA of the hops.
    • View Profile
Re: Simple page formatting
« Reply #3 on: November 06, 2020, 01:11:05 pm »
That's nice. There are many other things that can be done about it.