1
#------------------------------------------------------------------------------
3
# Copyright (c) 2005, Enthought, Inc.
6
# This software is provided without warranty under the terms of the BSD
7
# license included in enthought/LICENSE.txt and may be redistributed only
8
# under the conditions described in the aforementioned license. The license
9
# is also available online at http://www.enthought.com/licenses/BSD.txt
11
# Thanks for using Enthought open source!
13
# Author: David C. Morrill
16
#------------------------------------------------------------------------------
18
""" Trait definition for a wxPython-based font.
21
#-------------------------------------------------------------------------------
23
#-------------------------------------------------------------------------------
28
import Trait, TraitHandler, TraitError
30
#-------------------------------------------------------------------------------
31
# Convert a string into a valid 'wxFont' object (if possible):
32
#-------------------------------------------------------------------------------
34
# Mapping of strings to valid wxFont families
36
'default': wx.DEFAULT,
37
'decorative': wx.DECORATIVE,
44
# Mapping of strings to wxFont styles
50
# Mapping of strings wxFont weights
56
# Strings to ignore in text representations of fonts
57
font_noise = [ 'pt', 'point', 'family' ]
59
#-------------------------------------------------------------------------------
60
# Converts a wx.Font into a string description of itself:
61
#-------------------------------------------------------------------------------
63
def font_to_str ( font ):
64
""" Converts a wx.Font into a string description of itself.
66
weight = { wx.LIGHT: ' Light',
67
wx.BOLD: ' Bold' }.get( font.GetWeight(), '' )
68
style = { wx.SLANT: ' Slant',
69
wx.ITALIC: ' Italic' }.get( font.GetStyle(), '' )
71
if font.GetUnderlined():
72
underline = ' underline'
73
return '%s point %s%s%s%s' % (
74
font.GetPointSize(), font.GetFaceName(), style, weight, underline )
76
#-------------------------------------------------------------------------------
77
# Create a TraitFont object from a string description:
78
#-------------------------------------------------------------------------------
80
def create_traitsfont ( value ):
81
""" Create a TraitFont object from a string description.
83
if isinstance( value, wx.Font ):
84
value = font_to_str( value )
92
for word in value.split():
94
if font_families.has_key( lword ):
95
family = font_families[ lword ]
96
elif font_styles.has_key( lword ):
97
style = font_styles[ lword ]
98
elif font_weights.has_key( lword ):
99
weight = font_weights[ lword ]
100
elif lword == 'underline':
102
elif lword not in font_noise:
103
if point_size is None:
105
point_size = int( lword )
109
facename.append( word )
110
return TraitsFont( point_size or 10, family, style, weight, underline,
111
' '.join( facename ) )
113
#-------------------------------------------------------------------------------
114
# 'TraitsFont' class:
115
#-------------------------------------------------------------------------------
117
class TraitsFont ( wx.Font ):
118
""" A Traits-specific wx.Font.
120
#---------------------------------------------------------------------------
121
# Returns the pickleable form of a TraitsFont object:
122
#---------------------------------------------------------------------------
124
def __reduce_ex__ ( self, protocol ):
125
""" Returns the pickleable form of a TraitsFont object.
127
return ( create_traitsfont, ( font_to_str( self ), ) )
129
#---------------------------------------------------------------------------
130
# Returns a printable form of the font:
131
#---------------------------------------------------------------------------
133
def __str__ ( self ):
134
""" Returns a printable form of the font.
136
return font_to_str( self )
138
#-------------------------------------------------------------------------------
139
# 'TraitWXFont' class'
140
#-------------------------------------------------------------------------------
142
class TraitWXFont ( TraitHandler ):
143
""" Ensures that values assigned to a trait attribute are valid font
144
descriptor strings; the value actually assigned is the corresponding
147
#---------------------------------------------------------------------------
148
# Validates that the value is a valid font:
149
#---------------------------------------------------------------------------
151
def validate ( self, object, name, value ):
152
""" Validates that the value is a valid font descriptor string. If so,
153
it returns the corresponding TraitsFont; otherwise, it raises a
160
return create_traitsfont( value )
164
raise TraitError, ( object, name, 'a font descriptor string',
168
return ( "a string describing a font (e.g. '12 pt bold italic "
169
"swiss family Arial' or 'default 12')" )
171
#-------------------------------------------------------------------------------
172
# Define a wxPython specific font trait:
173
#-------------------------------------------------------------------------------
175
### Note: Declare the editor to be a function which returns the FontEditor
176
# class from traits ui to avoid circular import issues. For backwards
177
# compatibility with previous Traits versions, the 'editors' folder in Traits
178
# project declares 'from api import *' in its __init__.py. The 'api' in turn
179
# can contain classes that have a Font trait which lead to this file getting
180
# imported. This leads to a circular import when declaring a Font trait.
181
def get_font_editor(*args, **traits):
182
from font_editor import ToolkitEditorFactory
183
return ToolkitEditorFactory(*args, **traits)
186
WxFont = Trait( wx.SystemSettings_GetFont( wx.SYS_DEFAULT_GUI_FONT ), fh, editor = get_font_editor )