From b28ec44476d3f2c5858d06596ed5bd975012ec33 Mon Sep 17 00:00:00 2001 From: Jean-Marc Lasgouttes Date: Tue, 22 Mar 2016 16:53:25 +0100 Subject: [PATCH] Use new display() values to remove some inset hardcoding. The enum is now made of flags that can be combined. This introduces several new values for Inset::DisplayType: BreakBefore, BreakAfter and Display=BreakBefore|BreakAfter. This last value replaces AlignCenter. Additionally the flags NoBoundary and CanBreakAfter are introduced for future use. Now a left aligned displayed inset will be defined as Display|LeftAlign. A newline inset is characterized as BreakAfter. This structure is used in breakRow to avoid explicit calls to isNewline() or isEnvSeparator(). More improvements will be built on top of this. Additionally several redundant display() methods (which returned Inline) have been removed. --- src/TextMetrics.cpp | 37 ++++++++++++++---------------------- src/insets/Inset.h | 35 ++++++++++++++++++++++++++++++---- src/insets/InsetBibtex.h | 2 +- src/insets/InsetBox.h | 2 -- src/insets/InsetCaption.h | 2 +- src/insets/InsetFloatList.h | 2 +- src/insets/InsetInclude.cpp | 2 +- src/insets/InsetIndex.h | 2 +- src/insets/InsetListings.cpp | 2 +- src/insets/InsetNewline.h | 2 ++ src/insets/InsetNewpage.h | 2 +- src/insets/InsetNomencl.h | 2 +- src/insets/InsetNote.cpp | 6 ------ src/insets/InsetNote.h | 2 -- src/insets/InsetRef.h | 4 +--- src/insets/InsetScript.cpp | 6 ------ src/insets/InsetScript.h | 2 -- src/insets/InsetSeparator.h | 2 ++ src/insets/InsetTOC.h | 2 +- src/insets/InsetTabular.cpp | 8 ++++---- src/insets/InsetVSpace.h | 2 +- src/mathed/InsetMathHull.cpp | 20 +++++++++---------- 22 files changed, 74 insertions(+), 72 deletions(-) diff --git a/src/TextMetrics.cpp b/src/TextMetrics.cpp index 2b0903c750..4661b76fdb 100644 --- a/src/TextMetrics.cpp +++ b/src/TextMetrics.cpp @@ -557,19 +557,13 @@ LyXAlignment TextMetrics::getAlign(Paragraph const & par, Row const & row) const // Display-style insets should always be on a centered row if (Inset const * inset = par.getInset(row.pos())) { - switch (inset->display()) { - case Inset::AlignLeft: - align = LYX_ALIGN_BLOCK; - break; - case Inset::AlignCenter: - align = LYX_ALIGN_CENTER; - break; - case Inset::Inline: - // unchanged (use align) - break; - case Inset::AlignRight: - align = LYX_ALIGN_RIGHT; - break; + if (inset->display() & Inset::Display) { + if (inset->display() & Inset::AlignLeft) + align = LYX_ALIGN_BLOCK; + else if (inset->display() & Inset::AlignRight) + align = LYX_ALIGN_RIGHT; + else + align = LYX_ALIGN_CENTER; } } @@ -920,15 +914,12 @@ bool TextMetrics::breakRow(Row & row, int const right_margin) const } // Handle some situations that abruptly terminate the row - // - A newline inset - // - Before a display inset - // - After a display inset - Inset const * inset = 0; - if (par.isNewline(i) || par.isEnvSeparator(i) - || (i + 1 < end && (inset = par.getInset(i + 1)) - && inset->display()) - || (!row.empty() && row.back().inset - && row.back().inset->display())) { + // - Before an inset with BreakBefore + // - After an inset with BreakAfter + Inset const * prevInset = !row.empty() ? row.back().inset : 0; + Inset const * nextInset = (i + 1 < end) ? par.getInset(i + 1) : 0; + if ((nextInset && nextInset->display() & Inset::BreakBefore) + || (prevInset && prevInset->display() & Inset::BreakAfter)) { row.flushed(true); need_new_row = par.isNewline(i); ++i; @@ -1753,7 +1744,7 @@ int TextMetrics::leftMargin(pit_type const pit, pos_type const pos) const // display style insets are always centered, omit indentation && !(!par.empty() && par.isInset(pos) - && par.getInset(pos)->display()) + && par.getInset(pos)->display() & Inset::Display) && (!(tclass.isDefaultLayout(par.layout()) || tclass.isPlainLayout(par.layout())) || buffer.params().paragraph_separation diff --git a/src/insets/Inset.h b/src/insets/Inset.h index 0f76671ea8..302c958a1e 100644 --- a/src/insets/Inset.h +++ b/src/insets/Inset.h @@ -467,14 +467,26 @@ public: /// does this inset try to use all available space (like \\hfill does)? virtual bool isHfill() const { return false; } + // Describe how the inset should be typeset enum DisplayType { Inline = 0, - AlignLeft, - AlignCenter, - AlignRight + // break row before this inset + BreakBefore = 1, + // break row after this inset + BreakAfter = 2, + // optionally break row after this inset (not used yet) + CanBreakAfter = 4, + // specify an alignment (left, right) for a display inset (default is center) + AlignLeft = 8, + AlignRight = 16, + // do not allow cursor to go at the end of the row before + // a display inset (not used yet) + NoBoundary = 32, + // A display inset breaks row at both ends + Display = BreakBefore | BreakAfter }; - /// should we have a non-filled line before this inset? + /// How should this inset be typeset? virtual DisplayType display() const { return Inline; } /// indentation before this inset (only needed for displayed hull insets with fleqn option) virtual int indent(BufferView const &) const { return 0; } @@ -634,6 +646,21 @@ protected: Buffer * buffer_; }; + +inline Inset::DisplayType operator|(Inset::DisplayType const d1, + Inset::DisplayType const d2) +{ + return static_cast(int(d1) | int(d2)); +} + + +inline Inset::DisplayType operator&(Inset::DisplayType const d1, + Inset::DisplayType const d2) +{ + return static_cast(int(d1) & int(d2)); +} + + } // namespace lyx #endif diff --git a/src/insets/InsetBibtex.h b/src/insets/InsetBibtex.h index f2b8dd1576..c618574a47 100644 --- a/src/insets/InsetBibtex.h +++ b/src/insets/InsetBibtex.h @@ -48,7 +48,7 @@ public: /// InsetCode lyxCode() const { return BIBTEX_CODE; } /// - DisplayType display() const { return AlignCenter; } + DisplayType display() const { return Display; } /// void latex(otexstream &, OutputParams const &) const; /// diff --git a/src/insets/InsetBox.h b/src/insets/InsetBox.h index e181bd738a..04c57707af 100644 --- a/src/insets/InsetBox.h +++ b/src/insets/InsetBox.h @@ -111,8 +111,6 @@ public: /// void metrics(MetricsInfo &, Dimension &) const; /// - DisplayType display() const { return Inline; } - /// ColorCode backgroundColor(PainterInfo const &) const; /// LyXAlignment contentAlignment() const; diff --git a/src/insets/InsetCaption.h b/src/insets/InsetCaption.h index 92dc9405c2..1d3cac1bb9 100644 --- a/src/insets/InsetCaption.h +++ b/src/insets/InsetCaption.h @@ -38,7 +38,7 @@ private: /// void write(std::ostream & os) const; /// - DisplayType display() const { return AlignCenter; } + DisplayType display() const { return Display; } /// bool neverIndent() const { return true; } /// diff --git a/src/insets/InsetFloatList.h b/src/insets/InsetFloatList.h index 3d6a7a3569..8c2a57128f 100644 --- a/src/insets/InsetFloatList.h +++ b/src/insets/InsetFloatList.h @@ -32,7 +32,7 @@ public: /// InsetCode lyxCode() const { return FLOAT_LIST_CODE; } /// - DisplayType display() const { return AlignCenter; } + DisplayType display() const { return Display; } /// void write(std::ostream &) const; /// diff --git a/src/insets/InsetInclude.cpp b/src/insets/InsetInclude.cpp index 2d91f62bd2..d6936c837e 100644 --- a/src/insets/InsetInclude.cpp +++ b/src/insets/InsetInclude.cpp @@ -1159,7 +1159,7 @@ string InsetInclude::contextMenuName() const Inset::DisplayType InsetInclude::display() const { - return type(params()) == INPUT ? Inline : AlignCenter; + return type(params()) == INPUT ? Inline : Display; } diff --git a/src/insets/InsetIndex.h b/src/insets/InsetIndex.h index 01bf7096b0..8dc14b967c 100644 --- a/src/insets/InsetIndex.h +++ b/src/insets/InsetIndex.h @@ -119,7 +119,7 @@ public: /// bool hasSettings() const; /// - DisplayType display() const { return AlignCenter; } + DisplayType display() const { return Display; } //@} /// \name Static public methods obligated for InsetCommand derived classes diff --git a/src/insets/InsetListings.cpp b/src/insets/InsetListings.cpp index ec9ad7225f..1b1debb18d 100644 --- a/src/insets/InsetListings.cpp +++ b/src/insets/InsetListings.cpp @@ -68,7 +68,7 @@ InsetListings::~InsetListings() Inset::DisplayType InsetListings::display() const { - return params().isInline() || params().isFloat() ? Inline : AlignLeft; + return params().isInline() || params().isFloat() ? Inline : Display | AlignLeft; } diff --git a/src/insets/InsetNewline.h b/src/insets/InsetNewline.h index 2dc9c553ed..a2848a1e31 100644 --- a/src/insets/InsetNewline.h +++ b/src/insets/InsetNewline.h @@ -47,6 +47,8 @@ public: InsetNewline(InsetNewlineParams par) : Inset(0) { params_.kind = par.kind; } /// + DisplayType display() const { return BreakAfter | NoBoundary; } + /// static void string2params(std::string const &, InsetNewlineParams &); /// static std::string params2string(InsetNewlineParams const &); diff --git a/src/insets/InsetNewpage.h b/src/insets/InsetNewpage.h index 51f640a097..51142d0e2d 100644 --- a/src/insets/InsetNewpage.h +++ b/src/insets/InsetNewpage.h @@ -76,7 +76,7 @@ private: /// void write(std::ostream & os) const; /// - DisplayType display() const { return AlignCenter; } + DisplayType display() const { return Display; } /// docstring insetLabel() const; /// diff --git a/src/insets/InsetNomencl.h b/src/insets/InsetNomencl.h index 4d11a47d79..b5a4e9315c 100644 --- a/src/insets/InsetNomencl.h +++ b/src/insets/InsetNomencl.h @@ -100,7 +100,7 @@ public: /// bool hasSettings() const { return true; } /// - DisplayType display() const { return AlignCenter; } + DisplayType display() const { return Display; } /// void latex(otexstream &, OutputParams const &) const; /// diff --git a/src/insets/InsetNote.cpp b/src/insets/InsetNote.cpp index 02f47ba30e..920ebd82b7 100644 --- a/src/insets/InsetNote.cpp +++ b/src/insets/InsetNote.cpp @@ -116,12 +116,6 @@ docstring InsetNote::layoutName() const } -Inset::DisplayType InsetNote::display() const -{ - return Inline; -} - - void InsetNote::write(ostream & os) const { params_.write(os); diff --git a/src/insets/InsetNote.h b/src/insets/InsetNote.h index 9df1a501a2..5c99df1909 100644 --- a/src/insets/InsetNote.h +++ b/src/insets/InsetNote.h @@ -61,8 +61,6 @@ private: InsetCode lyxCode() const { return NOTE_CODE; } /// docstring layoutName() const; - /// - DisplayType display() const; /** returns false if, when outputing LaTeX, font changes should be closed before generating this inset. This is needed for insets that may contain several paragraphs */ diff --git a/src/insets/InsetRef.h b/src/insets/InsetRef.h index 0f5a5b201d..c42aa1fa72 100644 --- a/src/insets/InsetRef.h +++ b/src/insets/InsetRef.h @@ -49,14 +49,12 @@ public: docstring toolTip(BufferView const &, int, int) const { return tooltip_; } /// - docstring getTOCString() const; + docstring getTOCString() const; /// bool hasSettings() const { return true; } /// InsetCode lyxCode() const { return REF_CODE; } /// - DisplayType display() const { return Inline; } - /// void latex(otexstream &, OutputParams const &) const; /// int plaintext(odocstringstream & ods, OutputParams const & op, diff --git a/src/insets/InsetScript.cpp b/src/insets/InsetScript.cpp index 918492fb55..8057962d9b 100644 --- a/src/insets/InsetScript.cpp +++ b/src/insets/InsetScript.cpp @@ -151,12 +151,6 @@ docstring InsetScript::layoutName() const } -Inset::DisplayType InsetScript::display() const -{ - return Inline; -} - - void InsetScript::metrics(MetricsInfo & mi, Dimension & dim) const { int const shift = params_.shift(mi.base.font); diff --git a/src/insets/InsetScript.h b/src/insets/InsetScript.h index 3107b6255d..0b755aa118 100644 --- a/src/insets/InsetScript.h +++ b/src/insets/InsetScript.h @@ -67,8 +67,6 @@ public: /// docstring layoutName() const; /// - DisplayType display() const; - /// void metrics(MetricsInfo &, Dimension &) const; /// void draw(PainterInfo & pi, int x, int y) const; diff --git a/src/insets/InsetSeparator.h b/src/insets/InsetSeparator.h index 5d080ccae6..4d53b956c2 100644 --- a/src/insets/InsetSeparator.h +++ b/src/insets/InsetSeparator.h @@ -64,6 +64,8 @@ public: // remove warning return docstring(); } + /// + DisplayType display() const { return BreakAfter | NoBoundary; } private: /// InsetSeparatorParams params() const { return params_; } diff --git a/src/insets/InsetTOC.h b/src/insets/InsetTOC.h index 4d48c3220f..29d5e91e76 100644 --- a/src/insets/InsetTOC.h +++ b/src/insets/InsetTOC.h @@ -37,7 +37,7 @@ public: /// docstring layoutName() const; /// - DisplayType display() const { return AlignCenter; } + DisplayType display() const { return Display; } /// virtual void validate(LaTeXFeatures &) const; /// diff --git a/src/insets/InsetTabular.cpp b/src/insets/InsetTabular.cpp index 324987c8a4..0680d93be8 100644 --- a/src/insets/InsetTabular.cpp +++ b/src/insets/InsetTabular.cpp @@ -5396,13 +5396,13 @@ Inset::DisplayType InsetTabular::display() const if (tabular.is_long_tabular) { switch (tabular.longtabular_alignment) { case Tabular::LYX_LONGTABULAR_ALIGN_LEFT: - return AlignLeft; + return Display | AlignLeft; case Tabular::LYX_LONGTABULAR_ALIGN_CENTER: - return AlignCenter; + return Display; case Tabular::LYX_LONGTABULAR_ALIGN_RIGHT: - return AlignRight; + return Display | AlignRight; default: - return AlignCenter; + return Display; } } else return Inline; diff --git a/src/insets/InsetVSpace.h b/src/insets/InsetVSpace.h index 21afbcd036..42d76bc997 100644 --- a/src/insets/InsetVSpace.h +++ b/src/insets/InsetVSpace.h @@ -62,7 +62,7 @@ private: /// void write(std::ostream & os) const; /// - DisplayType display() const { return AlignCenter; } + DisplayType display() const { return Display; } /// void doDispatch(Cursor & cur, FuncRequest & cmd); /// diff --git a/src/mathed/InsetMathHull.cpp b/src/mathed/InsetMathHull.cpp index dffef84731..1c40a72e0f 100644 --- a/src/mathed/InsetMathHull.cpp +++ b/src/mathed/InsetMathHull.cpp @@ -540,7 +540,7 @@ void InsetMathHull::metrics(MetricsInfo & mi, Dimension & dim) const // insert a gap in front of the formula // value was hardcoded to 1 pixel dim.wid += mi.base.bv->zoomedPixels(1) ; - if (display()) { + if (display() != Inline) { dim.asc += display_margin; dim.des += display_margin; } @@ -549,13 +549,13 @@ void InsetMathHull::metrics(MetricsInfo & mi, Dimension & dim) const } Changer dummy1 = mi.base.changeFontSet(standardFont()); - Changer dummy2 = mi.base.font.changeStyle(display() ? LM_ST_DISPLAY + Changer dummy2 = mi.base.font.changeStyle(display() != Inline ? LM_ST_DISPLAY : LM_ST_TEXT); // let the cells adjust themselves InsetMathGrid::metrics(mi, dim); - if (display()) { + if (display() != Inline) { dim.asc += display_margin; dim.des += display_margin; } @@ -653,7 +653,7 @@ void InsetMathHull::draw(PainterInfo & pi, int x, int y) const Changer dummy0 = really_change_color ? pi.base.font.changeColor(color) : Changer(); Changer dummy1 = pi.base.changeFontSet(standardFont()); - Changer dummy2 = pi.base.font.changeStyle(display() ? LM_ST_DISPLAY + Changer dummy2 = pi.base.font.changeStyle(display() != Inline ? LM_ST_DISPLAY : LM_ST_TEXT); int xmath = x; @@ -695,7 +695,7 @@ void InsetMathHull::draw(PainterInfo & pi, int x, int y) const void InsetMathHull::metricsT(TextMetricsInfo const & mi, Dimension & dim) const { - if (display()) { + if (display() != Inline) { InsetMathGrid::metricsT(mi, dim); } else { odocstringstream os; @@ -711,7 +711,7 @@ void InsetMathHull::metricsT(TextMetricsInfo const & mi, Dimension & dim) const void InsetMathHull::drawT(TextPainter & pain, int x, int y) const { - if (display()) { + if (display() != Inline) { InsetMathGrid::drawT(pain, x, y); } else { odocstringstream os; @@ -1022,12 +1022,12 @@ Inset::DisplayType InsetMathHull::display() const case hullMultline: case hullGather: if (buffer().params().is_math_indent) - return AlignLeft; + return Display | AlignLeft; else - return AlignCenter; + return Display; } // avoid warning - return AlignCenter; + return Display; } @@ -2324,7 +2324,7 @@ int InsetMathHull::plaintext(odocstringstream & os, OutputParams const & op, size_t max_length) const { // Try enabling this now that there is a flag as requested at #2275. - if (buffer().isExporting() && display()) { + if (buffer().isExporting() && display() != Inline) { Dimension dim; TextMetricsInfo mi; metricsT(mi, dim); -- 2.39.2