/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. var FontBuilder = {
  38. _enumerator: null,
  39. get enumerator ()
  40. {
  41. if (!this._enumerator) {
  42. this._enumerator = Components.classes["@mozilla.org/gfx/fontenumerator;1"]
  43. .createInstance(Components.interfaces.nsIFontEnumerator);
  44. }
  45. return this._enumerator;
  46. },
  47. _allFonts: null,
  48. buildFontList: function (aLanguage, aFontType, aMenuList)
  49. {
  50. // Reset the list
  51. while (aMenuList.hasChildNodes())
  52. aMenuList.removeChild(aMenuList.firstChild);
  53. var defaultFont = null;
  54. // Load Font Lists
  55. var fonts = this.enumerator.EnumerateFonts(aLanguage, aFontType, { } );
  56. if (fonts.length > 0)
  57. defaultFont = this.enumerator.getDefaultFont(aLanguage, aFontType);
  58. else {
  59. fonts = this.enumerator.EnumerateFonts(aLanguage, "", { });
  60. if (fonts.length > 0)
  61. defaultFont = this.enumerator.getDefaultFont(aLanguage, "");
  62. }
  63. if (!this._allFonts)
  64. this._allFonts = this.enumerator.EnumerateAllFonts({});
  65. // Build the UI for the Default Font and Fonts for this CSS type.
  66. var popup = document.createElement("menupopup");
  67. var separator;
  68. if (fonts.length > 0) {
  69. if (defaultFont) {
  70. var bundlePreferences = document.getElementById("bundlePreferences");
  71. var label = bundlePreferences.getFormattedString("labelDefaultFont", [defaultFont]);
  72. var menuitem = document.createElement("menuitem");
  73. menuitem.setAttribute("label", label);
  74. menuitem.setAttribute("value", ""); // Default Font has a blank value
  75. popup.appendChild(menuitem);
  76. separator = document.createElement("menuseparator");
  77. popup.appendChild(separator);
  78. }
  79. for (var i = 0; i < fonts.length; ++i) {
  80. menuitem = document.createElement("menuitem");
  81. menuitem.setAttribute("value", fonts[i]);
  82. menuitem.setAttribute("label", fonts[i]);
  83. popup.appendChild(menuitem);
  84. }
  85. }
  86. // Build the UI for the remaining fonts.
  87. if (this._allFonts.length > fonts.length) {
  88. // Both lists are sorted, and the Fonts-By-Type list is a subset of the
  89. // All-Fonts list, so walk both lists side-by-side, skipping values we've
  90. // already created menu items for.
  91. var builtItem = separator ? separator.nextSibling : popup.firstChild;
  92. var builtItemValue = builtItem ? builtItem.getAttribute("value") : null;
  93. separator = document.createElement("menuseparator");
  94. popup.appendChild(separator);
  95. for (i = 0; i < this._allFonts.length; ++i) {
  96. if (this._allFonts[i] != builtItemValue) {
  97. menuitem = document.createElement("menuitem");
  98. menuitem.setAttribute("value", this._allFonts[i]);
  99. menuitem.setAttribute("label", this._allFonts[i]);
  100. popup.appendChild(menuitem);
  101. }
  102. else {
  103. builtItem = builtItem.nextSibling;
  104. builtItemValue = builtItem ? builtItem.getAttribute("value") : null;
  105. }
  106. }
  107. }
  108. aMenuList.appendChild(popup);
  109. }
  110. };