"""A free proof is a 15 cm window around the measured fine stroke, not the top.""" import io import tempfile import unittest from pathlib import Path from PIL import Image, ImageDraw from app.printpng import render PX_CM = 300 / 2.54 def item(height_cm=30, fraction='0.75'): return { 'mode': 'file', 'metres': '10.5', 'billed_metres': '0', 'production': { 'version': 3, 'film_width_cm': 20, 'height_cm': height_cm, 'sources': [{'upload_id': '00000000-0000-0000-0000-000000000001', 'kind': 'sheet', 'width_cm': 20, 'length_cm': height_cm, 'copies': 1, 'rotation_degrees': 0, 'mirrored': False, 'measurement': 'file'}], 'placements': [{'source_index': 0, 'copy_index': 0, 'x_cm': 0, 'y_cm': 0, 'width_cm': 20, 'length_cm': height_cm, 'rotation_degrees': 0, 'mirrored': False}], }, 'proof_parent_item_index': 0, 'proof_y_fraction': fraction, } class ProofRenderTests(unittest.TestCase): def _sheet(self, folder): height = round(30 * PX_CM) width = round(20 * PX_CM) image = Image.new('RGBA', (width, height), (220, 30, 60, 255)) ImageDraw.Draw(image).rectangle((0, height // 2, width - 1, height - 1), fill=(30, 60, 220, 255)) path = Path(folder) / 'sheet.png' image.save(path, 'PNG', dpi=(300, 300)) return str(path) def test_the_window_follows_the_finest_stroke(self): with tempfile.TemporaryDirectory() as source, tempfile.TemporaryDirectory() as out: sheet = self._sheet(source) parts, detail = render(item(), {0: (sheet, 'sheet.png')}, out, 'proof', limit_cm=15, proof_fraction='0.75') self.assertEqual(len(parts), 1) self.assertEqual(detail['parts'][0]['from_cm'], '15') with Image.open(parts[0][0]) as proof: self.assertEqual(proof.width, round(20 * PX_CM)) self.assertLessEqual(abs(proof.height - round(15 * PX_CM)), 1) # The bottom half is blue: the window covered 15 to 30 cm. self.assertEqual(proof.getpixel((proof.width // 2, round(5 * PX_CM))), (30, 60, 220, 255)) def test_the_top_is_still_the_default_without_a_measure(self): with tempfile.TemporaryDirectory() as source, tempfile.TemporaryDirectory() as out: sheet = self._sheet(source) parts, detail = render(item(fraction=None), {0: (sheet, 'sheet.png')}, out, 'proof', limit_cm=15) self.assertEqual(detail['parts'][0]['from_cm'], '0') with Image.open(parts[0][0]) as proof: self.assertEqual(proof.getpixel((proof.width // 2, round(5 * PX_CM))), (220, 30, 60, 255)) if __name__ == '__main__': unittest.main()