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 "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,10 +144,19 @@ 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,
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);
}

View File

@ -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)

View File

@ -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);