1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/toolkit/mozapps/preferences/fontbuilder.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,89 @@ 1.4 +// -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- 1.5 + 1.6 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.8 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.9 + 1.10 +var FontBuilder = { 1.11 + _enumerator: null, 1.12 + get enumerator () 1.13 + { 1.14 + if (!this._enumerator) { 1.15 + this._enumerator = Components.classes["@mozilla.org/gfx/fontenumerator;1"] 1.16 + .createInstance(Components.interfaces.nsIFontEnumerator); 1.17 + } 1.18 + return this._enumerator; 1.19 + }, 1.20 + 1.21 + _allFonts: null, 1.22 + buildFontList: function (aLanguage, aFontType, aMenuList) 1.23 + { 1.24 + // Reset the list 1.25 + while (aMenuList.hasChildNodes()) 1.26 + aMenuList.removeChild(aMenuList.firstChild); 1.27 + 1.28 + var defaultFont = null; 1.29 + // Load Font Lists 1.30 + var fonts = this.enumerator.EnumerateFonts(aLanguage, aFontType, { } ); 1.31 + if (fonts.length > 0) 1.32 + defaultFont = this.enumerator.getDefaultFont(aLanguage, aFontType); 1.33 + else { 1.34 + fonts = this.enumerator.EnumerateFonts(aLanguage, "", { }); 1.35 + if (fonts.length > 0) 1.36 + defaultFont = this.enumerator.getDefaultFont(aLanguage, ""); 1.37 + } 1.38 + 1.39 + if (!this._allFonts) 1.40 + this._allFonts = this.enumerator.EnumerateAllFonts({}); 1.41 + 1.42 + // Build the UI for the Default Font and Fonts for this CSS type. 1.43 + var popup = document.createElement("menupopup"); 1.44 + var separator; 1.45 + if (fonts.length > 0) { 1.46 + if (defaultFont) { 1.47 + var bundlePreferences = document.getElementById("bundlePreferences"); 1.48 + var label = bundlePreferences.getFormattedString("labelDefaultFont", [defaultFont]); 1.49 + var menuitem = document.createElement("menuitem"); 1.50 + menuitem.setAttribute("label", label); 1.51 + menuitem.setAttribute("value", ""); // Default Font has a blank value 1.52 + popup.appendChild(menuitem); 1.53 + 1.54 + separator = document.createElement("menuseparator"); 1.55 + popup.appendChild(separator); 1.56 + } 1.57 + 1.58 + for (var i = 0; i < fonts.length; ++i) { 1.59 + menuitem = document.createElement("menuitem"); 1.60 + menuitem.setAttribute("value", fonts[i]); 1.61 + menuitem.setAttribute("label", fonts[i]); 1.62 + popup.appendChild(menuitem); 1.63 + } 1.64 + } 1.65 + 1.66 + // Build the UI for the remaining fonts. 1.67 + if (this._allFonts.length > fonts.length) { 1.68 + // Both lists are sorted, and the Fonts-By-Type list is a subset of the 1.69 + // All-Fonts list, so walk both lists side-by-side, skipping values we've 1.70 + // already created menu items for. 1.71 + var builtItem = separator ? separator.nextSibling : popup.firstChild; 1.72 + var builtItemValue = builtItem ? builtItem.getAttribute("value") : null; 1.73 + 1.74 + separator = document.createElement("menuseparator"); 1.75 + popup.appendChild(separator); 1.76 + 1.77 + for (i = 0; i < this._allFonts.length; ++i) { 1.78 + if (this._allFonts[i] != builtItemValue) { 1.79 + menuitem = document.createElement("menuitem"); 1.80 + menuitem.setAttribute("value", this._allFonts[i]); 1.81 + menuitem.setAttribute("label", this._allFonts[i]); 1.82 + popup.appendChild(menuitem); 1.83 + } 1.84 + else { 1.85 + builtItem = builtItem.nextSibling; 1.86 + builtItemValue = builtItem ? builtItem.getAttribute("value") : null; 1.87 + } 1.88 + } 1.89 + } 1.90 + aMenuList.appendChild(popup); 1.91 + } 1.92 +};