mirror of
https://github.com/python-pillow/Pillow.git
synced 2024-12-26 18:06:18 +03:00
Removing buffer from imagefont
This commit is contained in:
parent
ed9945c71f
commit
450a068ccb
|
@ -29,6 +29,7 @@ from __future__ import print_function
|
||||||
|
|
||||||
from PIL import Image
|
from PIL import Image
|
||||||
import os, sys
|
import os, sys
|
||||||
|
import warnings
|
||||||
|
|
||||||
class _imagingft_not_installed:
|
class _imagingft_not_installed:
|
||||||
# module placeholder
|
# module placeholder
|
||||||
|
@ -129,9 +130,19 @@ class ImageFont:
|
||||||
class FreeTypeFont:
|
class FreeTypeFont:
|
||||||
"FreeType font wrapper (requires _imagingft service)"
|
"FreeType font wrapper (requires _imagingft service)"
|
||||||
|
|
||||||
def __init__(self, file=None, size=10, index=0, encoding="", file_like=None):
|
def __init__(self, font=None, size=10, index=0, encoding="", file=None):
|
||||||
# FIXME: use service provider instead
|
# FIXME: use service provider instead
|
||||||
self.font = core.getfont(file, size, index, encoding, file_like)
|
if file:
|
||||||
|
warnings.warn('`file` parameter deprecated, please use `font` instead.', DeprecationWarning)
|
||||||
|
font = file
|
||||||
|
|
||||||
|
if isinstance(font, basestring):
|
||||||
|
self.font = core.getfont(font, size, index, encoding)
|
||||||
|
else:
|
||||||
|
bytes = font.read()
|
||||||
|
font.seek(0, 2)
|
||||||
|
size = font.tell()
|
||||||
|
self.font = core.getfont("", size, index, encoding, bytes, size)
|
||||||
|
|
||||||
def getname(self):
|
def getname(self):
|
||||||
return self.font.family, self.font.style
|
return self.font.family, self.font.style
|
||||||
|
@ -212,10 +223,15 @@ 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=None, size=10, index=0, encoding="", file_like=None):
|
def truetype(font=None, size=10, index=0, encoding="", filename=None):
|
||||||
"Load a truetype font file."
|
"Load a truetype font file."
|
||||||
|
|
||||||
|
if filename:
|
||||||
|
warnings.warn('`file` parameter deprecated, please use `font` instead.', DeprecationWarning)
|
||||||
|
font = filename
|
||||||
|
|
||||||
try:
|
try:
|
||||||
return FreeTypeFont(filename, size, index, encoding, file_like)
|
return FreeTypeFont(font, size, index, encoding)
|
||||||
except IOError:
|
except IOError:
|
||||||
if sys.platform == "win32":
|
if sys.platform == "win32":
|
||||||
# check the windows font repository
|
# check the windows font repository
|
||||||
|
@ -223,8 +239,8 @@ def truetype(filename=None, size=10, index=0, encoding="", file_like=None):
|
||||||
# 1.5.2's os.environ.get()
|
# 1.5.2's os.environ.get()
|
||||||
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", font)
|
||||||
return FreeTypeFont(filename, size, index, encoding, file_like)
|
return FreeTypeFont(font, size, index, encoding)
|
||||||
raise
|
raise
|
||||||
|
|
||||||
##
|
##
|
||||||
|
|
BIN
Tests/fonts/FreeMono.ttf
Normal file
BIN
Tests/fonts/FreeMono.ttf
Normal file
Binary file not shown.
|
@ -1,6 +1,7 @@
|
||||||
from tester import *
|
from tester import *
|
||||||
|
|
||||||
from PIL import Image
|
from PIL import Image
|
||||||
|
import StringIO
|
||||||
try:
|
try:
|
||||||
from PIL import ImageFont
|
from PIL import ImageFont
|
||||||
ImageFont.core.getfont # check if freetype is available
|
ImageFont.core.getfont # check if freetype is available
|
||||||
|
@ -8,5 +9,21 @@ except ImportError:
|
||||||
skip()
|
skip()
|
||||||
|
|
||||||
def test_sanity():
|
def test_sanity():
|
||||||
|
|
||||||
assert_match(ImageFont.core.freetype2_version, "\d+\.\d+\.\d+$")
|
assert_match(ImageFont.core.freetype2_version, "\d+\.\d+\.\d+$")
|
||||||
|
|
||||||
|
def test_font_with_name():
|
||||||
|
font_name = "Tests/fonts/FreeMono.ttf"
|
||||||
|
font_size = 10
|
||||||
|
assert_no_exception(lambda: ImageFont.truetype(font_name, font_size))
|
||||||
|
|
||||||
|
def test_font_with_filelike():
|
||||||
|
font_name = "Tests/fonts/FreeMono.ttf"
|
||||||
|
font_filelike = StringIO.StringIO(open(font_name, 'rb').read())
|
||||||
|
font_size = 10
|
||||||
|
assert_no_exception(lambda: ImageFont.truetype(font_filelike, font_size))
|
||||||
|
|
||||||
|
def test_font_old_parameters():
|
||||||
|
font_name = "Tests/fonts/FreeMono.ttf"
|
||||||
|
font_size = 10
|
||||||
|
assert_warning(DeprecationWarning, lambda: ImageFont.truetype(filename=font_name, size=font_size))
|
||||||
|
|
||||||
|
|
21
_imagingft.c
21
_imagingft.c
|
@ -102,15 +102,17 @@ getfont(PyObject* self_, PyObject* args, PyObject* kw)
|
||||||
char* filename = NULL;
|
char* filename = NULL;
|
||||||
int size;
|
int size;
|
||||||
int index = 0;
|
int index = 0;
|
||||||
unsigned char* encoding = NULL;
|
unsigned char* encoding;
|
||||||
Py_buffer file_like;
|
unsigned char** file_like;
|
||||||
|
int file_like_size = 0;
|
||||||
static char* kwlist[] = {
|
static char* kwlist[] = {
|
||||||
"filename", "size", "index", "encoding", "file_like", NULL
|
"filename", "size", "index", "encoding", "file_like", "file_like_size", NULL
|
||||||
};
|
};
|
||||||
|
|
||||||
if (!PyArg_ParseTupleAndKeywords(args, kw, "eti|isz*", kwlist,
|
if (!PyArg_ParseTupleAndKeywords(args, kw, "eti|isz*i", kwlist,
|
||||||
Py_FileSystemDefaultEncoding, &filename,
|
Py_FileSystemDefaultEncoding, &filename,
|
||||||
&size, &index, &encoding, &file_like))
|
&size, &index, &encoding, file_like,
|
||||||
|
&file_like_size))
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
if (!library) {
|
if (!library) {
|
||||||
|
@ -125,10 +127,10 @@ getfont(PyObject* self_, PyObject* args, PyObject* kw)
|
||||||
if (!self)
|
if (!self)
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
if (filename && file_like.len<0) {
|
if (filename && file_like_size <= 0) {
|
||||||
error = FT_New_Face(library, filename, index, &self->face);
|
error = FT_New_Face(library, filename, index, &self->face);
|
||||||
} else {
|
} else {
|
||||||
error = FT_New_Memory_Face(library, (FT_Byte*)file_like.buf, file_like.len, index, &self->face);
|
error = FT_New_Memory_Face(library, (FT_Byte*)file_like, file_like_size, index, &self->face);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!error)
|
if (!error)
|
||||||
|
@ -142,13 +144,12 @@ 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);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fprintf(stderr, "> %d %d %s\n", error, file_like_size, self);
|
||||||
|
|
||||||
return (PyObject*) self;
|
return (PyObject*) self;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user