|
1 // -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- |
|
2 |
|
3 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
4 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
6 |
|
7 var FontBuilder = { |
|
8 _enumerator: null, |
|
9 get enumerator () |
|
10 { |
|
11 if (!this._enumerator) { |
|
12 this._enumerator = Components.classes["@mozilla.org/gfx/fontenumerator;1"] |
|
13 .createInstance(Components.interfaces.nsIFontEnumerator); |
|
14 } |
|
15 return this._enumerator; |
|
16 }, |
|
17 |
|
18 _allFonts: null, |
|
19 buildFontList: function (aLanguage, aFontType, aMenuList) |
|
20 { |
|
21 // Reset the list |
|
22 while (aMenuList.hasChildNodes()) |
|
23 aMenuList.removeChild(aMenuList.firstChild); |
|
24 |
|
25 var defaultFont = null; |
|
26 // Load Font Lists |
|
27 var fonts = this.enumerator.EnumerateFonts(aLanguage, aFontType, { } ); |
|
28 if (fonts.length > 0) |
|
29 defaultFont = this.enumerator.getDefaultFont(aLanguage, aFontType); |
|
30 else { |
|
31 fonts = this.enumerator.EnumerateFonts(aLanguage, "", { }); |
|
32 if (fonts.length > 0) |
|
33 defaultFont = this.enumerator.getDefaultFont(aLanguage, ""); |
|
34 } |
|
35 |
|
36 if (!this._allFonts) |
|
37 this._allFonts = this.enumerator.EnumerateAllFonts({}); |
|
38 |
|
39 // Build the UI for the Default Font and Fonts for this CSS type. |
|
40 var popup = document.createElement("menupopup"); |
|
41 var separator; |
|
42 if (fonts.length > 0) { |
|
43 if (defaultFont) { |
|
44 var bundlePreferences = document.getElementById("bundlePreferences"); |
|
45 var label = bundlePreferences.getFormattedString("labelDefaultFont", [defaultFont]); |
|
46 var menuitem = document.createElement("menuitem"); |
|
47 menuitem.setAttribute("label", label); |
|
48 menuitem.setAttribute("value", ""); // Default Font has a blank value |
|
49 popup.appendChild(menuitem); |
|
50 |
|
51 separator = document.createElement("menuseparator"); |
|
52 popup.appendChild(separator); |
|
53 } |
|
54 |
|
55 for (var i = 0; i < fonts.length; ++i) { |
|
56 menuitem = document.createElement("menuitem"); |
|
57 menuitem.setAttribute("value", fonts[i]); |
|
58 menuitem.setAttribute("label", fonts[i]); |
|
59 popup.appendChild(menuitem); |
|
60 } |
|
61 } |
|
62 |
|
63 // Build the UI for the remaining fonts. |
|
64 if (this._allFonts.length > fonts.length) { |
|
65 // Both lists are sorted, and the Fonts-By-Type list is a subset of the |
|
66 // All-Fonts list, so walk both lists side-by-side, skipping values we've |
|
67 // already created menu items for. |
|
68 var builtItem = separator ? separator.nextSibling : popup.firstChild; |
|
69 var builtItemValue = builtItem ? builtItem.getAttribute("value") : null; |
|
70 |
|
71 separator = document.createElement("menuseparator"); |
|
72 popup.appendChild(separator); |
|
73 |
|
74 for (i = 0; i < this._allFonts.length; ++i) { |
|
75 if (this._allFonts[i] != builtItemValue) { |
|
76 menuitem = document.createElement("menuitem"); |
|
77 menuitem.setAttribute("value", this._allFonts[i]); |
|
78 menuitem.setAttribute("label", this._allFonts[i]); |
|
79 popup.appendChild(menuitem); |
|
80 } |
|
81 else { |
|
82 builtItem = builtItem.nextSibling; |
|
83 builtItemValue = builtItem ? builtItem.getAttribute("value") : null; |
|
84 } |
|
85 } |
|
86 } |
|
87 aMenuList.appendChild(popup); |
|
88 } |
|
89 }; |