Reflow deterministico: stile iniettato nell'HTML del capitolo (chapterHtml+loadHtml), niente JS post-load; runJavaScript solo per lo scroll
This commit is contained in:
+28
-26
@@ -53,10 +53,15 @@ Page {
|
||||
if (!loaded)
|
||||
return
|
||||
loadWatchdog.stop()
|
||||
if (page.restoring)
|
||||
page.restoring = false
|
||||
page.ready = true
|
||||
applyFontScale()
|
||||
// ripristina la posizione in frazione sull'altezza ricalcolata
|
||||
if (page.chapterFraction > 0.01) {
|
||||
web.runJavaScript(
|
||||
"window.scrollTo(0," + page.chapterFraction
|
||||
+ "*Math.max(1,document.documentElement.scrollHeight-window.innerHeight));true",
|
||||
function() { })
|
||||
}
|
||||
page.restoring = false
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -224,33 +229,32 @@ Page {
|
||||
page.chapterFraction = 0
|
||||
}
|
||||
page.currentChapter = index
|
||||
page.restoring = true
|
||||
page.ready = false
|
||||
web.url = "file://" + book.chapterFilePath(index)
|
||||
loadChapter()
|
||||
}
|
||||
|
||||
function changeFontScale(delta) {
|
||||
page.fontScale = Math.max(0.7, Math.min(2.5, page.fontScale + delta))
|
||||
applyFontScale()
|
||||
// ricarica il capitolo con la nuova dimensione: il reflow avviene al load
|
||||
loadChapter()
|
||||
}
|
||||
|
||||
// reflow: imposta il font-size del documento (il testo si ri-avvolge)
|
||||
// e ripristina la posizione in frazione sull'altezza ricalcolata.
|
||||
// Serve uno <style> con !important: molti EPUB impostano px su body/p
|
||||
// che vincerebbero sul semplice font-size dell'html
|
||||
function applyFontScale() {
|
||||
if (!web.loaded)
|
||||
function fontPx() {
|
||||
return Math.round(16 * page.fontScale * 100) / 100
|
||||
}
|
||||
|
||||
// carica il capitolo corrente via loadHtml: lo stile di reflow (font-size)
|
||||
// è iniettato nell'HTML dal parser, quindi niente JS post-load
|
||||
function loadChapter() {
|
||||
var html = book.chapterHtml(page.currentChapter, page.fontPx())
|
||||
if (html.length === 0) {
|
||||
page.errorMessage = "Capitolo non leggibile"
|
||||
page.ready = true
|
||||
return
|
||||
var px = Math.round(16 * page.fontScale * 100) / 100
|
||||
var script = "(function(){"
|
||||
+ "var s=document.getElementById('cw-font');"
|
||||
+ "if(!s){s=document.createElement('style');s.id='cw-font';document.head.appendChild(s);}"
|
||||
+ "s.textContent='html,body{font-size:" + px + "px!important}"
|
||||
+ "p,div,li,td,blockquote,section{font-size:1em!important}';"
|
||||
+ "window.scrollTo(0," + page.chapterFraction
|
||||
+ "*Math.max(1,document.documentElement.scrollHeight-window.innerHeight));"
|
||||
+ "true})()"
|
||||
web.runJavaScript(script, function() { })
|
||||
}
|
||||
page.ready = false
|
||||
page.restoring = true
|
||||
loadWatchdog.restart()
|
||||
web.loadHtml(html, book.chapterBaseUrl(page.currentChapter))
|
||||
}
|
||||
|
||||
function showChapterList() {
|
||||
@@ -280,9 +284,7 @@ Page {
|
||||
page.currentChapter = Math.max(0, Math.min(ch, book.chapterCount - 1))
|
||||
page.chapterFraction = fr
|
||||
}
|
||||
page.ready = false
|
||||
loadWatchdog.restart()
|
||||
web.url = "file://" + book.chapterFilePath(page.currentChapter)
|
||||
loadChapter()
|
||||
}
|
||||
|
||||
onStatusChanged: {
|
||||
|
||||
Reference in New Issue
Block a user