Skip to content

Instantly share code, notes, and snippets.

@dmalinovsky
Created April 2, 2024 23:02
Show Gist options
  • Save dmalinovsky/69fd68af7848008b1cc80a76961f2d77 to your computer and use it in GitHub Desktop.
Save dmalinovsky/69fd68af7848008b1cc80a76961f2d77 to your computer and use it in GitHub Desktop.
Patch for KOReader to build logical page count
local ReaderPageMap = require("apps/reader/modules/readerpagemap")
local postInit_orig = ReaderPageMap._postInit
ReaderPageMap._postInit = function(self)
self.ui.document:buildSyntheticPageMapIfNoneDocumentProvided(1818)
-- Run original code
postInit_orig(self)
end
@dmalinovsky
Copy link
Author

My patch is simply calling buildSyntheticPageMapIfNoneDocumentProvided function. It checks if there's no existing page map and then marks a page every N symbols (in my case, 1818).

Here's a corresponding change in crengine: koreader/crengine#478. The author should look there.

@edo-jan
Copy link

edo-jan commented Aug 16, 2024

Thank you Denis!

@NQK
Copy link

NQK commented Mar 27, 2025

I was looking for something like this. My purpose is to see if one book is really longer than another. I use 1024 for no reason.

It's a pity that everyone has to invent their own counting methods.

How about a patch to use the ADE counting method? Why? I'm sure Pocketbook is using it and Calibre has a plugin to count pages using this. Somehow when I enter the 1024 to that plugin it doesn't return the number generated by current patch as shown in Koreader.

@dmalinovsky
Copy link
Author

I looked into it but it’s impossible (read: too tricky) to use from KOReader. ADE is using 1K of the compressed bytes as a single page and KOReader operates on uncompressed data.

@dmalinovsky
Copy link
Author

Here's the relevant comment in the issue: koreader/koreader#9020 (comment)

@NQK
Copy link

NQK commented Mar 29, 2025

Thank you.

So there is no way to automatically fill the calibre database with the reference/logical page, I guess.

@dmalinovsky
Copy link
Author

I think it should be possible to fill in the reference pages in bulk with Calibre. And then KOReader should use them.

@sgrodnik
Copy link

Thank you @dmalinovsky! How did you calculate 1818? I used 2048 as a common value, but I'm curious about your reasoning

@dmalinovsky
Copy link
Author

Thank you @dmalinovsky! How did you calculate 1818? I used 2048 as a common value, but I'm curious about your reasoning

It’s just Russian publishing specific number — 40,000 characters per 22 pages equals 1,818 characters.

@idontknowtocodeanything

How can I find a common value for Brazilian Portuguese publishing specific number? I'm using the default one in this patch. Thanks in advance!

@dmalinovsky
Copy link
Author

How can I find a common value for Brazilian Portuguese publishing specific number? I'm using the default one in this patch. Thanks in advance!

I don’t think you’ll find an exact number. The one I’m using is good enough. Feel free to tweak it.

@NQK
Copy link

NQK commented Sep 23, 2025

I think it should be possible to fill in the reference pages in bulk with Calibre. And then KOReader should use them.

I can fill the reference pages in bulk and download the books via opsd (I think the metadata is embedded in the process). But how can I tell KOReader to USE that value? Let's say I have it stored it a custom field in calibre named as "#k_pages"

@NQK
Copy link

NQK commented Sep 23, 2025

And is it possible to apply the logical pages in other places? I have noticed the "Book Information" panel now doesn't use that value. "Alternative status bar" doesn't use it either.

@dmalinovsky
Copy link
Author

I think it should be possible to fill in the reference pages in bulk with Calibre. And then KOReader should use them.

I can fill the reference pages in bulk and download the books via opsd (I think the metadata is embedded in the process). But how can I tell KOReader to USE that value? Let's say I have it stored it a custom field in calibre named as "#k_pages"

It doesn't work like that, but it's possible to pass reference pages from Calibrea to KOReader: koreader/koreader#13869. Note that there was a bug in Calibre recently breaking this.

And is it possible to apply the logical pages in other places? I have noticed the "Book Information" panel now doesn't use that value. "Alternative status bar" doesn't use it either.

This is up to the devs. Reading stats also don't support it: koreader/koreader#13345

Maybe I'll take a look some day but it's not a priority for me.

@dmalinovsky
Copy link
Author

This gist will be superseded by koreader/koreader#14405.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment