mirror of
https://github.com/python-pillow/Pillow.git
synced 2024-12-26 01:46:18 +03:00
Allowing to pass font as file-like objects
This commit is contained in:
parent
d1c6db88d4
commit
ed9945c71f
|
@ -129,9 +129,9 @@ class ImageFont:
|
|||
class FreeTypeFont:
|
||||
"FreeType font wrapper (requires _imagingft service)"
|
||||
|
||||
def __init__(self, file, size, index=0, encoding=""):
|
||||
def __init__(self, file=None, size=10, index=0, encoding="", file_like=None):
|
||||
# FIXME: use service provider instead
|
||||
self.font = core.getfont(file, size, index, encoding)
|
||||
self.font = core.getfont(file, size, index, encoding, file_like)
|
||||
|
||||
def getname(self):
|
||||
return self.font.family, self.font.style
|
||||
|
@ -212,10 +212,10 @@ def load(filename):
|
|||
# @return A font object.
|
||||
# @exception IOError If the file could not be read.
|
||||
|
||||
def truetype(filename, size, index=0, encoding=""):
|
||||
def truetype(filename=None, size=10, index=0, encoding="", file_like=None):
|
||||
"Load a truetype font file."
|
||||
try:
|
||||
return FreeTypeFont(filename, size, index, encoding)
|
||||
return FreeTypeFont(filename, size, index, encoding, file_like)
|
||||
except IOError:
|
||||
if sys.platform == "win32":
|
||||
# check the windows font repository
|
||||
|
@ -224,7 +224,7 @@ def truetype(filename, size, index=0, encoding=""):
|
|||
windir = os.environ.get("WINDIR")
|
||||
if windir:
|
||||
filename = os.path.join(windir, "fonts", filename)
|
||||
return FreeTypeFont(filename, size, index, encoding)
|
||||
return FreeTypeFont(filename, size, index, encoding, file_like)
|
||||
raise
|
||||
|
||||
##
|
||||
|
|
20
_imagingft.c
20
_imagingft.c
|
@ -99,17 +99,18 @@ getfont(PyObject* self_, PyObject* args, PyObject* kw)
|
|||
FontObject* self;
|
||||
int error;
|
||||
|
||||
char* filename;
|
||||
char* filename = NULL;
|
||||
int size;
|
||||
int index = 0;
|
||||
unsigned char* encoding = NULL;
|
||||
Py_buffer file_like;
|
||||
static char* kwlist[] = {
|
||||
"filename", "size", "index", "encoding", NULL
|
||||
"filename", "size", "index", "encoding", "file_like", NULL
|
||||
};
|
||||
|
||||
if (!PyArg_ParseTupleAndKeywords(args, kw, "eti|is", kwlist,
|
||||
if (!PyArg_ParseTupleAndKeywords(args, kw, "eti|isz*", kwlist,
|
||||
Py_FileSystemDefaultEncoding, &filename,
|
||||
&size, &index, &encoding))
|
||||
&size, &index, &encoding, &file_like))
|
||||
return NULL;
|
||||
|
||||
if (!library) {
|
||||
|
@ -124,8 +125,12 @@ getfont(PyObject* self_, PyObject* args, PyObject* kw)
|
|||
if (!self)
|
||||
return NULL;
|
||||
|
||||
error = FT_New_Face(library, filename, index, &self->face);
|
||||
|
||||
if (filename && file_like.len<0) {
|
||||
error = FT_New_Face(library, filename, index, &self->face);
|
||||
} else {
|
||||
error = FT_New_Memory_Face(library, (FT_Byte*)file_like.buf, file_like.len, index, &self->face);
|
||||
}
|
||||
|
||||
if (!error)
|
||||
error = FT_Set_Pixel_Sizes(self->face, 0, size);
|
||||
|
||||
|
@ -137,6 +142,9 @@ getfont(PyObject* self_, PyObject* args, PyObject* kw)
|
|||
}
|
||||
|
||||
if (error) {
|
||||
if(file_like.len < 0) {
|
||||
PyBuffer_Release(&file_like);
|
||||
}
|
||||
PyObject_Del(self);
|
||||
return geterror(error);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user