Added literallycanvas and react libraries.
[openemr.git] / library / js / literallycanvas / js / core / fontmetrics.js
blob1f2e7e22961a10c369079003ea6a95c023ef64c8
1 "use strict";
3 /**
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 ** -----------------------------------------------------------------------------
11   CHANGELOG:
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
38   THE SOFTWARE.
39 **/
40 (function () {
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.";
47   }
49   // store the old text metrics function on the Canvas2D prototype
50   CanvasRenderingContext2D.prototype.measureTextWidth = CanvasRenderingContext2D.prototype.measureText;
52   /**
53    *  shortcut function for getting computed CSS values
54    */
55   var getCSSValue = function getCSSValue(element, property) {
56     return document.defaultView.getComputedStyle(element, null).getPropertyValue(property);
57   };
59   // debug function
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)';
64     ctx.beginPath();
65     ctx.moveTo(xstart, 0);
66     ctx.lineTo(xstart, h);
67     ctx.closePath();
68     ctx.stroke();
70     ctx.beginPath();
71     ctx.moveTo(xstart + metrics.bounds.maxx, 0);
72     ctx.lineTo(xstart + metrics.bounds.maxx, h);
73     ctx.closePath();
74     ctx.stroke();
76     ctx.beginPath();
77     ctx.moveTo(0, h / 2 - metrics.ascent);
78     ctx.lineTo(w, h / 2 - metrics.ascent);
79     ctx.closePath();
80     ctx.stroke();
82     ctx.beginPath();
83     ctx.moveTo(0, h / 2 + metrics.descent);
84     ctx.lineTo(w, h / 2 + metrics.descent);
85     ctx.closePath();
86     ctx.stroke();
87   };
89   /**
90    * The new text metrics function
91    */
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;
113     }
114     document.body.removeChild(leadDiv);
116     // if we're not dealing with white space, we can compute metrics
117     if (!isSpace) {
118       // Have characters, so measure the text
119       var canvas = document.createElement("canvas");
120       var padding = 100;
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,
129           h = canvas.height,
130           baseline = h / 2;
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.
142       var i = 0,
143           w4 = w * 4,
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
151       i = len - 1;
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;) {
157         i += w4;
158         if (i >= len) {
159           i = i - len + 4;
160         }
161       }
162       var minx = i % w4 / 4 | 0;
164       // find the max-x coordinate
165       var step = 1;
166       for (i = len - 3; i >= 0 && pixelData[i] === 255;) {
167         i -= w4;
168         if (i < 0) {
169           i = len - 3 - step++ * 4;
170         }
171       }
172       var maxx = i % w4 / 4 + 1 | 0;
174       // set font metrics
175       metrics.ascent = baseline - ascent;
176       metrics.descent = descent - baseline;
177       metrics.bounds = { minx: minx - padding / 2,
178         maxx: maxx - padding / 2,
179         miny: 0,
180         maxy: descent - ascent };
181       metrics.height = 1 + (descent - ascent);
182     }
184     // if we ARE dealing with whitespace, most values will just be zero.
185     else {
186         // Only whitespace, so we can't measure the text
187         metrics.ascent = 0;
188         metrics.descent = 0;
189         metrics.bounds = { minx: 0,
190           maxx: metrics.width, // Best guess
191           miny: 0,
192           maxy: 0 };
193         metrics.height = 0;
194       }
195     return metrics;
196   };
197 })();