- 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
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 139
- 140
- 141
- 142
- 143
- 144
- 145
- 146
- 147
- 148
- 149
- 150
- 151
- 152
- 153
- 154
- 155
- 156
- 157
- 158
- 159
- 160
- 161
- 162
- 163
- 164
- 165
- 166
- 167
- 168
import Bounds from "../../physics/bounds.js";
import Text from "./text.js";
import setContextStyle from "./textstyle.js";
/**
* @classdesc
* a Text Metrics object that contains helper for text manipulation
* @augments Bounds
*/
export default class TextMetrics extends Bounds {
/**
* @param {Text|BitmapText} ancestor - the parent object that contains this TextMetrics object
*/
constructor(ancestor) {
// parent constructor
super();
/**
* a reference to the parent object that contains this TextMetrics object
* @public
* @type {Renderable}
* @default undefined
*/
this.ancestor = ancestor;
this.setMinMax(0, 0, 0, 0);
}
/**
* Returns the height of a segment of inline text in CSS pixels.
* @returns {number} the height of a segment of inline text in CSS pixels.
*/
lineHeight() {
if (this.ancestor instanceof Text) {
return this.ancestor.fontSize * this.ancestor.lineHeight;
} else { // it's a BitmapText
return this.ancestor.fontData.capHeight * this.ancestor.lineHeight * this.ancestor.fontScale.y;
}
}
/**
* Returns the width of the given segment of inline text in CSS pixels.
* @param {string} text - the text to be measured
* @param {CanvasRenderingContext2D} [context] - reference to an active 2d context for canvas rendering
* @returns {number} the width of the given segment of inline text in CSS pixels.
*/
lineWidth(text, context) {
if (this.ancestor instanceof Text) {
return context.measureText(text).width;
} else { // it's a BitmapText
let characters = text.split("");
const charactersLength = characters.length;
let width = 0;
let lastGlyph = null;
for (let i = 0; i < charactersLength; i++) {
let ch = characters[i].charCodeAt(0);
let glyph = this.ancestor.fontData.glyphs[ch];
if (typeof glyph !== "undefined") {
let kerning = (lastGlyph && lastGlyph.kerning) ? lastGlyph.getKerning(ch) : 0;
width += (glyph.xadvance + kerning) * this.ancestor.fontScale.x;
lastGlyph = glyph;
}
}
return width;
}
}
/**
* measure the given text size in CSS pixels
* @param {string} text - the text to be measured
* @param {CanvasRenderingContext2D} [context] - reference to an active 2d context for canvas rendering
* @returns {TextMetrics} this
*/
measureText(text, context) {
let strings;
if (!Array.isArray(text)) {
strings = ("" + text).split("\n");
} else {
strings = text;
}
if (typeof context !== "undefined") {
// save the previous context
context.save();
// apply the style font
setContextStyle(context, this.ancestor);
}
// compute the bounding box size
this.width = this.height = 0;
for (let i = 0; i < strings.length; i++) {
this.width = Math.max(this.lineWidth(strings[i].trimEnd(), context), this.width);
this.height += this.lineHeight();
}
this.width = Math.ceil(this.width);
this.height = Math.ceil(this.height);
// compute the bounding box position
this.x = Math.floor((this.ancestor.textAlign === "right" ? this.ancestor.pos.x - this.width : (
this.ancestor.textAlign === "center" ? this.ancestor.pos.x - (this.width / 2) : this.ancestor.pos.x
)));
this.y = Math.floor((this.ancestor.textBaseline.search(/^(top|hanging)$/) === 0) ? this.ancestor.pos.y : (
this.ancestor.textBaseline === "middle" ? this.ancestor.pos.y - (this.lineHeight() / 2) : this.ancestor.pos.y - this.lineHeight()
));
if (typeof context !== "undefined") {
// restore the context
context.restore();
}
return this;
}
/**
* wrap the given text based on the given width
* @param {string|string[]} text - the text to be wrapped
* @param {number} width - maximum width of one segment of text in css pixel
* @param {CanvasRenderingContext2D} [context] - reference to an active 2d context for canvas rendering
* @returns {string[]} an array of string representing wrapped text
*/
wordWrap(text, width, context) {
let words;
let currentLine = "";
let output = [];
if (Array.isArray(text)) {
// join into a single string
text = text.join(" ");
}
// word splitting to be improved as it replaces \n by space if present
words = text.replace(/[\r\n]+/g, " ").split(" ");
if (typeof context !== "undefined") {
// save the previous context
context.save();
// apply the style font
setContextStyle(context, this.ancestor);
}
for (let i = 0; i < words.length; i++) {
let word = words[i];
let lineWidth = this.lineWidth(currentLine + word + " ", context);
if (lineWidth < width) {
// add the word to the current line
currentLine += word + " ";
} else {
output.push(currentLine + "\n");
currentLine = word + " ";
}
}
// last line
output.push(currentLine);
if (typeof context !== "undefined") {
// restore the context
context.restore();
}
return output;
}
}