getFontSize

=sub getFontSize= You might want to measure actual font width/height in pixels, if you are using monospaced font (like Courier or Andale Mono) in a graphic box. Using numbers measured on your computer might be unreliable - it actually depends on you desktop settings (font size, DPI settings). For example, on a netbook with settings 120DPI I got width=10, height=20 for Courier New 10, while on a desktop (default 96 DPI) I got width=8, height=16.

It uses GetPixelValue$ function. code format="lb" open "test" for graphics_nsb as #gr gr$="#gr" #gr$ "trapclose [quit]" #gr$ "down; fill white"

fonName$="courier_new" fontSize=10 call getFontSize gr$, fonName$, fontSize, cw, ch   print "font ";fonName$;" ";fontSize print "Font heiht is ";ch;" pixels" print "Font width is ";cw;" pixels"

fonName$="courier" fontSize=10 call getFontSize gr$, fonName$, fontSize, cw, ch   print "font ";fonName$;" ";fontSize print "Font heiht is ";ch;" pixels" print "Font width is ";cw;" pixels"

fonName$="courier_new" fontSize=12 call getFontSize gr$, fonName$, fontSize, cw, ch   print "font ";fonName$;" ";fontSize print "Font heiht is ";ch;" pixels" print "Font width is ";cw;" pixels"

fonName$="courier_new" fontSize=14 call getFontSize gr$, fonName$, fontSize, cw, ch   print "font ";fonName$;" ";fontSize print "Font heiht is ";ch;" pixels" print "Font width is ";cw;" pixels"

wait

[quit] close #gr$ end

sub getFontSize gr$, fonName$, fontSize, byRef cw, byRef ch   #gr$ "cls" #gr$ "place 0 0" #gr$ "font ";fonName$;" ";fontSize #gr$ "color black" #gr$ "backcolor black"

#gr$ "\" #gr$ "posxy x y1" #gr$ "\0123456789" #gr$ "posxy x y2" #gr$ "backcolor white" ch=y2-y1 'char height

for x = 0 to 300 colr$=GetPixelValue$(x, ch/2, gr$) 'print x, colr$ if colr$<>"0 0 0" then cw=x/10 'char width, by 10 characters exit for end if   next end sub

'GetPixelValue$ returns a string with the RGB values of the pixel 'in coordinates x and y in window/graphicbox names handle$ (e.g, "#main.graph") function GetPixelValue$(x, y, handle$)

'Grab a 1*1 bitmap #handle$, "getbmp gpv "; x; " "; y; " "; 1; " "; 1

'Save in a bmp file bmpsave "gpv", "getpvaluetemp.bmp"

'Open the file for string input and get it's full contents open "getpvaluetemp.bmp" for input as #gpv s$ = input$(#gpv, lof(#gpv)) close #gpv

'Check if user's display is 32-bit, and read the red-green-blue values 'If display 16 bit, then colors are masked. So some last (3 for red, 2 for green, 3 for blue) bits always 0 'That means that you did not get 255 255 255 for white - (248 252 248) instead. You have to experiment 'otherwise function returns nothing (support for other display types could be added (?)) bpp = asc(mid$(s$, 29, 1)) select case bpp case 32 red = asc(mid$(s$, 69, 1)) green = asc(mid$(s$, 68, 1)) blue = asc(mid$(s$, 67, 1)) case 16 bytes = asc(mid$( s$, 67, 1)) + 256*asc(mid$( s$, 68, 1)) red = (bytes AND 63488) /256       '0xF800 green = (bytes AND 2016) / 32 * 4  '0x7E0 blue = (bytes AND 31) * 8          '0x1F end select

'concatenate the return value, delete temporary file and free memory GetPixelValue$ = str$(red)+" "+str$(green)+" "+str$(blue) kill "getpvaluetemp.bmp" unloadbmp "gpv" end function

code