Skip to content

Don't allow setting a 0 width font#2865

Merged
vadi2 merged 12 commits intoMudlet:developmentfrom
vadi2:fix-0-width-fonts
Aug 22, 2019
Merged

Don't allow setting a 0 width font#2865
vadi2 merged 12 commits intoMudlet:developmentfrom
vadi2:fix-0-width-fonts

Conversation

@vadi2
Copy link
Copy Markdown
Member

@vadi2 vadi2 commented Jul 26, 2019

Brief overview of PR changes/additions

Don't allow setting a 0 width font.

Motivation for adding to Mudlet

We need to divide by the font width in a few places and division by zero is a no-no. Also, such fonts don't seem to be practical fonts you could use in gaming!

Other info (issues closed, discussion etc)

image

I know we shouldn't be measuring W and instead using averageCharWidth() or similar function - but improvement that is outside of the scope of this fix.

Fix #2590, fix #2391.

@vadi2 vadi2 added this to the 4.0.0 milestone Jul 26, 2019
@vadi2 vadi2 requested a review from a team as a code owner July 26, 2019 03:20
@vadi2 vadi2 requested review from a team July 26, 2019 03:20
@add-deployment-links
Copy link
Copy Markdown

add-deployment-links bot commented Jul 26, 2019

Hey there! Thanks for helping Mudlet improve. 🌟

Test versions

You can directly test the changes here:

No need to install anything - just unzip and run.
Let us know if it works well, and if it doesn't, please give details.

@vadi2
Copy link
Copy Markdown
Member Author

vadi2 commented Jul 26, 2019

@Eraene @Xekon please test, thanks!

@SlySven
Copy link
Copy Markdown
Member

SlySven commented Jul 28, 2019

In revising a flaw in your #2869 I am also switching to use the QFontMetrics::averageCharWidth() - because the code concerns establishing the position of the mouse cursor in a line of TConsole text - which will interact with this PR...

@vadi2
Copy link
Copy Markdown
Member Author

vadi2 commented Jul 28, 2019

OK - but is this PR good to go? Would rather not have a merge conflict.

Copy link
Copy Markdown
Member

@keneanung keneanung left a comment

Choose a reason for hiding this comment

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

Looks good in general, but I'm not quite happy with a few details (which might result in greater work).

pHost->mDisplayFont = displayFont;
if (!pHost->setDisplayFont(font)) {
lua_pushnil(L);
lua_pushstring(L, "specified font is invalid (average character width is 0)");
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This works for now, but show knowledge about the internal workings of setDisplayFont(). Can we return an error string from the function instead as well?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

There is no error string from the function though?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

That's right. This is why I'd move this message into setDisplayFont instead.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I see what you mean.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Added, have a look - but I'm not sure I like it. The other choice is to just return an std::optional which we now have access to, but that also doesn't seem ideal when you actually try using it...

font.setPointSize(mFontSize);
if (pHost->mDisplayFont != font) {
pHost->mDisplayFont = font;
if (pHost->getDisplayFont() != font && pHost->setDisplayFont(font)) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

If the user selects such a 0 width font now, nothing happens when they click save. No message or error. They will wonder why.

@SlySven
Copy link
Copy Markdown
Member

SlySven commented Jul 29, 2019

Is it possible that (after my proposed revisions to) #2869 we will be using QFontMetrics::averageCharWidth() instead and we won't get the same crashes that we have been from selecting (as a known test case) the esint10 font?

@vadi2
Copy link
Copy Markdown
Member Author

vadi2 commented Jul 29, 2019

Not sure, that function isn't guaranteed not to return 0, is it...

@vadi2 vadi2 modified the milestones: 4.0.0, 4.1.0 Jul 30, 2019
@vadi2 vadi2 self-assigned this Aug 2, 2019
vadi2 added 3 commits August 6, 2019 08:14
…th-fonts

# Conflicts:
#	src/TLuaInterpreter.cpp
#	src/XMLimport.cpp
#	src/dlgProfilePreferences.cpp
QFont font = fontComboBox->currentFont();
font.setPointSize(mFontSize);
if (pHost->getDisplayFont() != font && pHost->setDisplayFont(font)) {
if (auto [setNewFont, errorMessage] = pHost->setDisplayFont(font); pHost->getDisplayFont() != font && setNewFont) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This makes the first condition obsolete because you set the font before you check if it is different. Also, why don't you use the error message here? My point from earlier still stands: Mudlet will silently do nothing.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Just haven't gotten around to showing the message yet, sorry!

const QFontMetrics metrics(font);
if (metrics.averageCharWidth() == 0) {
return false;
return std::make_pair(false, QStringLiteral("specified font is invalid (its letters have 0 width)"));
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Hm, this is unfortunate. On the one hand, we may want to translate this when showing on the GUI, but we don't want to translate this when using it in Lua code...

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Indeed.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

To be honest showing "the selected font has 0 width characters" is going to be nonsense to the majority of the people. We can just say "this isn't a valid font" in the UI and they'll ignore it or ask for help for it.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

You're probably right.

@vadi2 vadi2 assigned demonnic and SlySven and unassigned vadi2 Aug 22, 2019
Copy link
Copy Markdown
Member

@demonnic demonnic left a comment

Choose a reason for hiding this comment

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

lgtm

@vadi2 vadi2 dismissed keneanung’s stale review August 22, 2019 14:24

Requested changes addressed

@vadi2 vadi2 merged commit 66a4d34 into Mudlet:development Aug 22, 2019
@vadi2 vadi2 deleted the fix-0-width-fonts branch August 22, 2019 14:25
dicene pushed a commit to dicene/Mudlet that referenced this pull request Feb 19, 2020
* WIP

* Fix 0 width fonts crashing Mudlet

* Review feedback

* Return status + error msg

* Fix merge error

* Show error msg if invalid font is picked

* Reset tab index back to the first

* Strip PlaceholderText NoBrush - still doesn't work with Qt 5.11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

4 participants