🐛 Align WASM auto-width text size with HTML measurement

Add the 1px right margin used by the HTML `.paragraph-set` renderer
when reporting auto-width dimensions from Skia, so stored selrects
match get-text-dimensions and stale-text sync does not misfire.
This commit is contained in:
Alejandro Alonso 2026-08-28 12:37:18 +02:00
parent afd4db776f
commit 6f68468028

View File

@ -51,6 +51,11 @@ pub struct TextContentSize {
const DEFAULT_TEXT_CONTENT_SIZE: f32 = 0.01; const DEFAULT_TEXT_CONTENT_SIZE: f32 = 0.01;
/// Matches `marginRight: "1px"` on `.paragraph-set` in the HTML text renderer
/// (`frontend/src/app/main/ui/shapes/text/styles.cljs`). DOM `getBoundingClientRect`
/// includes that margin in auto-width measurements; Skia `longest_line()` does not.
const PARAGRAPH_SET_MARGIN_RIGHT: f32 = 1.0;
impl TextContentSize { impl TextContentSize {
pub fn default() -> Self { pub fn default() -> Self {
Self { Self {
@ -770,7 +775,7 @@ impl TextContent {
let measure_paragraphs = let measure_paragraphs =
build_paragraphs_from_paragraph_builders(&mut measure_builders, f32::MAX); build_paragraphs_from_paragraph_builders(&mut measure_builders, f32::MAX);
let width = measure_paragraphs let content_width = measure_paragraphs
.iter() .iter()
.flatten() .flatten()
.fold(0.0_f32, |auto_width, paragraph| { .fold(0.0_f32, |auto_width, paragraph| {
@ -778,9 +783,10 @@ impl TextContent {
}) })
.ceil(); .ceil();
// Re-layout at that width with the real alignment. // Re-layout at the intrinsic width (without the HTML margin slack).
let mut paragraph_builders = self.paragraph_builder_group_from_text(None); let mut paragraph_builders = self.paragraph_builder_group_from_text(None);
let paragraphs = build_paragraphs_from_paragraph_builders(&mut paragraph_builders, width); let paragraphs =
build_paragraphs_from_paragraph_builders(&mut paragraph_builders, content_width);
let height = paragraphs let height = paragraphs
.iter() .iter()
.flatten() .flatten()
@ -788,10 +794,11 @@ impl TextContent {
auto_height + paragraph.height() auto_height + paragraph.height()
}); });
let reported_width = content_width + PARAGRAPH_SET_MARGIN_RIGHT;
let size = TextContentSize::new_with_normalized_line_height( let size = TextContentSize::new_with_normalized_line_height(
width, reported_width,
height.ceil(), height.ceil(),
width, reported_width,
normalized_line_height, normalized_line_height,
); );
TextContentLayoutResult(paragraph_builders, paragraphs, size) TextContentLayoutResult(paragraph_builders, paragraphs, size)