1use crate::{PlotProjectionError, PlotProjectionErrorKind};
4use crate::{
5 PlotterFill as CorePlotterFill, PlotterLineStyle as CorePlotterLineStyle,
6 PlotterOperation as CorePlotterOperation, PlotterTextHAlign as CorePlotterTextHAlign,
7 PlotterTextVAlign as CorePlotterTextVAlign,
8};
9use kicad_monkey_contracts::JavaScriptSafeInteger;
10
11macro_rules! project_plotter_operation {
12 ($source_index:expr, $source_operation:expr) => {{
13 let index = u32::try_from($source_index).map_err(|_| {
14 PlotProjectionError::new(
15 PlotProjectionErrorKind::NumericRange,
16 "plotter operation index exceeds uint32",
17 )
18 })?;
19 let operation = $source_operation;
20 match operation {
21 CorePlotterOperation::ThickSegment(operation) => ThickSegmentOperation {
22 end_x: safe_integer(operation.end_x)?,
23 end_y: safe_integer(operation.end_y)?,
24 index,
25 kind: "ThickSegment".to_owned(),
26 layer: operation.layer,
27 layers: operation.layers,
28 mask_margin_nm: optional_safe_integer(operation.mask_margin_nm)?,
29 pad_size_x_nm: optional_safe_integer(operation.pad_size_x_nm)?,
30 pad_size_y_nm: optional_safe_integer(operation.pad_size_y_nm)?,
31 role: contract_drill_role(operation.role.as_deref())?,
32 start_x: safe_integer(operation.start_x)?,
33 start_y: safe_integer(operation.start_y)?,
34 stroke_color: None,
35 width_nm: safe_integer(operation.width_nm)?,
36 }
37 .into(),
38 CorePlotterOperation::ArcThreePoint(operation) => ArcThreePointOperation {
39 end_x: safe_integer(operation.end_x)?,
40 end_y: safe_integer(operation.end_y)?,
41 fill: contract_fill(operation.fill),
42 fill_color: operation.fill_color,
43 index,
44 kind: "ArcThreePoint".to_owned(),
45 layer: operation.layer,
46 line_style: operation.line_style.map(contract_line_style),
47 mid_x: safe_integer(operation.mid_x)?,
48 mid_y: safe_integer(operation.mid_y)?,
49 start_x: safe_integer(operation.start_x)?,
50 start_y: safe_integer(operation.start_y)?,
51 stroke_color: operation.stroke_color,
52 width_nm: safe_integer(operation.width_nm)?,
53 }
54 .into(),
55 CorePlotterOperation::Circle(operation) => CircleOperation {
56 cx: safe_integer(operation.cx)?,
57 cy: safe_integer(operation.cy)?,
58 diameter_nm: safe_integer(operation.diameter_nm)?,
59 fill: contract_fill(operation.fill),
60 fill_color: operation.fill_color,
61 index,
62 kind: "Circle".to_owned(),
63 layer: operation.layer,
64 layers: contract_circle_layers(operation.layers),
65 line_style: operation.line_style.map(contract_line_style),
66 mask_margin_nm: optional_safe_integer(operation.mask_margin_nm)?,
67 pad_size_x_nm: optional_safe_integer(operation.pad_size_x_nm)?,
68 pad_size_y_nm: optional_safe_integer(operation.pad_size_y_nm)?,
69 role: contract_drill_role(operation.role.as_deref())?,
70 stroke_color: operation.stroke_color,
71 width_nm: safe_integer(operation.width_nm)?,
72 }
73 .into(),
74 CorePlotterOperation::Rect(operation) => RectOperation {
75 corner_radius_nm: safe_integer(operation.corner_radius_nm)?,
76 fill: contract_fill(operation.fill),
77 fill_color: operation.fill_color,
78 index,
79 kind: "Rect".to_owned(),
80 layer: operation.layer,
81 line_style: operation.line_style.map(contract_line_style),
82 stroke_color: operation.stroke_color,
83 width_nm: safe_integer(operation.width_nm)?,
84 x1: safe_integer(operation.x1)?,
85 x2: safe_integer(operation.x2)?,
86 y1: safe_integer(operation.y1)?,
87 y2: safe_integer(operation.y2)?,
88 }
89 .into(),
90 CorePlotterOperation::PlotPoly(operation) => PlotPolyOperation {
91 fill: contract_fill(operation.fill),
92 fill_color: operation.fill_color,
93 index,
94 kind: "PlotPoly".to_owned(),
95 layer: operation.layer,
96 line_style: operation.line_style.map(contract_line_style),
97 points: operation
98 .points
99 .into_iter()
100 .map(|[x, y]| Ok(PlotterPoint::from([safe_integer(x)?, safe_integer(y)?])))
101 .collect::<Result<Vec<_>, PlotProjectionError>>()?,
102 stroke_color: operation.stroke_color,
103 width_nm: safe_integer(operation.width_nm)?,
104 }
105 .into(),
106 CorePlotterOperation::BezierCurve(operation) => BezierCurveOperation {
107 ctrl1_x: safe_integer(operation.ctrl1_x)?,
108 ctrl1_y: safe_integer(operation.ctrl1_y)?,
109 ctrl2_x: safe_integer(operation.ctrl2_x)?,
110 ctrl2_y: safe_integer(operation.ctrl2_y)?,
111 end_x: safe_integer(operation.end_x)?,
112 end_y: safe_integer(operation.end_y)?,
113 index,
114 kind: "BezierCurve".to_owned(),
115 layer: operation.layer,
116 line_style: operation.line_style.map(contract_line_style),
117 start_x: safe_integer(operation.start_x)?,
118 start_y: safe_integer(operation.start_y)?,
119 stroke_color: operation.stroke_color,
120 tolerance_nm: safe_integer(operation.tolerance_nm)?,
121 width_nm: safe_integer(operation.width_nm)?,
122 }
123 .into(),
124 CorePlotterOperation::Text(operation) => TextOperation {
125 bold: operation.bold,
126 color: operation.color,
127 context: None,
128 font_face: operation.font_face,
129 h_align: contract_text_h_align(operation.h_align),
130 index,
131 italic: operation.italic,
132 kind: "Text".to_owned(),
133 knockout: None,
134 layer: operation.layer,
135 mirror: operation.mirror.then_some(true),
136 multiline: operation.multiline,
137 orient_deg: operation.orient_deg,
138 pen_width_nm: safe_integer(operation.pen_width_nm)?,
139 polyline_per_segment: None,
140 render_cache: None,
141 render_cache_exact: None,
142 render_cache_polygons: Vec::new(),
143 render_cache_source: None,
144 size_x_nm: safe_integer(operation.size_x_nm)?,
145 size_y_nm: safe_integer(operation.size_y_nm)?,
146 text: operation.text,
147 text_as_polygons: None,
148 v_align: contract_text_v_align(operation.v_align),
149 x: safe_integer(operation.x)?,
150 y: safe_integer(operation.y)?,
151 }
152 .into(),
153 CorePlotterOperation::FlashPadCircle(operation) => FlashPadCircleOperation {
154 diameter_nm: safe_integer(operation.diameter_nm)?,
155 index,
156 kind: "FlashPadCircle".to_owned(),
157 layers: operation.layers,
158 mask_margin_nm: Some(safe_integer(operation.mask_margin_nm)?),
159 role: None,
160 x: safe_integer(operation.x)?,
161 y: safe_integer(operation.y)?,
162 }
163 .into(),
164 CorePlotterOperation::FlashPadOval(operation) => FlashPadOvalOperation {
165 index,
166 kind: "FlashPadOval".to_owned(),
167 layers: operation.layers,
168 mask_margin_nm: safe_integer(operation.mask_margin_nm)?,
169 orient_deg: operation.orient_deg,
170 size_x_nm: safe_integer(operation.size_x_nm)?,
171 size_y_nm: safe_integer(operation.size_y_nm)?,
172 x: safe_integer(operation.x)?,
173 y: safe_integer(operation.y)?,
174 }
175 .into(),
176 CorePlotterOperation::FlashPadRect(operation) => FlashPadRectOperation {
177 index,
178 kind: "FlashPadRect".to_owned(),
179 layers: operation.layers,
180 mask_margin_nm: safe_integer(operation.mask_margin_nm)?,
181 orient_deg: operation.orient_deg,
182 size_x_nm: safe_integer(operation.size_x_nm)?,
183 size_y_nm: safe_integer(operation.size_y_nm)?,
184 x: safe_integer(operation.x)?,
185 y: safe_integer(operation.y)?,
186 }
187 .into(),
188 CorePlotterOperation::FlashPadRoundRect(operation) => FlashPadRoundRectOperation {
189 corner_radius_nm: safe_integer(operation.corner_radius_nm)?,
190 index,
191 kind: "FlashPadRoundRect".to_owned(),
192 layers: operation.layers,
193 mask_margin_nm: safe_integer(operation.mask_margin_nm)?,
194 orient_deg: operation.orient_deg,
195 size_x_nm: safe_integer(operation.size_x_nm)?,
196 size_y_nm: safe_integer(operation.size_y_nm)?,
197 x: safe_integer(operation.x)?,
198 y: safe_integer(operation.y)?,
199 }
200 .into(),
201 CorePlotterOperation::FlashPadCustom(operation) => FlashPadCustomOperation {
202 anchor_shape: operation.anchor_shape,
203 index,
204 kind: "FlashPadCustom".to_owned(),
205 layers: operation.layers,
206 mask_margin_nm: safe_integer(operation.mask_margin_nm)?,
207 orient_deg: operation.orient_deg,
208 polygon_widths_nm: operation
209 .polygon_widths_nm
210 .unwrap_or_default()
211 .into_iter()
212 .map(safe_integer)
213 .collect::<Result<Vec<_>, PlotProjectionError>>()?,
214 polygons: operation
215 .polygons
216 .into_iter()
217 .map(|polygon| {
218 polygon
219 .into_iter()
220 .map(|[x, y]| {
221 Ok(PlotterPoint::from([safe_integer(x)?, safe_integer(y)?]))
222 })
223 .collect::<Result<Vec<_>, PlotProjectionError>>()
224 })
225 .collect::<Result<Vec<_>, PlotProjectionError>>()?,
226 size_x_nm: safe_integer(operation.size_x_nm)?,
227 size_y_nm: safe_integer(operation.size_y_nm)?,
228 x: safe_integer(operation.x)?,
229 y: safe_integer(operation.y)?,
230 }
231 .into(),
232 CorePlotterOperation::FlashPadTrapez(operation) => FlashPadTrapezOperation {
233 corners: contract_quad(operation.corners)?,
234 index,
235 kind: "FlashPadTrapez".to_owned(),
236 layers: operation.layers,
237 mask_margin_nm: safe_integer(operation.mask_margin_nm)?,
238 orient_deg: operation.orient_deg,
239 x: safe_integer(operation.x)?,
240 y: safe_integer(operation.y)?,
241 }
242 .into(),
243 }
244 }};
245}
246
247fn safe_integer(value: i64) -> Result<JavaScriptSafeInteger, PlotProjectionError> {
248 JavaScriptSafeInteger::try_from(value).map_err(|error| {
249 PlotProjectionError::new(PlotProjectionErrorKind::NumericRange, error.to_string())
250 })
251}
252
253fn optional_safe_integer(
254 value: Option<i64>,
255) -> Result<Option<JavaScriptSafeInteger>, PlotProjectionError> {
256 value.map(safe_integer).transpose()
257}
258
259macro_rules! define_plotter_projector {
260 ($adapter:ident, $generated:ident, $circle_layers:ty, $map_circle_layers:expr) => {
261 mod $adapter {
262 use super::*;
263 use kicad_monkey_contracts::generated::$generated::*;
264
265 pub(crate) fn project(
266 index: usize,
267 operation: CorePlotterOperation,
268 ) -> Result<PlotterOperation, PlotProjectionError> {
269 Ok(project_plotter_operation!(index, operation))
270 }
271
272 fn contract_circle_layers(value: Vec<String>) -> $circle_layers {
273 ($map_circle_layers)(value)
274 }
275
276 fn contract_fill(fill: CorePlotterFill) -> PlotterFill {
277 match fill {
278 CorePlotterFill::NoFill => PlotterFill::NoFill,
279 CorePlotterFill::FilledShape => PlotterFill::FilledShape,
280 CorePlotterFill::FilledWithBackgroundBodyColor => {
281 PlotterFill::FilledWithBgBodycolor
282 }
283 CorePlotterFill::FilledWithColor => PlotterFill::FilledWithColor,
284 CorePlotterFill::Hatch => PlotterFill::Hatch,
285 CorePlotterFill::ReverseHatch => PlotterFill::ReverseHatch,
286 CorePlotterFill::CrossHatch => PlotterFill::CrossHatch,
287 }
288 }
289
290 fn contract_line_style(style: CorePlotterLineStyle) -> PlotterLineStyle {
291 match style {
292 CorePlotterLineStyle::Default => PlotterLineStyle::Default,
293 CorePlotterLineStyle::Solid => PlotterLineStyle::Solid,
294 CorePlotterLineStyle::Dash => PlotterLineStyle::Dash,
295 CorePlotterLineStyle::Dot => PlotterLineStyle::Dot,
296 CorePlotterLineStyle::DashDot => PlotterLineStyle::DashDot,
297 CorePlotterLineStyle::DashDotDot => PlotterLineStyle::DashDotDot,
298 }
299 }
300
301 fn contract_text_h_align(value: CorePlotterTextHAlign) -> PlotterTextHAlign {
302 match value {
303 CorePlotterTextHAlign::Left => PlotterTextHAlign::GrTextHAlignLeft,
304 CorePlotterTextHAlign::Center => PlotterTextHAlign::GrTextHAlignCenter,
305 CorePlotterTextHAlign::Right => PlotterTextHAlign::GrTextHAlignRight,
306 }
307 }
308
309 fn contract_text_v_align(value: CorePlotterTextVAlign) -> PlotterTextVAlign {
310 match value {
311 CorePlotterTextVAlign::Top => PlotterTextVAlign::GrTextVAlignTop,
312 CorePlotterTextVAlign::Center => PlotterTextVAlign::GrTextVAlignCenter,
313 CorePlotterTextVAlign::Bottom => PlotterTextVAlign::GrTextVAlignBottom,
314 }
315 }
316
317 fn contract_drill_role(
318 value: Option<&str>,
319 ) -> Result<Option<PlotterDrillRole>, PlotProjectionError> {
320 value
321 .map(|role| {
322 PlotterDrillRole::try_from(role).map_err(|error| {
323 PlotProjectionError::new(
324 PlotProjectionErrorKind::InvalidModel,
325 error.to_string(),
326 )
327 })
328 })
329 .transpose()
330 }
331
332 fn contract_quad(corners: [[i64; 2]; 4]) -> Result<PlotterQuad, PlotProjectionError> {
333 let points = corners
334 .into_iter()
335 .map(|[x, y]| Ok(PlotterPoint::from([safe_integer(x)?, safe_integer(y)?])))
336 .collect::<Result<Vec<_>, PlotProjectionError>>()?;
337 let points: [PlotterPoint; 4] = points.try_into().map_err(|_| {
338 PlotProjectionError::new(
339 PlotProjectionErrorKind::InvalidModel,
340 "plotter quad must contain four points",
341 )
342 })?;
343 Ok(PlotterQuad::from(points))
344 }
345 }
346 };
347}
348
349define_plotter_projector!(
350 footprint,
351 footprint_plot_document,
352 Vec<String>,
353 std::convert::identity
354);
355define_plotter_projector!(
356 board,
357 board_plot_document,
358 Option<Vec<String>>,
359 |layers: Vec<String>| (!layers.is_empty()).then_some(layers)
360);
361define_plotter_projector!(
362 symbol,
363 symbol_plot_document,
364 Vec<String>,
365 std::convert::identity
366);
367define_plotter_projector!(
368 schematic,
369 schematic_plot_document,
370 Vec<String>,
371 std::convert::identity
372);
373
374pub fn contract_plotter_operation(
375 index: usize,
376 operation: CorePlotterOperation,
377) -> Result<kicad_monkey_contracts::generated::footprint_plot_document::PlotterOperation, String> {
378 footprint::project(index, operation).map_err(|error| error.to_string())
379}
380
381pub(crate) use board::project as contract_board_plotter_operation;
382pub(crate) use footprint::project as contract_footprint_plotter_operation;
383pub(crate) use schematic::project as contract_schematic_plotter_operation;
384pub(crate) use symbol::project as contract_symbol_plotter_operation;