2023-12-21 14:13:31 +03:00
|
|
|
from __future__ import annotations
|
2024-01-20 14:23:03 +03:00
|
|
|
|
2015-07-31 13:59:59 +03:00
|
|
|
import os
|
|
|
|
import sys
|
2021-04-25 08:33:36 +03:00
|
|
|
from io import BytesIO
|
2014-12-11 14:06:53 +03:00
|
|
|
|
2021-05-03 11:07:05 +03:00
|
|
|
import pytest
|
|
|
|
|
2019-07-06 23:40:53 +03:00
|
|
|
from PIL import Image, PSDraw
|
|
|
|
|
|
|
|
|
2020-01-27 14:46:52 +03:00
|
|
|
def _create_document(ps):
|
|
|
|
title = "hopper"
|
|
|
|
box = (1 * 72, 2 * 72, 7 * 72, 10 * 72) # in points
|
2014-12-11 14:06:53 +03:00
|
|
|
|
2020-01-27 14:46:52 +03:00
|
|
|
ps.begin_document(title)
|
2014-12-11 14:06:53 +03:00
|
|
|
|
2020-01-27 14:46:52 +03:00
|
|
|
# draw diagonal lines in a cross
|
|
|
|
ps.line((1 * 72, 2 * 72), (7 * 72, 10 * 72))
|
|
|
|
ps.line((7 * 72, 2 * 72), (1 * 72, 10 * 72))
|
2014-12-11 14:06:53 +03:00
|
|
|
|
2020-01-27 14:46:52 +03:00
|
|
|
# draw the image (75 dpi)
|
|
|
|
with Image.open("Tests/images/hopper.ppm") as im:
|
|
|
|
ps.image(box, im, 75)
|
|
|
|
ps.rectangle(box)
|
2014-12-27 23:50:17 +03:00
|
|
|
|
2020-01-27 14:46:52 +03:00
|
|
|
# draw title
|
|
|
|
ps.setfont("Courier", 36)
|
|
|
|
ps.text((3 * 72, 4 * 72), title)
|
2014-12-11 14:06:53 +03:00
|
|
|
|
2020-01-27 14:46:52 +03:00
|
|
|
ps.end_document()
|
2014-12-11 14:06:53 +03:00
|
|
|
|
2015-07-31 13:59:59 +03:00
|
|
|
|
2020-01-27 14:46:52 +03:00
|
|
|
def test_draw_postscript(tmp_path):
|
|
|
|
# Based on Pillow tutorial, but there is no textsize:
|
|
|
|
# https://pillow.readthedocs.io/en/latest/handbook/tutorial.html#drawing-postscript
|
2015-07-31 13:59:59 +03:00
|
|
|
|
2020-01-27 14:46:52 +03:00
|
|
|
# Arrange
|
|
|
|
tempfile = str(tmp_path / "temp.ps")
|
|
|
|
with open(tempfile, "wb") as fp:
|
|
|
|
# Act
|
|
|
|
ps = PSDraw.PSDraw(fp)
|
|
|
|
_create_document(ps)
|
2015-07-31 13:59:59 +03:00
|
|
|
|
2020-01-27 14:46:52 +03:00
|
|
|
# Assert
|
|
|
|
# Check non-zero file was created
|
|
|
|
assert os.path.isfile(tempfile)
|
|
|
|
assert os.path.getsize(tempfile) > 0
|
2014-12-11 14:06:53 +03:00
|
|
|
|
|
|
|
|
2021-05-03 11:07:05 +03:00
|
|
|
@pytest.mark.parametrize("buffer", (True, False))
|
|
|
|
def test_stdout(buffer):
|
2020-01-27 14:46:52 +03:00
|
|
|
# Temporarily redirect stdout
|
2021-05-07 13:44:46 +03:00
|
|
|
old_stdout = sys.stdout
|
2021-04-25 08:33:36 +03:00
|
|
|
|
2021-05-03 11:07:05 +03:00
|
|
|
if buffer:
|
|
|
|
|
|
|
|
class MyStdOut:
|
|
|
|
buffer = BytesIO()
|
|
|
|
|
|
|
|
mystdout = MyStdOut()
|
|
|
|
else:
|
|
|
|
mystdout = BytesIO()
|
2021-04-25 08:33:36 +03:00
|
|
|
|
2021-05-03 11:07:05 +03:00
|
|
|
sys.stdout = mystdout
|
2015-07-31 13:59:59 +03:00
|
|
|
|
2020-01-27 14:46:52 +03:00
|
|
|
ps = PSDraw.PSDraw()
|
|
|
|
_create_document(ps)
|
2015-07-31 13:59:59 +03:00
|
|
|
|
2020-01-27 14:46:52 +03:00
|
|
|
# Reset stdout
|
|
|
|
sys.stdout = old_stdout
|
2015-07-31 13:59:59 +03:00
|
|
|
|
2021-05-03 11:07:05 +03:00
|
|
|
if buffer:
|
|
|
|
mystdout = mystdout.buffer
|
|
|
|
assert mystdout.getvalue() != b""
|