Skip to content

Font queries ignore Font.weight #905

@corranwebster

Description

@corranwebster

The FontManager and FontQuery system ignore the Font.weight attribute and look for a style which holds flags for BOLD, ITALIC, BOLD_ITALIC, etc. See:

def _make_font_query(self):
""" Returns a FontQuery object that encapsulates our font properties.
"""
# XXX: change the weight to a numerical value
if self.style == BOLD or self.style == BOLD_ITALIC:
weight = "bold"
else:
weight = "normal"
if self.style == ITALIC or self.style == BOLD_ITALIC:
style = "italic"
else:
style = "normal"
query = FontQuery(
family=self.familymap[self.family],
style=style,
weight=weight,
size=self.size,
)
if self.face_name != "":
query.set_name(self.face_name)
return query

But the str_to_font function populates weight. See:

elif lword in font_weights:
weight = font_weights[lword]

As does the trait validator:

elif lword in font_weights:
weight = font_weights[lword]

As does the Wx editor:

weight=(
kc.NORMAL, kc.BOLD
)[font.GetWeight() == wx.FONTWEIGHT_BOLD],

To reproduce, you can use kvia_explorer.py with this script and the agg backend:

# The graphics context is available as gc.

from kiva.api import (
    Font, BOLD_ITALIC, BOLD, ITALIC
)
from kiva.fonttools import str_to_font 

font_1 = Font(
    'Courier New',
    style=BOLD_ITALIC,
    size=24,
)
font_2 = str_to_font('24 pt italic bold Courier New')


with gc:
    gc.set_font(font_1)
    gc.show_text_at_point(f"font_1: weight={font_1.weight} style={font_1.style}", 20, 60)
    gc.set_font(font_2)
    gc.show_text_at_point(f"font_2: weight={font_2.weight} style={font_2.style}", 20, 20)

Which produces:
image

We would expect the second line to be bold.

Most backends suffer from this: Cairo does not, and a number of backends ignore bold/italic

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions