mirror of
https://github.com/python-pillow/Pillow.git
synced 2025-09-24 13:07:00 +03:00
Added FreeTypeFont has_characters()
This commit is contained in:
parent
9d39fe6ada
commit
97e7a971e6
|
@ -891,6 +891,15 @@ def test_anchor_invalid(font: ImageFont.FreeTypeFont) -> None:
|
|||
d.multiline_textbbox((0, 0), "foo\nbar", anchor=anchor)
|
||||
|
||||
|
||||
def test_has_characters(font: ImageFont.FreeTypeFont) -> None:
|
||||
assert font.has_characters("")
|
||||
|
||||
assert font.has_characters("Test text")
|
||||
assert font.has_characters(b"Test text")
|
||||
|
||||
assert not font.has_characters("Test \u0001")
|
||||
|
||||
|
||||
@pytest.mark.parametrize("bpp", (1, 2, 4, 8))
|
||||
def test_bitmap_font(layout_engine: ImageFont.Layout, bpp: int) -> None:
|
||||
text = "Bitmap Font"
|
||||
|
|
|
@ -636,6 +636,16 @@ class FreeTypeFont:
|
|||
start,
|
||||
)
|
||||
|
||||
def has_characters(self, text: str | bytes) -> bool:
|
||||
"""
|
||||
Check if the font has all of the characters in the text.
|
||||
|
||||
:param text: Text to render.
|
||||
|
||||
:return: Boolean.
|
||||
"""
|
||||
return self.font.hascharacters(text)
|
||||
|
||||
def font_variant(
|
||||
self,
|
||||
font: StrOrBytesPath | BinaryIO | None = None,
|
||||
|
|
|
@ -54,6 +54,7 @@ class Font:
|
|||
lang: str | None,
|
||||
/,
|
||||
) -> float: ...
|
||||
def hascharacters(self, string: str | bytes) -> bool: ...
|
||||
def getvarnames(self) -> list[bytes]: ...
|
||||
def getvaraxes(self) -> list[ImageFont.Axis]: ...
|
||||
def setvarname(self, instance_index: int, /) -> None: ...
|
||||
|
|
|
@ -517,6 +517,44 @@ text_layout(
|
|||
return count;
|
||||
}
|
||||
|
||||
static PyObject *
|
||||
font_hascharacters(FontObject *self, PyObject *args) {
|
||||
int i;
|
||||
char *buffer = NULL;
|
||||
FT_ULong ch;
|
||||
Py_ssize_t count;
|
||||
FT_GlyphSlot glyph;
|
||||
PyObject *string;
|
||||
|
||||
if (!PyArg_ParseTuple(args, "O", &string)) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (PyUnicode_Check(string)) {
|
||||
count = PyUnicode_GET_LENGTH(string);
|
||||
} else if (PyBytes_Check(string)) {
|
||||
PyBytes_AsStringAndSize(string, &buffer, &count);
|
||||
} else {
|
||||
PyErr_SetString(PyExc_TypeError, "expected string or bytes");
|
||||
return 0;
|
||||
}
|
||||
if (count == 0) {
|
||||
return Py_True;
|
||||
}
|
||||
|
||||
for (i = 0; i < count; i++) {
|
||||
if (buffer) {
|
||||
ch = buffer[i];
|
||||
} else {
|
||||
ch = PyUnicode_READ_CHAR(string, i);
|
||||
}
|
||||
if (FT_Get_Char_Index(self->face, ch) == 0) {
|
||||
return Py_False;
|
||||
}
|
||||
}
|
||||
return Py_True;
|
||||
}
|
||||
|
||||
static PyObject *
|
||||
font_getlength(FontObject *self, PyObject *args) {
|
||||
int length; /* length along primary axis, in 26.6 precision */
|
||||
|
@ -1451,6 +1489,7 @@ static PyMethodDef font_methods[] = {
|
|||
{"render", (PyCFunction)font_render, METH_VARARGS},
|
||||
{"getsize", (PyCFunction)font_getsize, METH_VARARGS},
|
||||
{"getlength", (PyCFunction)font_getlength, METH_VARARGS},
|
||||
{"hascharacters", (PyCFunction)font_hascharacters, METH_VARARGS},
|
||||
#if FREETYPE_MAJOR > 2 || (FREETYPE_MAJOR == 2 && FREETYPE_MINOR > 9) || \
|
||||
(FREETYPE_MAJOR == 2 && FREETYPE_MINOR == 9 && FREETYPE_PATCH == 1)
|
||||
{"getvarnames", (PyCFunction)font_getvarnames, METH_NOARGS},
|
||||
|
|
Loading…
Reference in New Issue
Block a user