From ae31f18a9dc1eb5da3582f232e057498ca90322e Mon Sep 17 00:00:00 2001 From: NotsayJustdo <709444814@qq.com> Date: Wed, 3 Apr 2019 16:08:15 +0800 Subject: [PATCH] specify fontFamily to filter fonts if many fonts in stylesheets, dom-to-image will download all fonts, it will cause some problems: 1. waiting long time to load all fonts 2. use large computer memories, maybe make the browser breakdown. I suggest to add fontFamily option for user, user can specify they use fontFamily, it will avoid that problems! --- src/dom-to-image.js | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/src/dom-to-image.js b/src/dom-to-image.js index 27201ac9..ed42631f 100644 --- a/src/dom-to-image.js +++ b/src/dom-to-image.js @@ -135,6 +135,8 @@ } function copyOptions(options) { + // get user specifiy fontFamily + domtoimage.impl.options.fontFamily = options.fontFamily || [] // Copy options to impl options for use in impl if(typeof(options.imagePlaceholder) === 'undefined') { domtoimage.impl.options.imagePlaceholder = defaultOptions.imagePlaceholder; @@ -677,9 +679,24 @@ function getCssRules(styleSheets) { var cssRules = []; + let flag = false; styleSheets.forEach(function (sheet) { try { - util.asArray(sheet.cssRules || []).forEach(cssRules.push.bind(cssRules)); + util.asArray(sheet.cssRules || []).forEach(rule => { + flag = false + if (rule.cssText.indexOf('@font-face') !== -1) { + // filter fonts by specific fontFamily to void download all fonts + for (let i = 0; domtoimage.impl.options.fontFamily[i]; i++) { + if (rule.cssText.indexOf(domtoimage.impl.options.fontFamily[i]) !== -1) { + flag = true + break + } + } + } + if (flag) { + cssRules.push(rule) + } + }); } catch (e) { console.log('Error while reading CSS rules from ' + sheet.href, e.toString()); }