Files
gridcraft/crates/pdf/build.rs
T
Mouhand Kaddo 92ff21b9f2 Shape and preserve Arabic text in PDF exports (#940)
## Summary

Arabic text reached the PDF writer intact, then WinAnsi encoding replaced its characters with question marks. This fixes #923 through the shared PDF text path, including cells, headers and chart labels.

```diff
 PDF text
- encode Arabic through WinAnsi
+ shape with harfrust and Unicode bidi
+ embed licensed glyph outlines with matching weight coordinates
+ preserve logical source using ToUnicode and ActualText
+ fall back to the standard and CID fonts for characters the Arabic face lacks
```

Font binaries stay in the optional craft-fonts build input. Native unbundled builds can use installed Noto Sans Arabic. Explicit cell reading-direction integration follows separately.

**Web build:** shaped Arabic on web needs the bundled craft fonts. The current release workflow doesn't enable `web-craft-fonts`, so until a maintainer enables it, web sets Arabic in the CID font, as main does today, which has no Arabic glyphs, and shows a message that no Noto Sans Arabic face was found. The same applies to a desktop without Noto Sans Arabic, and to bold Arabic where only the regular face is installed.

## Evidence

- **Before:** Arabic exported as `?` (#923, v0.6.1); main now sets it in the CID font, which has no Arabic glyphs, so readers show blanks or boxes.
  **After:** native and bundled exports show joined letters, marks, Western/Arabic digits, bold/italic, wrapping, rotation and chart labels. Fresh `pdfinfo`, `pdffonts` and `pdftotext -bbox` checks accept the files; eight original Arabic strings match extracted logical text exactly.
- At the current head, with Noto Sans Arabic installed and no `CRAFT_FONTS_DIR`: the `gridcraft-pdf`, `gridcraft-engine` and `gridcraft-xlsx` tests, clippy, fmt, layers, assets, parity and the wasm check pass. The full `cargo xtask ci` with the craft-fonts bundle relies on the hosted gate.
- Characters the Arabic face lacks (emoji, Cyrillic) are set like non-Arabic text, and without an Arabic face for the text's weight Arabic takes the CID path main uses, with one message per export. Only a broken font or a size limit fails the export. Size limits cover text, glyph subsets, outlines and page content.

## Merge Danger

**Door:** two-way

**Blast Radius:** printing

`PdfDoc::finish` now returns a Result and every caller handles it. Latin-only rendering stays on its existing path. This is a glyph-outline PDF solution; platform printer and physical screen-reader behavior are not claimed.
2026-11-15 17:29:22 +01:00

89 lines
4.1 KiB
Rust

use std::fmt::Write as _;
fn main() -> Result<(), String> {
arabic_fonts()
}
/// Embed only Arabic fallback faces from the optional, separately licensed font bundle.
fn arabic_fonts() -> Result<(), String> {
use std::path::PathBuf;
println!("cargo::rerun-if-env-changed=CRAFT_FONTS_DIR");
println!("cargo::rerun-if-env-changed=CRAFT_FONTS_REQUIRED");
let required = std::env::var_os("CRAFT_FONTS_REQUIRED").is_some();
let entries = match std::env::var_os("CRAFT_FONTS_DIR").map(PathBuf::from) {
Some(dir) => arabic_font_entries(&dir),
None if required => Err("CRAFT_FONTS_REQUIRED needs CRAFT_FONTS_DIR".into()),
None => Ok(String::new()),
};
let entries = match entries {
Ok(entries) => entries,
Err(e) if required => return Err(e),
Err(e) => {
println!("cargo::warning=building without bundled Arabic fonts: {e}");
String::new()
}
};
let out = PathBuf::from(std::env::var_os("OUT_DIR").unwrap_or_default()).join("arabic_fonts.rs");
std::fs::write(&out, format!("pub(crate) static ARABIC_FONTS: &[(&str, &[u8])] = &[\n{entries}];\n"))
.map_err(|e| format!("writing {}: {e}", out.display()))
}
fn arabic_font_entries(dir: &std::path::Path) -> Result<String, String> {
use sha2::{Digest, Sha256};
use skrifa::MetadataProvider;
let root = dir.canonicalize().map_err(|e| format!("craft-fonts directory: {e}"))?;
let manifest = root.join("fonts/manifest.txt");
println!("cargo::rerun-if-changed={}", manifest.display());
let text = String::from_utf8(read_font_input(&manifest, 1 << 20)?).map_err(|e| e.to_string())?;
let mut out = String::new();
let mut has_regular = false;
for (line_number, line) in text.lines().enumerate() {
let line = line.trim();
if line.is_empty() || line.starts_with('#') {
continue;
}
let fields: Vec<_> = line.split(" | ").map(str::trim).collect();
let [family, style, file, scripts, _licence, licence_file, checksum, _source] = fields.as_slice() else {
return Err(format!("malformed craft-fonts manifest line {}", line_number + 1));
};
if !scripts.split(',').any(|script| script.trim() == "Arab") {
continue;
}
let path = root.join(file).canonicalize().map_err(|e| format!("{file}: {e}"))?;
let licence = root.join(licence_file).canonicalize().map_err(|e| format!("{licence_file}: {e}"))?;
if !path.starts_with(&root) || !licence.starts_with(&root) {
return Err("craft-fonts paths must stay inside the checkout".into());
}
println!("cargo::rerun-if-changed={}", path.display());
println!("cargo::rerun-if-changed={}", licence.display());
let bytes = read_font_input(&path, 64 << 20)?;
if Sha256::digest(&bytes).iter().map(|byte| format!("{byte:02x}")).collect::<String>() != *checksum {
return Err(format!("checksum mismatch for {file}"));
}
let font = skrifa::FontRef::from_index(&bytes, 0).map_err(|e| format!("invalid Arabic font {file}: {e}"))?;
if !"العربية١٢٣".chars().all(|ch| font.charmap().map(ch).is_some()) {
return Err(format!("{file} lacks Arabic glyphs"));
}
let _ = writeln!(out, " ({:?}, include_bytes!({:?})),", format!("craft:{family}:{style}"), path);
has_regular |= *style != "Bold";
}
if out.is_empty() {
return Err("craft-fonts manifest contains no Arabic face; update the checkout".into());
}
if !has_regular {
return Err("craft-fonts manifest has only bold Arabic faces; regular text needs a regular or variable face".into());
}
Ok(out)
}
fn read_font_input(path: &std::path::Path, limit: u64) -> Result<Vec<u8>, String> {
use std::io::Read as _;
let file = std::fs::File::open(path).map_err(|e| format!("{}: {e}", path.display()))?;
let mut bytes = Vec::new();
file.take(limit + 1).read_to_end(&mut bytes).map_err(|e| format!("{}: {e}", path.display()))?;
if bytes.len() as u64 > limit {
return Err(format!("{} exceeds the font input size limit", path.display()));
}
Ok(bytes)
}