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}
78
79impl TextMeasurementOperation {
80 pub const ALL: [Self; 19] = [
81 Self::Measure,
82 Self::ComputedLength,
83 Self::BBoxX,
84 Self::BBoxXWithAsciiOverhang,
85 Self::TitleBBoxX,
86 Self::SimpleBBoxWidth,
87 Self::RawBBoxWidth,
88 Self::TspanBBoxWidth,
89 Self::TspanBBoxHeight,
90 Self::WrapProbeBBoxWidth,
91 Self::SimpleBBoxHeight,
92 Self::Wrapped,
93 Self::WrappedWithRawWidth,
94 Self::BoundingClientRectWidth,
95 Self::CreateTextBBoxYOffset,
96 Self::MermaidCalculateTextDimensions,
97 Self::CanvasMeasureTextWidth,
98 Self::CreateTextMiddleBBoxYOffset,
99 Self::RawBBoxHeight,
100 ];
101
102 pub(crate) const fn index(self) -> usize {
103 self as usize
104 }
105
106 pub const fn external_code(self) -> i32 {
107 self as i32
108 }
109
110 pub const fn external_name(self) -> &'static str {
111 TEXT_MEASUREMENT_OPERATION_DESCRIPTORS[self as usize].external_name
112 }
113
114 pub const fn required_result_kind(self) -> TextMeasurementResultKind {
115 TEXT_MEASUREMENT_OPERATION_DESCRIPTORS[self as usize].result_kind
116 }
117
118 pub const fn accepts_signed_length(self) -> bool {
119 TEXT_MEASUREMENT_OPERATION_DESCRIPTORS[self as usize].accepts_signed_length
120 }
121
122 pub const fn from_external_code(code: i32) -> Option<Self> {
123 if code >= 0 && (code as usize) < Self::ALL.len() {
124 Some(Self::ALL[code as usize])
125 } else {
126 None
127 }
128 }
129
130 pub fn from_external_name(name: &str) -> Option<Self> {
131 Self::ALL.into_iter().find(|operation| operation.external_name() == name)
132 }
133}
134
135struct TextMeasurementOperationDescriptor {
136 external_name: &'static str,
137 result_kind: TextMeasurementResultKind,
138 accepts_signed_length: bool,
139}
140
141const TEXT_MEASUREMENT_OPERATION_DESCRIPTORS: [TextMeasurementOperationDescriptor; 19] = [
142 TextMeasurementOperationDescriptor { external_name: "measure", result_kind: TextMeasurementResultKind::Metrics, accepts_signed_length: false },
143 TextMeasurementOperationDescriptor { external_name: "computed-length", result_kind: TextMeasurementResultKind::Length, accepts_signed_length: false },
144 TextMeasurementOperationDescriptor { external_name: "bbox-x", result_kind: TextMeasurementResultKind::HorizontalExtents, accepts_signed_length: false },
145 TextMeasurementOperationDescriptor { external_name: "bbox-x-with-ascii-overhang", result_kind: TextMeasurementResultKind::HorizontalExtents, accepts_signed_length: false },
146 TextMeasurementOperationDescriptor { external_name: "title-bbox-x", result_kind: TextMeasurementResultKind::HorizontalExtents, accepts_signed_length: false },
147 TextMeasurementOperationDescriptor { external_name: "simple-bbox-width", result_kind: TextMeasurementResultKind::Length, accepts_signed_length: false },
148 TextMeasurementOperationDescriptor { external_name: "raw-bbox-width", result_kind: TextMeasurementResultKind::Length, accepts_signed_length: false },
149 TextMeasurementOperationDescriptor { external_name: "tspan-bbox-width", result_kind: TextMeasurementResultKind::Length, accepts_signed_length: false },
150 TextMeasurementOperationDescriptor { external_name: "tspan-bbox-height", result_kind: TextMeasurementResultKind::Length, accepts_signed_length: false },
151 TextMeasurementOperationDescriptor { external_name: "wrap-probe-bbox-width", result_kind: TextMeasurementResultKind::Length, accepts_signed_length: false },
152 TextMeasurementOperationDescriptor { external_name: "simple-bbox-height", result_kind: TextMeasurementResultKind::Length, accepts_signed_length: false },
153 TextMeasurementOperationDescriptor { external_name: "wrapped", result_kind: TextMeasurementResultKind::Metrics, accepts_signed_length: false },
154 TextMeasurementOperationDescriptor { external_name: "wrapped-with-raw-width", result_kind: TextMeasurementResultKind::WrappedWithRawWidth, accepts_signed_length: false },
155 TextMeasurementOperationDescriptor { external_name: "bounding-client-rect-width", result_kind: TextMeasurementResultKind::Length, accepts_signed_length: false },
156 TextMeasurementOperationDescriptor { external_name: "create-text-bbox-y-offset", result_kind: TextMeasurementResultKind::Length, accepts_signed_length: true },
157 TextMeasurementOperationDescriptor { external_name: "mermaid-calculate-text-dimensions", result_kind: TextMeasurementResultKind::Metrics, accepts_signed_length: false },
158 TextMeasurementOperationDescriptor { external_name: "canvas-measure-text-width", result_kind: TextMeasurementResultKind::Length, accepts_signed_length: false },
159 TextMeasurementOperationDescriptor { external_name: "create-text-middle-bbox-y-offset", result_kind: TextMeasurementResultKind::Length, accepts_signed_length: true },
160 TextMeasurementOperationDescriptor { external_name: "raw-bbox-height", result_kind: TextMeasurementResultKind::Length, accepts_signed_length: false },
161];