mirror of
https://github.com/storytold/wordcraft.git
synced 2026-12-04 22:49:54 +00:00
Merge pull request #7067
With AutoCraft's conflict resolution 1c87a92b.
This commit is contained in:
@@ -157,10 +157,12 @@ impl<'a> Builder<'a> {
|
||||
// A mark is set like the character it belongs to, as for `complex`: in small capitals at
|
||||
// that character's size, even when the run's font already draws the mark, so the two
|
||||
// stay in one shaping run, can compose or position together, and match their cluster
|
||||
// whole (#7865, #8571).
|
||||
// whole (#7865, #8571). Any other character: Small Caps draws lowercase letters, and
|
||||
// the space and ideographic space, at the small-caps size, as Word does; a no-break
|
||||
// space, digits and punctuation keep the full size (#7062).
|
||||
let small = match cur {
|
||||
Some(k) if mark => k.small,
|
||||
_ => rc.small_caps && !rc.caps && c.is_lowercase(),
|
||||
_ => rc.small_caps && !rc.caps && (c.is_lowercase() || c == ' ' || c == '\u{3000}'),
|
||||
};
|
||||
let key = SegKey { face, small, complex, rtl };
|
||||
match cur {
|
||||
@@ -206,7 +208,9 @@ impl<'a> Builder<'a> {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
let size = rc.draw_size() * if small { 0.8 } else { 1.0 };
|
||||
// Small capitals are set at 80% of the size rounded to the half point, as Word does
|
||||
// (16 pt → 13, 14 → 11, 12 → 9.5, #7062).
|
||||
let size = if small { (rc.draw_size() * 0.8 * 2.0).round() / 2.0 } else { rc.draw_size() };
|
||||
// A symbol font that isn't installed, or is installed one em tall (Apple's Symbol), takes
|
||||
// the height Word gives it.
|
||||
let (a, d) = match wordcraft_fonts::word::symbol_line(&rc.font, &r) {
|
||||
@@ -222,7 +226,9 @@ impl<'a> Builder<'a> {
|
||||
}
|
||||
None => wordcraft_fonts::word::line_metrics(&face),
|
||||
};
|
||||
let k = size as f64 / face.upem.max(1.0);
|
||||
// A small-caps piece is drawn at 80% but measured for the line at the full size: a Small
|
||||
// Caps line is as tall as without Small Caps, as in Word (#7063).
|
||||
let k = rc.draw_size() as f64 / face.upem.max(1.0);
|
||||
// A fallback face (letters the requested font lacks) is emboldened or slanted when it has
|
||||
// no bold or italic of its own.
|
||||
let (synth_bold, synth_italic) = match face_override {
|
||||
|
||||
@@ -15807,6 +15807,91 @@ fn caps_draw_sharp_s_as_two_capitals_in_one_cluster() {
|
||||
assert_eq!(cl.iter().map(|c| c.2.len()).sum::<usize>(), 6, "{cl:?}");
|
||||
}
|
||||
|
||||
/// `text` laid out with `f` applied to all of it: its paragraph's layout.
|
||||
fn para_with(text: &str, f: &dyn Fn(&mut wordcraft_doc::props::CharProps)) -> ParaLayout {
|
||||
let mut d = Document::from_text(text);
|
||||
d.format_range(&Pos::body(0, 0), &Pos::body(0, text.len()), f).unwrap();
|
||||
let l = lay(&d);
|
||||
l.pages[0].items.iter().find_map(|it| if let Placed::Lines { para, .. } = it { Some((**para).clone()) } else { None }).expect("a laid-out line")
|
||||
}
|
||||
|
||||
/// Small Caps draws a space (U+0020) at the small-caps size, like a lowercase letter, as Word
|
||||
/// does (measured: 40 spaces in Times New Roman Bold 10 pt are 80% as wide in Small Caps); a
|
||||
/// no-break space keeps its full width, and with All Caps as well the space keeps its full width,
|
||||
/// as All Caps wins (#7062).
|
||||
#[test]
|
||||
fn a_space_in_small_caps_is_as_wide_as_at_the_small_caps_size() {
|
||||
crate::test_fonts();
|
||||
// The gap between the first and the last letter of `text`.
|
||||
let gap = |text: &str, f: &dyn Fn(&mut wordcraft_doc::props::CharProps)| {
|
||||
let pl = para_with(text, f);
|
||||
let line = &pl.lines[0];
|
||||
let x = |start: usize| (line.c0..line.c1).find(|&k| pl.clusters[k].start == start).and_then(|k| line.cl_left(k)).unwrap();
|
||||
x(text.len() - 1) - x(1)
|
||||
};
|
||||
// At 10 pt the small-caps size is exactly 8 pt (80%, no rounding to the half point).
|
||||
let plain: &dyn Fn(&mut wordcraft_doc::props::CharProps) = &|c| c.size = Some(10.0);
|
||||
let small: &dyn Fn(&mut wordcraft_doc::props::CharProps) = &|c| {
|
||||
c.small_caps = Some(true);
|
||||
c.size = Some(10.0);
|
||||
};
|
||||
let both: &dyn Fn(&mut wordcraft_doc::props::CharProps) = &|c| {
|
||||
c.caps = Some(true);
|
||||
c.small_caps = Some(true);
|
||||
c.size = Some(10.0);
|
||||
};
|
||||
let spaces = format!("a{}a", " ".repeat(10));
|
||||
let ratio = gap(&spaces, small) / gap(&spaces, plain);
|
||||
assert!((ratio - 0.8).abs() < 0.01, "Small Caps spaces are 80% as wide: {ratio}");
|
||||
assert!((gap(&spaces, both) - gap(&spaces, plain)).abs() < 0.01, "All Caps wins: full-width spaces");
|
||||
let nbsp = format!("a{}a", "\u{a0}".repeat(10));
|
||||
assert!((gap( , small) - gap( , plain)).abs() < 0.01, "a no-break space keeps its width");
|
||||
}
|
||||
|
||||
/// Word sets small capitals at 80% of the size rounded to the half point, a size the format can
|
||||
/// store, like superscript (measured in Word for Microsoft 365, Times New Roman: 10 pt → 8,
|
||||
/// 11 → 9, 12 → 9.5, 14 → 11, 16 → 13); a Small Caps space takes that size too (#7062).
|
||||
#[test]
|
||||
fn small_caps_take_eighty_percent_of_the_size_rounded_to_the_half_point() {
|
||||
crate::test_fonts();
|
||||
let width = |text: &str, f: &dyn Fn(&mut wordcraft_doc::props::CharProps)| {
|
||||
let pl = para_with(text, f);
|
||||
let line = &pl.lines[0];
|
||||
(line.c0..line.c1).map(|k| pl.clusters[k].adv).sum::<f32>()
|
||||
};
|
||||
for (size, small) in [(16.0_f32, 13.0_f32), (14.0, 11.0), (12.0, 9.5)] {
|
||||
let small_caps = width("xxxxxxxxxx", &|c| {
|
||||
c.small_caps = Some(true);
|
||||
c.size = Some(size);
|
||||
});
|
||||
let capitals = width("XXXXXXXXXX", &|c| c.size = Some(small));
|
||||
assert!((small_caps - capitals).abs() < 0.01, "{size} pt Small Caps draws capitals at {small} pt: {small_caps} vs {capitals}");
|
||||
let spaces = width("x x", &|c| {
|
||||
c.small_caps = Some(true);
|
||||
c.size = Some(size);
|
||||
}) - width("xx", &|c| {
|
||||
c.small_caps = Some(true);
|
||||
c.size = Some(size);
|
||||
});
|
||||
let plain = width("x x", &|c| c.size = Some(small)) - width("xx", &|c| c.size = Some(small));
|
||||
assert!((spaces - plain).abs() < 0.01, "{size} pt Small Caps spaces are {small} pt spaces: {spaces} vs {plain}");
|
||||
}
|
||||
}
|
||||
|
||||
/// A Small Caps line of lowercase letters is as tall as the same line without Small Caps: the
|
||||
/// small capitals are drawn at 80% of the size, but the line is measured at the full size, as in
|
||||
/// Word (#7063).
|
||||
#[test]
|
||||
fn a_small_caps_line_of_lowercase_is_as_tall_as_without_small_caps() {
|
||||
crate::test_fonts();
|
||||
let text = "only lowercase letters here";
|
||||
let line = |f: &dyn Fn(&mut wordcraft_doc::props::CharProps)| para_with(text, f).lines[0].clone();
|
||||
let plain = line(&|c| c.bold = Some(false));
|
||||
let small = line(&|c| c.small_caps = Some(true));
|
||||
assert!((small.height - plain.height).abs() < 0.01, "line height: Small Caps {} vs plain {}", small.height, plain.height);
|
||||
assert!((small.baseline - plain.baseline).abs() < 0.01, "baseline: Small Caps {} vs plain {}", small.baseline, plain.baseline);
|
||||
}
|
||||
|
||||
/// A caps run in a symbol font draws each code as its symbol: ß never becomes two glyphs (#1581).
|
||||
#[test]
|
||||
fn caps_in_a_symbol_font_never_expand_characters() {
|
||||
|
||||
File diff suppressed because one or more lines are too long
Reference in New Issue
Block a user