GeezerVison is showing github.com — [Bug]: Cairo backends don't render italic text · Issue #28452 · matplotlib/matplotlib
[Bug]: Cairo backends don't render italic text #28452
Description
Bug summary
The anatomy example uses italic text for its annotations. When running with TkCairo (or any other Cairo-based backend), this text is just upright.
Code for reproduction
MPLBACKEND=TkCairo python galleries/examples/showcase/anatomy.py
Actual outcome

Expected outcome

Additional information
No response
Operating system
Fedora 40
Matplotlib Version
d7d1bba
Matplotlib Backend
TkAgg/TkCairo
Python version
3.12.3
Jupyter version
n/a
Installation
git checkout
Activity
anntzer commented on Jun 25, 2024
From a quick look, the relevant functions for cairo's "toy" font APIs are cairo{ft,quartz,win32}_font_face_create_for_toy, which do appear to have some (extremely limited) support for style/weight. Now we just need to figure out the right arguments to pass to these (if any...).
QuLogic commented on Jun 25, 2024
Well, we do attempt to set the slant and the weight, but I guess that is not sufficient? Or maybe the family name is too specific?
|
def _cairo_font_args_from_font_prop(prop): |
|
""" |
|
Convert a `.FontProperties` or a `.FontEntry` to arguments that can be |
|
passed to `.Context.select_font_face`. |
|
""" |
|
def attr(field): |
|
try: |
|
return getattr(prop, f"get_{field}")() |
|
except AttributeError: |
|
return getattr(prop, field) |
|
|
|
name = attr("name") |
|
slant = getattr(cairo, f"FONT_SLANT_{attr('style').upper()}") |
|
weight = attr("weight") |
|
weight = (cairo.FONT_WEIGHT_NORMAL |
|
if font_manager.weight_dict.get(weight, weight) < 550 |
|
else cairo.FONT_WEIGHT_BOLD) |
|
return name, slant, weight |
anntzer commented on Jun 25, 2024
I think the family name is too specific (from what I remember looking at the cairo code; haven't re-checked).
OTOH as the cairo docs say themselves, trying to render text without actually going through freetype (or win32 or quartz) is only implemented as a "toy" in cairo.
Although pycairo says it can be integrated with freetype-py (https://pycairo.readthedocs.io/en/latest/tutorial/other.html#freetype-py-cairo), from a quick skim at the examples they seem to just extract the bitmap using freetype and blit it (essentially like we do in FT2Font and FT2Image) rather than rendering as glyphs (text on vector output), so that doesn't really help.
Also cairo's actual freetype integration seems not implented in pycairo (https://pycairo.readthedocs.io/en/latest/reference/text.html#class-freetypefontface-fontface).
I suspect (though I would be happy to be proven wrong) that fixing this is quickly going to reach mplcairo territory ;-)
Metadata
Metadata
Development
No branches or pull requestsIssue actions