From cb7ae66556122a042ca3775783c7387039f5d00d Mon Sep 17 00:00:00 2001 From: Achilleas Anagnostopoulos Date: Thu, 13 Jul 2017 07:52:22 +0100 Subject: [PATCH] Define LogoSetter interface and BestFit selection helper --- src/gopheros/device/video/console/device.go | 9 +++++ .../device/video/console/logo/logo.go | 33 ++++++++++++++-- .../device/video/console/logo/logo_test.go | 39 +++++++++++++++++++ 3 files changed, 78 insertions(+), 3 deletions(-) create mode 100644 src/gopheros/device/video/console/logo/logo_test.go diff --git a/src/gopheros/device/video/console/device.go b/src/gopheros/device/video/console/device.go index c64c8b7..53c5506 100644 --- a/src/gopheros/device/video/console/device.go +++ b/src/gopheros/device/video/console/device.go @@ -2,6 +2,7 @@ package console import ( "gopheros/device/video/console/font" + "gopheros/device/video/console/logo" "image/color" ) @@ -68,3 +69,11 @@ type Device interface { type FontSetter interface { SetFont(*font.Font) } + +// LogoSetter is an interface implemented by console devices that +// support drawing of logo images. +// +// SetLogo selects the logo to be drawn by the console. +type LogoSetter interface { + SetLogo(*logo.Image) +} diff --git a/src/gopheros/device/video/console/logo/logo.go b/src/gopheros/device/video/console/logo/logo.go index 06def69..825bfa4 100644 --- a/src/gopheros/device/video/console/logo/logo.go +++ b/src/gopheros/device/video/console/logo/logo.go @@ -3,9 +3,10 @@ package logo import "image/color" -// ConsoleLogo defines the logo used by framebuffer consoles. If set to nil -// then no logo will be displayed. -var ConsoleLogo *Image +var ( + // The list of available logos. + availableLogos []*Image +) // Alignment defines the supported horizontal alignments for a console logo. type Alignment uint8 @@ -42,3 +43,29 @@ type Image struct { // represents an index in the logo palette. Data []uint8 } + +// BestFit returns the best logo from the available logo list given the +// specified console dimensions. +func BestFit(consoleWidth, consoleHeight uint32) *Image { + var ( + best *Image + bestDelta, absDelta uint32 + threshold = consoleHeight / 10 + ) + + for _, l := range availableLogos { + if l.Height > threshold { + absDelta = l.Height - threshold + } else { + absDelta = threshold - l.Height + } + + if best == nil || absDelta < bestDelta { + best = l + bestDelta = absDelta + continue + } + } + + return best +} diff --git a/src/gopheros/device/video/console/logo/logo_test.go b/src/gopheros/device/video/console/logo/logo_test.go new file mode 100644 index 0000000..ca9df10 --- /dev/null +++ b/src/gopheros/device/video/console/logo/logo_test.go @@ -0,0 +1,39 @@ +package logo + +import "testing" + +func TestBestFit(t *testing.T) { + defer func(origList []*Image) { + availableLogos = origList + }(availableLogos) + + availableLogos = []*Image{ + &Image{Height: 64}, + &Image{Height: 96}, + &Image{Height: 128}, + } + + specs := []struct { + consW, consH uint32 + expIndex int + }{ + {320, 200, 0}, + {800, 600, 0}, + {1024, 768, 0}, + {1280, 1024, 1}, + {3000, 3000, 2}, + {2500, 1600, 2}, + } + + for specIndex, spec := range specs { + got := BestFit(spec.consW, spec.consH) + if got == nil { + t.Errorf("[spec %d] unable to find a logo", specIndex) + continue + } + + if got.Height != availableLogos[spec.expIndex].Height { + t.Errorf("[spec %d] expected to get logo with height %d; got %d", specIndex, availableLogos[spec.expIndex].Height, got.Height) + } + } +}