TextWidth - strange on Mac OS :-)

Mac OSX specific forum
walbus
Addict
Addict
Posts: 929
Joined: Sat Mar 02, 2013 9:17 am

TextWidth - strange on Mac OS :-)

Post by walbus »

Try this, it took me a while to realize what was going on :twisted: 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()
User avatar
J. Baker
Addict
Addict
Posts: 2178
Joined: Sun Apr 27, 2003 8:12 am
Location: USA
Contact:

Re: TextWidth - strange on Mac OS :-)

Post by J. Baker »

I'm missing something? Debug...
64
64
www.posemotion.com

PureBasic Tools for OS X: PureMonitor, plist Tool, Data Maker & App Chef

Mac: 10.13.6 / 1.4GHz Core 2 Duo / 2GB DDR3 / Nvidia 320M
PC: Win 7 / AMD 64 4000+ / 3GB DDR / Nvidia 720GT


Even the vine knows it surroundings but the man with eyes does not.
walbus
Addict
Addict
Posts: 929
Joined: Sat Mar 02, 2013 9:17 am

Re: TextWidth - strange on Mac OS :-)

Post 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 ?
davido
Addict
Addict
Posts: 1890
Joined: Fri Nov 09, 2012 11:04 pm
Location: Uttoxeter, UK

Re: TextWidth - strange on Mac OS :-)

Post 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
DE AA EB
walbus
Addict
Addict
Posts: 929
Joined: Sat Mar 02, 2013 9:17 am

Re: TextWidth - strange on Mac OS :-)

Post 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
#NULL
Addict
Addict
Posts: 1440
Joined: Thu Aug 30, 2007 11:54 pm
Location: right here

Re: TextWidth - strange on Mac OS :-)

Post 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
Wolfram
Enthusiast
Enthusiast
Posts: 567
Joined: Thu May 30, 2013 4:39 pm

Re: TextWidth - strange on Mac OS :-)

Post 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()
macOS Catalina 10.15.7
walbus
Addict
Addict
Posts: 929
Joined: Sat Mar 02, 2013 9:17 am

Re: TextWidth - strange on Mac OS :-)

Post 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) :
Image
User avatar
J. Baker
Addict
Addict
Posts: 2178
Joined: Sun Apr 27, 2003 8:12 am
Location: USA
Contact:

Re: TextWidth - strange on Mac OS :-)

Post by J. Baker »

What's your default font? Maybe it's not mono spaced?
www.posemotion.com

PureBasic Tools for OS X: PureMonitor, plist Tool, Data Maker & App Chef

Mac: 10.13.6 / 1.4GHz Core 2 Duo / 2GB DDR3 / Nvidia 320M
PC: Win 7 / AMD 64 4000+ / 3GB DDR / Nvidia 720GT


Even the vine knows it surroundings but the man with eyes does not.
Wolfram
Enthusiast
Enthusiast
Posts: 567
Joined: Thu May 30, 2013 4:39 pm

Re: TextWidth - strange on Mac OS :-)

Post 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
macOS Catalina 10.15.7
User avatar
Josh
Addict
Addict
Posts: 1183
Joined: Sat Feb 13, 2010 3:45 pm

Re: TextWidth - strange on Mac OS :-)

Post 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
sorry for my bad english
Wolfram
Enthusiast
Enthusiast
Posts: 567
Joined: Thu May 30, 2013 4:39 pm

Re: TextWidth - strange on Mac OS :-)

Post 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.
macOS Catalina 10.15.7
davido
Addict
Addict
Posts: 1890
Joined: Fri Nov 09, 2012 11:04 pm
Location: Uttoxeter, UK

Re: TextWidth - strange on Mac OS :-)

Post 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
DE AA EB
Dude
Addict
Addict
Posts: 1907
Joined: Mon Feb 16, 2015 2:49 pm

Re: TextWidth - strange on Mac OS :-)

Post 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?
Wolfram
Enthusiast
Enthusiast
Posts: 567
Joined: Thu May 30, 2013 4:39 pm

Re: TextWidth - strange on Mac OS :-)

Post 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")
macOS Catalina 10.15.7
Post Reply