Commits

anatoly techtonik  committed 8fe1663

Add public API - have_font() and font_list() functions.

  • Participants
  • Parent commits 2059bba

Comments (0)

Files changed (1)

File fontquery.py

 """
 DEBUG = False
 
+__all__ = ['have_font', 'font_list']
+
 #-- CHAPTER 1: GET ALL SYSTEM FONTS USING EnumFontFamiliesEx FROM GDI --
 
 """
 # --- /define
 
 
+import sys
+PY3K = sys.version_info >= (3, 0)
+
+
 # --- prepare and call EnumFontFamiliesEx
 
 def query(charset=DEFAULT_CHARSET):
   # 3. Release DC
   user32.ReleaseDC(None, hdc)
 
-  # [ ] convert to string value for Python 3
-  return sorted(_font_names)
+  if PY3K: # convert to strings from bytes
+    return [name.decode('utf-8') for name in sorted(_font_names)]
+  else:
+    return sorted(_font_names)
+
+
+# --- Public API ---
+
+def have_font(name):
+  """Return True if font with specified name is present."""
+  if name in query():
+    return True
+  else:
+    return False
+
+def font_list():
+  """Return list of system installed font names."""
+  return query()
 
 
 if __name__ == '__main__':
-  names = query()
-  for f in names:
-    print('%s' % f)
   if DEBUG:
+    for f in font_list():
+      print(f)
     print("Total: %s" % len(names))
 
+  print('Have font "Arial"? %s' % have_font('Arial'))
+  print('Have font "missing-one"? %s' % have_font('missing-one'))
+
 
 
 #-- CHAPTER 2: WORK WITH FONT DIMENSIONS --