Pillow/docs/reference/ImageFont.rst

94 lines
2.6 KiB
ReStructuredText
Raw Normal View History

2013-10-13 04:14:12 +04:00
.. py:module:: PIL.ImageFont
.. py:currentmodule:: PIL.ImageFont
:py:mod:`~PIL.ImageFont` Module
===============================
2013-10-13 04:14:12 +04:00
The :py:mod:`~PIL.ImageFont` module defines a class with the same name. Instances of
2013-10-13 04:14:12 +04:00
this class store bitmap fonts, and are used with the
2020-07-09 20:48:04 +03:00
:py:meth:`PIL.ImageDraw.ImageDraw.text` method.
2013-10-13 04:14:12 +04:00
PIL uses its own font file format to store bitmap fonts, limited to 256 characters. You can use
2021-10-15 17:30:05 +03:00
`pilfont.py <https://github.com/python-pillow/pillow-scripts/blob/main/Scripts/pilfont.py>`_
2023-10-30 09:56:42 +03:00
from :pypi:`pillow-scripts` to convert BDF and
PCF font descriptors (X window font formats) to this format.
2013-10-13 04:14:12 +04:00
Starting with version 1.1.4, PIL can be configured to support TrueType and
OpenType fonts (as well as other font formats supported by the FreeType
library). For earlier versions, TrueType support is only available as part of
the imToolkit package.
2013-10-13 04:14:12 +04:00
2023-06-30 16:32:26 +03:00
.. warning::
To protect against potential DOS attacks when using arbitrary strings as
2023-11-01 21:18:25 +03:00
text input, Pillow will raise a :py:exc:`ValueError` if the number of characters
2023-06-30 16:32:26 +03:00
is over a certain limit, :py:data:`MAX_STRING_LENGTH`.
This threshold can be changed by setting
:py:data:`MAX_STRING_LENGTH`. It can be disabled by setting
``ImageFont.MAX_STRING_LENGTH = None``.
2013-10-13 04:14:12 +04:00
Example
-------
::
2013-10-13 04:14:12 +04:00
from PIL import ImageFont, ImageDraw
draw = ImageDraw.Draw(image)
# use a bitmap font
font = ImageFont.load("arial.pil")
draw.text((10, 10), "hello", font=font)
# use a truetype font
font = ImageFont.truetype("arial.ttf", 15)
draw.text((10, 25), "world", font=font)
Functions
---------
.. autofunction:: PIL.ImageFont.load
.. autofunction:: PIL.ImageFont.load_path
.. autofunction:: PIL.ImageFont.truetype
.. autofunction:: PIL.ImageFont.load_default
Methods
-------
2019-07-07 13:52:30 +03:00
.. autoclass:: PIL.ImageFont.ImageFont
:members:
2019-04-21 16:27:31 +03:00
.. autoclass:: PIL.ImageFont.FreeTypeFont
2019-04-21 16:28:08 +03:00
:members:
2019-07-07 13:52:30 +03:00
.. autoclass:: PIL.ImageFont.TransposedFont
:members:
2022-06-30 14:58:47 +03:00
:undoc-members:
2020-08-11 03:12:26 +03:00
Constants
---------
2022-01-15 01:02:31 +03:00
.. data:: PIL.ImageFont.Layout.BASIC
2020-08-11 03:12:26 +03:00
Use basic text layout for TrueType font.
Advanced features such as text direction are not supported.
2022-01-15 01:02:31 +03:00
.. data:: PIL.ImageFont.Layout.RAQM
2020-08-11 03:12:26 +03:00
Use Raqm text layout for TrueType font.
Advanced features are supported.
Requires Raqm, you can check support using
:py:func:`PIL.features.check_feature` with ``feature="raqm"``.
2023-06-30 16:32:26 +03:00
Constants
---------
.. data:: MAX_STRING_LENGTH
Set to 1,000,000, to protect against potential DOS attacks. Pillow will
2023-11-01 21:18:25 +03:00
raise a :py:exc:`ValueError` if the number of characters is over this limit. The
2023-06-30 16:32:26 +03:00
check can be disabled by setting ``ImageFont.MAX_STRING_LENGTH = None``.