Justifying text

Discussions about the BBC BASIC language, with particular reference to BB4W and BBCSDL
Post Reply
KenDown
Posts: 123
Joined: Wed 04 Apr 2018, 06:36

Justifying text

Post by KenDown » Sun 09 Feb 2020, 11:51

Dear old RiscOS - anyone remember that? - had a system call which justified any text you gave it. I like it and used it a lot. I have been rather disappointed that there does not appear to be a native justification routine in either Windows or (which might be a bit much to expect) BB4W.

I have bodged something which counts the number of space in a line, calculates the length of the line, subtracts that from the desired length, divides the remainder by the number of spaces, and then prints each word in turn, advancing the required distance at each space. It works pretty well, but if anyone knows of a better routine, I'd be grateful.

svein
Posts: 36
Joined: Tue 03 Apr 2018, 19:34

Re: Justifying text

Post by svein » Sun 09 Feb 2020, 14:06

Any help from the manual, 'Aligning proportional-spaced text' ?

Svein

KenDown
Posts: 123
Joined: Wed 04 Apr 2018, 06:36

Re: Justifying text

Post by KenDown » Sun 09 Feb 2020, 20:46

Thanks. As I said, I already calculate the line length and to do that I use the GetTextExtentPoint32 call referred to in the Help. Actually, in theory the WIDTH command in the latest version of BB4W should provide the same answer, but whether I am doing something wrong, it doesn't. It works most of the time and then falls over when I'm least expecting it. At least the SYS call is reliable.

RichardRussell
Posts: 132
Joined: Tue 15 Oct 2019, 09:10

Re: Justifying text

Post by RichardRussell » Mon 17 Feb 2020, 13:52

KenDown wrote:
Sun 09 Feb 2020, 20:46
Actually, in theory the WIDTH command in the latest version of BB4W should provide the same answer, but whether I am doing something wrong, it doesn't.
In fact the WIDTH() function does quite a lot more than the Windows API (as well as having the advantage of being cross-platform, of course):
  • WIDTH() takes account of whether BBC BASIC is running in ANSI or Unicode (UTF-8) mode, and will return the correct result in either state.
  • WIDTH() knows about VDU codes, which the API doesn't. So for example if a string has an embedded colour-change sequence WIDTH() should return the correct value whereas the API will not.
I cannot reproduce WIDTH() returning the wrong value; as far as I can ascertain it is totally reliable in that regard. However I have confirmed that in BB4W it can have an undesirable side-effect in respect of subsequent text or graphics rendering, which I suspect is what you observed. I will address that issue in a future release. BBCSDL is unaffected, and you should be able to use WIDTH() with confidence on that platform.
My posts are moderated; if you are seeing this it has already been approved. If you have a comment about the style or tone of the message please report it to the moderators by clicking the exclamation mark icon rather than complaining on the public forum.

Post Reply