4 This library rewrites the Canvas2D "measureText" function
5 so that it returns a more complete metrics object.
6 This library is licensed under the MIT (Expat) license,
7 the text for which is included below.
9 ** -----------------------------------------------------------------------------
13 2012-01-21 - Whitespace handling added by Joe Turner
14 (https://github.com/oampo)
16 2015-06-08 - Various hacks added by Steve Johnson
18 ** -----------------------------------------------------------------------------
20 Copyright (C) 2011 by Mike "Pomax" Kamermans
22 Permission is hereby granted, free of charge, to any person obtaining a copy
23 of this software and associated documentation files (the "Software"), to deal
24 in the Software without restriction, including without limitation the rights
25 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
26 copies of the Software, and to permit persons to whom the Software is
27 furnished to do so, subject to the following conditions:
29 The above copyright notice and this permission notice shall be included in
30 all copies or substantial portions of the Software.
32 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
33 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
34 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
35 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
36 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
37 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
41 var NAME = "FontMetrics Library";
42 var VERSION = "1-2012.0121.1300";
44 // if there is no getComputedStyle, this library won't work.
45 if (!document.defaultView.getComputedStyle) {
46 throw "ERROR: 'document.defaultView.getComputedStyle' not found. This library only works in browsers that can report computed CSS values.";
49 // store the old text metrics function on the Canvas2D prototype
50 CanvasRenderingContext2D.prototype.measureTextWidth = CanvasRenderingContext2D.prototype.measureText;
53 * shortcut function for getting computed CSS values
55 var getCSSValue = function getCSSValue(element, property) {
56 return document.defaultView.getComputedStyle(element, null).getPropertyValue(property);
60 var show = function show(canvas, ctx, xstart, w, h, metrics) {
61 document.body.appendChild(canvas);
62 ctx.strokeStyle = 'rgba(0, 0, 0, 0.5)';
65 ctx.moveTo(xstart, 0);
66 ctx.lineTo(xstart, h);
71 ctx.moveTo(xstart + metrics.bounds.maxx, 0);
72 ctx.lineTo(xstart + metrics.bounds.maxx, h);
77 ctx.moveTo(0, h / 2 - metrics.ascent);
78 ctx.lineTo(w, h / 2 - metrics.ascent);
83 ctx.moveTo(0, h / 2 + metrics.descent);
84 ctx.lineTo(w, h / 2 + metrics.descent);
90 * The new text metrics function
92 CanvasRenderingContext2D.prototype.measureText2 = function (textstring, fontSize, fontString) {
93 var metrics = this.measureTextWidth(textstring),
94 isSpace = !/\S/.test(textstring);
95 metrics.fontsize = fontSize;
97 // for text lead values, we meaure a multiline text container.
98 var leadDiv = document.createElement("div");
99 leadDiv.style.position = "absolute";
100 leadDiv.style.opacity = 0;
101 leadDiv.style.font = fontString;
102 leadDiv.innerHTML = textstring + "<br/>" + textstring;
103 document.body.appendChild(leadDiv);
105 // make some initial guess at the text leading (using the standard TeX ratio)
106 metrics.leading = 1.2 * fontSize;
108 // then we try to get the real value from the browser
109 var leadDivHeight = getCSSValue(leadDiv, "height");
110 leadDivHeight = leadDivHeight.replace("px", "");
111 if (leadDivHeight >= fontSize * 2) {
112 metrics.leading = leadDivHeight / 2 | 0;
114 document.body.removeChild(leadDiv);
116 // if we're not dealing with white space, we can compute metrics
118 // Have characters, so measure the text
119 var canvas = document.createElement("canvas");
121 canvas.width = metrics.width + padding;
122 canvas.height = 3 * fontSize;
123 canvas.style.opacity = 1;
124 canvas.style.font = fontString;
125 var ctx = canvas.getContext("2d");
126 ctx.font = fontString;
128 var w = canvas.width,
132 // Set all canvas pixeldata values to 255, with all the content
133 // data being 0. This lets us scan for data[i] != 255.
134 ctx.fillStyle = "white";
135 ctx.fillRect(-1, -1, w + 2, h + 2);
136 ctx.fillStyle = "black";
137 ctx.fillText(textstring, padding / 2, baseline);
138 var pixelData = ctx.getImageData(0, 0, w, h).data;
140 // canvas pixel data is w*4 by h*4, because R, G, B and A are separate,
141 // consecutive values in the array, rather than stored as 32 bit ints.
144 len = pixelData.length;
146 // Finding the ascent uses a normal, forward scanline
147 while (++i < len && pixelData[i] === 255) {}
148 var ascent = i / w4 | 0;
150 // Finding the descent uses a reverse scanline
152 while (--i > 0 && pixelData[i] === 255) {}
153 var descent = i / w4 | 0;
155 // find the min-x coordinate
156 for (i = 0; i < len && pixelData[i] === 255;) {
162 var minx = i % w4 / 4 | 0;
164 // find the max-x coordinate
166 for (i = len - 3; i >= 0 && pixelData[i] === 255;) {
169 i = len - 3 - step++ * 4;
172 var maxx = i % w4 / 4 + 1 | 0;
175 metrics.ascent = baseline - ascent;
176 metrics.descent = descent - baseline;
177 metrics.bounds = { minx: minx - padding / 2,
178 maxx: maxx - padding / 2,
180 maxy: descent - ascent };
181 metrics.height = 1 + (descent - ascent);
184 // if we ARE dealing with whitespace, most values will just be zero.
186 // Only whitespace, so we can't measure the text
189 metrics.bounds = { minx: 0,
190 maxx: metrics.width, // Best guess