OLD | NEW |
| (Empty) |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 // Mapping between font list ids and the generic family setting they | |
6 // represent. | |
7 var genericFamilies = [ | |
8 { fontList: 'standardFontList', name: 'standard' }, | |
9 { fontList: 'serifFontList', name: 'serif' }, | |
10 { fontList: 'sansSerifFontList', name: 'sansserif' }, | |
11 { fontList: 'fixedFontList', name: 'fixed' } | |
12 ]; | |
13 | |
14 // Ids of elements to contain the "Lorem ipsum" sample text. | |
15 var sampleTextDivIds = [ | |
16 'standardFontSample', | |
17 'serifFontSample', | |
18 'sansSerifFontSample', | |
19 'fixedFontSample' | |
20 ]; | |
21 | |
22 var defaultSampleText = 'Lorem ipsum dolor sit amat.'; | |
23 var scriptSpecificSampleText = { | |
24 // "Cyrllic script". | |
25 'Cyrl': 'Кириллица', | |
26 'Hang': '정 참판 양반댁 규수 큰 교자 타고 혼례 치른 날.', | |
27 'Hans': '床前明月光,疑是地上霜。举头望明月,低头思故乡。', | |
28 'Hant': '床前明月光,疑是地上霜。舉頭望明月,低頭思故鄉。', | |
29 'Jpan': '吾輩は猫である。名前はまだ無い。', | |
30 // "Khmer language". | |
31 'Khmr': '\u1797\u17B6\u179F\u17B6\u1781\u17D2\u1798\u17C2\u179A', | |
32 }; | |
33 | |
34 function getSelectedScript() { | |
35 var scriptList = document.getElementById('scriptList'); | |
36 return scriptList.options[scriptList.selectedIndex].value; | |
37 } | |
38 | |
39 function getSelectedFont(fontList) { | |
40 return fontList.options[fontList.selectedIndex].value; | |
41 } | |
42 | |
43 // Populates the font lists with the list of system fonts from |fonts|. | |
44 function populateLists(fonts) { | |
45 for (var i = 0; i < genericFamilies.length; i++) { | |
46 var list = document.getElementById(genericFamilies[i].fontList); | |
47 | |
48 // Add special "(none)" item to indicate fallback to the non-per-script | |
49 // font setting. The Font Settings API uses the empty string to indicate | |
50 // fallback. | |
51 var noneItem = document.createElement('option'); | |
52 noneItem.value = ''; | |
53 noneItem.text = '(none)'; | |
54 list.add(noneItem); | |
55 | |
56 for (var j = 0; j < fonts.length; j++) { | |
57 var item = document.createElement('option'); | |
58 item.value = fonts[j].fontId; | |
59 item.text = fonts[j].displayName; | |
60 list.add(item); | |
61 } | |
62 } | |
63 | |
64 updateFontListsForScript(); | |
65 } | |
66 | |
67 // Returns a function that updates the font setting for |genericFamily| | |
68 // to match the selected value in |fontList|. It can be used as an event | |
69 // handler for selection changes in |fontList|. | |
70 function getFontChangeHandler(fontList, genericFamily) { | |
71 return function() { | |
72 var script = getSelectedScript(); | |
73 var font = getSelectedFont(fontList); | |
74 | |
75 var details = {}; | |
76 details.genericFamily = genericFamily; | |
77 details.fontId = font; | |
78 details.script = script; | |
79 | |
80 chrome.fontSettings.setFont(details); | |
81 }; | |
82 } | |
83 | |
84 // Sets the selected value of |fontList| to |fontId|. | |
85 function setSelectedFont(fontList, fontId) { | |
86 var script = getSelectedScript(); | |
87 var i; | |
88 for (i = 0; i < fontList.length; i++) { | |
89 if (fontId == fontList.options[i].value) { | |
90 fontList.selectedIndex = i; | |
91 break; | |
92 } | |
93 } | |
94 if (i == fontList.length) { | |
95 console.warn("font '" + fontId + "' for " + fontList.id + ' for ' + | |
96 script + ' is not on the system'); | |
97 } | |
98 } | |
99 | |
100 // Returns a callback function that sets the selected value of |list| to the | |
101 // font returned from |chrome.fontSettings.getFont|. | |
102 function getFontHandler(list) { | |
103 return function(details) { | |
104 setSelectedFont(list, details.fontId); | |
105 list.disabled = !isControllableLevel(details.levelOfControl); | |
106 }; | |
107 } | |
108 | |
109 // Called when the script list selection changes. Sets the selected value of | |
110 // each font list to the current font setting, and updates the document's lang | |
111 // so that the samples are shown in the current font setting. | |
112 function updateFontListsForScript() { | |
113 var script = getSelectedScript(); | |
114 | |
115 for (var i = 0; i < genericFamilies.length; i++) { | |
116 var list = document.getElementById(genericFamilies[i].fontList); | |
117 var family = genericFamilies[i].name; | |
118 | |
119 var details = {}; | |
120 details.genericFamily = family; | |
121 details.script = script; | |
122 // For font selection it's the script code that matters, not language, so | |
123 // just use en for lang. | |
124 document.body.lang = 'en-' + script; | |
125 | |
126 chrome.fontSettings.getFont(details, getFontHandler(list)); | |
127 } | |
128 | |
129 if (typeof(scriptSpecificSampleText[script]) != 'undefined') | |
130 sample = scriptSpecificSampleText[script]; | |
131 else | |
132 sample = defaultSampleText; | |
133 for (var i = 0; i < sampleTextDivIds.length; i++) { | |
134 document.getElementById(sampleTextDivIds[i]).innerText = sample; | |
135 } | |
136 } | |
137 | |
138 // Returns a function to be called when the user changes the font size | |
139 // input element |elem|. The function calls the Font Settings Extension API | |
140 // function |setter| to commit the change. | |
141 function getFontSizeChangedFunc(elem, setter) { | |
142 return function() { | |
143 var pixelSize = parseInt(elem.value); | |
144 if (!isNaN(pixelSize)) { | |
145 setter({ pixelSize: pixelSize }); | |
146 } | |
147 } | |
148 } | |
149 | |
150 function isControllableLevel(levelOfControl) { | |
151 return levelOfControl == 'controllable_by_this_extension' || | |
152 levelOfControl == 'controlled_by_this_extension'; | |
153 } | |
154 | |
155 // Returns a function to be used as a listener for font size setting changed | |
156 // events from the Font Settings Extension API. The function updates the | |
157 // input element |elem| to reflect the change. | |
158 function getFontSizeChangedOnBrowserFunc(elem) { | |
159 return function(details) { | |
160 elem.value = details.pixelSize.toString(); | |
161 elem.disabled = !isControllableLevel(details.levelOfControl); | |
162 } | |
163 } | |
164 | |
165 // Maps the text input HTML element with |id| to the extension API accessor | |
166 // functions |getter| and |setter| for a setting and onChange event |apiEvent| | |
167 // for the setting. | |
168 function initFontSizePref(id, getter, setter, apiEvent) { | |
169 var elem = document.getElementById(id); | |
170 getter({}, function(details) { | |
171 elem.value = details.pixelSize.toString(); | |
172 elem.disabled = !isControllableLevel(details.levelOfControl); | |
173 }); | |
174 elem.addEventListener('change', getFontSizeChangedFunc(elem, setter)); | |
175 apiEvent.addListener(getFontSizeChangedOnBrowserFunc(elem)); | |
176 } | |
177 | |
178 function clearAllSettings() { | |
179 var scripts = | |
180 ["Afak", "Arab", "Armi", "Armn", "Avst", "Bali", "Bamu", "Bass", "Batk", | |
181 "Beng", "Blis", "Bopo", "Brah", "Brai", "Bugi", "Buhd", "Cakm", "Cans", | |
182 "Cari", "Cham", "Cher", "Cirt", "Copt", "Cprt", "Cyrl", "Cyrs", "Deva", | |
183 "Dsrt", "Dupl", "Egyd", "Egyh", "Egyp", "Elba", "Ethi", "Geor", "Geok", | |
184 "Glag", "Goth", "Gran", "Grek", "Gujr", "Guru", "Hang", "Hani", "Hano", | |
185 "Hans", "Hant", "Hebr", "Hluw", "Hmng", "Hung", "Inds", "Ital", "Java", | |
186 "Jpan", "Jurc", "Kali", "Khar", "Khmr", "Khoj", "Knda", "Kpel", "Kthi", | |
187 "Lana", "Laoo", "Latf", "Latg", "Latn", "Lepc", "Limb", "Lina", "Linb", | |
188 "Lisu", "Loma", "Lyci", "Lydi", "Mand", "Mani", "Maya", "Mend", "Merc", | |
189 "Mero", "Mlym", "Moon", "Mong", "Mroo", "Mtei", "Mymr", "Narb", "Nbat", | |
190 "Nkgb", "Nkoo", "Nshu", "Ogam", "Olck", "Orkh", "Orya", "Osma", "Palm", | |
191 "Perm", "Phag", "Phli", "Phlp", "Phlv", "Phnx", "Plrd", "Prti", "Rjng", | |
192 "Roro", "Runr", "Samr", "Sara", "Sarb", "Saur", "Sgnw", "Shaw", "Shrd", | |
193 "Sind", "Sinh", "Sora", "Sund", "Sylo", "Syrc", "Syre", "Syrj", "Syrn", | |
194 "Tagb", "Takr", "Tale", "Talu", "Taml", "Tang", "Tavt", "Telu", "Teng", | |
195 "Tfng", "Tglg", "Thaa", "Thai", "Tibt", "Tirh", "Ugar", "Vaii", "Visp", | |
196 "Wara", "Wole", "Xpeo", "Xsux", "Yiii", "Zmth", "Zsym", "Zyyy"]; | |
197 var families = | |
198 ["standard", "sansserif", "serif", "fixed", "cursive", "fantasy"]; | |
199 for (var i = 0; i < scripts.length; i++) { | |
200 for (var j = 0; j < families.length; j++) { | |
201 chrome.fontSettings.clearFont({ | |
202 script: scripts[i], | |
203 genericFamily: families[j] | |
204 }); | |
205 } | |
206 } | |
207 | |
208 chrome.fontSettings.clearDefaultFixedFontSize(); | |
209 chrome.fontSettings.clearDefaultFontSize(); | |
210 chrome.fontSettings.clearMinimumFontSize(); | |
211 } | |
212 | |
213 function init() { | |
214 scriptList = document.getElementById('scriptList'); | |
215 scriptList.addEventListener('change', | |
216 updateFontListsForScript); | |
217 | |
218 // Populate the font lists. | |
219 chrome.fontSettings.getFontList(populateLists); | |
220 | |
221 // Add change handlers to the font lists. | |
222 for (var i = 0; i < genericFamilies.length; i++) { | |
223 var list = document.getElementById(genericFamilies[i].fontList); | |
224 var handler = getFontChangeHandler(list, genericFamilies[i].name); | |
225 list.addEventListener('change', handler); | |
226 } | |
227 | |
228 chrome.fontSettings.onFontChanged.addListener( | |
229 updateFontListsForScript); | |
230 | |
231 initFontSizePref('defaultFontSize', | |
232 chrome.fontSettings.getDefaultFontSize, | |
233 chrome.fontSettings.setDefaultFontSize, | |
234 chrome.fontSettings.onDefaultFontSizeChanged); | |
235 initFontSizePref( | |
236 'defaultFixedFontSize', | |
237 chrome.fontSettings.getDefaultFixedFontSize, | |
238 chrome.fontSettings.setDefaultFixedFontSize, | |
239 chrome.fontSettings.onDefaultFixedFontSizeChanged); | |
240 initFontSizePref('minFontSize', | |
241 chrome.fontSettings.getMinimumFontSize, | |
242 chrome.fontSettings.setMinimumFontSize, | |
243 chrome.fontSettings.onMinimumFontSizeChanged); | |
244 | |
245 var clearButton = document.getElementById('clearButton'); | |
246 clearButton.addEventListener('click', clearAllSettings); | |
247 } | |
248 | |
249 document.addEventListener('DOMContentLoaded', init); | |
OLD | NEW |