Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 65 lines (59 sloc) 1.503 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
# -*- coding: utf-8
# @Author: Vayn a.k.a. VT <vayn@vayn.de>
# @Name: factory.pxi

cpdef query(family='', lang='', with_index=False):
  '''
Produce font object list for the queried language
'''
  cdef:
    FcChar8 *strpat
    FcPattern *pat = NULL
    FcFontSet *fs = NULL
    FcObjectSet *os = NULL
    list lst = []

  if lang:
    l_lang = ('%s:lang=%s' % (family, lang))
  else:
    l_lang = family
  l_lang = l_lang.encode('utf-8')
  strpat = <FcChar8*>(<char*>l_lang)
  pat = FcNameParse(strpat)
  if with_index:
    os = FcObjectSetBuild(FC_CHARSET, FC_FILE, FC_INDEX, NULL)
  else:
    os = FcObjectSetBuild(FC_CHARSET, FC_FILE, NULL)
  fs = FcFontList(<FcConfig*>0, pat, os)
  if fs is NULL or fs.nfont < 1:
    return lst

  cdef:
    int i
    FcChar8 *file
    FcCharSet *cs
    int index
  for i in range(fs.nfont):
    if FcPatternGetCharSet(fs.fonts[i], FC_CHARSET, 0, &cs) != Match:
      continue
    if FcPatternGetString(fs.fonts[i], FC_FILE, 0, &file) == Match:
      if with_index:
        if FcPatternGetInteger(fs.fonts[i], FC_INDEX, 0, &index) != Match:
          continue
        lst.append(((<char*>file).decode('utf8'), index))
      else:
        lst.append((<char*>file).decode('utf8'))

  FcPatternDestroy(pat)
  pat = NULL
  FcObjectSetDestroy(os)
  os = NULL
  FcCharSetDestroy(cs)
  cs = NULL
  FcFontSetDestroy(fs)
  fs = NULL
  return lst

def fromName(name):
  cdef:
    list names
  names = query(name)
  if names:
    return FcFont(names[0])
  else:
    return
Something went wrong with that request. Please try again.