Skip to content

Commit

Permalink
Map MODERN to "monospace" and TELETYPE to "typewriter" (#977)
Browse files Browse the repository at this point in the history
This changes font handling to:
- map "modern" font family to "monospace" rather than "sans-serif" like other backends
- distinguish between "monospace" (sans-serif fixed pitch fonts) and "typewriter" (serif fixed pitch fonts).

Replaces uses of "modern" with "sans-serif" where appropriate (particularly in defaults) to keep appearances the same.

This is not backwards compatible: users who use "modern" font family will get different fonts, but it is easily fixed by changing the font family.  So this should probably be part of a major release.

Fixes #974.
  • Loading branch information
corranwebster authored May 15, 2023
1 parent f623084 commit f65de7f
Show file tree
Hide file tree
Showing 20 changed files with 41 additions and 31 deletions.
2 changes: 1 addition & 1 deletion enable/drawing/drawing_canvas.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class Button(Component):

label = Str

label_font = KivaFont("modern 12")
label_font = KivaFont("sans-serif 12")

label_color = ColorTrait("white")

Expand Down
2 changes: 1 addition & 1 deletion enable/examples/demo/enable/filled_container_demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class MyFilledContainer(Container):
def _draw_container_mainlayer(self, gc, view_bounds, mode="default"):
'Draws a filled container with the word "Container" in the center'
if not self._font:
self._font = str_to_font("modern 10")
self._font = str_to_font("sans-serif 10")

with gc:
gc.set_fill_color(self.bgcolor_)
Expand Down
2 changes: 1 addition & 1 deletion enable/examples/demo/enable/hidpi_component.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class MyComponent(Component):

def draw(self, gc, **kwargs):
if not self._font:
self._font = str_to_font("modern 48")
self._font = str_to_font("sans-serif 48")

gc.clear((0.5, 0.5, 0.5))
mx = self.x + self.width / 2.0
Expand Down
2 changes: 1 addition & 1 deletion enable/examples/demo/enable/scrollbar_demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class Demo(DemoFrame):
def _create_component(self):
label = Label(
text="h:\nv:",
font="modern 16",
font="sans-serif 16",
position=[20, 50],
bounds=[100, 100],
bgcolor="red",
Expand Down
4 changes: 2 additions & 2 deletions enable/examples/demo/savage/buttons_on_canvas.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
from enable.api import BaseTool, Component, ComponentEditor, Container
from enable.savage.svg.backends.kiva.renderer import Renderer as KivaRenderer
from enable.savage.svg.document import SVGDocument
from kiva.api import MODERN, Font
from kiva.api import SWISS, Font
from traits.api import Callable, Enum, HasTraits, Instance, List, Str
from traitsui.api import Item, View

Expand Down Expand Up @@ -80,7 +80,7 @@ def _draw_svg_document(self, gc, document):

def _draw_label(self, gc):
with gc:
font = Font(family=MODERN)
font = Font(family=SWISS)
gc.set_font(font)

_x, _y, width, height = gc.get_text_extent(self.label)
Expand Down
6 changes: 3 additions & 3 deletions enable/gadgets/vu_meter.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,13 @@ class VUMeter(Component):
text = Str("VU")

# Font used to draw `text`.
text_font = KivaFont("modern 48")
text_font = KivaFont("sans-serif 48")

# Font for the db tick labels.
db_tick_font = KivaFont("modern 16")
db_tick_font = KivaFont("sans-serif 16")

# Font for the percent tick labels.
percent_tick_font = KivaFont("modern 12")
percent_tick_font = KivaFont("sans-serif 12")

# beta is the fraction of the of needle that is "hidden".
# beta == 0 puts the hinge point of the needle on the bottom
Expand Down
2 changes: 1 addition & 1 deletion enable/label.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class Label(Component):
border_color = black_color_trait

# The font of the label text.
font = KivaFont("modern 10")
font = KivaFont("sans-serif 10")

# Number of pixels of margin around the label, for both X and Y dimensions.
margin = Int(2)
Expand Down
4 changes: 2 additions & 2 deletions enable/primitives/shape.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from enable.colors import ColorTrait
from enable.component import Component
from enable.enable_traits import Pointer
from kiva.api import MODERN, Font
from kiva.api import SWISS, Font
from traits.api import Float, Property, Str, Tuple


Expand Down Expand Up @@ -157,7 +157,7 @@ def _draw_text(self, gc):
if len(self.text) > 0:
gc.set_fill_color(self._get_text_color(self.event_state))

gc.set_font(Font(family=MODERN, size=16))
gc.set_font(Font(family=SWISS, size=16))
tx, ty, tw, th = gc.get_text_extent(self.text)

dx, dy = self.bounds
Expand Down
2 changes: 1 addition & 1 deletion enable/text_grid.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class TextGrid(Component):
# ------------------------------------------------------------------------

# The font to use for the text of the grid
font = KivaFont("modern 14")
font = KivaFont("sans-serif 14")

# The color of the text
text_color = black_color_trait
Expand Down
2 changes: 1 addition & 1 deletion enable/tools/toolbars/toolbar_buttons.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class Button(Component):

label = Str

label_font = KivaFont("modern 11 bold")
label_font = KivaFont("sans-serif 11 bold")

label_color = ColorTrait("white")

Expand Down
4 changes: 2 additions & 2 deletions enable/trait_defs/ui/wx/rgba_color_editor.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ def init(self, parent):
from enable.window import Window

window = Window(
parent, component=Label("", border_size=1, font="modern 9")
parent, component=Label("", border_size=1, font="sans-serif 9")
)
self._swatch = window.component
self.control = window.control
Expand Down Expand Up @@ -342,7 +342,7 @@ def init(self, parent):
from enable.window import Window

window = Window(
parent, component=Label("", border_size=1, font="modern 9")
parent, component=Label("", border_size=1, font="sans-serif 9")
)
self._swatch = window.component
self.control = window.control
Expand Down
2 changes: 1 addition & 1 deletion kiva/agg/tests/test_graphics_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@ def test_set_get_text_position(self):

def test_get_set_font(self):
gc = agg.GraphicsContextArray((5, 5))
font1 = Font("modern")
font1 = Font("sans-serif")
gc.set_font(font1)
font3 = gc.get_font()
self.assertEqual(font1.face_name, font3.name)
Expand Down
16 changes: 13 additions & 3 deletions kiva/fonttools/_constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"fantasy",
"monospace",
"sans",
"modern",
"typewriter",
}

preferred_fonts = {
Expand All @@ -35,15 +35,25 @@
"cursive",
],
"monospace": [
"Consolas",
"Menlo",
"Monaco",
"Bitstream Vera Sans Mono",
"DejaVu Sans Mono",
"Andale Mono",
"Inconsolata",
"Fixed",
"Mono",
],
"typewriter": [
"Nimbus Mono L",
"FreeMono",
"Liberation Mono",
"Monospace", # ironically an OSS serif font: https://en.wikipedia.org/wiki/Monospace_(typeface)
"Courier New",
"Courier",
"Fixed",
"Terminal",
"monospace",
"typewriter",
],
"serif": [
"Bitstream Vera Serif",
Expand Down
2 changes: 1 addition & 1 deletion kiva/fonttools/_score.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def score_family(families, family2):
for i, family1 in enumerate(families):
family1 = family1.lower()
if family1 in font_family_aliases:
if family1 in {"sans", "sans serif", "modern"}:
if family1 in {"sans", "sans serif"}:
family1 = "sans-serif"
options = preferred_fonts[family1]
options = [x.lower() for x in options]
Expand Down
4 changes: 2 additions & 2 deletions kiva/fonttools/font.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,10 +184,10 @@ class Font(object):
DEFAULT: "serif",
SWISS: "sans-serif",
ROMAN: "serif",
MODERN: "sans-serif",
MODERN: "monospace",
DECORATIVE: "fantasy",
SCRIPT: "cursive",
TELETYPE: "monospace",
TELETYPE: "typewriter",
}

def __init__(self, face_name="", size=12, family=SWISS,
Expand Down
1 change: 0 additions & 1 deletion kiva/fonttools/tests/test_score.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ def test_score_family(self):
closest = preferred_fonts["sans-serif"][0]
self.assertEqual(score_family(["sans-serif"], closest), 0.0)
self.assertEqual(score_family(["sans"], closest), 0.0)
self.assertEqual(score_family(["unknown", "modern"], closest), 0.0)

# fuzzy matches
sans_count = len(preferred_fonts["sans-serif"])
Expand Down
6 changes: 3 additions & 3 deletions kiva/tests/agg/test_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,14 +120,14 @@ class test_text_image(unittest.TestCase):
def test_antialias(self):
gc = agg.GraphicsContextArray((200, 50), pix_format="bgra32")
gc.set_antialias(1)
f = Font("modern")
f = Font("sans-serif")
gc.set_font(f)
gc.show_text("hello")
save(gc)

def test_no_antialias(self):
gc = agg.GraphicsContextArray((200, 50), pix_format="bgra32")
f = Font("modern")
f = Font("sans-serif")
gc.set_font(f)
gc.set_antialias(0)
gc.show_text("hello")
Expand All @@ -136,7 +136,7 @@ def test_no_antialias(self):
def test_rotate(self):
text = "hello"
gc = agg.GraphicsContextArray((150, 150), pix_format="bgra32")
f = Font("modern")
f = Font("sans-serif")
gc.set_font(f)
tx, ty, sx, sy = gc.get_text_extent(text)
gc.translate_ctm(25, 25)
Expand Down
4 changes: 2 additions & 2 deletions kiva/tests/agg/test_text.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def test_locale_independence(self):

with locale_context(locale.LC_CTYPE, ("en", "utf8")):
gc = agg.GraphicsContextArray((200, 200))
f = Font("modern")
f = Font("sans-serif")
with gc:
gc.set_font(f)
gc.translate_ctm(50, 50)
Expand All @@ -55,7 +55,7 @@ def test_locale_independence(self):

with locale_context(locale.LC_CTYPE, ("C", "ASCII")):
gc = agg.GraphicsContextArray((200, 200))
f = Font("modern")
f = Font("sans-serif")
with gc:
gc.set_font(f)
gc.translate_ctm(50, 50)
Expand Down
2 changes: 1 addition & 1 deletion kiva/trait_defs/kiva_font_trait.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@
from enable.trait_defs.kiva_font_trait import KivaFont as _KivaFont

# old KivaFont defaulted to "modern" family rather than "default"
KivaFont = _KivaFont("modern 12")
KivaFont = _KivaFont("sans-serif 12")
3 changes: 2 additions & 1 deletion kiva/trait_defs/tests/test_kiva_font_trait.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
# Mapping of strings to Kiva font weights:
font_weights = {"bold": constants.WEIGHT_BOLD}


class FontExample(HasTraits):

font = KivaFont()
Expand Down Expand Up @@ -91,7 +92,7 @@ def test_font_trait_default(self):
example = FontExample()

self.assertIsInstance(example.font, Font)
self.assertEqual(example.font, Font(size=12, family=constants.MODERN))
self.assertEqual(example.font, Font(size=12, family=constants.SWISS))

def test_font_trait_none(self):
with self.assertRaises(TraitError):
Expand Down

0 comments on commit f65de7f

Please sign in to comment.