2929 <!ATTLIST include file CDATA #REQUIRED>
3030]>
3131"""
32- _STANDARD_FONTS_DIR = os .path .join (os .path .abspath (os .path .dirname (__file__ )), "assets" , "fonts" )
32+ _STANDARD_FONTS_DIR = os .path .join (
33+ os .path .abspath (os .path .dirname (__file__ )), "assets" , "fonts"
34+ )
35+
3336
3437def is_admin ():
3538 if os .name == "nt" :
@@ -54,19 +57,29 @@ def require_admin(quiet_run: bool = False, verbose: bool = False):
5457 command = [sys .executable ] + sys .argv
5558 else :
5659 command = sys .argv
60+ command [0 ] = os .path .abspath (os .path .normpath (command [0 ]))
61+ if os .name == "nt" and command [0 ].startswith ("\\ \\ ?\\ " ):
62+ command [0 ] = command [0 ].replace ("\\ \\ ?\\ " , "" )
63+
64+ if verbose :
65+ print (command )
5766
5867 if os .name == "posix" :
5968 os .execvp ("sudo" , ["sudo" ] + command )
6069 else :
6170 params = " " .join (command [1 :])
62- ctypes .windll .shell32 .ShellExecuteW (
71+ result = ctypes .windll .shell32 .ShellExecuteW (
6372 None ,
6473 "runas" ,
6574 command [0 ],
6675 params ,
6776 None ,
6877 0 if quiet_run else 1 ,
6978 )
79+ if result <= 32 :
80+ if result == 5 :
81+ print ("ERROR: Access is denied." )
82+ raise ctypes .WinError (result )
7083 sys .exit (0 )
7184 else :
7285 if verbose :
@@ -152,7 +165,7 @@ def try_cmd(cmd):
152165 "magick.exe" if "ImageMagick-7" in imgmgck_directory else "convert.exe"
153166 )
154167 imgmgck_binary = os .path .join (imgmgck_directory , imgmgck_executable )
155- if os .path .exists (imgmgck_binary ):
168+ if os .path .isfile (imgmgck_binary ):
156169 return imgmgck_binary
157170 else :
158171 return "unset"
@@ -206,13 +219,13 @@ def _get_font_info(font_path: str) -> dict:
206219
207220
208221def register_font (fontpath : str , quiet_run : bool = False , verbose : bool = False ) -> str :
209- if not os .path .exists (fontpath ):
210- fontpath = os .path .join (_STANDARD_FONTS_DIR , fontpath )
211- if not os .path .exists (fontpath ):
222+ if not os .path .isfile (fontpath ):
223+ fontpath = os .path .join (_STANDARD_FONTS_DIR , os . path . basename ( fontpath ) )
224+ if not os .path .isfile (fontpath ):
212225 raise FileNotFoundError (f"Font file not found: { fontpath } " )
213226
214227 font_container = os .path .join (imagemagick_directory (), "type-ghostscript.xml" )
215- if not os .path .exists (font_container ):
228+ if not os .path .isfile (font_container ):
216229 raise FileNotFoundError ("Font container not found" )
217230
218231 require_admin (quiet_run , verbose )
@@ -250,11 +263,17 @@ def register_font(fontpath: str, quiet_run: bool = False, verbose: bool = False)
250263
251264def is_font_registered (font_path_or_name : str ) -> str | None :
252265 font_container = os .path .join (imagemagick_directory (), "type-ghostscript.xml" )
253- if not os .path .exists (font_container ):
266+ if not os .path .isfile (font_container ):
254267 raise FileNotFoundError ("Font container not found" )
255268
256- if os .path .exists (font_path_or_name ):
269+ if os .path .isfile (font_path_or_name ):
257270 font_name = _get_font_info (font_path_or_name )["Full Font Name" ]
271+ elif os .path .isfile (
272+ os .path .join (_STANDARD_FONTS_DIR , os .path .basename (font_path_or_name ))
273+ ):
274+ font_name = _get_font_info (
275+ os .path .join (_STANDARD_FONTS_DIR , os .path .basename (font_path_or_name ))
276+ )["Full Font Name" ]
258277 else :
259278 font_name = font_path_or_name .removesuffix (".ttf" )
260279
@@ -272,16 +291,16 @@ def _get_font_path(font: str) -> tuple[str, str]:
272291 if not font .endswith (".ttf" ):
273292 raise ValueError ("Only TrueType fonts are currently supported" )
274293
275- if os .path .exists (font ):
294+ if os .path .isfile (font ):
276295 injected_font_name = is_font_registered (font )
277296 if injected_font_name :
278297 return os .path .abspath (font ), injected_font_name
279298
280299 raise FontNotRegisteredError (font )
281300
282- font = os .path .join (_STANDARD_FONTS_DIR , font )
301+ font = os .path .join (_STANDARD_FONTS_DIR , os . path . basename ( font ) )
283302
284- if not os .path .exists (font ):
303+ if not os .path .isfile (font ):
285304 raise FileNotFoundError (f"Font '{ font } ' not found" )
286305
287306 injected_font_name = is_font_registered (font )
@@ -295,13 +314,19 @@ def unregister_font(
295314 font_path_or_name : str , quiet_run : bool = False , verbose : bool = False
296315) -> bool :
297316 font_container = os .path .join (imagemagick_directory (), "type-ghostscript.xml" )
298- if not os .path .exists (font_container ):
317+ if not os .path .isfile (font_container ):
299318 raise FileNotFoundError ("Font container not found" )
300319
301320 require_admin (quiet_run , verbose )
302321
303- if os .path .exists (font_path_or_name ):
322+ if os .path .isfile (font_path_or_name ):
304323 font_name = _get_font_info (font_path_or_name )["Full Font Name" ]
324+ elif os .path .isfile (
325+ os .path .join (_STANDARD_FONTS_DIR , os .path .basename (font_path_or_name ))
326+ ):
327+ font_name = _get_font_info (
328+ os .path .join (_STANDARD_FONTS_DIR , os .path .basename (font_path_or_name ))
329+ )["Full Font Name" ]
305330 else :
306331 font_name = font_path_or_name .removesuffix (".ttf" )
307332
0 commit comments