3 Copyright (c) 2013 The Chromium Authors. All rights reserved.
4 Use of this source code is governed by a BSD-style license that can be
5 found in the LICENSE file.
7 <link rel="import" href="/tvcm.html">
12 * @fileoverview Provides a caching layer for elided text values.
14 tvcm.exportTo('tracing', function() {
16 * Cache for elided strings.
17 * Moved from the ElidedTitleCache protoype to a "global" for speed
18 * (variable reference is 100x faster).
19 * key: String we wish to elide.
20 * value: Another dict whose key is width
21 * and value is an ElidedStringWidthPair.
23 var elidedTitleCacheDict = {};
24 var elidedTitleCache = new ElidedTitleCache();
27 * A cache for elided strings.
30 function ElidedTitleCache() {
31 // TODO(jrg): possibly obsoleted with the elided string cache.
33 this.textWidthMap = {};
36 ElidedTitleCache.prototype = {
40 * @param {ctx} Context The graphics context.
41 * @param {pixWidth} Pixel width.
42 * @param {title} Original title text.
43 * @param {width} Drawn width in world coords.
44 * @param {sliceDuration} Where the title must fit (in world coords).
45 * @return {ElidedStringWidthPair} Elided string and width.
47 get: function(ctx, pixWidth, title, width, sliceDuration) {
48 var elidedDict = elidedTitleCacheDict[title];
51 elidedTitleCacheDict[title] = elidedDict;
54 var elidedDictForPixWidth = elidedDict[pixWidth];
55 if (!elidedDictForPixWidth) {
56 elidedDict[pixWidth] = {};
57 elidedDictForPixWidth = elidedDict[pixWidth];
60 var stringWidthPair = elidedDictForPixWidth[sliceDuration];
61 if (stringWidthPair === undefined) {
64 while (this.labelWidthWorld(ctx, newtitle, pixWidth) > sliceDuration) {
65 if (newtitle.length * 0.75 < 1)
67 newtitle = newtitle.substring(0, newtitle.length * 0.75);
71 if (elided && newtitle.length > 3)
72 newtitle = newtitle.substring(0, newtitle.length - 3) + '...';
74 stringWidthPair = new ElidedStringWidthPair(
75 newtitle, this.labelWidth(ctx, newtitle));
76 elidedDictForPixWidth[sliceDuration] = stringWidthPair;
78 return stringWidthPair;
81 quickMeasureText_: function(ctx, text) {
82 var w = this.textWidthMap[text];
84 w = ctx.measureText(text).width;
85 this.textWidthMap[text] = w;
90 labelWidth: function(ctx, title) {
91 return this.quickMeasureText_(ctx, title) + 2;
94 labelWidthWorld: function(ctx, title, pixWidth) {
95 return this.labelWidth(ctx, title) * pixWidth;
100 * A pair representing an elided string and world-coordinate width
104 function ElidedStringWidthPair(string, width) {
105 this.string = string;
110 ElidedTitleCache: ElidedTitleCache