Skip to content

DesignCraft: An open-source page and layout design editor written by scratch in pure Rust

Merge smallcap-tounicode: PDF: small caps and substituted figures keep their text

aiongg778a270f484a
diff --cc crates/compose/src/kenten.rs
index c02880ab6,000000000..1053c26b8
mode 100644,000000..100644
--- a/crates/compose/src/kenten.rs
+++ b/crates/compose/src/kenten.rs
@@@ -1,193 -1,0 +1,194 @@@
+//! Kenten (圏点): emphasis marks set beside each character of a run, over it in horizontal text
+//! and to its right in vertical text (where the frame's turn carries "above"), or under it / to
+//! its left. They don't change line breaks or leading.
+//!
+//! Placement follows W3C JLReq (emphasis dots) and JIS X 4051: each mark is centred in its own em
+//! box, that box sits next to the character's em box (plus the kenten distance) and is centred on
+//! the character, or starts with it when the alignment is Start (肩付き).
+
+use designcraft_doc::CharProps;
+use designcraft_fonts::{FaceRef, FontDb, ScopedFonts};
+use designcraft_geom::{Rect, Shape as _};
+
+use crate::{PlacedGlyph, RunStyle, upright_in_vertical};
+
+/// A run's kenten, resolved for layout ([`RunStyle::kenten_mark`]).
+#[derive(Clone, Debug, PartialEq)]
+pub struct KentenMark {
+ /// The mark: one character, or a short custom string.
+ pub text: String,
+ /// Family and style of a custom mark's font (`None` = the character's font).
+ pub font: Option<(String, String)>,
+ /// Size in points, and horizontal / vertical scale (1.0 = 100%).
+ pub size: f64,
+ pub x_scale: f64,
+ pub y_scale: f64,
+ /// Points between the character's em box and the mark's.
+ pub distance: f64,
+ /// Under the character (to its left in vertical text) rather than over it (to its right).
+ pub below: bool,
+ /// At the character's start rather than centred on it.
+ pub start: bool,
+ /// The marks' run style (their colour) in the composed story's style table.
+ pub style: u32,
+}
+
+/// Largest mark size and distance, in points (the largest type size).
+const MAX_POINTS: f64 = 1296.0;
+
+fn finite_or(v: f64, default: f64) -> f64 {
+ if v.is_finite() { v } else { default }
+}
+
+/// The mark size: the set size, else half the text size.
+fn mark_size(p: &CharProps) -> f64 {
+ p.kenten_size.filter(|v| v.is_finite() && *v > 0.0).unwrap_or(finite_or(p.size, 12.0) * 0.5).clamp(0.1, MAX_POINTS)
+}
+
+impl KentenMark {
+ /// The kenten of text formatted with `p`, drawn in run style `style`.
+ pub(crate) fn of(p: &CharProps, style: u32) -> KentenMark {
+ use designcraft_doc::cjk_settings::{KentenAlignment, KentenKind, KentenPosition, kenten_mark};
+ let custom_font = p.kenten_kind == KentenKind::Custom && !p.kenten_font.trim().is_empty();
+ let font_style = if p.kenten_font_style.trim().is_empty() { "Regular".to_string() } else { p.kenten_font_style.clone() };
+ KentenMark {
+ text: kenten_mark(p.kenten_kind, &p.kenten_character),
+ font: custom_font.then(|| (p.kenten_font.clone(), font_style)),
+ size: mark_size(p),
+ x_scale: finite_or(p.kenten_x_scale, 1.0).clamp(0.01, 10.0),
+ y_scale: finite_or(p.kenten_y_scale, 1.0).clamp(0.01, 10.0),
+ distance: finite_or(p.kenten_distance, 0.0).clamp(-MAX_POINTS, MAX_POINTS),
+ below: p.kenten_position == KentenPosition::BelowLeft,
+ start: p.kenten_alignment == KentenAlignment::Start,
+ style,
+ }
+ }
+}
+
+/// The marks' own character format (interned as their run style): Kenten Color over the text's
+/// colour, at the mark size, without the text's decorations.
+pub(crate) fn mark_props(p: &CharProps) -> CharProps {
+ let tint = |t: Option<f32>, text: f32| t.filter(|t| t.is_finite()).map_or(text, |t| t.clamp(0.0, 1.0));
+ let mut m = p.clone();
+ m.kenten = false;
+ m.size = mark_size(p);
+ if !p.kenten_fill.is_empty() {
+ m.fill.clone_from(&p.kenten_fill);
+ }
+ m.fill_tint = tint(p.kenten_fill_tint, p.fill_tint);
+ if !p.kenten_stroke.is_empty() {
+ m.stroke.clone_from(&p.kenten_stroke);
+ }
+ m.stroke_tint = tint(p.kenten_stroke_tint, p.stroke_tint);
+ if let Some(w) = p.kenten_stroke_weight.filter(|w| w.is_finite()) {
+ m.stroke_weight = w.clamp(0.0, 1000.0);
+ }
+ m.underline = false;
+ m.strikethrough = false;
+ m.skew = 0.0;
+ m.shatai_magnification = 0.0;
+ m.ruby = String::new();
+ m.warichu = false;
+ m.xml_tag = String::new();
+ m
+}
+
+/// Add the kenten of a laid-out line's characters. `glyph_fallback`: the document draws missing
+/// glyphs from fallback fonts (a missing font's substitute always does).
+pub(crate) fn annotate(db: &ScopedFonts<'_>, styles: &[RunStyle], line: &mut Vec<PlacedGlyph>, glyph_fallback: bool) {
+ if !line.iter().any(|g| styles.get(g.style as usize).is_some_and(|s| s.kenten_mark.is_some())) {
+ return;
+ }
+ let outlines = FontDb::global();
+ let mut extra = Vec::new();
+ for b in line.iter() {
+ let Some(st) = styles.get(b.style as usize) else { continue };
+ let Some(m) = &st.kenten_mark else { continue };
+ // Characters only: not generated glyphs (ruby, labels), controls or spaces.
+ if b.len == 0 || !b.visible || b.adv <= 0.0 || outlines.outline(&b.face, b.gid).elements().is_empty() {
+ continue;
+ }
+ extra.extend(marks(db, b, m, glyph_fallback || st.missing_font));
+ }
+ line.extend(extra);
+}
+
+/// `text` shaped in `face` at the mark's size and scales, from x = 0 on the baseline.
+fn shape_mark(face: FaceRef, text: &str, m: &KentenMark, b: &PlacedGlyph) -> Vec<PlacedGlyph> {
+ let k = m.size / face.units_per_em().max(1.0);
+ let (sx, sy) = (k * m.x_scale, k * m.y_scale);
+ let mut x = 0.0;
+ let mut out = Vec::new();
+ for sg in designcraft_fonts::shape(&face, text, &[], |c| c) {
+ let ch = text.get(sg.cluster..).and_then(|t| t.chars().next()).unwrap_or(' ');
+ let adv = f64::from(sg.x_advance) * sx;
+ out.push(PlacedGlyph {
+ rendered_char: ch,
+ face,
+ gid: sg.gid,
+ x: x + f64::from(sg.x_offset) * sx,
+ y: -f64::from(sg.y_offset) * sy,
+ adv,
+ sx,
+ sy,
+ style: m.style,
+ byte: b.byte,
+ len: 0,
++ generated_text: None,
+ visible: true,
+ upright: upright_in_vertical(ch),
+ tcy: None,
+ rtl: false,
+ dx: 0.0,
+ });
+ x += adv;
+ }
+ out
+}
+
+/// The marks beside base character `b`. A mark the font lacks is its missing-glyph box, unless
+/// `fallback`: then a fallback font draws it, or failing that a bullet.
+fn marks(db: &ScopedFonts<'_>, b: &PlacedGlyph, m: &KentenMark, fallback: bool) -> Vec<PlacedGlyph> {
+ let mut face = match &m.font {
+ Some((family, style)) => FaceRef::of(&db.face(family, style)),
+ None => b.face,
+ };
+ if fallback
+ && let Some(c) = m.text.chars().next()
+ && !face.covers(c)
+ && let Some(f) = db.fallback_for(c, face.id(), None)
+ {
+ face = FaceRef::of(&f);
+ }
+ let mut glyphs = shape_mark(face, &m.text, m, b);
+ if fallback && glyphs.first().is_some_and(|d| d.gid == 0) {
+ glyphs = shape_mark(b.face, "\u{2022}", m, b);
+ }
+ // The character's em box across the line (y down from the line's baseline).
+ let (em_top, em_bottom) = b.face.em_box();
+ let (base_top, base_bottom) = (b.y - em_top * b.sy, b.y - em_bottom * b.sy);
+ let (w, h) = (m.size * m.x_scale, m.size * m.y_scale);
+ let (box_top, box_bottom) =
+ if m.below { (base_bottom + m.distance, base_bottom + m.distance + h) } else { (base_top - m.distance - h, base_top - m.distance) };
+ let centre_x = if m.start { b.x + w / 2.0 } else { b.x + b.adv / 2.0 };
+ let centre_y = (box_top + box_bottom) / 2.0;
+ // Centre the marks' ink in their em box (a mark's advance and baseline may hold it off centre).
+ let outlines = FontDb::global();
+ let ink = glyphs
+ .iter()
+ .map(|g| {
+ let r = outlines.outline(&g.face, g.gid).bounding_box();
+ Rect::new(g.x + r.x0 * g.sx, g.y + r.y0 * g.sy, g.x + r.x1 * g.sx, g.y + r.y1 * g.sy)
+ })
+ .filter(|r| r.width() > 0.0 && r.height() > 0.0)
+ .reduce(|a, r| a.union(r));
+ let (dx, dy) = match ink {
+ Some(r) => (centre_x - (r.x0 + r.x1) / 2.0, centre_y - (r.y0 + r.y1) / 2.0),
+ None => (centre_x - glyphs.iter().map(|g| g.adv).sum::<f64>() / 2.0, box_bottom),
+ };
+ for g in &mut glyphs {
+ g.x += dx;
+ g.y += dy;
+ }
+ glyphs
+}
diff --cc crates/compose/src/lib.rs
index 8e430e47f,b7ac073fd..05c158184
--- a/crates/compose/src/lib.rs
+++ b/crates/compose/src/lib.rs
@@@ -3052,7 -2414,18 +3054,19 @@@ fn layout_line
}
// One tatweel stretched over the gap (overlapping its neighbours a little).
let k = (l + 0.4) / w;
- out.push(PlacedGlyph { gid, x: at - 0.2, y: -g.shift, adv: 0.0, sx: g.sx * k, len: 0, upright: false, tcy: None, dx: 0.0, ..place(g, at) });
+ out.push(PlacedGlyph {
+ gid,
+ x: at - 0.2,
+ y: -g.shift,
+ adv: 0.0,
+ sx: g.sx * k,
+ len: 0,
+ generated_text: None,
+ upright: false,
+ tcy: None,
++ dx: 0.0,
+ ..place(g, at)
+ });
}
(out, x, ratio)
}
diff --cc crates/compose/src/ruby.rs
index 0cddef5c6,d27ff428e..1afea6b0d
--- a/crates/compose/src/ruby.rs
+++ b/crates/compose/src/ruby.rs
@@@ -405,260 -94,33 +405,261 @@@ pub(crate) fn reserve(db: &ScopedFonts<
i += 1;
continue;
};
- // The group: following glyphs with the same ruby.
+ let missing = st.missing_font;
let mut j = i + 1;
- while j < line.len() && (line[j].len == 0 || style(&line[j]).is_some_and(|s| s.ruby.as_ref() == Some(text))) {
+ while glyphs.get(j).is_some_and(|g| in_group(styles.as_slice(), g, (text.as_str(), unit))) {
j += 1;
}
- let base: Vec<&PlacedGlyph> = line[i..j].iter().filter(|b| b.len > 0).collect();
- let x0 = base.iter().map(|b| b.x).fold(f64::MAX, f64::min);
- let x1 = base.iter().map(|b| b.x + b.adv).fold(f64::MIN, f64::max);
- let (mut glyphs, w) = set(g, text, st.size * SCALE, st.size);
- let n = glyphs.len().max(1) as f64;
- if w < x1 - x0 {
- // Shorter than the base: spread out, half a gap at each end (the 1-2-1 rule).
- let gap = (x1 - x0 - w) / n;
- for (k, r) in glyphs.iter_mut().enumerate() {
- r.x += x0 + gap * (k as f64 + 0.5);
+ let parents: Vec<usize> = (i..j).filter(|&k| glyphs.get(k).is_some_and(|g| g.len > 0)).collect();
+ let (Some(&first), Some(&last)) = (parents.first(), parents.last()) else {
+ i = j;
+ continue;
+ };
+ let Some((face, parent_size)) = glyphs.get(first).map(|g| (g.face, g.size)) else {
+ i = j;
+ continue;
+ };
+ let shaped = shape_ruby(db, face, &text, &spec, parent_size, fallback || missing, vertical);
+ let width: f64 = glyphs.get(i..j).unwrap_or_default().iter().map(|g| g.adv).sum();
+ let mono = unit.is_some();
+ let side = |k: Option<usize>| -> Side {
+ let Some(g) = k.and_then(|k| glyphs.get(k)) else { return Side::Edge };
+ if g.ch.is_control() || g.ch == crate::shape::HIDDEN {
+ return Side::Edge;
}
- } else {
- // Longer: centred over the base.
- let start = (x0 + x1 - w) / 2.0;
- for r in &mut glyphs {
- r.x += start;
+ match styles.get(g.style as usize) {
+ Some(s) if s.ruby.is_some() => Side::Ruby { free: unit_free(db, g.face, g.adv, s, mono, fallback, vertical) },
+ _ => Side::Char { ch: g.ch, adv: g.adv },
+ }
+ };
+ let before = allowance(&spec, shaped.em, parent_size, side(i.checked_sub(1)), true);
+ let after = allowance(&spec, shaped.em, parent_size, side(Some(j)), false);
+ let (before, after) = usable(spec.alignment, before, after);
+ let w = shaped.width * auto_scale(&spec, shaped.width, width, before + after);
+ if let Some((lead, inner, trail)) = parent_spacing(spec.parent_spacing, parents.len(), w - width - before - after) {
+ if let Some(g) = glyphs.get_mut(first) {
+ g.dx += lead;
+ g.adv += lead;
+ }
+ for pair in parents.windows(2) {
+ if let Some(g) = pair.get(1).and_then(|n| n.checked_sub(1)).and_then(|k| glyphs.get_mut(k)) {
+ g.adv += inner;
+ }
+ }
+ if let Some(g) = j.checked_sub(1).and_then(|k| glyphs.get_mut(k)) {
+ g.adv += trail;
+ }
+ }
+ // The overhang this relies on needs the neighbour on the same line: at a line edge the
+ // edge takes none (auto-align) or only the overhang amount, so the group stays with each
+ // neighbour its ruby overhangs and the spacing reserved here is what `annotate` places.
+ let spaced: f64 = glyphs.get(i..j).unwrap_or_default().iter().map(|g| g.adv).sum();
+ let excess = w - spaced;
+ if excess > 1e-9 {
+ let lead = lead_overhang(spec.alignment, excess, before, after);
+ if lead > 1e-9
+ && let Some(g) = i.checked_sub(1).and_then(|k| glyphs.get_mut(k))
+ {
+ g.break_after = Some(false);
+ }
+ if excess - lead > 1e-9
+ && let Some(g) = j.checked_sub(1).and_then(|k| glyphs.get_mut(k))
+ {
+ g.break_after = Some(false);
}
}
- extra.extend(glyphs);
- // Kenten on the rest of the group.
- for b in line[i + 1..j].iter().filter(|b| st.kenten && b.len > 0 && b.adv > 0.0) {
- extra.extend(kenten(db, b, st.size, &st.kenten_character, glyph_fallback || st.missing_font));
+ // Group ruby keeps its parent on one line (JLReq 3.3).
+ if !mono {
+ for g in glyphs.get_mut(first..last).unwrap_or_default() {
+ g.break_after = Some(false);
+ }
+ }
+ i = j;
+ }
+}
+
+// ---------- after line breaking ----------
+
+/// The run style ruby glyphs are drawn in: the parent's, with the ruby's colours and none of the
+/// parent's decorations.
+fn ruby_style(styles: &mut Vec<RunStyle>, base: &RunStyle, spec: &RubySpec, size: f64, missing: bool) -> u32 {
+ let mut rs = base.clone();
+ if spec.fill.is_empty() {
+ if let Some(t) = spec.fill_tint {
+ rs.fill_tint = t;
+ }
+ } else {
+ rs.fill.clone_from(&spec.fill);
+ rs.fill_tint = spec.fill_tint.unwrap_or(1.0);
+ }
+ if spec.stroke.is_empty() {
+ if let Some(t) = spec.stroke_tint {
+ rs.stroke_tint = t;
+ }
+ } else {
+ rs.stroke.clone_from(&spec.stroke);
+ rs.stroke_tint = spec.stroke_tint.unwrap_or(1.0);
+ }
+ if let Some(w) = spec.stroke_weight {
+ rs.stroke_weight = w;
+ }
+ // Text itself never overprints, so `Auto` (follow the text) is off.
+ rs.overprint_fill = spec.overprint_fill == AdornmentOverprint::On;
+ rs.overprint_stroke = spec.overprint_stroke == AdornmentOverprint::On;
+ rs.size = size;
+ rs.missing_font = missing;
+ rs.underline = false;
+ rs.strikethrough = false;
+ rs.custom_tracking = false;
+ rs.condition = None;
+ rs.xml_tag = None;
+ rs.ruby = None;
+ rs.ruby_spec = None;
+ rs.ruby_unit = None;
+ rs.kenten_mark = None;
+ rs.warichu = false;
+ intern(styles, rs)
+}
+
+/// How far the ruby must move away from its parent (down for ruby below, up for ruby above: the
+/// result is signed along y) so that it clears kenten set on the same side of the parent
+/// characters. Kenten sit next to the parent (JIS X 4051, JLReq: emphasis dots go between the
+/// base characters and ruby on the same side); the ruby's em box then starts where the marks' em
+/// box ends. `y` is the ruby's baseline without kenten and `tall` its em height.
+fn kenten_clearance(styles: &[RunStyle], base: &[&PlacedGlyph], shaped: &Shaped, tall: f64, position: RubyPosition, y: f64) -> f64 {
+ let below = position == RubyPosition::BelowLeft;
+ let (ruby_top, ruby_bottom) = shaped.face.em_box();
+ let k = tall / shaped.face.units_per_em().max(1.0);
+ let overlap = base
+ .iter()
+ .filter_map(|b| {
+ let m = styles.get(b.style as usize)?.kenten_mark.as_ref().filter(|m| m.below == below)?;
+ let (top, bottom) = b.face.em_box();
+ let h = m.size * m.y_scale;
+ Some(if below {
+ (b.y - bottom * b.sy + m.distance + h) - (y - ruby_top * k)
+ } else {
+ (y - ruby_bottom * k) - (b.y - top * b.sy - m.distance - h)
+ })
+ })
+ .fold(0.0, f64::max);
+ if below { overlap } else { -overlap }
+}
+
+/// Add the ruby of a laid-out line's glyphs. `text` is the story text the glyphs'
+/// bytes index; `glyph_fallback`: the document draws missing glyphs from fallback fonts (a
+/// missing font's substitute always does); `vertical`: the frame is vertical.
+pub(crate) fn annotate(
+ db: &ScopedFonts<'_>,
+ styles: &mut Vec<RunStyle>,
+ text: &str,
+ line: &mut Vec<PlacedGlyph>,
+ glyph_fallback: bool,
+ vertical: bool,
+) {
+ if !line.iter().any(|g| styles.get(g.style as usize).is_some_and(|s| s.ruby.is_some())) {
+ return;
+ }
+ let mut extra = Vec::new();
+ let mut i = 0;
+ while i < line.len() {
+ let Some(g) = line.get(i) else { break };
+ let Some(st) = styles.get(g.style as usize).filter(|_| g.len > 0 && g.visible).cloned() else {
+ i += 1;
+ continue;
+ };
+ let fallback = glyph_fallback || st.missing_font;
+ let (Some(reading), Some(spec)) = (st.ruby.clone(), st.ruby_spec.clone()) else {
+ i += 1;
+ continue;
+ };
+ // The group: following glyphs with the same ruby (and unit).
+ let key = (reading.as_str(), st.ruby_unit);
+ let mut j = i + 1;
+ while line.get(j).is_some_and(|b| b.len == 0 || styles.get(b.style as usize).and_then(ruby_key).is_some_and(|k| k == key)) {
+ j += 1;
+ }
+ let group = line.get(i..j).unwrap_or_default();
+ let base: Vec<&PlacedGlyph> = group.iter().filter(|b| b.len > 0).collect();
+ let Some(&first) = base.first() else {
+ i = j;
+ continue;
+ };
+ // The parent's extent from its pen positions (parent spacing lies inside it).
+ let x0 = base.iter().map(|b| b.x - b.dx).fold(f64::MAX, f64::min);
+ let x1 = base.iter().map(|b| b.x - b.dx + b.adv).fold(f64::MIN, f64::max);
+ let width = (x1 - x0).max(0.0);
+ let shaped = shape_ruby(db, first.face, &reading, &spec, st.size, fallback, vertical);
+ let mono = st.ruby_unit.is_some();
+ let side = |b: Option<&PlacedGlyph>| -> Side {
+ let Some(b) = b.filter(|b| b.visible) else { return Side::Edge };
+ match styles.get(b.style as usize) {
+ Some(s) if s.ruby.is_some() => Side::Ruby { free: unit_free(db, b.face, b.adv, s, mono, glyph_fallback, vertical) },
+ _ => Side::Char { ch: text.get(b.byte..).and_then(|s| s.chars().next()).unwrap_or(' '), adv: b.adv },
+ }
+ };
+ let prev = line.get(..i).unwrap_or_default().iter().rev().find(|b| b.len > 0);
+ let next = line.get(j..).unwrap_or_default().iter().find(|b| b.len > 0);
+ let before = allowance(&spec, shaped.em, st.size, side(prev), true);
+ let after = allowance(&spec, shaped.em, st.size, side(next), false);
+ let (before, after) = usable(spec.alignment, before, after);
+ let scale = auto_scale(&spec, shaped.width, width, before + after);
+ let w = shaped.width * scale;
+ // Each glyph's offset from the parent's start.
+ let offsets: Vec<f64> = if w <= width + 1e-9 {
+ let mut slots: Vec<f64> = Vec::new();
+ for r in &shaped.glyphs {
+ match slots.last_mut() {
+ Some(a) if r.joined => *a += r.adv,
+ _ => slots.push(r.adv),
+ }
+ }
+ let at = spread(spec.alignment, width, &slots, shaped.em);
+ let (mut slot, mut slot_x) = (0usize, 0.0);
+ let mut out = Vec::with_capacity(shaped.glyphs.len());
+ for (n, r) in shaped.glyphs.iter().enumerate() {
+ if !r.joined {
+ if n > 0 {
+ slot += 1;
+ }
+ slot_x = r.x;
+ }
+ out.push(at.get(slot).copied().unwrap_or(0.0) + (r.x - slot_x));
+ }
+ out
+ } else {
+ let lead = lead_overhang(spec.alignment, w - width, before, after);
+ shaped.glyphs.iter().map(|r| r.x * scale - lead).collect()
+ };
+ let tall = shaped.size * spec.y_scale;
+ let y = match spec.position {
+ // Its em box sits just clear of the parent's.
+ RubyPosition::AboveRight => -(st.size * 0.88 + tall * 0.2) - spec.y_offset,
+ RubyPosition::BelowLeft => st.size * 0.12 + tall * 0.96 + spec.y_offset,
+ };
+ let y = y + kenten_clearance(styles, &base, &shaped, tall, spec.position, y);
+ let missing = if spec.font_family.is_empty() { st.missing_font } else { !db.has_family(&spec.font_family) };
+ let style = ruby_style(styles, &st, &spec, shaped.size, missing);
+ for (r, off) in shaped.glyphs.iter().zip(offsets) {
+ extra.push(PlacedGlyph {
+ rendered_char: r.ch,
+ face: shaped.face,
+ gid: r.gid,
+ x: x0 + off + spec.x_offset,
+ y: y + r.y,
+ adv: r.adv * scale,
+ sx: r.sx * scale,
+ sy: r.sy,
+ style,
+ byte: first.byte,
+ len: 0,
++ generated_text: None,
+ visible: true,
+ upright: r.tcy.is_none() && upright_in_vertical(r.ch),
+ tcy: r.tcy,
+ rtl: false,
+ dx: 0.0,
+ });
}
i = j;
}
diff --cc crates/compose/src/shape.rs
index 58d419c39,a7bf99a62..e75da3a6a
--- a/crates/compose/src/shape.rs
+++ b/crates/compose/src/shape.rs
@@@ -1361,7 -961,6 +1374,8 @@@ pub(crate) fn hyphen_after(g: &Glyph) -
h.byte = g.byte + g.len;
h.len = 0;
h.ch = '-';
+ h.rendered_char = '-';
+ h.moji = Default::default();
+ h.generated_text = None;
h
}
diff --cc crates/pdf/src/tests.rs
index 1f62c4ae9,707ce9505..8607b9ff3
--- a/crates/pdf/src/tests.rs
+++ b/crates/pdf/src/tests.rs
@@@ -488,17 -447,36 +488,51 @@@ fn column_rules_are_drawn()
assert!(rules(false).is_empty());
}
+#[test]
+fn ruby_overprint_is_reported_as_not_exported() {
+ use designcraft_doc::cjk_settings::AdornmentOverprint;
+ let mut d = doc_with_text("漢字です");
+ let sid = *d.stories.keys().next().unwrap();
+ let clean = export_pdf_with_report(&d, &Cache::new(), &PdfOptions::default()).unwrap();
+ assert!(!clean.warnings.iter().any(|w| w.contains("overprint")), "{:?}", clean.warnings);
+ d.story_mut(sid).unwrap().format_chars(0.."漢字".len(), |f| {
+ f.over.ruby = Some("かんじ".into());
+ f.over.ruby_overprint_fill = Some(AdornmentOverprint::On);
+ });
+ let r = export_pdf_with_report(&d, &Cache::new(), &PdfOptions::default()).unwrap();
+ assert!(r.warnings.iter().any(|w| w.contains("overprint is not exported yet")), "{:?}", r.warnings);
+}
++
+ #[test]
+ fn small_caps_and_small_cap_figures_extract_as_typed() {
+ // Source Sans 3 substitutes letters and figures under `smcp` / `c2sc` (`one` → `one.s`),
+ // in typed text, in a page number and in a list label alike (each with its own figures, so
+ // one's mapping can't stand in for another's).
+ use designcraft_doc::{Capitalization, ListType, story::PAGE_NUMBER};
+ for caps in [Capitalization::SmallCaps, Capitalization::OpenTypeAllSmallCaps] {
+ let text = format!("Abc 234 x{PAGE_NUMBER}\nItem");
+ let mut d = doc_with_text(&text);
+ let sid = *d.stories.keys().next().unwrap();
+ let st = d.story_mut(sid).unwrap();
+ st.format_paras(0..text.len(), |f| {
+ f.chars.font_family = Some("Source Sans 3".into());
+ f.chars.capitalization = Some(caps);
+ });
+ st.format_paras(text.len() - 1..text.len(), |f| {
+ f.para.list_type = Some(ListType::Numbers);
+ f.para.start_at = Some(Some(5));
+ });
+ let got = extract_text(&export_pdf(&d, &Cache::new(), &PdfOptions::default()).unwrap()).concat();
+ assert!(!got.contains('\u{FFFD}'), "{caps:?}: {got:?}");
+ assert_eq!(got.split_whitespace().collect::<Vec<_>>(), ["Abc", "234", "x15.Item"], "{caps:?}: {got:?}");
+ }
+ // All small caps draws `A` and `a` with one glyph, which one ToUnicode entry can't cover: the
+ // second spelling goes out as ActualText (which this extractor doesn't read).
+ let mut d = doc_with_text("Aa");
+ let sid = *d.stories.keys().next().unwrap();
+ d.story_mut(sid).unwrap().format_chars(0..2, |f| {
+ f.over.font_family = Some("Source Sans 3".into());
+ f.over.capitalization = Some(Capitalization::OpenTypeAllSmallCaps);
+ });
+ assert!(uncompressed(&d, PdfOptions::default()).contains("/ActualText"));
+ }
diff --cc crates/pdf/src/text.rs
index c3c4f4be6,40635d407..f38f09382
--- a/crates/pdf/src/text.rs
+++ b/crates/pdf/src/text.rs
@@@ -438,12 -263,19 +438,23 @@@ impl Exporter<'_>
} else if g.len == 0 && prev_src == Some(g.byte) && !ranges.is_empty() {
// Second glyph of a multi-glyph cluster: share the cluster's text.
ranges[ranges.len() - 1].clone()
+ } else if let Some(t) = g.generated_text.as_deref().filter(|t| !t.is_empty() && !t.chars().any(is_marker)) {
+ // Generated text (page numbers, list labels): its own characters, which a glyph
+ // the font substituted (small-cap or old-style figures) can't be mapped back to.
+ prev_src = None;
+ let a = text.len();
+ text.push_str(t);
+ a..text.len()
+ } else if g.generated_text.as_deref() == Some("") && !ranges.is_empty() {
+ // Later glyph of a generated cluster.
+ prev_src = None;
+ ranges[ranges.len() - 1].clone()
+ } else if g.len == 0 && cmap.get(&g.gid) == Some(&'\u{0640}') {
+ // A kashida that justification inserted: drawn, but no text of its own.
+ prev_src = None;
+ text.len()..text.len()
} else {
- // Inserted glyphs (page numbers, list labels, hyphens): map back through the font.
+ // Other inserted glyphs (hyphens, tab leaders, ruby): map back through the font.
prev_src = None;
match cmap.get(&g.gid) {
Some(c) => {