From 4655ad5d31c4dadf7953589d00511ce22d75a1ea Mon Sep 17 00:00:00 2001 From: Developer Date: Thu, 14 May 2026 21:13:49 +0800 Subject: [PATCH] fix: differentiate style presets with wider font/line/paragraph spacing values, apply paragraph_spacing --- src/reader.rs | 2 +- src/style.rs | 27 +++++++++++++++++++++------ 2 files changed, 22 insertions(+), 7 deletions(-) diff --git a/src/reader.rs b/src/reader.rs index 9330c3b..ce06281 100644 --- a/src/reader.rs +++ b/src/reader.rs @@ -205,7 +205,7 @@ pub fn reading_view( let start = section.pages[*current_page]; let end = section.pages[*current_page + 1]; let raw_text: String = section.content.chars().skip(start).take(end - start).collect(); - let indented = crate::style::apply_indent(&raw_text, style.first_line_indent); + let indented = style.apply_to_text(&raw_text); let align = match style.alignment { TextAlignment::Left => egui::Align::LEFT, diff --git a/src/style.rs b/src/style.rs index 0e9d9a3..9ffe463 100644 --- a/src/style.rs +++ b/src/style.rs @@ -31,18 +31,18 @@ impl StyleProfile { StyleProfile { name: "紧凑".into(), alignment: TextAlignment::Left, - line_spacing: 1.4, - paragraph_spacing: 0.5, + line_spacing: 1.2, + paragraph_spacing: 0.3, first_line_indent: 2.0, - font_size: 18.0, + font_size: 16.0, }, StyleProfile { name: "宽松".into(), alignment: TextAlignment::Left, - line_spacing: 2.2, - paragraph_spacing: 1.5, + line_spacing: 2.4, + paragraph_spacing: 1.8, first_line_indent: 2.0, - font_size: 22.0, + font_size: 24.0, }, StyleProfile { name: "居中".into(), @@ -59,6 +59,21 @@ impl StyleProfile { self.font_size * self.line_spacing } + pub fn apply_to_text(&self, text: &str) -> String { + let indented = apply_indent(text, self.first_line_indent); + apply_para_spacing(&indented, self.paragraph_spacing) + } + +} + +fn apply_para_spacing(text: &str, spacing: f32) -> String { + if spacing <= 0.5 { + return text.to_string(); + } + // Each unit of paragraph_spacing adds one extra blank line + let extra_nls = (spacing * 2.0).round() as usize; + let sep = "\n".repeat(extra_nls); + text.replace("\n\n", &sep) } pub fn apply_indent(text: &str, indent_chars: f32) -> String {