From dbf644fb03a25f7370d1e893f38cb5dc15a53dde Mon Sep 17 00:00:00 2001 From: Jean-Marc Lasgouttes Date: Wed, 22 Sep 2021 15:09:26 +0200 Subject: [PATCH] Improve row flushing Add new row flags Flush and FlushBefore to let insets indicate whether they cause flushing of current row (eg. newline) or of previous row (e.g. display insets). --- src/RowFlags.h | 20 ++++++++++++-------- src/TextMetrics.cpp | 6 ++++-- src/insets/InsetNewline.cpp | 9 +++++++++ src/insets/InsetNewline.h | 2 +- src/insets/InsetSeparator.h | 2 +- 5 files changed, 27 insertions(+), 12 deletions(-) diff --git a/src/RowFlags.h b/src/RowFlags.h index f94f0c62d5..01c4dd24ed 100644 --- a/src/RowFlags.h +++ b/src/RowFlags.h @@ -32,22 +32,26 @@ enum RowFlags { BreakBefore = 1 << 0, // Avoid breaking row before this element NoBreakBefore = 1 << 1, + // flush the row before this element (useful with BreakBefore) + FlushBefore = 1 << 2, // force new (maybe empty) row after this element - AlwaysBreakAfter = 1 << 2, + AlwaysBreakAfter = 1 << 3, // break row after this element if there are more elements - BreakAfter = 1 << 3, + BreakAfter = 1 << 4, // break row whenever needed after this element - CanBreakAfter = 1 << 4, + CanBreakAfter = 1 << 5, // Avoid breaking row after this element - NoBreakAfter = 1 << 5, + NoBreakAfter = 1 << 6, // The contents of the row may be broken in two (e.g. string) - CanBreakInside = 1 << 6, + CanBreakInside = 1 << 7, + // Flush the row that ends with this element + Flush = 1 << 8, // specify an alignment (left, right) for a display element // (default is center) - AlignLeft = 1 << 7, - AlignRight = 1 << 8, + AlignLeft = 1 << 9, + AlignRight = 1 << 10, // A display element breaks row at both ends - Display = BreakBefore | BreakAfter, + Display = FlushBefore | BreakBefore | BreakAfter, // Flags that concern breaking after element AfterFlags = AlwaysBreakAfter | BreakAfter | CanBreakAfter | NoBreakAfter }; diff --git a/src/TextMetrics.cpp b/src/TextMetrics.cpp index 14dc7051ef..2b1ee5adbe 100644 --- a/src/TextMetrics.cpp +++ b/src/TextMetrics.cpp @@ -1068,7 +1068,6 @@ void cleanupRow(Row & row, bool at_end) } row.endpos(row.back().endpos); - row.flushed(at_end); // remove trailing spaces on row break if (!at_end) row.back().rtrim(); @@ -1118,8 +1117,11 @@ RowList TextMetrics::breakParagraph(Row const & bigrow) const int const f2 = (fcit == end) ? (end_label ? Inline : NoBreakBefore) : fcit->row_flags; if (rows.empty() || needsRowBreak(f1, f2)) { - if (!rows.empty()) + if (!rows.empty()) { cleanupRow(rows.back(), false); + // Flush row as requested by row flags + rows.back().flushed((f1 & Flush) || (f2 & FlushBefore)); + } pos_type pos = rows.empty() ? 0 : rows.back().endpos(); rows.push_back(newRow(*this, bigrow.pit(), pos, is_rtl)); // the width available for the row. diff --git a/src/insets/InsetNewline.cpp b/src/insets/InsetNewline.cpp index f5fcd42ac6..ecfacf47ce 100644 --- a/src/insets/InsetNewline.cpp +++ b/src/insets/InsetNewline.cpp @@ -39,6 +39,15 @@ InsetNewline::InsetNewline() : Inset(nullptr) {} +int InsetNewline::rowFlags() const +{ + if (params_.kind == InsetNewlineParams::LINEBREAK) + return AlwaysBreakAfter; + else + return AlwaysBreakAfter | Flush; +} + + void InsetNewlineParams::write(ostream & os) const { switch (kind) { diff --git a/src/insets/InsetNewline.h b/src/insets/InsetNewline.h index 1ef0ae52fb..c85a97df66 100644 --- a/src/insets/InsetNewline.h +++ b/src/insets/InsetNewline.h @@ -47,7 +47,7 @@ public: explicit InsetNewline(InsetNewlineParams par) : Inset(0) { params_.kind = par.kind; } /// - int rowFlags() const override { return AlwaysBreakAfter; } + int rowFlags() const override; /// static void string2params(std::string const &, InsetNewlineParams &); /// diff --git a/src/insets/InsetSeparator.h b/src/insets/InsetSeparator.h index 9352bdf0df..0c12d95686 100644 --- a/src/insets/InsetSeparator.h +++ b/src/insets/InsetSeparator.h @@ -65,7 +65,7 @@ public: return docstring(); } /// - int rowFlags() const override { return BreakAfter; } + int rowFlags() const override { return BreakAfter | Flush; } private: /// InsetCode lyxCode() const override { return SEPARATOR_CODE; } -- 2.39.5