Skip to content

Commit

Permalink
fix OpenGL font issues on HiDPI displays
Browse files Browse the repository at this point in the history
  • Loading branch information
devernay committed Sep 10, 2021
1 parent 157cf66 commit a8b40b4
Show file tree
Hide file tree
Showing 11 changed files with 216 additions and 168 deletions.
30 changes: 19 additions & 11 deletions Gui/CurveWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -616,6 +616,16 @@ CurveWidget::paintGL()
if (_imp->zoomCtx.factor() <= 0) {
return;
}

{
double screenPixelRatio = getScreenPixelRatio();
if (screenPixelRatio != _imp->_screenPixelRatio) {
_imp->_screenPixelRatio = screenPixelRatio;
_imp->_textFont.reset(new QFont(appFont, appFontSize * screenPixelRatio));
}
}
assert(_imp->_textFont);

double zoomLeft, zoomRight, zoomBottom, zoomTop;
zoomLeft = _imp->zoomCtx.left();
zoomRight = _imp->zoomCtx.right();
Expand All @@ -625,8 +635,6 @@ CurveWidget::paintGL()
double bgR, bgG, bgB;
appPTR->getCurrentSettings()->getCurveEditorBGColor(&bgR, &bgG, &bgB);

double screenPixelRatio = getScreenPixelRatio();

if ( (zoomLeft == zoomRight) || (zoomTop == zoomBottom) ) {
glClearColor(bgR, bgG, bgB, 1.);
glClear(GL_COLOR_BUFFER_BIT);
Expand Down Expand Up @@ -664,22 +672,22 @@ CurveWidget::paintGL()
glCheckErrorIgnoreOSXBug();
}

_imp->drawScale(screenPixelRatio);
_imp->drawScale(_imp->_screenPixelRatio);



if (_imp->_timelineEnabled) {
_imp->drawTimelineMarkers(screenPixelRatio);
_imp->drawTimelineMarkers(_imp->_screenPixelRatio);
}

if (_imp->_drawSelectedKeyFramesBbox) {
_imp->drawSelectedKeyFramesBbox(screenPixelRatio);
_imp->drawSelectedKeyFramesBbox(_imp->_screenPixelRatio);
}

_imp->drawCurves(screenPixelRatio);
_imp->drawCurves(_imp->_screenPixelRatio);

if ( !_imp->_selectionRectangle.isNull() ) {
_imp->drawSelectionRectangle(screenPixelRatio);
_imp->drawSelectionRectangle(_imp->_screenPixelRatio);
}
} // GLProtectAttrib a(GL_TRANSFORM_BIT | GL_COLOR_BUFFER_BIT);
glCheckError();
Expand Down Expand Up @@ -727,8 +735,8 @@ CurveWidget::renderText(double x,
if ( (w <= 0) || (h <= 0) || (right <= left) || (top <= bottom) ) {
return;
}
double scalex = (right - left) / w;
double scaley = (top - bottom) / h;
double scalex = (right - left) / (w * _imp->_screenPixelRatio);
double scaley = (top - bottom) / (h * _imp->_screenPixelRatio);
_imp->textRenderer.renderText(x, y, scalex, scaley, text, color, font, flags);
glCheckError();
}
Expand Down Expand Up @@ -2006,7 +2014,7 @@ CurveWidget::getFont() const
// always running in the main thread
assert( qApp && qApp->thread() == QThread::currentThread() );

return *_imp->_font;
return *_imp->_textFont;
}

const SelectedKeys &
Expand All @@ -2024,7 +2032,7 @@ CurveWidget::getTextFont() const
// always running in the main thread
assert( qApp && qApp->thread() == QThread::currentThread() );

return *_imp->_font;
return *_imp->_textFont;
}

void
Expand Down
24 changes: 12 additions & 12 deletions Gui/CurveWidgetPrivate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,8 @@ CurveWidgetPrivate::CurveWidgetPrivate(Gui* gui,
, _selectedCurveColor(255, 255, 89, 255)
, _nextCurveAddedColor()
, textRenderer()
, _font( new QFont(appFont, appFontSize) )
, _screenPixelRatio(0.)
, _textFont()
, _curves()
, _selectedKeyFrames()
, _mustSetDragOrientation(false)
Expand Down Expand Up @@ -116,7 +117,6 @@ CurveWidgetPrivate::~CurveWidgetPrivate()
// always running in the main thread
assert( qApp && qApp->thread() == QThread::currentThread() );

delete _font;
_curves.clear();
}

Expand Down Expand Up @@ -454,7 +454,7 @@ CurveWidgetPrivate::drawScale(double screenPixelRatio)
return;
}

QFontMetrics fontM(*_font);
QFontMetrics fm(*_textFont);
const double smallestTickSizePixel = 10.; // tick size (in pixels) for alpha = 0.
const double largestTickSizePixel = 500.; // tick size (in pixels) for alpha = 1.
double gridR, gridG, gridB;
Expand Down Expand Up @@ -492,7 +492,7 @@ CurveWidgetPrivate::drawScale(double screenPixelRatio)
ticks_fill(half_tick, ticks_max, m1, m2, &ticks);
const double smallestTickSize = range * smallestTickSizePixel / rangePixel;
const double largestTickSize = range * largestTickSizePixel / rangePixel;
const double minTickSizeTextPixel = (axis == 0) ? fontM.width( QLatin1String("00") ) : fontM.height(); // AXIS-SPECIFIC
const double minTickSizeTextPixel = ( (axis == 0) ? fm.width( QLatin1String("00") ) : fm.height() ) / _screenPixelRatio; // AXIS-SPECIFIC
const double minTickSizeText = range * minTickSizeTextPixel / rangePixel;
for (int i = m1; i <= m2; ++i) {
double value = i * smallTickSize + offset;
Expand All @@ -517,22 +517,22 @@ CurveWidgetPrivate::drawScale(double screenPixelRatio)
if (tickSize > minTickSizeText) {
const int tickSizePixel = rangePixel * tickSize / range;
const QString s = QString::number(value);
const int sSizePixel = (axis == 0) ? fontM.width(s) : fontM.height(); // AXIS-SPECIFIC
const double sSizePixel = ( (axis == 0) ? fm.width(s) : fm.height() ) / _screenPixelRatio; // AXIS-SPECIFIC
if (tickSizePixel > sSizePixel) {
const int sSizeFullPixel = sSizePixel + minTickSizeTextPixel;
const double sSizeFullPixel = sSizePixel + minTickSizeTextPixel;
double alphaText = 1.0; //alpha;
if (tickSizePixel < sSizeFullPixel) {
// when the text size is between sSizePixel and sSizeFullPixel,
// draw it with a lower alpha
alphaText *= (tickSizePixel - sSizePixel) / (double)minTickSizeTextPixel;
}
alphaText = std::min(alphaText, alpha); // don't draw more opaque than tcks
//alphaText = std::min(alphaText, alpha); // don't draw more opaque than ticks
QColor c = scaleColor;
c.setAlpha(255 * alphaText);
if (axis == 0) {
_widget->renderText(value, btmLeft.y(), s, c, *_font, Qt::AlignHCenter); // AXIS-SPECIFIC
_widget->renderText(value, btmLeft.y(), s, c, *_textFont, Qt::AlignHCenter); // AXIS-SPECIFIC
} else {
_widget->renderText(btmLeft.x(), value, s, c, *_font, Qt::AlignVCenter); // AXIS-SPECIFIC
_widget->renderText(btmLeft.x(), value, s, c, *_textFont, Qt::AlignVCenter); // AXIS-SPECIFIC
}
}
}
Expand Down Expand Up @@ -771,7 +771,7 @@ CurveWidgetPrivate::isNearbyKeyFrameText(const QPoint& pt) const
topLeftWidget.ry() += yOffset;

QString coordStr = QString::fromUtf8("x: %1, y: %2").arg( (*it2)->key.getTime() ).arg( (*it2)->key.getValue() );
QPointF btmRightWidget( topLeftWidget.x() + fm.width(coordStr), topLeftWidget.y() + fm.height() );
QPointF btmRightWidget( topLeftWidget.x() + fm.width(coordStr) / _screenPixelRatio, topLeftWidget.y() + fm.height() / _screenPixelRatio );

if ( (pt.x() >= topLeftWidget.x() - CLICK_DISTANCE_FROM_CURVE_ACCEPTANCE) && (pt.x() <= btmRightWidget.x() + CLICK_DISTANCE_FROM_CURVE_ACCEPTANCE) &&
( pt.y() >= topLeftWidget.y() - CLICK_DISTANCE_FROM_CURVE_ACCEPTANCE) && ( pt.y() <= btmRightWidget.y() + CLICK_DISTANCE_FROM_CURVE_ACCEPTANCE) ) {
Expand Down Expand Up @@ -836,8 +836,8 @@ CurveWidgetPrivate::isNearbySelectedTangentText(const QPoint & pt) const

QString leftCoordStr = QString( tr("l: %1") ).arg(std::floor( ( (*it2)->key.getLeftDerivative() * rounding ) + 0.5 ) / rounding);
QString rightCoordStr = QString( tr("r: %1") ).arg(std::floor( ( (*it2)->key.getRightDerivative() * rounding ) + 0.5 ) / rounding);
QPointF btmRight_LeftTanWidget( topLeft_LeftTanWidget.x() + fm.width(leftCoordStr), topLeft_LeftTanWidget.y() + fm.height() );
QPointF btmRight_RightTanWidget( topLeft_RightTanWidget.x() + fm.width(rightCoordStr), topLeft_RightTanWidget.y() + fm.height() );
QPointF btmRight_LeftTanWidget( topLeft_LeftTanWidget.x() + fm.width(leftCoordStr) / _screenPixelRatio, topLeft_LeftTanWidget.y() + fm.height() / _screenPixelRatio );
QPointF btmRight_RightTanWidget( topLeft_RightTanWidget.x() + fm.width(rightCoordStr) / _screenPixelRatio, topLeft_RightTanWidget.y() + fm.height() / _screenPixelRatio );

if ( (pt.x() >= topLeft_LeftTanWidget.x() - CLICK_DISTANCE_FROM_CURVE_ACCEPTANCE) && (pt.x() <= btmRight_LeftTanWidget.x() + CLICK_DISTANCE_FROM_CURVE_ACCEPTANCE) &&
( pt.y() >= topLeft_LeftTanWidget.y() - CLICK_DISTANCE_FROM_CURVE_ACCEPTANCE) && ( pt.y() <= btmRight_LeftTanWidget.y() + CLICK_DISTANCE_FROM_CURVE_ACCEPTANCE) ) {
Expand Down
3 changes: 2 additions & 1 deletion Gui/CurveWidgetPrivate.h
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,8 @@ class CurveWidgetPrivate
QColor _selectedCurveColor;
QColor _nextCurveAddedColor;
TextRenderer textRenderer;
QFont* _font;
double _screenPixelRatio;
boost::scoped_ptr<QFont> _textFont;
Curves _curves;
SelectedKeys _selectedKeyFrames;
bool _mustSetDragOrientation;
Expand Down

0 comments on commit a8b40b4

Please sign in to comment.