This repository has been archived on 2024-06-02. You can view files and clone it, but cannot push or open issues or pull requests.
x/canvas/canvas.go

134 lines
3.4 KiB
Go
Raw Normal View History

2023-07-02 00:52:14 -06:00
package xcanvas
import "image"
import "image/color"
import "github.com/jezek/xgbutil"
2023-07-03 22:04:00 -06:00
import "github.com/jezek/xgb/xproto"
2023-07-02 00:52:14 -06:00
import "github.com/jezek/xgbutil/xgraphics"
import "git.tebibyte.media/tomo/tomo/canvas"
// Canvas satisfies the canvas.Canvas interface. It draws to an xgraphics.Image.
2023-08-22 19:24:38 -06:00
// It must be closed after use.
2023-07-02 00:52:14 -06:00
type Canvas struct {
*xgraphics.Image
}
2023-08-22 19:24:38 -06:00
// NewCanvas creates a new canvas from a bounding rectangle.
func NewCanvas (x *xgbutil.XUtil, bounds image.Rectangle) *Canvas {
return NewCanvasFrom(xgraphics.New(x, bounds))
2023-07-02 00:52:14 -06:00
}
2023-08-22 19:24:38 -06:00
// NewCanvasFrom creates a new canvas from an existing xgraphics.Image. Note
// that calling Close() on the resulting canvas will destroy this image.
func NewCanvasFrom (image *xgraphics.Image) *Canvas {
2023-07-04 22:44:56 -06:00
if image == nil { return nil }
return &Canvas { image }
2023-07-02 00:52:14 -06:00
}
// Pen returns a new drawing context.
2023-07-04 22:44:56 -06:00
func (this *Canvas) Pen () canvas.Pen {
return &pen {
2023-07-02 00:52:14 -06:00
image: this.Image,
}
}
// Clip returns a sub-canvas of this canvas.
2023-07-04 22:44:56 -06:00
func (this *Canvas) Clip (bounds image.Rectangle) canvas.Canvas {
this.assert()
subImage := this.Image.SubImage(bounds)
if subImage == nil { return nil }
xImage := subImage.(*xgraphics.Image)
return &Canvas { xImage }
2023-07-02 00:52:14 -06:00
}
2023-07-03 22:04:00 -06:00
// Push pushes this canvas to the screen.
2023-07-04 22:44:56 -06:00
func (this *Canvas) Push (window xproto.Window) {
this.assert()
2023-07-03 22:04:00 -06:00
this.XDraw()
this.XExpPaint(window, this.Bounds().Min.X, this.Bounds().Min.Y)
}
2023-08-22 19:24:38 -06:00
// Close frees this canvas from the X server.
func (this *Canvas) Close () {
this.assert()
this.Image.Destroy()
}
2023-07-04 22:44:56 -06:00
func (this *Canvas) assert () {
if this == nil { panic("nil canvas") }
}
2023-07-02 00:52:14 -06:00
// TODO: we need to implement:
// - cap
// - joint
// - align
type pen struct {
image *xgraphics.Image
2023-08-23 17:21:28 -06:00
closed bool
endCap canvas.Cap
joint canvas.Joint
weight int
align canvas.StrokeAlign
stroke xgraphics.BGRA
fill xgraphics.BGRA
texture *Texture
2023-07-02 00:52:14 -06:00
}
2023-07-04 22:44:56 -06:00
func (this *pen) Rectangle (bounds image.Rectangle) {
bounds = bounds.Canon()
2023-07-02 00:52:14 -06:00
if this.weight == 0 {
2023-08-29 13:52:24 -06:00
if this.fill.A > 0 && !this.textureObscures() {
2023-08-23 17:21:28 -06:00
this.fillRectangle(this.fill, bounds)
}
2023-08-29 13:52:24 -06:00
if this.texture != nil {
this.textureRectangle(bounds)
}
2023-07-02 00:52:14 -06:00
} else {
2023-08-23 17:21:28 -06:00
if this.stroke.A > 0 {
this.strokeRectangle(this.stroke, bounds)
}
2023-07-02 00:52:14 -06:00
}
}
2023-07-04 22:44:56 -06:00
func (this *pen) Path (points ...image.Point) {
2023-07-02 00:52:14 -06:00
if this.weight == 0 {
2023-08-23 17:21:28 -06:00
if this.fill.A > 0 {
this.fillPolygon(this.fill, points...)
}
2023-07-02 00:52:14 -06:00
} else if this.closed {
2023-08-23 17:21:28 -06:00
if this.stroke.A > 0 {
this.strokePolygon(this.stroke, points...)
}
2023-07-02 00:52:14 -06:00
} else {
2023-08-23 17:21:28 -06:00
if this.stroke.A > 0 {
this.polyLine(this.stroke, points...)
}
2023-07-02 00:52:14 -06:00
}
}
2023-08-22 19:24:38 -06:00
2023-07-04 22:44:56 -06:00
func (this *pen) Closed (closed bool) { this.closed = closed }
func (this *pen) Cap (endCap canvas.Cap) { this.endCap = endCap }
func (this *pen) Joint (joint canvas.Joint) { this.joint = joint }
func (this *pen) StrokeWeight (weight int) { this.weight = weight }
func (this *pen) StrokeAlign (align canvas.StrokeAlign) { this.align = align }
2023-07-02 00:52:14 -06:00
2023-08-24 13:54:22 -06:00
func (this *pen) Stroke (stroke color.Color) { this.stroke = convertColor(stroke) }
func (this *pen) Fill (fill color.Color) { this.fill = convertColor(fill) }
func (this *pen) Texture (texture canvas.Texture) { this.texture = AssertTexture(texture) }
2023-07-02 00:52:14 -06:00
2023-08-29 13:52:24 -06:00
func (this *pen) textureObscures () bool {
return this.texture != nil && this.texture.Opaque()
}
2023-08-22 19:24:38 -06:00
func convertColor (c color.Color) xgraphics.BGRA {
2023-07-02 00:52:14 -06:00
r, g, b, a := c.RGBA()
2023-08-22 19:24:38 -06:00
return xgraphics.BGRA {
B: uint8(b >> 8),
G: uint8(g >> 8),
R: uint8(r >> 8),
A: uint8(a >> 8),
2023-07-02 00:52:14 -06:00
}
}