fix(pagination): use accurate text area dimensions and char_width=1.0 for Chinese, sync pages before toolbar

This commit is contained in:
Developer
2026-05-15 20:00:22 +08:00
parent 4e79181e07
commit 1cb0c2aef2

View File

@@ -4,10 +4,10 @@ use crate::style::{StyleProfile, TextAlignment};
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) {
// 中文全角字符宽度 ≈ 0.85 × font_size(含安全余量)
let char_width = font_size * 0.85;
// 安全系数:留 10% 余量抵消段落间距/缩进等样式增加的内容
let safety = 0.9;
// 中文全角字符宽度 ≈ 1.0 × font_size
let char_width = font_size * 1.0;
// 安全系数 0.97:抵消段落间距/缩进等样式增加的内容
let safety = 0.97;
let chars_per_line = if char_width > 0.0 {
((panel_width / char_width) * safety).max(1.0) as usize
} else {
@@ -59,6 +59,12 @@ pub fn reading_view(
let mut jump_to_bookmark: Option<usize> = None;
let colors = theme::reader_colors(theme);
// 工具栏之前预估文本区尺寸完成分页(扣除边距 48px + 工具条 ~60px
let panel_size = ui.available_size();
let est_width = (panel_size.x - 48.0).max(100.0);
let est_height = (panel_size.y - 60.0).max(200.0);
recalculate_pages(book, style.font_size, style.line_height(), est_width, est_height);
let has_bookmark = bookmarks.iter().any(|b| b.section == *current_section && b.page == *current_page);
// --- Sidebar (TOC + Bookmarks) ---
@@ -217,16 +223,13 @@ egui::ComboBox::from_id_salt("bg_type_selector")
let (rect, response) = ui.allocate_at_least(available, egui::Sense::click());
// Add reading margins (inset)
let inset = 24.0;
let text_rect = egui::Rect::from_min_size(
egui::pos2(rect.min.x + inset, rect.min.y),
egui::vec2((rect.width() - inset * 2.0).max(100.0), rect.height()),
);
let inset = 24.0;
let text_rect = egui::Rect::from_min_size(
egui::pos2(rect.min.x + inset, rect.min.y),
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) {
let start = section.pages[*current_page];
let end = section.pages[*current_page + 1];