diff --git a/segdisp/segment/segment.go b/segdisp/segment/segment.go index 9bd06da..91d40fb 100644 --- a/segdisp/segment/segment.go +++ b/segdisp/segment/segment.go @@ -60,8 +60,9 @@ type Option interface { // options stores the provided options. type options struct { - cellOpts []cell.Option - skipSlopes bool + cellOpts []cell.Option + skipSlopes bool + reverseSlopes bool } // option implements Option. @@ -88,6 +89,24 @@ func SkipSlopes() Option { }) } +// ReverseSlopes if provided reverses the order in which slopes are drawn. +// This only has a visible effect when the horizontal segment has height of two +// or the vertical segment has width of two. +// Without this option segments with height / width of two look like this: +// - | +// --- || +// | +// +// With this option: +// --- | +// - || +// | +func ReverseSlopes() Option { + return option(func(opts *options) { + opts.reverseSlopes = true + }) +} + // validArea validates the provided area. func validArea(ar image.Rectangle) error { if ar.Min.X < 0 || ar.Min.Y < 0 { @@ -180,6 +199,9 @@ func nextHorizLine(num int, ar image.Rectangle, opt *options) (image.Point, imag return start, end } } + if height == 2 && opt.reverseSlopes { + return adjustHoriz(start, end, width, num) + } if num < halfHeight { adjust := halfHeight - num @@ -236,6 +258,9 @@ func nextVertLine(num int, ar image.Rectangle, opt *options) (image.Point, image return start, end } } + if width == 2 && opt.reverseSlopes { + return adjustVert(start, end, width, num) + } if num < halfWidth { adjust := halfWidth - num diff --git a/segdisp/segment/segment_test.go b/segdisp/segment/segment_test.go index 9488964..aa2dcdd 100644 --- a/segdisp/segment/segment_test.go +++ b/segdisp/segment/segment_test.go @@ -310,6 +310,24 @@ func TestHV(t *testing.T) { return ft }, }, + { + desc: "horizontal, segment 3x2, reverse slopes", + opts: []Option{ + ReverseSlopes(), + }, + cellCanvas: image.Rect(0, 0, 2, 1), + ar: image.Rect(0, 0, 3, 2), + st: Horizontal, + want: func(size image.Point) *faketerm.Terminal { + ft := faketerm.MustNew(size) + bc := testbraille.MustNew(ft.Area()) + + testdraw.MustBrailleLine(bc, image.Point{0, 0}, image.Point{2, 0}) + testdraw.MustBrailleLine(bc, image.Point{1, 1}, image.Point{1, 1}) + testbraille.MustApply(bc, ft) + return ft + }, + }, { desc: "horizontal, segment 3x3", cellCanvas: image.Rect(0, 0, 2, 1), @@ -326,6 +344,25 @@ func TestHV(t *testing.T) { return ft }, }, + { + desc: "horizontal, segment 3x3, reverse slopes has no effect on larger height", + opts: []Option{ + ReverseSlopes(), + }, + cellCanvas: image.Rect(0, 0, 2, 1), + ar: image.Rect(0, 0, 3, 3), + st: Horizontal, + want: func(size image.Point) *faketerm.Terminal { + ft := faketerm.MustNew(size) + bc := testbraille.MustNew(ft.Area()) + + testdraw.MustBrailleLine(bc, image.Point{1, 0}, image.Point{1, 0}) + testdraw.MustBrailleLine(bc, image.Point{0, 1}, image.Point{2, 1}) + testdraw.MustBrailleLine(bc, image.Point{1, 2}, image.Point{1, 2}) + testbraille.MustApply(bc, ft) + return ft + }, + }, { desc: "horizontal, segment 3x3, skip slopes", opts: []Option{ @@ -665,6 +702,25 @@ func TestHV(t *testing.T) { return ft }, }, + { + desc: "vertical, segment 2x3, reverse slopes", + opts: []Option{ + ReverseSlopes(), + }, + cellCanvas: image.Rect(0, 0, 1, 1), + ar: image.Rect(0, 0, 2, 3), + st: Vertical, + want: func(size image.Point) *faketerm.Terminal { + ft := faketerm.MustNew(size) + bc := testbraille.MustNew(ft.Area()) + + testdraw.MustBrailleLine(bc, image.Point{0, 0}, image.Point{0, 0}) + testdraw.MustBrailleLine(bc, image.Point{0, 1}, image.Point{1, 1}) + testdraw.MustBrailleLine(bc, image.Point{0, 2}, image.Point{0, 2}) + testbraille.MustApply(bc, ft) + return ft + }, + }, { desc: "vertical, segment 2x4", cellCanvas: image.Rect(0, 0, 1, 1), @@ -745,6 +801,25 @@ func TestHV(t *testing.T) { return ft }, }, + { + desc: "vertical, segment 3x3, reverse slopes has no effect on larger width", + opts: []Option{ + ReverseSlopes(), + }, + cellCanvas: image.Rect(0, 0, 2, 1), + ar: image.Rect(0, 0, 3, 3), + st: Vertical, + want: func(size image.Point) *faketerm.Terminal { + ft := faketerm.MustNew(size) + bc := testbraille.MustNew(ft.Area()) + + testdraw.MustBrailleLine(bc, image.Point{1, 0}, image.Point{1, 0}) + testdraw.MustBrailleLine(bc, image.Point{0, 1}, image.Point{2, 1}) + testdraw.MustBrailleLine(bc, image.Point{1, 2}, image.Point{1, 2}) + testbraille.MustApply(bc, ft) + return ft + }, + }, { desc: "vertical, segment 3x3, skips slopes", opts: []Option{