Feat: implement CTB/STB plot style table with PDF export integration

- src/io/plot_style.rs: PlotStyleTable and PlotStyleEntry data structures;
  CTB reader (deflate/zlib decompressed text format) with ACI→RGB and
  ACI→lineweight lookup; CTB writer (zlib compressed); LW_TABLE constant
- src/io/pdf_export.rs: accept optional PlotStyleTable; apply ACI color
  and lineweight overrides during PDF rendering
- src/scene/wire_model.rs: add `aci: u8` field to carry ACI index
- src/scene/render.rs: render_style() now returns ACI alongside RGBA
- src/scene/mod.rs + tessellate.rs: propagate aci through wire building
- app: PlotStyleLoad/Loaded/Clear messages and handlers;
  PLOTSTYLE LOAD | CLEAR | STATUS command; active_plot_style state on H7CAD
- flate2 added as dependency

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Hakan SevenandClaude Sonnet 4.6 committed 2026-04-06 14:37:23 +03:00
1 parent 911e36ba04
commit 1db3ad6a33
11 files changed
+429 -28

No files matched your search

+34 -13
View File
@@ -9,6 +9,7 @@
// drawing origin at the paper origin.
use crate::scene::WireModel;
use crate::io::plot_style::PlotStyleTable;
use printpdf::{Color, Line, LineCapStyle, LineJoinStyle, LinePoint, Mm, Op, PdfDocument,
PdfPage, PdfSaveOptions, Point, Pt, Rgb};
use std::io::Write;
@@ -30,8 +31,9 @@ pub fn export_pdf(
offset_y: f32,
rotation_deg: i32,
path: &Path,
plot_style: Option<&PlotStyleTable>,
) -> Result<(), String> {
let bytes = build_pdf(wires, paper_w as f32, paper_h as f32, offset_x, offset_y, rotation_deg);
let bytes = build_pdf(wires, paper_w as f32, paper_h as f32, offset_x, offset_y, rotation_deg, plot_style);
let mut file = std::fs::File::create(path).map_err(|e| e.to_string())?;
file.write_all(&bytes).map_err(|e| e.to_string())
}
@@ -50,7 +52,7 @@ pub async fn pick_pdf_path_owned(stem: String) -> Option<std::path::PathBuf> {
// ── PDF builder ───────────────────────────────────────────────────────────
fn build_pdf(wires: &[WireModel], paper_w: f32, paper_h: f32, ox: f32, oy: f32, rotation_deg: i32) -> Vec<u8> {
fn build_pdf(wires: &[WireModel], paper_w: f32, paper_h: f32, ox: f32, oy: f32, rotation_deg: i32, plot_style: Option<&PlotStyleTable>) -> Vec<u8> {
let mut doc = PdfDocument::new("H7CAD Export");
let mut ops: Vec<Op> = Vec::new();
@@ -93,6 +95,10 @@ fn build_pdf(wires: &[WireModel], paper_w: f32, paper_h: f32, ox: f32, oy: f32,
let mut last_color: Option<[f32; 3]> = None;
let mut last_lw: Option<f32> = None;
// mm to PDF points (1 mm = 2.834645 pt).
const MM_TO_PT: f32 = 2.834645;
// Screen px to PDF points (approximate at 96 dpi).
const PX_TO_PT: f32 = 0.35278;
for wire in wires {
let [mut r, mut g, mut b, a] = wire.color;
@@ -103,15 +109,29 @@ fn build_pdf(wires: &[WireModel], paper_w: f32, paper_h: f32, ox: f32, oy: f32,
if wire.name == "__paper_boundary__" {
continue;
}
// Near-white and near-yellow (viewport active border) → dark grey for print.
let is_light = r > 0.80 && g > 0.80 && b > 0.80;
let is_yellow = r > 0.80 && g > 0.70 && b < 0.30;
let is_cyan = r < 0.30 && g > 0.70 && b > 0.70;
if is_light || is_yellow {
r = 0.0; g = 0.0; b = 0.0;
} else if is_cyan {
// Viewport border: print as dark blue.
r = 0.0; g = 0.15; b = 0.50;
// Apply CTB plot style table overrides (color + lineweight).
let mut lw_override: Option<f32> = None;
if let Some(ctb) = plot_style {
if wire.aci > 0 {
if let Some([cr, cg, cb]) = ctb.resolve_color(wire.aci) {
r = cr; g = cg; b = cb;
}
lw_override = ctb.resolve_lineweight(wire.aci)
.map(|mm| (mm * MM_TO_PT).max(0.1));
}
}
// Near-white and near-yellow (viewport active border) → dark grey for print
// (only when no CTB override was applied).
if lw_override.is_none() {
let is_light = r > 0.80 && g > 0.80 && b > 0.80;
let is_yellow = r > 0.80 && g > 0.70 && b < 0.30;
let is_cyan = r < 0.30 && g > 0.70 && b > 0.70;
if is_light || is_yellow {
r = 0.0; g = 0.0; b = 0.0;
} else if is_cyan {
// Viewport border: print as dark blue.
r = 0.0; g = 0.15; b = 0.50;
}
}
if last_color.map(|c| {
@@ -123,8 +143,9 @@ fn build_pdf(wires: &[WireModel], paper_w: f32, paper_h: f32, ox: f32, oy: f32,
last_color = Some([r, g, b]);
}
// Line weight: screen px → points (approximate 1 px ≈ 0.35 pt).
let lw_pt = (wire.line_weight_px * 0.35278_f32).max(0.1);
// Line weight: CTB override (in pt) or screen px → points.
let lw_pt = lw_override
.unwrap_or_else(|| (wire.line_weight_px * PX_TO_PT).max(0.1));
if last_lw.map(|l| (l - lw_pt).abs() > 0.01).unwrap_or(true) {
ops.push(Op::SetOutlineThickness { pt: Pt(lw_pt) });
last_lw = Some(lw_pt);