UNPKG

@itwin/core-common

Version:

iTwin.js components common to frontend and backend

229 lines • 11.1 kB
/*--------------------------------------------------------------------------------------------- * 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 */ import { ColorDef } from "../ColorDef"; /** Set of predefined shapes that can be computed and drawn around the margins of a [[TextBlock]] * @beta */ export const textAnnotationFrameShapes = ["none", "line", "rectangle", "circle", "equilateralTriangle", "diamond", "square", "pentagon", "hexagon", "octagon", "capsule", "roundedRectangle"]; ; function deepFreeze(obj) { if (obj === null || typeof obj !== "object" || Object.isFrozen(obj)) return; Object.getOwnPropertyNames(obj).forEach((prop) => { const value = obj[prop]; if (value && typeof value === "object") { deepFreeze(value); } }); Object.freeze(obj); } /** A description of the formatting to be applied to a [[TextBlockComponent]]. * Named instances of these settings can be stored as [AnnotationTextStyle]($backend)s in an iModel. * @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 an iModel, 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 computed 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; /** Properties describing appearance of leaders in a [[TextAnnotation]]. * Used when producing geometry for [[TextAnnotation]]. */ leader; /** The size (in meters) used to calculate the tab stops in a run. * These are equally spaced from the left edge of the TextBlock. Default is 4 meters. */ tabInterval; /** The frame settings of the [[TextAnnotation]]. */ frame; /** A fully-populated JSON representation of the default settings. A real `fontName` must be provided before use. */ 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, leader: { color: "inherit", wantElbow: false, elbowLength: 1.0, terminatorHeightFactor: 1.0, terminatorWidthFactor: 1.0, }, tabInterval: 4, frame: { shape: "none", fill: "none", border: ColorDef.black.toJSON(), borderWeight: 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; const leader = { color: props.leader?.color ?? defaults.leader.color, wantElbow: props.leader?.wantElbow ?? defaults.leader.wantElbow, elbowLength: props.leader?.elbowLength ?? defaults.leader.elbowLength, terminatorHeightFactor: props.leader?.terminatorHeightFactor ?? defaults.leader.terminatorHeightFactor, terminatorWidthFactor: props.leader?.terminatorWidthFactor ?? defaults.leader.terminatorWidthFactor, }; this.leader = Object.freeze(leader); this.tabInterval = props.tabInterval ?? defaults.tabInterval; const frame = { shape: props.frame?.shape ?? defaults.frame.shape, fill: props.frame?.fill ?? defaults.frame.fill, border: props.frame?.border ?? defaults.frame.border, borderWeight: props.frame?.borderWeight ?? defaults.frame.borderWeight, }; // Cast to indicate to TypeScript that the frame properties are all defined this.frame = Object.freeze(frame); } /** Create a copy of these settings, modified according to the properties defined by `alteredProps`. */ clone(alteredProps) { return alteredProps ? new TextStyleSettings(alteredProps, this) : this; } /** Creates a deep copy of the `TextStyleSettingsProps`. */ static cloneProps(props) { const copy = { ...props }; if (props.leader) { copy.leader = { ...props.leader }; } if (props.frame) { copy.frame = { ...props.frame }; } return copy; } /** Create settings from their JSON representation. */ static fromJSON(props) { return props ? new TextStyleSettings(props) : TextStyleSettings.defaults; } toJSON() { return { ...this }; } /** Compare two [[TextLeaderStyleProps]] for equality. * @param other The other leader style properties to compare against. * @returns true if the two leader styles are equal, false otherwise. */ leaderEquals(other) { return this.leader.color === other.color && this.leader.wantElbow === other.wantElbow && this.leader.elbowLength === other.elbowLength && this.leader.terminatorHeightFactor === other.terminatorHeightFactor && this.leader.terminatorWidthFactor === other.terminatorWidthFactor; } frameEquals(other) { return this.frame?.shape === other.shape && this.frame?.fill === other.fill && this.frame?.border === other.border && this.frame?.borderWeight === other.borderWeight; } 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 && this.tabInterval === other.tabInterval && this.leaderEquals(other.leader) && this.frameEquals(other.frame); } /** * Returns a list of validation errors for this instance. * * A TextStyleSettings object may contain values that are invalid in all contexts. * If this method returns any error strings, using the settings will likely result in rendering failures or runtime exceptions. * * This method only checks for universally invalid values. Additional domain-specific validation may be required depending on the context in which these settings are used. * * @returns An array of error strings describing the invalid values, or an empty array if the settings are valid. */ getValidationErrors() { const errorMessages = []; if (this.fontName.trim() === "") { errorMessages.push("fontName must be provided"); } if (this.lineHeight <= 0) { errorMessages.push("lineHeight must be greater than 0"); } if (this.stackedFractionScale <= 0) { errorMessages.push("stackedFractionScale must be greater than 0"); } return errorMessages; } } deepFreeze(TextStyleSettings.defaultProps); deepFreeze(TextStyleSettings.defaults); //# sourceMappingURL=TextStyle.js.map