Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PageMap: allow building synthetic pages numbers #478

Merged
merged 1 commit into from
May 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions crengine/include/lvtinydom.h
Original file line number Diff line number Diff line change
Expand Up @@ -2306,10 +2306,13 @@ class LVPageMapItem
/// PageMapItems container
class LVPageMap
{
friend class LVDocView;
friend class ldomDocument;
private:
ldomDocument * _doc;
int _valid_for_visible_page_numbers;
int _chars_per_synthetic_page; // non-0 means this pagemap has synthetic pages
bool _is_document_provided;
bool _has_document_provided;
lString32 _source;
LVPtrVector<LVPageMapItem> _children;
void addPage( LVPageMapItem * item ) {
Expand Down Expand Up @@ -2341,12 +2344,21 @@ class LVPageMap
_valid_for_visible_page_numbers = visible_page_numbers;
}
void invalidatePageInfo() { _valid_for_visible_page_numbers = 0; }
// Whether this page map comes from the document itself
void setIsDocumentProvided( bool is_document_provided ) {
_is_document_provided = is_document_provided;
if ( _is_document_provided ) // If this was called once with true, the document provides some pagemap
_has_document_provided = true;
}
bool isDocumentProvided() const { return _is_document_provided; }
bool hasDocumentProvided() const { return _has_document_provided; }
int isSynthetic() const { return _chars_per_synthetic_page; }
// Page source (info about the book paper version the page labels reference)
void setSource( lString32 source ) { _source = source; }
lString32 getSource() const { return _source; }
// root node constructor
LVPageMap( ldomDocument * doc )
: _doc(doc), _valid_for_visible_page_numbers(0) { }
: _doc(doc), _valid_for_visible_page_numbers(0), _chars_per_synthetic_page(0), _is_document_provided(false), _has_document_provided(false) { }
~LVPageMap() { clear(); }
};

Expand Down Expand Up @@ -2513,6 +2525,8 @@ class ldomDocument : public lxmlDocBase

/// returns pointer to PageMapItems container
LVPageMap * getPageMap() { return &m_pagemap; }
/// generate synthetic page map
void buildSyntheticPageMap( int chars_per_synthetic_page );

#if BUILD_LITE!=1
bool isTocFromCacheValid() { return _toc_from_cache_valid; }
Expand Down
7 changes: 5 additions & 2 deletions crengine/src/epubfmt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1551,8 +1551,11 @@ bool ImportEpubDocument( LVStreamRef stream, ldomDocument * m_doc, LVDocViewCall
}
}

if ( m_doc->getPageMap()->getChildCount() > 0 && !pageMapSource.empty() )
m_doc->getPageMap()->setSource(pageMapSource);
if ( m_doc->getPageMap()->getChildCount() > 0 ) {
m_doc->getPageMap()->setIsDocumentProvided(true);
if ( !pageMapSource.empty() )
m_doc->getPageMap()->setSource(pageMapSource);
}

writer.OnTagClose(U"", U"body");
writer.OnStop();
Expand Down
58 changes: 54 additions & 4 deletions crengine/src/lvtinydom.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ extern const int gDOMVersionCurrent = DOM_VERSION_CURRENT;

/// change in case of incompatible changes in swap/cache file format to avoid using incompatible swap file
// increment to force complete reload/reparsing of old file
#define CACHE_FILE_FORMAT_VERSION "3.05.67k"
#define CACHE_FILE_FORMAT_VERSION "3.05.68k"
/// increment following value to force re-formatting of old book after load
#define FORMATTING_VERSION_ID 0x002D

Expand Down Expand Up @@ -19966,6 +19966,48 @@ void ldomDocument::buildAlternativeToc()
_toc_from_cache_valid = false; // to force update of page numbers
}

void ldomDocument::buildSyntheticPageMap( int chars_per_synthetic_page )
{
m_pagemap._is_document_provided = false;
if ( m_pagemap._chars_per_synthetic_page == chars_per_synthetic_page ) {
return; // already done
}
m_pagemap.clear();
if ( chars_per_synthetic_page <= 0 ) {
m_pagemap._chars_per_synthetic_page = 0;
return;
}
printf("CRE: building synthetic page map (%d)\n", chars_per_synthetic_page);
m_pagemap._chars_per_synthetic_page = chars_per_synthetic_page;
ldomXPointerEx xp = ldomXPointerEx( getRootNode(), 0 );
int page_num = 0;
int count = 0; // chars needed until new page
// We loop thru all text nodes, whether visible or not (as visiblity can be changed
// with styles, and we want this to be stable)
while ( xp.nextText() ) {
// But skip text from elements which would by default be hidden (<style>, <script>,
// some FB2 elements...), mostly so that a huge <style> at top of document doesn't
// generate many synthetic pages that would end up all on the first screen page.
const css_elem_def_props_t * ntype = xp.getNode()->getParentNode()->getElementTypePtr();
if ( ntype && ntype->display == css_d_none ) {
continue;
}
lString32 txt = xp.getText();
int len = txt.length();
while (len > count) {
xp.setOffset(xp.getOffset() + count);
len -= count;
count = chars_per_synthetic_page;
page_num++;
lString32 title;
title.appendDecimal(page_num);
m_pagemap.addPage(title, ldomXPointer(xp.getNode(), xp.getOffset()), lString32::empty_str);
}
count -= len;
}
// cache file will have to be updated with the new page map
setCacheFileStale(true);
}
/// returns position pointer
ldomXPointer LVPageMapItem::getXPointer()
{
Expand Down Expand Up @@ -20037,7 +20079,9 @@ bool LVPageMapItem::deserialize( ldomDocument * doc, SerialBuf & buf )
/// serialize to byte array (pointer will be incremented by number of bytes written)
bool LVPageMap::serialize( SerialBuf & buf )
{
buf << (lUInt32)_valid_for_visible_page_numbers << (lUInt32)_children.length() << _source;
buf << (lUInt32)_valid_for_visible_page_numbers << (lUInt32)_chars_per_synthetic_page
<< (lUInt32)_is_document_provided << (lUInt32)_has_document_provided
<< (lUInt32)_children.length() << _source;
if ( buf.error() )
return false;
for ( int i=0; i<_children.length(); i++ ) {
Expand All @@ -20053,12 +20097,18 @@ bool LVPageMap::deserialize( ldomDocument * doc, SerialBuf & buf )
{
if ( buf.error() )
return false;
lUInt32 childCount = 0;
lUInt32 validForVisiblePageNumbers = 0;
buf >> validForVisiblePageNumbers >> childCount >> _source;
lUInt32 charsPerSyntheticPage = 0;
lUInt32 isDocumentProvided = 0;
lUInt32 hasDocumentProvided = 0;
lUInt32 childCount = 0;
buf >> validForVisiblePageNumbers >> charsPerSyntheticPage >> isDocumentProvided >> hasDocumentProvided >> childCount >> _source;
if ( buf.error() )
return false;
_valid_for_visible_page_numbers = (int)validForVisiblePageNumbers;
_chars_per_synthetic_page = (int)charsPerSyntheticPage;
_is_document_provided = (bool)isDocumentProvided;
_has_document_provided = (bool)hasDocumentProvided;
for ( int i=0; i<childCount; i++ ) {
LVPageMapItem * item = new LVPageMapItem(doc);
if ( !item->deserialize( doc, buf ) ) {
Expand Down