From 7af6c9797dc8d54fd44f6d1cdab87a63667a2d8d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cau=C3=AA=20Faleiros?= Date: Wed, 2 Dec 2026 09:09:25 -0300 Subject: [PATCH] fix: loose art nested by shape passes checkout again, and ink on ink goes to hand preparation (DTF-WEB-2B) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Since 27/11 (bb6fb14) the checkout refused any crossing rectangles. The Site nests loose art by its shape (Misturado, and Automático when it saves film), so rectangles cross where the art is clear, and honest carts were refused with "Duas peças da montagem ficaram uma sobre a outra". The print file composes with alpha_composite, so those layouts print right. Loose art: two pieces in one place are still refused (the stacked-copies price trick bb6fb14 closed); partial crossing passes. Ready sheets: any crossing is still refused, sheets are never nested. When the film is composed, ink landing on ink (alpha > 10 on the same pixel) raises Unsupported, so the item goes to hand preparation with the reason instead of printing over itself. Contract: output/revisao/contrato-encaixe-forma.md. Co-Authored-By: Claude Opus 5.5 --- app/core/models.py | 39 ++++++++++++++++++------- app/printpng.py | 19 ++++++++++-- tests/test_printpng.py | 65 +++++++++++++++++++++++++++++++++++++++++ tests/test_sheet_gap.py | 17 ++++++++--- 4 files changed, 123 insertions(+), 17 deletions(-) diff --git a/app/core/models.py b/app/core/models.py index 5d0c91b..70655ad 100644 --- a/app/core/models.py +++ b/app/core/models.py @@ -295,17 +295,34 @@ class Item(StrictModel): raise ValueError('Finished sheets must retain their original orientation') if placed != expected: raise ValueError('Production layout does not cover every source copy') - # No piece on top of another: stacked pieces bill less film than they - # print on, and print over each other. Swept down the film, so only - # the pieces still open at each row are compared. - boxes = sorted((p.y_cm, p.y_cm + p.length_cm, p.x_cm, p.x_cm + p.width_cm) - for p in self.production.placements) - open_boxes = [] - for top, bottom, left, right in boxes: - open_boxes = [b for b in open_boxes if b[0] > top + tolerance] - if any(l < right - tolerance and left < r - tolerance for _, l, r in open_boxes): - raise ValueError('Duas peças da montagem ficaram uma sobre a outra. Monte o pedido de novo.') - open_boxes.append((bottom, left, right)) + # Stacked pieces bill less film than they print on (bb6fb14). + # output/revisao/contrato-encaixe-forma.md. + stacked = 'Duas peças da montagem ficaram uma sobre a outra. Monte o pedido de novo.' + if is_sheet: + # Ready sheets go one under the other, never nested: no crossing + # at all. Swept down the film, so only the pieces still open at + # each row are compared. + boxes = sorted((p.y_cm, p.y_cm + p.length_cm, p.x_cm, p.x_cm + p.width_cm) + for p in self.production.placements) + open_boxes = [] + for top, bottom, left, right in boxes: + open_boxes = [b for b in open_boxes if b[0] > top + tolerance] + if any(l < right - tolerance and left < r - tolerance for _, l, r in open_boxes): + raise ValueError(stacked) + open_boxes.append((bottom, left, right)) + else: + # Loose art may be nested by its shape: rectangles cross where it + # is clear, and the print file composes transparency (Sentry + # DTF-WEB-2B). Two pieces in one place are refused here; ink that + # would land on ink is refused when the film is composed + # (app/printpng.py film_bands). + step = Decimal('0.01') + boxes = set() + for p in self.production.placements: + box = tuple(v.quantize(step) for v in (p.x_cm, p.y_cm, p.width_cm, p.length_cm)) + if box in boxes: + raise ValueError(stacked) + boxes.add(box) if self.quality_status == 'warning' and not self.quality_acknowledged: raise ValueError('Resolution warning must be acknowledged') return self diff --git a/app/printpng.py b/app/printpng.py index e8f1770..88fbe47 100644 --- a/app/printpng.py +++ b/app/printpng.py @@ -33,7 +33,7 @@ import struct import zlib from decimal import Decimal -from PIL import Image, ImageDraw, ImageFont, ImageOps +from PIL import Image, ImageChops, ImageDraw, ImageFont, ImageOps from .core.models import PRODUCTION_VERSIONS from .printfile import (FOOTER_CM, FOOTER_LOGO, FOOTER_LOGO_CM, MAX_DECODED_PIXELS, SUPPORTED_FORMATS, @@ -730,6 +730,18 @@ def render(item, files, out_dir, prefix, footer=None, part_cm=None, tiles=False, 'min_dpi': round(lowest) if lowest else None} +def ink_meets_ink(band, rows, at): + """Whether `rows`, put at `at` on `band`, would lay ink (alpha above + INK_ALPHA) where the band already has ink. Pieces nested by shape cross + rectangles only where one of them is clear (contrato-encaixe-forma.md P2).""" + x, y = at + under = band.crop((x, y, x + rows.width, y + rows.height)).getchannel('A') + if not under.getbbox(): + return False + ink = lambda image: image.point(lambda a: 255 if a > INK_ALPHA else 0) + return ImageChops.multiply(ink(under), ink(rows.getchannel('A'))).getbbox() is not None + + def film_bands(pieces, top, bottom, width): """The film's rows [top, bottom) composed, BAND_ROWS at a time: (band top, RGBA image). Pieces are read forward only, so ranges go down the film.""" @@ -747,7 +759,10 @@ def film_bands(pieces, top, bottom, width): left = max(0, -piece.x) if left or piece.x + rows.width > width: rows = rows.crop((left, 0, min(rows.width, width - piece.x), rows.height)) - band.alpha_composite(rows, (max(0, piece.x), a - band_top)) + at = (max(0, piece.x), a - band_top) + if ink_meets_ink(band, rows, at): + raise Unsupported('duas peças da montagem têm tinta no mesmo lugar') + band.alpha_composite(rows, at) yield band_top, band # A piece the film has passed is done with: its pixels can go. for piece in pieces: diff --git a/tests/test_printpng.py b/tests/test_printpng.py index 843afe1..211997c 100644 --- a/tests/test_printpng.py +++ b/tests/test_printpng.py @@ -226,5 +226,70 @@ class PngRenderTests(unittest.TestCase): self.assertEqual(detail['vector_sources'], 1) +def half_inked(color, side): + """1 x 1 cm at 300 DPI (118 x 118 px) with ink on one half only.""" + image = Image.new('RGBA', (118, 118)) + box = (0, 0, 59, 118) if side == 'left' else (59, 0, 118, 118) + image.paste(color + (255,), box) + return image + + +class InkOverlapTests(unittest.TestCase): + """Pieces nested by shape cross rectangles where one is clear; ink on ink + is refused when the film is composed (contrato-encaixe-forma.md P1-P3).""" + + def setUp(self): + self.dir = tempfile.TemporaryDirectory() + self.addCleanup(self.dir.cleanup) + + def save(self, image, name): + path = os.path.join(self.dir.name, name) + image.save(path) + return path + + def render(self, spec, paths): + out = os.path.join(self.dir.name, 'out') + os.makedirs(out, exist_ok=True) + files = {i: (path, os.path.basename(path)) for i, path in enumerate(paths)} + return render(spec, files, out, 'DTF-7-item-1') + + def crossing(self, first, second): + spec = item([placement(0, 0, 1, 1), placement(0.5, 0, 1, 1, index=1)], + [source(1, 1), source(1, 1)], 1) + return self.render(spec, [self.save(first, 'first.png'), self.save(second, 'second.png')]) + + def test_ink_crossing_where_one_is_clear_prints_both_whole(self): + parts, _ = self.crossing(half_inked(RED, 'left'), half_inked(GREEN, 'right')) + film = Image.open(parts[0][0]).convert('RGBA') + for x, y in ((10, 10), (10, 59), (50, 100)): # the first piece's left half + self.assertEqual(film.getpixel((x, y))[:3], RED, (x, y)) + start = px(0.5) + for x, y in ((start + 70, 10), (start + 100, 59), (start + 110, 100)): + self.assertEqual(film.getpixel((x, y))[:3], GREEN, (x, y)) # the second's right half + # Where the rectangles cross, both are clear: no ink there. + self.assertEqual(film.getpixel((px(0.75), 10))[3], 0) + + def test_ink_on_ink_is_refused(self): + art = Image.new('RGBA', (118, 118), RED + (255,)) + with self.assertRaisesRegex(printpng.Unsupported, 'tinta no mesmo lugar'): + self.crossing(art, art) + + def test_edges_meeting_below_the_ink_alpha_are_not_ink(self): + first = Image.new('RGBA', (118, 118)) + first.paste((255, 0, 0, 8), (59, 0, 118, 118)) # meets the second at alpha 8 + second = Image.new('RGBA', (118, 118)) + second.paste((0, 160, 0, 8), (0, 0, 59, 118)) # meets the first at alpha 8 + parts, _ = self.crossing(first, second) + self.assertEqual([name for _, name in parts], ['DTF-7-item-1.png']) + + def test_ink_meets_ink_sees_only_ink_above_the_alpha(self): + band = Image.new('RGBA', (20, 20)) + band.paste(RED + (255,), (0, 0, 10, 10)) + rows = Image.new('RGBA', (10, 10), GREEN + (255,)) + self.assertTrue(printpng.ink_meets_ink(band, rows, (5, 5))) + self.assertFalse(printpng.ink_meets_ink(band, rows, (10, 10))) + self.assertFalse(printpng.ink_meets_ink(Image.new('RGBA', (20, 20)), rows, (0, 0))) + + if __name__ == '__main__': unittest.main() diff --git a/tests/test_sheet_gap.py b/tests/test_sheet_gap.py index 7b3bc59..2e5a947 100644 --- a/tests/test_sheet_gap.py +++ b/tests/test_sheet_gap.py @@ -59,14 +59,23 @@ class OverlapTests(unittest.TestCase): Item.model_validate(loose([(0, 0), (20.5, 0), (0, 10.5), (20.5, 10.5)])) Item.model_validate(loose([(0, 0), (20, 0), (0, 10)])) - def test_pieces_on_each_other_are_refused(self): - for layout in ([(0, 0), (0, 0)], [(0, 0), (10, 5)], [(0, 0), (19.9, 9.9)], - [(0, 0)] * 100): + def test_nested_rectangles_pass(self): + # The Site nests art by its shape: rectangles crossing over transparent + # parts are honest (Sentry DTF-WEB-2B, contrato-encaixe-forma.md K2). + Item.model_validate(loose([(0, 0), (10, 5)])) + Item.model_validate(loose([(0, 0), (19.9, 9.9)])) + + def test_pieces_in_one_place_are_refused(self): + for layout in ([(0, 0), (0, 0)], [(0, 0)] * 100): with self.assertRaises(ValueError) as caught: Item.model_validate(loose(layout)) self.assertIn('uma sobre a outra', str(caught.exception)) - with self.assertRaises(ValueError): + + def test_ready_sheets_never_cross(self): + # Sheets go one under the other, never nested (contrato-encaixe-forma.md K4). + with self.assertRaises(ValueError) as caught: sheet(gap=-5) + self.assertIn('uma sobre a outra', str(caught.exception)) class ClosePiecesTests(unittest.TestCase):