#.textwidth() command

Post Reply
User avatar
rbytes
Posts: 1338
Joined: Sun May 31, 2015 12:11 am
My devices: iPhone 11 Pro Max
iPad Pro 11
MacBook
Dell Inspiron laptop
CHUWI Plus 10 convertible Windows/Android tablet
Location: Calgary, Canada
Flag: Canada
Contact:

#.textwidth() command

Post by rbytes »

This demo uses very similar code to Font Type a few posts back. The screen looks almost identical too. But the new command #.textwidth calculates the width of the first drawtext on each line. The x value for the next word always equals the previous x value plus the previous text's width (variable w)

The spacing is very accurate. Before the #.textwidth() command was created in SPL version 19, the only way to get the correct second x value was by trial and error! Remember to use text size commands right after you define the text, before you make any changes to the font name, type or size.

Code: Select all

'' 
Text Width by rbytes
September 2017
Adapted from Font Type, posted recently

The main difference is that the second drawtext on each line has
its x position calculated by the #.textwidth command, rather than
choosing a value by trial and error. The word spacing is not always 
what I prefer, but at least #.textwidth() gets close enough that only
small correction values are needed.
''

#.fontsize(30)
a = #.normal
b = #.bold
c = #.italic
c1 = "Some helpful "
c2 = "advice ..."
d = "Keep on "
e = "smiling."
f = "It does your face "
g = "good!"

#.drawtext(100,25,"You can use bold and/or italic text to highlight important words.")
#.drawcolor(.4,.4,1)
#.fontsize(60)
#.drawtext(100,75,c1)      ' Some helpful
w = #.textwidth(c1)        ' returns width w of text c1
#.fonttype(c)
#.drawtext(100 + w,75,c2)  ' advice
#.drawcolor(1,0,1)
#.fonttype(a)
#.drawtext(100,150,d)      ' Keep on
w = #.textwidth(d)         ' returns width w of text d.
#.fonttype(b)
#.drawtext(100 + w,150,e)  ' smiling
#.drawcolor(0,.6,.6)
#.fonttype(a)
#.drawtext(100,225,f)      ' It does your face
w = #.textwidth(f)         ' returns width w of text f.
#.fonttype(b,c)
#.drawtext(100 + w,225,g)  ' good!
#.fonttype(a)
#.fontsize(30)
#.drawcolor(0,0,0)
#.drawtext(100,365,"But some specialty fonts may not show the results you expect")
#.fontname("Ranchers")
#.fontsize(60)
#.drawcolor(.4,.4,1)
#.drawtext(100,425,c1)     ' Some helpful
w = #.textwidth(c1)        ' returns width w of text c1.
#.fonttype(c)
#.drawtext(100 + w,425,c2) ' advice
#.drawcolor(1,0,1)
#.fonttype(a)
#.drawtext(100,510,d)      ' Keep on
w = #.textwidth(d)         ' returns width w of text d.
#.fonttype(b)
#.drawtext(100 + w,510,e)  ' smiling
#.drawcolor(0,.6,.6)
#.fonttype(a)
#.drawtext(100,595,f)      ' It does your face
w = #.textwidth(f)         ' returns width w of text f.
#.fonttype(b,c)
#.drawtext(100 + w,595,g)  ' good!
#.fonttype(a)
#.fontname("default")
#.fontsize(30)
#.drawcolor(0,0,0)
#.drawtext(100,690,"Ranchers always looks bold, so using the bold command changes little.")
Attachments
Screenshot (58).png
Screenshot (58).png (128.11 KiB) Viewed 2976 times
Last edited by rbytes on Fri Sep 22, 2017 4:37 am, edited 1 time in total.
The only thing that gets me down is gravity...

User avatar
Mr. Kibernetik
Site Admin
Posts: 4786
Joined: Mon Nov 19, 2012 10:16 pm
My devices: iPhone, iPad, MacBook
Location: Russia
Flag: Russia

Re: #.textwidth() command

Post by Mr. Kibernetik »

You change font type before you measure text width with previous font type, so you get incorrect text width.
For example, here:

Code: Select all

#.drawtext(100,425,c1)     ' Some helpful
#.fonttype(c)
w = #.textwidth(c1)        ' returns width w of text c1.
you print the text, then change font type to C, and only after that you measure text width of printed text. But it is got measured using font type C but not with font type it was printed. This may be the reason of inconsistent spaces.

User avatar
rbytes
Posts: 1338
Joined: Sun May 31, 2015 12:11 am
My devices: iPhone 11 Pro Max
iPad Pro 11
MacBook
Dell Inspiron laptop
CHUWI Plus 10 convertible Windows/Android tablet
Location: Calgary, Canada
Flag: Canada
Contact:

Re: #.textwidth() command

Post by rbytes »

Fixed - see original post.
Last edited by rbytes on Fri Sep 22, 2017 2:32 pm, edited 1 time in total.
The only thing that gets me down is gravity...

User avatar
rbytes
Posts: 1338
Joined: Sun May 31, 2015 12:11 am
My devices: iPhone 11 Pro Max
iPad Pro 11
MacBook
Dell Inspiron laptop
CHUWI Plus 10 convertible Windows/Android tablet
Location: Calgary, Canada
Flag: Canada
Contact:

Re: #.textwidth() command

Post by rbytes »

Couldn't wait. I redid the code, and the spacing is now correct. I have also updated my comments.

You raised an important point, that the font type (and probably the font name and font size) should not be changed before calling any of the text size commands. Perhaps this could be mentioned in the description of these commands.
The only thing that gets me down is gravity...

User avatar
Mr. Kibernetik
Site Admin
Posts: 4786
Joined: Mon Nov 19, 2012 10:16 pm
My devices: iPhone, iPad, MacBook
Location: Russia
Flag: Russia

Re: #.textwidth() command

Post by Mr. Kibernetik »

rbytes wrote:
Fri Sep 22, 2017 4:30 am
Perhaps this could be mentioned in the description of these commands.
Yes, good suggestion. I have updated the documentation.

Post Reply