UNPKG

@awayjs/scene

Version:
443 lines (442 loc) 17.3 kB
import { __extends } from "tslib"; import { AssetBase } from '@awayjs/core'; import { FontStyleName } from './FontStyleName'; import { TextFormatAlign } from './TextFormatAlign'; import { Font } from './Font'; import { TesselatedFontTable } from './TesselatedFontTable'; import { DefaultFontManager } from '../managers/DefaultFontManager'; import { FontLookUpMode } from './FontLookUpMode'; import { DeviceFontManager } from '../managers/DeviceFontManager'; /** * The TextFormat class represents character formatting information. Use the * TextFormat class to create specific text formatting for text fields. You * can apply text formatting to both static and dynamic text fields. The * properties of the TextFormat class apply to device and embedded fonts. * However, for embedded fonts, bold and italic text actually require specific * fonts. If you want to display bold or italic text with an embedded font, * you need to embed the bold and italic variations of that font. * * <p> You must use the constructor <code>new TextFormat()</code> to create a * TextFormat object before setting its properties. When you apply a * TextFormat object to a text field using the * <code>TextField.defaultTextFormat</code> property or the * <code>TextField.setTextFormat()</code> method, only its defined properties * are applied. Use the <code>TextField.defaultTextFormat</code> property to * apply formatting BEFORE you add text to the <code>TextField</code>, and the * <code>setTextFormat()</code> method to add formatting AFTER you add text to * the <code>TextField</code>. The TextFormat properties are <code>null</code> * by default because if you don't provide values for the properties, Flash * Player uses its own default formatting. The default formatting that Flash * Player uses for each property(if property's value is <code>null</code>) is * as follows:</p> * * <p>The default formatting for each property is also described in each * property description.</p> */ var PUBLIC_FIELDS = [ 'font', 'align', 'leftMargin', 'rightMargin', 'indent', 'size', 'color', 'bold', 'italic', 'underline', 'leading', 'letterSpacing' ]; var TextFormat = /** @class */ (function (_super) { __extends(TextFormat, _super); /** * Creates a TextFormat object with the specified properties. You can then * change the properties of the TextFormat object to change the formatting of * text fields. * * <p>Any parameter may be set to <code>null</code> to indicate that it is * not defined. All of the parameters are optional; any omitted parameters * are treated as <code>null</code>.</p> * * @param font The name of a font for text as a string. * @param size An integer that indicates the size in pixels. * @param color The color of text using this text format. A number * containing three 8-bit RGB components; for example, * 0xFF0000 is red, and 0x00FF00 is green. * @param bold A Boolean value that indicates whether the text is * boldface. * @param italic A Boolean value that indicates whether the text is * italicized. * @param underline A Boolean value that indicates whether the text is * underlined. * @param url The URL to which the text in this text format * hyperlinks. If <code>url</code> is an empty string, the * text does not have a hyperlink. * @param target The target window where the hyperlink is displayed. If * the target window is an empty string, the text is * displayed in the default target window * <code>_self</code>. If the <code>url</code> parameter * is set to an empty string or to the value * <code>null</code>, you can get or set this property, * but the property will have no effect. * @param align The alignment of the paragraph, as a TextFormatAlign * value. * @param leftMargin Indicates the left margin of the paragraph, in pixels. * @param rightMargin Indicates the right margin of the paragraph, in pixels. * @param indent An integer that indicates the indentation from the left * margin to the first character in the paragraph. * @param leading A number that indicates the amount of leading vertical * space between lines. */ function TextFormat(font, size, color, bold, italic, underline, url, link_target, align, leftMargin, rightMargin, indent, leading) { if (font === void 0) { font = '_sans'; } if (size === void 0) { size = null; } if (color === void 0) { color = null; } if (bold === void 0) { bold = null; } if (italic === void 0) { italic = null; } if (underline === void 0) { underline = null; } if (url === void 0) { url = null; } if (link_target === void 0) { link_target = null; } if (align === void 0) { align = null; } if (leftMargin === void 0) { leftMargin = null; } if (rightMargin === void 0) { rightMargin = null; } if (indent === void 0) { indent = null; } if (leading === void 0) { leading = null; } var _this = _super.call(this) || this; _this.fontLookUpMode = FontLookUpMode.DEFAULT; // flag marked that format is changed. _this._updateID = 0; _this._fontTableDirty = true; /** * Specifies custom tab stops as an array of non-negative integers. Each tab * stop is specified in pixels. If custom tab stops are not specified * (<code>null</code>), the default tab stop is 4(average character width). */ //todo: not used with in tesselated-font-table yet _this.tabStops = []; _this._size = size; _this._color = color; _this._bold = bold; _this._italic = italic; _this._underline = underline; _this.url = url; // not really used yet _this.link_target = link_target; // not really used yet _this._align = align; _this._leftMargin = leftMargin; _this._rightMargin = rightMargin; _this._indent = indent; _this._leading = leading; if (typeof font === 'string') { _this.font_name = font; } else { _this.font = font; } return _this; } Object.defineProperty(TextFormat.prototype, "updateID", { get: function () { return this._updateID; }, enumerable: false, configurable: true }); /** * return true if a certain property was set for this format. * * @param property_name */ TextFormat.prototype.hasPropertySet = function (property_name) { return this['_' + property_name] !== null; }; Object.defineProperty(TextFormat.prototype, "align", { get: function () { return this._align ? this._align : TextFormatAlign.LEFT; }, set: function (value) { if (this._align !== value) { this._updateID++; } this._align = value; }, enumerable: false, configurable: true }); Object.defineProperty(TextFormat.prototype, "blockIndent", { get: function () { return this._blockIndent ? this._blockIndent : 0; }, set: function (value) { if (this._blockIndent !== value) { this._updateID++; } this._blockIndent = value; }, enumerable: false, configurable: true }); Object.defineProperty(TextFormat.prototype, "leftMargin", { get: function () { return this._leftMargin ? this._leftMargin : 0; }, set: function (value) { if (value !== this._leftMargin) { this._updateID++; } this._leftMargin = value; }, enumerable: false, configurable: true }); Object.defineProperty(TextFormat.prototype, "rightMargin", { get: function () { return this._rightMargin ? this._rightMargin : 0; }, set: function (value) { if (value !== this._rightMargin) { this._updateID++; } this._rightMargin = value; }, enumerable: false, configurable: true }); Object.defineProperty(TextFormat.prototype, "indent", { get: function () { return this._indent ? this._indent : 0; }, set: function (value) { if (value !== this._indent) { this._updateID++; } this._indent = value; }, enumerable: false, configurable: true }); Object.defineProperty(TextFormat.prototype, "color", { get: function () { return (this._color !== null) ? this._color : 0x000000; }, set: function (value) { if (value !== this._color) { this._updateID++; } this._color = value; }, enumerable: false, configurable: true }); Object.defineProperty(TextFormat.prototype, "kerning", { get: function () { return this._kerning ? this._kerning : false; }, set: function (value) { if (value !== this._kerning) { this._updateID++; } this._kerning = value; }, enumerable: false, configurable: true }); Object.defineProperty(TextFormat.prototype, "leading", { get: function () { return this._leading ? this._leading : 0; }, set: function (value) { if (value !== this._leading) { this._updateID++; } this._leading = value; }, enumerable: false, configurable: true }); Object.defineProperty(TextFormat.prototype, "letterSpacing", { get: function () { return this._letterSpacing ? this._letterSpacing : 0; }, set: function (value) { if (value !== this._letterSpacing) { this._updateID++; } this._letterSpacing = value; }, enumerable: false, configurable: true }); Object.defineProperty(TextFormat.prototype, "size", { get: function () { return this._size ? this._size : 12; }, set: function (value) { if (value !== this._size) { this._updateID++; } this._size = value; }, enumerable: false, configurable: true }); Object.defineProperty(TextFormat.prototype, "bold", { get: function () { return this._bold ? this._bold : false; }, set: function (value) { if (value !== this._bold) { this._updateID++; } this._bold = value; this._fontTableDirty = true; }, enumerable: false, configurable: true }); Object.defineProperty(TextFormat.prototype, "italic", { get: function () { return this._italic ? this._italic : false; }, set: function (value) { if (value !== this._italic) { this._updateID++; } this._italic = value; this._fontTableDirty = true; }, enumerable: false, configurable: true }); Object.defineProperty(TextFormat.prototype, "underline", { get: function () { return this._underline ? this._underline : false; }, set: function (value) { if (value !== this._underline) { this._updateID++; } this._underline = value; }, enumerable: false, configurable: true }); Object.defineProperty(TextFormat.prototype, "font_table", { get: function () { if (!this._font_table || this._fontTableDirty) this.update_font_table(); return this._font_table; }, set: function (value) { // setting fonttable does not change bold / izalic // font_table is only ever set in parser or when using label-data // after using "bold" or "itaic" font_table should never be set directly this._font_table = value; this._fontTableDirty = false; }, enumerable: false, configurable: true }); Object.defineProperty(TextFormat.prototype, "font_name", { get: function () { var _a; return (_a = this.font) === null || _a === void 0 ? void 0 : _a.name; }, set: function (value) { var _a; if (((_a = this.font) === null || _a === void 0 ? void 0 : _a.name) == value) return; if (this.fontLookUpMode == FontLookUpMode.DEFAULT) { this._font = DefaultFontManager.getFont(value); } else if (this.fontLookUpMode == FontLookUpMode.DEVICE) { this._font = DeviceFontManager.getDeviceFont(value); } else if (this.fontLookUpMode == FontLookUpMode.EMBED_CFF) { this._font = DefaultFontManager.getFont_CFF(value); } this._fontTableDirty = true; this._updateID++; }, enumerable: false, configurable: true }); TextFormat.prototype.update_font_table = function () { this._fontTableDirty = false; var stylename = FontStyleName.STANDART; if (this._italic && !this._bold) stylename = FontStyleName.ITALIC; else if (!this._italic && this._bold) stylename = FontStyleName.BOLD; else if (this._italic && this._bold) stylename = FontStyleName.BOLDITALIC; if (this._font) { var font_table = this._font.get_font_table(stylename, TesselatedFontTable.assetType, null, true); // if we have a font-table, and we switch to invalid style, // we just keep the existing font-table if (this._font_table && !font_table) return; this._font_table = font_table ? font_table : Font.emptyFontTable; } }; Object.defineProperty(TextFormat.prototype, "font", { get: function () { return this._font; }, set: function (value) { if (this._font == value) return; this._font = value; this._fontTableDirty = true; this._updateID++; }, enumerable: false, configurable: true }); TextFormat.prototype.clone = function () { var clonedFormat = new TextFormat(this._font, this._size, this._color, this._bold, this._italic, this._underline, this.url, this.link_target, this._align, this._leftMargin, this._rightMargin, this._indent, this._leading); clonedFormat.font_table = this._font_table; //investigate why this needs to be copied return clonedFormat; }; TextFormat.prototype.equal = function (format) { for (var _i = 0, PUBLIC_FIELDS_1 = PUBLIC_FIELDS; _i < PUBLIC_FIELDS_1.length; _i++) { var field = PUBLIC_FIELDS_1[_i]; if (this[field] !== format[field]) { return false; } } return true; }; TextFormat.prototype.applyToFormat = function (format) { var change = false; for (var _i = 0, PUBLIC_FIELDS_2 = PUBLIC_FIELDS; _i < PUBLIC_FIELDS_2.length; _i++) { var field = PUBLIC_FIELDS_2[_i]; if (this['_' + field] !== null && format[field] !== this['_' + field]) { change = true; format[field] = this['_' + field]; } } return change; }; /** * Merge this format with other format by this rules: * if both formats have same property-valu8e, property value persists * if both formats have different property-value, property-value becomes null * Used by Textfield.getTextFormat * @param format */ TextFormat.prototype.mergeFormat = function (format) { for (var _i = 0, PUBLIC_FIELDS_3 = PUBLIC_FIELDS; _i < PUBLIC_FIELDS_3.length; _i++) { var field = PUBLIC_FIELDS_3[_i]; if (this['_' + field] !== format[field]) { this['_' + field] = null; } } }; Object.defineProperty(TextFormat.prototype, "assetType", { /** * */ get: function () { return TextFormat.assetType; }, enumerable: false, configurable: true }); TextFormat.assetType = '[asset TextFormat]'; return TextFormat; }(AssetBase)); export { TextFormat };