Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / third_party / WebKit / Source / devtools / front_end / elements / PlatformFontsWidget.js
blob037897bf31f34654ec009aa43d83912f321795ed
1 /*
2 * Copyright (C) 2013 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met:
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.
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.
31 /**
32 * @constructor
33 * @extends {WebInspector.ThrottledWidget}
34 * @param {!WebInspector.SharedSidebarModel} sharedModel
36 WebInspector.PlatformFontsWidget = function(sharedModel)
38 WebInspector.ThrottledWidget.call(this);
39 this.element.classList.add("platform-fonts");
41 this._sharedModel = sharedModel;
42 this._sharedModel.addEventListener(WebInspector.SharedSidebarModel.Events.ComputedStyleChanged, this.update, this);
44 this._sectionTitle = createElementWithClass("div", "sidebar-separator");
45 this.element.appendChild(this._sectionTitle);
46 this._sectionTitle.textContent = WebInspector.UIString("Rendered Fonts");
47 this._fontStatsSection = this.element.createChild("div", "stats-section");
50 /**
51 * @param {!WebInspector.SharedSidebarModel} sharedModel
52 * @return {!WebInspector.ElementsSidebarViewWrapperPane}
54 WebInspector.PlatformFontsWidget.createSidebarWrapper = function(sharedModel)
56 var widget = new WebInspector.PlatformFontsWidget(sharedModel);
57 return new WebInspector.ElementsSidebarViewWrapperPane(WebInspector.UIString("Fonts"), widget)
60 WebInspector.PlatformFontsWidget.prototype = {
61 /**
62 * @override
63 * @protected
64 * @return {!Promise.<?>}
66 doUpdate: function()
68 var cssModel = this._sharedModel.cssModel();
69 var node = this._sharedModel.node();
70 if (!node || !cssModel)
71 return Promise.resolve();
73 return cssModel.platformFontsPromise(node.id)
74 .then(this._refreshUI.bind(this, node))
77 /**
78 * @param {!WebInspector.DOMNode} node
79 * @param {?Array.<!CSSAgent.PlatformFontUsage>} platformFonts
81 _refreshUI: function(node, platformFonts)
83 if (this._sharedModel.node() !== node)
84 return;
86 this._fontStatsSection.removeChildren();
88 var isEmptySection = !platformFonts || !platformFonts.length;
89 this._sectionTitle.classList.toggle("hidden", isEmptySection);
90 if (isEmptySection)
91 return;
93 platformFonts.sort(function (a, b) {
94 return b.glyphCount - a.glyphCount;
95 });
96 for (var i = 0; i < platformFonts.length; ++i) {
97 var fontStatElement = this._fontStatsSection.createChild("div", "font-stats-item");
99 var fontNameElement = fontStatElement.createChild("span", "font-name");
100 fontNameElement.textContent = platformFonts[i].familyName;
102 var fontDelimeterElement = fontStatElement.createChild("span", "delimeter");
103 fontDelimeterElement.textContent = "\u2014";
105 var fontUsageElement = fontStatElement.createChild("span", "font-usage");
106 var usage = platformFonts[i].glyphCount;
107 fontUsageElement.textContent = usage === 1 ? WebInspector.UIString("%d glyph", usage) : WebInspector.UIString("%d glyphs", usage);
111 __proto__: WebInspector.ThrottledWidget.prototype