Skip to content

Commit

Permalink
Use #define for keyword color values
Browse files Browse the repository at this point in the history
Make the purpose of 0xFFFFFFFF clearer, and allow
using CSS color values like 0xFFFFFFFF.
  • Loading branch information
poire-z committed Apr 6, 2022
1 parent ec1b148 commit c7b6737
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 26 deletions.
9 changes: 9 additions & 0 deletions crengine/include/lvtextfm.h
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,15 @@ enum ltext_extra_t {
LTEXT_EXTRA_CSS_WORD_BREAK, // word-break: break-all or keep-all
};

// Text color reserved values
// Some color keywords can be provided when drawing (possibly by frontends).
// We will replace CSS computed values that collide with them, with 0xFFFFFFEF, which
// shouldn't have any impact as an alpha value of 0xFF means it won't be drawn.
#define LTEXT_COLOR_CURRENT 0xFFFFFFFF // keep/use current buffer color
#define LTEXT_COLOR_TRANSPARENT 0xFFFFFFFE // don't draw (CSS color:transparent)
#define LTEXT_COLOR_IS_RESERVED(c) ( (bool)( (c & 0xFFFFFFFE) == 0xFFFFFFFE ) )
#define LTEXT_COLOR_RESERVED_REPLACE 0xFFFFFFEF

/** \brief Source text line
*/
typedef struct
Expand Down
10 changes: 5 additions & 5 deletions crengine/src/lvdocview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1092,15 +1092,15 @@ void LVDocView::drawCoverTo(LVDrawBuf * drawBuf, lvRect & rc) {
title = "no title";
LFormattedText txform;
if (!authors.empty())
txform.AddSourceLine(authors.c_str(), authors.length(), 0xFFFFFFFF,
0xFFFFFFFF, author_fnt.get(), NULL, LTEXT_ALIGN_CENTER,
txform.AddSourceLine(authors.c_str(), authors.length(), LTEXT_COLOR_CURRENT,
LTEXT_COLOR_CURRENT, author_fnt.get(), NULL, LTEXT_ALIGN_CENTER,
author_fnt->getHeight() * 18 / 16);
txform.AddSourceLine(title.c_str(), title.length(), 0xFFFFFFFF, 0xFFFFFFFF,
txform.AddSourceLine(title.c_str(), title.length(), LTEXT_COLOR_CURRENT, LTEXT_COLOR_CURRENT,
title_fnt.get(), NULL, LTEXT_ALIGN_CENTER,
title_fnt->getHeight() * 18 / 16);
if (!series.empty())
txform.AddSourceLine(series.c_str(), series.length(), 0xFFFFFFFF,
0xFFFFFFFF, series_fnt.get(), NULL, LTEXT_ALIGN_CENTER,
txform.AddSourceLine(series.c_str(), series.length(), LTEXT_COLOR_CURRENT,
LTEXT_COLOR_CURRENT, series_fnt.get(), NULL, LTEXT_ALIGN_CENTER,
series_fnt->getHeight() * 18 / 16);
int title_w = rc.width() - rc.width() / 4;
int h = txform.Format((lUInt16)title_w, (lUInt16)rc.height());
Expand Down
25 changes: 14 additions & 11 deletions crengine/src/lvrend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2356,16 +2356,19 @@ LVFontRef getFont(ldomNode * node, css_style_rec_t * style, int documentId)

inline lUInt32 getBackgroundColor(const css_style_ref_t style)
{
return style->background_color.type == css_val_color ? style->background_color.value : 0xFFFFFFFF;
if ( style->background_color.type == css_val_color ) {
return LTEXT_COLOR_IS_RESERVED(style->background_color.value) ? LTEXT_COLOR_RESERVED_REPLACE : style->background_color.value;
}
return LTEXT_COLOR_CURRENT;
}

inline lUInt32 getForegroundColor(const css_style_ref_t style)
{
if ( style->color.type == css_val_color )
return style->color.value;
return LTEXT_COLOR_IS_RESERVED(style->color.value) ? LTEXT_COLOR_RESERVED_REPLACE : style->color.value;
if ( style->color.value == css_generic_transparent && style->color.type == css_val_unspecified )
return 0xDDFFFFFF; // Handled by LFormattedText::Draw(), which will skip drawing this fragment
return 0xFFFFFFFF;
return LTEXT_COLOR_TRANSPARENT; // Handled by LFormattedText::Draw(), which will skip drawing this fragment
return LTEXT_COLOR_CURRENT;
}

lUInt32 styleToTextFmtFlags( bool is_block, const css_style_ref_t & style, lUInt32 oldflags, int direction )
Expand Down Expand Up @@ -3670,7 +3673,7 @@ void renderFinalBlock( ldomNode * enode, LFormattedText * txform, RenderRectAcce
//txform->AddSourceLine(U"title", 5, 0x000000, 0xffffff, font, baseflags, interval, margin, NULL, 0, 0);
LVFontRef font = enode->getFont();
lUInt32 cl = getForegroundColor(style);
lUInt32 bgcl = 0xFFFFFFFF; // erm_final: any background will be drawn by DrawDocument
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
Expand Down Expand Up @@ -3792,7 +3795,7 @@ void renderFinalBlock( ldomNode * enode, LFormattedText * txform, RenderRectAcce
lUInt32 cl = getForegroundColor(style);
// If erm_final, the background will be drawn by DrawDocument, and should not
// be drawn by the LFormattedText txform
lUInt32 bgcl = rm == erm_final ? 0xFFFFFFFF : getBackgroundColor(style);
lUInt32 bgcl = rm == erm_final ? LTEXT_COLOR_CURRENT : getBackgroundColor(style);

// The following is needed for fribidi to do the right thing when the content creator
// has provided hints to explicite ambiguous cases.
Expand Down Expand Up @@ -3897,7 +3900,7 @@ void renderFinalBlock( ldomNode * enode, LFormattedText * txform, RenderRectAcce
if ( addGeneratedContent ) {
LVFontRef font = enode->getFont();
lUInt32 cl = getForegroundColor(style);
lUInt32 bgcl = rm == erm_final ? 0xFFFFFFFF : getBackgroundColor(style);
lUInt32 bgcl = rm == erm_final ? LTEXT_COLOR_CURRENT : getBackgroundColor(style);
// See comment above: these are the closing counterpart
if ( closeWithPDI ) {
txform->AddSourceLine( U"\x2069", 1, cl, bgcl, font.get(), lang_cfg, flags|LTEXT_FLAG_OWNTEXT, line_h, valign_dy, indent, enode);
Expand All @@ -3922,7 +3925,7 @@ void renderFinalBlock( ldomNode * enode, LFormattedText * txform, RenderRectAcce
LVFontRef font = enode->getFont();
css_style_ref_t style = enode->getStyle();
lUInt32 cl = getForegroundColor(style);
lUInt32 bgcl = rm == erm_final ? 0xFFFFFFFF : getBackgroundColor(style);
lUInt32 bgcl = rm == erm_final ? LTEXT_COLOR_CURRENT : getBackgroundColor(style);
txform->AddSourceLine( U" ", 1, cl, bgcl, font.get(), lang_cfg, LTEXT_LOCKED_SPACING|LTEXT_FLAG_OWNTEXT, line_h, valign_dy, 0, enode);
/*
// We used to specify two UNICODE_NO_BREAK_SPACE (that would not collapse)
Expand Down Expand Up @@ -3973,7 +3976,7 @@ void renderFinalBlock( ldomNode * enode, LFormattedText * txform, RenderRectAcce
// (This makes consecutive and stuck <br><br><br> work)
LVFontRef font = enode->getFont();
lUInt32 cl = getForegroundColor(style);
lUInt32 bgcl = rm == erm_final ? 0xFFFFFFFF : getBackgroundColor(style);
lUInt32 bgcl = rm == erm_final ? LTEXT_COLOR_CURRENT : getBackgroundColor(style);
txform->AddSourceLine( U" ", 1, cl, bgcl, font.get(), lang_cfg,
baseflags | LTEXT_FLAG_PREFORMATTED | LTEXT_FLAG_OWNTEXT,
line_h, valign_dy, 0, enode);
Expand Down Expand Up @@ -4034,7 +4037,7 @@ void renderFinalBlock( ldomNode * enode, LFormattedText * txform, RenderRectAcce
// string to text, and just call floatClearText().
LVFontRef font = enode->getFont();
lUInt32 cl = getForegroundColor(style);
lUInt32 bgcl = 0xFFFFFFFF; // erm_final: any background will be drawn by DrawDocument
lUInt32 bgcl = LTEXT_COLOR_CURRENT; // erm_final: any background will be drawn by DrawDocument
txform->AddSourceLine( U" ", 1, cl, bgcl, font.get(), lang_cfg,
baseflags | LTEXT_SRC_IS_CLEAR_LAST | LTEXT_FLAG_PREFORMATTED | LTEXT_FLAG_OWNTEXT,
line_h, valign_dy, 0, enode);
Expand Down Expand Up @@ -4068,7 +4071,7 @@ void renderFinalBlock( ldomNode * enode, LFormattedText * txform, RenderRectAcce
lUInt32 cl = getForegroundColor(style);
// If erm_final, the background will be drawn by DrawDocument, and should not
// be drawn over each word by the LFormattedText txform
lUInt32 bgcl = parent->getRendMethod() == erm_final ? 0xFFFFFFFF : getBackgroundColor(style);
lUInt32 bgcl = parent->getRendMethod() == erm_final ? LTEXT_COLOR_CURRENT : getBackgroundColor(style);

switch (style->text_transform) {
case css_tt_uppercase:
Expand Down
23 changes: 13 additions & 10 deletions crengine/src/lvtextfm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5039,7 +5039,7 @@ void LFormattedText::Draw( LVDrawBuf * buf, int x, int y, ldomMarkedRangeList *
// handled here (only looking at the style of the inline node
// that contains the word, and not at its other inline parents),
// some words may not get their proper bgcolor
lUInt32 lastWordColor = 0xFFFFFFFF;
lUInt32 lastWordColor = LTEXT_COLOR_CURRENT; // meaning unset, no bgcolor yet
int lastWordStart = -1;
int lastWordEnd = -1;
for (j=0; j<frmline->word_count; j++)
Expand All @@ -5061,7 +5061,7 @@ void LFormattedText::Draw( LVDrawBuf * buf, int x, int y, ldomMarkedRangeList *
lUInt32 bgcl = srcline->bgcolor;
if ( lastWordColor!=bgcl || lastWordStart==-1 ) {
if ( lastWordStart!=-1 )
if ( ((lastWordColor>>24) & 0xFF) < 128 )
if ( ((lastWordColor>>24) & 0xFF) != 0xFF ) // Not reserved, not alpha=100% (not transparent)
buf->FillRect( lastWordStart, y + frmline->y, lastWordEnd, y + frmline->y + frmline->height, lastWordColor );
lastWordColor=bgcl;
lastWordStart = x+frmline->x+word->x;
Expand All @@ -5070,7 +5070,7 @@ void LFormattedText::Draw( LVDrawBuf * buf, int x, int y, ldomMarkedRangeList *
}
}
if ( lastWordStart!=-1 )
if ( ((lastWordColor>>24) & 0xFF) < 128 )
if ( ((lastWordColor>>24) & 0xFF) != 0xFF )
buf->FillRect( lastWordStart, y + frmline->y, lastWordEnd, y + frmline->y + frmline->height, lastWordColor );

// process marks
Expand Down Expand Up @@ -5228,13 +5228,16 @@ void LFormattedText::Draw( LVDrawBuf * buf, int x, int y, ldomMarkedRangeList *
lUInt32 oldBgColor = buf->GetBackgroundColor();
lUInt32 cl = srcline->color;
lUInt32 bgcl = srcline->bgcolor;
if ( cl!=0xFFFFFFFF ) {
if ( cl==0xDDFFFFFF ) // color: transparent
if ( LTEXT_COLOR_IS_RESERVED(cl) ) {
if ( cl == LTEXT_COLOR_TRANSPARENT ) { // color: transparent
continue; // Don't draw this word
else
buf->SetTextColor( cl );
}
// Otherwise, LTEXT_COLOR_CURRENT: keep current buffer color
}
else {
buf->SetTextColor( cl );
}
if ( bgcl!=0xFFFFFFFF )
if ( !LTEXT_COLOR_IS_RESERVED(bgcl) )
buf->SetBackgroundColor( bgcl );
// Add drawing flags: text decoration (underline...)
lUInt32 drawFlags = srcline->flags & LTEXT_TD_MASK;
Expand Down Expand Up @@ -5284,9 +5287,9 @@ void LFormattedText::Draw( LVDrawBuf * buf, int x, int y, ldomMarkedRangeList *
val.c_str(), val.length(), '?', NULL, false);
}
*/
if ( cl!=0xFFFFFFFF )
if ( !LTEXT_COLOR_IS_RESERVED(cl) )
buf->SetTextColor( oldColor );
if ( bgcl!=0xFFFFFFFF )
if ( !LTEXT_COLOR_IS_RESERVED(bgcl) )
buf->SetBackgroundColor( oldBgColor );
}
lastWordSrcIndex = word->src_text_index;
Expand Down

0 comments on commit c7b6737

Please sign in to comment.