To support retina graphics, the general technique is to get the backingScaleFactor (which will typically be 2.0 on retina screens) then render at 2x width and height, then scale down when drawing.
The problem is that if you have a font which is set to “System” or “SmallSystem”, the font size is zero in the IDE and presumably determined at runtime, so you can’t simply double the font size when rendering text.
What’s the proper way to handle this?
It appears that this is OS dependent, i.e. it’s not Xojo that chooses the default font size, but it’s the OS.
On the Mac, you’d use NSFont’s systemFontSize and systemFontOfSize: methods
On Win and Linux … well, maybe someone else knows?
I did some testing, and in fact, it seems quite easy - in graphics.paint you can check graphics.textSize and it seems to return a sensible value. Multiply by the backingScaleFactor and draw at that size.
BTW, if you don’t use your own graphics buffer but draw directly into the Graphics object that you get in the Paint event, then you do not even need to care about doubling the font size - it’s automagically Retina-savvy!
I found that out when I updated CustomEditField to support Retina: The code did use interim Pictures to prepare some parts of the graphics, and then drew those into the final “g”. On Windows, this is necessary to avoid flicker. But on OSX, this isn’t necessary, and when I remove these interim buffers, I had instant Retina support.
Indeed, you are correct. I’ll probably keep my code as-is (using a 2nd offscreen picture) so that it’s flicker-free on Windows, but that’s good to know for mac-only apps.
Avoid an offscreen buffer on the Mac whenever possible. You’ll get better performance and batter text rendering when you draw directly to the graphics object in Paint.
In a Canvas.Open event, I usually do something like
Me.DoubleBuffer = TargetWin32
Me.EraseBackground = Not TargetWin32
Now your canvas is ready to draw flicker free using the Paint event.