mirror of
https://github.com/storytold/pdfcraft.git
synced 2026-11-26 04:58:07 +00:00
pdf-workflows-w13: net change of 2 commits
- pdf-workflows-w13: written (committed by the lead: the run's own commit failed while gpg was locked after a reboot) - pdf-workflows-w13: review fixes (review-1)
This commit is contained in:
@@ -10,6 +10,7 @@ workspace = true
|
||||
|
||||
[dependencies]
|
||||
craft-docpath = { workspace = true }
|
||||
craft-font-catalog = { workspace = true }
|
||||
craft-image-codecs = { workspace = true }
|
||||
pdfcraft-measure = { workspace = true }
|
||||
image = { version = "0.25", default-features = false }
|
||||
|
||||
@@ -8,28 +8,10 @@ pub struct FontFile {
|
||||
pub face: String,
|
||||
}
|
||||
|
||||
/// Standard user-installed font folders; discovery is performed only on request.
|
||||
/// The font folders of this platform, the system's and the user's, as the font catalogue lists
|
||||
/// them; discovery is performed only on request.
|
||||
pub fn installed_directories() -> Vec<PathBuf> {
|
||||
let home = std::env::var_os("HOME").map(PathBuf::from);
|
||||
if cfg!(target_os = "macos") {
|
||||
let mut dirs = vec![PathBuf::from("/Library/Fonts")];
|
||||
if let Some(home) = home {
|
||||
dirs.push(home.join("Library/Fonts"));
|
||||
}
|
||||
dirs
|
||||
} else if cfg!(windows) {
|
||||
let mut dirs = std::env::var_os("WINDIR").map(|p| PathBuf::from(p).join("Fonts")).into_iter().collect::<Vec<_>>();
|
||||
if let Some(local) = std::env::var_os("LOCALAPPDATA") {
|
||||
dirs.push(PathBuf::from(local).join("Microsoft/Windows/Fonts"));
|
||||
}
|
||||
dirs
|
||||
} else {
|
||||
let mut dirs = vec![PathBuf::from("/usr/share/fonts"), PathBuf::from("/usr/local/share/fonts")];
|
||||
if let Some(home) = home {
|
||||
dirs.extend([home.join(".local/share/fonts"), home.join(".fonts")]);
|
||||
}
|
||||
dirs
|
||||
}
|
||||
craft_font_catalog::font_dirs(&craft_font_catalog::FontEnv::from_process())
|
||||
}
|
||||
|
||||
/// Bounded scan with resolved roots and no descendant symlinks.
|
||||
@@ -148,47 +130,6 @@ mod tests {
|
||||
assert_eq!(found.len(), 501, "do not silently discard accepted fonts after sorting");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_installed_directories() {
|
||||
let dirs = installed_directories();
|
||||
assert!(!dirs.is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_font_extension() {
|
||||
assert!(font_extension(Path::new("Arial.ttf")));
|
||||
assert!(font_extension(Path::new("Noto.otf")));
|
||||
assert!(font_extension(Path::new("CUSTOM.TTF")));
|
||||
assert!(font_extension(Path::new("ROBOTO.OTF")));
|
||||
assert!(!font_extension(Path::new("document.pdf")));
|
||||
assert!(!font_extension(Path::new("font.woff")));
|
||||
assert!(!font_extension(Path::new("font.woff2")));
|
||||
assert!(!font_extension(Path::new("no_extension")));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_font_content_key() {
|
||||
let empty = font_content_key(&[]);
|
||||
assert_eq!(empty.0, 0);
|
||||
|
||||
let a = font_content_key(b"TrueType font sample data bytes");
|
||||
let b = font_content_key(b"TrueType font sample data bytes");
|
||||
let c = font_content_key(b"Different font sample data bytes");
|
||||
|
||||
assert_eq!(a, b, "identical bytes must produce identical keys");
|
||||
assert_ne!(a, c, "differing bytes must produce distinct keys");
|
||||
assert_eq!(a.0, b"TrueType font sample data bytes".len());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_discover_empty_and_nonexistent_roots() {
|
||||
let empty = discover(&[]);
|
||||
assert!(empty.is_empty());
|
||||
|
||||
let nonexistent = discover(&[PathBuf::from("Z:\\nonexistent_font_path_xyz_12345")]);
|
||||
assert!(nonexistent.is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_discover_deduplicates_non_adjacent_same_content() {
|
||||
let root = std::env::temp_dir().join(format!("pdfcraft-font-dedup-test-{}", std::process::id()));
|
||||
|
||||
@@ -5,6 +5,8 @@ use std::collections::HashSet;
|
||||
use std::path::PathBuf;
|
||||
use std::sync::Arc;
|
||||
|
||||
use craft::docpath::names::{CopyNumbering, safe_file_stem, unique_file_name};
|
||||
use craft::host::WriteIntent;
|
||||
use pdfcraft_engine::{EditError, SourceKind};
|
||||
|
||||
use crate::PdfCraftApp;
|
||||
@@ -54,34 +56,16 @@ impl PdfCraftApp {
|
||||
refused.push(format!("{name} ({})", tl!("already a PDF")));
|
||||
continue;
|
||||
}
|
||||
let base = crate::files::file_stem_from_user(stem(&name));
|
||||
let base = safe_file_stem(stem(&name), "pdf");
|
||||
let base = if base.is_empty() { "document" } else { &base };
|
||||
let mut output = None;
|
||||
for number in 1..10_000 {
|
||||
let filename = if number == 1 { format!("{base}.pdf") } else { format!("{base} ({number}).pdf") };
|
||||
let candidate = dir.as_ref().map(|d| d.join(&filename));
|
||||
if !used.contains(&filename.to_lowercase()) && !candidate.as_ref().is_some_and(|path| path.exists()) {
|
||||
used.insert(filename.to_lowercase());
|
||||
output = Some((filename, candidate));
|
||||
break;
|
||||
}
|
||||
}
|
||||
let Some((filename, target)) = output else {
|
||||
refused.push(format!("{name} ({})", tl!("no unused output name")));
|
||||
continue;
|
||||
};
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
let result = match target {
|
||||
Some(path) => crate::editing::write_atomically(&path.to_string_lossy(), &pdf).map_err(|e| e.to_string()),
|
||||
None => Err(tl!("no output folder was chosen").to_string()),
|
||||
};
|
||||
#[cfg(target_arch = "wasm32")]
|
||||
let result = {
|
||||
let _ = target;
|
||||
crate::editing::download(&filename, &pdf)
|
||||
};
|
||||
match result {
|
||||
Ok(()) => made += 1,
|
||||
let filename = unique_file_name(&format!("{base}.pdf"), CopyNumbering::Parenthesized, |candidate| {
|
||||
used.contains(&candidate.to_lowercase()) || dir.as_ref().is_some_and(|d| d.join(candidate).exists())
|
||||
});
|
||||
used.insert(filename.to_lowercase());
|
||||
// Without a folder (the web) the write is a download.
|
||||
let path = dir.as_ref().map_or_else(|| PathBuf::from(&filename), |d| d.join(&filename));
|
||||
match self.host.files().write(&path, &pdf, WriteIntent::SaveAs) {
|
||||
Ok(_) => made += 1,
|
||||
Err(error) => refused.push(format!("{filename} ({error})")),
|
||||
}
|
||||
}
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user