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 /** 32 * @constructor 33 * @extends {WebInspector.SidebarPane} 34 */ 35 WebInspector.PlatformFontsSidebarPane = function() 36 { 37 WebInspector.SidebarPane.call(this, WebInspector.UIString("Fonts")); 38 this.element.classList.add("platform-fonts"); 39 40 this._sectionTitle = document.createElementWithClass("div", "sidebar-separator"); 41 this.element.insertBefore(this._sectionTitle, this.bodyElement); 42 this._sectionTitle.textContent = WebInspector.UIString("Rendered Fonts"); 43 this._fontStatsSection = this.bodyElement.createChild("div", "stats-section"); 44 } 45 46 WebInspector.PlatformFontsSidebarPane.prototype = { 47 _onNodeChange: function() 48 { 49 if (this._innerUpdateTimeout) 50 return; 51 this._innerUpdateTimeout = setTimeout(this._innerUpdate.bind(this), 100); 52 }, 53 54 /** 55 * @param {?WebInspector.DOMNode} node 56 */ 57 update: function(node) 58 { 59 if (!node) { 60 delete this._node; 61 return; 62 } 63 this._node = node; 64 this._updateTarget(node.target()); 65 this._innerUpdate(); 66 }, 67 68 /** 69 * @param {!WebInspector.Target} target 70 */ 71 _updateTarget: function(target) 72 { 73 if (this._target === target) 74 return; 75 if (this._target) { 76 this._target.domModel.removeEventListener(WebInspector.DOMModel.Events.AttrModified, this._onNodeChange, this); 77 this._target.domModel.removeEventListener(WebInspector.DOMModel.Events.AttrRemoved, this._onNodeChange, this); 78 this._target.domModel.removeEventListener(WebInspector.DOMModel.Events.CharacterDataModified, this._onNodeChange, this); 79 } 80 this._target = target; 81 this._target.domModel.addEventListener(WebInspector.DOMModel.Events.AttrModified, this._onNodeChange, this); 82 this._target.domModel.addEventListener(WebInspector.DOMModel.Events.AttrRemoved, this._onNodeChange, this); 83 this._target.domModel.addEventListener(WebInspector.DOMModel.Events.CharacterDataModified, this._onNodeChange, this); 84 }, 85 86 _innerUpdate: function() 87 { 88 if (this._innerUpdateTimeout) { 89 clearTimeout(this._innerUpdateTimeout); 90 delete this._innerUpdateTimeout; 91 } 92 if (!this._node) 93 return; 94 this._target.cssModel.getPlatformFontsForNode(this._node.id, this._refreshUI.bind(this, this._node)); 95 }, 96 97 /** 98 * @param {!WebInspector.DOMNode} node 99 * @param {?string} cssFamilyName 100 * @param {?Array.<!CSSAgent.PlatformFontUsage>} platformFonts 101 */ 102 _refreshUI: function(node, cssFamilyName, platformFonts) 103 { 104 if (this._node !== node) 105 return; 106 107 this._fontStatsSection.removeChildren(); 108 109 var isEmptySection = !platformFonts || !platformFonts.length; 110 this._sectionTitle.classList.toggle("hidden", isEmptySection); 111 if (isEmptySection) 112 return; 113 platformFonts.sort(function (a, b) { 114 return b.glyphCount - a.glyphCount; 115 }); 116 for (var i = 0; i < platformFonts.length; ++i) { 117 var fontStatElement = this._fontStatsSection.createChild("div", "font-stats-item"); 118 119 var fontNameElement = fontStatElement.createChild("span", "font-name"); 120 fontNameElement.textContent = platformFonts[i].familyName; 121 122 var fontDelimeterElement = fontStatElement.createChild("span", "delimeter"); 123 fontDelimeterElement.textContent = "\u2014"; 124 125 var fontUsageElement = fontStatElement.createChild("span", "font-usage"); 126 var usage = platformFonts[i].glyphCount; 127 fontUsageElement.textContent = usage === 1 ? WebInspector.UIString("%d glyph", usage) : WebInspector.UIString("%d glyphs", usage); 128 } 129 }, 130 131 __proto__: WebInspector.SidebarPane.prototype 132 } 133