Skip to content

Commit

Permalink
Add support for using JsBarcode in implementations that don't impleme…
Browse files Browse the repository at this point in the history
…nt measureText.
  • Loading branch information
nvdbleek committed Apr 24, 2021
1 parent 19cdeea commit 262fe59
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion bin/renderers/shared.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,20 @@ function messureText(string, options, context) {
} else if (typeof document !== "undefined") {
ctx = document.createElement("canvas").getContext("2d");
} else {
// If the text cannot be messured we will return 0.
// If the text cannot be measured we will return 0.
// This will make some barcode with big text render incorrectly
return 0;
}
ctx.font = options.fontOptions + " " + options.fontSize + "px " + options.font;

// Calculate the width of the encoding
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 = ctx.measureText(string).width;

return size;
Expand Down

0 comments on commit 262fe59

Please sign in to comment.