| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
use serde::Deserialize; |
| 7 |
|
| 8 |
use crate::Error; |
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
#[derive(Debug, Clone, Copy)] |
| 26 |
pub struct Dim { |
| 27 |
pub band: f64, |
| 28 |
pub weight: f64, |
| 29 |
} |
| 30 |
|
| 31 |
impl Dim { |
| 32 |
pub fn resolve(self, band_extent: f64, base_stroke: f64) -> f64 { |
| 33 |
band_extent * self.band + base_stroke * self.weight |
| 34 |
} |
| 35 |
} |
| 36 |
|
| 37 |
impl<'de> Deserialize<'de> for Dim { |
| 38 |
fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> { |
| 39 |
#[derive(Deserialize)] |
| 40 |
#[serde(untagged)] |
| 41 |
enum Raw { |
| 42 |
Flat(f64), |
| 43 |
Pair { |
| 44 |
band: f64, |
| 45 |
#[serde(default)] |
| 46 |
weight: f64, |
| 47 |
}, |
| 48 |
} |
| 49 |
Ok(match Raw::deserialize(deserializer)? { |
| 50 |
Raw::Flat(band) => Dim { band, weight: 0.0 }, |
| 51 |
Raw::Pair { band, weight } => Dim { band, weight }, |
| 52 |
}) |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
#[derive(Debug, Deserialize)] |
| 57 |
pub struct Manifest { |
| 58 |
pub set: SetMeta, |
| 59 |
#[serde(default, rename = "glyph")] |
| 60 |
pub glyphs: Vec<GlyphSpec>, |
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
#[serde(default, rename = "generated")] |
| 68 |
pub generated: Vec<Generated>, |
| 69 |
} |
| 70 |
|
| 71 |
#[derive(Debug, Deserialize)] |
| 72 |
pub struct Generated { |
| 73 |
pub block: crate::cells::Block, |
| 74 |
} |
| 75 |
|
| 76 |
#[derive(Debug, Deserialize)] |
| 77 |
pub struct SetMeta { |
| 78 |
pub version: u32, |
| 79 |
pub name: String, |
| 80 |
} |
| 81 |
|
| 82 |
#[derive(Debug, Deserialize)] |
| 83 |
pub struct GlyphSpec { |
| 84 |
|
| 85 |
pub codepoint: u32, |
| 86 |
|
| 87 |
pub name: String, |
| 88 |
|
| 89 |
pub role: String, |
| 90 |
|
| 91 |
#[serde(default)] |
| 92 |
pub source: Option<String>, |
| 93 |
|
| 94 |
|
| 95 |
#[serde(default)] |
| 96 |
pub purpose: Purpose, |
| 97 |
#[serde(flatten)] |
| 98 |
pub shape: Shape, |
| 99 |
} |
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Deserialize)] |
| 122 |
#[serde(rename_all = "kebab-case")] |
| 123 |
pub enum Purpose { |
| 124 |
|
| 125 |
#[default] |
| 126 |
House, |
| 127 |
|
| 128 |
Coverage, |
| 129 |
} |
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
#[derive(Debug, Deserialize)] |
| 134 |
#[serde(tag = "shape", rename_all = "kebab-case")] |
| 135 |
pub enum Shape { |
| 136 |
|
| 137 |
Triangle { |
| 138 |
direction: Direction, |
| 139 |
|
| 140 |
span: Dim, |
| 141 |
|
| 142 |
|
| 143 |
depth: f64, |
| 144 |
#[serde(default)] |
| 145 |
anchor: Anchor, |
| 146 |
}, |
| 147 |
|
| 148 |
Cross { |
| 149 |
width: Dim, |
| 150 |
height: Dim, |
| 151 |
|
| 152 |
stroke: f64, |
| 153 |
}, |
| 154 |
|
| 155 |
OpenBox { |
| 156 |
width: Dim, |
| 157 |
height: Dim, |
| 158 |
|
| 159 |
bottom: f64, |
| 160 |
|
| 161 |
stroke: f64, |
| 162 |
}, |
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
Arrow { |
| 172 |
direction: Direction, |
| 173 |
|
| 174 |
#[serde(default)] |
| 175 |
both_ends: bool, |
| 176 |
|
| 177 |
length: Dim, |
| 178 |
|
| 179 |
head_span: Dim, |
| 180 |
|
| 181 |
head_depth: f64, |
| 182 |
|
| 183 |
stroke: f64, |
| 184 |
}, |
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
#[serde(skip)] |
| 191 |
Cell(crate::cells::Cell), |
| 192 |
|
| 193 |
ReturnArrow { |
| 194 |
|
| 195 |
top: f64, |
| 196 |
|
| 197 |
shaft: f64, |
| 198 |
|
| 199 |
head_span: Dim, |
| 200 |
|
| 201 |
head_depth: Dim, |
| 202 |
|
| 203 |
stroke: f64, |
| 204 |
}, |
| 205 |
} |
| 206 |
|
| 207 |
impl Shape { |
| 208 |
pub fn kind(&self) -> &'static str { |
| 209 |
match self { |
| 210 |
Shape::Arrow { .. } => "arrow", |
| 211 |
Shape::Cell(cell) => cell.kind(), |
| 212 |
Shape::Triangle { .. } => "triangle", |
| 213 |
Shape::Cross { .. } => "cross", |
| 214 |
Shape::OpenBox { .. } => "open-box", |
| 215 |
Shape::ReturnArrow { .. } => "return-arrow", |
| 216 |
} |
| 217 |
} |
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
pub fn is_cell_furniture(&self) -> bool { |
| 228 |
matches!(self, Shape::Cell(_)) |
| 229 |
} |
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
pub fn weight_response(&self) -> WeightResponse { |
| 238 |
match self { |
| 239 |
Shape::Cell(cell) => cell.weight_response(), |
| 240 |
Shape::Triangle { .. } => WeightResponse::Grows, |
| 241 |
Shape::Arrow { .. } |
| 242 |
| Shape::Cross { .. } |
| 243 |
| Shape::OpenBox { .. } |
| 244 |
| Shape::ReturnArrow { .. } => WeightResponse::Thickens, |
| 245 |
} |
| 246 |
} |
| 247 |
} |
| 248 |
|
| 249 |
|
| 250 |
#[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 251 |
pub enum WeightResponse { |
| 252 |
|
| 253 |
Grows, |
| 254 |
|
| 255 |
Thickens, |
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
Holds, |
| 263 |
} |
| 264 |
|
| 265 |
#[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize)] |
| 266 |
#[serde(rename_all = "kebab-case")] |
| 267 |
pub enum Direction { |
| 268 |
Up, |
| 269 |
Down, |
| 270 |
Left, |
| 271 |
Right, |
| 272 |
} |
| 273 |
|
| 274 |
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Deserialize)] |
| 275 |
#[serde(rename_all = "kebab-case")] |
| 276 |
pub enum Anchor { |
| 277 |
|
| 278 |
#[default] |
| 279 |
BandCenter, |
| 280 |
|
| 281 |
XHeight, |
| 282 |
} |
| 283 |
|
| 284 |
impl Manifest { |
| 285 |
pub fn parse(source: &str) -> Result<Self, Error> { |
| 286 |
let mut manifest: Manifest = |
| 287 |
toml::from_str(source).map_err(|e| Error::Manifest(e.to_string()))?; |
| 288 |
for generated in &manifest.generated { |
| 289 |
manifest.glyphs.extend(crate::cells::specs(generated.block)); |
| 290 |
} |
| 291 |
manifest.glyphs.sort_by_key(|glyph| glyph.codepoint); |
| 292 |
let mut seen: Vec<u32> = Vec::new(); |
| 293 |
for glyph in &manifest.glyphs { |
| 294 |
if char::from_u32(glyph.codepoint).is_none() { |
| 295 |
return Err(Error::Manifest(format!( |
| 296 |
"{} is not a Unicode scalar value", |
| 297 |
format_codepoint(glyph.codepoint) |
| 298 |
))); |
| 299 |
} |
| 300 |
if seen.contains(&glyph.codepoint) { |
| 301 |
return Err(Error::Manifest(format!( |
| 302 |
"{} appears twice in the set", |
| 303 |
format_codepoint(glyph.codepoint) |
| 304 |
))); |
| 305 |
} |
| 306 |
seen.push(glyph.codepoint); |
| 307 |
} |
| 308 |
Ok(manifest) |
| 309 |
} |
| 310 |
} |
| 311 |
|
| 312 |
pub fn format_codepoint(codepoint: u32) -> String { |
| 313 |
format!("U+{codepoint:04X}") |
| 314 |
} |
| 315 |
|
| 316 |
#[cfg(test)] |
| 317 |
mod tests { |
| 318 |
use super::*; |
| 319 |
|
| 320 |
#[test] |
| 321 |
fn the_shipped_set_parses() { |
| 322 |
let manifest = Manifest::parse(crate::HOUSE_SET).expect("the shipped set parses"); |
| 323 |
let authored = manifest |
| 324 |
.glyphs |
| 325 |
.iter() |
| 326 |
.filter(|glyph| !glyph.shape.is_cell_furniture()) |
| 327 |
.count(); |
| 328 |
assert_eq!( |
| 329 |
authored, 16, |
| 330 |
"the seven marks, five arrows and four scroll markers" |
| 331 |
); |
| 332 |
assert_eq!( |
| 333 |
manifest.glyphs.len(), |
| 334 |
176, |
| 335 |
"plus both generated blocks, 160 of them" |
| 336 |
); |
| 337 |
} |
| 338 |
|
| 339 |
#[test] |
| 340 |
fn a_repeated_codepoint_is_rejected() { |
| 341 |
let source = r#" |
| 342 |
[set] |
| 343 |
version = 1 |
| 344 |
name = "t" |
| 345 |
[[glyph]] |
| 346 |
codepoint = 0x25B2 |
| 347 |
name = "a" |
| 348 |
role = "r" |
| 349 |
shape = "triangle" |
| 350 |
direction = "up" |
| 351 |
span = 1.0 |
| 352 |
depth = 1.0 |
| 353 |
[[glyph]] |
| 354 |
codepoint = 0x25B2 |
| 355 |
name = "b" |
| 356 |
role = "r" |
| 357 |
shape = "triangle" |
| 358 |
direction = "down" |
| 359 |
span = 1.0 |
| 360 |
depth = 1.0 |
| 361 |
"#; |
| 362 |
let err = Manifest::parse(source).unwrap_err(); |
| 363 |
assert!(err.to_string().contains("twice"), "{err}"); |
| 364 |
} |
| 365 |
|
| 366 |
#[test] |
| 367 |
fn solid_marks_grow_and_stroked_marks_thicken() { |
| 368 |
let manifest = Manifest::parse(crate::HOUSE_SET).unwrap(); |
| 369 |
for glyph in manifest |
| 370 |
.glyphs |
| 371 |
.iter() |
| 372 |
.filter(|glyph| !glyph.shape.is_cell_furniture()) |
| 373 |
{ |
| 374 |
let expected = match glyph.shape.kind() { |
| 375 |
"triangle" => WeightResponse::Grows, |
| 376 |
_ => WeightResponse::Thickens, |
| 377 |
}; |
| 378 |
assert_eq!( |
| 379 |
glyph.shape.weight_response(), |
| 380 |
expected, |
| 381 |
"{} ({})", |
| 382 |
glyph.name, |
| 383 |
glyph.shape.kind() |
| 384 |
); |
| 385 |
} |
| 386 |
} |
| 387 |
|
| 388 |
#[test] |
| 389 |
fn a_dimension_reads_as_a_bare_number_or_as_a_pair() { |
| 390 |
let flat: Dim = toml::from_str::<toml::Value>("v = 0.5").unwrap()["v"] |
| 391 |
.clone() |
| 392 |
.try_into() |
| 393 |
.unwrap(); |
| 394 |
assert!((flat.band - 0.5).abs() < 1e-9 && flat.weight == 0.0); |
| 395 |
let pair: Dim = toml::from_str::<toml::Value>("v = { band = 0.5, weight = 0.6 }").unwrap() |
| 396 |
["v"] |
| 397 |
.clone() |
| 398 |
.try_into() |
| 399 |
.unwrap(); |
| 400 |
assert!((pair.band - 0.5).abs() < 1e-9 && (pair.weight - 0.6).abs() < 1e-9); |
| 401 |
|
| 402 |
assert!((pair.resolve(476.0, 84.0) - (476.0 * 0.5 + 84.0 * 0.6)).abs() < 1e-9); |
| 403 |
} |
| 404 |
} |
| 405 |
|