Skip to content

Commit

Permalink
Font support for OSX using system_profiler
Browse files Browse the repository at this point in the history
Updated initsysfonts_darwin() to use the system_profiler command if fc-list is not found
  • Loading branch information
KuzyWoozy committed Sep 15, 2018
1 parent d5cb29b commit c79d701
Showing 1 changed file with 53 additions and 1 deletion.
54 changes: 53 additions & 1 deletion src_py/sysfont.py
Expand Up @@ -138,8 +138,60 @@ def initsysfonts_win32():
return fonts


def system_profiler_darwin():
import xml.etree.ElementTree as ET

fonts = {}

try:
#Get the Font info via xml using system_profiler command
mac_fonts_xml, errors = subprocess.Popen(['system_profiler','-xml','SPFontsDataType'],stdout=-1).communicate()

This comment has been minimized.

Copy link
@illume

illume Sep 15, 2018

Remember to check the exit code of system_profiler is 0 (meaning success). The man system_profiler does not say anything about other error codes.

except Exception:
return fonts

try:
#Get the ElementTree object from the xml string

This comment has been minimized.

Copy link
@illume

illume Sep 15, 2018

Probably should make a function out of every thing in here. Which system_profiler_darwin would call.

mac_fonts_xml_tree = ET.fromstring(mac_fonts_xml)

#Get the elements of the tree where the font information is located
font_xml_elements = mac_fonts_xml_tree.iterfind("./array/dict/array/dict")


for font_node in font_xml_elements:

font_name = font_path = None

#Gets all the text within each tag in the node
tag_text = font_node.itertext()

#Cleans the list of any whitespace items
tag_content_list = [content for content in tag_text if content.strip() != ""]


for count, tag_content in enumerate(tag_content_list):
if tag_content == "_name":
font_name = tag_content_list[count+1].lower()
if splitext(font_name)[1] not in OpenType_extensions:
break

if tag_content == "path" and font_name is not None:
font_path = tag_content_list[count+1]
bold = "bold" in font_name
italic = "italic" in font_name
_addfont(_simplename(font_name),bold,italic,font_path,fonts)
break

except Exception:
return fonts


return fonts



def initsysfonts_darwin():
"""read the fonts on OS X. X11 is required for this to work."""
fonts = {}
# if the X11 binary exists... try and use that.
# Not likely to be there on pre 10.4.x ...
if exists("/usr/X11/bin/fc-list"):
Expand All @@ -149,7 +201,7 @@ def initsysfonts_darwin():
elif exists("/usr/X11R6/bin/fc-list"):
fonts = initsysfonts_unix("/usr/X11R6/bin/fc-list")
else:
fonts = {}
fonts = system_profiler_darwin()

This comment has been minimized.

Copy link
@illume

illume Sep 15, 2018

Use your system_profiler_darwin code, and remove the fc-list stuff.


return fonts

Expand Down

1 comment on commit c79d701

@illume
Copy link

@illume illume commented on c79d701 Sep 15, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice job :)

When you're ready, you should do now is create a Pull Request from your fork.

Please sign in to comment.