fix: differentiate style presets with wider font/line/paragraph spacing values, apply paragraph_spacing
This commit is contained in:
@@ -205,7 +205,7 @@ pub fn reading_view(
|
|||||||
let start = section.pages[*current_page];
|
let start = section.pages[*current_page];
|
||||||
let end = section.pages[*current_page + 1];
|
let end = section.pages[*current_page + 1];
|
||||||
let raw_text: String = section.content.chars().skip(start).take(end - start).collect();
|
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 {
|
let align = match style.alignment {
|
||||||
TextAlignment::Left => egui::Align::LEFT,
|
TextAlignment::Left => egui::Align::LEFT,
|
||||||
|
|||||||
27
src/style.rs
27
src/style.rs
@@ -31,18 +31,18 @@ impl StyleProfile {
|
|||||||
StyleProfile {
|
StyleProfile {
|
||||||
name: "紧凑".into(),
|
name: "紧凑".into(),
|
||||||
alignment: TextAlignment::Left,
|
alignment: TextAlignment::Left,
|
||||||
line_spacing: 1.4,
|
line_spacing: 1.2,
|
||||||
paragraph_spacing: 0.5,
|
paragraph_spacing: 0.3,
|
||||||
first_line_indent: 2.0,
|
first_line_indent: 2.0,
|
||||||
font_size: 18.0,
|
font_size: 16.0,
|
||||||
},
|
},
|
||||||
StyleProfile {
|
StyleProfile {
|
||||||
name: "宽松".into(),
|
name: "宽松".into(),
|
||||||
alignment: TextAlignment::Left,
|
alignment: TextAlignment::Left,
|
||||||
line_spacing: 2.2,
|
line_spacing: 2.4,
|
||||||
paragraph_spacing: 1.5,
|
paragraph_spacing: 1.8,
|
||||||
first_line_indent: 2.0,
|
first_line_indent: 2.0,
|
||||||
font_size: 22.0,
|
font_size: 24.0,
|
||||||
},
|
},
|
||||||
StyleProfile {
|
StyleProfile {
|
||||||
name: "居中".into(),
|
name: "居中".into(),
|
||||||
@@ -59,6 +59,21 @@ impl StyleProfile {
|
|||||||
self.font_size * self.line_spacing
|
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 {
|
pub fn apply_indent(text: &str, indent_chars: f32) -> String {
|
||||||
|
|||||||
Reference in New Issue
Block a user