Skip to content

Commit

Permalink
Accept float stroke widths
Browse files Browse the repository at this point in the history
  • Loading branch information
radarhere committed Sep 11, 2024
1 parent cb2a0c4 commit 01ba1e2
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 9 deletions.
Binary file added Tests/images/imagedraw_stroke_float.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
14 changes: 14 additions & 0 deletions Tests/test_imagedraw.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 9 additions & 9 deletions src/_imagingft.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -853,7 +853,7 @@ font_render(FontObject *self, PyObject *args) {

if (!PyArg_ParseTuple(
args,
"OO|zzOzizLffO:render",
"OO|zzOzfzLffO:render",
&string,
&fill,
&mode,
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 01ba1e2

Please sign in to comment.