[LyX/master] Improve alignment of math decorations

Enrico Forestieri forenr at lyx.org
Tue Feb 2 18:44:03 UTC 2021


commit d999d8e8d951642278602195ace199118c6292dd
Author: Enrico Forestieri <forenr at lyx.org>
Date:   Tue Feb 2 19:39:52 2021 +0100

    Improve alignment of math decorations
    
    This patch takes into account the skewness of a character for properly
    align a decoration. Unfortunately, the required amount of shift depends
    on single chars, so that the recently introduced FontMetrics::italicSlope()
    method cannot be used with all characters. A heuristics that produces
    satisfactory results is using the italic slope only with characters whose
    italic correction (kerning) is zero.
    
    Part of #11491
---
 src/mathed/InsetMathDecoration.cpp |   22 ++++++++++++++++------
 src/mathed/MathSupport.cpp         |   11 +++++++++++
 src/mathed/MathSupport.h           |    2 ++
 3 files changed, 29 insertions(+), 6 deletions(-)

diff --git a/src/mathed/InsetMathDecoration.cpp b/src/mathed/InsetMathDecoration.cpp
index adf5656..c82416d 100644
--- a/src/mathed/InsetMathDecoration.cpp
+++ b/src/mathed/InsetMathDecoration.cpp
@@ -13,13 +13,14 @@
 
 #include "InsetMathDecoration.h"
 
-#include "BufferView.h"
+#include "InsetMathChar.h"
 #include "MathData.h"
 #include "MathParser.h"
 #include "MathSupport.h"
 #include "MathStream.h"
 #include "MetricsInfo.h"
 
+#include "BufferView.h"
 #include "LaTeXFeatures.h"
 
 #include "support/debug.h"
@@ -143,11 +144,20 @@ void InsetMathDecoration::draw(PainterInfo & pi, int x, int y) const
 
 	cell(0).draw(pi, x, y);
 	Dimension const & dim0 = cell(0).dimension(*pi.base.bv);
-	if (wide())
-		mathed_draw_deco(pi, x + 1, y + dy_, dim0.wid, dh_, key_->name);
-	else
-		mathed_draw_deco(pi, x + 1 + (dim0.wid - dw_) / 2,
-			y + dy_, dw_, dh_, key_->name);
+	if (wide()) {
+		mathed_draw_deco(pi, x, y + dy_, dim0.wid, dh_, key_->name);
+		return;
+	}
+	// Lacking the necessary font parameters, in order to properly align
+	// the decoration we have to resort to heuristics for choosing a
+	// suitable value for shift
+	char_type c = (cell(0).empty() || !cell(0)[0]->asCharInset())
+		? 0 : cell(0)[0]->asCharInset()->getChar();
+	double slope = (c == 0) ? 0.0 : mathed_char_slope(pi.base, c);
+	int kerning = (c == 0) ? 0 : mathed_char_kerning(pi.base.font, c);
+	int shift = (kerning == 0) ? int(dim0.asc * slope) : kerning;
+	mathed_draw_deco(pi, x + (dim0.wid - dw_) / 2 + shift,
+	                 y + dy_, dw_, dh_, key_->name);
 }
 
 
diff --git a/src/mathed/MathSupport.cpp b/src/mathed/MathSupport.cpp
index cc8927f..36fc15c 100644
--- a/src/mathed/MathSupport.cpp
+++ b/src/mathed/MathSupport.cpp
@@ -20,6 +20,7 @@
 #include "MathParser.h"
 #include "MathStream.h"
 
+#include "Encoding.h"
 #include "LaTeXFeatures.h"
 #include "MetricsInfo.h"
 
@@ -32,6 +33,7 @@
 #include "support/docstream.h"
 #include "support/lassert.h"
 #include "support/Length.h"
+#include "support/textutils.h"
 
 #include <map>
 #include <algorithm>
@@ -572,6 +574,15 @@ int mathed_char_kerning(FontInfo const & font, char_type c)
 }
 
 
+double mathed_char_slope(MetricsBase const & mb, char_type c)
+{
+	bool slanted = isAlphaASCII(c) || Encodings::isMathAlpha(c);
+	if (slanted && mb.fontname == "mathnormal")
+		return theFontMetrics(mb.font).italicSlope();
+	return 0.0;
+}
+
+
 void mathed_string_dim(FontInfo const & font,
 		       docstring const & s,
 		       Dimension & dim)
diff --git a/src/mathed/MathSupport.h b/src/mathed/MathSupport.h
index 9fde420..1f83ffb 100644
--- a/src/mathed/MathSupport.h
+++ b/src/mathed/MathSupport.h
@@ -46,6 +46,8 @@ int mathed_char_width(FontInfo const &, char_type c);
 
 int mathed_char_kerning(FontInfo const &, char_type c);
 
+double mathed_char_slope(MetricsBase const & mb, char_type c);
+
 void mathed_draw_deco(PainterInfo & pi, int x, int y, int w, int h,
 	docstring const & name);
 


More information about the lyx-cvs mailing list