Skip to content

Commit 05cb813

Browse files
committed
Improved font outline docs.
Specify that the thickness is in pixels, and added example to render outlined text.
1 parent 0cd38a8 commit 05cb813

File tree

2 files changed

+26
-5
lines changed

2 files changed

+26
-5
lines changed

docs/reST/ref/font.rst

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -309,15 +309,36 @@ solves no longer exists, it will likely be removed in the future.
309309
310310
.. attribute:: outline
311311

312-
| :sl:`Gets or sets the font's outline value`
312+
| :sl:`Gets or sets the font's outline thickness (pixels)`
313313
| :sg:`outline -> int`
314314
315315
The outline value of the font.
316316

317317
When set to 0, the font will be drawn normally. When positive,
318-
the text will be drawn as a hollow outline. This can be drawn
319-
underneath unoutlined text to create a text outline effect. Larger values
320-
produce a thicker outline. Negative values are not valid.
318+
the text will be drawn as a hollow outline. The outline grows in all
319+
directions a number of pixels equal to the value set. Negative values
320+
are not allowed.
321+
322+
This can be drawn underneath unoutlined text to create a text outline
323+
effect. For example: ::
324+
325+
def render_outlined(
326+
font: pygame.Font,
327+
text: str,
328+
text_color: pygame.typing.ColorLike,
329+
outline_color: pygame.typing.ColorLike,
330+
outline_width: int,
331+
) -> pygame.Surface:
332+
old_outline = font.outline
333+
if old_outline != 0:
334+
font.outline = 0
335+
base_text_surf = font.render(text, True, text_color)
336+
font.outline = outline_width
337+
outlined_text_surf = font.render(text, True, outline_color)
338+
339+
outlined_text_surf.blit(base_text_surf, (outline_width, outline_width))
340+
font.outline = old_outline
341+
return outlined_text_surf
321342

322343
.. versionadded:: 2.5.6
323344

src_c/doc/font_doc.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
#define DOC_FONT_FONT_STRIKETHROUGH "strikethrough -> bool\nGets or sets whether the font should be rendered with a strikethrough."
1818
#define DOC_FONT_FONT_ALIGN "align -> int\nSet how rendered text is aligned when given a wrap length."
1919
#define DOC_FONT_FONT_POINTSIZE "point_size -> int\nGets or sets the font's point size"
20-
#define DOC_FONT_FONT_OUTLINE "outline -> int\nGets or sets the font's outline value"
20+
#define DOC_FONT_FONT_OUTLINE "outline -> int\nGets or sets the font's outline thickness (pixels)"
2121
#define DOC_FONT_FONT_RENDER "render(text, antialias, color, bgcolor=None, wraplength=0) -> Surface\ndraw text on a new Surface"
2222
#define DOC_FONT_FONT_SIZE "size(text, /) -> (width, height)\ndetermine the amount of space needed to render text"
2323
#define DOC_FONT_FONT_SETUNDERLINE "set_underline(bool, /) -> None\ncontrol if text is rendered with an underline"

0 commit comments

Comments
 (0)