Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

First attempt to correct kerning under Windows #3298

Merged
merged 1 commit into from
Dec 7, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions libmscore/mscore.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,11 @@ static constexpr qreal INCH = 25.4;
static constexpr qreal PPI = 72.0; // printer points per inch
static constexpr qreal SPATIUM20 = 5.0;
static constexpr qreal DPI = 72.0;
#ifdef Q_OS_WIN
static constexpr qreal DPIFACTOR = 100.0; // scaling factor to adjust kerning under Windows, see
#else
static constexpr qreal DPIFACTOR = 1.0;
#endif
static constexpr qreal DPMM = DPI / INCH;

static constexpr int MAX_STAVES = 4;
Expand Down
35 changes: 25 additions & 10 deletions libmscore/text.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -198,8 +198,14 @@ bool TextFragment::operator ==(const TextFragment& f) const

void TextFragment::draw(QPainter* p, const Text* t) const
{
p->setFont(font(t));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I will add a #ifdef Q_OS_WIN here. It seems a pity to do all the following if DPIFACTOR is 1.0. Otherwise, I don't see any artefact on MacOS. So I'll merge this so we can get more testers.

p->drawText(pos, text);
p->save();
QFont font2 = font(t);
font2.setPixelSize(font2.pixelSize() * DPIFACTOR);
p->setFont(font2);
qreal dpiMag = 1 / DPIFACTOR;
p->scale(dpiMag,dpiMag);
p->drawText(pos * DPIFACTOR, text);
p->restore();
}

//---------------------------------------------------------
Expand Down Expand Up @@ -297,9 +303,11 @@ void TextBlock::layout(Text* t)
else {
for (TextFragment& f : _text) {
f.pos.setX(x);
QFontMetricsF fm(f.font(t));
QFont font2 = f.font(t);
font2.setPixelSize(font2.pixelSize() * DPIFACTOR);
QFontMetricsF fm(font2);
if (f.format.valign() != VerticalAlignment::AlignNormal) {
qreal voffset = fm.xHeight() / subScriptSize; // use original height
qreal voffset = fm.xHeight() / subScriptSize / DPIFACTOR; // use original height
if (f.format.valign() == VerticalAlignment::AlignSubScript)
voffset *= subScriptOffset;
else
Expand All @@ -308,13 +316,16 @@ void TextBlock::layout(Text* t)
}
else
f.pos.setY(0.0);
qreal w = fm.width(f.text);
qreal w = fm.width(f.text) / DPIFACTOR;
QRectF r;
if (f.format.type() == CharFormatType::SYMBOL)
r = fm.tightBoundingRect(f.text);
else
r = fm.boundingRect(f.text);

QSizeF rectSize = r.size() / DPIFACTOR;
r.setTopLeft((r.topLeft() / DPIFACTOR).toPoint());
r.setSize(rectSize.toSize());
// for whatever reason the boundingRect() is different
// on second doLayout() (paint() ?)
r.setX(0); //HACK
Expand All @@ -323,7 +334,7 @@ void TextBlock::layout(Text* t)
_bbox |= r.translated(f.pos);
x += w;
// _lineSpacing = (_lineSpacing == 0 || fm.lineSpacing() == 0) ? qMax(_lineSpacing, fm.lineSpacing()) : qMin(_lineSpacing, fm.lineSpacing());
_lineSpacing = qMax(_lineSpacing, fm.lineSpacing());
_lineSpacing = qMax(_lineSpacing, rint(fm.lineSpacing() / DPIFACTOR));
}
}
qreal rx;
Expand All @@ -349,15 +360,17 @@ qreal TextBlock::xpos(int column, const Text* t) const
for (const TextFragment& f : _text) {
if (column == col)
return f.pos.x();
QFontMetricsF fm(f.font(t));
QFont font2 = f.font(t);
font2.setPixelSize(font2.pixelSize() * DPIFACTOR);
QFontMetricsF fm(font2);
int idx = 0;
for (const QChar& c : f.text) {
++idx;
if (c.isHighSurrogate())
continue;
++col;
if (column == col)
return f.pos.x() + fm.width(f.text.left(idx));
return f.pos.x() + (fm.width(f.text.left(idx)) / DPIFACTOR);
}
}
return _bbox.x();
Expand Down Expand Up @@ -444,8 +457,10 @@ int TextBlock::column(qreal x, Text* t) const
++idx;
if (c.isHighSurrogate())
continue;
QFontMetricsF fm(f.font(t));
qreal xo = fm.width(f.text.left(idx));
QFont font2 = f.font(t);
font2.setPixelSize(font2.pixelSize() * DPIFACTOR);
QFontMetricsF fm(font2);
qreal xo = fm.width(f.text.left(idx)) / DPIFACTOR;
if (x <= f.pos.x() + px + (xo-px)*.5)
return col;
++col;
Expand Down