Thanks for your help.
I tried dynamically calculating the font size with the following code, but didn't get any noticeably better results. It sets a starting font size of 0.05 times the viewport height, then measures the width of a string containing the letter M repeated 40 times and adjusts the font size until the difference is less than 0.1, or the loop was repeated 10 times already (sometimes it would jump back and forth between two font sizes).
let viewport: CGSize
var fontSize = viewport.height * 0.05
for _ in 0..<10 {
let stringSize = String(repeating: "M", count: 40).size(withAttributes: [.font: NSFont.systemFont(ofSize: fontSize)])
let newFontSize = fontSize * viewport.width / stringSize.width
if abs(newFontSize - fontSize) < 0.1 {
break
}
fontSize = newFontSize
}
I could resize the text with pixel perfection using an image, but then I would need to render it at a very high resolution to make it sharp enough for all possible viewport sizes. I'm not sure that's worth the effort in my case.
It just occurred to me that WebKit is open source: https://github.com/WebKit/WebKit/tree/main Is the font scaling (e.g. font-size: 20vh) done at the WebKit level (can anybody point me to the source file?), or is it delegated to the operating system?
By the way, I created FB20195868.