/
options.js
180 lines (138 loc) · 5.85 KB
/
options.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
// options.js
function initOptions () {
jQuery('#replacement option').each( function (i) {
var fontName = jQuery(this).val();
if (!jQuery(this).hasClass('embeddable')
&& !jQuery(this).hasClass('custom')
&& !isFontAvailable( fontName )) {
jQuery(this).attr('disabled', 'disabled');
jQuery('#warnings')
.html('Some font options may be unavailable in your system.')
.show();
// jQuery(this).remove();
}
} );
var options = JSON.parse( localStorage.allvetica );
jQuery('#replacement option').filter('[value="' + options.replacement + '"]').attr('selected', true);
jQuery('#replace_arial')
.attr('checked', options.replaceArial);
jQuery('#replace_comic_sans')
.attr('checked', options.replaceComicSans);
jQuery('#replace_marker_felt')
.attr('checked', options.replaceMarkerFelt);
jQuery('#replace_papyrus')
.attr('checked', options.replacePapyrus);
jQuery('#optimize_legibility')
.attr('checked', options.optimizeLegibility);
jQuery('#replacement_custom').val(options.replacementCustom);
if (jQuery('#replacement option:selected').hasClass('custom')) {
jQuery('#preview').show();
jQuery('#replacement-advanced').show();
checkFont(true);
} else {
jQuery('#preview').hide();
jQuery('#replacement-advanced').hide();
}
}
function saveOptions () {
var options = {};
// custom replacement font
var customReplacementFont = jQuery('#replacement_custom').val();
var customFontInstalled = isFontAvailable( customReplacementFont );
if (jQuery('#replacement option:selected').hasClass('custom')) {
jQuery('#replacement-advanced').slideDown('fast');
checkFont(true);
} else {
jQuery('#replacement-advanced').slideUp('fast');
jQuery('#preview').slideUp('fast');
jQuery('#warnings').slideUp('fast');
}
if (customReplacementFont && customFontInstalled) {
options.replacementCustom = customReplacementFont;
options.replacementCustomValid = true;
} else if (customReplacementFont) {
// Invalid
var previous = JSON.parse( localStorage.allvetica );
options.replacementCustom = previous.replacementCustom;
options.replacementCustomValid = true;
} else {
options.replacementCustom = '';
options.replacementCustomValid = false;
}
options.replaceArial
= jQuery('#replace_arial:checked').val() != null;
options.replaceComicSans
= jQuery('#replace_comic_sans:checked').val() != null;
options.replaceMarkerFelt
= jQuery('#replace_marker_felt:checked').val() != null;
options.replacePapyrus
= jQuery('#replace_papyrus:checked').val() != null;
options.replacement
= jQuery('#replacement option:selected').val();
options.optimizeLegibility
= jQuery('#optimize_legibility:checked').val() != null;
localStorage.allvetica = JSON.stringify( options );
}
function checkFont (saving) {
var fontName = jQuery('#replacement_custom').val();
var customFontInstalled = (fontName) ? isFontAvailable( fontName ) : false;
if (customFontInstalled) {
jQuery('#replacement_custom').removeClass('notfound');
if (saving) {
jQuery('#warnings').slideUp('fast');
jQuery('#custom-font-sample').css('font-family', fontName);
jQuery('#preview').slideDown('fast');
}
} else {
jQuery('#replacement_custom').addClass('notfound');
if (saving) {
var warningMsg = (fontName)
? 'The custom font you specified is not available.'
: 'You must specify a custom font.';
jQuery('#warnings').html(warningMsg).slideDown('fast');
jQuery('#preview').slideUp('fast');
}
}
}
function isFontAvailable (font) {
var testString = '~iomwIOMW';
var containerId = 'is-font-available-container';
var fontArray = font instanceof Array;
if (!fontArray) {
font = [ font ];
}
var fontAvailability = [];
var containerSel = '#' + containerId;
var spanSel = containerSel + ' span';
var familySansSerif = 'sans-serif';
var familyMonospace = 'monospace, monospace';
// Why monospace twice? It's a bug in the Mozilla and Webkit rendering engines:
// http://www.undermyhat.org/blog/2009/09/css-font-family-monospace-renders-inconsistently-in-firefox-and-chrome/
// DOM:
$('body').append('<div id="' + containerId + '"></div>');
$(containerSel).append('<span></span>');
$(spanSel).append(document.createTextNode(testString));
// CSS:
$(containerSel).css('visibility', 'hidden');
$(containerSel).css('position', 'absolute');
$(containerSel).css('left', '-9999px');
$(containerSel).css('top', '0');
$(containerSel).css('font-weight', 'bold');
$(containerSel).css('font-size', '200px !important');
jQuery.each( font, function (i, v) {
$(spanSel).css('font-family', v + ',' + familyMonospace );
var monospaceFallbackWidth = $(spanSel).width();
var monospaceFallbackHeight = $(spanSel).height();
$(spanSel).css('font-family', v + ',' + familySansSerif );
var sansSerifFallbackWidth = $(spanSel).width();
var sansSerifFallbackHeight = $(spanSel).height();
fontAvailability[i] = true
&& monospaceFallbackWidth == sansSerifFallbackWidth
&& monospaceFallbackHeight == sansSerifFallbackHeight;
} );
$(containerSel).remove();
if (!fontArray && fontAvailability.length == 1) {
fontAvailability = fontAvailability[0];
}
return fontAvailability;
}