mirror of
https://github.com/python-pillow/Pillow.git
synced 2025-06-05 05:33:10 +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:
|
class FreeTypeFont:
|
||||||
"FreeType font wrapper (requires _imagingft service)"
|
"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
|
# 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):
|
def getname(self):
|
||||||
return self.font.family, self.font.style
|
return self.font.family, self.font.style
|
||||||
|
@ -212,10 +212,10 @@ def load(filename):
|
||||||
# @return A font object.
|
# @return A font object.
|
||||||
# @exception IOError If the file could not be read.
|
# @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."
|
"Load a truetype font file."
|
||||||
try:
|
try:
|
||||||
return FreeTypeFont(filename, size, index, encoding)
|
return FreeTypeFont(filename, size, index, encoding, file_like)
|
||||||
except IOError:
|
except IOError:
|
||||||
if sys.platform == "win32":
|
if sys.platform == "win32":
|
||||||
# check the windows font repository
|
# check the windows font repository
|
||||||
|
@ -224,7 +224,7 @@ def truetype(filename, size, index=0, encoding=""):
|
||||||
windir = os.environ.get("WINDIR")
|
windir = os.environ.get("WINDIR")
|
||||||
if windir:
|
if windir:
|
||||||
filename = os.path.join(windir, "fonts", filename)
|
filename = os.path.join(windir, "fonts", filename)
|
||||||
return FreeTypeFont(filename, size, index, encoding)
|
return FreeTypeFont(filename, size, index, encoding, file_like)
|
||||||
raise
|
raise
|
||||||
|
|
||||||
##
|
##
|
||||||
|
|
18
_imagingft.c
18
_imagingft.c
|
@ -99,17 +99,18 @@ getfont(PyObject* self_, PyObject* args, PyObject* kw)
|
||||||
FontObject* self;
|
FontObject* self;
|
||||||
int error;
|
int error;
|
||||||
|
|
||||||
char* filename;
|
char* filename = NULL;
|
||||||
int size;
|
int size;
|
||||||
int index = 0;
|
int index = 0;
|
||||||
unsigned char* encoding = NULL;
|
unsigned char* encoding = NULL;
|
||||||
|
Py_buffer file_like;
|
||||||
static char* kwlist[] = {
|
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,
|
Py_FileSystemDefaultEncoding, &filename,
|
||||||
&size, &index, &encoding))
|
&size, &index, &encoding, &file_like))
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
if (!library) {
|
if (!library) {
|
||||||
|
@ -124,7 +125,11 @@ getfont(PyObject* self_, PyObject* args, PyObject* kw)
|
||||||
if (!self)
|
if (!self)
|
||||||
return NULL;
|
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)
|
if (!error)
|
||||||
error = FT_Set_Pixel_Sizes(self->face, 0, size);
|
error = FT_Set_Pixel_Sizes(self->face, 0, size);
|
||||||
|
@ -137,6 +142,9 @@ getfont(PyObject* self_, PyObject* args, PyObject* kw)
|
||||||
}
|
}
|
||||||
|
|
||||||
if (error) {
|
if (error) {
|
||||||
|
if(file_like.len < 0) {
|
||||||
|
PyBuffer_Release(&file_like);
|
||||||
|
}
|
||||||
PyObject_Del(self);
|
PyObject_Del(self);
|
||||||
return geterror(error);
|
return geterror(error);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user