use text_document::{BlockSnapshot, FlowElementSnapshot, FlowSnapshot, FragmentContent};
#[derive(Debug, Clone, PartialEq)]
pub struct EditorTypographyDefaults {
pub font_family: Option<String>,
pub line_height: f32,
pub first_line_indent: f32,
pub paragraph_spacing_before: f32,
pub paragraph_spacing_after: f32,
}
impl Default for EditorTypographyDefaults {
fn default() -> Self {
Self {
font_family: None,
line_height: 1.0,
first_line_indent: 0.0,
paragraph_spacing_before: 0.0,
paragraph_spacing_after: 0.0,
}
}
}
pub(crate) fn needs_snapshot_fill(d: &EditorTypographyDefaults) -> bool {
d.font_family.is_some()
|| (d.line_height - 1.0).abs() > f32::EPSILON
|| d.first_line_indent.abs() > f32::EPSILON
|| d.paragraph_spacing_before.abs() > f32::EPSILON
|| d.paragraph_spacing_after.abs() > f32::EPSILON
}
pub(crate) fn apply_to_flow(flow: &mut FlowSnapshot, d: &EditorTypographyDefaults) {
apply_to_elements(&mut flow.elements, d);
}
fn apply_to_elements(elements: &mut [FlowElementSnapshot], d: &EditorTypographyDefaults) {
for el in elements {
match el {
FlowElementSnapshot::Block(b) => apply_to_block(b, d),
FlowElementSnapshot::Table(t) => {
for cell in &mut t.cells {
for b in &mut cell.blocks {
apply_to_block(b, d);
}
}
}
FlowElementSnapshot::Frame(fr) => apply_to_elements(&mut fr.elements, d),
}
}
}
pub(crate) fn apply_to_block(block: &mut BlockSnapshot, d: &EditorTypographyDefaults) {
if block.block_format.line_height.is_none() {
block.block_format.line_height = Some(d.line_height);
}
let is_heading = block.block_format.heading_level.unwrap_or(0) != 0;
let is_list_item = block.list_info.is_some();
if !is_heading && !is_list_item {
if block.block_format.text_indent.is_none() {
block.block_format.text_indent = Some(d.first_line_indent.round() as i32);
}
if block.block_format.top_margin.is_none() {
block.block_format.top_margin = Some(d.paragraph_spacing_before.round() as i32);
}
if block.block_format.bottom_margin.is_none() {
block.block_format.bottom_margin = Some(d.paragraph_spacing_after.round() as i32);
}
}
if let Some(family) = &d.font_family {
if block.fragments.is_empty() {
block.fragments.push(FragmentContent::Text {
text: String::new(),
format: text_document::TextFormat {
font_family: Some(family.clone()),
..Default::default()
},
offset: 0,
length: 0,
element_id: 0,
word_starts: Vec::new(),
});
} else {
for frag in &mut block.fragments {
if let FragmentContent::Text { format, .. } = frag
&& format.font_family.is_none()
{
format.font_family = Some(family.clone());
}
}
}
}
}
pub(crate) fn needs_params_fill(d: &EditorTypographyDefaults) -> bool {
d.font_family.is_some() || (d.line_height - 1.0).abs() > f32::EPSILON
}
pub(crate) fn apply_to_block_params(
params: &mut text_typeset::layout::block::BlockLayoutParams,
d: &EditorTypographyDefaults,
) {
if params.line_height_multiplier.is_none() {
params.line_height_multiplier = Some(d.line_height);
}
if let Some(family) = &d.font_family {
for frag in &mut params.fragments {
if frag.font_family.is_none() {
frag.font_family = Some(family.clone());
}
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use text_document::{BlockFormat, BlockSnapshot, ListInfo, ListStyle, TextFormat};
fn block(text: &str, format: TextFormat, block_format: BlockFormat) -> BlockSnapshot {
let len = text.chars().count();
BlockSnapshot {
block_id: 0,
position: 0,
length: len,
text: text.to_string(),
fragments: vec![FragmentContent::Text {
text: text.to_string(),
format,
offset: 0,
length: len,
element_id: 0,
word_starts: Vec::new(),
}],
block_format,
list_info: None,
parent_frame_id: None,
table_cell: None,
paint_highlights: Vec::new(),
}
}
fn defaults() -> EditorTypographyDefaults {
EditorTypographyDefaults {
font_family: Some("Literata".into()),
line_height: 1.5,
first_line_indent: 28.0,
paragraph_spacing_before: 12.0,
paragraph_spacing_after: 6.0,
}
}
fn frag_family(b: &BlockSnapshot) -> Option<&str> {
let FragmentContent::Text { format, .. } = &b.fragments[0] else {
panic!("expected text fragment");
};
format.font_family.as_deref()
}
#[test]
fn default_is_a_noop() {
assert!(!needs_snapshot_fill(&EditorTypographyDefaults::default()));
}
#[test]
fn any_set_field_requests_fill() {
assert!(needs_snapshot_fill(&EditorTypographyDefaults {
font_family: Some("X".into()),
..Default::default()
}));
assert!(needs_snapshot_fill(&EditorTypographyDefaults {
line_height: 1.5,
..Default::default()
}));
assert!(needs_snapshot_fill(&EditorTypographyDefaults {
first_line_indent: 24.0,
..Default::default()
}));
assert!(needs_snapshot_fill(&EditorTypographyDefaults {
paragraph_spacing_before: 10.0,
..Default::default()
}));
}
#[test]
fn fills_unset_body_block() {
let mut b = block("Hello", TextFormat::default(), BlockFormat::default());
apply_to_block(&mut b, &defaults());
assert_eq!(b.block_format.line_height, Some(1.5));
assert_eq!(b.block_format.text_indent, Some(28));
assert_eq!(b.block_format.top_margin, Some(12));
assert_eq!(b.block_format.bottom_margin, Some(6));
assert_eq!(frag_family(&b), Some("Literata"));
}
#[test]
fn does_not_override_explicit_values() {
let block_format = BlockFormat {
line_height: Some(2.0),
text_indent: Some(4),
top_margin: Some(3),
bottom_margin: Some(5),
..Default::default()
};
let format = TextFormat {
font_family: Some("Courier".into()),
..Default::default()
};
let mut b = block("Hi", format, block_format);
apply_to_block(&mut b, &defaults());
assert_eq!(b.block_format.line_height, Some(2.0));
assert_eq!(b.block_format.text_indent, Some(4));
assert_eq!(b.block_format.top_margin, Some(3));
assert_eq!(b.block_format.bottom_margin, Some(5));
assert_eq!(frag_family(&b), Some("Courier"));
}
#[test]
fn heading_gets_leading_and_font_but_not_indent() {
let block_format = BlockFormat {
heading_level: Some(1),
..Default::default()
};
let mut b = block("Title", TextFormat::default(), block_format);
apply_to_block(&mut b, &defaults());
assert_eq!(b.block_format.line_height, Some(1.5));
assert_eq!(
b.block_format.text_indent, None,
"headings must not be first-line indented"
);
assert_eq!(
b.block_format.top_margin, None,
"headings must not get body paragraph spacing"
);
assert_eq!(frag_family(&b), Some("Literata"));
}
#[test]
fn an_empty_block_gets_a_synthetic_fragment_carrying_the_default_family() {
let mut b = block("", TextFormat::default(), BlockFormat::default());
b.fragments.clear();
apply_to_block(&mut b, &defaults());
assert_eq!(
b.fragments.len(),
1,
"a fragment-less block must gain one synthetic fragment, or the \
typesetter measures its empty line in the registry default face \
and the line resizes on the first keystroke"
);
let FragmentContent::Text {
text,
format,
length,
..
} = &b.fragments[0]
else {
panic!("expected a text fragment");
};
assert!(text.is_empty());
assert_eq!(*length, 0);
assert_eq!(format.font_family.as_deref(), Some("Literata"));
}
#[test]
fn no_family_default_synthesizes_no_fragment() {
let mut b = block("", TextFormat::default(), BlockFormat::default());
b.fragments.clear();
let d = EditorTypographyDefaults {
first_line_indent: 28.0,
..Default::default()
};
apply_to_block(&mut b, &d);
assert!(
b.fragments.is_empty(),
"without a family default the synthetic fragment would change \
nothing — the fill must stay a no-op"
);
}
#[test]
fn a_block_with_fragments_gains_no_synthetic_one() {
let mut b = block("Hello", TextFormat::default(), BlockFormat::default());
apply_to_block(&mut b, &defaults());
assert_eq!(b.fragments.len(), 1);
}
#[test]
fn list_item_is_not_indented() {
let mut b = block("Item", TextFormat::default(), BlockFormat::default());
b.list_info = Some(ListInfo {
list_id: 1,
style: ListStyle::Disc,
indent: 0,
marker: "•".into(),
item_index: 0,
});
apply_to_block(&mut b, &defaults());
assert_eq!(
b.block_format.text_indent, None,
"list items must not be first-line indented"
);
assert_eq!(b.block_format.line_height, Some(1.5));
}
}