Skip to content

Commit e1756d7

Browse files
committed
Optimize font key caching for Qt 6 and disable fast path for Qt 5 to ensure consistent text rendering
1 parent 27a0e17 commit e1756d7

1 file changed

Lines changed: 17 additions & 1 deletion

File tree

qwt/text.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,12 +198,28 @@ def draw(self, painter, rect, flags, text):
198198
# The cache is bounded; once it grows past the limit it is rebuilt to keep
199199
# only the most recently inserted entry. Tick-rendering uses a tiny number
200200
# of fonts in practice so a small cap is sufficient.
201+
#
202+
# NOTE: the id-keyed fast path is intentionally disabled on Qt 5. On Qt 5
203+
# the order in which QFont instances trigger metrics initialisation has a
204+
# subtle (sub-perceptual) effect on antialiased text rendering, which made
205+
# a handful of test screenshots differ by a few pixels at edges. Skipping
206+
# the fast path on Qt 5 keeps the rendering bit-identical to upstream.
207+
# Qt 6 (PyQt6/PySide6) is unaffected and continues to benefit from the
208+
# fast path - which is also where the optimisation matters most.
209+
from qtpy import QT_VERSION as _QT_VERSION # noqa: E402
210+
201211
_FONT_KEY_CACHE: dict = {}
202212
_FONT_KEY_CACHE_LIMIT = 1024
213+
_USE_FONT_KEY_FAST_PATH = not str(_QT_VERSION).startswith("5.")
203214

204215

205216
def font_key_cached(font) -> str:
206-
"""Return ``font.key()`` using a process-wide id-keyed cache."""
217+
"""Return ``font.key()`` using a process-wide id-keyed cache.
218+
219+
On Qt 5 this is a thin wrapper around ``font.key()`` (see module note).
220+
"""
221+
if not _USE_FONT_KEY_FAST_PATH:
222+
return font.key()
207223
fid = id(font)
208224
entry = _FONT_KEY_CACHE.get(fid)
209225
if entry is not None:

0 commit comments

Comments
 (0)