-
Notifications
You must be signed in to change notification settings - Fork 44
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
New, more modern KivaFont trait #929
Merged
Merged
Changes from 1 commit
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
3dcfb9e
Add a new font-editor that uses Enable to draw fonts
corranwebster d588a71
New more modern KivaFont trait
corranwebster bd05d4b
Merge branch 'main' into enh/new-font-trait
corranwebster 3fc6f67
Fix long line.
corranwebster 32a3b36
Save all files before finishing merge!
corranwebster 555419e
Fix tests.
corranwebster cb2b1da
Apply suggestions from code review
corranwebster 82ea9fd
Handle color editor tests for null backend.
corranwebster 01a4109
Fixes from PR review.
corranwebster ea32353
Some more small fixes coming from PR discussion.
corranwebster File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
# (C) Copyright 2008-2022 Enthought, Inc., Austin, TX | ||
# All rights reserved. | ||
# | ||
# This software is provided without warranty under the terms of the BSD | ||
# license included in LICENSE.txt and may be redistributed only under | ||
# the conditions described in the aforementioned license. The license | ||
# is also available online at http://www.enthought.com/licenses/BSD.txt | ||
# | ||
# Thanks for using Enthought open source! | ||
|
||
import unittest | ||
|
||
from traits.api import HasTraits, TraitError | ||
|
||
from kiva.fonttools.font import Font, FAMILIES | ||
from kiva import constants | ||
from enable.trait_defs.kiva_font_trait import ( | ||
KivaFont, font_families, font_styles, font_weights | ||
mdickinson marked this conversation as resolved.
Show resolved
Hide resolved
mdickinson marked this conversation as resolved.
Show resolved
Hide resolved
|
||
) | ||
|
||
|
||
class FontExample(HasTraits): | ||
|
||
font = KivaFont() | ||
|
||
|
||
class TestKivaFont(unittest.TestCase): | ||
|
||
def test_validate_str(self): | ||
expected_outcomes = {} | ||
expected_outcomes[""] = Font(size=10, family=constants.DEFAULT) | ||
|
||
for weight, kiva_weight in font_weights.items(): | ||
expected_outcomes[weight] = Font(weight=kiva_weight, size=10, family=constants.DEFAULT) | ||
jwiggins marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
for style, kiva_style in font_styles.items(): | ||
expected_outcomes[style] = Font(style=kiva_style, size=10, family=constants.DEFAULT) | ||
|
||
expected_outcomes["underline"] = Font(underline=True, size=10, family=constants.DEFAULT) | ||
|
||
expected_outcomes["18"] = Font(size=18, family=constants.DEFAULT) | ||
expected_outcomes["18 pt"] = Font(size=18, family=constants.DEFAULT) | ||
expected_outcomes["18 point"] = Font(size=18, family=constants.DEFAULT) | ||
|
||
for family, kiva_family in FAMILIES.items(): | ||
expected_outcomes[family] = Font(family=kiva_family, size=10) | ||
|
||
expected_outcomes["Courier"] = Font("Courier", size=10, family=constants.DEFAULT) | ||
expected_outcomes["Comic Sans"] = Font("Comic Sans", size=10, family=constants.DEFAULT) | ||
expected_outcomes["18 pt Bold Italic Underline Comic Sans script"] = Font( | ||
"Comic Sans", 18, constants.SCRIPT, weight=constants.WEIGHT_BOLD, | ||
style=constants.ITALIC, underline=True, | ||
) | ||
|
||
for name, expected in expected_outcomes.items(): | ||
with self.subTest(name=name): | ||
example = FontExample(font=name) | ||
result = example.font | ||
|
||
# test we get expected font | ||
self.assertIsInstance(result, Font) | ||
self.assertEqual(result, expected) | ||
|
||
def test_validate_font(self): | ||
font = Font("Comic Sans", 18) | ||
example = FontExample(font=font) | ||
|
||
result = example.font | ||
|
||
# test we get expected font | ||
self.assertIsInstance(result, Font) | ||
self.assertIs(result, font) | ||
|
||
def test_validate_pyface_font(self): | ||
font = Font("Comic Sans", 18) | ||
example = FontExample(font=font) | ||
|
||
result = example.font | ||
|
||
# test we get expected font | ||
self.assertIsInstance(result, Font) | ||
self.assertIs(result, font) | ||
jwiggins marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
def test_font_trait_default(self): | ||
example = FontExample() | ||
|
||
self.assertIsInstance(example.font, Font) | ||
self.assertEqual(example.font, Font(size=12, family=constants.MODERN)) | ||
|
||
def test_font_trait_none(self): | ||
with self.assertRaises(TraitError): | ||
FontExample(font=None) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
# (C) Copyright 2005-2022 Enthought, Inc., Austin, TX | ||
# All rights reserved. | ||
# | ||
# This software is provided without warranty under the terms of the BSD | ||
# license included in LICENSE.txt and may be redistributed only under | ||
# the conditions described in the aforementioned license. The license | ||
# is also available online at http://www.enthought.com/licenses/BSD.txt | ||
# | ||
# Thanks for using Enthought open source! | ||
""" Trait definition for a wxPython-based Kiva font. | ||
""" | ||
|
||
from pyface.font import Font as PyfaceFont | ||
from traits.api import DefaultValue, TraitError, TraitType, NoDefaultSpecified | ||
|
||
import kiva.constants as kc | ||
from kiva.fonttools.font import Font, simple_parser | ||
corranwebster marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
|
||
font_attrs = [ | ||
'face_name', 'size', 'family', 'weight', 'style', 'underline', 'encoding', | ||
] | ||
|
||
pyface_family_to_kiva_family = { | ||
mdickinson marked this conversation as resolved.
Show resolved
Hide resolved
|
||
'default': kc.DEFAULT, | ||
'fantasy': kc.DECORATIVE, | ||
'decorative': kc.DECORATIVE, | ||
'serif': kc.ROMAN, | ||
'roman': kc.ROMAN, | ||
'cursive': kc.SCRIPT, | ||
'script': kc.SCRIPT, | ||
'sans-serif': kc.SWISS, | ||
'swiss': kc.SWISS, | ||
'monospace': kc.MODERN, | ||
'modern': kc.MODERN, | ||
'typewriter': kc.TELETYPE, | ||
'teletype': kc.TELETYPE, | ||
} | ||
|
||
|
||
def pyface_font_to_font(font): | ||
"""Convert a Pyface font to an equivalent Kiva Font. | ||
|
||
This ignores stretch and some options like small caps and strikethrough | ||
as the Kiva font object can't represent these at the moment. | ||
|
||
Parameters | ||
---------- | ||
font : Pyface Font instance | ||
The font to convert. | ||
|
||
Returns | ||
------- | ||
font : Kiva Font instance | ||
The resulting Kiva Font object. | ||
""" | ||
face_name = font.family[0] | ||
for face in font.family: | ||
if face in pyface_family_to_kiva_family: | ||
family = pyface_family_to_kiva_family[face] | ||
break | ||
else: | ||
family = kc.DEFAULT | ||
size = int(font.size) | ||
weight = font.weight_ | ||
style = kc.NORMAL if font.style is 'normal' else kc.ITALIC | ||
corranwebster marked this conversation as resolved.
Show resolved
Hide resolved
|
||
underline = 'underline' in font.decorations | ||
return Font(face_name, size, family, weight, style, underline) | ||
|
||
|
||
class KivaFont(TraitType): | ||
""" A Trait which casts strings to a Kiva Font value. | ||
""" | ||
|
||
#: The default value should be a tuple (factory, args, kwargs) | ||
default_value_type = DefaultValue.callable_and_args | ||
|
||
#: The parser to use when converting text to keyword args. This should | ||
#: accept a string and return a dictionary of Font class trait values (ie. | ||
#: "family", "size", "weight", etc.). | ||
mdickinson marked this conversation as resolved.
Show resolved
Hide resolved
|
||
parser = None | ||
|
||
def __init__(self, default_value=None, *, parser=simple_parser, **metadata): | ||
self.parser = parser | ||
default_value = self._get_default_value(default_value) | ||
super().__init__(default_value, **metadata) | ||
|
||
def validate(self, object, name, value): | ||
if isinstance(value, Font): | ||
return value | ||
if isinstance(value, PyfaceFont): | ||
return pyface_font_to_font(value) | ||
if isinstance(value, str): | ||
try: | ||
return Font(**self.parser(value)) | ||
except FontParseError: | ||
self.error(object, name, value) | ||
mdickinson marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
self.error(object, name, value) | ||
|
||
def info(self): | ||
return ( | ||
"a Kiva Font, a Pyface Font, or a string describing a font" | ||
) | ||
|
||
def get_editor(self, trait): | ||
from enable.trait_defs.ui.kiva_font_editor import KivaFontEditor | ||
return KivaFontEditor() | ||
|
||
def clone(self, default_value=NoDefaultSpecified, **metadata): | ||
mdickinson marked this conversation as resolved.
Show resolved
Hide resolved
|
||
new = super().clone(NoDefaultSpecified, **metadata) | ||
if default_value is not NoDefaultSpecified: | ||
new.default_value = self._get_default_value(default_value) | ||
mdickinson marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return new | ||
|
||
def _get_default_value(self, default_value): | ||
"""Construct a default value suitable for callable_and_args.""" | ||
if default_value is not None: | ||
try: | ||
font = self.validate(None, None, default_value) | ||
except TraitError: | ||
raise ValueError( | ||
"expected " + self.info() | ||
+ f", but got {default_value!r}" | ||
corranwebster marked this conversation as resolved.
Show resolved
Hide resolved
|
||
) | ||
klass = font.__class__ | ||
kwargs = {attr: getattr(font, attr) for attr in font_attrs} | ||
else: | ||
klass = Font | ||
kwargs = {} | ||
return (klass, (), kwargs) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nits:
font_trait
is an instance ofKivaFont
, rather than a synonym for it. Worth rephrasing?