diff --git a/.github/workflows/sonarcloud.yml b/.github/workflows/sonarcloud.yml index 283af50e1..e2901225e 100644 --- a/.github/workflows/sonarcloud.yml +++ b/.github/workflows/sonarcloud.yml @@ -23,5 +23,5 @@ jobs: # The codec and format code behind features that are off by default. Without these the # coverage build never compiles it, and new code there reads as untested. cargo-features: >- - craft-video/dnx,craft-video/vp8,craft-video/prores,craft-video/h264,craft-video/hevc,craft-video/vp9,craft-video/av1,craft-video/threads,craft-audio/decode,craft-audio/encode,craft-audio/peaks,craft-audio/rayon,craft-audio/serde,craft-media/matroska,craft-media/ogg,craft-media/bitstream,craft-media/serde,craft-mesh/stl,craft-mesh/obj,craft-mesh/ply,craft-mesh/gltf,craft-metafile/write,craft-image-codecs/sgi + craft-video/dnx,craft-video/vp8,craft-video/prores,craft-video/h264,craft-video/hevc,craft-video/vp9,craft-video/av1,craft-video/threads,craft-audio/decode,craft-audio/encode,craft-audio/peaks,craft-audio/rayon,craft-audio/serde,craft-media/matroska,craft-media/ogg,craft-media/bitstream,craft-media/serde,craft-mesh/stl,craft-mesh/obj,craft-mesh/ply,craft-mesh/gltf,craft-metafile/write,craft-image-codecs/sgi,craft-image-codecs/sunraster secrets: inherit diff --git a/ATTRIBUTION.md b/ATTRIBUTION.md index 43d543062..ab167207c 100644 --- a/ATTRIBUTION.md +++ b/ATTRIBUTION.md @@ -101,4 +101,5 @@ and the exact transfer revision are recorded in each vendored crate's SOURCE.jso `crates/plist` is extracted from `storytold/craft-anything` (`crates/formats/src/plist.rs` and checked byte reads), at source main `88d1d9cac852aec1c7fd97886db451e005e49d2a`. MIT OR Apache-2.0; source license texts are retained in the crate. Primary import Git authors preserve Brandon Thomas, BFlat, RRFZ and Afloat. Regression imports preserve BFlat. XML, JSON projection, calendar formatting and app policy were not imported into the public API. +`crates/tga/src/metadata_tests.rs` and metadata-boundary helpers in `crates/tga/src/lib.rs` are adapted from nuclearspike's FilmCraft #1932 at `e7842f76f887005c460c64a8ab51d59d086e6f27` (MIT OR Apache-2.0). Synthetic fixtures are original contributor test data; primary Git author retained. The XML reader in `crates/plist/src/xml.rs` and its base64 compatibility helper are ported from Craft Anything `8f3b775157d331dd31cfa4da911e26d8856b8c77` (same MIT OR Apache-2.0 source licenses). Brandon Thomas is the primary import author; retained hardening credits include Afloat, BFlat, Rahul, RRFZ and NetterSilver. The finite-real fix preserves gmrmad1 as author from Craft Anything #1905 (`d85e104f`); quoted-attribute/line-ending regressions preserve RRFZ. Bounds/adaptation and bounded entity scanning are subsequent Dylan M. Taylor changes. App-specific JSON projections and keyed-archive interpretation remain outside this crate. diff --git a/Cargo.lock b/Cargo.lock index d606ce018..33f71e63b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1785,9 +1785,12 @@ name = "craft-font-catalog" version = "0.1.0" dependencies = [ "fontique", + "harfrust 0.13.3", "kurbo 0.13.1", "skrifa 0.47.0", "thiserror 2.0.21", + "unicode-bidi", + "unicode-segmentation", "windows-registry", "write-fonts 0.54.1", ] @@ -2022,6 +2025,7 @@ name = "craft-jsonrpc" version = "0.1.0" dependencies = [ "serde_json", + "tokio", ] [[package]] @@ -2657,6 +2661,10 @@ dependencies = [ name = "craft-sheet" version = "0.1.0" +[[package]] +name = "craft-shx" +version = "0.1.0" + [[package]] name = "craft-silhouette-studio" version = "0.1.0" @@ -6140,6 +6148,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8ea1f30cedd69f0a2954655f7188c6a834246d2bcf1e315e2ac40c4b24dc9519" dependencies = [ "cfg-if", + "rayon", ] [[package]] @@ -7977,6 +7986,7 @@ dependencies = [ "loop9", "quick-error 2.0.1", "rav1e", + "rayon", "rgb", ] @@ -9920,9 +9930,21 @@ dependencies = [ "mio", "pin-project-lite", "socket2", + "tokio-macros", "windows-sys 0.61.2", ] +[[package]] +name = "tokio-macros" +version = "2.7.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "78773a2a397f451582ce068015985c33193cf6dea8b74d2a639fe457b2f07b0e" +dependencies = [ + "proc-macro2", + "quote", + "syn 3.0.6", +] + [[package]] name = "tokio-rustls" version = "0.26.6" diff --git a/Cargo.toml b/Cargo.toml index 64099b941..bd08fa725 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,6 +11,7 @@ rust-version = "1.95" repository = "https://github.com/storytold/craft-libs" [workspace.dependencies] +craft-shx = { path = "crates/shx" } craft-http-url = { path = "crates/http-url" } craft-usd = { path = "crates/usd" } craft-smime = { path = "crates/smime" } diff --git a/crates/cms/src/lib.rs b/crates/cms/src/lib.rs index e1841d2e7..2f4abdc50 100644 --- a/crates/cms/src/lib.rs +++ b/crates/cms/src/lib.rs @@ -29,6 +29,7 @@ pub mod lut3d; pub mod lutfile; pub mod math; pub mod named; +mod normalize; pub mod pipeline; pub mod profile; pub mod sony_lut; @@ -45,6 +46,7 @@ pub use gamut::GamutCheck; pub use gpu_matrix_shaper::GpuMatrixShaper; pub use lut3d::Lut3d; pub use named::{NamedColor, NamedColors}; +pub use normalize::normalize_for_embedding; pub use profile::{ColorSpace, Pcs, Profile, ProfileClass}; pub use transform::{SampleKind, Transform, TransformOptions, black_point, cached}; pub use write::profile_id; diff --git a/crates/cms/src/normalize.rs b/crates/cms/src/normalize.rs new file mode 100644 index 000000000..85d690783 --- /dev/null +++ b/crates/cms/src/normalize.rs @@ -0,0 +1,142 @@ +//! Bounded encoded ICC layout normalization, originating in LightCraft. + +use std::collections::BTreeMap; + +/// Zero the creation date, align tag payloads to four bytes, preserve shared payloads, +/// update the profile length and clear its stale profile ID. No colour tags are interpreted. +/// +/// This is a layout operation, not profile validation. Malformed tables or layouts exceeding +/// 4096 tags or 64 MiB output are returned unchanged except for the creation date. The owned +/// input is reused on failure, so rejected input does not require an additional full copy. +/// Consumers needing rejection of invalid profiles must separately use `Profile::parse`. +pub fn normalize_for_embedding(mut bytes: Vec) -> Vec { + if let Some(date) = bytes.get_mut(24..36) { + date.fill(0); + } + align(&bytes).unwrap_or(bytes) +} + +const MAX_BYTES: usize = 64 * 1024 * 1024; +const MAX_TAGS: usize = 4096; + +fn be(bytes: &[u8], at: usize) -> Option { + let s = bytes.get(at..at.checked_add(4)?)?; + usize::try_from(u32::from_be_bytes([s[0], s[1], s[2], s[3]])).ok() +} + +fn align(bytes: &[u8]) -> Option> { + if bytes.len() > MAX_BYTES { + return None; + } + let count = be(bytes, 128)?; + if count > MAX_TAGS { + return None; + } + let end = 132usize.checked_add(count.checked_mul(12)?)?; + bytes.get(..end)?; + let mut placements = BTreeMap::new(); + let mut offsets = Vec::new(); + offsets.try_reserve_exact(count).ok()?; + let mut size = end; + for index in 0..count { + let entry = 132 + index * 12; + let off = be(bytes, entry + 4)?; + let len = be(bytes, entry + 8)?; + if off < end { + return None; + } + bytes.get(off..off.checked_add(len)?)?; + let new_off = if let Some(&placed) = placements.get(&(off, len)) { + placed + } else { + let aligned = size.checked_add(3)? & !3; + size = aligned.checked_add(len)?; + if size > MAX_BYTES { + return None; + } + placements.insert((off, len), aligned); + aligned + }; + offsets.push(new_off); + } + size = size.checked_add(3)? & !3; + if size > MAX_BYTES { + return None; + } + let mut out = Vec::new(); + out.try_reserve_exact(size).ok()?; + out.resize(size, 0); + out[..end].copy_from_slice(&bytes[..end]); + for ((off, len), target) in placements { + out[target..target + len].copy_from_slice(&bytes[off..off + len]); + } + for (index, off) in offsets.into_iter().enumerate() { + let field = 132 + index * 12 + 4; + out[field..field + 4].copy_from_slice(&u32::try_from(off).ok()?.to_be_bytes()); + } + out[..4].copy_from_slice(&u32::try_from(size).ok()?.to_be_bytes()); + out[84..100].fill(0); + Some(out) +} + +#[cfg(test)] +#[allow(clippy::unwrap_used, clippy::expect_used, clippy::panic)] +#[path = "normalize_legacy_tests.rs"] +mod legacy; + +#[cfg(test)] +#[allow(clippy::unwrap_used, clippy::expect_used, clippy::panic)] +mod tests { + use super::*; + + #[test] + fn matches_frozen_writer_and_preserves_aliases() { + for count in 0..20usize { + let end = 132 + count * 12; + let mut bytes = vec![17; end + 31]; + bytes[128..132].copy_from_slice(&(count as u32).to_be_bytes()); + for index in 0..count { + let field = 132 + index * 12 + 4; + let off = end + index % 3; + bytes[field..field + 4].copy_from_slice(&(off as u32).to_be_bytes()); + bytes[field + 4..field + 8].copy_from_slice(&7u32.to_be_bytes()); + } + let expected = legacy::align_tags(&legacy::fixed_date(bytes.clone())); + let result = normalize_for_embedding(bytes); + assert_eq!(result, expected); + assert_eq!(normalize_for_embedding(result.clone()), result); + } + } + + #[test] + fn real_profile_preserves_color_tags_and_legacy_bytes() { + let encoded = crate::Builtin::Srgb.profile().clone().with_encoded_bytes(); + let input = encoded.to_bytes().to_vec(); + let normalized = normalize_for_embedding(input.clone()); + assert_eq!(normalized, legacy::align_tags(&legacy::fixed_date(input))); + assert_eq!(&normalized[24..36], &[0; 12]); + assert_eq!(&normalized[84..100], &[0; 16]); + assert!(crate::Profile::parse(&normalized).is_ok()); + for index in 0..be(&normalized, 128).unwrap() { + assert_eq!(be(&normalized, 136 + index * 12).unwrap() % 4, 0); + } + } + + #[test] + fn hostile_tables_return_owned_input_without_expansion() { + for len in 0..132 { + let bytes = vec![1; len]; + assert_eq!(normalize_for_embedding(bytes.clone()), legacy::fixed_date(bytes)); + } + for count in [4097u32, u32::MAX] { + let mut bytes = vec![1; 144]; + bytes[128..132].copy_from_slice(&count.to_be_bytes()); + assert_eq!(normalize_for_embedding(bytes.clone()), legacy::fixed_date(bytes)); + } + let mut bytes = vec![1; 144]; + bytes[128..132].copy_from_slice(&1u32.to_be_bytes()); + bytes[136..140].copy_from_slice(&u32::MAX.to_be_bytes()); + bytes[140..144].copy_from_slice(&u32::MAX.to_be_bytes()); + assert_eq!(normalize_for_embedding(bytes.clone()), legacy::fixed_date(bytes)); + } +} diff --git a/crates/cms/src/normalize_legacy_tests.rs b/crates/cms/src/normalize_legacy_tests.rs new file mode 100644 index 000000000..dae5c2770 --- /dev/null +++ b/crates/cms/src/normalize_legacy_tests.rs @@ -0,0 +1,43 @@ +//! Encoded ICC normalization extracted from LightCraft. +pub(super) fn align_tags(b: &[u8]) -> Vec { + let be = |i: usize| b.get(i..i + 4).map(|s| u32::from_be_bytes([s[0], s[1], s[2], s[3]]) as usize); + let Some(n) = be(128) else { return b.to_vec() }; + let table_end = 132 + 12 * n; + let tags: Option> = (0..n).map(|t| Some((be(132 + 12 * t + 4)?, be(132 + 12 * t + 8)?))).collect(); + let Some(tags) = tags.filter(|t| table_end <= b.len() && t.iter().all(|&(o, l)| o >= table_end && o + l <= b.len())) else { + return b.to_vec(); + }; + let mut out = b[..table_end].to_vec(); + let mut placed: Vec<((usize, usize), usize)> = Vec::new(); + for (t, &(off, len)) in tags.iter().enumerate() { + let new_off = match placed.iter().find(|(k, _)| *k == (off, len)) { + // tags sharing data keep sharing it + Some(&(_, o)) => o, + None => { + while !out.len().is_multiple_of(4) { + out.push(0); + } + let o = out.len(); + out.extend_from_slice(&b[off..off + len]); + placed.push(((off, len), o)); + o + } + }; + out[132 + 12 * t + 4..132 + 12 * t + 8].copy_from_slice(&(new_off as u32).to_be_bytes()); + } + while !out.len().is_multiple_of(4) { + out.push(0); + } + let size = out.len() as u32; + out[0..4].copy_from_slice(&size.to_be_bytes()); + // the profile ID (MD5) would no longer match: clear it (= "not computed") + out[84..100].fill(0); + out +} + +pub(super) fn fixed_date(mut b: Vec) -> Vec { + if let Some(date) = b.get_mut(24..36) { + date.fill(0); + } + b +} diff --git a/crates/dpx/src/lib.rs b/crates/dpx/src/lib.rs index a762566f8..26d9e8656 100644 --- a/crates/dpx/src/lib.rs +++ b/crates/dpx/src/lib.rs @@ -34,6 +34,9 @@ //! Input is treated as hostile: every header read is bounds-checked, the picture size is capped //! by [`Limits`] and the image data must be present in full before anything is allocated. +/// RGBA8-to-10-bit DPX encoding. +pub mod rgba8_dpx10; + #[cfg(test)] mod tests; diff --git a/crates/dpx/src/rgba8_dpx10.rs b/crates/dpx/src/rgba8_dpx10.rs new file mode 100644 index 000000000..6554a1eb8 --- /dev/null +++ b/crates/dpx/src/rgba8_dpx10.rs @@ -0,0 +1,110 @@ +//! Uncompressed RGB(A) output from top-down RGBA8 pixels. +/// Why the supplied frame cannot be encoded. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum Rgba8EncodeError { + /// Empty, overflowing, or mismatched dimensions. + FrameSize, + /// A dimension cannot fit the output format. + TooLarge, + /// The output buffer could not be allocated. + OutOfMemory, +} +fn checked_len(rgba: &[u8], w: u32, h: u32) -> std::result::Result { + let n = (w as usize).checked_mul(h as usize).and_then(|n| n.checked_mul(4)).ok_or(Rgba8EncodeError::FrameSize)?; + if w == 0 || h == 0 || rgba.len() != n { + return Err(Rgba8EncodeError::FrameSize); + } + Ok(n) +} +/// Header size of the DPX files we write (file + image + orientation + film + TV headers). +const DPX_HEADER: usize = 2048; + +/// A DPX file: 10-bit RGB (8-bit values widened by bit replication, so 0 → 0 and 255 → 1023). +pub fn encode_rgba8_dpx10(rgba: &[u8], w: u32, h: u32, creator: &str) -> std::result::Result, Rgba8EncodeError> { + let n = checked_len(rgba, w, h)?; + // one 32-bit word per pixel; `n` is already `w * h * 4` + let size = n.checked_add(DPX_HEADER).ok_or(Rgba8EncodeError::FrameSize)?; + let file_size = u32::try_from(size).map_err(|_| Rgba8EncodeError::TooLarge)?; + // SMPTE ST 268: undefined numeric fields are all ones, undefined text fields are NUL + let mut hd = vec![0xFF_u8; DPX_HEADER]; + for (a, b) in [ + (36, 660), // file name, creation time, creator, project, copyright + (664, 768), // reserved + (1356, 1408), // reserved + (1432, 1620), // source file name, time, input device, serial number + (1644, 1664), // reserved + (1664, 1712), // film manufacturer, type, offset, prefix, count, format + (1732, 1920), // frame id, slate, reserved + (1972, 2048), // reserved + ] { + hd[a..b].fill(0); + } + for e in 0..8 { + let d = 780 + e * 72 + 40; + hd[d..d + 32].fill(0); // element descriptions + } + let put32 = |hd: &mut Vec, at: usize, v: u32| hd[at..at + 4].copy_from_slice(&v.to_be_bytes()); + let put16 = |hd: &mut Vec, at: usize, v: u16| hd[at..at + 2].copy_from_slice(&v.to_be_bytes()); + // file information header + hd[0..4].copy_from_slice(b"SDPX"); + put32(&mut hd, 4, DPX_HEADER as u32); // offset to the image data + hd[8..16].copy_from_slice(b"V2.0\0\0\0\0"); + put32(&mut hd, 16, file_size); + put32(&mut hd, 20, 1); // ditto key: new frame + put32(&mut hd, 24, 1664); // generic header length + put32(&mut hd, 28, 384); // industry header length + put32(&mut hd, 32, 0); // user data length + let creator = creator.as_bytes(); + let n = creator.len().min(100); + hd[160..160 + n].copy_from_slice(&creator[..n]); + // image information header + put16(&mut hd, 768, 0); // orientation: left to right, top to bottom + put16(&mut hd, 770, 1); // one image element + put32(&mut hd, 772, w); + put32(&mut hd, 776, h); + let el = 780; + put32(&mut hd, el, 0); // unsigned + put32(&mut hd, el + 4, 0); // reference low data code + put32(&mut hd, el + 12, 1023); // reference high data code + hd[el + 20] = 50; // descriptor: RGB + hd[el + 21] = 6; // transfer: ITU-R BT.709 + hd[el + 22] = 6; // colorimetric: ITU-R BT.709 + hd[el + 23] = 10; // bit depth + put16(&mut hd, el + 24, 1); // packing: filled to 32-bit words, method A + put16(&mut hd, el + 26, 0); // no encoding (uncompressed) + put32(&mut hd, el + 28, DPX_HEADER as u32); // offset to the element's data + put32(&mut hd, el + 32, 0); // end-of-line padding + put32(&mut hd, el + 36, 0); // end-of-image padding + // image orientation header: no offset, square pixels + put32(&mut hd, 1408, 0); + put32(&mut hd, 1412, 0); + put32(&mut hd, 1424, w); + put32(&mut hd, 1428, h); + put32(&mut hd, 1628, 1); + put32(&mut hd, 1632, 1); + // television header: progressive + hd[1928] = 0; + // The caller already holds the frame; reserve the output fallibly so a large export reports + // an error instead of aborting the process. + let mut out = Vec::new(); + out.try_reserve_exact(size).map_err(|_| Rgba8EncodeError::OutOfMemory)?; + out.extend_from_slice(&hd); + let ten = |v: u8| (u32::from(v) << 2) | (u32::from(v) >> 6); + for &[r, g, b, _] in rgba.as_chunks::<4>().0 { + out.extend_from_slice(&((ten(r) << 22) | (ten(g) << 12) | (ten(b) << 2)).to_be_bytes()); + } + Ok(out) +} + +#[cfg(test)] +mod tests { + use super::*; + #[test] + fn packed_rgb_and_creator() { + let bytes = encode_rgba8_dpx10(&[255, 0, 128, 255], 1, 1, "FilmCraft").unwrap(); + assert_eq!(&bytes[..4], b"SDPX"); + assert_eq!(&bytes[160..169], b"FilmCraft"); + assert_eq!(&bytes[2048..], &((1023u32 << 22) | (514u32 << 2)).to_be_bytes()); + assert_eq!(encode_rgba8_dpx10(&[], 0, 1, ""), Err(Rgba8EncodeError::FrameSize)); + } +} diff --git a/crates/font-catalog/Cargo.toml b/crates/font-catalog/Cargo.toml index 1f0830a63..d4620862b 100644 --- a/crates/font-catalog/Cargo.toml +++ b/crates/font-catalog/Cargo.toml @@ -15,9 +15,12 @@ default = ["system"] system = ["dep:fontique"] [dependencies] +harfrust = "0.13" kurbo = { workspace = true } skrifa = { version = "0.47", default-features = false, features = ["std"] } thiserror = { workspace = true } +unicode-bidi = "0.3.18" +unicode-segmentation = "1.13" [target.'cfg(not(target_arch = "wasm32"))'.dependencies] fontique = { version = "0.11.1", default-features = false, features = ["system", "fontconfig-dlopen"], optional = true } diff --git a/crates/font-catalog/src/fallback.rs b/crates/font-catalog/src/fallback.rs new file mode 100644 index 000000000..2b0d181d0 --- /dev/null +++ b/crates/font-catalog/src/fallback.rs @@ -0,0 +1,62 @@ +//! Face choice for a grapheme cluster. + +/// Characters requiring their own glyph, after numeral or other caller-supplied mapping. +/// Controls, joiners and variation selectors affect shaping but do not need a separate glyph. +pub fn drawable_characters<'a>(cluster: &'a str, map: impl Fn(char) -> char + Copy + 'a) -> impl Iterator + 'a { + cluster + .chars() + .map(map) + .filter(|&c| !(c.is_control() || matches!(c, '\u{200C}' | '\u{200D}' | '\u{2028}' | '\u{FE00}'..='\u{FE0F}' | '\u{E0100}'..='\u{E01EF}'))) +} + +/// Select one face for a cluster: prefer the primary if it covers every drawable character; +/// otherwise try each character's fallback until a face covers the whole cluster. If none does, +/// use the first character's fallback when the primary lacks it, or keep the primary. +/// The caller controls the font catalog, ordering and style matching. +pub fn cluster_face( + cluster: &str, + primary: &F, + map: impl Fn(char) -> char + Copy, + primary_covers: impl Fn(char) -> bool, + mut fallback: impl FnMut(char) -> Option, + covers: impl Fn(&F, char) -> bool, +) -> F { + let characters = || drawable_characters(cluster, map); + if characters().all(&primary_covers) { + return primary.clone(); + } + for c in characters() { + if let Some(face) = fallback(c) + && characters().all(|ch| covers(&face, ch)) + { + return face; + } + } + if let Some(c) = characters().next() + && !primary_covers(c) + && let Some(face) = fallback(c) + { + return face; + } + primary.clone() +} + +#[cfg(test)] +mod tests { + use super::*; + #[test] + fn chooses_one_face_for_a_base_and_mark() { + let fallback = |c| match c { + 'a' => Some(1), + '\u{301}' => Some(2), + _ => None, + }; + let covers = |face: &u8, c| *face == 2 || (*face == 1 && c == 'a'); + assert_eq!(cluster_face("a\u{301}\u{200d}", &0, |c| c, |_| false, fallback, covers), 2); + assert_eq!(drawable_characters("a\u{200d}\u{fe0f}", |c| c).collect::(), "a"); + } + #[test] + fn uses_first_character_fallback_when_no_face_covers_cluster() { + assert_eq!(cluster_face("ab", &0, |c| c, |_| false, |c| if c == 'a' { Some(1) } else { Some(2) }, |face, c| (*face, c) == (1, 'a')), 1); + } +} diff --git a/crates/font-catalog/src/lib.rs b/crates/font-catalog/src/lib.rs index 3f155d35d..95cd244bd 100644 --- a/crates/font-catalog/src/lib.rs +++ b/crates/font-catalog/src/lib.rs @@ -23,21 +23,33 @@ mod catalog; mod embedding; mod env; +mod fallback; mod fontconfig; +mod ligature; +mod line_break; mod outline; mod registry; mod scan; +mod segments; /// Bounded SFNT face metadata discovery over caller-provided random-access readers. pub mod sfnt; +mod shape; mod suitcase; pub mod system; +mod visual_order; pub use catalog::{Catalog, FontData, FontFileError, Limits, Lister, LoadedFace, Match, Watch}; pub use embedding::{SubsetEmbedding, subset_embedding}; pub use env::{FontEnv, Os, font_dirs, font_store_dir, office_font_dirs}; +pub use fallback::{cluster_face, drawable_characters}; pub use fontconfig::fontconfig_dirs; +pub use ligature::ligature_caret_stops; +pub use line_break::settle_line_break; pub use outline::{FontOutline, GlyphPen, OutlineError}; pub use scan::{FaceInfo, LocalizedName, Source, is_font_file, style_italic, style_weight, style_width}; +pub use segments::SegmentBoundary; +pub use shape::{RunGlyph, RunInput, ShapeRunError, shape_run}; +pub use visual_order::{VisualGlyph, visual_order}; #[cfg(test)] mod tests; diff --git a/crates/font-catalog/src/ligature.rs b/crates/font-catalog/src/ligature.rs new file mode 100644 index 000000000..fa9efe5da --- /dev/null +++ b/crates/font-catalog/src/ligature.rs @@ -0,0 +1,33 @@ +//! Map ligature caret fractions to grapheme boundaries. +use unicode_segmentation::UnicodeSegmentation; + +/// Return logical source-byte caret stops inside a shaped cluster. `font_carets` receives the +/// number of stops and may return their normalized positions along the glyph's logical width. +/// Missing or incorrectly sized font data falls back to equal divisions of the width. +pub fn ligature_caret_stops(text: &str, byte: usize, len: usize, font_carets: impl FnOnce(usize) -> Option>) -> Vec<(usize, f64)> { + let Some(s) = byte.checked_add(len).and_then(|end| text.get(byte..end)).filter(|_| len > 1) else { return Vec::new() }; + let starts: Vec = s.grapheme_indices(true).skip(1).map(|(i, _)| byte + i).collect(); + if starts.is_empty() { + return Vec::new(); + } + let parts = starts.len() + 1; + let mut at = + font_carets(starts.len()).filter(|c| c.len() == starts.len()).unwrap_or_else(|| (1..parts).map(|j| j as f64 / parts as f64).collect()); + at.sort_by(f64::total_cmp); + starts.into_iter().zip(at).collect() +} + +#[cfg(test)] +mod tests { + use super::*; + #[test] + fn maps_graphemes_and_sorts_font_positions() { + assert_eq!(ligature_caret_stops("ábc", 0, 5, |_| Some(vec![0.8, 0.3])), vec![(3, 0.3), (4, 0.8)]); + } + #[test] + fn falls_back_and_rejects_invalid_ranges() { + assert_eq!(ligature_caret_stops("abc", 0, 3, |_| Some(vec![0.5])), vec![(1, 1.0 / 3.0), (2, 2.0 / 3.0)]); + assert!(ligature_caret_stops("abc", usize::MAX, 2, |_| None).is_empty()); + assert!(ligature_caret_stops("é", 1, 1, |_| None).is_empty()); + } +} diff --git a/crates/font-catalog/src/line_break.rs b/crates/font-catalog/src/line_break.rs new file mode 100644 index 000000000..10983d717 --- /dev/null +++ b/crates/font-catalog/src/line_break.rs @@ -0,0 +1,49 @@ +//! Choose a line break after measuring candidate lines as drawn. + +/// Choose the latest candidate that fits when a tab changes the line's drawn width. +/// +/// `breaks` must be sorted by glyph index and the boolean marks hyphenated breaks. +/// `checks` limits the candidates measured individually from the end; older non-hyphenated +/// candidates are bisected, assuming their drawn widths are nondecreasing. If none fits, +/// the earliest eligible break is returned. Without a tab or a measurement callback the +/// original `chosen` break is returned. +pub fn settle_line_break( + tabbed: bool, + checks: usize, + start: usize, + chosen: (usize, bool), + breaks: &[(usize, bool)], + fits: Option<&dyn Fn(usize, bool) -> bool>, +) -> (usize, bool) { + let Some(fits) = fits.filter(|_| tabbed) else { return chosen }; + if fits(chosen.0, chosen.1) { + return chosen; + } + let earlier = breaks.get(breaks.partition_point(|b| b.0 <= start)..breaks.partition_point(|b| b.0 < chosen.0)).unwrap_or_default(); + if let Some(&b) = earlier.iter().rev().take(checks).find(|b| fits(b.0, b.1)) { + return b; + } + let rest = earlier.get(..earlier.len().saturating_sub(checks)).unwrap_or_default(); + let plain: Vec<&(usize, bool)> = rest.iter().filter(|b| !b.1).collect(); + let fitting = plain.partition_point(|b| fits(b.0, b.1)); + fitting.checked_sub(1).and_then(|k| plain.get(k).copied()).or(earlier.first()).copied().unwrap_or(chosen) +} + +#[cfg(test)] +mod tests { + use super::*; + #[test] + fn measures_latest_candidates_and_preserves_hyphenation() { + let breaks = [(1, false), (2, false), (3, true), (4, false)]; + let fits = |i, hyphen| i <= 2 || (i == 3 && hyphen); + assert_eq!(settle_line_break(true, 2, 0, (4, false), &breaks, Some(&fits)), (3, true)); + assert_eq!(settle_line_break(false, 2, 0, (4, false), &breaks, Some(&fits)), (4, false)); + } + #[test] + fn bisects_older_plain_breaks_and_falls_back_to_first_eligible() { + let breaks = [(1, false), (2, true), (3, false), (4, false), (5, false)]; + assert_eq!(settle_line_break(true, 1, 0, (5, false), &breaks, Some(&|i, _| i <= 3)), (3, false)); + assert_eq!(settle_line_break(true, 1, 1, (5, false), &breaks, Some(&|_, _| false)), (2, true)); + assert_eq!(settle_line_break(true, 1, 5, (5, false), &breaks, Some(&|_, _| false)), (5, false)); + } +} diff --git a/crates/font-catalog/src/segments.rs b/crates/font-catalog/src/segments.rs new file mode 100644 index 000000000..e36a05144 --- /dev/null +++ b/crates/font-catalog/src/segments.rs @@ -0,0 +1,55 @@ +//! Boundaries between runs that need separate shaping. + +/// Tracks the properties of the current shaping segment while walking grapheme clusters. +/// The caller resolves font faces, bidi levels and script classification; marks stay with the +/// preceding segment even when their face, level or case differs. +pub struct SegmentBoundary { + face: F, + level: L, + small_caps: bool, + strong_script: Option, +} + +impl SegmentBoundary { + /// Begin at the first segment's face and bidi level. + pub fn new(face: F, level: L) -> Self { + Self { face, level, small_caps: false, strong_script: None } + } + + /// Observe a cluster and return whether it begins a new shaping segment. Pass `None` for + /// common or inherited script and `true` for marks, joiners and variation selectors. + pub fn advance(&mut self, face: F, level: L, small_caps: bool, strong_script: Option, mark: bool) -> bool { + let script_change = matches!((self.strong_script, strong_script), (Some(a), Some(b)) if a != b); + let split = (self.face != face || self.level != level || self.small_caps != small_caps || script_change) && !mark; + if split { + self.face = face; + self.level = level; + self.small_caps = small_caps; + } + if let Some(script) = strong_script { + self.strong_script = Some(script); + } + split + } +} + +#[cfg(test)] +mod tests { + use super::*; + #[test] + fn preserves_marks_and_splits_on_face_direction_case_and_script() { + let mut state = SegmentBoundary::::new(1, 0); + assert!(!state.advance(1, 0, false, Some(1), false)); + assert!(!state.advance(2, 1, true, None, true)); + assert!(state.advance(2, 1, true, Some(2), false)); + assert!(!state.advance(2, 1, true, None, false)); + assert!(state.advance(2, 1, true, Some(1), false)); + } + #[test] + fn strong_script_on_a_mark_updates_the_following_boundary() { + let mut state = SegmentBoundary::::new(1, 0); + assert!(!state.advance(1, 0, false, Some(1), false)); + assert!(!state.advance(1, 0, false, Some(2), true)); + assert!(!state.advance(1, 0, false, Some(2), false)); + } +} diff --git a/crates/font-catalog/src/shape.rs b/crates/font-catalog/src/shape.rs new file mode 100644 index 000000000..966597d3b --- /dev/null +++ b/crates/font-catalog/src/shape.rs @@ -0,0 +1,181 @@ +//! Shape a run with its source clusters and surrounding text context. + +use std::sync::Arc; + +use harfrust::{BufferClusterLevel, Direction, Feature, Language, Script, ShapeOptions, ShapePlan, Shaper, UnicodeBuffer}; + +/// Parameters for a single directional run. Text is in logical order and `source_start` is its +/// byte offset in the complete source. Context influences joining but emits no glyphs. +pub struct RunInput<'a> { + /// Text in this run. + pub text: &'a str, + /// Its byte offset in the complete source. + pub source_start: usize, + /// Text before the run, within the same shaping context. + pub pre_context: &'a str, + /// Text after the run, within the same shaping context. + pub post_context: &'a str, + /// Direction of shaping. + pub direction: Direction, + /// Language used for language-specific forms. + pub language: Option<&'a Language>, + /// OpenType features with ranges in source byte coordinates. + pub features: &'a [Feature], + /// Use character-level instead of monotone grapheme clusters. + pub character_clusters: bool, + /// Uppercase each mapped character without losing its source cluster. + pub uppercase: bool, +} + +/// A glyph in design units, with its source-byte cluster and directional advances and offsets. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub struct RunGlyph { + pub glyph_id: u32, + pub cluster: u32, + pub x_advance: i32, + pub y_advance: i32, + pub x_offset: i32, + pub y_offset: i32, +} + +/// A run's source offsets cannot fit HarfRust's 32-bit clusters. +#[derive(Debug, thiserror::Error)] +#[error("shaping cluster exceeds the 32-bit source offset range")] +pub struct ShapeRunError; + +/// Shape a run using a caller's face-specific plan cache. `map_character` applies digit or other +/// character mapping before case conversion; expansions retain the original byte cluster. +/// The plan callback receives the guessed script, direction, language and feature set so it can +/// reuse plans for the particular shaper and its variable-font instance. +pub fn shape_run( + shaper: &Shaper<'_>, + input: &RunInput<'_>, + mut map_character: impl FnMut(char) -> char, + plan: impl FnOnce(&Shaper<'_>, Direction, Option