-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathjquery.fontface.js
112 lines (101 loc) · 3.44 KB
/
jquery.fontface.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/*
* FontFace - jQuery Plugin
* Helps you face up to the battlefield of @font-face challenges
*
* Copyright (c) 2010 Craig Sharkie
*
* Version: 1.3 (11/11/2010)
*
* Dual licensed under the MIT and GPL licenses:
* http://www.opensource.org/licenses/mit-license.php
* http://www.gnu.org/licenses/gpl.html
*/
// http://www.sitepoint.com/the-fontface-jquery-plugin/
;(function($){
$.fn.fontface = function(options) {
var stacks = {
serif : ", Times New Roman , serif",
sansserif : ", Helvetica, Arial, sans-serif"
},
defaults = {
filePath: "fonts/",//change this to your font directory location
fontFamily: "sans-serif",
fontStack: false,
fontStretch: "normal",
fontStyle: "normal",
fontVariant: "normal",
fontWeight: "normal"
},
options = $.extend(defaults, options);
options.fontFile = options.filePath + options.fileName;
if (options.fontStack || options.fontFamily === "sans-serif") {
if (options.fontStack && options.fontStack.indexOf(", ") === -1) {
options.fontFamily = options.fontName + stacks[options.fontStack];
}
else if (options.fontStack && options.fontStack.indexOf(", ") !== -1) {
var concat = (options.fontStack.substring(0,2) !== ", ") ? "" : ", ";
options.fontFamily = options.fontName + concat + options.fontStack;
}
else {
options.fontFamily = options.fontName + stacks.sansserif
}
}
if (typeof options.fontFamily === "object") {
options.fontFamily = options.fontFamily.join(", ");
}
if ($("#jQueryFontFace").length === 0) {//haven't already made one
$("head").prepend($("<style type=\"text/css\" id=\"jQueryFontFace\"/>"));
}
var FF = {
selector: function (obj) {
var tag = obj.tagName,
className = (obj.className) ? "." + obj.className.split(" ").join(".") : "",
id = ($(obj).attr("id")) ? "#" + $(obj).attr("id") : "";
return tag + id + className;
},
create: function (obj) {
var fontFace = "",
rule = "",
fontfamily = options.fontFamily.replace(/\s/g,"").replace(/,/g,""),
fontfamilyStyleWeight = fontfamily + options.fontStyle + options.fontWeight,
selector = FF.selector(obj);
if (!$("#jQueryFontFace").data(fontfamilyStyleWeight)) {
fontFace = [
"@font-face {",
"\tfont-family: \"" + options.fontName + "\";",
"\tsrc: url('" + options.fontFile + ".eot');",
"\tsrc: local('☺'), url('" + options.fontFile + ".ttf') format('truetype');",
"\tfont-stretch: " + options.fontStretch + ";",
"\tfont-style: " + options.fontStyle + ";",
"\tfont-variant: " + options.fontVariant + ";",
"\tfont-weight: " + options.fontWeight + ";",
"}"
].join("\n");
$("#jQueryFontFace").data(fontfamilyStyleWeight, true);
}
if (!$("#jQueryFontFace").data(selector)) {
rule = [
selector + " {",
"\tfont-family: " + FF.quote(options.fontFamily) + " !important;",
"}"
].join("\n");
$("#jQueryFontFace").data(selector, selector);
}
return (fontFace.length || rule.length) ? fontFace + "\n" + rule + "\n" : "";
},
quote: function (string) {
var split = string.split(", "),
length = split.length;
for (var i = 0; i < length; i += 1) {
if (split[i].indexOf(" ") !== -1) {
split[i] = '"' + split[i] + '"';
}
}
return split.join(", ");
}
};
return this.each(function() {
$("#jQueryFontFace").text($("#jQueryFontFace").text() + FF.create(this));
});
};
})(jQuery);