mirror of
https://github.com/python-pillow/Pillow.git
synced 2025-07-04 11:53:32 +03:00
add getbbox and getlength to TransposedFont with tests
This commit is contained in:
parent
e2158344a0
commit
93acbcf9e2
|
@ -315,16 +315,31 @@ class TestImageFont:
|
||||||
|
|
||||||
# Original font
|
# Original font
|
||||||
draw.font = font
|
draw.font = font
|
||||||
box_size_a = draw.textsize(word)
|
with pytest.warns(DeprecationWarning) as log:
|
||||||
|
box_size_a = draw.textsize(word)
|
||||||
|
assert len(log) == 1
|
||||||
|
bbox_a = draw.textbbox((10, 10), word)
|
||||||
|
|
||||||
# Rotated font
|
# Rotated font
|
||||||
draw.font = transposed_font
|
draw.font = transposed_font
|
||||||
box_size_b = draw.textsize(word)
|
with pytest.warns(DeprecationWarning) as log:
|
||||||
|
box_size_b = draw.textsize(word)
|
||||||
|
assert len(log) == 1
|
||||||
|
bbox_b = draw.textbbox((20, 20), word)
|
||||||
|
|
||||||
# Check (w,h) of box a is (h,w) of box b
|
# Check (w,h) of box a is (h,w) of box b
|
||||||
assert box_size_a[0] == box_size_b[1]
|
assert box_size_a[0] == box_size_b[1]
|
||||||
assert box_size_a[1] == box_size_b[0]
|
assert box_size_a[1] == box_size_b[0]
|
||||||
|
|
||||||
|
# Check bbox b is (20, 20, 20 + h, 20 + w)
|
||||||
|
assert bbox_b[0] == 20
|
||||||
|
assert bbox_b[1] == 20
|
||||||
|
assert bbox_b[2] == 20 + bbox_a[3] - bbox_a[1]
|
||||||
|
assert bbox_b[3] == 20 + bbox_a[2] - bbox_a[0]
|
||||||
|
|
||||||
|
# text length is undefined for vertical text
|
||||||
|
pytest.raises(ValueError, draw.textlength, word)
|
||||||
|
|
||||||
def test_unrotated_transposed_font(self):
|
def test_unrotated_transposed_font(self):
|
||||||
img_grey = Image.new("L", (100, 100))
|
img_grey = Image.new("L", (100, 100))
|
||||||
draw = ImageDraw.Draw(img_grey)
|
draw = ImageDraw.Draw(img_grey)
|
||||||
|
@ -336,15 +351,31 @@ class TestImageFont:
|
||||||
|
|
||||||
# Original font
|
# Original font
|
||||||
draw.font = font
|
draw.font = font
|
||||||
box_size_a = draw.textsize(word)
|
with pytest.warns(DeprecationWarning) as log:
|
||||||
|
box_size_a = draw.textsize(word)
|
||||||
|
assert len(log) == 1
|
||||||
|
bbox_a = draw.textbbox((10, 10), word)
|
||||||
|
length_a = draw.textlength(word)
|
||||||
|
|
||||||
# Rotated font
|
# Rotated font
|
||||||
draw.font = transposed_font
|
draw.font = transposed_font
|
||||||
box_size_b = draw.textsize(word)
|
with pytest.warns(DeprecationWarning) as log:
|
||||||
|
box_size_b = draw.textsize(word)
|
||||||
|
assert len(log) == 1
|
||||||
|
bbox_b = draw.textbbox((20, 20), word)
|
||||||
|
length_b = draw.textlength(word)
|
||||||
|
|
||||||
# Check boxes a and b are same size
|
# Check boxes a and b are same size
|
||||||
assert box_size_a == box_size_b
|
assert box_size_a == box_size_b
|
||||||
|
|
||||||
|
# Check bbox b is (20, 20, 20 + w, 20 + h)
|
||||||
|
assert bbox_b[0] == 20
|
||||||
|
assert bbox_b[1] == 20
|
||||||
|
assert bbox_b[2] == 20 + bbox_a[2] - bbox_a[0]
|
||||||
|
assert bbox_b[3] == 20 + bbox_a[3] - bbox_a[1]
|
||||||
|
|
||||||
|
assert length_a == length_b
|
||||||
|
|
||||||
def test_rotated_transposed_font_get_mask(self):
|
def test_rotated_transposed_font_get_mask(self):
|
||||||
# Arrange
|
# Arrange
|
||||||
text = "mask this"
|
text = "mask this"
|
||||||
|
|
|
@ -853,6 +853,21 @@ class TransposedFont:
|
||||||
return im.transpose(self.orientation)
|
return im.transpose(self.orientation)
|
||||||
return im
|
return im
|
||||||
|
|
||||||
|
def getbbox(self, text, *args, **kwargs):
|
||||||
|
# TransposedFont doesn't support getmask2, move top-left point to (0, 0)
|
||||||
|
# this has no effect on ImageFont and simulates anchor="lt" for FreeTypeFont
|
||||||
|
left, top, right, bottom = self.font.getbbox(text, *args, **kwargs)
|
||||||
|
width = right - left
|
||||||
|
height = bottom - top
|
||||||
|
if self.orientation in (Image.Transpose.ROTATE_90, Image.Transpose.ROTATE_270):
|
||||||
|
return 0, 0, height, width
|
||||||
|
return 0, 0, width, height
|
||||||
|
|
||||||
|
def getlength(self, text, *args, **kwargs):
|
||||||
|
if self.orientation in (Image.Transpose.ROTATE_90, Image.Transpose.ROTATE_270):
|
||||||
|
raise ValueError("text length is undefined for rotated text")
|
||||||
|
return self.font.getlength(text, *args, **kwargs)
|
||||||
|
|
||||||
|
|
||||||
def load(filename):
|
def load(filename):
|
||||||
"""
|
"""
|
||||||
|
|
Loading…
Reference in New Issue
Block a user