@@ -156,44 +156,60 @@ def output_layout(layout, depth=1):
156156 return (' ' * depth + "* '{text}'\n " .format (text = layout ['text' ].strip ()))
157157
158158
159- def fonts_path (system = False ):
160- """Return the path for cross platform user fonts."""
161- if os .name == 'nt' :
162- import winreg
163- fonts_dir = os .path .join (winreg .ExpandEnvironmentStrings ('%windir%' ), 'fonts' )
164- elif sys .platform == 'darwin' :
165- if system :
166- fonts_dir = os .path .expanduser ('/Library/Fonts' )
167- else :
168- fonts_dir = os .path .expanduser ('~/Library/Fonts' )
169- elif sys .platform .startswith ('linux' ):
170- fonts_dir = os .path .expanduser ('~/.local/share/fonts/' )
171- else :
172- raise NotImplementedError ('System not supported!' )
173-
174- return fonts_dir
175-
176-
177159def copy_fonts (system = False ):
178160 """Copy needed files for running tests."""
179- fonts_folder = fonts_path (system = system )
161+ fonts_folder = FontDatabase .fonts_path (system = system )
162+
180163 if not os .path .isdir (fonts_folder ):
181164 os .makedirs (fonts_folder )
165+
182166 fonts_data_path = os .path .join (HERE , 'data' , 'fonts' )
183167 font_files = sorted ([item for item in os .listdir (fonts_data_path ) if item .endswith ('.ttf' )])
184168 for font_file in font_files :
185169 font_file_data_path = os .path .join (fonts_data_path , font_file )
186170 font_file_path = os .path .join (fonts_folder , font_file )
171+
187172 if not os .path .isfile (font_file_path ):
188173 shutil .copyfile (font_file_data_path , font_file_path )
174+ # Register font
175+
176+ if os .name == 'nt' :
177+ import winreg # noqa
178+ base_key = winreg .HKEY_LOCAL_MACHINE if system else winreg .HKEY_CURRENT_USER
179+ key_path = r"Software\Microsoft\Windows NT\CurrentVersion\Fonts"
180+
181+ if '_' in font_file :
182+ font_name = font_file .split ('_' )[- 1 ].split ('.' )[0 ]
183+ else :
184+ font_name = font_file .split ('.' )[0 ]
185+
186+ # This font has a space in its system name
187+ if font_name == 'WhiteSpace' :
188+ font_name = 'White Space'
189+
190+ font_name = font_name + ' (TrueType)'
191+
192+ with winreg .OpenKey (base_key , key_path , 0 , winreg .KEY_ALL_ACCESS ) as reg_key :
193+ value = None
194+ try :
195+ # Query if it exists
196+ value = winreg .QueryValueEx (reg_key , font_name )
197+ except FileNotFoundError :
198+ pass
199+
200+ # If it does not exists, add value
201+ if value != font_file_path :
202+ winreg .SetValueEx (reg_key , font_name , 0 , winreg .REG_SZ , font_file_path )
189203
190204
191205class ColosseumTestCase (TestCase ):
192206 """Install test fonts before running tests that use them."""
207+ _FONTS_ACTIVE = False
193208
194209 def __init__ (self , * args , ** kwargs ):
195210 super ().__init__ (* args , ** kwargs )
196- self .copy_fonts ()
211+ if self ._FONTS_ACTIVE is False :
212+ self .copy_fonts ()
197213
198214 def copy_fonts (self ):
199215 copy_fonts ()
@@ -204,6 +220,8 @@ def copy_fonts(self):
204220 raise Exception ('\n \n Testing fonts (Ahem & Ahem Extra) are not active.\n '
205221 '\n Please run the test suite one more time.\n ' )
206222
223+ ColosseumTestCase ._FONTS_ACTIVE = True
224+
207225
208226class LayoutTestCase (ColosseumTestCase ):
209227 def setUp (self ):
@@ -451,7 +469,9 @@ def test_method(self):
451469
452470
453471if __name__ == '__main__' :
454- print ('Copying test fonts...' )
455- print (fonts_path (system = True ))
456- copy_fonts (system = True )
457- print (list (sorted (os .listdir (fonts_path ()))))
472+ # On CI we use system font locations except for linux containers
473+ system = bool (os .environ .get ('GITHUB_WORKSPACE' , None ))
474+ if sys .platform .startswith ('linux' ):
475+ system = False
476+ print ('Copying test fonts to "{path}"...' .format (path = FontDatabase .fonts_path (system = system )))
477+ copy_fonts (system = system )
0 commit comments