mirror of
https://github.com/storytold/wordcraft.git
synced 2026-12-04 22:49:54 +00:00
Compare commits
3
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
20fadf402e | ||
|
|
2f4da4a96f | ||
|
|
34560d6cd2 |
@@ -427,8 +427,17 @@ fn word_spans(pl: &ParaLayout, line: &crate::para::Line, (k0, k1): (usize, usize
|
||||
words
|
||||
}
|
||||
|
||||
/// The top and height of the text itself within a line, leaving out the leading line spacing adds.
|
||||
fn line_text_extent(ctx: &LineContext<'_, '_, '_>) -> (f32, f32) {
|
||||
let (asc, desc) = ctx.pl.caret_metrics(ctx.li);
|
||||
let text_top = (ctx.base - asc).max(ctx.top);
|
||||
let text_bottom = (ctx.base + desc).min(ctx.bottom);
|
||||
(text_top, (text_bottom - text_top).max(0.0))
|
||||
}
|
||||
|
||||
fn comment_backgrounds(ctx: &LineContext<'_, '_, '_>, out: &mut Vec<Draw>) {
|
||||
let Some(para) = ctx.para.filter(|_| ctx.opts.markup && ctx.opts.revisions.comments) else { return };
|
||||
let (text_top, text_height) = line_text_extent(ctx);
|
||||
let mut open: Option<usize> = None;
|
||||
let mut ranges = Vec::new();
|
||||
for off in para.object_offsets() {
|
||||
@@ -448,7 +457,7 @@ fn comment_backgrounds(ctx: &LineContext<'_, '_, '_>, out: &mut Vec<Draw>) {
|
||||
for (x0, x1) in ctx.pl.x_spans(ctx.li, start.max(ctx.line.start), end.min(ctx.line.stop)) {
|
||||
if x1 > x0 {
|
||||
out.push(Draw::Fill {
|
||||
rect: Rect::new(ctx.x + x0, ctx.top, x1 - x0, ctx.line.height),
|
||||
rect: Rect::new(ctx.x + x0, text_top, x1 - x0, text_height),
|
||||
color: Rgb(0xEF, 0xE3, 0xF7),
|
||||
alpha: ctx.alpha,
|
||||
text_bg: false,
|
||||
@@ -460,6 +469,7 @@ fn comment_backgrounds(ctx: &LineContext<'_, '_, '_>, out: &mut Vec<Draw>) {
|
||||
|
||||
/// Paint highlights and shading before glyphs, grouping adjacent equal character borders.
|
||||
fn character_backgrounds(ctx: &LineContext<'_, '_, '_>, out: &mut Vec<Draw>) {
|
||||
let (text_top, text_height) = line_text_extent(ctx);
|
||||
let mut open: Option<(Border, f32, f32)> = None;
|
||||
for k in ctx.line.c0..ctx.line.c1 {
|
||||
let (Some(cluster), Some(left), Some(right)) = (ctx.pl.clusters.get(k), ctx.line.cl_left(k), ctx.line.cl_right(k)) else { continue };
|
||||
@@ -469,7 +479,7 @@ fn character_backgrounds(ctx: &LineContext<'_, '_, '_>, out: &mut Vec<Draw>) {
|
||||
}
|
||||
if let Some(color) = style.rc.highlight.or(style.rc.shading) {
|
||||
out.push(Draw::Fill {
|
||||
rect: Rect::new(ctx.x + left, ctx.top, (right - left).max(0.0), ctx.line.height),
|
||||
rect: Rect::new(ctx.x + left, text_top, (right - left).max(0.0), text_height),
|
||||
color,
|
||||
alpha: ctx.alpha,
|
||||
text_bg: true,
|
||||
|
||||
@@ -975,16 +975,20 @@ impl DocLayout {
|
||||
continue;
|
||||
}
|
||||
let top = l.top - first.top;
|
||||
let (asc, desc) = para.caret_metrics(li);
|
||||
let base = top + (l.baseline - l.top);
|
||||
let sel_top = (base - asc).max(top);
|
||||
let sel_height = (base + desc - sel_top).max(0.0);
|
||||
if !l.vis.is_empty() {
|
||||
// Bidirectional line: the selected text can be several visual pieces.
|
||||
for (x0, x1) in para.x_spans(li, from, to) {
|
||||
put(Rect::new(x0, top, (x1 - x0).max(0.0), l.height));
|
||||
put(Rect::new(x0, sel_top, (x1 - x0).max(0.0), sel_height));
|
||||
}
|
||||
// Selection continuing past the paragraph end shows the paragraph mark.
|
||||
if li + 1 == para.lines.len() && path != b.path {
|
||||
let ex = l.end_x();
|
||||
let x0 = if l.rtl { ex - PARA_MARK_WIDTH } else { ex };
|
||||
put(Rect::new(x0, top, PARA_MARK_WIDTH, l.height));
|
||||
put(Rect::new(x0, sel_top, PARA_MARK_WIDTH, sel_height));
|
||||
}
|
||||
continue;
|
||||
}
|
||||
@@ -1005,7 +1009,7 @@ impl DocLayout {
|
||||
if x1 <= x0 && !(last_line && path != b.path) {
|
||||
continue;
|
||||
}
|
||||
put(Rect::new(x0, top, (x1 - x0).max(0.0), l.height));
|
||||
put(Rect::new(x0, sel_top, (x1 - x0).max(0.0), sel_height));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17201,6 +17201,41 @@ fn caret_height_ignores_line_spacing() {
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn selection_and_highlight_height_ignore_line_spacing() {
|
||||
// #3468: extra line spacing is leading below the text; selection and highlight heights stay the height of the font.
|
||||
use wordcraft_doc::props::{Highlight, LineSpacing};
|
||||
let metrics = |spacing: Option<LineSpacing>| {
|
||||
let mut d = Document::new();
|
||||
let chr = wordcraft_doc::CharProps {
|
||||
font: Some("Source Sans 3".into()),
|
||||
size: Some(12.0),
|
||||
highlight: Some(Highlight::Yellow),
|
||||
..Default::default()
|
||||
};
|
||||
let mut p = wordcraft_doc::Paragraph::with_text("Alpha probe", chr);
|
||||
p.props = ParaProps { line_spacing: spacing, ..Default::default() };
|
||||
d.body = vec![wordcraft_doc::para_block(p)];
|
||||
let l = lay(&d);
|
||||
let sel_h = l.selection_rects(&d, &Pos::body(0, 0), &Pos::body(0, 5), 0)[0].1.h;
|
||||
let draws = display::page_display(&d, &l.pages[0], &Default::default());
|
||||
let hl_h = draws
|
||||
.iter()
|
||||
.find_map(|draw| match draw {
|
||||
display::Draw::Fill { rect, text_bg: true, .. } => Some(rect.h),
|
||||
_ => None,
|
||||
})
|
||||
.unwrap();
|
||||
(sel_h, hl_h)
|
||||
};
|
||||
let (single_sel, single_hl) = metrics(Some(LineSpacing::Multiple(1.0)));
|
||||
for spacing in [LineSpacing::Multiple(2.0), LineSpacing::AtLeast(40.0), LineSpacing::Exactly(40.0)] {
|
||||
let (wide_sel, wide_hl) = metrics(Some(spacing));
|
||||
assert!((single_sel - wide_sel).abs() < 0.01, "selection {spacing:?}: single {single_sel}, wide {wide_sel}");
|
||||
assert!((single_hl - wide_hl).abs() < 0.01, "highlight {spacing:?}: single {single_hl}, wide {wide_hl}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn bottom_border_reserves_gap() {
|
||||
use wordcraft_doc::props::{BorderStyle, Borders};
|
||||
|
||||
Reference in New Issue
Block a user