PageRenderTime 74ms CodeModel.GetById 37ms app.highlight 32ms RepoModel.GetById 1ms app.codeStats 1ms

/toolkit/mozapps/preferences/fontbuilder.js

http://github.com/zpao/v8monkey
JavaScript | 120 lines | 103 code | 10 blank | 7 comment | 15 complexity | 4f6bdae4985ceebf573a500dd0db3491 MD5 | raw file
  1# -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
  2# ***** BEGIN LICENSE BLOCK *****
  3# Version: MPL 1.1/GPL 2.0/LGPL 2.1
  4#
  5# The contents of this file are subject to the Mozilla Public License Version
  6# 1.1 (the "License"); you may not use this file except in compliance with
  7# the License. You may obtain a copy of the License at
  8# http://www.mozilla.org/MPL/
  9#
 10# Software distributed under the License is distributed on an "AS IS" basis,
 11# WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 12# for the specific language governing rights and limitations under the
 13# License.
 14#
 15# The Original Code is Mozilla.org Code.
 16#
 17# The Initial Developer of the Original Code is
 18# Doron Rosenberg.
 19# Portions created by the Initial Developer are Copyright (C) 2001
 20# the Initial Developer. All Rights Reserved.
 21#
 22# Contributor(s):
 23#
 24# Alternatively, the contents of this file may be used under the terms of
 25# either the GNU General Public License Version 2 or later (the "GPL"), or
 26# the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
 27# in which case the provisions of the GPL or the LGPL are applicable instead
 28# of those above. If you wish to allow use of your version of this file only
 29# under the terms of either the GPL or the LGPL, and not to allow others to
 30# use your version of this file under the terms of the MPL, indicate your
 31# decision by deleting the provisions above and replace them with the notice
 32# and other provisions required by the GPL or the LGPL. If you do not delete
 33# the provisions above, a recipient may use your version of this file under
 34# the terms of any one of the MPL, the GPL or the LGPL.
 35#
 36# ***** END LICENSE BLOCK *****
 37
 38var FontBuilder = {
 39  _enumerator: null,
 40  get enumerator ()
 41  {
 42    if (!this._enumerator) {
 43      this._enumerator = Components.classes["@mozilla.org/gfx/fontenumerator;1"]
 44                                   .createInstance(Components.interfaces.nsIFontEnumerator);
 45    }
 46    return this._enumerator;
 47  },
 48
 49  _allFonts: null,
 50  buildFontList: function (aLanguage, aFontType, aMenuList) 
 51  {
 52    // Reset the list
 53    while (aMenuList.hasChildNodes())
 54      aMenuList.removeChild(aMenuList.firstChild);
 55    
 56    var defaultFont = null;
 57    // Load Font Lists
 58    var fonts = this.enumerator.EnumerateFonts(aLanguage, aFontType, { } );
 59    if (fonts.length > 0)
 60      defaultFont = this.enumerator.getDefaultFont(aLanguage, aFontType);
 61    else {
 62      fonts = this.enumerator.EnumerateFonts(aLanguage, "", { });
 63      if (fonts.length > 0)
 64        defaultFont = this.enumerator.getDefaultFont(aLanguage, "");
 65    }
 66    
 67    if (!this._allFonts)
 68      this._allFonts = this.enumerator.EnumerateAllFonts({});
 69    
 70    // Build the UI for the Default Font and Fonts for this CSS type.
 71    var popup = document.createElement("menupopup");
 72    var separator;
 73    if (fonts.length > 0) {
 74      if (defaultFont) {
 75        var bundlePreferences = document.getElementById("bundlePreferences");
 76        var label = bundlePreferences.getFormattedString("labelDefaultFont", [defaultFont]);
 77        var menuitem = document.createElement("menuitem");
 78        menuitem.setAttribute("label", label);
 79        menuitem.setAttribute("value", ""); // Default Font has a blank value
 80        popup.appendChild(menuitem);
 81        
 82        separator = document.createElement("menuseparator");
 83        popup.appendChild(separator);
 84      }
 85      
 86      for (var i = 0; i < fonts.length; ++i) {
 87        menuitem = document.createElement("menuitem");
 88        menuitem.setAttribute("value", fonts[i]);
 89        menuitem.setAttribute("label", fonts[i]);
 90        popup.appendChild(menuitem);
 91      }
 92    }
 93    
 94    // Build the UI for the remaining fonts. 
 95    if (this._allFonts.length > fonts.length) {
 96      // Both lists are sorted, and the Fonts-By-Type list is a subset of the
 97      // All-Fonts list, so walk both lists side-by-side, skipping values we've
 98      // already created menu items for. 
 99      var builtItem = separator ? separator.nextSibling : popup.firstChild;
100      var builtItemValue = builtItem ? builtItem.getAttribute("value") : null;
101
102      separator = document.createElement("menuseparator");
103      popup.appendChild(separator);
104      
105      for (i = 0; i < this._allFonts.length; ++i) {
106        if (this._allFonts[i] != builtItemValue) {
107          menuitem = document.createElement("menuitem");
108          menuitem.setAttribute("value", this._allFonts[i]);
109          menuitem.setAttribute("label", this._allFonts[i]);
110          popup.appendChild(menuitem);
111        }
112        else {
113          builtItem = builtItem.nextSibling;
114          builtItemValue = builtItem ? builtItem.getAttribute("value") : null;
115        }
116      }
117    }
118    aMenuList.appendChild(popup);    
119  }
120};