Emscripten: fix edge case of js_canvas_find_font_midpoint.

If the puzzle canvas is at a ludicrously small size, so that you
attempt to use a zero-height font, then obviously nothing sensible
will appear in the way of text, but you'd at least like to avoid a
crash. But currently, js_canvas_find_font_midpoint will make a canvas,
print some height-0 text into it, and try to retrieve the image pixels
to see what the actual font height was - and this will involve asking
getImageData for a zero-sized rectangle of pixels, which is an error.

Of course, there's only one possible return value from this function
if the font height is 0, so we can just return it without going via
getImageData at all.

(This crash can be provoked by trying to resize the puzzle canvas to
Far Too Small, or by interleaving canvas resizes with browser-tab
zooming. I've had one report that it also occurs in less silly
situations, which I haven't been able to reproduce. However, this
seems like a general improvement anyway.)
This commit is contained in:
Simon Tatham
2023-05-26 21:29:29 +01:00
parent 8237b02be4
commit b6c842a28c

View File

@ -458,6 +458,12 @@ mergeInto(LibraryManager.library, {
* per (font,height) pair.
*/
js_canvas_find_font_midpoint: function(height, monospaced) {
if (height == 0) {
// Handle this degenerate case by hand. Otherwise we end
// up passing height=0 to the getImageData call below,
// causing browsers to report errors.
return 0;
}
// Resolve the font into a string.
var ctx1 = onscreen_canvas.getContext('2d', { alpha: false });