1pub const TEXT_MEASUREMENT_PROTOCOL_VERSION: u32 = 1;
5
6#[repr(i32)]
7#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
8pub enum TextMeasurementResultKind {
9 Metrics = 0,
10 Length = 1,
11 HorizontalExtents = 2,
12 WrappedWithRawWidth = 3,
13}
14
15impl TextMeasurementResultKind {
16 pub const ALL: [Self; 4] = [
17 Self::Metrics,
18 Self::Length,
19 Self::HorizontalExtents,
20 Self::WrappedWithRawWidth,
21 ];
22
23 pub const fn external_code(self) -> i32 {
24 self as i32
25 }
26
27 pub const fn external_name(self) -> &'static str {
28 TEXT_MEASUREMENT_RESULT_KIND_NAMES[self as usize]
29 }
30
31 pub const fn from_external_code(code: i32) -> Option<Self> {
32 if code >= 0 && (code as usize) < Self::ALL.len() {
33 Some(Self::ALL[code as usize])
34 } else {
35 None
36 }
37 }
38
39 pub fn from_external_name(name: &str) -> Option<Self> {
40 Self::ALL.into_iter().find(|kind| kind.external_name() == name)
41 }
42
43 pub const fn expected_for_operation(operation: TextMeasurementOperation) -> Self {
44 operation.required_result_kind()
45 }
46}
47
48const TEXT_MEASUREMENT_RESULT_KIND_NAMES: [&str; 4] = [
49 "metrics",
50 "length",
51 "horizontal-extents",
52 "wrapped-with-raw-width",
53];
54
55#[repr(i32)]
56#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
57pub enum TextMeasurementOperation {
58 Measure = 0,
59 ComputedLength = 1,
60 BBoxX = 2,
61 BBoxXWithAsciiOverhang = 3,
62 TitleBBoxX = 4,
63 SimpleBBoxWidth = 5,
64 RawBBoxWidth = 6,
65 TspanBBoxWidth = 7,
66 TspanBBoxHeight = 8,
67 WrapProbeBBoxWidth = 9,
68 SimpleBBoxHeight = 10,
69 Wrapped = 11,
70 WrappedWithRawWidth = 12,
71 BoundingClientRectWidth = 13,
72 CreateTextBBoxYOffset = 14,
73 MermaidCalculateTextDimensions = 15,
74 CanvasMeasureTextWidth = 16,
75 CreateTextMiddleBBoxYOffset = 17,
76 RawBBoxHeight = 18,
77 RawBBoxYOffset = 19,
78 RawMiddleBBoxYOffset = 20,
79 RawHangingBBoxYOffset = 21,
80}
81
82impl TextMeasurementOperation {
83 pub const ALL: [Self; 22] = [
84 Self::Measure,
85 Self::ComputedLength,
86 Self::BBoxX,
87 Self::BBoxXWithAsciiOverhang,
88 Self::TitleBBoxX,
89 Self::SimpleBBoxWidth,
90 Self::RawBBoxWidth,
91 Self::TspanBBoxWidth,
92 Self::TspanBBoxHeight,
93 Self::WrapProbeBBoxWidth,
94 Self::SimpleBBoxHeight,
95 Self::Wrapped,
96 Self::WrappedWithRawWidth,
97 Self::BoundingClientRectWidth,
98 Self::CreateTextBBoxYOffset,
99 Self::MermaidCalculateTextDimensions,
100 Self::CanvasMeasureTextWidth,
101 Self::CreateTextMiddleBBoxYOffset,
102 Self::RawBBoxHeight,
103 Self::RawBBoxYOffset,
104 Self::RawMiddleBBoxYOffset,
105 Self::RawHangingBBoxYOffset,
106 ];
107
108 pub(crate) const fn index(self) -> usize {
109 self as usize
110 }
111
112 pub const fn external_code(self) -> i32 {
113 self as i32
114 }
115
116 pub const fn external_name(self) -> &'static str {
117 TEXT_MEASUREMENT_OPERATION_DESCRIPTORS[self as usize].external_name
118 }
119
120 pub const fn required_result_kind(self) -> TextMeasurementResultKind {
121 TEXT_MEASUREMENT_OPERATION_DESCRIPTORS[self as usize].result_kind
122 }
123
124 pub const fn accepts_signed_length(self) -> bool {
125 TEXT_MEASUREMENT_OPERATION_DESCRIPTORS[self as usize].accepts_signed_length
126 }
127
128 pub const fn from_external_code(code: i32) -> Option<Self> {
129 if code >= 0 && (code as usize) < Self::ALL.len() {
130 Some(Self::ALL[code as usize])
131 } else {
132 None
133 }
134 }
135
136 pub fn from_external_name(name: &str) -> Option<Self> {
137 Self::ALL.into_iter().find(|operation| operation.external_name() == name)
138 }
139}
140
141struct TextMeasurementOperationDescriptor {
142 external_name: &'static str,
143 result_kind: TextMeasurementResultKind,
144 accepts_signed_length: bool,
145}
146
147const TEXT_MEASUREMENT_OPERATION_DESCRIPTORS: [TextMeasurementOperationDescriptor; 22] = [
148 TextMeasurementOperationDescriptor { external_name: "measure", result_kind: TextMeasurementResultKind::Metrics, accepts_signed_length: false },
149 TextMeasurementOperationDescriptor { external_name: "computed-length", result_kind: TextMeasurementResultKind::Length, accepts_signed_length: false },
150 TextMeasurementOperationDescriptor { external_name: "bbox-x", result_kind: TextMeasurementResultKind::HorizontalExtents, accepts_signed_length: false },
151 TextMeasurementOperationDescriptor { external_name: "bbox-x-with-ascii-overhang", result_kind: TextMeasurementResultKind::HorizontalExtents, accepts_signed_length: false },
152 TextMeasurementOperationDescriptor { external_name: "title-bbox-x", result_kind: TextMeasurementResultKind::HorizontalExtents, accepts_signed_length: false },
153 TextMeasurementOperationDescriptor { external_name: "simple-bbox-width", result_kind: TextMeasurementResultKind::Length, accepts_signed_length: false },
154 TextMeasurementOperationDescriptor { external_name: "raw-bbox-width", result_kind: TextMeasurementResultKind::Length, accepts_signed_length: false },
155 TextMeasurementOperationDescriptor { external_name: "tspan-bbox-width", result_kind: TextMeasurementResultKind::Length, accepts_signed_length: false },
156 TextMeasurementOperationDescriptor { external_name: "tspan-bbox-height", result_kind: TextMeasurementResultKind::Length, accepts_signed_length: false },
157 TextMeasurementOperationDescriptor { external_name: "wrap-probe-bbox-width", result_kind: TextMeasurementResultKind::Length, accepts_signed_length: false },
158 TextMeasurementOperationDescriptor { external_name: "simple-bbox-height", result_kind: TextMeasurementResultKind::Length, accepts_signed_length: false },
159 TextMeasurementOperationDescriptor { external_name: "wrapped", result_kind: TextMeasurementResultKind::Metrics, accepts_signed_length: false },
160 TextMeasurementOperationDescriptor { external_name: "wrapped-with-raw-width", result_kind: TextMeasurementResultKind::WrappedWithRawWidth, accepts_signed_length: false },
161 TextMeasurementOperationDescriptor { external_name: "bounding-client-rect-width", result_kind: TextMeasurementResultKind::Length, accepts_signed_length: false },
162 TextMeasurementOperationDescriptor { external_name: "create-text-bbox-y-offset", result_kind: TextMeasurementResultKind::Length, accepts_signed_length: true },
163 TextMeasurementOperationDescriptor { external_name: "mermaid-calculate-text-dimensions", result_kind: TextMeasurementResultKind::Metrics, accepts_signed_length: false },
164 TextMeasurementOperationDescriptor { external_name: "canvas-measure-text-width", result_kind: TextMeasurementResultKind::Length, accepts_signed_length: false },
165 TextMeasurementOperationDescriptor { external_name: "create-text-middle-bbox-y-offset", result_kind: TextMeasurementResultKind::Length, accepts_signed_length: true },
166 TextMeasurementOperationDescriptor { external_name: "raw-bbox-height", result_kind: TextMeasurementResultKind::Length, accepts_signed_length: false },
167 TextMeasurementOperationDescriptor { external_name: "raw-bbox-y-offset", result_kind: TextMeasurementResultKind::Length, accepts_signed_length: true },
168 TextMeasurementOperationDescriptor { external_name: "raw-middle-bbox-y-offset", result_kind: TextMeasurementResultKind::Length, accepts_signed_length: true },
169 TextMeasurementOperationDescriptor { external_name: "raw-hanging-bbox-y-offset", result_kind: TextMeasurementResultKind::Length, accepts_signed_length: true },
170];