mirror of
https://github.com/python-pillow/Pillow.git
synced 2025-07-04 03:43:13 +03:00
Merge pull request #4664 from nulano/ft-getsize-mode
This commit is contained in:
commit
390b34c231
BIN
Tests/images/text_mono.gif
Normal file
BIN
Tests/images/text_mono.gif
Normal file
Binary file not shown.
After Width: | Height: | Size: 1.5 KiB |
|
@ -11,6 +11,7 @@ from PIL import Image, ImageDraw, ImageFont
|
||||||
|
|
||||||
from .helper import (
|
from .helper import (
|
||||||
assert_image_equal,
|
assert_image_equal,
|
||||||
|
assert_image_equal_tofile,
|
||||||
assert_image_similar,
|
assert_image_similar,
|
||||||
assert_image_similar_tofile,
|
assert_image_similar_tofile,
|
||||||
is_pypy,
|
is_pypy,
|
||||||
|
@ -736,3 +737,19 @@ class TestImageFont:
|
||||||
@skip_unless_feature("raqm")
|
@skip_unless_feature("raqm")
|
||||||
class TestImageFont_RaqmLayout(TestImageFont):
|
class TestImageFont_RaqmLayout(TestImageFont):
|
||||||
LAYOUT_ENGINE = ImageFont.LAYOUT_RAQM
|
LAYOUT_ENGINE = ImageFont.LAYOUT_RAQM
|
||||||
|
|
||||||
|
|
||||||
|
def test_render_mono_size():
|
||||||
|
# issue 4177
|
||||||
|
|
||||||
|
if distutils.version.StrictVersion(ImageFont.core.freetype2_version) < "2.4":
|
||||||
|
pytest.skip("Different metrics")
|
||||||
|
|
||||||
|
im = Image.new("P", (100, 30), "white")
|
||||||
|
draw = ImageDraw.Draw(im)
|
||||||
|
ttf = ImageFont.truetype(
|
||||||
|
"Tests/fonts/DejaVuSans.ttf", 18, layout_engine=ImageFont.LAYOUT_BASIC
|
||||||
|
)
|
||||||
|
|
||||||
|
draw.text((10, 10), "r" * 10, "black", ttf)
|
||||||
|
assert_image_equal_tofile(im, "Tests/images/text_mono.gif")
|
||||||
|
|
|
@ -259,7 +259,7 @@ class FreeTypeFont:
|
||||||
|
|
||||||
:return: (width, height)
|
:return: (width, height)
|
||||||
"""
|
"""
|
||||||
size, offset = self.font.getsize(text, direction, features, language)
|
size, offset = self.font.getsize(text, False, direction, features, language)
|
||||||
return (
|
return (
|
||||||
size[0] + stroke_width * 2 + offset[0],
|
size[0] + stroke_width * 2 + offset[0],
|
||||||
size[1] + stroke_width * 2 + offset[1],
|
size[1] + stroke_width * 2 + offset[1],
|
||||||
|
@ -468,7 +468,9 @@ class FreeTypeFont:
|
||||||
:py:mod:`PIL.Image.core` interface module, and the text offset, the
|
:py:mod:`PIL.Image.core` interface module, and the text offset, the
|
||||||
gap between the starting coordinate and the first marking
|
gap between the starting coordinate and the first marking
|
||||||
"""
|
"""
|
||||||
size, offset = self.font.getsize(text, direction, features, language)
|
size, offset = self.font.getsize(
|
||||||
|
text, mode == "1", direction, features, language
|
||||||
|
)
|
||||||
size = size[0] + stroke_width * 2, size[1] + stroke_width * 2
|
size = size[0] + stroke_width * 2, size[1] + stroke_width * 2
|
||||||
im = fill("L", size, 0)
|
im = fill("L", size, 0)
|
||||||
self.font.render(
|
self.font.render(
|
||||||
|
|
|
@ -609,6 +609,8 @@ font_getsize(FontObject* self, PyObject* args)
|
||||||
FT_Face face;
|
FT_Face face;
|
||||||
int xoffset, yoffset;
|
int xoffset, yoffset;
|
||||||
int horizontal_dir;
|
int horizontal_dir;
|
||||||
|
int mask = 0;
|
||||||
|
int load_flags;
|
||||||
const char *dir = NULL;
|
const char *dir = NULL;
|
||||||
const char *lang = NULL;
|
const char *lang = NULL;
|
||||||
size_t i, count;
|
size_t i, count;
|
||||||
|
@ -618,11 +620,11 @@ font_getsize(FontObject* self, PyObject* args)
|
||||||
/* calculate size and bearing for a given string */
|
/* calculate size and bearing for a given string */
|
||||||
|
|
||||||
PyObject* string;
|
PyObject* string;
|
||||||
if (!PyArg_ParseTuple(args, "O|zOz:getsize", &string, &dir, &features, &lang)) {
|
if (!PyArg_ParseTuple(args, "O|izOz:getsize", &string, &mask, &dir, &features, &lang)) {
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
count = text_layout(string, self, dir, features, lang, &glyph_info, 0);
|
count = text_layout(string, self, dir, features, lang, &glyph_info, mask);
|
||||||
if (PyErr_Occurred()) {
|
if (PyErr_Occurred()) {
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
@ -641,7 +643,11 @@ font_getsize(FontObject* self, PyObject* args)
|
||||||
/* Note: bitmap fonts within ttf fonts do not work, see #891/pr#960
|
/* Note: bitmap fonts within ttf fonts do not work, see #891/pr#960
|
||||||
* Yifu Yu<root@jackyyf.com>, 2014-10-15
|
* Yifu Yu<root@jackyyf.com>, 2014-10-15
|
||||||
*/
|
*/
|
||||||
error = FT_Load_Glyph(face, index, FT_LOAD_DEFAULT|FT_LOAD_NO_BITMAP);
|
load_flags = FT_LOAD_NO_BITMAP;
|
||||||
|
if (mask) {
|
||||||
|
load_flags |= FT_LOAD_TARGET_MONO;
|
||||||
|
}
|
||||||
|
error = FT_Load_Glyph(face, index, load_flags);
|
||||||
if (error) {
|
if (error) {
|
||||||
return geterror(error);
|
return geterror(error);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user