Skip to content

Commit 4ce149d

Browse files
committed
Merge pull request kivy#2673 from kivy/system_fonts
System fonts
2 parents a6afff2 + 4437955 commit 4ce149d

File tree

2 files changed

+52
-2
lines changed

2 files changed

+52
-2
lines changed

kivy/core/text/__init__.py

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,11 @@
3939
from functools import partial
4040
from copy import copy
4141
from kivy import kivy_data_dir
42+
from kivy.utils import platform
4243
from kivy.graphics.texture import Texture
4344
from kivy.core import core_select_lib
4445
from kivy.core.text.text_layout import layout_text, LayoutWord
45-
from kivy.resources import resource_find
46+
from kivy.resources import resource_find, resource_add_path
4647
from kivy.compat import PY2
4748

4849
DEFAULT_FONT = 'DroidSans'
@@ -136,6 +137,8 @@ class LabelBase(object):
136137

137138
_fonts_cache = {}
138139

140+
_fonts_dirs = []
141+
139142
_texture_1px = None
140143

141144
def __init__(self, text='', font_size=12, font_name=DEFAULT_FONT,
@@ -144,6 +147,10 @@ def __init__(self, text='', font_size=12, font_name=DEFAULT_FONT,
144147
line_height=1.0, strip=False, shorten_from='center',
145148
split_str=' ', unicode_errors='replace', **kwargs):
146149

150+
# Include system fonts_dir in resource paths.
151+
# This allows us to specify a font from those dirs.
152+
LabelBase.get_system_fonts_dir()
153+
147154
options = {'text': text, 'font_size': font_size,
148155
'font_name': font_name, 'bold': bold, 'italic': italic,
149156
'halign': halign, 'valign': valign, 'shorten': shorten,
@@ -229,6 +236,11 @@ def resolve_font_name(self):
229236
options['font_name_r'] = fontscache[fontname]
230237
else:
231238
filename = resource_find(fontname)
239+
if not filename:
240+
fontname = fontname + \
241+
('' if fontname.endswith('.ttf') else '.ttf')
242+
filename = resource_find(fontname)
243+
232244
if filename is None:
233245
# XXX for compatibility, check directly in the data dir
234246
filename = os.path.join(kivy_data_dir, fontname)
@@ -237,6 +249,41 @@ def resolve_font_name(self):
237249
fontscache[fontname] = filename
238250
options['font_name_r'] = filename
239251

252+
@staticmethod
253+
def get_system_fonts_dir():
254+
'''Return the Directory used by the system for fonts.
255+
'''
256+
if LabelBase._fonts_dirs:
257+
return LabelBase._fonts_dirs
258+
259+
fdirs = []
260+
if platform == 'linux':
261+
fdirs = [
262+
'/usr/share/fonts/truetype', '/usr/local/share/fonts',
263+
os.path.expanduser('~/.fonts'),
264+
os.path.expanduser('~/.local/share/fonts')]
265+
elif platform == 'macosx':
266+
fdirs = ['/Library/Fonts', '/System/Library/Fonts',
267+
os.path.expanduser('~/Library/Fonts')]
268+
elif platform == 'win':
269+
fdirs = [os.environ['SYSTEMROOT'] + os.sep + 'Fonts']
270+
elif platform == 'ios':
271+
fdirs = ['/Systiem/Library/Fonts']
272+
elif platform == 'android':
273+
fdirs = ['/system/fonts']
274+
275+
if fdirs:
276+
fdirs.append(kivy_data_dir + os.sep + 'fonts')
277+
# let's register the font dirs
278+
rdirs = []
279+
for _dir in fdirs:
280+
if os.path.exists(_dir):
281+
resource_add_path(_dir)
282+
rdirs.append(_dir)
283+
LabelBase._fonts_dirs = rdirs
284+
return rdirs
285+
raise Exception("Unknown Platform {}".format(platform))
286+
240287
def get_extents(self, text):
241288
'''Return a tuple (width, height) indicating the size of the specified
242289
text'''
@@ -695,3 +742,4 @@ def _set_text_size(self, x):
695742
'data/fonts/DroidSans-Italic.ttf',
696743
'data/fonts/DroidSans-Bold.ttf',
697744
'data/fonts/DroidSans-BoldItalic.ttf')
745+

kivy/core/text/_text_sdl2.pyx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,14 +104,16 @@ cdef TTF_Font *_get_font(self):
104104

105105
# fallback to search a system font
106106
if fontobject == NULL:
107-
s_error = (<bytes>SDL_GetError()).decode('utf-8')
107+
s_error = (<bytes>SDL_GetError()).encode('utf-8')
108108
print(s_error)
109109
assert(0)
110+
110111
sdl2_cache[fontid] = ttfc = _TTFContainer()
111112
ttfc.font = fontobject
112113
sdl2_cache_order.append(fontid)
113114

114115
# to prevent too much file open, limit the number of opened fonts to 64
116+
115117
while len(sdl2_cache_order) > 64:
116118
popid = sdl2_cache_order.pop(0)
117119
ttfc = sdl2_cache[popid]

0 commit comments

Comments
 (0)