aboutsummaryrefslogtreecommitdiff
path: root/emcclib.js
diff options
context:
space:
mode:
authorSimon Tatham <anakin@pobox.com>2023-05-26 21:29:29 +0100
committerSimon Tatham <anakin@pobox.com>2023-05-26 21:29:29 +0100
commitb6c842a28cf6597df063fcff35079c3e3982381e (patch)
tree675cae68755a7b04b179947d5fd86549a9a9b26a /emcclib.js
parent8237b02be4e6f92a9e1aebbed23d7b5de0e3543c (diff)
downloadpuzzles-b6c842a28cf6597df063fcff35079c3e3982381e.zip
puzzles-b6c842a28cf6597df063fcff35079c3e3982381e.tar.gz
puzzles-b6c842a28cf6597df063fcff35079c3e3982381e.tar.bz2
puzzles-b6c842a28cf6597df063fcff35079c3e3982381e.tar.xz
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.)
Diffstat (limited to 'emcclib.js')
-rw-r--r--emcclib.js6
1 files changed, 6 insertions, 0 deletions
diff --git a/emcclib.js b/emcclib.js
index 8dd1554..4a09c60 100644
--- a/emcclib.js
+++ b/emcclib.js
@@ -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 });