michael@0: // -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- michael@0: michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: var FontBuilder = { michael@0: _enumerator: null, michael@0: get enumerator () michael@0: { michael@0: if (!this._enumerator) { michael@0: this._enumerator = Components.classes["@mozilla.org/gfx/fontenumerator;1"] michael@0: .createInstance(Components.interfaces.nsIFontEnumerator); michael@0: } michael@0: return this._enumerator; michael@0: }, michael@0: michael@0: _allFonts: null, michael@0: buildFontList: function (aLanguage, aFontType, aMenuList) michael@0: { michael@0: // Reset the list michael@0: while (aMenuList.hasChildNodes()) michael@0: aMenuList.removeChild(aMenuList.firstChild); michael@0: michael@0: var defaultFont = null; michael@0: // Load Font Lists michael@0: var fonts = this.enumerator.EnumerateFonts(aLanguage, aFontType, { } ); michael@0: if (fonts.length > 0) michael@0: defaultFont = this.enumerator.getDefaultFont(aLanguage, aFontType); michael@0: else { michael@0: fonts = this.enumerator.EnumerateFonts(aLanguage, "", { }); michael@0: if (fonts.length > 0) michael@0: defaultFont = this.enumerator.getDefaultFont(aLanguage, ""); michael@0: } michael@0: michael@0: if (!this._allFonts) michael@0: this._allFonts = this.enumerator.EnumerateAllFonts({}); michael@0: michael@0: // Build the UI for the Default Font and Fonts for this CSS type. michael@0: var popup = document.createElement("menupopup"); michael@0: var separator; michael@0: if (fonts.length > 0) { michael@0: if (defaultFont) { michael@0: var bundlePreferences = document.getElementById("bundlePreferences"); michael@0: var label = bundlePreferences.getFormattedString("labelDefaultFont", [defaultFont]); michael@0: var menuitem = document.createElement("menuitem"); michael@0: menuitem.setAttribute("label", label); michael@0: menuitem.setAttribute("value", ""); // Default Font has a blank value michael@0: popup.appendChild(menuitem); michael@0: michael@0: separator = document.createElement("menuseparator"); michael@0: popup.appendChild(separator); michael@0: } michael@0: michael@0: for (var i = 0; i < fonts.length; ++i) { michael@0: menuitem = document.createElement("menuitem"); michael@0: menuitem.setAttribute("value", fonts[i]); michael@0: menuitem.setAttribute("label", fonts[i]); michael@0: popup.appendChild(menuitem); michael@0: } michael@0: } michael@0: michael@0: // Build the UI for the remaining fonts. michael@0: if (this._allFonts.length > fonts.length) { michael@0: // Both lists are sorted, and the Fonts-By-Type list is a subset of the michael@0: // All-Fonts list, so walk both lists side-by-side, skipping values we've michael@0: // already created menu items for. michael@0: var builtItem = separator ? separator.nextSibling : popup.firstChild; michael@0: var builtItemValue = builtItem ? builtItem.getAttribute("value") : null; michael@0: michael@0: separator = document.createElement("menuseparator"); michael@0: popup.appendChild(separator); michael@0: michael@0: for (i = 0; i < this._allFonts.length; ++i) { michael@0: if (this._allFonts[i] != builtItemValue) { michael@0: menuitem = document.createElement("menuitem"); michael@0: menuitem.setAttribute("value", this._allFonts[i]); michael@0: menuitem.setAttribute("label", this._allFonts[i]); michael@0: popup.appendChild(menuitem); michael@0: } michael@0: else { michael@0: builtItem = builtItem.nextSibling; michael@0: builtItemValue = builtItem ? builtItem.getAttribute("value") : null; michael@0: } michael@0: } michael@0: } michael@0: aMenuList.appendChild(popup); michael@0: } michael@0: };