Page 1 of 2
TextWidth - strange on Mac OS :-)
Posted: Sat Nov 11, 2017 9:29 pm
by walbus
Try this, it took me a while to realize what was going on

LOL
Code: Select all
CreateImage(1, 100, 100)
StartDrawing(ImageOutput(1))
Debug TextWidth("Hello World")
Debug TextWidth("H")+TextWidth("e")+TextWidth("l")+TextWidth("l")+TextWidth("o")+
TextWidth(" ")+TextWidth("W")+TextWidth("o")+TextWidth("r")+TextWidth("l")+TextWidth("d")
StopDrawing()
Re: TextWidth - strange on Mac OS :-)
Posted: Sat Nov 11, 2017 11:38 pm
by J. Baker
I'm missing something? Debug...
64
64
Re: TextWidth - strange on Mac OS :-)
Posted: Sat Nov 11, 2017 11:55 pm
by walbus
J. Baker, your result looks OK
Its strange...
On Win and Linux i get for this sample above both values equal (PB561)
On Mac OS 10.12 -10.13 i get 61 55 (PB 561)
With any text, the difference is approximately one character width
I am sure, different users can confirm it
The question is, what is the reason ?
Re: TextWidth - strange on Mac OS :-)
Posted: Sun Nov 12, 2017 6:22 am
by davido
@
walbus,
My system: MacBook Pro
System Version: OS X 10.10.5 (14F2511)
Kernel Version: Darwin 14.5.0
PureBasic 5.61 x64
Debugger wrote:
61
55
Re: TextWidth - strange on Mac OS :-)
Posted: Sun Nov 12, 2017 9:19 am
by walbus
@davido Many thanks for confirming
Meanwhile, I assume a PB bug
I post it in the Bug forum, now
It can be very unpleasant and strange for text length calculations
Code: Select all
CreateImage(1, 300, 100)
OpenWindow(1, 0, 0, 300, 100, "", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
StartDrawing(ImageOutput(1))
Debug TextWidth("Hello World")
twidth=TextWidth("H")+TextWidth("e")+TextWidth("l")+TextWidth("l")+TextWidth("o")+
TextWidth(" ")+TextWidth("W")+TextWidth("o")+TextWidth("r")+TextWidth("l")+TextWidth("d")
Debug twidth
Box(0, 0, twidth, 20, $FF)
DrawText(0, 0, "Hello World")
StopDrawing()
ImageGadget(1, 0, 0, 0, 0, ImageID(1))
Repeat : Until WaitWindowEvent()=#PB_Event_CloseWindow
Re: TextWidth - strange on Mac OS :-)
Posted: Sun Nov 12, 2017 2:14 pm
by #NULL
i have no mac but the same problem on linux
Code: Select all
CreateImage(1, 100, 100)
StartDrawing(ImageOutput(1))
Debug TextWidth("Hello World")
Debug TextWidth("H")+TextWidth("e")+TextWidth("l")+TextWidth("l")+TextWidth("o")+
TextWidth(" ")+TextWidth("W")+TextWidth("o")+TextWidth("r")+TextWidth("l")+TextWidth("d")
StopDrawing()
Debug ""
LoadFont(0, "arial", 12)
CreateImage(1, 100, 100)
StartDrawing(ImageOutput(1))
DrawingFont(FontID(0))
Debug TextWidth("Hello World")
Debug TextWidth("H")+TextWidth("e")+TextWidth("l")+TextWidth("l")+TextWidth("o")+
TextWidth(" ")+TextWidth("W")+TextWidth("o")+TextWidth("r")+TextWidth("l")+TextWidth("d")
StopDrawing()
Debug ""
CreateImage(1, 100, 100)
StartDrawing(ImageOutput(1))
DrawingFont(#PB_Default)
Debug TextWidth("Hello World")
Debug TextWidth("H")+TextWidth("e")+TextWidth("l")+TextWidth("l")+TextWidth("o")+
TextWidth(" ")+TextWidth("W")+TextWidth("o")+TextWidth("r")+TextWidth("l")+TextWidth("d")
StopDrawing()
96
97
84
84
96
97
seems to happen only with the default font.
ubuntu 16.04, pb 5.61 x64
Re: TextWidth - strange on Mac OS :-)
Posted: Sun Nov 12, 2017 2:38 pm
by Wolfram
I get always the same result.
Can you try the example?
Code: Select all
CreateImage(1, 100, 100)
StartDrawing(ImageOutput(1))
Debug TextWidth("Hello World")
Debug TextWidth("H")+TextWidth("e")+TextWidth("l")+TextWidth("l")+TextWidth("o")+
TextWidth(" ")+TextWidth("W")+TextWidth("o")+TextWidth("r")+TextWidth("l")+TextWidth("d")
StopDrawing()
Debug ""
LoadFont(0, "Andale Mono", 12) ; has fix tracking
CreateImage(1, 100, 100)
StartDrawing(ImageOutput(1))
DrawingFont(FontID(0))
Debug TextWidth("Hello World")
Debug TextWidth("H")+TextWidth("e")+TextWidth("l")+TextWidth("l")+TextWidth("o")+
TextWidth(" ")+TextWidth("W")+TextWidth("o")+TextWidth("r")+TextWidth("l")+TextWidth("d")
StopDrawing()
Debug ""
CreateImage(1, 100, 100)
StartDrawing(ImageOutput(1))
DrawingFont(#PB_Default)
Debug TextWidth("Hello World")
Debug TextWidth("H")+TextWidth("e")+TextWidth("l")+TextWidth("l")+TextWidth("o")+
TextWidth(" ")+TextWidth("W")+TextWidth("o")+TextWidth("r")+TextWidth("l")+TextWidth("d")
StopDrawing()
Re: TextWidth - strange on Mac OS :-)
Posted: Sun Nov 12, 2017 3:02 pm
by walbus
Yep, on Linux Mint 18 x64/x86
96
97
110
110
96
97
On Win 10 all equal
On Mac 10.12 - 10.13
61
55
79
77
61
55
Stretched text looks so (Stretch factor here x=7, y=7, output from a demo code) :

Re: TextWidth - strange on Mac OS :-)
Posted: Sun Nov 12, 2017 5:37 pm
by J. Baker
What's your default font? Maybe it's not mono spaced?
Re: TextWidth - strange on Mac OS :-)
Posted: Sun Nov 12, 2017 6:14 pm
by Wolfram
J. Baker wrote:What's your default font? Maybe it's not mono spaced?
Thats why I ask to tested with my example. This use a fix tracking size.
http://www.purebasic.fr/english/viewtop ... 09#p514809
Re: TextWidth - strange on Mac OS :-)
Posted: Sun Nov 12, 2017 7:13 pm
by Josh
I think that depends on the font. It is not said that two separate characters result in the same width as the two same characters together.
https://en.wikipedia.org/wiki/Kerning
Re: TextWidth - strange on Mac OS :-)
Posted: Sun Nov 12, 2017 7:51 pm
by Wolfram
Josh wrote:I think that depends on the font. It is not said that two separate characters result in the same width as the two same characters together.
https://en.wikipedia.org/wiki/Kerning
Yes, and to be sure that this is the problem here use my example code please.
I use a fond which does't use kerning.
Re: TextWidth - strange on Mac OS :-)
Posted: Sun Nov 12, 2017 8:18 pm
by davido
@
Wolfram,
I ran your code and got the following results:
Debug wrote:61
55
79
77
61
55
My system: MacBook Pro
System Version: OS X 10.10.5 (14F2511)
Kernel Version: Darwin 14.5.0
PureBasic 5.61 x64
Re: TextWidth - strange on Mac OS :-)
Posted: Sun Nov 12, 2017 10:07 pm
by Dude
J. Baker wrote:What's your default font? Maybe it's not mono spaced?
I already mentioned fonts and kerning in this other thread:
http://www.purebasic.fr/english/viewtop ... 24&t=69616
That's the problem with discussing the exact same thing in two threads: the answers go everywhere. One of these threads should be deleted.
As for the issue being Mac-specific, I just read on Wikipedia:
Wikipedia wrote:Apple has however extended the kern table with more complex features, including contextual and class-based kerning
So that may explain why the issue appears on iOS and not Windows/Linux?
Re: TextWidth - strange on Mac OS :-)
Posted: Sun Nov 12, 2017 10:44 pm
by Wolfram
You can try these cocoa example. It returns always 64.0 here.
Code: Select all
Procedure stringWidth(string.s)
NSSize.NSSize
FontSize.CGFloat = 12.0
NSFont = CocoaMessage(0,0, "NSFont fontWithName:$", @"Andale Mono", "size:@", @FontSize)
NSDictionary = CocoaMessage(0, 0, "NSDictionary dictionaryWithObject:", NSFont, "forKey:$", @"NSFontAttributeName")
NSString = CocoaMessage(0, 0, "NSString stringWithString:$", @string)
CocoaMessage(NSSize, NSString, "sizeWithAttributes:", NSDictionary)
ProcedureReturn NSSize\width
EndProcedure
Debug stringWidth("Hello World")
Debug stringWidth("H") +stringWidth("e") +stringWidth("l") +stringWidth("l") +stringWidth("o") +stringWidth(" ") +
stringWidth("W") +stringWidth("o") +stringWidth("r") +stringWidth("l") +stringWidth("d")