ref: b6c842a28cf6597df063fcff35079c3e3982381e
parent: 8237b02be4e6f92a9e1aebbed23d7b5de0e3543c
author: Simon Tatham <anakin@pobox.com>
date: Fri May 26 17:29:29 EDT 2023
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.)
--- a/emcclib.js
+++ b/emcclib.js
@@ -458,6 +458,12 @@
* 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 });