1use crate::contract::{ShapingInput, TextDirection};
4use crate::{
5 TextContourError, TextContourErrorKind, TextContourOutput, TextContourRequest,
6 shape_text_contours_a0, shape_text_contours_hinted_a0,
7};
8pub use crate::{TextHorizontalAlignment, TextVerticalAlignment};
9
10pub const TEXT_HEIGHT_FUDGE_FACTOR: f64 = 1.17;
13
14#[derive(Clone, Copy, Debug)]
16pub struct TextLayoutRequest<'a> {
17 pub shaping: &'a ShapingInput,
18 pub em_x: f64,
20 pub em_y: f64,
22 pub alignment_height: f64,
27 pub position_x: f64,
28 pub position_y: f64,
29 pub angle_degrees: f64,
30 pub mirrored: bool,
31 pub horizontal_alignment: TextHorizontalAlignment,
32 pub vertical_alignment: TextVerticalAlignment,
33 pub max_error: f64,
34}
35
36pub fn layout_single_line_text_a0(
44 font_bytes: &[u8],
45 request: TextLayoutRequest<'_>,
46) -> Result<TextContourOutput, TextContourError> {
47 layout_single_line_text_with_outline_mode(font_bytes, request, false)
48}
49
50pub fn layout_single_line_text_hinted_a0(
52 font_bytes: &[u8],
53 request: TextLayoutRequest<'_>,
54) -> Result<TextContourOutput, TextContourError> {
55 layout_single_line_text_with_outline_mode(font_bytes, request, true)
56}
57
58fn layout_single_line_text_with_outline_mode(
59 font_bytes: &[u8],
60 request: TextLayoutRequest<'_>,
61 hinted: bool,
62) -> Result<TextContourOutput, TextContourError> {
63 let transform = validated_transform(request)?;
64 let contour_request = TextContourRequest {
65 shaping: request.shaping,
66 em_x: request.em_x,
67 em_y: request.em_y,
68 origin_x: 0.0,
69 origin_y: 0.0,
70 max_error: request.max_error,
71 fake_bold: false,
72 fake_italic: false,
73 };
74 let mut output = if hinted {
75 shape_text_contours_hinted_a0(font_bytes, contour_request)?
76 } else {
77 shape_text_contours_a0(font_bytes, contour_request)?
78 };
79 let translation = aligned_translation(request, output.advance_x)?;
80 transform_output(&mut output, translation, transform)?;
81 Ok(output)
82}
83
84#[derive(Clone, Copy)]
85#[doc(hidden)]
86pub struct LayoutTransform {
87 origin_x: f64,
88 origin_y: f64,
89 sin_angle: f64,
90 cos_angle: f64,
91 mirrored: bool,
92 rotated: bool,
93}
94
95#[doc(hidden)]
96pub fn validated_transform(
97 request: TextLayoutRequest<'_>,
98) -> Result<LayoutTransform, TextContourError> {
99 if matches!(
100 request.shaping.direction,
101 TextDirection::TopToBottom | TextDirection::BottomToTop
102 ) {
103 return Err(invalid_layout(
104 "$.shaping.direction",
105 "vertical shaping directions are not supported by single-line horizontal layout",
106 ));
107 }
108 if [
109 request.angle_degrees,
110 request.position_x,
111 request.position_y,
112 ]
113 .into_iter()
114 .any(|value| !value.is_finite())
115 {
116 return Err(invalid_layout(
117 "$.transform",
118 "text angle and authored position must be finite",
119 ));
120 }
121 let radians = request.angle_degrees.to_radians();
122 let (sin_angle, cos_angle) = radians.sin_cos();
123 if [radians, sin_angle, cos_angle]
124 .into_iter()
125 .any(|value| !value.is_finite())
126 {
127 return Err(invalid_layout(
128 "$.angle_degrees",
129 "text angle is outside the finite transform range",
130 ));
131 }
132 Ok(LayoutTransform {
133 origin_x: request.position_x,
134 origin_y: request.position_y,
135 sin_angle,
136 cos_angle,
137 mirrored: request.mirrored,
138 rotated: request.angle_degrees != 0.0,
139 })
140}
141
142fn aligned_translation(
143 request: TextLayoutRequest<'_>,
144 line_width: f64,
145) -> Result<(f64, f64), TextContourError> {
146 let horizontal_offset = match request.horizontal_alignment {
147 TextHorizontalAlignment::Left => 0.0,
148 TextHorizontalAlignment::Center => -line_width / 2.0,
149 TextHorizontalAlignment::Right => -line_width,
150 };
151 let height = request.alignment_height * TEXT_HEIGHT_FUDGE_FACTOR;
152 let vertical_offset = match request.vertical_alignment {
153 TextVerticalAlignment::Top => request.alignment_height,
154 TextVerticalAlignment::Center => request.alignment_height - height / 2.0,
155 TextVerticalAlignment::Bottom => request.alignment_height - height,
156 };
157 let translation_x = request.position_x + horizontal_offset;
158 let translation_y = request.position_y + vertical_offset;
159 if [translation_x, translation_y]
160 .into_iter()
161 .any(|value| !value.is_finite())
162 {
163 return Err(invalid_layout(
164 "$.position",
165 "aligned text position is not finite",
166 ));
167 }
168 Ok((translation_x, translation_y))
169}
170
171fn transform_output(
172 output: &mut TextContourOutput,
173 translation: (f64, f64),
174 transform: LayoutTransform,
175) -> Result<(), TextContourError> {
176 transform_contours(&mut output.contours, translation, transform)
177}
178
179#[doc(hidden)]
180pub fn transform_contours(
181 contours: &mut [crate::TextContour],
182 translation: (f64, f64),
183 transform: LayoutTransform,
184) -> Result<(), TextContourError> {
185 for contour in contours {
186 for point in &mut contour.points {
187 let (x, y) =
188 transform_point(point.x + translation.0, point.y + translation.1, transform)?;
189 point.x = x;
190 point.y = y;
191 }
192 }
193 Ok(())
194}
195
196#[doc(hidden)]
198pub fn transform_point(
199 x: f64,
200 y: f64,
201 transform: LayoutTransform,
202) -> Result<(f64, f64), TextContourError> {
203 let mut x = x;
204 let mut y = y;
205 if transform.mirrored {
206 x = transform.origin_x - (x - transform.origin_x);
207 }
208 if transform.rotated {
209 let delta_x = x - transform.origin_x;
210 let delta_y = y - transform.origin_y;
211 x = transform.origin_x + delta_x * transform.cos_angle + delta_y * transform.sin_angle;
212 y = transform.origin_y + delta_y * transform.cos_angle - delta_x * transform.sin_angle;
213 }
214 if [x, y].into_iter().any(|value| !value.is_finite()) {
215 return Err(invalid_layout(
216 "$.contours",
217 "transformed text coordinate is not finite",
218 ));
219 }
220 Ok((x, y))
221}
222
223fn invalid_layout(path: &'static str, message: &'static str) -> TextContourError {
224 TextContourError {
225 kind: TextContourErrorKind::InvalidInput,
226 path: path.to_owned(),
227 message,
228 }
229}