39
39
from functools import partial
40
40
from copy import copy
41
41
from kivy import kivy_data_dir
42
+ from kivy .utils import platform
42
43
from kivy .graphics .texture import Texture
43
44
from kivy .core import core_select_lib
44
45
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
46
47
from kivy .compat import PY2
47
48
48
49
DEFAULT_FONT = 'DroidSans'
@@ -136,6 +137,8 @@ class LabelBase(object):
136
137
137
138
_fonts_cache = {}
138
139
140
+ _fonts_dirs = []
141
+
139
142
_texture_1px = None
140
143
141
144
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,
144
147
line_height = 1.0 , strip = False , shorten_from = 'center' ,
145
148
split_str = ' ' , unicode_errors = 'replace' , ** kwargs ):
146
149
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
+
147
154
options = {'text' : text , 'font_size' : font_size ,
148
155
'font_name' : font_name , 'bold' : bold , 'italic' : italic ,
149
156
'halign' : halign , 'valign' : valign , 'shorten' : shorten ,
@@ -229,6 +236,11 @@ def resolve_font_name(self):
229
236
options ['font_name_r' ] = fontscache [fontname ]
230
237
else :
231
238
filename = resource_find (fontname )
239
+ if not filename :
240
+ fontname = fontname + \
241
+ ('' if fontname .endswith ('.ttf' ) else '.ttf' )
242
+ filename = resource_find (fontname )
243
+
232
244
if filename is None :
233
245
# XXX for compatibility, check directly in the data dir
234
246
filename = os .path .join (kivy_data_dir , fontname )
@@ -237,6 +249,41 @@ def resolve_font_name(self):
237
249
fontscache [fontname ] = filename
238
250
options ['font_name_r' ] = filename
239
251
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
+
240
287
def get_extents (self , text ):
241
288
'''Return a tuple (width, height) indicating the size of the specified
242
289
text'''
@@ -695,3 +742,4 @@ def _set_text_size(self, x):
695
742
'data/fonts/DroidSans-Italic.ttf' ,
696
743
'data/fonts/DroidSans-Bold.ttf' ,
697
744
'data/fonts/DroidSans-BoldItalic.ttf' )
745
+
0 commit comments