-
Notifications
You must be signed in to change notification settings - Fork 51
Expand file tree
/
Copy pathFontMetrics.js
More file actions
82 lines (70 loc) · 1.91 KB
/
FontMetrics.js
File metadata and controls
82 lines (70 loc) · 1.91 KB
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
"use strict";
/**
* A simple wrapper for system fonts to provide
* @param {String} family Font Family (same as in CSS)
* @param {Number} size Size in px
* @constructor
*/
var FontMetrics = function(family, size) {
this._family = family || (family = "Monaco, 'Courier New', Courier, monospace");
this._size = parseInt(size) || (size = 12);
// Preparing container
var line = document.createElement('div'),
body = document.body;
line.style.position = 'absolute';
line.style.whiteSpace = 'nowrap';
line.style.font = size + 'px ' + family;
body.appendChild(line);
// Now we can measure width and height of the letter
var text = 'mmmmmmmmmm'; // 10 symbols to be more accurate with width
line.innerHTML = text;
this._width = line.offsetWidth / text.length;
this._height = line.offsetHeight;
// Now creating 1px sized item that will be aligned to baseline
// to calculate baseline shift
var span = document.createElement('span');
span.style.display = 'inline-block';
span.style.overflow = 'hidden';
span.style.width = '1px';
span.style.height = '1px';
line.appendChild(span);
// Baseline is important for positioning text on canvas
this._baseline = span.offsetTop + span.offsetHeight;
document.body.removeChild(line);
};
module.exports = FontMetrics;
/**
* Returns font family
* @return {String}
*/
FontMetrics.prototype.getFamily = function() {
return this._family;
};
/**
* Returns font family
* @return {Number}
*/
FontMetrics.prototype.getSize = function() {
return this._size;
};
/**
* Returns line height in px
* @return {Number}
*/
FontMetrics.prototype.getHeight = function() {
return this._height;
};
/**
* Returns line height in px
* @return {Number}
*/
FontMetrics.prototype.getWidth = function() {
return this._width;
};
/**
* Returns line height in px
* @return {Number}
*/
FontMetrics.prototype.getBaseline = function() {
return this._baseline;
};