From 6189182e51aa03015051f1abe4ba34f42572ca34 Mon Sep 17 00:00:00 2001 From: Nick Van den Bleeken Date: Sat, 24 Apr 2021 19:03:24 +0200 Subject: [PATCH] Add support for using JsBarcode in implementations that don't implement measureText. --- src/renderers/shared.js | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/renderers/shared.js b/src/renderers/shared.js index b7fe4002..243126e9 100644 --- a/src/renderers/shared.js +++ b/src/renderers/shared.js @@ -80,8 +80,14 @@ function messureText(string, options, context){ ctx.font = options.fontOptions + " " + options.fontSize + "px " + options.font; // Calculate the width of the encoding - var size = ctx.measureText(string).width; - + var measureTextResult = ctx.measureText(string); + if (!measureTextResult) { + // Some implementations don't implement measureText and return undefined. + // If the text cannot be measured we will return 0. + // This will make some barcode with big text render incorrectly + return 0; + } + var size = measureTextResult.width; return size; }