package display import ( "image" "image/color" "image/draw" "os" "golang.org/x/image/font" "golang.org/x/image/font/basicfont" "golang.org/x/image/font/inconsolata" "golang.org/x/image/math/fixed" "go.maze.io/harmon/internal/pixel" _ "image/gif" // GIF codec _ "image/jpeg" // JPEG codec _ "image/png" // PNG codec ) var ( Font7x13 = basicfont.Face7x13 Font8x16 = inconsolata.Regular8x16 ) type Drawer interface { Draw(r image.Rectangle, src image.Image, sp image.Point) error } func DrawRectangle(d draw.Image, x, y, w, h int, c color.Color) { for i, l := y, y+h; i < l; i++ { DrawLine(d, x, i, x+w, i, c) } } func DrawLine(p draw.Image, x1, y1, x2, y2 int, c color.Color) { var dx, dy, e, slope int // Because drawing p1 -> p2 is equivalent to draw p2 -> p1, // I sort points in x-axis order to handle only half of possible cases. if x1 > x2 { x1, y1, x2, y2 = x2, y2, x1, y1 } dx, dy = x2-x1, y2-y1 // Because point is x-axis ordered, dx cannot be negative if dy < 0 { dy = -dy } switch { // Is line a point ? case x1 == x2 && y1 == y2: p.Set(x1, y1, c) // Is line an horizontal ? case y1 == y2: for ; dx != 0; dx-- { p.Set(x1, y1, c) x1++ } p.Set(x1, y1, c) // Is line a vertical ? case x1 == x2: if y1 > y2 { y1, y2 = y2, y1 } for ; dy != 0; dy-- { p.Set(x1, y1, c) y1++ } p.Set(x1, y1, c) // Is line a diagonal ? case dx == dy: if y1 < y2 { for ; dx != 0; dx-- { p.Set(x1, y1, c) x1++ y1++ } } else { for ; dx != 0; dx-- { p.Set(x1, y1, c) x1++ y1-- } } p.Set(x1, y1, c) // wider than high ? case dx > dy: if y1 < y2 { // BresenhamDxXRYD(img, x1, y1, x2, y2, col) dy, e, slope = 2*dy, dx, 2*dx for ; dx != 0; dx-- { p.Set(x1, y1, c) x1++ e -= dy if e < 0 { y1++ e += slope } } } else { // BresenhamDxXRYU(img, x1, y1, x2, y2, col) dy, e, slope = 2*dy, dx, 2*dx for ; dx != 0; dx-- { p.Set(x1, y1, c) x1++ e -= dy if e < 0 { y1-- e += slope } } } p.Set(x2, y2, c) // higher than wide. default: if y1 < y2 { // BresenhamDyXRYD(img, x1, y1, x2, y2, col) dx, e, slope = 2*dx, dy, 2*dy for ; dy != 0; dy-- { p.Set(x1, y1, c) y1++ e -= dx if e < 0 { x1++ e += slope } } } else { // BresenhamDyXRYU(img, x1, y1, x2, y2, col) dx, e, slope = 2*dx, dy, 2*dy for ; dy != 0; dy-- { p.Set(x1, y1, c) y1-- e -= dx if e < 0 { x1++ e += slope } } } p.Set(x2, y2, c) } } func DrawText(d draw.Image, x, y int, face font.Face, text string) { var ( fontHeight int fontWidth int ) if f, ok := face.(*basicfont.Face); ok { fontWidth = f.Advance fontHeight = f.Height } else { metrics := face.Metrics() if advance, ok := face.GlyphAdvance('X'); ok { fontWidth = advance.Round() } fontHeight = metrics.Height.Ceil() } drawer := &font.Drawer{ Dst: d, Src: &image.Uniform{pixel.On}, Face: face, } drawer.Dot = fixed.P(x*fontWidth, y*fontHeight) drawer.DrawString(text) } func drawCircle(d draw.Image, xc, yc, x, y int, c color.Color) { d.Set(xc+x, yc+y, c) d.Set(xc-x, yc+y, c) d.Set(xc+x, yc-y, c) d.Set(xc-x, yc-y, c) d.Set(xc+y, yc+x, c) d.Set(xc-y, yc+x, c) d.Set(xc+y, yc-x, c) d.Set(xc-y, yc-x, c) } func DrawCircle(i draw.Image, xc, yc, r int, c color.Color) { var ( x int y = r d = 3 - 2*r ) drawCircle(i, xc, yc, x, y, c) for y >= x { if d > 0 { y-- d = d + 4*(x-y) + 10 } else { d = d + 4*x + 6 } x++ drawCircle(i, xc, yc, x, y, c) } } func DrawImage(d draw.Image, x, y int, name string) error { f, err := os.Open(name) if err != nil { return err } i, _, err := image.Decode(f) if err != nil { _ = f.Close() return err } r := d.Bounds() if d, ok := d.(Drawer); ok { r.Min = image.Pt(x, y) return d.Draw(r, i, image.Point{}) } draw.Draw(d, r, i, image.Point{}, draw.Src) return nil } func Scroll(d draw.Image, xstep, ystep int) { var ( sx, y, xend, yend, dx, dy int r = d.Bounds() ) if xstep < 0 { xend = r.Max.X + xstep if xend <= 0 { return } dx = 1 } else { sx = r.Max.X - 1 xend = xstep - 1 if xend >= sx { return } } if ystep < 0 { yend = r.Max.Y + ystep if yend <= 0 { return } dy = 1 } else { y = r.Max.Y - 1 yend = ystep - 1 if yend >= y { return } dy = -1 } for ; y != yend; y += dy { for x := sx; x != xend; x += dx { d.Set(x, y, d.At(x, y)) } } }