mirror of
https://github.com/python-pillow/Pillow.git
synced 2025-01-27 17:54:32 +03:00
Merge pull request #6484 from radarhere/imagedraw_font
This commit is contained in:
commit
87ecd01fc0
|
@ -1314,6 +1314,23 @@ def test_stroke_multiline():
|
||||||
assert_image_similar_tofile(im, "Tests/images/imagedraw_stroke_multiline.png", 3.3)
|
assert_image_similar_tofile(im, "Tests/images/imagedraw_stroke_multiline.png", 3.3)
|
||||||
|
|
||||||
|
|
||||||
|
def test_setting_default_font():
|
||||||
|
# Arrange
|
||||||
|
im = Image.new("RGB", (100, 250))
|
||||||
|
draw = ImageDraw.Draw(im)
|
||||||
|
font = ImageFont.truetype("Tests/fonts/FreeMono.ttf", 120)
|
||||||
|
|
||||||
|
# Act
|
||||||
|
ImageDraw.ImageDraw.font = font
|
||||||
|
|
||||||
|
# Assert
|
||||||
|
try:
|
||||||
|
assert draw.getfont() == font
|
||||||
|
finally:
|
||||||
|
ImageDraw.ImageDraw.font = None
|
||||||
|
assert isinstance(draw.getfont(), ImageFont.ImageFont)
|
||||||
|
|
||||||
|
|
||||||
def test_same_color_outline():
|
def test_same_color_outline():
|
||||||
# Prepare shape
|
# Prepare shape
|
||||||
x0, y0 = 5, 5
|
x0, y0 = 5, 5
|
||||||
|
|
|
@ -64,7 +64,7 @@ Fonts
|
||||||
|
|
||||||
PIL can use bitmap fonts or OpenType/TrueType fonts.
|
PIL can use bitmap fonts or OpenType/TrueType fonts.
|
||||||
|
|
||||||
Bitmap fonts are stored in PIL’s own format, where each font typically consists
|
Bitmap fonts are stored in PIL's own format, where each font typically consists
|
||||||
of two files, one named .pil and the other usually named .pbm. The former
|
of two files, one named .pil and the other usually named .pbm. The former
|
||||||
contains font metrics, the latter raster data.
|
contains font metrics, the latter raster data.
|
||||||
|
|
||||||
|
@ -146,6 +146,11 @@ Methods
|
||||||
|
|
||||||
Get the current default font.
|
Get the current default font.
|
||||||
|
|
||||||
|
To set the default font for all future ImageDraw instances::
|
||||||
|
|
||||||
|
from PIL import ImageDraw, ImageFont
|
||||||
|
ImageDraw.ImageDraw.font = ImageFont.truetype("Tests/fonts/FreeMono.ttf")
|
||||||
|
|
||||||
:returns: An image font.
|
:returns: An image font.
|
||||||
|
|
||||||
.. py:method:: ImageDraw.arc(xy, start, end, fill=None, width=0)
|
.. py:method:: ImageDraw.arc(xy, start, end, fill=None, width=0)
|
||||||
|
|
|
@ -26,6 +26,16 @@ TODO
|
||||||
API Additions
|
API Additions
|
||||||
=============
|
=============
|
||||||
|
|
||||||
|
Allow default ImageDraw font to be set
|
||||||
|
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||||
|
|
||||||
|
Rather than specifying a font when calling text-related ImageDraw methods, or
|
||||||
|
setting a font on each ImageDraw instance, the default font can now be set for
|
||||||
|
all future ImageDraw operations::
|
||||||
|
|
||||||
|
from PIL import ImageDraw, ImageFont
|
||||||
|
ImageDraw.ImageDraw.font = ImageFont.truetype("Tests/fonts/FreeMono.ttf")
|
||||||
|
|
||||||
Saving multiple MPO frames
|
Saving multiple MPO frames
|
||||||
^^^^^^^^^^^^^^^^^^^^^^^^^^
|
^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||||
|
|
||||||
|
|
|
@ -46,6 +46,8 @@ directly.
|
||||||
|
|
||||||
|
|
||||||
class ImageDraw:
|
class ImageDraw:
|
||||||
|
font = None
|
||||||
|
|
||||||
def __init__(self, im, mode=None):
|
def __init__(self, im, mode=None):
|
||||||
"""
|
"""
|
||||||
Create a drawing instance.
|
Create a drawing instance.
|
||||||
|
@ -86,12 +88,16 @@ class ImageDraw:
|
||||||
else:
|
else:
|
||||||
self.fontmode = "L" # aliasing is okay for other modes
|
self.fontmode = "L" # aliasing is okay for other modes
|
||||||
self.fill = 0
|
self.fill = 0
|
||||||
self.font = None
|
|
||||||
|
|
||||||
def getfont(self):
|
def getfont(self):
|
||||||
"""
|
"""
|
||||||
Get the current default font.
|
Get the current default font.
|
||||||
|
|
||||||
|
To set the default font for all future ImageDraw instances::
|
||||||
|
|
||||||
|
from PIL import ImageDraw, ImageFont
|
||||||
|
ImageDraw.ImageDraw.font = ImageFont.truetype("Tests/fonts/FreeMono.ttf")
|
||||||
|
|
||||||
:returns: An image font."""
|
:returns: An image font."""
|
||||||
if not self.font:
|
if not self.font:
|
||||||
# FIXME: should add a font repository
|
# FIXME: should add a font repository
|
||||||
|
|
Loading…
Reference in New Issue
Block a user