Created
September 7, 2018 18:03
-
-
Save toddprouty/ed5bb34e1389a80708291a0b38715250 to your computer and use it in GitHub Desktop.
Modified version of MyFonts.com font loader that allows style linking (use bold and italic without specifying a font-family every time)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* ────────────────────────────────────────────────────────────────────────────┐ | |
* Modified MyFonts JavaScript font loader | |
* | |
* The JavaScript font loader that is included with MyFonts font kits forces the | |
* use of unique font-family names, and requires you to set font-weight and | |
* font-style to 'normal' globally. Anywhere you are using bold or italic, a | |
* bold or italic font-family will need to be referenced in your CSS. This can | |
* cause a few issues, including having more styles to override, and worse, if | |
* the font files don't load, any and all bold/italic text will just look like | |
* normal text. | |
* | |
* This modified version allows for style linking. With style linking, the same | |
* font-family is used across multiple @font-face at-rules. Each @font-face | |
* references a unique font file and includes a unique combination of | |
* font-weight and font-style properties. Once that is set up, it allows you to | |
* set the font-family once for all body text, for example, and any instances of | |
* bold or italic text will be shown using the corresponding font file. There | |
* is no need to set a font-family anywhere the bold or italic font should | |
* appear; 'font-weight: bold' or 'font-style: italic' will do. Style linking is | |
* explained well here: http://j.mp/2wRPnpn. | |
* | |
* The modifications start at line 194 ('var fonts'). The original script is | |
* included as a first revision for comparison purposes. | |
* | |
* NOTE: This is based on formerly minified code. 'Prettifying' isn't always | |
* pretty. Some cleanup was done for readability. | |
* ────────────────────────────────────────────────────────────────────────────┘ | |
*/ | |
var protocol = document.location.protocol; | |
"https:" != protocol && (protocol = "http:"); | |
var count = document.createElement("script"); | |
count.type = "text/javascript"; | |
count.async = !0; | |
count.src = protocol + "//hello.myfonts.net/count/YOUR_FONT_KIT_ID"; | |
var s = document.getElementsByTagName("script")[0]; | |
s.parentNode.insertBefore(count, s); | |
var svgEnabled = 0 | |
var woff2Enabled = 1; | |
var browserName, | |
browserVersion, | |
webfontType; | |
if ("undefined" == typeof woffEnabled) { | |
var woffEnabled = !0; | |
} | |
if ("undefined" != typeof customPath) { | |
var path = customPath; | |
} else { | |
var scripts = document.getElementsByTagName("SCRIPT") | |
, script = scripts[scripts.length - 1].src; | |
script.match("://") || "/" == script.charAt(0) || (script = "./" + script); | |
path = script.replace(/\\/g, "/").replace(/\/[^\/]*\/?$/, "") | |
} | |
var wfpath = path + "/fonts/"; | |
var browsers = [{ | |
regex: "MSIE (\\d+\\.\\d+)", | |
versionRegex: "new Number(RegExp.$1)", | |
type: [{ | |
version: 9, | |
type: "woff" | |
}, { | |
version: 5, | |
type: "eot" | |
}] | |
}, { | |
regex: "Trident/(\\d+\\.\\d+); (.+)?rv:(\\d+\\.\\d+)", | |
versionRegex: "new Number(RegExp.$3)", | |
type: [{ | |
version: 11, | |
type: "woff" | |
}] | |
}, { | |
regex: "Firefox[/s](\\d+\\.\\d+)", | |
versionRegex: "new Number(RegExp.$1)", | |
type: [{ | |
version: 3.6, | |
type: "woff" | |
}, { | |
version: 3.5, | |
type: "ttf" | |
}] | |
}, { | |
regex: "Edge/(\\d+\\.\\d+)", | |
versionRegex: "new Number(RegExp.$1)", | |
type: [{ | |
version: 12, | |
type: "woff" | |
}] | |
}, { | |
regex: "Chrome/(\\d+\\.\\d+)", | |
versionRegex: "new Number(RegExp.$1)", | |
type: [{ | |
version: 36, | |
type: "woff2" | |
}, { | |
version: 6, | |
type: "woff" | |
}, { | |
version: 4, | |
type: "ttf" | |
}] | |
}, { | |
regex: "Mozilla.*Android (\\d+\\.\\d+).*AppleWebKit.*Safari", | |
versionRegex: "new Number(RegExp.$1)", | |
type: [{ | |
version: 4.1, | |
type: "woff" | |
}, { | |
version: 3.1, | |
type: "svg#wf" | |
}, { | |
version: 2.2, | |
type: "ttf" | |
}] | |
}, { | |
regex: "Mozilla.*(iPhone|iPad).* OS (\\d+)_(\\d+).* AppleWebKit.*Safari", | |
versionRegex: "new Number(RegExp.$2) + (new Number(RegExp.$3) / 10)", | |
unhinted: !0, | |
type: [{ | |
version: 5, | |
type: "woff" | |
}, { | |
version: 4.2, | |
type: "ttf" | |
}, { | |
version: 1, | |
type: "svg#wf" | |
}] | |
}, { | |
regex: "Mozilla.*(iPhone|iPad|BlackBerry).*AppleWebKit.*Safari", | |
versionRegex: "1.0", | |
type: [{ | |
version: 1, | |
type: "svg#wf" | |
}] | |
}, { | |
regex: "Version/(\\d+\\.\\d+)(\\.\\d+)? Safari/(\\d+\\.\\d+)", | |
versionRegex: "new Number(RegExp.$1)", | |
type: [{ | |
version: 5.1, | |
type: "woff" | |
}, { | |
version: 3.1, | |
type: "ttf" | |
}] | |
}, { | |
regex: "Opera/(\\d+\\.\\d+)(.+)Version/(\\d+\\.\\d+)(\\.\\d+)?", | |
versionRegex: "new Number(RegExp.$3)", | |
type: [{ | |
version: 24, | |
type: "woff2" | |
}, { | |
version: 11.1, | |
type: "woff" | |
}, { | |
version: 10.1, | |
type: "ttf" | |
}] | |
}]; | |
var browLen = browsers.length; | |
var suffix = ""; | |
var i = 0; | |
a: for (; i < browLen; i++) { | |
var regex = new RegExp(browsers[i].regex); | |
if (regex.test(navigator.userAgent)) { | |
browserVersion = eval(browsers[i].versionRegex); | |
var typeLen = browsers[i].type.length; | |
for (j = 0; j < typeLen; j++) { | |
if (browserVersion >= browsers[i].type[j].version && (1 == browsers[i].unhinted && (suffix = "_unhinted"), | |
webfontType = browsers[i].type[j].type, | |
"woff" != webfontType || woffEnabled) && ("woff2" != webfontType || woff2Enabled) && ("svg#wf" != webfontType || svgEnabled)) { | |
break a; | |
} | |
} | |
} else { | |
webfontType = "woff" | |
} | |
} | |
/(Macintosh|Android)/.test(navigator.userAgent) && "svg#wf" != webfontType && (suffix = "_unhinted"); | |
var head = document.getElementsByTagName("head")[0], | |
stylesheet = document.createElement("style"); | |
stylesheet.setAttribute("type", "text/css"); | |
head.appendChild(stylesheet); | |
for (var fonts = [{ | |
// Body font, bold -- SomeFont - Heavy | |
fontFamily: "SomeFont", // Same font-family used for all 3 body fonts... | |
url: wfpath + "YOUR_FONT_KIT_ID_0" + suffix + "_0." + webfontType, | |
fontWeight: "bold", // ...but source file, font-weight and | |
fontStyle: "normal" // font-style vary. This sets up style linking. | |
}, { | |
// Body font, italic -- SomeFont - Oblique | |
fontFamily: "SomeFont", // See above | |
url: wfpath + "YOUR_FONT_KIT_ID_1" + suffix + "_0." + webfontType, | |
fontWeight: "normal", | |
fontStyle: "italic" | |
}, { | |
// Body font, normal -- SomeFont - Roman | |
fontFamily: "SomeFont", // See above | |
url: wfpath + "YOUR_FONT_KIT_ID_2" + suffix + "_0." + webfontType, | |
fontWeight: "normal", | |
fontStyle: "normal" | |
}, { | |
// Heading font -- SomeFont - Light | |
fontFamily: "SomeFont-Light", // The heading font has its own font-family, | |
// though it could be given another weight | |
// instead (e.g. fontWeight: "300"), in which | |
// case the font-family could be the same as | |
// the one used for the body font. | |
url: wfpath + "YOUR_FONT_KIT_ID_3" + suffix + "_0." + webfontType, | |
fontWeight: "normal", | |
fontStyle: "normal" | |
}], len = fonts.length, css = "", i = 0; i < len; i++) { | |
var format = "svg#wf" == webfontType ? 'format("svg")' : "ttf" == webfontType ? 'format("truetype")' : "eot" == webfontType ? "" : 'format("' + webfontType + '")', | |
css = css + ("@font-face{font-family: " + fonts[i].fontFamily + ";src:url(" + fonts[i].url + ")" + format + ";"); | |
// fonts[i].fontWeight is defined, unlike in the original | |
fonts[i].fontWeight && (css += "font-weight: " + fonts[i].fontWeight + ";"); | |
// fonts[i].fontStyle is defined, unlike in the original | |
fonts[i].fontStyle && (css += "font-style: " + fonts[i].fontStyle + ";"); | |
css += "}" | |
} | |
stylesheet.styleSheet ? stylesheet.styleSheet.cssText = css : stylesheet.innerHTML = css; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment