forked from 1b-agent/opencadstudio-upstream
fix(plot-style): load plot style tables written by other applications
Tables copied from other applications into the plot styles folder were listed but refused on selection, showing as "missing" with the reason lost on a console: the loader failed hard on a checksum or a text length that did not add up and on any non-UTF-8 byte — and those files carry their descriptions in Windows-1252. - decompress_ctb reads the file as it is: a checksum or length mismatch is reported and read past (a genuinely cut-off file is still an error), a zlib stream with a bad trailer is retried as raw deflate, and text that is not UTF-8 is read as Windows-1252 (pens and colours are numbers and unaffected; other code pages only show wrong characters in descriptions). - PlotStyleTable carries load_warnings; every load site reports them on the command line, so a table that needed tolerance says so. - load_named explains itself: a .stb name says named tables are not supported yet, a .ctb missing from the folder says which folder to copy it into (instead of "Unknown built-in plot style"). - The Plot dialog shows the reason under the Table row (style_error) — from a selection, from a page setup that names a table, and from the Load… picker, whose error used to be swallowed. - A table picked with Load… from outside the plot styles folder is copied into it (unless a file of that name is already there), so it is listed from then on and page setups naming it resolve after a restart. Tests: flipped checksum, text length off by one, a short payload field, Windows-1252 descriptions, a truncated stream, .stb and missing names. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
1 parent
2b38f798c0
commit
7a75d430cd
28 files changed
+339
-35
No files matched your search
+26
-3
@@ -1522,7 +1522,12 @@ mod save_failure_tests {
|
||||
// ── Plot Style Table ──────────────────────────────────────────────────────
|
||||
|
||||
/// Show a file-open dialog and load the selected CTB or STB file.
|
||||
pub async fn pick_plot_style() -> Option<plot_style::PlotStyleTable> {
|
||||
/// Pick a plot style table file and load it. `Ok(None)` when the picker was
|
||||
/// cancelled; `Err` says why the chosen file could not be read. A table
|
||||
/// picked from outside the plot styles folder is copied into it (unless a
|
||||
/// file of that name is already there), so the dialog lists it from now on
|
||||
/// and page setups naming it resolve after a restart.
|
||||
pub async fn pick_plot_style() -> Result<Option<plot_style::PlotStyleTable>, String> {
|
||||
let dialog = crate::sys::file_dialog()
|
||||
.set_title(crate::t!("Load Plot Style Table").as_ref())
|
||||
.add_filter(crate::t!("Plot Style Tables").as_ref(), &["ctb", "CTB"])
|
||||
@@ -1533,8 +1538,26 @@ pub async fn pick_plot_style() -> Option<plot_style::PlotStyleTable> {
|
||||
Ok(dir) => dialog.set_directory(dir),
|
||||
Err(_) => dialog,
|
||||
};
|
||||
let handle = dialog.pick_file().await?;
|
||||
plot_style::PlotStyleTable::load(&crate::sys::handle_path(&handle)).ok()
|
||||
let Some(handle) = dialog.pick_file().await else {
|
||||
return Ok(None);
|
||||
};
|
||||
let path = crate::sys::handle_path(&handle);
|
||||
let table = plot_style::PlotStyleTable::load(&path)?;
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
if let (Ok(dir), Some(file_name)) = (plot_style::ensure_plot_styles_dir(), path.file_name()) {
|
||||
let destination = dir.join(file_name);
|
||||
let same_place = path.parent().is_some_and(|parent| {
|
||||
parent
|
||||
.canonicalize()
|
||||
.ok()
|
||||
.zip(dir.canonicalize().ok())
|
||||
.is_some_and(|(a, b)| a == b)
|
||||
});
|
||||
if !same_place && !destination.exists() {
|
||||
let _ = std::fs::copy(&path, &destination);
|
||||
}
|
||||
}
|
||||
Ok(Some(table))
|
||||
}
|
||||
|
||||
// ── Image file picker ─────────────────────────────────────────────────────
|
||||
|
||||
Reference in new issue
Block a user