fix(pagination): recalculate pages using actual text area size, fix char_width for Chinese

This commit is contained in:
Developer
2026-05-15 19:43:40 +08:00
parent eecab02ead
commit 4e79181e07

View File

@@ -4,14 +4,17 @@ use crate::style::{StyleProfile, TextAlignment};
use crate::theme::{self, BgType, Theme}; use crate::theme::{self, BgType, Theme};
pub fn recalculate_pages(book: &mut Book, font_size: f32, line_height: f32, panel_width: f32, panel_height: f32) { pub fn recalculate_pages(book: &mut Book, font_size: f32, line_height: f32, panel_width: f32, panel_height: f32) {
let char_width = font_size * 0.6; // 中文全角字符宽度 ≈ 0.85 × font_size含安全余量
let char_width = font_size * 0.85;
// 安全系数:留 10% 余量抵消段落间距/缩进等样式增加的内容
let safety = 0.9;
let chars_per_line = if char_width > 0.0 { let chars_per_line = if char_width > 0.0 {
(panel_width / char_width).max(1.0) as usize ((panel_width / char_width) * safety).max(1.0) as usize
} else { } else {
1 1
}; };
let lines_per_page = if line_height > 0.0 { let lines_per_page = if line_height > 0.0 {
(panel_height / line_height).max(1.0) as usize ((panel_height / line_height) * safety).max(1.0) as usize
} else { } else {
1 1
}; };
@@ -55,10 +58,7 @@ pub fn reading_view(
}; };
let mut jump_to_bookmark: Option<usize> = None; let mut jump_to_bookmark: Option<usize> = None;
let panel_size = ui.available_size(); let colors = theme::reader_colors(theme);
recalculate_pages(book, style.font_size, style.line_height(), panel_size.x, panel_size.y);
let colors = theme::reader_colors(theme);
let has_bookmark = bookmarks.iter().any(|b| b.section == *current_section && b.page == *current_page); let has_bookmark = bookmarks.iter().any(|b| b.section == *current_section && b.page == *current_page);
// --- Sidebar (TOC + Bookmarks) --- // --- Sidebar (TOC + Bookmarks) ---
@@ -217,11 +217,14 @@ egui::ComboBox::from_id_salt("bg_type_selector")
let (rect, response) = ui.allocate_at_least(available, egui::Sense::click()); let (rect, response) = ui.allocate_at_least(available, egui::Sense::click());
// Add reading margins (inset) // Add reading margins (inset)
let inset = 24.0; let inset = 24.0;
let text_rect = egui::Rect::from_min_size( let text_rect = egui::Rect::from_min_size(
egui::pos2(rect.min.x + inset, rect.min.y), egui::pos2(rect.min.x + inset, rect.min.y),
egui::vec2((rect.width() - inset * 2.0).max(100.0), rect.height()), egui::vec2((rect.width() - inset * 2.0).max(100.0), rect.height()),
); );
// 在文本区域内用实际可用宽高重新分页
recalculate_pages(book, style.font_size, style.line_height(), text_rect.width(), text_rect.height());
if let Some(section) = book.sections.get(*current_section) { if let Some(section) = book.sections.get(*current_section) {
if *current_page < section.pages.len().saturating_sub(1) { if *current_page < section.pages.len().saturating_sub(1) {