- 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
- 169
- 170
- 171
- 172
- 173
- 174
- 175
- 176
- 177
- 178
- 179
- 180
- 181
- 182
- 183
- 184
- 185
- 186
- 187
- 188
- 189
- 190
- 191
- 192
- 193
- 194
- 195
- 196
- 197
- 198
- 199
- 200
- 201
- 202
- 203
- 204
- 205
- 206
- 207
- 208
- 209
- 210
- 211
- 212
- 213
- 214
- 215
- 216
- 217
- 218
- 219
- 220
- 221
- 222
- 223
- 224
- 225
- 226
- 227
- 228
- 229
- 230
- 231
- 232
- 233
- 234
- 235
- 236
- 237
- 238
- 239
- 240
- 241
- 242
- 243
- 244
- 245
- 246
- 247
- 248
- 249
- 250
- 251
- 252
- 253
- 254
- 255
- 256
- 257
- 258
- 259
- 260
- 261
- 262
- 263
- 264
- 265
- 266
- 267
- 268
- 269
- 270
- 271
- 272
- 273
- 274
- 275
- 276
- 277
- 278
- 279
- 280
- 281
- 282
- 283
- 284
- 285
- 286
- 287
- 288
- 289
- 290
- 291
- 292
- 293
- 294
- 295
- 296
- 297
- 298
- 299
- 300
- 301
- 302
- 303
- 304
- 305
- 306
- 307
- 308
- 309
- 310
- 311
- 312
- 313
- 314
- 315
- 316
- 317
- 318
- 319
- 320
- 321
- 322
- 323
- 324
- 325
- 326
- 327
- 328
- 329
- 330
- 331
- 332
- 333
- 334
- 335
- 336
- 337
- 338
- 339
- 340
- 341
- 342
- 343
- 344
- 345
- 346
- 347
- 348
- 349
- 350
- 351
- 352
- 353
- 354
- 355
- 356
- 357
- 358
- 359
- 360
- 361
- 362
- 363
- 364
- 365
- 366
- 367
- 368
- 369
- 370
- 371
- 372
- 373
- 374
- 375
- 376
- 377
- 378
- 379
- 380
- 381
- 382
- 383
- 384
- 385
- 386
- 387
- 388
- 389
- 390
- 391
- 392
- 393
- 394
- 395
- 396
- 397
- 398
- 399
- 400
- 401
- 402
- 403
- 404
- 405
- 406
- 407
- 408
- 409
- 410
- 411
- 412
- 413
- 414
- 415
- 416
import Color from "../../math/color.js";
import { renderer as globalRenderer } from "../../video/video.js";
import pool from "../../system/pooling.js";
import Renderable from "../renderable.js";
import { nextPowerOfTwo } from "../../math/math.js";
import setContextStyle from "./textstyle.js";
import TextMetrics from "./textmetrics.js";
/*
* ASCII Table
* http://www.asciitable.com/
* [ !"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz]
*
* -> first char " " 32d (0x20);
*/
const runits = ["ex", "em", "pt", "px"];
const toPX = [12, 24, 0.75, 1];
/**
* @classdesc
* a generic system font object.
* @augments Renderable
*/
export default class Text extends Renderable {
/**
* @param {number} x - position of the text object
* @param {number} y - position of the text object
* @param {object} settings - the text configuration
* @param {string} settings.font - a CSS family font name
* @param {number|string} settings.size - size, or size + suffix (px, em, pt)
* @param {Color|string} [settings.fillStyle="#000000"] - a CSS color value
* @param {Color|string} [settings.strokeStyle="#000000"] - a CSS color value
* @param {number} [settings.lineWidth=0] - line width, in pixels, when drawing stroke
* @param {string} [settings.textAlign="left"] - horizontal text alignment
* @param {string} [settings.textBaseline="top"] - the text baseline
* @param {number} [settings.lineHeight=1.0] - line spacing height
* @param {Vector2d} [settings.anchorPoint={x:0.0, y:0.0}] - anchor point to draw the text at
* @param {number} [settings.wordWrapWidth] - the maximum length in CSS pixel for a single segment of text
* @param {(string|string[])} [settings.text=""] - a string, or an array of strings
* @example
* let font = new me.Text(0, 0, {font: "Arial", size: 8, fillStyle: this.color});
*/
constructor(x, y, settings) {
// call the parent constructor
super(x, y, settings.width || 0, settings.height || 0);
/**
* defines the color used to draw the font.
* @type {Color}
* @default black
*/
this.fillStyle = pool.pull("Color", 0, 0, 0);
/**
* defines the color used to draw the font stroke.<br>
* @type {Color}
* @default black
*/
this.strokeStyle = pool.pull("Color", 0, 0, 0);
/**
* sets the current line width, in pixels, when drawing stroke
* @type {number}
* @default 0
*/
this.lineWidth = 0;
/**
* Set the default text alignment (or justification),<br>
* possible values are "left", "right", and "center".<br>
* @type {string}
* @default "left"
*/
this.textAlign = "left";
/**
* Set the text baseline (e.g. the Y-coordinate for the draw operation), <br>
* possible values are "top", "hanging, "middle, "alphabetic, "ideographic, "bottom"<br>
* @type {string}
* @default "top"
*/
this.textBaseline = "top";
/**
* Set the line spacing height (when displaying multi-line strings). <br>
* Current font height will be multiplied with this value to set the line height.
* @type {number}
* @default 1.0
*/
this.lineHeight = 1.0;
/**
* the maximum length in CSS pixel for a single segment of text.
* (use -1 to disable word wrapping)
* @type {number}
* @default -1
*/
this.wordWrapWidth = -1;
/**
* the font size (in px)
* @type {number}
* @default 10
*/
this.fontSize = 10;
/**
* the text to be displayed
* @private
*/
this._text = [];
// initalize the object based on the given settings
this.onResetEvent(x, y, settings);
}
/** @ignore */
onResetEvent(x, y, settings) {
if (typeof this.fillStyle === "undefined") {
this.fillStyle = pool.pull("Color", 0, 0, 0);
}
if (typeof this.strokeStyle === "undefined") {
this.strokeStyle = pool.pull("Color", 0, 0, 0);
}
if (typeof settings.fillStyle !== "undefined") {
if (settings.fillStyle instanceof Color) {
this.fillStyle.copy(settings.fillStyle);
} else {
// string (#RGB, #ARGB, #RRGGBB, #AARRGGBB)
this.fillStyle.parseCSS(settings.fillStyle);
}
}
if (typeof settings.strokeStyle !== "undefined") {
if (settings.strokeStyle instanceof Color) {
this.strokeStyle.copy(settings.strokeStyle);
} else {
// string (#RGB, #ARGB, #RRGGBB, #AARRGGBB)
this.strokeStyle.parseCSS(settings.strokeStyle);
}
}
this.lineWidth = settings.lineWidth || 0;
this.textAlign = settings.textAlign || "left";
this.textBaseline = settings.textBaseline || "top";
this.lineHeight = settings.lineHeight || 1.0;
this.wordWrapWidth = settings.wordWrapWidth || -1;
this.fontSize = 10;
// anchor point
if (typeof settings.anchorPoint !== "undefined") {
this.anchorPoint.setV(settings.anchorPoint);
} else {
this.anchorPoint.set(0, 0);
}
// if floating was specified through settings
if (typeof settings.floating !== "undefined") {
this.floating = !!settings.floating;
}
// font name and type
this.setFont(settings.font, settings.size);
// aditional
if (settings.bold === true) {
this.bold();
}
if (settings.italic === true) {
this.italic();
}
// the canvas Texture used to render this text
// XXX: offscreenCanvas is currently disabled for text rendering due to issue in WebGL mode
this.canvasTexture = pool.pull("CanvasRenderTarget", 2, 2, { offscreenCanvas: false });
// instance to text metrics functions
this.metrics = new TextMetrics(this);
// set the text
this.setText(settings.text);
}
/**
* make the font bold
* @returns {Text} this object for chaining
*/
bold() {
this.font = "bold " + this.font;
this.isDirty = true;
return this;
}
/**
* make the font italic
* @returns {Text} this object for chaining
*/
italic() {
this.font = "italic " + this.font;
this.isDirty = true;
return this;
}
/**
* set the font family and size
* @param {string} font - a CSS font name
* @param {number|string} [size=10] - size in px, or size + suffix (px, em, pt)
* @returns {Text} this object for chaining
* @example
* font.setFont("Arial", 20);
* font.setFont("Arial", "1.5em");
*/
setFont(font, size = 10) {
// font name and type
let font_names = font.split(",").map((value) => {
value = value.trim();
return (
!/(^".*"$)|(^'.*'$)/.test(value)
) ? "\"" + value + "\"" : value;
});
// font size
if (typeof size === "number") {
this.fontSize = size;
size += "px";
} else /* string */ {
// extract the units and convert if necessary
let CSSval = size.match(/([-+]?[\d.]*)(.*)/);
this.fontSize = parseFloat(CSSval[1]);
if (CSSval[2]) {
this.fontSize *= toPX[runits.indexOf(CSSval[2])];
} else {
// no unit define, assume px
size += "px";
}
}
this.height = this.fontSize;
this.font = size + " " + font_names.join(",");
this.isDirty = true;
return this;
}
/**
* change the text to be displayed
* @param {number|string|string[]} value - a string, or an array of strings
* @returns {Text} this object for chaining
*/
setText(value = "") {
let bounds = this.getBounds();
// set the next text
if (this._text.toString() !== value.toString()) {
if (!Array.isArray(value)) {
this._text = ("" + value).split("\n");
} else {
this._text = value;
}
}
// word wrap if necessary
if (this._text.length > 0 && this.wordWrapWidth > 0) {
this._text = this.metrics.wordWrap(this._text, this.wordWrapWidth, this.canvasTexture.context);
}
// calculcate the text size and update the bounds accordingly
bounds.addBounds(this.metrics.measureText(this._text, this.canvasTexture.context), true);
// update the offScreenCanvas texture if required
let width = Math.ceil(this.metrics.width),
height = Math.ceil(this.metrics.height);
if (globalRenderer.WebGLVersion === 1) {
// round size to next Pow2
width = nextPowerOfTwo(this.metrics.width);
height = nextPowerOfTwo(this.metrics.height);
}
// invalidate the texture
this.canvasTexture.invalidate(globalRenderer);
// resize the cache canvas if necessary
if (this.canvasTexture.width < width || this.canvasTexture.height < height) {
this.canvasTexture.resize(width, height);
}
this.canvasTexture.clear();
this._drawFont(this.canvasTexture.context, this._text, this.pos.x - this.metrics.x, this.pos.y - this.metrics.y);
this.isDirty = true;
return this;
}
/**
* measure the given text size in pixels
* @param {CanvasRenderer|WebGLRenderer} renderer - reference to the active renderer
* @param {string} [text] - the text to be measured
* @returns {TextMetrics} a TextMetrics object defining the dimensions of the given piece of text
*/
measureText(renderer, text = this._text) {
return this.metrics.measureText(text, this.canvasTexture.context);
}
/**
* draw a text at the specified coord
* @param {CanvasRenderer|WebGLRenderer} renderer - Reference to the destination renderer instance
* @param {string} [text]
* @param {number} [x]
* @param {number} [y]
*/
draw(renderer, text, x = this.pos.x, y = this.pos.y) {
// "hacky patch" for backward compatibilty
if (typeof this.ancestor === "undefined") {
// update position if changed
if (this.pos.x !== x || this.pos.y !== y) {
this.pos.x = x;
this.pos.y = y;
this.isDirty = true;
}
// update text cache
this.setText(text);
// save the previous context
renderer.save();
// apply the defined alpha value
renderer.setGlobalAlpha(renderer.globalAlpha() * this.getOpacity());
}
// adjust x,y position based on the bounding box
x = this.metrics.x;
y = this.metrics.y;
// clamp to pixel grid if required
if (renderer.settings.subPixel === false) {
x = ~~x;
y = ~~y;
}
// draw the text
renderer.drawImage(this.canvasTexture.canvas, x, y);
// for backward compatibilty
if (typeof this.ancestor === "undefined") {
// restore previous context
renderer.restore();
}
}
/**
* draw a stroke text at the specified coord, as defined by the `lineWidth` and `fillStroke` properties.
* @deprecated since 15.0.0
* @param {CanvasRenderer|WebGLRenderer} renderer - Reference to the destination renderer instance
* @param {string} text
* @param {number} x
* @param {number} y
*/
drawStroke(renderer, text, x, y) {
this.draw(renderer, text, x, y);
}
/**
* @ignore
*/
_drawFont(context, text, x, y) {
setContextStyle(context, this);
for (let i = 0; i < text.length; i++) {
const string = text[i].trimEnd();
// draw the string
if (this.fillStyle.alpha > 0) {
context.fillText(string, x, y);
}
// stroke the text
if (this.lineWidth > 0 && this.strokeStyle.alpha > 0) {
context.strokeText(string, x, y);
}
// add leading space
y += this.metrics.lineHeight();
}
return this.metrics;
}
/**
* Destroy function
* @ignore
*/
destroy() {
if (typeof globalRenderer.gl !== "undefined") {
// make sure the right compositor is active
globalRenderer.setCompositor("quad");
globalRenderer.currentCompositor.deleteTexture2D(globalRenderer.currentCompositor.getTexture2D(this.glTextureUnit));
this.glTextureUnit = undefined;
}
globalRenderer.cache.delete(this.canvasTexture.canvas);
pool.push(this.canvasTexture);
this.canvasTexture = undefined;
pool.push(this.fillStyle);
pool.push(this.strokeStyle);
this.fillStyle = this.strokeStyle = undefined;
this.metrics = undefined;
this._text.length = 0;
super.destroy();
}
}