Skip to content

Commit

Permalink
WIP: hanging punctuation rework
Browse files Browse the repository at this point in the history
  • Loading branch information
poire-z committed Jul 9, 2020
1 parent 5948344 commit ebbd803
Show file tree
Hide file tree
Showing 8 changed files with 432 additions and 183 deletions.
3 changes: 3 additions & 0 deletions crengine/include/lvrend.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
#define RENDER_RECT_FLAG_FINAL_FOOTPRINT_AS_SAVED_FLOAT_IDS 0x0040
#define RENDER_RECT_FLAG_FLOATBOX_IS_RIGHT 0x0080
#define RENDER_RECT_FLAG_NO_INTERLINE_SCALE_UP 0x0100 // for ruby elements to not scale up
#define RENDER_RECT_FLAG_IS_IN_MAIN_FLOW 0x0200 // for hanging punctuation
#define RENDER_RECT_FLAG_TEMP_USED_AS_CSS_CHECK_CACHE 0x8000 // has been cleared and is used as a CSS checks cache

#define RENDER_RECT_SET_FLAG(r, f) ( r.setFlags( r.getFlags() | RENDER_RECT_FLAG_##f ) )
Expand Down Expand Up @@ -173,6 +174,8 @@ extern int gRenderDPI;
extern bool gRenderScaleFontWithDPI;
extern int gRootFontSize;

extern bool gHangingPunctuationEnabled;

#define INTERLINE_SCALE_FACTOR_NO_SCALE 1024
#define INTERLINE_SCALE_FACTOR_SHIFT 10
extern int gInterlineScaleFactor;
Expand Down
6 changes: 1 addition & 5 deletions crengine/include/lvtextfm.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,6 @@
#include "lvbmpbuf.h"
#include "textlang.h"

// comment out following line to use old formatter
#define USE_NEW_FORMATTER 1

#ifdef __cplusplus
extern "C" {
#endif
Expand Down Expand Up @@ -432,6 +429,7 @@ class LFormattedText

lUInt32 Format(lUInt16 width, lUInt16 page_height,
int para_direction=0, // = REND_DIRECTION_UNSET in lvrend.h
bool hanging_punctuation=false,
BlockFloatFootprint * float_footprint = NULL );

int GetSrcCount()
Expand Down Expand Up @@ -481,6 +479,4 @@ class LFormattedText

#endif

extern bool gFlgFloatingPunctuationEnabled;

#endif
3 changes: 3 additions & 0 deletions crengine/include/textlang.h
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,9 @@ class TextLangCfg
lString16 & getOpeningQuote( bool update_level=true );
lString16 & getClosingQuote( bool update_level=true );

int getHyphenHangingPercent();
int getHangingPercent( bool right_hanging, bool & check_font, const lChar16 * text, int pos, int next_usable );

#if USE_HARFBUZZ==1
hb_language_t getHBLanguage() const { return _hb_language; }
#endif
Expand Down
5 changes: 3 additions & 2 deletions crengine/src/lvdocview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6463,8 +6463,9 @@ CRPropRef LVDocView::propsApply(CRPropRef props) {
REQUEST_RENDER("propsApply footnotes")
} else if (name == PROP_FLOATING_PUNCTUATION) {
bool value = props->getBoolDef(PROP_FLOATING_PUNCTUATION, true);
if ( gFlgFloatingPunctuationEnabled != value ) {
gFlgFloatingPunctuationEnabled = value;
if ( gHangingPunctuationEnabled != value ) {
gHangingPunctuationEnabled = value;
requestRender(); // does m_doc->clearRendBlockCache(), which is needed on hanging punctuation change
REQUEST_RENDER("propsApply floating punct")
}
} else if (name == PROP_RENDER_BLOCK_RENDERING_FLAGS) {
Expand Down
20 changes: 18 additions & 2 deletions crengine/src/lvrend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@
// crengine default used to be "width: 100%", but now that we
// can shrink to fit, it is "width: auto".

bool gHangingPunctuationEnabled = false;

int gInterlineScaleFactor = INTERLINE_SCALE_FACTOR_NO_SCALE;

int gRenderDPI = DEF_RENDER_DPI; // if 0: old crengine behaviour: 1px/pt=1px, 1in/cm/pc...=0px
Expand Down Expand Up @@ -4142,6 +4144,8 @@ int renderBlockElementLegacy( LVRendPageContext & context, ldomNode * enode, int
fmt.setX( fmt.getX() );
fmt.setY( fmt.getY() );
fmt.setLangNodeIndex( 0 ); // No support for lang in legacy rendering
// No support for floating punctuation in legacy mode (we don't know
// if this final node is in the main flow or not)
fmt.push();
//if ( CRLog::isTraceEnabled() )
// CRLog::trace("rendering final node: %s %d %s", LCSTR(enode->getNodeName()), enode->getDataIndex(), LCSTR(ldomXPointer(enode,0).toString()) );
Expand Down Expand Up @@ -7116,6 +7120,17 @@ void renderBlockElementEnhanced( FlowState * flow, ldomNode * enode, int x, int
fmt.setInnerY( padding_top );
fmt.setInnerWidth( inner_width );
RENDER_RECT_SET_FLAG(fmt, INNER_FIELDS_SET);
if ( flow->isMainFlow() ) {
RENDER_RECT_SET_FLAG(fmt, IS_IN_MAIN_FLOW);
// Hanging punctuation will only be ensured on final blocks
// in the main flow where we assume we can render glyphs
// in the margins - but not in floats, inline-boxes and
// table cells, which might have borders and surrounding
// content.
// (We could check for the available padding/margin until
// some border is met (but we would have to store the
// available width on each side in new fields...)
}
fmt.push();
// (These setInner* needs to be set before creating float_footprint if
// we want to debug/valide floatIds coordinates)
Expand Down Expand Up @@ -9226,6 +9241,8 @@ void getRenderedWidths(ldomNode * node, int &maxWidth, int &minWidth, int direct
// do, but only with widths and horizontal margin/border/padding and indent
// (with no width constraint, so no line splitting and hyphenation - and we
// don't care about vertical spacing and alignment).
// Note that, as this is called only on blocks that are not in the main flow,
// hanging punctuation, even if enabled, won't be ensured on them.
// Limitations: no support for css_d_run_in (hardly ever used, we don't care)
// todo : probably more tweaking to do when direction=RTL, and we should
// also handle direction change when walking inner elements... (For now,
Expand Down Expand Up @@ -9846,8 +9863,7 @@ void getRenderedWidths(ldomNode * node, int &maxWidth, int &minWidth, int direct
static lUInt8 flags[MAX_TEXT_CHUNK_SIZE+1];
// We adjust below each word width with calls to getLeftSideBearing()
// and getRightSideBearing(). These should be called with the exact same
// parameters as used in lvtextfm.cpp getAdditionalCharWidth() and
// getAdditionalCharWidthOnLeft().
// parameters as used in lvtextfm.cpp addLine().
// todo: use fribidi and split measurement at fribidi level change,
// and beware left/right side bearing adjustments...
#if (USE_LIBUNIBREAK==1)
Expand Down
Loading

0 comments on commit ebbd803

Please sign in to comment.