Accept float stroke widths

This commit is contained in:
Andrew Murray 2024-09-11 19:53:52 +10:00
parent cb2a0c40f3
commit 01ba1e2252
3 changed files with 23 additions and 9 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.5 KiB

View File

@ -1368,6 +1368,20 @@ def test_stroke() -> None:
)
@skip_unless_feature("freetype2")
def test_stroke_float() -> None:
# Arrange
im = Image.new("RGB", (120, 130))
draw = ImageDraw.Draw(im)
font = ImageFont.truetype("Tests/fonts/FreeMono.ttf", 120)
# Act
draw.text((12, 12), "A", "#f00", font, stroke_width=0.5)
# Assert
assert_image_similar_tofile(im, "Tests/images/imagedraw_stroke_float.png", 3.1)
@skip_unless_feature("freetype2")
def test_stroke_descender() -> None:
# Arrange

View File

@ -833,7 +833,7 @@ font_render(FontObject *self, PyObject *args) {
Py_ssize_t id;
int mask = 0; /* is FT_LOAD_TARGET_MONO enabled? */
int color = 0; /* is FT_LOAD_COLOR enabled? */
int stroke_width = 0;
float stroke_width = 0;
PY_LONG_LONG foreground_ink_long = 0;
unsigned int foreground_ink;
const char *mode = NULL;
@ -853,7 +853,7 @@ font_render(FontObject *self, PyObject *args) {
if (!PyArg_ParseTuple(
args,
"OO|zzOzizLffO:render",
"OO|zzOzfzLffO:render",
&string,
&fill,
&mode,
@ -919,8 +919,8 @@ font_render(FontObject *self, PyObject *args) {
return NULL;
}
width += stroke_width * 2 + ceil(x_start);
height += stroke_width * 2 + ceil(y_start);
width += ceil(stroke_width * 2 + x_start);
height += ceil(stroke_width * 2 + y_start);
image = PyObject_CallFunction(fill, "ii", width, height);
if (image == Py_None) {
PyMem_Del(glyph_info);
@ -934,8 +934,8 @@ font_render(FontObject *self, PyObject *args) {
Py_XDECREF(imageId);
im = (Imaging)id;
x_offset -= stroke_width;
y_offset -= stroke_width;
x_offset = round(x_offset - stroke_width);
y_offset = round(y_offset - stroke_width);
if (count == 0 || width == 0 || height == 0) {
PyMem_Del(glyph_info);
return Py_BuildValue("N(ii)", image, x_offset, y_offset);
@ -950,7 +950,7 @@ font_render(FontObject *self, PyObject *args) {
FT_Stroker_Set(
stroker,
(FT_Fixed)stroke_width * 64,
(FT_Fixed)round(stroke_width * 64),
FT_STROKER_LINECAP_ROUND,
FT_STROKER_LINEJOIN_ROUND,
0
@ -988,8 +988,8 @@ font_render(FontObject *self, PyObject *args) {
}
/* set pen position to text origin */
x = (-x_min + stroke_width + x_start) * 64;
y = (-y_max + (-stroke_width) - y_start) * 64;
x = round((-x_min + stroke_width + x_start) * 64);
y = round((-y_max + (-stroke_width) - y_start) * 64);
if (stroker == NULL) {
load_flags |= FT_LOAD_RENDER;