summaryrefslogtreecommitdiff
path: root/chromium/third_party/catapult/tracing/tracing/ui/base/elided_cache.html
blob: 7019f9807da2661911add48c75d8d781b99f3387 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
<!DOCTYPE html>
<!--
Copyright (c) 2013 The Chromium Authors. All rights reserved.
Use of this source code is governed by a BSD-style license that can be
found in the LICENSE file.
-->
<link rel="import" href="/tracing/base/base.html">
<script>
'use strict';

/**
 * @fileoverview Provides a caching layer for elided text values.
 */
tr.exportTo('tr.ui.b', function() {
  /**
   * Cache for elided strings.
   * Moved from the ElidedTitleCache protoype to a "global" for speed
   * (variable reference is 100x faster).
   *   key: String we wish to elide.
   *   value: Another dict whose key is width
   *     and value is an ElidedStringWidthPair.
   */
  var elidedTitleCacheDict = {};
  var elidedTitleCache = new ElidedTitleCache();

  /**
   * A cache for elided strings.
   * @constructor
   */
  function ElidedTitleCache() {
    // TODO(jrg): possibly obsoleted with the elided string cache.
    // Consider removing.
    this.textWidthMap = {};
  }

  ElidedTitleCache.prototype = {
    /**
     * Return elided text.
     *
     * @param {ctx} Context The graphics context.
     * @param {pixWidth} Pixel width.
     * @param {title} Original title text.
     * @param {width} Drawn width in world coords.
     * @param {sliceDuration} Where the title must fit (in world coords).
     * @return {ElidedStringWidthPair} Elided string and width.
     */
    get: function(ctx, pixWidth, title, width, sliceDuration) {
      var elidedDict = elidedTitleCacheDict[title];
      if (!elidedDict) {
        elidedDict = {};
        elidedTitleCacheDict[title] = elidedDict;
      }

      var elidedDictForPixWidth = elidedDict[pixWidth];
      if (!elidedDictForPixWidth) {
        elidedDict[pixWidth] = {};
        elidedDictForPixWidth = elidedDict[pixWidth];
      }

      var stringWidthPair = elidedDictForPixWidth[sliceDuration];
      if (stringWidthPair === undefined) {
        var newtitle = title;
        var elided = false;
        while (this.labelWidthWorld(ctx, newtitle, pixWidth) > sliceDuration) {
          if (newtitle.length * 0.75 < 1)
            break;
          newtitle = newtitle.substring(0, newtitle.length * 0.75);
          elided = true;
        }

        if (elided && newtitle.length > 3)
          newtitle = newtitle.substring(0, newtitle.length - 3) + '...';

        stringWidthPair = new ElidedStringWidthPair(
            newtitle, this.labelWidth(ctx, newtitle));
        elidedDictForPixWidth[sliceDuration] = stringWidthPair;
      }
      return stringWidthPair;
    },

    quickMeasureText_: function(ctx, text) {
      var w = this.textWidthMap[text];
      if (!w) {
        w = ctx.measureText(text).width;
        this.textWidthMap[text] = w;
      }
      return w;
    },

    labelWidth: function(ctx, title) {
      return this.quickMeasureText_(ctx, title) + 2;
    },

    labelWidthWorld: function(ctx, title, pixWidth) {
      return this.labelWidth(ctx, title) * pixWidth;
    }
  };

  /**
   * A pair representing an elided string and world-coordinate width
   * to draw it.
   * @constructor
   */
  function ElidedStringWidthPair(string, width) {
    this.string = string;
    this.width = width;
  }

  return {
    ElidedTitleCache: ElidedTitleCache
  };
});
</script>