// PCB SVG config authority. Generate downstream contracts; edit this source.
import "@typespec/json-schema";
using TypeSpec.JsonSchema;
namespace KiCadCruncher;

alias SyntheticLayer = "BOARD_SUBSTRATE" | "BOARD_OUTLINE" | "BOARD_CUTOUTS" | "DRILLS" | "SLOTS" | "SOLDERMASK_FILM_TOP" | "SOLDERMASK_FILM_BOTTOM" | "ILLUSTRATION_TOP" | "ILLUSTRATION_BOTTOM" | "ASSEMBLY_HLR_TOP" | "ASSEMBLY_HLR_BOTTOM" | "ASSEMBLY_DESIGNATORS_TOP" | "ASSEMBLY_DESIGNATORS_BOTTOM" | "PIN1_TOP" | "PIN1_BOTTOM" | "ASSEMBLY_HLR_TOP_OUTLINE" | "ASSEMBLY_HLR_BOTTOM_OUTLINE" | "ASSEMBLY_HLR_TOP_DETAIL" | "ASSEMBLY_HLR_BOTTOM_DETAIL" | "ASSEMBLY_BOUNDS_TOP_MODEL" | "ASSEMBLY_BOUNDS_BOTTOM_MODEL" | "ASSEMBLY_BOUNDS_TOP_PADS" | "ASSEMBLY_BOUNDS_BOTTOM_PADS";

alias ProjectionMode = "detail" | "outline" | "bounding_box" | "model_bounds" | "pad_bounds" | "pad_shape" | "none";

model ComponentOverride {
  "side"?: "top" | "bottom";
  @doc("How the assembly HLR draws the component, over the view's assembly_hlr_mode. \"pad_bounds\" is the box around every pad land on the side, non-plated holes included; \"pad_shape\" outlines each copper pad on the side in its own shape and each non-plated hole; \"none\" draws nothing. The fixed-mode tokens (ASSEMBLY_HLR_*_OUTLINE and _DETAIL, ASSEMBLY_BOUNDS_*) keep their own mode over it.")
  "projection"?: ProjectionMode;
  "assembly_hlr"?: ComponentOverrideAssemblyHlr;
  "assembly_designators"?: ComponentOverrideAssemblyDesignators;
  "illustration"?: ComponentIllustration;
  "pin1_enabled"?: boolean;
  "pin1_pad"?: string;
  "cathode_pad"?: string;
  "diode"?: boolean;
  "diode_line_art"?: boolean;
  "show_designator"?: boolean;
}

model ComponentIllustration {
  @minValue(0) @maxValue(1) "opacity"?: float64;
}

model ComponentOverrideAssemblyHlr {
  ...Record<unknown>;
}

model ComponentOverrideAssemblyDesignators {
  ...Record<unknown>;
  ...LabelPaintOptions;
}

model LabelPaintOptions {
  "color"?: string;
  @minValue(0) @maxValue(1) "opacity"?: float64;
  "stroke_color"?: string;
  @minValue(0) "stroke_width_mm"?: float64;
  @minValue(0) "stroke_width_pct"?: float64;
}

model StyleTable {
  ...Record<StyleTableExtension>;
  "illustration"?: StyleTableIllustration;
  "board_substrate"?: StyleTableBoardSubstrate;
  "soldermask_film"?: StyleTableSoldermaskFilm;
  "board_outline"?: StyleTableBoardOutline;
  "board_cutouts"?: StyleTableBoardCutouts;
  "drills"?: HoleStyle;
  "slots"?: HoleStyle;
  "pin1_marker"?: StyleTablePin1Marker;
  "assembly_designators"?: StyleTableAssemblyDesignators;
  "assembly_hlr"?: StyleTableAssemblyHlr;
  "silkscreen_component_graphics"?: StyleTableSilkscreen;
  "silkscreen_designators"?: StyleTableSilkscreen;
  "silkscreen_board_graphics"?: StyleTableSilkscreen;
}

model StyleTableIllustration {
  ...Record<unknown>;
  "enabled"?: boolean;
  @minValue(0)
  @maxValue(1)
  "opacity"?: float64;
  @minValueExclusive(0)
  "outline_width_mm"?: float64;
  @minValueExclusive(0)
  "detail_width_mm"?: float64;
}

model StyleTableBoardSubstrate {
  ...Record<unknown>;
  "enabled"?: boolean;
  "color"?: string;
  @minValue(0)
  @maxValue(1)
  "opacity"?: float64;
}

model StyleTableSoldermaskFilm {
  ...Record<unknown>;
  "enabled"?: boolean;
  @doc("The film colour, or \"auto\": the board's saved solder-mask colour for the view's side.")
  "color"?: string;
  @minValue(0)
  @maxValue(1)
  "opacity"?: float64;
}

model StyleTableSilkscreen {
  ...Record<unknown>;
  "enabled"?: boolean;
  @doc("The silkscreen colour, or \"contrast\": black on a white mask, else near-white, against the view's resolved film colour.")
  "color"?: string;
}

model StyleTableExtension {
  ...Record<unknown>;
}

model StyleTableBoardOutline {
  ...Record<unknown>;
  "enabled"?: boolean;
  "color"?: string;
  @minValueExclusive(0)
  "line_width_mm"?: float64;
  @minValueExclusive(0)
  "max_arc_segment_mm"?: float64;
  @minValueExclusive(0)
  "max_curve_segment_mm"?: float64;
  @minValueExclusive(0)
  "max_circle_segment_mm"?: float64;
  @minValue(1)
  "min_arc_segments"?: integer;
  @minValue(1)
  "min_curve_segments"?: integer;
  @minValue(8)
  "min_circle_segments"?: integer;
  @minValue(8)
  "max_arc_segments"?: integer;
  @minValue(8)
  "max_curve_segments"?: integer;
  @minValue(8)
  "max_circle_segments"?: integer;
}

model StyleTableBoardCutouts {
  ...Record<unknown>;
  "enabled"?: boolean;
  "color"?: string;
  "hatch"?: boolean;
  @minValueExclusive(0)
  "hatch_spacing_mm"?: float64;
  "hatch_angle_deg"?: float64;
  @minValueExclusive(0)
  "hatch_line_width_mm"?: float64;
  "hatch_color"?: string;
  @minValue(0)
  @maxValue(1)
  "hatch_opacity"?: float64;
  "outline_style"?: "solid" | "dashed";
  @minValueExclusive(0)
  "outline_dash_mm"?: float64;
  @minValueExclusive(0)
  "outline_width_mm"?: float64;
  @minValue(0)
  @maxValue(1)
  "outline_opacity"?: float64;
}

model StyleTablePin1Marker {
  ...Record<unknown>;
  "enabled"?: boolean;
  "color"?: string;
  @minValueExclusive(0)
  "dot_diameter_mm"?: float64;
  @minValue(0)
  "pad_diameter_ratio"?: float64;
  @minValue(0)
  "min_dot_diameter_mm"?: float64;
  @minValue(0)
  "max_dot_diameter_mm"?: float64;
}

model StyleTableAssemblyDesignators {
  ...Record<unknown>;
  ...LabelPaintOptions;
  "enabled"?: boolean;
  "font_family"?: string;
  "font_weight"?: string | float64;
  @doc("The share of the part's label area the designator may fill, clamped to 0.05..1 (a value outside warns), as Altium Cruncher.")
  "box_fill_ratio"?: float64;
  @minValue(0)
  "max_font_size_mm"?: float64;
  "selector_overrides"?: StyleTableAssemblyDesignatorsSelectorOverrides;
}

model StyleTableAssemblyDesignatorsSelectorOverrides {
  ...Record<StyleTableAssemblyDesignatorsSelectorOverridesExtension>;
}

model StyleTableAssemblyDesignatorsSelectorOverridesExtension {
  ...Record<unknown>;
}

model StyleTableAssemblyHlr {
  ...Record<unknown>;
  "enabled"?: boolean;
  "color"?: string;
  @minValueExclusive(0)
  "line_width_mm"?: float64;
  @minValue(0)
  @maxValue(1)
  "opacity"?: float64;
}

model HoleStyle {
  ...Record<unknown>;
  "enabled"?: boolean;
  "plated_color"?: string;
  "non_plated_color"?: string;
  "outline"?: boolean;
  @minValueExclusive(0)
  "outline_width_mm"?: float64;
  "respect_tenting"?: boolean;
  @minValue(0)
  @maxValue(1)
  "opacity"?: float64;
}

model Pin1 {
  "exclude_designators"?: string | (string)[];
  "exclude_designator_prefixes"?: (string)[];
  "exclude_single_pin"?: boolean;
}

@jsonSchema
@extension("title", "PcbSvgConfig")
@id("pcb_svg_config.a0.schema.json")
model PcbSvgConfig {
  "schema": "kicad_cruncher.pcb_svg.config.a0";
  "global": PcbSvgConfigGlobal;
  "assembly"?: PcbSvgConfigAssembly;
  "dnp"?: PcbSvgConfigDnp;
  "diodes"?: PcbSvgConfigDiodes;
  "pin1"?: PcbSvgConfigPin1;
  @doc("Per-component overrides. A key that is a designator on the board is an exact, case-sensitive key for that part. Any other key is a selector, matched case-insensitively, as in assembly_designators.selector_overrides: a prefix (a key ending in *, or one of letters only, as \"TP\" or \"TP*\") or a range <P><n>-<P2><m> (P any run of non-digits, P2 empty or P, either order, as \"U5-U15\" or \"U$1-U$9\"); a key that is neither matches nothing. Every matching entry applies field by field, selector keys in file order with a later key over an earlier one, then the exact key; style mappings merge key by key. A newly written config holds the generated entries of kcr's assembly part rules.")
  "components"?: PcbSvgConfigComponents;
  "layer_outputs": PcbSvgConfigLayerOutputs;
  "views": (PcbSvgConfigViewsItem)[];
}

model PcbSvgConfigGlobal {
  ...Record<unknown>;
  "pcbdoc"?: string | null;
  @doc("The one canvas of every view and layer output of a board: bounds \"board_outline\" fits the Edge.Cuts outline at the global board_outline.line_width_mm, drawn or not (all geometry when the board has no outline); \"all_geometry\" fits all source geometry. Either grows by margin_mm. Toon keeps its own side frame.")
  "canvas"?: PcbSvgConfigGlobalCanvas;
  "include_metadata"?: boolean;
  "show_empty_layers"?: boolean;
  "clip_to_outline"?: boolean;
  "clip_holes_from_copper"?: boolean;
  "mirror_bottom_view"?: boolean;
  @minValueExclusive(0)
  "svg_scale"?: float64;
  "svg_size_unit"?: string;
  "clean_output"?: boolean;
  "styles"?: StyleTable;
}

model PcbSvgConfigGlobalCanvas {
  "bounds"?: "board_outline" | "all_geometry";
  @minValue(0)
  "margin_mm"?: float64;
}

model PcbSvgConfigAssembly {
  @doc("The projection of standalone ASSEMBLY_HLR_TOP/BOTTOM and ASSEMBLY_DESIGNATORS_* layer outputs only, for a component that sets none; configured views use their assembly_hlr_mode.")
  "default_projection"?: ProjectionMode;
  "dnp_projection"?: ProjectionMode;
  "designator_color"?: string;
  "dnp_designator_color"?: string;
}

model PcbSvgConfigDnp {
  "color"?: string;
  "hatch"?: boolean;
  @minValueExclusive(0)
  "hatch_spacing_mm"?: float64;
  "hatch_angle_deg"?: float64;
  @minValueExclusive(0)
  "hatch_line_width_mm"?: float64;
}

model PcbSvgConfigDiodes {
  "enabled"?: boolean;
  "line_art"?: boolean;
  "marker_color"?: string;
  "numeric_cathode_pad"?: string;
  "cathode_pad_names"?: (string)[];
  "designator_prefixes"?: (string)[];
  "parameter_terms"?: (string)[];
}

model PcbSvgConfigPin1 {
  "exclude_designators"?: string | (string)[];
  "exclude_designator_prefixes"?: (string)[];
  "exclude_single_pin"?: boolean;
}

model PcbSvgConfigComponents {
  ...Record<ComponentOverride>;
}

model PcbSvgConfigLayerOutputs {
  "enabled"?: boolean;
  "layers"?: "auto" | (string)[];
  "include_special_layers"?: (SyntheticLayer)[];
  @doc("Include raw Edge.Cuts geometry as context in each non-Edge.Cuts physical layer output.")
  "add_edge_cuts_to_physical_layers"?: boolean;
  @doc("Include computed round drill overlays as context in each non-Edge.Cuts physical layer output.")
  "add_drills_to_physical_layers"?: boolean;
  @doc("Include computed slot overlays as context in each non-Edge.Cuts physical layer output.")
  "add_slots_to_physical_layers"?: boolean;
  @doc("Write standalone __virtual__ layer SVG files selected by include_special_layers and synthetic layer tokens.")
  "write_virtual_layers"?: boolean;
  "output_dir"?: string;
}

model PcbSvgConfigViewsItem {
  @minLength(1)
  "name": string;
  "enabled"?: boolean;
  "group_id"?: string;
  "output_svg"?: string;
  "layers": (string)[];
  "mirror"?: boolean;
  @doc("How the view's ASSEMBLY_HLR_* layers draw a component that sets no projection, \"detail\" when unset; its designators fit the same geometry.")
  "assembly_hlr_mode"?: "outline" | "detail" | "bounding_box" | "model_bounds" | "pad_bounds" | "pad_shape" | "none";
  "styles"?: StyleTable;
  "pin1"?: Pin1;
  "description"?: string;
}

