Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

specify fontFamily to filter fonts #282

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion src/dom-to-image.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,8 @@
}

function copyOptions(options) {
// get user specifiy fontFamily
domtoimage.impl.options.fontFamily = options.fontFamily || []

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should alias this to a local var to reduce double-dotting

// Copy options to impl options for use in impl
if(typeof(options.imagePlaceholder) === 'undefined') {
domtoimage.impl.options.imagePlaceholder = defaultOptions.imagePlaceholder;
Expand Down Expand Up @@ -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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nitpick code style of this repository requires trailing ;

}
}
}
if (flag) {
cssRules.push(rule)
}
});
} catch (e) {
console.log('Error while reading CSS rules from ' + sheet.href, e.toString());
}
Expand Down