Permalink
Browse files

core/text: implement Label.register(), to register an font alias with…

… associated ttf for bold/italic/bolditalic.

Added DroidSans alias, and use it almost everywhere.
  • Loading branch information...
tito committed Feb 7, 2012
1 parent ae531b9 commit 8a853ea27f860cba2447b9bc99a59b81e6dfa7cd
View
@@ -18,11 +18,14 @@
from kivy.graphics.texture import Texture
from kivy.core import core_select_lib
from kivy.utils import platform
from kivy.resources import resource_find
DEFAULT_FONT = 'Liberation Sans,Bitstream Vera Sans,Free Sans,Arial, Sans'
label_font_cache = {}
FONT_REGULAR = 0
FONT_ITALIC = 1
FONT_BOLD = 2
FONT_BOLDITALIC = 3
class LabelBase(object):
'''Core text label.
@@ -74,6 +77,10 @@ class LabelBase(object):
_cache_glyphs = {}
_fonts = {}
_fonts_cache = {}
def __init__(self, **kwargs):
kwargs.setdefault('font_size', 12)
kwargs.setdefault('font_name', DEFAULT_FONT)
@@ -86,6 +93,7 @@ def __init__(self, **kwargs):
kwargs.setdefault('padding_y', None)
kwargs.setdefault('shorten', False)
kwargs.setdefault('mipmap', False)
kwargs.setdefault('color', (1, 1, 1, 1))
padding = kwargs.get('padding', None)
if not kwargs.get('padding_x', None):
@@ -121,20 +129,86 @@ def __init__(self, **kwargs):
self.options = kwargs
self.texture = None
if 'font_name' in self.options:
fontname = self.options['font_name']
if fontname in label_font_cache:
if label_font_cache[fontname] is not None:
self.options['font_name'] = label_font_cache[fontname]
self.resolve_font_name()
self.text = kwargs.get('text', '')
@staticmethod
def register(name, fn_regular, fn_italic=None, fn_bold=None,
fn_bolditalic=None):
'''Register an alias for a Font.
.. versionadded:: 1.0.10
If you're using directly a ttf, you might not be able to use bold/italic
of the ttf version. If the font is delivered with different version of
it (one regular, one italic and one bold), then you need to register it
and use the alias instead.
All the fn_regular/fn_italic/fn_bold parameters are resolved with
:func:`kivy.resources.resource_find`. If fn_italic/fn_bold are None,
fn_regular will be used instead.
'''
_fn_regular = resource_find(fn_regular)
if _fn_regular is None:
raise IOError('File %r not found' % fn_regular)
if fn_italic is None:
_fn_italic = _fn_regular
else:
_fn_italic = resource_find(fn_italic)
if _fn_italic is None:
raise IOError('File %r not found' % fn_italic)
if fn_bold is None:
_fn_bold = _fn_regular
else:
_fn_bold = resource_find(fn_bold)
if _fn_bold is None:
raise IOError('File %r not found' % fn_bold)
if fn_bolditalic is None:
_fn_bolditalic = _fn_regular
else:
_fn_bolditalic = resource_find(fn_bolditalic)
if _fn_bolditalic is None:
raise IOError('Label: File %r not found' % fn_bolditalic)
LabelBase._fonts[name] = (_fn_regular, _fn_italic, _fn_bold, _fn_bolditalic)
def resolve_font_name(self):
options = self.options
if 'font_name' not in options:
return
fontname = options['font_name']
fonts = self._fonts
fontscache = self._fonts_cache
# is the font is registered ?
if fontname in fonts:
# return the prefered font for the current bold/italic combinaison
bold = options['bold']
italic = options['italic']
font = fonts[fontname]
if not bold and not italic:
options['font_name'] = font[FONT_REGULAR]
elif bold and italic:
options['font_name'] = font[FONT_BOLDITALIC]
elif bold:
options['font_name'] = font[FONT_BOLD]
else:
options['font_name'] = font[FONT_ITALIC]
elif fontname in fontscache:
options['font_name'] = fontscache[fontname]
else:
filename = resource_find(fontname)
if filename is None:
# XXX for compatibility, check directly in the data dir
filename = os.path.join(kivy_data_dir, fontname)
if os.path.exists(filename):
label_font_cache[fontname] = filename
self.options['font_name'] = filename
else:
label_font_cache[fontname] = None
if not os.path.exists(filename):
filename = None
if filename is None:
raise IOError('Label: File %r not found' % fontname)
fontscache[fontname] = filename
options['font_name'] = filename
self.text = kwargs.get('text', '')
def get_extents(self, text):
'''Return a tuple with (width, height) for a text.'''
@@ -440,3 +514,10 @@ def _set_text_size(self, x):
('pil', 'text_pil', 'LabelPIL'),
))
# For the first initalization, register the default font
Label.register('DroidSans',
'data/fonts/DroidSans.ttf',
'data/fonts/DroidSans-Italic.ttf',
'data/fonts/DroidSans-Bold.ttf',
'data/fonts/DroidSans-BoldItalic.ttf')
Binary file not shown.
View
Binary file not shown.
View
@@ -125,7 +125,7 @@ def texture_update(self, *largs):
default to (None, None), meaning no size restriction by default.
'''
font_name = StringProperty('fonts/DroidSans.ttf')
font_name = StringProperty('DroidSans')
'''Filename of the font to use, the path can be absolute or relative.
Relative paths are resolved by the :func:`~kivy.resources.resource_find`
function.
@@ -144,7 +144,7 @@ def texture_update(self, *largs):
.. |unicodechar| image:: images/unicode-char.png
:data:`font_name` is a :class:`~kivy.properties.StringProperty`, default to
'fonts/DroidSans.ttf'.
'DroidSans'.
'''
font_size = NumericProperty(12)
View
@@ -1192,7 +1192,7 @@ def _set_text(self, text):
:data:`text` a :class:`~kivy.properties.StringProperty`.
'''
font_name = StringProperty('fonts/DroidSans.ttf')
font_name = StringProperty('DroidSans')
'''Filename of the font to use, the path can be absolute or relative.
Relative paths are resolved by the :func:`~kivy.resources.resource_find`
function.
@@ -1211,7 +1211,7 @@ def _set_text(self, text):
.. |unicodechar| image:: images/unicode-char.png
:data:`font_name` is a :class:`~kivy.properties.StringProperty`, default to
'fonts/DroidSans.ttf'.
'DroidSans'.
'''
font_size = NumericProperty(10)

0 comments on commit 8a853ea

Please sign in to comment.