[LyX/master] Add space before display math that starts a paragraph

Jean-Marc Lasgouttes lasgouttes at lyx.org
Tue Aug 25 10:54:35 UTC 2020


commit cc349fd03183d902bd334d499c3f3bd5f7e9c848
Author: Jean-Marc Lasgouttes <lasgouttes at lyx.org>
Date:   Tue Aug 25 13:05:29 2020 +0200

    Add space before display math that starts a paragraph
    
    This extra spacing was missing and is important for detecting extra
    par breaks before equations (which are most of the times not wanted).
    
    To do that, add a new member vmode to MetricsInfo which is equivalent
    to LaTeX's \ifvmode (start of paragraph).
    
    If in vmode, add the equivalent of an empty line before a display math inset.
    
    At the same time, tweak value of \(above|below)displayskip, which was
    12pt, whereas LaTeX uses 10pt in 10pt size (our reference).
    
    Fixes bug #11891.
---
 src/MetricsInfo.cpp          |    4 ++--
 src/MetricsInfo.h            |    4 +++-
 src/TextMetrics.cpp          |    2 +-
 src/mathed/InsetMathHull.cpp |   24 +++++++++++++++++-------
 4 files changed, 23 insertions(+), 11 deletions(-)

diff --git a/src/MetricsInfo.cpp b/src/MetricsInfo.cpp
index e7de5d0..c4caa75 100644
--- a/src/MetricsInfo.cpp
+++ b/src/MetricsInfo.cpp
@@ -119,8 +119,8 @@ int MetricsBase::inPixels(Length const & len) const
 /////////////////////////////////////////////////////////////////////////
 
 MetricsInfo::MetricsInfo(BufferView * bv, FontInfo font, int textwidth,
-                         MacroContext const & mc)
-	: base(bv, font, textwidth), macrocontext(mc)
+                         MacroContext const & mc, bool vm)
+	: base(bv, font, textwidth), macrocontext(mc), vmode(vm)
 {}
 
 
diff --git a/src/MetricsInfo.h b/src/MetricsInfo.h
index 14c3990..1508eb3 100644
--- a/src/MetricsInfo.h
+++ b/src/MetricsInfo.h
@@ -95,12 +95,14 @@ public:
 	MetricsInfo();
 	///
 	MetricsInfo(BufferView * bv, FontInfo font, int textwidth,
-	            MacroContext const & mc);
+	            MacroContext const & mc, bool vm);
 
 	///
 	MetricsBase base;
 	/// The context to resolve macros
 	MacroContext const & macrocontext;
+	/// Are we at the start of a paragraph (vertical mode)?
+	bool vmode;
 };
 
 
diff --git a/src/TextMetrics.cpp b/src/TextMetrics.cpp
index b393c1d..ea594e9 100644
--- a/src/TextMetrics.cpp
+++ b/src/TextMetrics.cpp
@@ -517,7 +517,7 @@ bool TextMetrics::redoParagraph(pit_type const pit, bool const align_rows)
 		Font const & font = e.inset->inheritFont() ?
 			displayFont(pit, e.pos) : bufferfont;
 		MacroContext mc(&buffer, parPos);
-		MetricsInfo mi(bv_, font.fontInfo(), w, mc);
+		MetricsInfo mi(bv_, font.fontInfo(), w, mc, e.pos == 0);
 		e.inset->metrics(mi, dim);
 		if (!insetCache.has(e.inset) || insetCache.dim(e.inset) != dim) {
 			insetCache.add(e.inset, dim);
diff --git a/src/mathed/InsetMathHull.cpp b/src/mathed/InsetMathHull.cpp
index 068854b..cb6cec5 100644
--- a/src/mathed/InsetMathHull.cpp
+++ b/src/mathed/InsetMathHull.cpp
@@ -56,6 +56,7 @@
 #include "graphics/PreviewLoader.h"
 
 #include "frontends/alert.h"
+#include "frontends/FontMetrics.h"
 #include "frontends/Painter.h"
 
 #include "support/convert.h"
@@ -520,9 +521,18 @@ bool previewTooSmall(Dimension const & dim)
 
 void InsetMathHull::metrics(MetricsInfo & mi, Dimension & dim) const
 {
-	// true value in LaTeX is 12pt plus 3pt minus 9pt
-	// FIXME: even better would be to handle the short skip case.
-	int const display_margin = display() ? mi.base.inPixels(Length(12, Length::PT)) : 0;
+	/* Compute \(above|below)displayskip
+	   true value in LaTeX is 10pt plus 2pt minus 5pt (in normal size at 10pt)
+	   FIXME: make this dependent of current size? (minor improvement)
+	   FIXME: if would be nice if this was not part of the inset, but
+	          just increased the row ascent/descent.
+	   FIXME: even better would be to handle the short skip case.
+	*/
+	int const bottom_display_margin = mi.base.inPixels(Length(10, Length::PT));
+	int top_display_margin = bottom_display_margin;
+	// at start of paragraph, add an empty line
+	if (mi.vmode)
+		top_display_margin += theFontMetrics(mi.base.font).maxHeight() + 2;
 
 	if (previewState(mi.base.bv)) {
 		preview_->metrics(mi, dim);
@@ -535,8 +545,8 @@ void InsetMathHull::metrics(MetricsInfo & mi, Dimension & dim) const
 			// value was hardcoded to 1 pixel
 			dim.wid += mi.base.bv->zoomedPixels(1) ;
 			if (display()) {
-				dim.asc += display_margin;
-				dim.des += display_margin;
+				dim.asc += top_display_margin;
+				dim.des += bottom_display_margin;
 			}
 		}
 		return;
@@ -577,8 +587,8 @@ void InsetMathHull::metrics(MetricsInfo & mi, Dimension & dim) const
 
 
 	if (display()) {
-		dim.asc += display_margin;
-		dim.des += display_margin;
+		dim.asc += top_display_margin;
+		dim.des += bottom_display_margin;
 	}
 
 	// reserve some space for marker.


More information about the lyx-cvs mailing list