From a591f95d2949a0224143eeeebedd3ea3ea9fdc20 Mon Sep 17 00:00:00 2001 From: Jakub Sobon Date: Mon, 29 Apr 2019 22:56:59 -0400 Subject: [PATCH] Factoring out function that calculates segment size. --- internal/segdisp/segdisp.go | 16 ++++++++++ internal/segdisp/segdisp_test.go | 43 ++++++++++++++++++++++++++ internal/segdisp/sixteen/attributes.go | 19 ++---------- 3 files changed, 61 insertions(+), 17 deletions(-) diff --git a/internal/segdisp/segdisp.go b/internal/segdisp/segdisp.go index 156d247..65f405f 100644 --- a/internal/segdisp/segdisp.go +++ b/internal/segdisp/segdisp.go @@ -72,3 +72,19 @@ func ToBraille(cvs *canvas.Canvas) (*braille.Canvas, image.Rectangle, error) { } return bc, area.WithRatio(bc.Area(), aspectRatio), nil } + +// SegmentSize given an area for the display segment determines the size of +// individual segments, i.e. the width of a vertical or the height of a +// horizontal segment. +func SegmentSize(ar image.Rectangle) int { + // widthPerc is the relative width of a segment to the width of the canvas. + const widthPerc = 9 + s := int(math.Round(float64(ar.Dx()) * widthPerc / 100)) + if s > 3 && s%2 == 0 { + // Segments with odd number of pixels in their width/height look + // better, since the spike at the top of their slopes has only one + // pixel. + s++ + } + return s +} diff --git a/internal/segdisp/segdisp_test.go b/internal/segdisp/segdisp_test.go index 19d3881..1fa5e3b 100644 --- a/internal/segdisp/segdisp_test.go +++ b/internal/segdisp/segdisp_test.go @@ -125,3 +125,46 @@ func TestToBraille(t *testing.T) { }) } } + +func TestSegmentSize(t *testing.T) { + tests := []struct { + desc string + ar image.Rectangle + want int + }{ + { + desc: "zero area", + ar: image.ZR, + want: 0, + }, + { + desc: "smallest segment size", + ar: image.Rect(0, 0, 15, 1), + want: 1, + }, + { + desc: "allows even size of two", + ar: image.Rect(0, 0, 22, 1), + want: 2, + }, + { + desc: "lands on even width, corrected to odd", + ar: image.Rect(0, 0, 44, 1), + want: 5, + }, + { + desc: "lands on odd width", + ar: image.Rect(0, 0, 55, 1), + want: 5, + }, + } + + for _, tc := range tests { + t.Run(tc.desc, func(t *testing.T) { + got := SegmentSize(tc.ar) + if got != tc.want { + t.Errorf("SegmentSize => %d, want %d", got, tc.want) + } + }) + } +} diff --git a/internal/segdisp/sixteen/attributes.go b/internal/segdisp/sixteen/attributes.go index fa535ae..3905322 100644 --- a/internal/segdisp/sixteen/attributes.go +++ b/internal/segdisp/sixteen/attributes.go @@ -23,6 +23,7 @@ import ( "math" "github.com/mum4k/termdash/internal/numbers" + "github.com/mum4k/termdash/internal/segdisp" "github.com/mum4k/termdash/internal/segdisp/segment" ) @@ -50,22 +51,6 @@ var diaSegType = map[Segment]segment.DiagonalType{ L: segment.LeftToRight, } -// segmentSize given an area for the display determines the size of individual -// segments, i.e. the width of a vertical or the height of a horizontal -// segment. -func segmentSize(ar image.Rectangle) int { - // widthPerc is the relative width of a segment to the width of the canvas. - const widthPerc = 9 - s := int(math.Round(float64(ar.Dx()) * widthPerc / 100)) - if s > 3 && s%2 == 0 { - // Segments with odd number of pixels in their width/height look - // better, since the spike at the top of their slopes has only one - // pixel. - s++ - } - return s -} - // attributes contains attributes needed to draw the segment display. // Refer to doc/segment_placement.svg for a visual aid and explanation of the // usage of the square roots. @@ -115,7 +100,7 @@ type attributes struct { // newAttributes calculates attributes needed to place the segments for the // provided pixel area. func newAttributes(bcAr image.Rectangle) *attributes { - segSize := segmentSize(bcAr) + segSize := segdisp.SegmentSize(bcAr) // diaPerc is the size of the diaGap in percentage of the segment's size. const diaPerc = 40