1mod fit;
10pub mod json;
11pub mod layout;
12mod text;
13pub mod text_animation;
14
15use std::fmt;
16use std::path::{Path, PathBuf};
17use std::sync::Arc;
18
19use alpha2_text::{TextFont, TextRasterError};
20
21pub use fit::FittedSize;
22pub use layout::{
23 LAYOUT_WRAP_SLACK, LayoutBounds, NodeBounds, NodeRects, ResolvedLayout, fit_texts,
24 layout_texts, measure_layout, resolve, shape_texts,
25};
26pub use text::{
27 MAX_FONT_PIXELS, MAX_PADDING_PIXELS, MAX_STROKE_PIXELS, ShadowPixels, TextAnimation,
28 TextBounds, TextDraw, animated_text_key, fit_text, font_pixels, layout_text_key, measure_text,
29 rgba8, shadow_pixels, shaped_key, stroke_pixels, text_draw, text_fill, text_key, text_request,
30 text_request_at,
31};
32
33#[derive(Copy, Clone, Debug, Eq, Hash, PartialEq)]
37pub struct PictureSize {
38 width: u32,
39 height: u32,
40}
41
42#[derive(Copy, Clone, Debug, Eq, PartialEq)]
44#[non_exhaustive]
45pub enum PictureSizeError {
46 Empty,
48 TooLarge,
50}
51
52impl fmt::Display for PictureSizeError {
53 fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
54 formatter.write_str(match self {
55 Self::Empty => "picture dimensions must be non-zero",
56 Self::TooLarge => "picture dimensions exceed the largest frame",
57 })
58 }
59}
60
61impl std::error::Error for PictureSizeError {}
62
63impl PictureSize {
64 pub const fn new(width: u32, height: u32) -> Result<Self, PictureSizeError> {
69 if width == 0 || height == 0 {
70 Err(PictureSizeError::Empty)
71 } else if width > alpha2_media::MAX_FRAME_DIMENSION
72 || height > alpha2_media::MAX_FRAME_DIMENSION
73 || (width as u64) * (height as u64) > alpha2_media::MAX_FRAME_PIXELS
74 {
75 Err(PictureSizeError::TooLarge)
76 } else {
77 Ok(Self { width, height })
78 }
79 }
80
81 #[must_use]
83 pub const fn width(self) -> u32 {
84 self.width
85 }
86
87 #[must_use]
89 pub const fn height(self) -> u32 {
90 self.height
91 }
92
93 #[must_use]
95 #[allow(clippy::cast_precision_loss)]
96 pub fn unit(self) -> f32 {
97 self.width.min(self.height) as f32
98 }
99}
100
101#[derive(Copy, Clone, Debug, PartialEq)]
103pub struct CanvasRect {
104 pub left: f32,
106 pub top: f32,
108 pub right: f32,
110 pub bottom: f32,
112}
113
114#[derive(Copy, Clone, Debug, PartialEq)]
117pub struct Baselines {
118 pub first: f32,
120 pub last: f32,
122}
123
124#[derive(Debug)]
126#[non_exhaustive]
127pub enum FontFileError {
128 Read {
130 path: PathBuf,
132 error: std::io::Error,
134 },
135 Invalid {
137 path: PathBuf,
139 face_index: u32,
141 error: TextRasterError,
143 },
144}
145
146impl fmt::Display for FontFileError {
147 fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
148 match self {
149 Self::Read { path, error } => write!(formatter, "font {}: {error}", path.display()),
150 Self::Invalid {
151 path,
152 face_index,
153 error,
154 } => write!(
155 formatter,
156 "font {} face {face_index}: {error}",
157 path.display()
158 ),
159 }
160 }
161}
162
163impl std::error::Error for FontFileError {
164 fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
165 match self {
166 Self::Read { error, .. } => Some(error),
167 Self::Invalid { error, .. } => Some(error),
168 }
169 }
170}
171
172#[derive(Clone, Debug)]
174pub struct FontFiles {
175 pub faces: Vec<TextFont>,
177 pub bytes: Vec<Arc<[u8]>>,
179}
180
181pub fn fonts_from_files(paths: &[impl AsRef<Path>]) -> Result<FontFiles, FontFileError> {
191 let mut fonts = Vec::with_capacity(paths.len());
192 let mut files = Vec::with_capacity(paths.len());
193 for path in paths {
194 let path = path.as_ref();
195 let bytes: Arc<[u8]> = std::fs::read(path)
196 .map_err(|error| FontFileError::Read {
197 path: path.to_owned(),
198 error,
199 })?
200 .into();
201 let family = path
202 .file_stem()
203 .map_or_else(String::new, |stem| stem.to_string_lossy().into_owned());
204 for face_index in 0..alpha2_text::face_count(&bytes) {
205 alpha2_text::check_font(&bytes, face_index).map_err(|error| {
206 FontFileError::Invalid {
207 path: path.to_owned(),
208 face_index,
209 error,
210 }
211 })?;
212 fonts.push(TextFont {
213 family: family.clone(),
214 bytes: Arc::clone(&bytes),
215 face_index,
216 });
217 }
218 files.push(bytes);
219 }
220 Ok(FontFiles {
221 faces: fonts,
222 bytes: files,
223 })
224}
225
226#[cfg(test)]
227mod tests;