PlatformFontsSidebarPane.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /*
  2. * Copyright (C) 2013 Google Inc. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions are
  6. * met:
  7. *
  8. * * Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * * Redistributions in binary form must reproduce the above
  11. * copyright notice, this list of conditions and the following disclaimer
  12. * in the documentation and/or other materials provided with the
  13. * distribution.
  14. * * Neither the name of Google Inc. nor the names of its
  15. * contributors may be used to endorse or promote products derived from
  16. * this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  19. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  20. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  21. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  22. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  23. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  24. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  25. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  26. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  28. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. */
  30. /**
  31. * @constructor
  32. * @extends {WebInspector.SidebarPane}
  33. */
  34. WebInspector.PlatformFontsSidebarPane = function()
  35. {
  36. WebInspector.SidebarPane.call(this, WebInspector.UIString("Fonts"));
  37. this.element.addStyleClass("platform-fonts");
  38. WebInspector.domAgent.addEventListener(WebInspector.DOMAgent.Events.AttrModified, this._onNodeChange.bind(this));
  39. WebInspector.domAgent.addEventListener(WebInspector.DOMAgent.Events.AttrRemoved, this._onNodeChange.bind(this));
  40. WebInspector.domAgent.addEventListener(WebInspector.DOMAgent.Events.CharacterDataModified, this._onNodeChange.bind(this));
  41. var cssFontSection = this.element.createChild("div", "stats-section monospace");
  42. var cssFontPrefix = cssFontSection.createChild("span", "webkit-css-property");
  43. cssFontPrefix.textContent = "font-family";
  44. cssFontSection.createTextChild(":");
  45. this._cssFontValue = cssFontSection.createChild("span", "css-font-value");
  46. this._fontStatsSection = this.element.createChild("div", "stats-section");
  47. }
  48. WebInspector.PlatformFontsSidebarPane.prototype = {
  49. _onNodeChange: function()
  50. {
  51. if (this._innerUpdateTimeout)
  52. return;
  53. this._innerUpdateTimeout = setTimeout(this._innerUpdate.bind(this), 100);
  54. },
  55. /**
  56. * @param {WebInspector.DOMNode=} node
  57. */
  58. update: function(node)
  59. {
  60. if (!node) {
  61. delete this._node;
  62. return;
  63. }
  64. this._node = node;
  65. this._innerUpdate();
  66. },
  67. _innerUpdate: function()
  68. {
  69. if (this._innerUpdateTimeout) {
  70. clearTimeout(this._innerUpdateTimeout);
  71. delete this._innerUpdateTimeout;
  72. }
  73. if (!this._node)
  74. return;
  75. WebInspector.cssModel.getPlatformFontsForNode(this._node.id, this._refreshUI.bind(this, this._node));
  76. },
  77. /**
  78. * @param {String} cssFamilyName
  79. * @param {WebInspector.DOMNode} node
  80. */
  81. _refreshUI: function(node, cssFamilyName, platformFonts)
  82. {
  83. if (this._node !== node)
  84. return;
  85. this._cssFontValue.textContent = cssFamilyName + ";";
  86. this._fontStatsSection.removeChildren();
  87. if (!platformFonts || !platformFonts.length)
  88. return;
  89. platformFonts.sort(function (a, b) {
  90. return b.glyphCount - a.glyphCount;
  91. });
  92. for (var i = 0; i < platformFonts.length; ++i) {
  93. var fontStatElement = this._fontStatsSection.createChild("div", "font-stats-item");
  94. var fontNameElement = fontStatElement.createChild("span", "font-name");
  95. fontNameElement.textContent = platformFonts[i].familyName;
  96. var fontDelimeterElement = fontStatElement.createChild("span", "delimeter");
  97. fontDelimeterElement.textContent = "\u2014";
  98. var fontUsageElement = fontStatElement.createChild("span", "font-usage");
  99. var usage = platformFonts[i].glyphCount;
  100. fontUsageElement.textContent = usage === 1 ? WebInspector.UIString("%d glyph", usage) : WebInspector.UIString("%d glyphs", usage);
  101. }
  102. },
  103. __proto__: WebInspector.SidebarPane.prototype
  104. }