forked from vayn/python-fontconfig
/
fontconfig.pyx
220 lines (187 loc) · 5.36 KB
/
fontconfig.pyx
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
# -*- coding: utf-8
'''
Python-fontconfig
~~~~~~~~~~~~~~~~~
Python binding for Fontconfig library.
.. _Python-fontconfig source code:
http://github.com/Vayn/python-fontconfig
:author: Vayn a.k.a VT <vayn@vayn.de>
:license: GPLv3+, see LICENSE for details.
'''
__version__ = '0.4.0'
__docformat__ = 'restructuredtext'
#------------------------------------------------
# Imports
#------------------------------------------------
from cpython.version cimport PY_MAJOR_VERSION
from cfontconfig cimport *
include 'fontconfig.pxi'
include 'factory.pxi'
#------------------------------------------------
# Code
#------------------------------------------------
# Factory Function
query = query
#
cdef class FcFont:
'''
FcF is a class of Fontconfig
This class provides all infomation about font.
TODO: Reduce the whole class
'''
# Fontconfig library version
__version__ = fc_version()
cdef:
FcPattern *_pat
FcBlanks *_blanks
FcCharSet *_cs
bytes _file
readonly dict _buf_dict
readonly dict _attr_dict
FcChar8 *cvar
int ivar
FcBool bvar
def __cinit__(self, file):
'''
:param file: The absolute path of the font
'''
self._file = file.encode('utf8')
self.init()
self.init_attrdict()
cdef init(self):
cdef char *file = self._file
cdef int count
self._blanks = FcConfigGetBlanks(NULL)
self._pat = FcFreeTypeQuery(<FcChar8*>file, 0, self._blanks, &count)
self._buf_dict = {}
cdef init_attrdict(self):
self._attr_dict = {}
lst = []
lst.append(['fontformat', 'foundry', 'capability'])
lst.append(['slant', 'index', 'weight', 'width', 'spacing'])
lst.append(['scalable', 'outline', 'decorative'])
for i in lst[0]:
self._attr_dict.update({i: 'str'})
for i in lst[1]:
self._attr_dict.update({i: 'int'})
for i in lst[2]:
self._attr_dict.update({i: 'bool'})
def __dealloc__(self):
if self._pat is not NULL:
FcPatternDestroy(self._pat)
self._pat = NULL
def __repr__(self):
try:
if PY_MAJOR_VERSION < 3:
# To avoid `UnicodeEncodeError` in Python 2
family = self.family[0][0].encode('utf8')
else:
family = self.family[0][0]
value = '<%s: %s>' % (
self.__class__.__name__,
family
)
except:
value = self.__class__
return value
property file:
def __get__(self):
return self._file.decode('utf8')
def __set__(self, file):
self._file = file.encode('utf8')
self.init()
def __getattr__(self, arg):
obj = arg.encode('utf8')
if self._attr_dict.get(arg) == 'str':
if self._buf_dict.get(arg) is None:
if FcPatternGetString(self._pat, obj, 0, &self.cvar) == Match:
ret = FcChar8_to_unicode(self.cvar)
self._buf_dict[arg] = ret
elif self._attr_dict.get(arg) == 'int':
if self._buf_dict.get(arg) is None:
if FcPatternGetInteger(self._pat, obj, 0, &self.ivar) == Match:
self._buf_dict[arg] = self.ivar
elif self._attr_dict.get(arg) == 'bool':
if self._buf_dict.get(arg) is None:
if FcPatternGetBool(self._pat, obj, 0, &self.bvar) == Match:
self._buf_dict[arg] = self.bvar
try:
return self._buf_dict[arg]
except KeyError:
raise AttributeError("This font doesn't have %s." % arg)
cdef list _langen(self, arg):
'''
Fragile code generator
Used by family, style and fullname to avoid repeated code
'''
cdef:
int id
bytes obj
bytes obj1
list got
list ret
ret = self._buf_dict.get(arg)
if ret is None:
id = 0
obj = arg.encode('utf8')
obj1 = obj+b'lang'
got = []
ret = []
while 1:
if FcPatternGetString(self._pat, obj, id, &self.cvar) == Match:
got.append(FcChar8_to_unicode(self.cvar))
FcPatternGetString(self._pat, obj1, id, &self.cvar)
got.append(FcChar8_to_unicode(self.cvar))
ret.append(tuple(got))
got = []
id += 1
else:
self._buf_dict[arg] = ret
return ret
else:
return ret
property family:
def __get__(self):
obj = 'family'
return self._langen(obj)
property style:
def __get__(self):
obj = 'style'
return self._langen(obj)
property fullname:
def __get__(self):
obj = 'fullname'
return self._langen(obj)
def lang(self):
'''Print all languages the font supports'''
cdef FcValue var
if FcPatternGet(self._pat, FC_LANG, 0, &var) == Match:
FcValuePrint(var)
def raw_info(self):
'''Print all infomation of the font in the wild'''
FcPatternPrint(self._pat)
cpdef bint has_char(self, ch):
'''
Check whether the font supports the given character
:param ch: The character you want to check
'''
cdef:
int ret = 0
int count
bytes byte_ch
FcChar32 ucs4_ch
if FcPatternGetCharSet(self._pat, FC_CHARSET, 0, &self._cs) != Match:
return ret
byte_ch = ch.encode('utf8')
FcUtf8ToUcs4(<FcChar8*>(<char*>byte_ch), &ucs4_ch, 3)
if FcCharSetHasChar(self._cs, ucs4_ch):
ret = 1
return ret
def charset_count(self):
'''
Count the amount of unicode characters in font
'''
cdef FcChar32 count = 0
if FcPatternGetCharSet(self._pat, FC_CHARSET, 0, &self._cs) == Match:
count = FcCharSetCount(self._cs)
return count