/
TextString.ts
122 lines (114 loc) · 4.19 KB
/
TextString.ts
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
/*---------------------------------------------------------------------------------------------
* Copyright (c) Bentley Systems, Incorporated. All rights reserved.
* See LICENSE.md in the project root for license terms and full copyright notice.
*--------------------------------------------------------------------------------------------*/
/** @packageDocumentation
* @module Geometry
*/
import { Point2d, Point3d, Range2d, Transform, Vector3d, XYZProps, YawPitchRollAngles, YawPitchRollProps } from "@itwin/core-geometry";
import { FontId } from "../Fonts";
/**
* Optional cached text layout information used to support legacy proxy graphics.
* @beta
*/
export interface TextStringGlyphData {
glyphIds: number[];
glyphOrigins: Point2d[];
range: Range2d;
}
/** Properties for a TextString class.
* @see [[GeometryStreamEntryProps]]
* @public
* @extensions
*/
export interface TextStringProps {
/** text string */
text: string;
/** FontId for the TextString. FontIds are mapped to font names and types via the FontMap */
font: FontId;
/* text height */
height: number;
/* width / height ratio. Default is 1.0 */
widthFactor?: number;
/** bold text. Default is false */
bold?: boolean;
/** italic text. Default is false */
italic?: boolean;
/** underline text. Default is false */
underline?: boolean;
/** Optional position relative to element's placement. Default is 0,0,0 */
origin?: XYZProps;
/** Optional rotation relative to element's placement. Default is identity matrix */
rotation?: YawPitchRollProps;
}
/** A single line of text, all with the same font, styles (underline, bold, italic), and size.
* This class also holds the origin and direction for the text.
* A paragraph is composed of one or more instances of TextStrings.
* @public
*/
export class TextString {
/** Text string */
public text: string;
/** FontId for this TextString. FontIds are mapped to font names and types via the FontMap */
public font: FontId;
/* text height, in meters */
public height: number;
/* width / height ratio. Default is 1.0 */
public widthFactor?: number;
/** bold text. Default is false */
public bold?: boolean;
/** italic text. Default is false */
public italic?: boolean;
/** underline text. Default is false */
public underline?: boolean;
/** position relative to element's placement */
public readonly origin: Point3d;
/** Rotation relative to element's placement */
public readonly rotation: YawPitchRollAngles;
public get width() { return this.height * (this.widthFactor ? this.widthFactor : 1.0); }
public constructor(props: TextStringProps) {
this.text = props.text;
this.font = props.font;
this.height = props.height;
this.widthFactor = props.widthFactor;
this.bold = props.bold;
this.italic = props.italic;
this.underline = props.underline;
this.origin = Point3d.fromJSON(props.origin);
this.rotation = YawPitchRollAngles.fromJSON(props.rotation);
}
public toJSON(): TextStringProps {
const val: any = {};
val.text = this.text;
val.font = this.font;
val.height = this.height;
val.widthFactor = this.widthFactor;
val.bold = this.bold;
val.italic = this.italic;
val.underline = this.underline;
if (!this.origin.isAlmostZero)
val.origin = this.origin;
if (!this.rotation.isIdentity())
val.rotation = this.rotation;
return val;
}
public transformInPlace(transform: Transform): boolean {
const newOrigin = transform.multiplyPoint3d(this.origin, this.origin);
const newTransform = this.rotation.toMatrix3d().multiplyMatrixTransform(transform);
const scales = new Vector3d();
if (!newTransform.matrix.normalizeColumnsInPlace(scales))
return false;
const newRotation = YawPitchRollAngles.createFromMatrix3d(newTransform.matrix);
if (undefined === newRotation)
return false;
const newHeight = this.height * scales.y;
const newWidth = this.width * scales.x;
if (newHeight < 1.0e-10 || newWidth < 1.0e-10)
return false;
this.origin.setFrom(newOrigin);
this.rotation.setFrom(newRotation);
this.height = newHeight;
this.widthFactor = (newHeight === newWidth ? undefined : (newWidth / newHeight));
return true;
}
}