Skip to content

Commit

Permalink
Merge pull request #3896 from ali65/master
Browse files Browse the repository at this point in the history
add font rendering options
  • Loading branch information
akshayaurora committed Jan 25, 2016
2 parents eb13dba + 4d64912 commit 9d07f45
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 10 deletions.
9 changes: 7 additions & 2 deletions kivy/core/text/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,9 @@ def __init__(
halign='left', valign='bottom', shorten=False,
text_size=None, mipmap=False, color=None, line_height=1.0, strip=False,
strip_reflow=True, shorten_from='center', split_str=' ',
unicode_errors='replace', **kwargs):
unicode_errors='replace',
font_hinting=None, font_kerning=None, font_blended=None,
**kwargs):

# Include system fonts_dir in resource paths.
# This allows us to specify a font from those dirs.
Expand All @@ -171,7 +173,10 @@ def __init__(
'mipmap': mipmap, 'line_height': line_height,
'strip': strip, 'strip_reflow': strip_reflow,
'shorten_from': shorten_from, 'split_str': split_str,
'unicode_errors': unicode_errors}
'unicode_errors': unicode_errors,
'font_hinting': font_hinting,
'font_kerning': font_kerning,
'font_blended': font_blended}

options['color'] = color or (1, 1, 1, 1)
options['padding'] = kwargs.get('padding', (0, 0))
Expand Down
41 changes: 39 additions & 2 deletions kivy/core/text/_text_sdl2.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,44 @@ cdef class _SurfaceContainer:
c.g = <int>(color[1] * 255)
c.b = <int>(color[2] * 255)
bytes_text = <bytes>text.encode('utf-8')
st = TTF_RenderUTF8_Blended(font, <char *>bytes_text, c)
hinting = (
container.options['font_hinting']
if 'font_hinting' in container.options
else None
)
if hinting == 'normal':
if TTF_GetFontHinting(font) != TTF_HINTING_NORMAL:
TTF_SetFontHinting(font, TTF_HINTING_NORMAL)
if hinting == 'light':
if TTF_GetFontHinting(font) != TTF_HINTING_LIGHT:
TTF_SetFontHinting(font, TTF_HINTING_LIGHT)
if hinting == 'mono':
if TTF_GetFontHinting(font) != TTF_HINTING_MONO:
TTF_SetFontHinting(font, TTF_HINTING_MONO)
if hinting == 'none':
if TTF_GetFontHinting(font) != TTF_HINTING_NONE:
TTF_SetFontHinting(font, TTF_HINTING_NONE)
kerning = (
container.options['font_kerning']
if 'font_kerning' in container.options
else None
)
if kerning is True:
if TTF_GetFontKerning(font) == 0:
TTF_SetFontKerning(font, 1)
if kerning is False:
if TTF_GetFontKerning(font) != 0:
TTF_SetFontKerning(font, 0)
blended = (
container.options['font_blended']
if 'font_blended' in container.options
else None
)
st = (
TTF_RenderUTF8_Blended(font, <char *>bytes_text, c)
if blended
else TTF_RenderUTF8_Solid(font, <char *>bytes_text, c)
)
if st == NULL:
return
r.x = x
Expand Down Expand Up @@ -108,7 +145,7 @@ cdef TTF_Font *_get_font(self):
print(s_error)
assert(0)

# set underline and strikethrough style
# set underline and strikethrough style
style = TTF_STYLE_NORMAL
if self.options['underline']:
style = style | TTF_STYLE_UNDERLINE
Expand Down
14 changes: 9 additions & 5 deletions kivy/lib/sdl2.pxi
Original file line number Diff line number Diff line change
Expand Up @@ -631,11 +631,11 @@ cdef extern from "SDL_ttf.h":
##define TTF_STYLE_ITALIC 0x02
##define TTF_STYLE_UNDERLINE 0x04
##define TTF_STYLE_STRIKETHROUGH 0x08
cdef int TTF_STYLE_NORMAL
cdef int TTF_STYLE_BOLD
cdef int TTF_STYLE_ITALIC
cdef int TTF_STYLE_UNDERLINE
cdef int TTF_STYLE_STRIKETHROUGH
cdef int TTF_STYLE_NORMAL = 0
cdef int TTF_STYLE_BOLD = 1
cdef int TTF_STYLE_ITALIC = 2
cdef int TTF_STYLE_UNDERLINE = 4
cdef int TTF_STYLE_STRIKETHROUGH = 8
cdef int TTF_GetFontStyle( TTF_Font *font)
cdef void TTF_SetFontStyle(TTF_Font *font, int style)
cdef int TTF_GetFontOutline( TTF_Font *font)
Expand All @@ -646,6 +646,10 @@ cdef extern from "SDL_ttf.h":
##define TTF_HINTING_LIGHT 1
##define TTF_HINTING_MONO 2
##define TTF_HINTING_NONE 3
cdef int TTF_HINTING_NORMAL = 0
cdef int TTF_HINTING_LIGHT = 1
cdef int TTF_HINTING_MONO = 2
cdef int TTF_HINTING_NONE = 3
cdef int TTF_GetFontHinting( TTF_Font *font)
cdef void TTF_SetFontHinting(TTF_Font *font, int hinting)

Expand Down
41 changes: 40 additions & 1 deletion kivy/uix/label.py
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,8 @@ class Label(Widget):
'halign', 'valign', 'padding_x', 'padding_y',
'text_size', 'shorten', 'mipmap', 'markup',
'line_height', 'max_lines', 'strip', 'shorten_from',
'split_str', 'unicode_errors')
'split_str', 'unicode_errors',
'font_hinting', 'font_kerning', 'font_blended')

def __init__(self, **kwargs):
self._trigger_texture = Clock.create_trigger(self.texture_update, -1)
Expand Down Expand Up @@ -812,3 +813,41 @@ def print_it(instance, value):
:attr:`strip` is a :class:`~kivy.properties.BooleanProperty` and
defaults to False.
'''

font_hinting = OptionProperty(
'none', options=['normal', 'light', 'mono', 'none'])
'''What hinting option to use for font rendering.
Can be `'normal'`, `'light'`, `'mono'`, `'none'`
.. note::
This feature requires a SDL2 window provider.
.. versionadded:: 1.9.2
:attr:`font_hinting` is an :class:`~kivy.properties.OptionProperty` and
defaults to `'none'`.
'''

font_kerning = BooleanProperty(False)
'''Whether kerning is enable for font rendering
.. note::
This feature requires a SDL2 window provider.
.. versionadded:: 1.9.2
:attr:`font_kerning` is a :class:`~kivy.properties.BooleanProperty` and
defaults to False.
'''

font_blended = BooleanProperty(False)
'''Whether blended or solid font rendering should be used
.. note::
This feature requires a SDL2 window provider.
.. versionadded:: 1.9.2
:attr:`font_blended` is a :class:`~kivy.properties.BooleanProperty` and
defaults to False.
'''

0 comments on commit 9d07f45

Please sign in to comment.