OLD | NEW |
1 // | 1 // |
2 // Copyright 2014 Google Inc. All rights reserved. | 2 // Copyright 2014 Google Inc. All rights reserved. |
3 // | 3 // |
4 // Use of this source code is governed by a BSD-style | 4 // Use of this source code is governed by a BSD-style |
5 // license that can be found in the LICENSE file or at | 5 // license that can be found in the LICENSE file or at |
6 // https://developers.google.com/open-source/licenses/bsd | 6 // https://developers.google.com/open-source/licenses/bsd |
7 // | 7 // |
8 | 8 |
9 /// Provides a way to measure rendered text width for clipping | 9 /// Provides a way to measure rendered text width for clipping |
10 /// text on tooltips and ticks when they are too long. | 10 /// text on tooltips and ticks when they are too long. |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
50 context.font = fontStyle; | 50 context.font = fontStyle; |
51 currentFontStyle = fontStyle; | 51 currentFontStyle = fontStyle; |
52 } | 52 } |
53 } | 53 } |
54 | 54 |
55 /// Measure width of [text] in pixels. | 55 /// Measure width of [text] in pixels. |
56 /// Optionally, uses [fontStyle] instead of using the default style | 56 /// Optionally, uses [fontStyle] instead of using the default style |
57 double getTextWidth(String text, {String fontStyle}) { | 57 double getTextWidth(String text, {String fontStyle}) { |
58 assert(text.length <= MAX_STRING_LENGTH); | 58 assert(text.length <= MAX_STRING_LENGTH); |
59 setFontStyle(fontStyle); | 59 setFontStyle(fontStyle); |
60 return context.measureText(text).width; | 60 return context.measureText(text).width.toDouble(); |
61 } | 61 } |
62 | 62 |
63 /// Gets length of the longest string in the given [strings]. | 63 /// Gets length of the longest string in the given [strings]. |
64 /// Optionally, uses [fontStyle] instead of using the default style. | 64 /// Optionally, uses [fontStyle] instead of using the default style. |
65 double getLongestTextWidth(Iterable<String> strings, {String fontStyle}) { | 65 double getLongestTextWidth(Iterable<String> strings, {String fontStyle}) { |
66 setFontStyle(fontStyle); | 66 setFontStyle(fontStyle); |
67 double maxWidth = 0.0; | 67 num maxWidth = 0.0; |
68 for (int i = 0; i < strings.length; ++i) { | 68 for (int i = 0; i < strings.length; ++i) { |
69 assert(strings.elementAt(i).length <= MAX_STRING_LENGTH); | 69 assert(strings.elementAt(i).length <= MAX_STRING_LENGTH); |
70 double width = context.measureText(strings.elementAt(i)).width; | 70 double width = context.measureText(strings.elementAt(i)).width.toDouble(); |
71 if (width > maxWidth) { | 71 if (width > maxWidth) { |
72 maxWidth = width; | 72 maxWidth = width; |
73 } | 73 } |
74 } | 74 } |
75 | 75 |
76 return maxWidth; | 76 return maxWidth; |
77 } | 77 } |
78 | 78 |
79 /// Truncates given [text] to fit in [width]. Adds an ellipsis to the | 79 /// Truncates given [text] to fit in [width]. Adds an ellipsis to the |
80 /// returned string, if it needed to be truncated. | 80 /// returned string, if it needed to be truncated. |
81 /// Optionally, uses [fontStyle] instead of using the default style. | 81 /// Optionally, uses [fontStyle] instead of using the default style. |
82 String ellipsizeText(String text, double width, {String fontStyle}) { | 82 String ellipsizeText(String text, num width, {String fontStyle}) { |
83 assert(text.length <= MAX_STRING_LENGTH); | 83 assert(text.length <= MAX_STRING_LENGTH); |
84 setFontStyle(fontStyle); | 84 setFontStyle(fontStyle); |
85 double computedWidth = context.measureText(text).width; | 85 double computedWidth = context.measureText(text).width.toDouble(); |
86 if (computedWidth > width) { | 86 if (computedWidth > width) { |
87 var indices = graphemeBreakIndices(text); | 87 var indices = graphemeBreakIndices(text); |
88 var position = 0, | 88 var position = 0, |
89 min = 0, max = indices.length - 1, mid, | 89 min = 0, |
| 90 max = indices.length - 1, |
| 91 mid, |
90 ellipsis = context.measureText('…').width; | 92 ellipsis = context.measureText('…').width; |
91 width = width - ellipsis; | 93 width = width - ellipsis; |
92 while (max >= min) { | 94 while (max >= min) { |
93 mid = (min + max) ~/ 2; | 95 mid = (min + max) ~/ 2; |
94 position = indices[mid]; | 96 position = indices[mid]; |
95 if (context.measureText(text.substring(0, position)).width > width) { | 97 if (context.measureText(text.substring(0, position)).width > width) { |
96 max = mid - 1; | 98 max = mid - 1; |
97 } else { | 99 } else { |
98 min = mid + 1; | 100 min = mid + 1; |
99 } | 101 } |
100 } | 102 } |
101 text = text.substring(0, indices[max]) + '…'; | 103 text = text.substring(0, indices[max]) + '…'; |
102 } | 104 } |
103 return text; | 105 return text; |
104 } | 106 } |
105 | 107 |
106 /// Truncates text in the given [element], which is either a [SvgTextElement] | 108 /// Truncates text in the given [element], which is either a [SvgTextElement] |
107 /// or a [SvgTspanElement] to fit in [width]. Appends an ellipsis to the text | 109 /// or a [SvgTspanElement] to fit in [width]. Appends an ellipsis to the text |
108 /// if it had to be truncated. | 110 /// if it had to be truncated. |
109 /// Calling this method may force a layout on the document. For better | 111 /// Calling this method may force a layout on the document. For better |
110 /// performance, use [TextMetrics.ellipsizeText]. | 112 /// performance, use [TextMetrics.ellipsizeText]. |
111 static ellipsizeTextElement() { | 113 static ellipsizeTextElement() {} |
112 } | |
113 } | 114 } |
OLD | NEW |