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

134 lines
3.5 KiB
Go
Raw Normal View History

2023-07-02 06:52:14 +00:00
package xcanvas
import "image"
import "image/color"
import "github.com/jezek/xgbutil"
2023-07-04 04:04:00 +00:00
import "github.com/jezek/xgb/xproto"
2023-07-02 06:52:14 +00: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-23 01:24:38 +00:00
// It must be closed after use.
2023-07-02 06:52:14 +00:00
type Canvas struct {
*xgraphics.Image
}
2023-08-23 01:24:38 +00: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 06:52:14 +00:00
}
2023-08-23 01:24:38 +00: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-05 04:44:56 +00:00
if image == nil { return nil }
return &Canvas { image }
2023-07-02 06:52:14 +00:00
}
// Pen returns a new drawing context.
2023-07-05 04:44:56 +00:00
func (this *Canvas) Pen () canvas.Pen {
return &pen {
2023-07-02 06:52:14 +00:00
image: this.Image,
}
}
2024-05-26 04:55:20 +00:00
// SubCanvas returns a subset of this canvas that points to the same data.
func (this *Canvas) SubCanvas (bounds image.Rectangle) canvas.Canvas {
2023-07-05 04:44:56 +00:00
this.assert()
subImage := this.Image.SubImage(bounds)
if subImage == nil { return nil }
xImage := subImage.(*xgraphics.Image)
return &Canvas { xImage }
2023-07-02 06:52:14 +00:00
}
2023-07-04 04:04:00 +00:00
// Push pushes this canvas to the screen.
2023-07-05 04:44:56 +00:00
func (this *Canvas) Push (window xproto.Window) {
this.assert()
2023-07-04 04:04:00 +00:00
this.XDraw()
this.XExpPaint(window, this.Bounds().Min.X, this.Bounds().Min.Y)
}
2023-08-23 01:24:38 +00:00
// Close frees this canvas from the X server.
func (this *Canvas) Close () {
this.assert()
this.Image.Destroy()
}
2023-07-05 04:44:56 +00:00
func (this *Canvas) assert () {
if this == nil { panic("nil canvas") }
}
2023-07-02 06:52:14 +00:00
// TODO: we need to implement:
// - cap
// - joint
// - align
type pen struct {
image *xgraphics.Image
2023-08-23 23:21:28 +00: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 06:52:14 +00:00
}
2023-07-05 04:44:56 +00:00
func (this *pen) Rectangle (bounds image.Rectangle) {
bounds = bounds.Canon()
2023-07-02 06:52:14 +00:00
if this.weight == 0 {
2023-08-29 19:52:24 +00:00
if this.fill.A > 0 && !this.textureObscures() {
2023-08-23 23:21:28 +00:00
this.fillRectangle(this.fill, bounds)
}
2023-08-29 19:52:24 +00:00
if this.texture != nil {
this.textureRectangle(bounds)
}
2023-07-02 06:52:14 +00:00
} else {
2023-08-23 23:21:28 +00:00
if this.stroke.A > 0 {
this.strokeRectangle(this.stroke, bounds)
}
2023-07-02 06:52:14 +00:00
}
}
2023-07-05 04:44:56 +00:00
func (this *pen) Path (points ...image.Point) {
2023-07-02 06:52:14 +00:00
if this.weight == 0 {
2023-08-23 23:21:28 +00:00
if this.fill.A > 0 {
this.fillPolygon(this.fill, points...)
}
2023-09-08 20:39:58 +00:00
} else if this.closed && len(points) > 2 {
2023-08-23 23:21:28 +00:00
if this.stroke.A > 0 {
this.strokePolygon(this.stroke, points...)
}
2023-07-02 06:52:14 +00:00
} else {
2023-08-23 23:21:28 +00:00
if this.stroke.A > 0 {
this.polyLine(this.stroke, points...)
}
2023-07-02 06:52:14 +00:00
}
}
2023-08-23 01:24:38 +00:00
2023-07-05 04:44:56 +00: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 06:52:14 +00:00
2023-08-24 19:54:22 +00: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 06:52:14 +00:00
2023-08-29 19:52:24 +00:00
func (this *pen) textureObscures () bool {
return this.texture != nil && this.texture.Opaque()
}
2023-08-23 01:24:38 +00:00
func convertColor (c color.Color) xgraphics.BGRA {
2023-07-02 06:52:14 +00:00
r, g, b, a := c.RGBA()
2023-08-23 01:24:38 +00:00
return xgraphics.BGRA {
B: uint8(b >> 8),
G: uint8(g >> 8),
R: uint8(r >> 8),
A: uint8(a >> 8),
2023-07-02 06:52:14 +00:00
}
}