Page 1 of 1

Fit font-size to screenwidth V2

Posted: Mon Feb 20, 2017 12:15 pm
by Dutchman
With monospaced fonts, e.g. “Courier” or "Menlo-Regular", the number of characters per line can be fixed.
In the following code the number of characters per line can be set for the editor and for the output:

Code: Select all

'Set Editor and Output V2
'by Dutchman febr. 2017
'Adjust fontsize to the picture-width
'fonts should be monospace: "Menlo-Regular" or "Courier"
'
'--- presets for iPad
Neditor=100 'number of chatacters per line on minimum width
Noutput=80  'number of chatacters per line on minimum width
CALL SetEditor(Neditor)
CALL SetOutput(Noutput)
'
'--- test
FOR i=1 TO Noutput
q$&=i%10
NEXT i
PRINT q$
'check editor-width. Next line is 100 characters long
'234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890
END

'==== Functions
DEF SetEditor(n) 'set style and characters per minimum width
'--- set fontsize
GET SCREEN SIZE sw,sh
fmin=9 'minimum fontsize
fsize=MAX((MIN(sw,sh)-20)/(0.6*n),fmin)
'--- set style
SET EDITOR FONT COLOR 0,0,0
SET EDITOR BACK COLOR 1,.94,.86
SET EDITOR FONT NAME "Menlo-Regular"
SET EDITOR FONT SIZE fsize
'SET EDITOR FONT NAME "Courier"
'SET EDITOR DEFAULT
TEXT CLEAR
END DEF
'
DEF SetOutput(n)'set style and characters per minimum width
GET SCREEN SIZE sw,sh
fmin=9 'minimum fontsize
fsize=MAX((MIN(sw,sh)-20)/(0.6*n),fmin)
SET OUTPUT FONT COLOR 0,0,0
SET OUTPUT BACK COLOR 1,1,1
SET OUTPUT FONT NAME "Menlo-Regular"
SET OUTPUT FONT SIZE fsize
END DEF
In the manual some notes are added about this subject.

Re: Fit font-size to screenwidth

Posted: Wed Feb 22, 2017 5:08 pm
by rbytes
Nice coding! This will be very useful.

Incidentally, I am a big fan of Menlo. I think it is the best-looking monospaced font - and very readable.

Re: Fit font-size to screenwidth

Posted: Thu Feb 23, 2017 10:20 am
by Joel
please be careful. this program changes your customized editor settings!!

Re: Fit font-size to screenwidth

Posted: Tue Feb 28, 2017 8:16 am
by Dutchman
Joel wrote:
Thu Feb 23, 2017 10:20 am
please be careful. this program changes your customized editor settings!!
Yes and it can be restored with SET EDITOR DEFAULT ;)

Re: Fit font-size to screenwidth V2

Posted: Tue Mar 28, 2017 8:34 am
by Dutchman
I updated the code so that fontsize remains the same if device is in horizontal or vertical position.