Skip to content

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

Text: list labels don't raise the Ascent first baseline

aiongg8ce8ed5fbe9f
diff --git a/crates/compose/src/lib.rs b/crates/compose/src/lib.rs
index 156f55a90..8c4d087c6 100644
--- a/crates/compose/src/lib.rs
+++ b/crates/compose/src/lib.rs
@@ -704,23 +704,27 @@ fn compose_with_db(doc: &Document, story: &Story, frames: &[FrameSpec], opts: &C
let drop_cap = split_drop_cap(db, &mut sp.glyphs, drop_end, pp.drop_cap_lines, base_leading, base_cap, drop_grid, drop_align);
// List labels take the default super/subscript settings.
let label_env = shape::TypeEnv { adv: Default::default(), ..env };
- match pp.list_type {
+ let list_label = match pp.list_type {
designcraft_doc::ListType::Numbers if !pp.list_name.is_empty() => {
// A named list: carries on past other paragraphs (and from earlier stories).
let n = named_numbers.get(pi).copied().flatten().unwrap_or(1);
- let label = format!("{}.{}", pp.number_style.format(n), pp.list_separator);
- prepend_label(db, &mut sp.glyphs, &label, prange.start, &base_chars, label_env, &mut table);
+ Some(format!("{}.{}", pp.number_style.format(n), pp.list_separator))
}
designcraft_doc::ListType::Numbers => {
list_counter = pp.start_at.map_or(list_counter + 1, |s| s.max(1));
- let label = format!("{}.{}", pp.number_style.format(list_counter), pp.list_separator);
- prepend_label(db, &mut sp.glyphs, &label, prange.start, &base_chars, label_env, &mut table);
+ Some(format!("{}.{}", pp.number_style.format(list_counter), pp.list_separator))
}
- designcraft_doc::ListType::Bullets => {
- let label = format!("{}{}", pp.bullet_char, pp.list_separator);
- prepend_label(db, &mut sp.glyphs, &label, prange.start, &base_chars, label_env, &mut table);
+ designcraft_doc::ListType::Bullets => Some(format!("{}{}", pp.bullet_char, pp.list_separator)),
+ designcraft_doc::ListType::None => {
+ list_counter = 0;
+ None
+ }
+ };
+ if let Some(label) = list_label {
+ let n = prepend_label(db, &mut sp.glyphs, &label, prange.start, &base_chars, label_env, &mut table);
+ for g in sp.glyphs.iter_mut().take(n) {
+ g.list_label = true;
}
- designcraft_doc::ListType::None => list_counter = 0,
}
if pi == 0
&& let Some(label) = &opts.label
@@ -1555,8 +1559,9 @@ fn line_metrics(
base: &designcraft_doc::CharProps,
) -> (f64, Tops, f64, f64) {
let src: &[Glyph] = if line.is_empty() { all.get(s..(s + 1).min(all.len())).unwrap_or(&[]) } else { line };
+ let face = || db.face(&base.font_family, &base.font_style);
if src.is_empty() {
- let face = db.face(&base.font_family, &base.font_style);
+ let face = face();
let k = base_size / face.upem;
let tops = Tops { typo_ascent: face.typo_ascent * k, cap: face.cap_height * k, xh: face.x_height * k };
return (face.ascent * k, tops, face.descent * k, base_leading);
@@ -1566,16 +1571,26 @@ fn line_metrics(
let mut desc: f64 = 0.0;
let mut lead: f64 = 0.0;
let reference = src.iter().max_by(|a, b| a.size.total_cmp(&b.size));
+ let mut own_text = false;
for g in src {
let shift = g.shift + reference.map_or(0.0, |r| cjk_alignment_shift(g, r));
let up = shift.max(0.0);
asc = asc.max(g.ascent + up);
- tops.typo_ascent = tops.typo_ascent.max(g.typo_ascent + up);
+ // A list's bullet or number doesn't raise the Ascent offset: only the line's own text does.
+ if !g.list_label {
+ own_text = true;
+ tops.typo_ascent = tops.typo_ascent.max(g.typo_ascent + up);
+ }
tops.cap = tops.cap.max(g.cap + up);
tops.xh = tops.xh.max(g.xh + up);
desc = desc.max(g.descent - shift.min(0.0));
lead = lead.max(g.leading);
}
+ if !own_text {
+ // Only a label (an empty list paragraph): as an empty paragraph.
+ let face = face();
+ tops.typo_ascent = face.typo_ascent * base_size / face.upem;
+ }
(asc, tops, desc, lead)
}
@@ -2298,6 +2313,7 @@ fn place_drop_cap(dc: &DropCap, at: f64, rtl: bool) -> Vec<PlacedGlyph> {
out
}
+/// Put generated `label` text before the paragraph's glyphs; returns how many glyphs it added.
fn prepend_label(
db: &ScopedFonts<'_>,
glyphs: &mut Vec<Glyph>,
@@ -2306,7 +2322,7 @@ fn prepend_label(
base: &designcraft_doc::CharProps,
env: shape::TypeEnv,
table: &mut StyleTable<'_>,
-) {
+) -> usize {
// Shape the label as a tiny standalone story so it uses the paragraph's base character style.
let mut tmp = Story::new(StoryId(0));
tmp.insert(0, label);
@@ -2321,8 +2337,10 @@ fn prepend_label(
g
})
.collect();
+ let n = pre.len();
pre.append(glyphs);
*glyphs = pre;
+ n
}
type LimitsKey = (usize, usize, usize, bool);
diff --git a/crates/compose/src/shape.rs b/crates/compose/src/shape.rs
index 47e1b1ec8..5e43560af 100644
--- a/crates/compose/src/shape.rs
+++ b/crates/compose/src/shape.rs
@@ -69,6 +69,9 @@ pub struct Glyph {
/// Upright in a vertical frame: shaped top to bottom (`adv` is its vertical advance) and hung
/// from its vertical origin on the line's centre (see [`crate::PlacedGlyph::vertical_xf`]).
pub upright: bool,
+ /// Part of the bullet or number the paragraph's list adds: not the line's own text, so it
+ /// doesn't raise the Ascent first baseline.
+ pub list_label: bool,
}
impl Glyph {
@@ -807,6 +810,7 @@ fn control_glyph(face: &Arc<FontFace>, p: &CharProps, auto_leading: TypeEnv, sty
shaping_rtl: false,
lang: designcraft_doc::language_tag(&p.language),
upright: false,
+ list_label: false,
}
}
@@ -930,6 +934,7 @@ fn shape_segment(
shaping_rtl: sg.rtl,
lang,
upright: up,
+ list_label: false,
});
}
}
diff --git a/crates/compose/src/tests.rs b/crates/compose/src/tests.rs
index 68fb5670b..cb753f4b0 100644
--- a/crates/compose/src/tests.rs
+++ b/crates/compose/src/tests.rs
@@ -1468,6 +1468,50 @@ fn a_visible_frame_stroke_insets_the_text() {
}
}
+#[test]
+fn list_labels_do_not_raise_the_ascent_first_baseline() {
+ use designcraft_doc::{CharAttrs, ListType};
+ // The paragraph's own character size (the label's) is 40 pt; its text is 10 pt (Source Sans 3:
+ // typo ascender 1 em).
+ let line = |text: &str, list: ListType| {
+ let (mut d, sid, _) = doc_with(text, Rect::new(36.0, 36.0, 300.0, 300.0), ParaAttrs { list_type: Some(list), ..Default::default() });
+ let st = d.story_mut(sid).unwrap();
+ for p in &mut st.paras {
+ p.chars = CharAttrs { font_family: Some("Source Sans 3".into()), size: Some(40.0), ..Default::default() };
+ }
+ st.format_chars(0..text.len(), |f| f.over.size = Some(10.0));
+ let l = compose_story(&d, sid, &ComposeOptions::default()).frames[0].lines[0].clone();
+ (l.baseline, l.ascent, l.leading)
+ };
+ let (b, asc, lead) = line("Hxg Hxg", ListType::None);
+ // 1 em of the 10 pt text below the frame top.
+ assert!((b - 46.0).abs() < 0.01, "{b}");
+ for list in [ListType::Bullets, ListType::Numbers] {
+ let (lb, lasc, llead) = line("Hxg Hxg", list);
+ assert!((lb - b).abs() < 1e-6, "{list:?}: {lb} vs {b}");
+ // The 40 pt label still sets the line's ascent and leading.
+ assert!(lasc > asc + 20.0 && (llead - 48.0).abs() < 1e-6 && (lead - 12.0).abs() < 1e-6, "{list:?}: {lasc} {asc} {llead} {lead}");
+ }
+ // An empty bulleted paragraph sits like an empty paragraph.
+ let (eb, _, _) = line("", ListType::None);
+ let (ebl, _, _) = line("", ListType::Bullets);
+ assert!((ebl - eb).abs() < 1e-6, "{ebl} vs {eb}");
+}
+
+#[test]
+fn spaces_count_toward_the_ascent_first_baseline() {
+ // 10 pt text with a 40 pt space between its words (Source Sans 3: typo ascender 1 em).
+ let (mut d, sid, _) = doc_with("Hxg Hxg", Rect::new(36.0, 36.0, 300.0, 300.0), ParaAttrs::default());
+ let st = d.story_mut(sid).unwrap();
+ st.format_chars(0..7, |f| {
+ f.over.font_family = Some("Source Sans 3".into());
+ f.over.size = Some(10.0);
+ });
+ st.format_chars(3..4, |f| f.over.size = Some(40.0));
+ let l = &compose_story(&d, sid, &ComposeOptions::default()).frames[0].lines[0];
+ assert!((l.baseline - (36.0 + 40.0)).abs() < 0.01, "{}", l.baseline);
+}
+
#[test]
fn vertical_scale_does_not_move_the_ascent_first_baseline() {
let db = designcraft_fonts::FontDb::global();