kicad_monkey_contracts/
font_text_contract.rs1use crate::generated::outline_vector::{CoordinateComparisonPolicy, OutlineVectorA0};
2use crate::generated::shaping_record::ShapingRecordA0;
3use crate::{ValidationError, validation_error};
4use cad_text_realization::{
5 OutlineCommand, ShapedGlyph, validate_font_sha256, validate_font_variations,
6 validate_shaping_input,
7};
8use std::collections::HashSet;
9
10pub fn validate_shaping_record_contract(record: &ShapingRecordA0) -> Result<(), ValidationError> {
12 if record.schema != "kicad_monkey.shaping_record.a0"
13 || record.type_ != "kicad_monkey.shaping_record"
14 || record.version != "a0"
15 || record.comparison.mode != "exact"
16 {
17 return Err(error(
18 "unsupported_contract",
19 "$",
20 "unsupported shaping record identity or index policy",
21 ));
22 }
23 validate_shaping_input(&record.input).map_err(rebase_input_error)?;
24 validate_glyph_clusters(&record.input.text, &record.glyphs)
25}
26
27fn rebase_input_error(error: cad_text_realization::ValidationError) -> ValidationError {
28 let mut error = ValidationError::from(error);
29 error.path = if error.path == "$" {
30 "$.input".to_owned()
31 } else {
32 format!("$.input{}", &error.path[1..])
33 };
34 error
35}
36
37pub fn validate_outline_vector_contract(record: &OutlineVectorA0) -> Result<(), ValidationError> {
39 if record.schema != "kicad_monkey.outline_vector.a0"
40 || record.type_ != "kicad_monkey.outline_vector"
41 || record.version != "a0"
42 || record.coordinate_format != "font_design_units_f64"
43 {
44 return Err(error(
45 "unsupported_contract",
46 "$",
47 "unsupported outline vector identity or coordinate format",
48 ));
49 }
50 validate_outline_comparison(&record.coordinate_comparison)?;
51 validate_font_sha256(&record.font_sha256, "$.font_sha256")?;
52 validate_font_variations(&record.variations, "$.variations")?;
53 validate_outline_commands(&record.commands)
54}
55
56fn validate_outline_comparison(
57 comparison: &CoordinateComparisonPolicy,
58) -> Result<(), ValidationError> {
59 match comparison {
60 CoordinateComparisonPolicy::ExactComparisonPolicy(value) if value.mode == "exact" => {}
61 CoordinateComparisonPolicy::AbsoluteToleranceComparisonPolicy(value)
62 if value.mode == "absolute_tolerance" => {}
63 _ => {
64 return Err(error(
65 "invalid_comparison",
66 "$.coordinate_comparison",
67 "outline comparison applies only through a registered coordinate mode",
68 ));
69 }
70 }
71 Ok(())
72}
73
74fn validate_outline_commands(commands: &[OutlineCommand]) -> Result<(), ValidationError> {
75 for (index, command) in commands.iter().enumerate() {
76 let valid = match command {
80 OutlineCommand::MoveTo(value) => matches!(value.kind.as_str(), "move_to" | "line_to"),
81 OutlineCommand::LineTo(value) => value.kind == "line_to",
82 OutlineCommand::QuadTo(value) => value.kind == "quad_to",
83 OutlineCommand::CurveTo(value) => value.kind == "curve_to",
84 OutlineCommand::Close(value) => value.kind == "close",
85 };
86 if !valid {
87 return Err(error(
88 "invalid_outline_command",
89 format!("$.commands[{index}]"),
90 "outline command kind does not match its payload",
91 ));
92 }
93 }
94 Ok(())
95}
96
97fn validate_glyph_clusters(text: &str, glyphs: &[ShapedGlyph]) -> Result<(), ValidationError> {
98 let char_starts = text
99 .char_indices()
100 .map(|(index, _)| index as u32)
101 .collect::<HashSet<_>>();
102 for (index, glyph) in glyphs.iter().enumerate() {
103 if !char_starts.contains(&glyph.cluster) {
104 return Err(error(
105 "invalid_text_index",
106 format!("$.glyphs[{index}].cluster"),
107 "glyph cluster must be a UTF-8 code-point boundary",
108 ));
109 }
110 }
111 Ok(())
112}
113
114fn error(code: &'static str, path: impl Into<String>, message: &'static str) -> ValidationError {
115 validation_error(code, path, message)
116}