mirror of
https://github.com/mum4k/termdash.git
synced 2025-04-25 13:48:50 +08:00
Factoring out function that calculates segment size.
This commit is contained in:
parent
b8a6427d47
commit
a591f95d29
@ -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
|
||||
}
|
||||
|
@ -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)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
@ -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
|
||||
|
Loading…
x
Reference in New Issue
Block a user