Don't allow setting a 0 width font#2865
Conversation
|
Hey there! Thanks for helping Mudlet improve. 🌟 Test versionsYou can directly test the changes here:
No need to install anything - just unzip and run. |
|
In revising a flaw in your #2869 I am also switching to use the |
|
OK - but is this PR good to go? Would rather not have a merge conflict. |
keneanung
left a comment
There was a problem hiding this comment.
Looks good in general, but I'm not quite happy with a few details (which might result in greater work).
src/TLuaInterpreter.cpp
Outdated
| pHost->mDisplayFont = displayFont; | ||
| if (!pHost->setDisplayFont(font)) { | ||
| lua_pushnil(L); | ||
| lua_pushstring(L, "specified font is invalid (average character width is 0)"); |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
There is no error string from the function though?
There was a problem hiding this comment.
That's right. This is why I'd move this message into setDisplayFont instead.
There was a problem hiding this comment.
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...
src/dlgProfilePreferences.cpp
Outdated
| font.setPointSize(mFontSize); | ||
| if (pHost->mDisplayFont != font) { | ||
| pHost->mDisplayFont = font; | ||
| if (pHost->getDisplayFont() != font && pHost->setDisplayFont(font)) { |
There was a problem hiding this comment.
If the user selects such a 0 width font now, nothing happens when they click save. No message or error. They will wonder why.
|
Is it possible that (after my proposed revisions to) #2869 we will be using |
|
Not sure, that function isn't guaranteed not to return 0, is it... |
…th-fonts # Conflicts: # src/TLuaInterpreter.cpp # src/XMLimport.cpp # src/dlgProfilePreferences.cpp
src/dlgProfilePreferences.cpp
Outdated
| 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) { |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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)")); |
There was a problem hiding this comment.
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...
There was a problem hiding this comment.
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.
# Conflicts: # src/dlgMapper.cpp # src/dlgProfilePreferences.cpp
# Conflicts: # src/TConsole.cpp # src/TLuaInterpreter.cpp # src/TTextEdit.cpp
* 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
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)
I know we shouldn't be measuring
Wand instead usingaverageCharWidth()or similar function - but improvement that is outside of the scope of this fix.Fix #2590, fix #2391.