diff --git a/Cargo.lock b/Cargo.lock
index e1318be926..a872e904cc 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -2232,6 +2232,15 @@ dependencies = [
"image",
]
+[[package]]
+name = "craft-gpu"
+version = "0.1.0"
+dependencies = [
+ "serde",
+ "serde_json",
+ "thiserror 2.0.21",
+]
+
[[package]]
name = "craft-gpu-device"
version = "0.1.0"
@@ -2243,6 +2252,31 @@ dependencies = [
"wgpu",
]
+[[package]]
+name = "craft-gpu-null"
+version = "0.1.0"
+dependencies = [
+ "craft-gpu",
+]
+
+[[package]]
+name = "craft-gpu-providers"
+version = "0.1.0"
+dependencies = [
+ "craft-gpu",
+ "craft-gpu-null",
+]
+
+[[package]]
+name = "craft-gpu-wgpu"
+version = "0.1.0"
+dependencies = [
+ "craft-gpu",
+ "craft-gpu-providers",
+ "pollster 0.4.0",
+ "wgpu",
+]
+
[[package]]
name = "craft-gridcraft-format"
version = "0.1.0"
@@ -2399,6 +2433,10 @@ dependencies = [
"widestring",
]
+[[package]]
+name = "craft-ipp"
+version = "0.1.0"
+
[[package]]
name = "craft-java-class"
version = "0.1.0"
@@ -2935,6 +2973,7 @@ dependencies = [
name = "craft-plotter"
version = "0.1.0"
dependencies = [
+ "kurbo 0.13.1",
"serde",
"thiserror 2.0.21",
]
diff --git a/Cargo.toml b/Cargo.toml
index 1400a94a84..6f275a7c3d 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -115,9 +115,14 @@ craft-gif = { path = "crates/gif" }
craft-hdr = { path = "crates/hdr" }
craft-mesh = { path = "crates/mesh" }
craft-html = { path = "crates/html" }
+craft-ipp = { path = "crates/ipp" }
craft-heif = { path = "crates/heif" }
craft-fig = { path = "crates/fig" }
craft-fonts-build = { path = "crates/fonts-build" }
+craft-gpu = { path = "crates/gpu" }
+craft-gpu-null = { path = "crates/gpu-null" }
+craft-gpu-providers = { path = "crates/gpu-providers" }
+craft-gpu-wgpu = { path = "crates/gpu-wgpu" }
craft-i18n = { path = "crates/i18n" }
craft-ipc = { path = "crates/ipc" }
craft-idml = { path = "crates/idml" }
@@ -232,6 +237,9 @@ proptest = "1"
encoding_rs = "0.8"
# All date/time conversion goes through chrono (no hand-rolled calendar code beyond the 400-year range extension; AGENTS.md).
chrono = { version = "0.4.45", default-features = false, features = ["std"] }
+# Foundation crates for craft-gpu-wgpu. The versions match the ones the app repos already pin.
+pollster = "0.4"
+wgpu = { version = "30", default-features = false, features = ["std", "wgsl"] }
[workspace.lints.rust]
unsafe_code = "forbid"
diff --git a/README.md b/README.md
index f6d3e55763..3f90f0b275 100644
--- a/README.md
+++ b/README.md
@@ -2,6 +2,7 @@ Craft App Libraries
===================
+
This repo contains libraries used by the various Craft Apps. It's a Rust monorepo workspace, and the libraries can be included by any Craft Apps that need them.
diff --git a/crates/cms/src/lib.rs b/crates/cms/src/lib.rs
index 38f926dcf1..ab3427744d 100644
--- a/crates/cms/src/lib.rs
+++ b/crates/cms/src/lib.rs
@@ -31,6 +31,7 @@ pub mod lutfile;
pub mod math;
pub mod named;
mod normalize;
+pub mod palette;
pub mod pipeline;
pub mod profile;
pub mod sony_lut;
diff --git a/crates/cms/src/palette.rs b/crates/cms/src/palette.rs
new file mode 100644
index 0000000000..eb2c2fcdac
--- /dev/null
+++ b/crates/cms/src/palette.rs
@@ -0,0 +1,681 @@
+//! Palette I/O for `.gpl` (GIMP Palette), `.css` (CSS custom properties, write-only),
+//! `.ase` (Adobe Swatch Exchange, binary), and `.acb` (Adobe Color Book, read-only).
+
+use std::collections::{HashMap, HashSet};
+
+/// A colour in a palette swatch.
+#[derive(Clone, Copy, Debug, PartialEq)]
+pub enum PaletteColor {
+ Rgb { r: f32, g: f32, b: f32 },
+ Cmyk { c: f32, m: f32, y: f32, k: f32 },
+ Lab { l: f32, a: f32, b: f32 },
+ Gray { k: f32 },
+}
+
+impl PaletteColor {
+ pub fn rgb(r: f32, g: f32, b: f32) -> Self {
+ Self::Rgb { r, g, b }
+ }
+
+ pub fn rgb8(r: u8, g: u8, b: u8) -> Self {
+ Self::Rgb { r: f32::from(r) / 255.0, g: f32::from(g) / 255.0, b: f32::from(b) / 255.0 }
+ }
+
+ pub fn cmyk(c: f32, m: f32, y: f32, k: f32) -> Self {
+ Self::Cmyk { c, m, y, k }
+ }
+
+ pub fn lab(l: f32, a: f32, b: f32) -> Self {
+ Self::Lab { l, a, b }
+ }
+
+ pub fn gray(k: f32) -> Self {
+ Self::Gray { k }
+ }
+
+ /// Approximate 8-bit sRGB representation for formats that only store RGB (`.gpl`, `.css`).
+ pub fn to_rgb8(self) -> [u8; 3] {
+ let to_u8 = |v: f32| (v.clamp(0.0, 1.0) * 255.0).round() as u8;
+ match self {
+ Self::Rgb { r, g, b } => [to_u8(r), to_u8(g), to_u8(b)],
+ Self::Gray { k } => {
+ let v = to_u8(1.0 - k.clamp(0.0, 1.0));
+ [v, v, v]
+ }
+ Self::Cmyk { c, m, y, k } => {
+ let (c, m, y, k) = (c.clamp(0.0, 1.0), m.clamp(0.0, 1.0), y.clamp(0.0, 1.0), k.clamp(0.0, 1.0));
+ [to_u8((1.0 - c) * (1.0 - k)), to_u8((1.0 - m) * (1.0 - k)), to_u8((1.0 - y) * (1.0 - k))]
+ }
+ Self::Lab { l, a, b } => lab_to_srgb8(l, a, b),
+ }
+ }
+}
+
+fn lab_to_srgb8(l: f32, a: f32, b: f32) -> [u8; 3] {
+ let fy = (l + 16.0) / 116.0;
+ let fx = a / 500.0 + fy;
+ let fz = fy - b / 200.0;
+ let inv = |t: f32| {
+ if t > 6.0 / 29.0 { t * t * t } else { 3.0 * (6.0 / 29.0) * (6.0 / 29.0) * (t - 4.0 / 29.0) }
+ };
+ // D65 white point
+ let x = 0.95047 * inv(fx);
+ let y = 1.00000 * inv(fy);
+ let z = 1.08883 * inv(fz);
+ let rl = 3.2404542 * x - 1.5371385 * y - 0.4985314 * z;
+ let gl = -0.969_266 * x + 1.8760108 * y + 0.0415560 * z;
+ let bl = 0.0556434 * x - 0.2040259 * y + 1.0572252 * z;
+ let gamma = |u: f32| {
+ let u = u.clamp(0.0, 1.0);
+ let s = if u <= 0.0031308 { 12.92 * u } else { 1.055 * u.powf(1.0 / 2.4) - 0.055 };
+ (s.clamp(0.0, 1.0) * 255.0).round() as u8
+ };
+ [gamma(rl), gamma(gl), gamma(bl)]
+}
+
+/// A single named colour swatch in a [`Palette`].
+#[derive(Clone, Debug, PartialEq)]
+pub struct PaletteSwatch {
+ pub name: String,
+ pub color: PaletteColor,
+ pub global: bool,
+ pub spot: bool,
+}
+
+/// A named group of swatches in a [`Palette`].
+#[derive(Clone, Debug, Default, PartialEq)]
+pub struct PaletteGroup {
+ pub name: String,
+ pub swatches: Vec,
+}
+
+/// A colour palette / swatch library.
+#[derive(Clone, Debug, Default, PartialEq)]
+pub struct Palette {
+ pub name: String,
+ pub swatches: Vec,
+ pub groups: Vec,
+}
+
+/// Supported palette file formats for writing.
+#[derive(Clone, Copy, Debug, PartialEq, Eq)]
+pub enum PaletteFormat {
+ /// `.gpl`: plain-text 8-bit RGB palette.
+ Gpl,
+ /// `.ase`: binary Adobe Swatch Exchange.
+ Ase,
+ /// `.css`: CSS custom properties on `:root` (write-only).
+ Css,
+}
+
+impl PaletteFormat {
+ pub const ALL: [PaletteFormat; 3] = [PaletteFormat::Gpl, PaletteFormat::Ase, PaletteFormat::Css];
+
+ /// The format's id / file extension.
+ pub fn id(self) -> &'static str {
+ match self {
+ PaletteFormat::Gpl => "gpl",
+ PaletteFormat::Ase => "ase",
+ PaletteFormat::Css => "css",
+ }
+ }
+
+ /// Parse a format id or extension (case-insensitive, leading dot allowed).
+ pub fn parse(s: &str) -> Option {
+ let s = s.trim_start_matches('.').to_ascii_lowercase();
+ Self::ALL.into_iter().find(|f| f.id() == s)
+ }
+
+ pub fn readable(self) -> bool {
+ self != PaletteFormat::Css
+ }
+
+ pub fn binary(self) -> bool {
+ self == PaletteFormat::Ase
+ }
+}
+
+const GPL_HEADER: &str = "GIMP Palette";
+const GPL_GROUP: &str = "# Group:";
+const ASE_SIGNATURE: &[u8] = b"ASEF";
+const ASE_GROUP_START: u16 = 0xC001;
+const ASE_GROUP_END: u16 = 0xC002;
+const ASE_COLOR: u16 = 0x0001;
+const ASE_MAX_ENTRIES: usize = 100_000;
+const ASE_MAX_NAME: usize = 1024;
+const ASE_CUT_SHORT: &str = "the swatch exchange file is cut short";
+const ACB_SIGNATURE: &[u8] = b"8BCB";
+const ACB_CUT_SHORT: &str = "the color book is cut short";
+const ACB_MAX_NAME_BYTES: usize = 64 << 20;
+
+/// Write `palette` in `format`.
+pub fn write_palette(palette: &Palette, format: PaletteFormat) -> Result, String> {
+ match format {
+ PaletteFormat::Gpl => Ok(write_gpl(palette).into_bytes()),
+ PaletteFormat::Ase => write_ase(palette),
+ PaletteFormat::Css => Ok(write_css(palette).into_bytes()),
+ }
+}
+
+/// Read a palette from `bytes` (`.ase`, `.acb`, or `.gpl`, detected from content).
+/// `fallback_name` is used when the file does not specify a library name.
+pub fn read_palette(bytes: &[u8], fallback_name: &str) -> Result {
+ if bytes.starts_with(ASE_SIGNATURE) {
+ return read_ase(bytes, fallback_name);
+ }
+ if bytes.starts_with(ACB_SIGNATURE) {
+ return read_acb(bytes, fallback_name).map_err(|e| if e == ASE_CUT_SHORT { ACB_CUT_SHORT.into() } else { e });
+ }
+ let text = String::from_utf8_lossy(bytes);
+ let text = text.trim_start_matches('\u{feff}').trim_start();
+ if text.starts_with(GPL_HEADER) {
+ return Ok(read_gpl(text, fallback_name));
+ }
+ Err("not a supported palette (.gpl, .ase, or .acb)".into())
+}
+
+/// Returns `true` if `bytes` looks like a `.gpl`, `.ase`, or `.acb` palette.
+pub fn sniff_palette(bytes: &[u8]) -> bool {
+ if bytes.starts_with(ASE_SIGNATURE) || bytes.starts_with(ACB_SIGNATURE) {
+ return true;
+ }
+ let prefix = bytes.get(..1024).unwrap_or(bytes);
+ let text = String::from_utf8_lossy(prefix);
+ text.trim_start_matches('\u{feff}').trim_start().starts_with(GPL_HEADER)
+}
+
+fn one_line(s: &str) -> String {
+ s.split(['\n', '\r', '\t']).collect::>().join(" ").trim().to_string()
+}
+
+fn write_gpl(lib: &Palette) -> String {
+ let mut out = format!("{GPL_HEADER}\nName: {}\nColumns: 0\n#\n", one_line(&lib.name));
+ let colors = |out: &mut String, list: &[PaletteSwatch]| {
+ for w in list {
+ let [r, g, b] = w.color.to_rgb8();
+ out.push_str(&format!("{r:3} {g:3} {b:3}\t{}\n", one_line(&w.name)));
+ }
+ };
+ colors(&mut out, &lib.swatches);
+ for g in &lib.groups {
+ out.push_str(&format!("{GPL_GROUP} {}\n", one_line(&g.name)));
+ colors(&mut out, &g.swatches);
+ }
+ out
+}
+
+/// `name` as a CSS custom property (`--` and an escaped identifier): lower case, spaces as
+/// hyphens, other characters an identifier can't hold escaped with a backslash.
+pub fn css_property(name: &str) -> String {
+ let mut out = String::from("--");
+ for word in name.split_whitespace() {
+ if out.len() > 2 {
+ out.push('-');
+ }
+ for ch in word.chars().flat_map(char::to_lowercase) {
+ if ch.is_ascii_alphanumeric() || ch == '-' || ch == '_' || !ch.is_ascii() {
+ out.push(ch);
+ } else if ch.is_ascii_graphic() {
+ out.push('\\');
+ out.push(ch);
+ } else {
+ out.push_str(&format!("\\{:x} ", ch as u32));
+ }
+ }
+ }
+ if out.len() == 2 {
+ out.push_str("swatch");
+ }
+ out
+}
+
+fn css_comment(s: &str) -> String {
+ one_line(s).replace("*/", "* /")
+}
+
+fn write_css(lib: &Palette) -> String {
+ let mut out = format!("/* {} */\n:root {{\n", css_comment(&lib.name));
+ let mut taken: HashSet = HashSet::new();
+ let mut next: HashMap = HashMap::new();
+ let mut props = |out: &mut String, list: &[PaletteSwatch]| {
+ for w in list {
+ let [r, g, b] = w.color.to_rgb8();
+ let value = format!("#{r:02x}{g:02x}{b:02x}");
+ let base = css_property(&w.name);
+ let name = if taken.insert(base.clone()) {
+ base
+ } else {
+ let n = next.entry(base.clone()).or_insert(1);
+ loop {
+ *n += 1;
+ let candidate = format!("{base}-{n}");
+ if taken.insert(candidate.clone()) {
+ break candidate;
+ }
+ }
+ };
+ let note = if w.spot { " /* spot */" } else { "" };
+ out.push_str(&format!(" {name}: {value};{note}\n"));
+ }
+ };
+ props(&mut out, &lib.swatches);
+ for g in &lib.groups {
+ out.push_str(&format!(" /* {} */\n", css_comment(&g.name)));
+ props(&mut out, &g.swatches);
+ }
+ out.push_str("}\n");
+ out
+}
+
+fn write_ase(lib: &Palette) -> Result, String> {
+ let entries = lib.swatches.len() + lib.groups.iter().map(|g| 1 + g.swatches.len()).sum::();
+ if entries > ASE_MAX_ENTRIES {
+ return Err(format!("at most {ASE_MAX_ENTRIES} colors and groups are supported in a swatch exchange file, and this library has {entries}"));
+ }
+ let mut body = vec![];
+ let mut blocks = put_ase_colors(&mut body, &lib.swatches)?;
+ for g in &lib.groups {
+ let mut name = vec![];
+ put_ase_name(&mut name, &g.name)?;
+ put_ase_block(&mut body, ASE_GROUP_START, &name)?;
+ blocks += 2 + put_ase_colors(&mut body, &g.swatches)?;
+ put_ase_block(&mut body, ASE_GROUP_END, &[])?;
+ }
+ let blocks = u32::try_from(blocks).map_err(|_| "too many swatch exchange blocks".to_string())?;
+ let mut out = ASE_SIGNATURE.to_vec();
+ out.extend(1u16.to_be_bytes());
+ out.extend(0u16.to_be_bytes());
+ out.extend(blocks.to_be_bytes());
+ out.extend(body);
+ Ok(out)
+}
+
+fn put_ase_colors(out: &mut Vec, list: &[PaletteSwatch]) -> Result {
+ let unit = |v: f32| if v.is_finite() { v.clamp(0.0, 1.0) } else { 0.0 };
+ let ab = |v: f32| if v.is_finite() { v.clamp(-128.0, 127.0) } else { 0.0 };
+ let mut n = 0;
+ for w in list {
+ let mut body = vec![];
+ put_ase_name(&mut body, &w.name)?;
+ let (model, values): (&[u8; 4], Vec) = match w.color {
+ PaletteColor::Rgb { r, g, b } => (b"RGB ", vec![unit(r), unit(g), unit(b)]),
+ PaletteColor::Cmyk { c, m, y, k } => (b"CMYK", vec![unit(c), unit(m), unit(y), unit(k)]),
+ PaletteColor::Lab { l, a, b } => (b"LAB ", vec![unit(l / 100.0), ab(a), ab(b)]),
+ PaletteColor::Gray { k } => (b"Gray", vec![1.0 - unit(k)]),
+ };
+ body.extend(model);
+ body.extend(values.iter().flat_map(|v| v.to_be_bytes()));
+ let kind: u16 = if w.spot {
+ 1
+ } else if w.global {
+ 0
+ } else {
+ 2
+ };
+ body.extend(kind.to_be_bytes());
+ put_ase_block(out, ASE_COLOR, &body)?;
+ n += 1;
+ }
+ Ok(n)
+}
+
+fn put_ase_block(out: &mut Vec, kind: u16, body: &[u8]) -> Result<(), String> {
+ let len = u32::try_from(body.len()).map_err(|_| "a swatch exchange block is too long".to_string())?;
+ out.extend(kind.to_be_bytes());
+ out.extend(len.to_be_bytes());
+ out.extend_from_slice(body);
+ Ok(())
+}
+
+fn put_ase_name(out: &mut Vec, name: &str) -> Result<(), String> {
+ let mut units: Vec = vec![];
+ for c in name.chars().filter(|&c| c != '\0') {
+ let mut buf = [0; 2];
+ let encoded = c.encode_utf16(&mut buf);
+ if units.len() + encoded.len() > ASE_MAX_NAME {
+ break;
+ }
+ units.extend_from_slice(encoded);
+ }
+ units.push(0);
+ let count = u16::try_from(units.len()).map_err(|_| "a swatch exchange name is too long".to_string())?;
+ out.extend(count.to_be_bytes());
+ out.extend(units.iter().flat_map(|u| u.to_be_bytes()));
+ Ok(())
+}
+
+fn read_gpl(text: &str, fallback: &str) -> Palette {
+ let mut lib = Palette { name: fallback.into(), ..Default::default() };
+ let mut names = UniqueNames::default();
+ for line in text.lines().skip(1) {
+ let line = line.trim();
+ if let Some(g) = line.strip_prefix(GPL_GROUP) {
+ lib.groups.push(PaletteGroup { name: names.unique(group_name(g)), swatches: vec![] });
+ continue;
+ }
+ if let Some(n) = line.strip_prefix("Name:") {
+ if !n.trim().is_empty() {
+ lib.name = n.trim().to_string();
+ }
+ continue;
+ }
+ let words: Vec<&str> = line.split_whitespace().collect();
+ let rgb: Vec = words.iter().take(3).map_while(|v| v.parse().ok()).collect();
+ let [r, g, b] = rgb[..] else { continue };
+ let rest = words[3..].join(" ");
+ let base = if rest.is_empty() { format!("R={r} G={g} B={b}") } else { rest };
+ let w = PaletteSwatch { name: names.unique(base), color: PaletteColor::rgb8(r, g, b), global: false, spot: false };
+ match lib.groups.last_mut() {
+ Some(grp) => grp.swatches.push(w),
+ None => lib.swatches.push(w),
+ }
+ }
+ lib
+}
+
+fn read_ase(bytes: &[u8], fallback: &str) -> Result {
+ let mut data = AseData(bytes);
+ data.bytes(ASE_SIGNATURE.len())?;
+ let (major, minor) = (data.u16()?, data.u16()?);
+ if major != 1 {
+ return Err(format!("swatch exchange version {major}.{minor} isn't supported (only 1.x)"));
+ }
+ let blocks = data.u32()?;
+ let mut lib = Palette { name: fallback.into(), ..Default::default() };
+ let mut names = UniqueNames::default();
+ let (mut in_group, mut entries) = (false, 0usize);
+ for _ in 0..blocks {
+ let kind = data.u16()?;
+ let len = usize::try_from(data.u32()?).unwrap_or(usize::MAX);
+ let mut body = AseData(data.bytes(len)?);
+ if matches!(kind, ASE_GROUP_START | ASE_COLOR) {
+ entries += 1;
+ if entries > ASE_MAX_ENTRIES {
+ return Err(format!("the swatch exchange file holds more than {ASE_MAX_ENTRIES} colors and groups"));
+ }
+ }
+ match kind {
+ ASE_GROUP_START => {
+ lib.groups.push(PaletteGroup { name: names.unique(group_name(&body.name()?)), swatches: vec![] });
+ in_group = true;
+ }
+ ASE_GROUP_END => in_group = false,
+ ASE_COLOR => {
+ let Some(mut w) = ase_swatch(&mut body)? else { continue };
+ w.name = names.unique(std::mem::take(&mut w.name));
+ match lib.groups.last_mut().filter(|_| in_group) {
+ Some(g) => g.swatches.push(w),
+ None => lib.swatches.push(w),
+ }
+ }
+ _ => {}
+ }
+ }
+ Ok(lib)
+}
+
+fn read_acb(bytes: &[u8], fallback: &str) -> Result {
+ let mut data = AseData(bytes);
+ data.bytes(ACB_SIGNATURE.len())?;
+ let version = data.u16()?;
+ if version != 1 {
+ return Err(format!("color book version {version} isn't supported (only 1)"));
+ }
+ let _book_id = data.u16()?;
+ let [title, prefix, suffix, _description] = [data.acb_string()?, data.acb_string()?, data.acb_string()?, data.acb_string()?];
+ let count = usize::from(data.u16()?);
+ if count > ASE_MAX_ENTRIES {
+ return Err(format!("the color book holds more than {ASE_MAX_ENTRIES} colors"));
+ }
+ let (_page_size, _page_offset) = (data.u16()?, data.u16()?);
+ let space = data.u16()?;
+ let mut colors = Vec::with_capacity(count);
+ for _ in 0..count {
+ let name = data.acb_string()?;
+ let _code = data.bytes(6)?;
+ let color = match space {
+ 0 => {
+ let [r, g, b] = data.array::<3>()?;
+ PaletteColor::rgb8(r, g, b)
+ }
+ 2 => {
+ let ink = |v: u8| f32::from(255 - v) / 255.0;
+ let [c, m, y, k] = data.array::<4>()?;
+ PaletteColor::cmyk(ink(c), ink(m), ink(y), ink(k))
+ }
+ 7 => {
+ let [l, a, b] = data.array::<3>()?;
+ PaletteColor::lab(f32::from(l) * 100.0 / 255.0, f32::from(a) - 128.0, f32::from(b) - 128.0)
+ }
+ other => return Err(format!("color books in color space {other} aren't supported (only RGB, CMYK and Lab)")),
+ };
+ colors.push((name, color));
+ }
+ let spot = data.0.windows(8).last() != Some(b"spflproc".as_slice());
+ let mut lib = Palette { name: if title.is_empty() { fallback.into() } else { title }, ..Default::default() };
+ let mut names = UniqueNames::default();
+ let mut kept = 0usize;
+ for (name, color) in colors.into_iter().filter(|(n, _)| !n.trim().is_empty()) {
+ kept = kept.saturating_add(2 * (prefix.len() + name.len() + suffix.len()));
+ if kept > ACB_MAX_NAME_BYTES {
+ return Err(format!("the color book's names take more than {} MB", ACB_MAX_NAME_BYTES >> 20));
+ }
+ let name = names.unique(format!("{prefix}{name}{suffix}").trim().to_string());
+ lib.swatches.push(PaletteSwatch { name, color, global: spot, spot });
+ }
+ Ok(lib)
+}
+
+fn ase_swatch(body: &mut AseData) -> Result