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
This commit is contained in:
Enrico Forestieri 2021-02-02 19:39:52 +01:00
parent bc806084cd
commit d999d8e8d9
3 changed files with 29 additions and 6 deletions

View File

@ -13,13 +13,14 @@
#include "InsetMathDecoration.h" #include "InsetMathDecoration.h"
#include "BufferView.h" #include "InsetMathChar.h"
#include "MathData.h" #include "MathData.h"
#include "MathParser.h" #include "MathParser.h"
#include "MathSupport.h" #include "MathSupport.h"
#include "MathStream.h" #include "MathStream.h"
#include "MetricsInfo.h" #include "MetricsInfo.h"
#include "BufferView.h"
#include "LaTeXFeatures.h" #include "LaTeXFeatures.h"
#include "support/debug.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); cell(0).draw(pi, x, y);
Dimension const & dim0 = cell(0).dimension(*pi.base.bv); Dimension const & dim0 = cell(0).dimension(*pi.base.bv);
if (wide()) if (wide()) {
mathed_draw_deco(pi, x + 1, y + dy_, dim0.wid, dh_, key_->name); mathed_draw_deco(pi, x, y + dy_, dim0.wid, dh_, key_->name);
else return;
mathed_draw_deco(pi, x + 1 + (dim0.wid - dw_) / 2, }
y + dy_, dw_, dh_, key_->name); // 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);
} }

View File

@ -20,6 +20,7 @@
#include "MathParser.h" #include "MathParser.h"
#include "MathStream.h" #include "MathStream.h"
#include "Encoding.h"
#include "LaTeXFeatures.h" #include "LaTeXFeatures.h"
#include "MetricsInfo.h" #include "MetricsInfo.h"
@ -32,6 +33,7 @@
#include "support/docstream.h" #include "support/docstream.h"
#include "support/lassert.h" #include "support/lassert.h"
#include "support/Length.h" #include "support/Length.h"
#include "support/textutils.h"
#include <map> #include <map>
#include <algorithm> #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, void mathed_string_dim(FontInfo const & font,
docstring const & s, docstring const & s,
Dimension & dim) Dimension & dim)

View File

@ -46,6 +46,8 @@ int mathed_char_width(FontInfo const &, char_type c);
int mathed_char_kerning(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, void mathed_draw_deco(PainterInfo & pi, int x, int y, int w, int h,
docstring const & name); docstring const & name);