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

Fix FB2 image sizing & centering, and bottom border on empty block elements #563

Merged
merged 3 commits into from
Apr 7, 2024
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion cr3gui/data/fb2.css
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ a { display: inline; text-decoration: underline; }
a[type="note"] { vertical-align: super; font-size: 70%; text-decoration: none }
a[type="note"]::before { content: "\2060" } /* word joiner, avoid break before */

image { text-align: center; text-indent: 0; display: block }
image { text-indent: 0; display: block; margin: 0 auto; }
p image { display: inline }
li image { display: inline }

Expand Down
7 changes: 4 additions & 3 deletions crengine/src/epubfmt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2068,14 +2068,15 @@ bool ImportEpubDocument( LVStreamRef stream, ldomDocument * m_doc, LVDocViewCall

if ( fontList.length() != fontList_nb_before_head_parsing ) {
// New fonts met when parsing <head><style> of some DocFragments
// Drop styles (before unregistering fonts, as they may reference them)
m_doc->forceReinitStyles();
// todo: we could avoid forceReinitStyles() when embedded fonts are disabled
// (but being here is quite rare - and having embedded font disabled even more)
m_doc->unregisterEmbeddedFonts();
// set document font list, and register fonts
m_doc->getEmbeddedFontList().set(fontList);
m_doc->registerEmbeddedFonts();
printf("CRE: document loaded, but styles re-init needed (cause: embedded fonts)\n");
m_doc->forceReinitStyles();
// todo: we could avoid forceReinitStyles() when embedded fonts are disabled
// (but being here is quite rare - and having embedded font disabled even more)
}

// fragmentCount is not fool proof, best to check if we really made
Expand Down
94 changes: 58 additions & 36 deletions crengine/src/lvrend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3918,35 +3918,47 @@ void renderFinalBlock( ldomNode * enode, LFormattedText * txform, RenderRectAcce
// If block image, forget any current flags and start from baseflags (?)
lUInt32 flags = styleToTextFmtFlags( true, enode->getStyle(), baseflags, direction );
flags |= linkflags;
//txform->AddSourceLine(U"title", 5, 0x000000, 0xffffff, font, baseflags, interval, margin, NULL, 0, 0);
LVFontRef font = enode->getFont();
lUInt32 cl = getForegroundColor(style);
lUInt32 bgcl = LTEXT_COLOR_CURRENT; // erm_final: any background will be drawn by DrawDocument
lString32 title;
//txform->AddSourceLine( title.c_str(), title.length(), cl, bgcl, font, LTEXT_FLAG_OWNTEXT|LTEXT_FLAG_NEWLINE, line_h, 0, NULL );
//baseflags
title = enode->getAttributeValue(attr_suptitle);
if ( !title.empty() ) {
lString32Collection lines;
lines.parse(title, cs32("\\n"), true);
for ( int i=0; i<lines.length(); i++ )
txform->AddSourceLine( lines[i].c_str(), lines[i].length(), cl, bgcl, font.get(), lang_cfg, flags|LTEXT_FLAG_OWNTEXT, line_h, valign_dy, 0, enode );
}
txform->AddSourceObject(flags, LTEXT_OBJECT_IS_IMAGE, line_h, valign_dy, indent, enode, lang_cfg );
title = enode->getAttributeValue(attr_subtitle);
if ( !title.empty() ) {
lString32Collection lines;
lines.parse(title, cs32("\\n"), true);
for ( int i=0; i<lines.length(); i++ )
txform->AddSourceLine( lines[i].c_str(), lines[i].length(), cl, bgcl, font.get(), lang_cfg, flags|LTEXT_FLAG_OWNTEXT, line_h, valign_dy, 0, enode );
lString32 suptitle = enode->getAttributeValue(attr_suptitle);
lString32 subtitle = enode->getAttributeValue(attr_subtitle);
lString32 title = enode->getAttributeValue(attr_title);
if ( !suptitle.empty() || !subtitle.empty() || !title.empty() ) {
// If any of these exist and are not empty, we add them around the images.
// We can't easily ensure and adequate height to the image so they all fit
// on a page. We then don't need to care about setting a zero strut and line_h
// as done below with standalone block images.
LVFontRef font = enode->getFont();
lUInt32 cl = getForegroundColor(style);
lUInt32 bgcl = LTEXT_COLOR_CURRENT; // erm_final: any background will be drawn by DrawDocument
if ( !suptitle.empty() ) {
lString32Collection lines;
lines.parse(suptitle, cs32("\\n"), true);
for ( int i=0; i<lines.length(); i++ )
txform->AddSourceLine( lines[i].c_str(), lines[i].length(), cl, bgcl, font.get(), lang_cfg, flags|LTEXT_FLAG_OWNTEXT, line_h, valign_dy, 0, enode );
}
txform->AddSourceObject(flags, LTEXT_OBJECT_IS_IMAGE, line_h, valign_dy, indent, enode, lang_cfg );
if ( !subtitle.empty() ) {
lString32Collection lines;
lines.parse(subtitle, cs32("\\n"), true);
for ( int i=0; i<lines.length(); i++ )
txform->AddSourceLine( lines[i].c_str(), lines[i].length(), cl, bgcl, font.get(), lang_cfg, flags|LTEXT_FLAG_OWNTEXT, line_h, valign_dy, 0, enode );
}
if ( !title.empty() ) {
lString32Collection lines;
lines.parse(title, cs32("\\n"), true);
for ( int i=0; i<lines.length(); i++ )
txform->AddSourceLine( lines[i].c_str(), lines[i].length(), cl, bgcl, font.get(), lang_cfg, flags|LTEXT_FLAG_OWNTEXT, line_h, valign_dy, 0, enode );
}
}
title = enode->getAttributeValue(attr_title);
if ( !title.empty() ) {
lString32Collection lines;
lines.parse(title, cs32("\\n"), true);
for ( int i=0; i<lines.length(); i++ )
txform->AddSourceLine( lines[i].c_str(), lines[i].length(), cl, bgcl, font.get(), lang_cfg, flags|LTEXT_FLAG_OWNTEXT, line_h, valign_dy, 0, enode );
else {
if ( rm == erm_final ) {
// Do as just below for non-FB2 images, so a standalone image won't exceed the page height.
txform->setStrut(0, 0);
line_h = 0;
indent = 0;
}
txform->AddSourceObject(flags, LTEXT_OBJECT_IS_IMAGE, line_h, valign_dy, indent, enode, lang_cfg );
}

}
else if ( isBlock ) {
// Block image in HTML
Expand Down Expand Up @@ -5882,11 +5894,7 @@ class FlowState {
last_split_after_flag = RN_GET_SPLIT_AFTER(flags);
}

int addContentLine( int height, int flags, int baseline=NO_BASELINE_UPDATE, bool is_padding=false ) {
// As we may push vertical margins, we return the total height moved
// (needed when adding bottom padding that may push inner vertical
// margins, which should be accounted in the element height).
int start_c_y = c_y;
void addContentLine( int height, int flags, int baseline=NO_BASELINE_UPDATE, bool is_padding=false ) {
int line_dir_flag = direction == REND_DIRECTION_RTL ? RN_LINE_IS_RTL : 0;
// Ensure avoid_pb_inside
if ( avoid_pb_inside_just_toggled_off ) {
Expand Down Expand Up @@ -5964,7 +5972,6 @@ class FlowState {
}
}
}
return c_y - start_c_y;
}

void addContentSpace( int height, int line_h, bool split_avoid_before,
Expand Down Expand Up @@ -8548,9 +8555,24 @@ void renderBlockElementEnhanced( FlowState * flow, ldomNode * enode, int x, int
// (Firefox, with a float taller than text, both in another
// float, applies bottom padding after the inner float)
if (padding_bottom>0) {
int padding_bottom_with_inner_pushed_vm = flow->addContentLine(padding_bottom, RN_SPLIT_BEFORE_AVOID, 0, true);
h += padding_bottom_with_inner_pushed_vm;
bottom_overflow -= padding_bottom_with_inner_pushed_vm;
// We may push any inner vertical margin: gather how much we moved
int c_y = flow->getCurrentAbsoluteY();
flow->addContentLine(padding_bottom, RN_SPLIT_BEFORE_AVOID, 0, true);
int padding_bottom_with_inner_pushed_vm = flow->getCurrentAbsoluteY() - c_y;
if (h <= 0) {
// Empty block: any pushed vertical margin can be put outside this block
// Note: this different behaviour seems needed for this bottom padding/border
// to be drawn at the expected position. Not really sure what happens, it
// might be that pushVerticalMargin() shifts or not our node differently if
// it happens to have no content and didn't call other flow methods...
h += padding_bottom;
}
else {
// We have some content/height: any pushed vertical margin
// is inside and part of our block height
h += padding_bottom_with_inner_pushed_vm;
bottom_overflow -= padding_bottom_with_inner_pushed_vm;
}
}

if (h <=0) {
Expand Down
2 changes: 1 addition & 1 deletion crengine/src/lvtinydom.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ extern const int gDOMVersionCurrent = DOM_VERSION_CURRENT;
// increment to force complete reload/reparsing of old file
#define CACHE_FILE_FORMAT_VERSION "3.05.74k"
/// increment following value to force re-formatting of old book after load
#define FORMATTING_VERSION_ID 0x0032
#define FORMATTING_VERSION_ID 0x0033

#ifndef DOC_DATA_COMPRESSION_LEVEL
/// data compression level (0=no compression, 1=fast compressions, 3=normal compression)
Expand Down