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

Allow calculating a nicer normal line-height #603

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
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
7 changes: 5 additions & 2 deletions crengine/include/lvstyles.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,10 @@ enum css_style_rec_important_bit {
imp_bit_box_sizing,
imp_bit_caption_side,
imp_bit_content,
imp_bit_cr_hint
imp_bit_cr_hint,
imp_bit_cr_normal_line_height
};
#define NB_IMP_BITS 70 // The number of lines in the enum above: KEEP IT UPDATED.
#define NB_IMP_BITS 71 // The number of lines in the enum above: KEEP IT UPDATED.

#define NB_IMP_SLOTS ((NB_IMP_BITS-1)>>5)+1
// In lvstyles.cpp, we have hardcoded important[0] ... importance[2]
Expand Down Expand Up @@ -185,6 +186,7 @@ struct css_style_rec_tag {
css_caption_side_t caption_side;
lString32 content;
css_length_t cr_hint;
css_length_t cr_normal_line_height;
// The following should only be used when applying stylesheets while in lvend.cpp setNodeStyle(),
// and cleaned up there, before the style is cached and shared. They are not serialized.
lInt8 flags; // bitmap of STYLE_REC_FLAG_*
Expand Down Expand Up @@ -243,6 +245,7 @@ struct css_style_rec_tag {
, box_sizing(css_bs_content_box)
, caption_side(css_cs_inherit)
, cr_hint(css_val_inherited, 0)
, cr_normal_line_height(css_val_inherited, 0)
, flags(0)
, pseudo_elem_before_style(NULL)
, pseudo_elem_after_style(NULL)
Expand Down
30 changes: 27 additions & 3 deletions crengine/src/lvrend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3129,7 +3129,14 @@ lString32 renderListItemMarker( ldomNode * enode, int & marker_width, int * fina
if (line_h < 0) { // -1, not specified by caller: find it out from the node
if ( style->line_height.type == css_val_unspecified &&
style->line_height.value == css_generic_normal ) {
line_h = font->getHeight(); // line-height: normal
int em = font->getSize();
if ( style->cr_normal_line_height.type == css_val_unspecified &&
style->cr_normal_line_height.value == css_generic_normal ) {
line_h = font->getHeight(); // line-height: normal
}
else {
line_h = lengthToPx(enode, style->cr_normal_line_height, em, em, true);
}
Comment on lines +3132 to +3139
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can shove the int em = font->getSize(); in the else {} branch.

}
else {
int em = font->getSize();
Expand Down Expand Up @@ -3400,7 +3407,13 @@ void renderFinalBlock( ldomNode * enode, LFormattedText * txform, RenderRectAcce
// Only "normal" uses enode->getFont()->getHeight()
if ( style->line_height.type == css_val_unspecified &&
style->line_height.value == css_generic_normal ) {
line_h = enode->getFont()->getHeight(); // line-height: normal
if ( style->cr_normal_line_height.type == css_val_unspecified &&
style->cr_normal_line_height.value == css_generic_normal ) {
line_h = enode->getFont()->getHeight(); // line-height: normal
}
else {
line_h = lengthToPx(enode, style->cr_normal_line_height, em, em, true);
}
}
else {
// In all other cases (%, em, unitless/unspecified), we can just scale 'em',
Expand Down Expand Up @@ -4584,6 +4597,8 @@ void copystyle( css_style_ref_t source, css_style_ref_t dest )
dest->content = source->content ;
dest->cr_hint.type = source->cr_hint.type ;
dest->cr_hint.value = source->cr_hint.value ;
dest->cr_normal_line_height.type = source->cr_normal_line_height.type ;
dest->cr_normal_line_height.value = source->cr_normal_line_height.value ;
}

// Only used by renderBlockElementLegacy()
Expand Down Expand Up @@ -7455,7 +7470,13 @@ void renderBlockElementEnhanced( FlowState * flow, ldomNode * enode, int x, int
int line_h;
if ( style->line_height.type == css_val_unspecified &&
style->line_height.value == css_generic_normal ) {
line_h = enode->getFont()->getHeight(); // line-height: normal
if ( style->cr_normal_line_height.type == css_val_unspecified &&
style->cr_normal_line_height.value == css_generic_normal ) {
line_h = enode->getFont()->getHeight(); // line-height: normal
}
else {
line_h = lengthToPx(enode, style->cr_normal_line_height, em, em, true);
}
}
else {
// In all other cases (%, em, unitless/unspecified), we can just
Expand Down Expand Up @@ -11205,6 +11226,9 @@ void setNodeStyle( ldomNode * enode, css_style_ref_t parent_style, LVFontRef par
break;
}
}
if (pstyle->cr_normal_line_height.type == css_val_inherited) {
pstyle->cr_normal_line_height = parent_style->cr_normal_line_height; // inherit as-is
}
Comment on lines 11227 to +11231
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just asking in case you did wonder and your brain worked better than mine:
No need to do as in the previous if (pstyle->line_height.type == css_val_inherited) { just above, in case one uses line-height: normal; -cr-normal-line-height: 120% or 1.2em on the parent and line-height: inherit; -cr-normal-line-height: inherit on the child? (or some other kind of combination like that... my brain hurts trying to think about that :/)


// vertical_align: computed value: "for percentage and length values, the absolute length, otherwise the keyword as specified"
// Not inherited by default (html5.css has TR,TD,TH explicitely set to "vertical-align: inherit",
Expand Down
11 changes: 9 additions & 2 deletions crengine/src/lvstsheet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ enum css_decl_code {
cssd_cr_hint,
cssd_cr_only_if,
cssd_cr_apply_func,
cssd_cr_normal_line_height,
cssd_stop
};

Expand Down Expand Up @@ -252,6 +253,7 @@ static const char * css_decl_name[] = {
"-cr-hint",
"-cr-only-if",
"-cr-apply-func",
"-cr-normal-line-height",
NULL
};

Expand Down Expand Up @@ -3916,6 +3918,7 @@ bool LVCssDeclaration::parse( const char * &decl, bool higher_importance, lxmlDo
// no named value found, don't break: continue checking if value is a number
case cssd_line_height:
case cssd_letter_spacing:
case cssd_cr_normal_line_height:
IF_g_PUSH_LENGTH_AND_break(1, true, css_val_unspecified, css_generic_normal);
case cssd_font_size:
IF_g_PUSH_LENGTH_AND_break(1, true, css_val_rem, 256);
Expand Down Expand Up @@ -3964,11 +3967,11 @@ bool LVCssDeclaration::parse( const char * &decl, bool higher_importance, lxmlDo
accept_none = true;
// only line-height and letter-spacing accept keyword "normal"
bool accept_normal = false;
if ( prop_code==cssd_line_height || prop_code==cssd_letter_spacing )
if ( prop_code==cssd_line_height || prop_code==cssd_letter_spacing || prop_code==cssd_cr_normal_line_height)
accept_normal = true;
// only line-height accepts numbers with unspecified unit
bool accept_unspecified = false;
if ( prop_code==cssd_line_height )
if ( prop_code==cssd_line_height || prop_code==cssd_cr_normal_line_height )
accept_unspecified = true;
// only font-size is... font-size
bool is_font_size = false;
Expand Down Expand Up @@ -5085,6 +5088,10 @@ void LVCssDeclaration::apply( css_style_rec_t * style, const ldomNode * node ) c
style->Apply( read_length(p), &style->line_height, imp_bit_line_height, is_important );
style->flags |= STYLE_REC_FLAG_INHERITABLE_APPLIED;
break;
case cssd_cr_normal_line_height:
style->Apply( read_length(p), &style->cr_normal_line_height, imp_bit_cr_normal_line_height, is_important );
style->flags |= STYLE_REC_FLAG_INHERITABLE_APPLIED;
break;
case cssd_letter_spacing:
style->Apply( read_length(p), &style->letter_spacing, imp_bit_letter_spacing, is_important );
style->flags |= STYLE_REC_FLAG_INHERITABLE_APPLIED;
Expand Down
8 changes: 6 additions & 2 deletions crengine/src/lvstyles.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ lUInt32 calcHash(font_ref_t & f)
lUInt32 calcHash(css_style_rec_t & rec)
{
if ( !rec.hash )
rec.hash = (((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((
rec.hash = ((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((
+ (lUInt32)rec.important[0]) * 31
+ (lUInt32)rec.important[1]) * 31
+ (lUInt32)rec.important[2]) * 31
Expand Down Expand Up @@ -117,6 +117,7 @@ lUInt32 calcHash(css_style_rec_t & rec)
+ (lUInt32)rec.box_sizing) * 31
+ (lUInt32)rec.caption_side) * 31
+ (lUInt32)rec.cr_hint.pack()) * 31
+ (lUInt32)rec.cr_normal_line_height.pack()) * 31
+ (lUInt32)rec.font_name.getHash()
+ (lUInt32)rec.background_image.getHash()
+ (lUInt32)rec.content.getHash());
Expand Down Expand Up @@ -198,7 +199,8 @@ bool operator == (const css_style_rec_t & r1, const css_style_rec_t & r2)
r1.box_sizing == r2.box_sizing&&
r1.caption_side == r2.caption_side&&
r1.content == r2.content&&
r1.cr_hint==r2.cr_hint;
r1.cr_hint==r2.cr_hint&&
r1.cr_normal_line_height==r2.cr_normal_line_height;
}


Expand Down Expand Up @@ -401,6 +403,7 @@ bool css_style_rec_t::serialize( SerialBuf & buf )
ST_PUT_ENUM(caption_side);
buf << content;
ST_PUT_LEN(cr_hint);
ST_PUT_LEN(cr_normal_line_height);
lUInt32 hash = calcHash(*this);
buf << hash;
return !buf.error();
Expand Down Expand Up @@ -475,6 +478,7 @@ bool css_style_rec_t::deserialize( SerialBuf & buf )
ST_GET_ENUM(css_caption_side_t, caption_side);
buf>>content;
ST_GET_LEN(cr_hint);
ST_GET_LEN(cr_normal_line_height);
lUInt32 hash = 0;
buf >> hash;
// printf("imp: %llx oldhash: %lx ", important, hash);
Expand Down
7 changes: 7 additions & 0 deletions crengine/src/lvtinydom.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5143,6 +5143,7 @@ bool ldomDocument::setRenderProps( int width, int dy, bool /*showCover*/, int /*
s->word_break = css_wb_normal;
s->caption_side = css_cs_top;
s->cr_hint = css_length_t(css_val_unspecified, CSS_CR_HINT_NONE);
s->cr_normal_line_height = css_length_t(css_val_unspecified, css_generic_normal); // derived from font metrics

//lUInt32 defStyleHash = (((_ua_stylesheet.getHash() * 31) + calcHash(_def_style))*31 + calcHash(_def_font));
//defStyleHash = defStyleHash * 31 + getDocFlags();
Expand Down Expand Up @@ -21301,6 +21302,8 @@ void runBasicTinyDomUnitTests()
style1->line_height.value = css_generic_normal; // line-height: normal
style1->cr_hint.type = css_val_unspecified;
style1->cr_hint.value = CSS_CR_HINT_NONE;
style1->cr_normal_line_height.type = css_val_unspecified;
style1->cr_normal_line_height.value = css_generic_normal;

css_style_ref_t style2;
style2 = css_style_ref_t( new css_style_rec_t );
Expand Down Expand Up @@ -21334,6 +21337,8 @@ void runBasicTinyDomUnitTests()
style2->line_height.value = css_generic_normal; // line-height: normal
style2->cr_hint.type = css_val_unspecified;
style2->cr_hint.value = CSS_CR_HINT_NONE;
style2->cr_normal_line_height.type = css_val_unspecified;
style2->cr_normal_line_height.value = css_generic_normal;

css_style_ref_t style3;
style3 = css_style_ref_t( new css_style_rec_t );
Expand Down Expand Up @@ -21367,6 +21372,8 @@ void runBasicTinyDomUnitTests()
style3->line_height.value = css_generic_normal; // line-height: normal
style3->cr_hint.type = css_val_unspecified;
style3->cr_hint.value = CSS_CR_HINT_NONE;
style3->cr_normal_line_height.type = css_val_unspecified;
style3->cr_normal_line_height.value = css_generic_normal;

el1->setStyle(style1);
css_style_ref_t s1 = el1->getStyle();
Expand Down