/** * a bitmap font object. * * The font descriptor uses the AngelCode BMFont format and may be supplied in * either flavour — the **text** (`.fnt`) or the **XML** form — the * serialisation is auto-detected, so packs that ship an `.xml` descriptor load * as-is, with no conversion step. Load the descriptor as a `binary` asset and * its page image as an `image` asset, then reference both by the same name. * @category Text */ export default class BitmapText 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|Image} settings.font - a font name to identify the corresponding source image * @param {string} [settings.fontData=settings.font] - the bitmap font data corresponding name, or the bitmap font data itself (AngelCode BMFont, `.fnt` text or `.xml`) * @param {number} [settings.size=1.0] - a scaling RATIO applied to the font's authored size, not a pixel size: `2` draws it at double. ({@link Text} takes pixels here; this one does not.) * @param {Color|string} [settings.fillStyle] - a CSS color value used to tint the glyphs, see {@link Renderable#tint} * @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 {string|Vector2d|{x:number,y:number}} [settings.anchorPoint={x:0.0, y:0.0}] - anchor point to draw the text at. Also accepts the named presets `"center"`, `"top"`, `"bottom"`, `"left"`, `"right"`, `"top-left"`, `"top-right"`, `"bottom-left"`, `"bottom-right"`. * @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 * // Load the BMFont descriptor as a "binary" asset and its page as an "image". * // Both the text (.fnt) and XML (.xml) BMFont flavours are accepted and * // auto-detected, so an .xml descriptor can be used directly: * loader.preload([ * // text (.fnt) BMFont * { name: "arial", type: "binary", src: "data/font/arial.fnt" }, * { name: "arial", type: "image", src: "data/font/arial.png" }, * // XML BMFont (exported by many bitmap-font tools) — loaded as-is * { name: "pixel", type: "binary", src: "data/font/pixel.xml" }, * { name: "pixel", type: "image", src: "data/font/pixel.png" }, * ]) * // Then create an instance of your bitmap font: * let myFont = new BitmapText(x, y, { font: "arial", text: "Hello" }); * // add it to the world container * app.world.addChild(myFont); */ constructor(x: number, y: number, settings: { font: string | (new (width?: number, height?: number) => HTMLImageElement); fontData?: string | undefined; size?: number | undefined; fillStyle?: string | Color | undefined; textAlign?: string | undefined; textBaseline?: string | undefined; lineHeight?: number | undefined; anchorPoint?: string | Vector2d | { x: number; y: number; }; wordWrapWidth?: number | undefined; text?: string | string[] | undefined; }); /** * Set the default text alignment (or justification),
* possible values are "left", "right", and "center". * @public * @type {string} * @default "left" */ public textAlign: string; /** * Set the text baseline (e.g. the Y-coordinate for the draw operation),
* possible values are "top", "hanging", "middle", "alphabetic", "ideographic", "bottom"
* @public * @type {string} * @default "top" */ public textBaseline: string; /** * Set the line spacing height (when displaying multi-line strings).
* Current font height will be multiplied with this value to set the line height. * @public * @type {number} * @default 1.0 */ public lineHeight: number; /** * the maximum length in CSS pixel for a single segment of text. * (use -1 to disable word wrapping) * @public * @type {number} * @default -1 */ public wordWrapWidth: number; /** * scaled font size * @private */ private fontScale; /** * font image * @private */ private fontImage; /** * font data * @private */ private fontData; public set fillStyle(value: Color); /** * defines the color used to tint the bitmap text. * * This is {@link Renderable#tint} under another name, so the same rule * applies: white — `(255, 255, 255)` — is the absence of a tint, and any * other colour tints away from there. A page image authored in white * therefore keeps every colour available to it. * @public * @type {Color} * @see Renderable#tint * @example * // tint at construction... * const score = new BitmapText(8, 8, { * font: "arial", * text: "1000", * fillStyle: "#ffd700", // gold * }); * app.world.addChild(score); * * // ...or at any point after it, from a CSS string or a Color * score.fillStyle = "#ff4040"; // flash red on damage * score.fillStyle = new Color(255, 255, 255); // back to untinted */ public get fillStyle(): Color; metrics: TextMetrics; /** * change the font settings * @param {string} textAlign - ("left", "center", "right") * @param {number} [scale] - a scaling ratio, applied through {@link BitmapText#resize} when given * @returns {BitmapText} this object for chaining */ set(textAlign: string, scale?: number): BitmapText; /** * change the text to be displayed * @param {number|string|string[]} value - a string, or an array of strings * @returns {BitmapText} this object for chaining */ setText(value?: number | string | string[]): BitmapText; public set visibleCharacters(value: number); /** * the number of characters to display (use -1 to show all). * Useful for typewriter effects combined with Tween. * @public * @type {number} * @default -1 * @see BitmapText#visibleRatio * @example * // show only the first 5 characters * bitmapText.visibleCharacters = 5; * // typewriter effect * bitmapText.visibleCharacters = 0; * new Tween(bitmapText).to({ visibleRatio: 1.0 }, { duration: 2000 }).start(); */ public get visibleCharacters(): number; public set visibleRatio(value: number); /** * the ratio of visible characters (0.0 to 1.0). * Setting this automatically updates {@link visibleCharacters}. * @public * @type {number} */ public get visibleRatio(): number; /** * change the font display size * @param {number} scale - a ratio against the font's authored size, NOT a * pixel size: `1` is the page image at its native scale, `2` is double * @returns {BitmapText} this object for chaining * @example * // a bitmap font is pixel art — whole-number ratios stay crisp, and * // fractional ones resample the page image * title.resize(3); // three times its authored size * title.set("center", 2); // align and rescale in one call */ resize(scale: number): BitmapText; /** * measure the given text size in pixels * @param {string} [text] * @returns {TextMetrics} a TextMetrics object with two properties: `width` and `height`, defining the output dimensions * @example * // size a panel around a label, at the label's CURRENT scale * const size = label.measureText(); * panel.resize(size.width + 16, size.height + 16); */ measureText(text?: string): TextMetrics; /** * draw the bitmap font * @param {CanvasRenderer|WebGLRenderer} renderer - Reference to the destination renderer instance */ draw(renderer: CanvasRenderer | WebGLRenderer): void; } import Renderable from "../renderable.js"; import { Color } from "../../math/color.ts"; import TextMetrics from "./textmetrics.js"; //# sourceMappingURL=bitmaptext.d.ts.map