Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
return function(context, caption) {
if (!caption) {
return;
}
var lines = [[]],
maxWidth = 0,
words = smartquotes(caption + "").trim().replace(/\s\s+/g, " \n").split(/ /g);
context.font = theme.captionFont;
context.textBaseline = "top";
context.textAlign = theme.captionAlign || "center";
// Check whether each word exceeds the width limit
// Wrap onto next line as needed
words.forEach(function(word,i){
var width = context.measureText(lines[lines.length - 1].concat([word]).join(" ")).width;
if (word[0] === "\n" || (lines[lines.length - 1].length && width > captionWidth)) {
word = word.trim();
lines.push([word]);
width = context.measureText(word).width;