Incorrect character width calculation
See original GitHub issueHi,
In console_widget.py#L491 the character width is calculated using boundingRect
. In my system (Windows 10, python 3.8.5, pyqt5 5.15.0) the result of boundingRect
width()
is not consistent (even though the font is monospaced).
print(' ', font_metrics.boundingRect(' ').width(), font_metrics.horizontalAdvance(' '), file=f)
for c in range(ord('a'), ord('z') + 1):
print(chr(c), font_metrics.boundingRect(chr(c)).width(), font_metrics.horizontalAdvance(chr(c)), file=f)
1 6 <- ' ' character
a 5 6
b 6 6
c 5 6
d 5 6
e 5 6
f 5 6
g 5 6
h 5 6
i 5 6
j 4 6
k 6 6
l 5 6
m 6 6
n 5 6
o 6 6
p 6 6
q 5 6
r 5 6
s 5 6
t 5 6
u 5 6
v 6 6
w 6 6
x 5 6
y 6 6
z 5 6
This results in the wrong window width. What works is to use horizontalAdvance
which is the alternative to the deprecated width
method (https://doc.qt.io/qt-5/qfontmetricsf-obsolete.html#width-1).
Issue Analytics
- State:
- Created 3 years ago
- Comments:5 (2 by maintainers)
Top Results From Across the Web
character width calculation wrong with some font which can ...
You have to ensure the font is properly loaded on the canvas before creating a fabricObject that uses it, if not fabricjs creates...
Read more >STR #3364: Fl_Text_Display: wrong text width calculation
There is something wrong with text width calculation in Fl_Text_Display. ... look at the '/' characters at line ends => they miss a...
Read more >How to properly calculate text size in PIL images
size_width and size_height would return incorrect values. The solution. So, the solution is to use different calculations for the the text size.
Read more >How to Tune Typography Based on Characters Per Line
The width factor from the equation above tells you how many increments of width are needed to reach the desired CPL. Once you...
Read more >Incorrect Font Metrics | Qt Forum
boundingRect(text).size(). Problem When using the Freestyle Script font the bounding box calculation performed by QFontMetricsF is incorrect ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@yannick1974 Thanks, I have updated the pull request.
Hi,
I think it’s worth replacing the other
boundingRect
. On my system, for a reason I don’t understand,boundingRect(' ').width()
returns 0 (I tried with different fonts). This leads to aZeroDivisionError
atcompletion_html.py:313
that makes qtconsole crash when using pyqt5 (but not with pyside). Also, another effect is the qtconsole does not honourc.JupyterWidget.console_width
.I replaced all the
boundingRect(' ').width()
byhorizontalAdvance(' ')
and now everything works as expected.