Skip to content

Commit

Permalink
Allow calculating a nicer normal line-height
Browse files Browse the repository at this point in the history
  • Loading branch information
moben committed Sep 6, 2024
1 parent e2c62ef commit ab4cf26
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 3 deletions.
1 change: 1 addition & 0 deletions crengine/include/lvdocviewprops.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@
// use 0 for old crengine behaviour (no support for absolute units and 1css px = 1 screen px)
#define PROP_RENDER_DPI "crengine.render.dpi"
#define PROP_RENDER_SCALE_FONT_WITH_DPI "crengine.render.scale.font.with.dpi"
#define PROP_RENDER_MIN_NORMAL_LINE_HEIGHT "crengine.render.min.normal.line.height"
#define PROP_RENDER_BLOCK_RENDERING_FLAGS "crengine.render.block.rendering.flags"
#define PROP_REQUESTED_DOM_VERSION "crengine.render.requested_dom_version"

Expand Down
2 changes: 2 additions & 0 deletions crengine/include/lvrend.h
Original file line number Diff line number Diff line change
Expand Up @@ -188,8 +188,10 @@ bool getInkOffsets( ldomNode * node, lvRect &inkOffsets, bool measure_hidden_con
#define BASE_CSS_DPI 96 // at 96 dpi, 1 css px = 1 screen px
#define DEF_RENDER_DPI 96
#define DEF_RENDER_SCALE_FONT_WITH_DPI 0
#define DEF_RENDER_MIN_NORMAL_LINE_HEIGHT 0
extern int gRenderDPI;
extern bool gRenderScaleFontWithDPI;
extern int gRenderMinNormalLineHeight;
extern int gRootFontSize;

#define INTERLINE_SCALE_FACTOR_NO_SCALE 1024
Expand Down
7 changes: 7 additions & 0 deletions crengine/src/lvdocview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6721,6 +6721,7 @@ void LVDocView::propsUpdateDefaults(CRPropRef props) {

props->setIntDef(PROP_RENDER_DPI, DEF_RENDER_DPI); // 96 dpi
props->setIntDef(PROP_RENDER_SCALE_FONT_WITH_DPI, DEF_RENDER_SCALE_FONT_WITH_DPI); // no scale
props->setIntDef(PROP_RENDER_MIN_NORMAL_LINE_HEIGHT, DEF_RENDER_MIN_NORMAL_LINE_HEIGHT); // no scale
props->setIntDef(PROP_RENDER_BLOCK_RENDERING_FLAGS, DEF_RENDER_BLOCK_RENDERING_FLAGS);

props->setIntDef(PROP_FILE_PROPS_FONT_SIZE, 22);
Expand Down Expand Up @@ -7080,6 +7081,12 @@ CRPropRef LVDocView::propsApply(CRPropRef props) {
gRenderScaleFontWithDPI = value;
REQUEST_RENDER("propsApply render scale font with dpi")
}
} else if (name == PROP_RENDER_MIN_NORMAL_LINE_HEIGHT) {
int value = props->getIntDef(PROP_RENDER_MIN_NORMAL_LINE_HEIGHT, DEF_RENDER_MIN_NORMAL_LINE_HEIGHT);
if ( gRenderMinNormalLineHeight != value ) {
gRenderMinNormalLineHeight = value;
REQUEST_RENDER("propsApply render min normal line height")
}
} else if (name == PROP_FORMAT_SPACE_WIDTH_SCALE_PERCENT) {
int value = props->getIntDef(PROP_FORMAT_SPACE_WIDTH_SCALE_PERCENT, DEF_SPACE_WIDTH_SCALE_PERCENT);
if (m_doc) // not when noDefaultDocument=true
Expand Down
26 changes: 23 additions & 3 deletions crengine/src/lvrend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@

int gRenderDPI = DEF_RENDER_DPI; // if 0: old crengine behaviour: 1px/pt=1px, 1in/cm/pc...=0px
bool gRenderScaleFontWithDPI = DEF_RENDER_SCALE_FONT_WITH_DPI;
int gRenderMinNormalLineHeight = DEF_RENDER_MIN_NORMAL_LINE_HEIGHT;

static const css_length_t relative_normal_line_height = css_length_t(css_val_em, 1*256 + (256 * 5 + 100 / 2) / 100); // 1.05em
static const css_length_t fallback_normal_line_height = css_length_t(css_val_unspecified, 1*256 + (256 * 2 + 10 / 2) / 10); // 1.2 unitless

int scaleForRenderDPI( int value ) {
// if gRenderDPI == 0 or 96, use value as is (1px = 1px)
Expand Down Expand Up @@ -3131,7 +3135,13 @@ 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 (gRenderMinNormalLineHeight > 0) {
line_h = gRenderMinNormalLineHeight + lengthToPx(enode, relative_normal_line_height, em, em, true);
}
else {
line_h = font->getHeight(); // line-height: normal
}
}
else {
int em = font->getSize();
Expand Down Expand Up @@ -3402,7 +3412,12 @@ 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 (gRenderMinNormalLineHeight > 0) {
line_h = gRenderMinNormalLineHeight + lengthToPx(enode, relative_normal_line_height, em, em, true);
}
else {
line_h = enode->getFont()->getHeight(); // line-height: normal
}
}
else {
// In all other cases (%, em, unitless/unspecified), we can just scale 'em',
Expand Down Expand Up @@ -7457,7 +7472,12 @@ 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 (gRenderMinNormalLineHeight > 0) {
line_h = gRenderMinNormalLineHeight + lengthToPx(enode, relative_normal_line_height, em, em, true);
}
else {
line_h = enode->getFont()->getHeight(); // line-height: normal
}
}
else {
// In all other cases (%, em, unitless/unspecified), we can just
Expand Down
1 change: 1 addition & 0 deletions crengine/src/lvtinydom.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,7 @@ lUInt32 calcGlobalSettingsHash(int documentId, bool already_rendered)
// hash = hash * 31 + (int)fontMan->GetHintingMode();
hash = hash * 31 + LVRendGetBaseFontWeight();
hash = hash * 31 + gRenderDPI;
hash = hash * 31 + gRenderMinNormalLineHeight;
// If not yet rendered (initial loading with XML parsing), we can
// ignore some global flags that have not yet produced any effect,
// so they can possibly be updated between loading and rendering
Expand Down

0 comments on commit ab4cf26

Please sign in to comment.