@itwin/core-common
Version:
iTwin.js components common to frontend and backend
147 lines • 7.3 kB
JavaScript
/*---------------------------------------------------------------------------------------------
* Copyright (c) Bentley Systems, Incorporated. All rights reserved.
* See LICENSE.md in the project root for license terms and full copyright notice.
*--------------------------------------------------------------------------------------------*/
/** @packageDocumentation
* @module Annotation
*/
/** A description of the formatting to be applied to a [[TextBlockComponent]].
* Named instances of these settings can be stored as [[TextStyle]]s in a [Workspace]($backend).
* @note This is an immutable type. Use [[clone]] to create a modified copy.
* @see [[TextStyleSettingsProps]] for documentation of each of the settings.
* @beta
*/
export class TextStyleSettings {
/** The color of the text. */
color;
/** The name of a font stored in a [Workspace]($backend), used to draw the contents of a [[TextRun]].
*/
fontName;
/** The height each line of text, in meters. Many other settings use the line height as the basis for computing their own values.
* For example, the height and offset from baseline of a subscript [[TextRun]] are compuated as lineHeight * [[subScriptScale]] and
* lineHeight * [[subScriptOffsetFactor]], respectively.
*/
lineHeight;
/** Multiplier used to compute the vertical distance between two lines of text.
* The distance is computed in meters as lineSpacingFactor * [[lineHeight]].
*/
lineSpacingFactor;
/** Specifies whether the content of a [[TextRun]] should be rendered **bold**. */
isBold;
/** Specifies whether the content of a [[TextRun]] should be rendered in *italics*. */
isItalic;
/** Specifies whether the content of a [[TextRun]] should be underlined. */
isUnderlined;
/** Multiplier used to compute the height of both the numerator and denominator of a [[FractionRun]].
* The height is computed in meters as stackedFractionScale * [[lineHeight]].
*/
stackedFractionScale;
/** Specifies how to separate the numerator and denominator of a [[FractionRun]]. */
stackedFractionType;
/** Multiplier used to compute the vertical offset from the baseline for a subscript [[TextRun]].
* The offset is computed in meters as subScriptOffsetFactor * [[lineHeight]].
*/
subScriptOffsetFactor;
/** Multiplier used to compute the height of a subscript [[TextRun]].
* The height is computed as subScriptScale * [[lineHeight]].
*/
subScriptScale;
/** Multiplier used to compute the vertical offset from the baseline for a super [[TextRun]].
* The offset is computed in meters as superScriptOffsetFactor * [[lineHeight]].
*/
superScriptOffsetFactor;
/** Multiplier used to compute the height of a superscript [[TextRun]].
* The height is computed as superScriptScale * [[lineHeight]].
*/
superScriptScale;
/** Multiplier used to compute the width of each glyph, relative to [[lineHeight]]. */
widthFactor;
/** A fully-populated JSON representation of the default settings. */
static defaultProps = {
color: "subcategory",
fontName: "",
lineHeight: 1,
lineSpacingFactor: 0.5,
isBold: false,
isItalic: false,
isUnderlined: false,
stackedFractionScale: 0.7,
stackedFractionType: "horizontal",
subScriptOffsetFactor: -0.15,
subScriptScale: 2 / 3,
superScriptOffsetFactor: 0.5,
superScriptScale: 2 / 3,
widthFactor: 1,
};
/** Settings initialized to all default values. */
static defaults = new TextStyleSettings({});
constructor(props, defaults) {
if (!defaults) {
defaults = TextStyleSettings.defaultProps;
}
this.color = props.color ?? defaults.color;
this.fontName = props.fontName ?? defaults.fontName;
this.lineHeight = props.lineHeight ?? defaults.lineHeight;
this.lineSpacingFactor = props.lineSpacingFactor ?? defaults.lineSpacingFactor;
this.isBold = props.isBold ?? defaults.isBold;
this.isItalic = props.isItalic ?? defaults.isItalic;
this.isUnderlined = props.isUnderlined ?? defaults.isUnderlined;
this.stackedFractionScale = props.stackedFractionScale ?? defaults.stackedFractionScale;
this.stackedFractionType = props.stackedFractionType ?? defaults.stackedFractionType;
this.subScriptOffsetFactor = props.subScriptOffsetFactor ?? defaults.subScriptOffsetFactor;
this.subScriptScale = props.subScriptScale ?? defaults.subScriptScale;
this.superScriptOffsetFactor = props.superScriptOffsetFactor ?? defaults.superScriptOffsetFactor;
this.superScriptScale = props.superScriptScale ?? defaults.superScriptScale;
this.widthFactor = props.widthFactor ?? defaults.widthFactor;
}
/** Create a copy of these settings, modified according to the properties defined by `alteredProps`. */
clone(alteredProps) {
return alteredProps ? new TextStyleSettings(alteredProps, this) : this;
}
/** Create settings from their JSON representation. */
static fromJSON(props) {
return props ? new TextStyleSettings(props) : TextStyleSettings.defaults;
}
toJSON() {
return { ...this };
}
equals(other) {
return this.color === other.color && this.fontName === other.fontName
&& this.lineHeight === other.lineHeight && this.lineSpacingFactor === other.lineSpacingFactor && this.widthFactor === other.widthFactor
&& this.isBold === other.isBold && this.isItalic === other.isItalic && this.isUnderlined === other.isUnderlined
&& this.stackedFractionType === other.stackedFractionType && this.stackedFractionScale === other.stackedFractionScale
&& this.subScriptOffsetFactor === other.subScriptOffsetFactor && this.subScriptScale === other.subScriptScale
&& this.superScriptOffsetFactor === other.superScriptOffsetFactor && this.superScriptScale === other.superScriptScale;
}
}
Object.freeze(TextStyleSettings.defaultProps);
Object.freeze(TextStyleSettings.defaults);
/** A named, immutable [[TextStyleSettings]] stored in a [Workspace]($backend).
* @see [[TextBlockComponent.styleName]] to define the text style for a component of a [[TextBlock]].
* @note This is an immutable type. Use [[clone]] to create a modified copy.
* @beta
*/
export class TextStyle {
name;
settings;
constructor(name, settings) {
this.name = name;
this.settings = settings;
}
/** Create a style from its JSON representation. */
static fromJSON(json) {
return TextStyle.create(json.name, TextStyleSettings.fromJSON(json.settings));
}
/** Create a new style. */
static create(name, settings) {
return new TextStyle(name, settings);
}
/** Create a copy of this style with the same name, and settings modified according to the properties defined by `alteredSettings`. */
clone(alteredSettings) {
return TextStyle.create(this.name, this.settings.clone(alteredSettings));
}
equals(other) {
return this.name === other.name && this.settings.equals(other.settings);
}
}
//# sourceMappingURL=TextStyle.js.map