Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions ReferenceTests/Retro68/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ add_application(CatalogOrderCapture CatalogOrderCapture.c)
add_application(HighLevelEventCapture HighLevelEventCapture.c HighLevelEventCapture.r)
add_application(MemoryManagerCapture MemoryManagerCapture.c)
add_application(MixedModeCapture MixedModeCapture.c)
add_application(RegionRecordingCapture RegionRecordingCapture.c)
33 changes: 33 additions & 0 deletions ReferenceTests/Retro68/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -229,3 +229,36 @@ The capture doesn't cover 68k callers, which reach a descriptor through its
trap word, or PowerPC fragments that need preparing. `MixedModeTests` checks
`GraphiteMixedMode` against the capture; Graphite throws for the cases that
crashed or weren't run.

## Region recording

`RegionRecordingCapture.c` draws in a port over a bitmap in memory, between
`OpenRgn` and `CloseRgn`, and writes the region each case gives: its record's
bytes, the pen's location and how many pixels were inked. Its output on Mac OS
9.2.2 is `RegionRecording-macos-9.2.2-ppc.txt` (CR line endings), from the
UTM setup above.

- Nothing was drawn while a region was open, whatever the verb: no pixel was
inked by paint, erase, invert or fill either.
- Only framed shapes were recorded: FrameRect, FrameOval, FrameRoundRect,
FrameRgn and FramePoly gave the shape's region, whatever the pen's size,
and with the pen hidden. PaintRect, EraseRect, InvertRect, FillRect and
PaintOval gave nothing, so did FrameRect on an empty rectangle, and
FrameArc, whose edge alone encloses nothing.
- Shapes combined by the even-odd rule: two overlapping rectangles left the
overlap out.
- Lines added a region only as a path that came back to where it began. A
triangle of three `LineTo`s gave the same region as `FramePoly`; one line,
or two lines that didn't close, gave an empty one. (A single diagonal
`Line(4,4)` gave a staircase of pixels, which Graphite doesn't reproduce.)
`LineTo` moved the pen as usual, and `FramePoly` left it at the polygon's
last point.
- A polygon's region covers, in each row, the columns right of the left
edge's crossing and left of the right edge's, the crossings taken at the
row's top: a triangle with its left edge at column 2 starts at column 3.
Only one triangle was measured.
- A second `OpenRgn` started the region over, and `CloseRgn` with none open
gave an empty region, with no error.

`RegionRecordingTests` checks `Port.beginRegion` and `endRegion` against the
capture.
1 change: 1 addition & 0 deletions ReferenceTests/Retro68/RegionRecording-macos-9.2.2-ppc.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Nothing recordedOpenRgn, CloseRgn -> size 10, pen (0,0), inked 0, bytes: 000A 0000 0000 0000 0000# Framed shapesFrameRect -> size 10, pen (0,0), inked 0, bytes: 000A 0002 0002 0006 0006FrameRect, FrameRect overlapping -> size 44, pen (0,0), inked 0, bytes: 002C 0002 0002 000A 000A 0002 0002 0006 7FFF 0004 0004 000A 7FFF 0006 0002 0006 7FFF 000A 0004 000A 7FFF 7FFFFrameOval -> size 52, pen (0,0), inked 0, bytes: 0034 0004 0004 000A 000A 0004 0005 0009 7FFF 0005 0004 0005 0009 000A 7FFF 0009 0004 0005 0009 000A 7FFF 000A 0005 0009 7FFF 7FFFFrameRoundRect 4,4 -> size 52, pen (0,0), inked 0, bytes: 0034 0004 0004 000A 000A 0004 0005 0009 7FFF 0005 0004 0005 0009 000A 7FFF 0009 0004 0005 0009 000A 7FFF 000A 0005 0009 7FFF 7FFFFrameArc 0,90 -> size 10, pen (0,0), inked 0, bytes: 000A 0000 0000 0000 0000FrameRect of an empty rectangle -> size 10, pen (0,0), inked 0, bytes: 000A 0000 0000 0000 0000FrameRect, pen 2x2 -> size 10, pen (0,0), inked 0, bytes: 000A 0002 0002 0006 0006FrameRgn -> size 10, pen (0,0), inked 0, bytes: 000A 0002 0002 0006 0006FramePoly of a triangle -> size 60, pen (2,2), inked 0, bytes: 003C 0002 0003 0007 0008 0002 0003 0008 7FFF 0003 0007 0008 7FFF 0004 0003 0004 7FFF 0005 0006 0007 7FFF 0006 0004 0005 7FFF 0007 0005 0006 7FFF 7FFF# LinesMoveTo, LineTo: one line -> size 10, pen (9,1), inked 0, bytes: 000A 0000 0000 0000 0000open path of two lines -> size 10, pen (9,7), inked 0, bytes: 000A 0000 0000 0000 0000closed triangle of lines -> size 60, pen (2,2), inked 0, bytes: 003C 0002 0003 0007 0008 0002 0003 0008 7FFF 0003 0007 0008 7FFF 0004 0003 0004 7FFF 0005 0006 0007 7FFF 0006 0004 0005 7FFF 0007 0005 0006 7FFF 7FFFLine(4,4) -> size 44, pen (5,5), inked 0, bytes: 002C 0001 0001 0004 0005 0001 0001 0002 7FFF 0002 0002 0003 7FFF 0003 0003 0004 7FFF 0004 0004 0005 7FFF 7FFFone line, pen 2x2 -> size 10, pen (9,1), inked 0, bytes: 000A 0000 0000 0000 0000FrameRect with the pen hidden -> size 10, pen (0,0), inked 0, bytes: 000A 0002 0002 0006 0006# Other verbs while openPaintRect -> size 10, pen (0,0), inked 0, bytes: 000A 0000 0000 0000 0000EraseRect -> size 10, pen (0,0), inked 0, bytes: 000A 0000 0000 0000 0000InvertRect -> size 10, pen (0,0), inked 0, bytes: 000A 0000 0000 0000 0000FillRect black -> size 10, pen (0,0), inked 0, bytes: 000A 0000 0000 0000 0000PaintOval -> size 10, pen (0,0), inked 0, bytes: 000A 0000 0000 0000 0000FrameRect then PaintRect -> size 10, pen (0,0), inked 0, bytes: 000A 0002 0002 0006 0006# Open twicesecond OpenRgn after FrameRect, then FrameRect, one CloseRgn -> size 10, pen (0,0), inked 0, bytes: 000A 0004 0004 000A 000A# Without OpenRgnFrameRect, nothing open -> nil, QDError 0 (inked count above is the framed rectangle drawn)# CloseRgn with none openCloseRgn without OpenRgn -> size 10, pen (0,0), inked 0, bytes: 000A 0000 0000 0000 0000# Done
Expand Down
148 changes: 148 additions & 0 deletions ReferenceTests/Retro68/RegionRecordingCapture.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
/*
* Captures what OpenRgn and CloseRgn record, writing "RegionRecording.txt"
* next to itself. It refuses to run if that file is already there. The file
* is flushed after every line, so a call that crashes leaves the lines
* before it; the calls most likely to crash come last.
*
* It draws in a port of its own over a 32 by 16 pixel bitmap in memory, not
* on the screen. For each case it records the region CloseRgn gives: its
* handle's size and bytes (rgnSize, rgnBBox, then scanline inversion
* points), the pen's location afterward, and how many bitmap pixels were
* set, which says whether anything was drawn while the region was open.
*/
#include <Quickdraw.h>
#include <MacMemory.h>
#include <Files.h>
#include <stdio.h>
#include <string.h>

static short out;
static GrafPort port;
static BitMap bits;
static char pixels[4 * 16];

static void line(const char *text)
{
long count = strlen(text);
FSWrite(out, &count, text);
count = 1;
FSWrite(out, &count, "\r");
FlushVol(0, 0);
}

static long inked(void)
{
long count = 0;
int i, bit;
for (i = 0; i < (int)sizeof pixels; i++)
for (bit = 0; bit < 8; bit++)
if (((unsigned char)pixels[i] >> bit) & 1) count++;
return count;
}

static void clear(void)
{
memset(pixels, 0, sizeof pixels);
PenNormal();
MoveTo(0, 0);
}

/* The region CloseRgn gave: size, bytes, pen and ink, then disposed. */
static void report(const char *label, RgnHandle rgn)
{
char text[400];
Point pen;
long size, i;
int used;
GetPen(&pen);
if (!rgn) {
snprintf(text, sizeof text, "%s -> nil, QDError %d", label, QDError());
line(text);
return;
}
size = GetHandleSize((Handle)rgn);
used = snprintf(text, sizeof text, "%s -> size %ld, pen (%d,%d), inked %ld, bytes:",
label, size, pen.h, pen.v, inked());
for (i = 0; i < size && used < (int)sizeof text - 4; i++)
used += snprintf(text + used, sizeof text - used, "%s%02X", i % 2 ? "" : " ", ((unsigned char *)*rgn)[i]);
line(text);
DisposeRgn(rgn);
}

/* CloseRgn into a new region, then report it. */
static void finish(const char *label)
{
RgnHandle rgn = NewRgn();
CloseRgn(rgn);
report(label, rgn);
}

int main(void)
{
static const unsigned char name[] = { 19, 'R','e','g','i','o','n','R','e','c','o','r','d','i','n','g','.','t','x','t' };
Rect box = { 0, 0, 16, 32 };
Rect a = { 2, 2, 6, 6 }, b = { 4, 4, 10, 10 }, flat = { 5, 5, 5, 9 };
RgnHandle rgn;
PolyHandle poly;

if (Create(name, 0, 'Grph', 'TEXT') != noErr) return 1;
if (FSOpen(name, 0, &out) != noErr) return 2;

OpenPort(&port);
bits.baseAddr = pixels;
bits.rowBytes = 4;
bits.bounds = box;
SetPortBits(&bits);
port.portRect = box;
RectRgn(port.visRgn, &box);
RectRgn(port.clipRgn, &box);

line("# Nothing recorded");
clear(); OpenRgn(); finish("OpenRgn, CloseRgn");

line("# Framed shapes");
clear(); OpenRgn(); FrameRect(&a); finish("FrameRect");
clear(); OpenRgn(); FrameRect(&a); FrameRect(&b); finish("FrameRect, FrameRect overlapping");
clear(); OpenRgn(); FrameOval(&b); finish("FrameOval");
clear(); OpenRgn(); FrameRoundRect(&b, 4, 4); finish("FrameRoundRect 4,4");
clear(); OpenRgn(); FrameArc(&b, 0, 90); finish("FrameArc 0,90");
clear(); OpenRgn(); FrameRect(&flat); finish("FrameRect of an empty rectangle");
clear(); PenSize(2, 2); OpenRgn(); FrameRect(&a); finish("FrameRect, pen 2x2");
clear(); rgn = NewRgn(); RectRgn(rgn, &a); OpenRgn(); FrameRgn(rgn); finish("FrameRgn");
DisposeRgn(rgn);
clear(); poly = OpenPoly(); MoveTo(2, 2); LineTo(8, 2); LineTo(5, 8); LineTo(2, 2); ClosePoly();
clear(); OpenRgn(); FramePoly(poly); finish("FramePoly of a triangle");
KillPoly(poly);

line("# Lines");
clear(); OpenRgn(); MoveTo(1, 1); LineTo(9, 1); finish("MoveTo, LineTo: one line");
clear(); OpenRgn(); MoveTo(1, 1); LineTo(9, 1); LineTo(9, 7); finish("open path of two lines");
clear(); OpenRgn(); MoveTo(2, 2); LineTo(8, 2); LineTo(5, 8); LineTo(2, 2); finish("closed triangle of lines");
clear(); OpenRgn(); MoveTo(1, 1); Line(4, 4); finish("Line(4,4)");
clear(); PenSize(2, 2); OpenRgn(); MoveTo(1, 1); LineTo(9, 1); finish("one line, pen 2x2");
clear(); HidePen(); OpenRgn(); FrameRect(&a); finish("FrameRect with the pen hidden"); ShowPen();

line("# Other verbs while open");
clear(); OpenRgn(); PaintRect(&a); finish("PaintRect");
clear(); OpenRgn(); EraseRect(&a); finish("EraseRect");
clear(); OpenRgn(); InvertRect(&a); finish("InvertRect");
clear(); OpenRgn(); FillRect(&a, &qd.black); finish("FillRect black");
clear(); OpenRgn(); PaintOval(&a); finish("PaintOval");
clear(); OpenRgn(); FrameRect(&a); PaintRect(&b); finish("FrameRect then PaintRect");

line("# Open twice");
clear(); OpenRgn(); FrameRect(&a); OpenRgn(); FrameRect(&b); finish("second OpenRgn after FrameRect, then FrameRect, one CloseRgn");

line("# Without OpenRgn");
clear(); FrameRect(&a); report("FrameRect, nothing open", (RgnHandle)0);
line(" (inked count above is the framed rectangle drawn)");

/* Last: a CloseRgn with no region open may crash. */
line("# CloseRgn with none open");
clear(); rgn = NewRgn(); CloseRgn(rgn); report("CloseRgn without OpenRgn", rgn);

line("# Done");
FSClose(out);
FlushVol(0, 0);
return 0;
}
19 changes: 18 additions & 1 deletion Sources/Graphite2D/Drawing.swift
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@ public extension Port {

/// Does one of QuickDraw's shape operations on a region.
func perform(_ operation: ShapeOperation, on region: Region, outline: () -> Region) {
// A region being recorded takes a framed shape, and nothing is drawn.
if openRegion != nil {
if case .frame = operation { record(region) }
return
}
switch operation {
case .frame:
guard isPenVisible else { return }
Expand Down Expand Up @@ -102,6 +107,8 @@ public extension Port {
/// FrameArc and the rest: FrameArc draws the oval's edge alone, not the
/// wedge's sides.
func perform(_ operation: ShapeOperation, arc rect: Rect, startAngle: Int16, arcAngle: Int16) {
// The arc's edge alone isn't closed, so it encloses nothing to record.
guard openRegion == nil else { return }
let region = Region.arc(in: rect, startAngle: startAngle, arcAngle: arcAngle)
perform(operation, on: region) {
region.subtracting(.oval(in: rect.insetBy(dh: pen.size.h, dv: pen.size.v)))
Expand All @@ -111,6 +118,14 @@ public extension Port {
/// FramePoly and the rest: FramePoly draws lines between the points, as
/// LineTo does.
func perform(_ operation: ShapeOperation, polygon: Polygon) {
if openRegion != nil {
// Mac OS 9.2.2 left the pen at the polygon's last point.
if case .frame = operation, let last = polygon.points.last {
record(.outlinedPolygon(polygon))
pen.location = last
}
return
}
let region = Region.polygon(polygon)
perform(operation, on: region) {
var outline = Region()
Expand All @@ -129,7 +144,9 @@ public extension Port {
/// LineTo: a line from the pen's location to a point, which becomes the
/// pen's location.
func line(to point: Point) {
if isPenVisible {
if openRegion != nil {
recordLine(from: pen.location, to: point)
} else if isPenVisible {
draw(.line(from: pen.location, to: point, pen: pen.size), pattern: pen.pattern, mode: pen.mode)
}
pen.location = point
Expand Down
46 changes: 46 additions & 0 deletions Sources/Graphite2D/Port.swift
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,55 @@ public final class Port {
/// others don't cover; a world's, all of it.
public var visibleRegion: Region

/// The region OpenRgn has begun and CloseRgn hasn't ended, or nil. While
/// there is one, nothing is drawn: framed shapes add to it, and so does
/// a path of lines that comes back to where it began. Mac OS 9.2.2
/// (ReferenceTests/Retro68/RegionRecording-macos-9.2.2-ppc.txt) recorded
/// nothing for paint, erase, invert and fill, for FrameArc, or for a
/// path left open.
public private(set) var openRegion: Region?

/// The points of the path of lines being recorded.
private var recordedPath: [Point] = []

/// The device the port's pixels are on, for its global coordinates.
public weak var device: GraphicsDevice?

/// OpenRgn: starts recording a region, as an empty one. One already
/// being recorded is lost.
public func beginRegion() {
openRegion = Region()
recordedPath = []
}

/// CloseRgn: the region recorded since OpenRgn. Mac OS 9.2.2 gave an
/// empty region, and no error, when none was open; this gives nil.
public func endRegion() -> Region? {
defer {
openRegion = nil
recordedPath = []
}
return openRegion
}

/// Adds to the region being recorded; nothing if none is open. Where
/// recorded shapes overlap, Mac OS 9.2.2 left a hole: outlines record by
/// the even-odd rule.
func record(_ region: Region) {
openRegion = openRegion?.symmetricDifference(region)
}

/// A line of a path being recorded. A path that returns to its start,
/// after at least three lines, adds the polygon it makes.
func recordLine(from start: Point, to end: Point) {
if recordedPath.last != start { recordedPath = [start] }
recordedPath.append(end)
if recordedPath.count >= 4, end == recordedPath[0] {
record(.outlinedPolygon(Polygon(points: Array(recordedPath.dropLast()))))
recordedPath = []
}
}

/// A port over pixels, showing all of them.
public init(pixels: PixelBuffer) {
self.pixels = pixels
Expand Down
31 changes: 31 additions & 0 deletions Sources/Graphite2D/Shapes.swift
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,37 @@ public extension Region {
return Region(bands: bands)
}

/// The region Mac OS 9.2.2 recorded for a framed polygon, or a path of
/// lines that came back to its start (ReferenceTests/Retro68/
/// RegionRecording-macos-9.2.2-ppc.txt): by the even-odd rule, in each
/// row a pair of crossings, taken where the edges meet the row's top,
/// covers the columns right of the first and left of the second's whole
/// column. Measured on one triangle; PaintPoly is not known to agree.
static func outlinedPolygon(_ polygon: Polygon) -> Region {
let points = polygon.points
guard points.count >= 3 else { return Region() }
let box = polygon.bounds
var bands: [Band] = []
for row in Int(box.top) ..< Int(box.bottom) {
let y = Double(row)
var crossings: [Double] = []
for (index, a) in points.enumerated() {
let b = points[(index + 1) % points.count]
let ay = Double(a.v), by = Double(b.v)
guard (ay <= y && y < by) || (by <= y && y < ay) else { continue }
crossings.append(Double(a.h) + (y - ay) / (by - ay) * Double(Int(b.h) - Int(a.h)))
}
crossings.sort()
var spans: [Span] = []
for pair in stride(from: 0, to: crossings.count - 1, by: 2) {
let left = Int(crossings[pair].rounded(.down)) + 1, right = Int(crossings[pair + 1].rounded(.down))
if left < right { spans.append(Span(left: left, right: right)) }
}
if !spans.isEmpty { bands.append(Band(top: row, bottom: row + 1, spans: spans)) }
}
return Region(bands: bands)
}

/// A line from one point to another, each point it passes through
/// covered by the pen's rectangle, `pen.h` wide and `pen.v` high.
static func line(from start: Point, to end: Point, pen: Point) -> Region {
Expand Down
Loading
Loading