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