draw2d/draw2dgl/gc.go

378 lines
10 KiB
Go
Raw Normal View History

2011-04-27 08:06:14 +00:00
package draw2dgl
import (
"errors"
2011-04-27 08:06:14 +00:00
"image"
2012-01-13 09:14:12 +00:00
"image/color"
"image/draw"
"log"
"math"
"runtime"
2015-04-16 09:51:13 +00:00
"github.com/go-gl/gl/v2.1/gl"
"github.com/golang/freetype/raster"
"github.com/golang/freetype/truetype"
2015-04-19 15:14:42 +00:00
"github.com/llgcode/draw2d"
"github.com/llgcode/draw2d/draw2dbase"
2015-08-14 20:22:01 +00:00
"github.com/llgcode/draw2d/draw2dimg"
"golang.org/x/image/font"
"golang.org/x/image/math/fixed"
2011-04-27 08:06:14 +00:00
)
func init() {
runtime.LockOSThread()
}
2015-04-16 09:59:00 +00:00
type Painter struct {
2011-04-27 08:06:14 +00:00
// The Porter-Duff composition operator.
Op draw.Op
// The 16-bit color to paint the spans.
cr, cg, cb uint8
ca uint32
colors []uint8
vertices []int32
}
const M16 uint32 = 1<<16 - 1
// Paint satisfies the Painter interface by painting ss onto an image.RGBA.
2015-04-16 09:59:00 +00:00
func (p *Painter) Paint(ss []raster.Span, done bool) {
2011-04-27 08:06:14 +00:00
//gl.Begin(gl.LINES)
sslen := len(ss)
clenrequired := sslen * 8
vlenrequired := sslen * 4
if clenrequired >= (cap(p.colors) - len(p.colors)) {
p.Flush()
if clenrequired >= cap(p.colors) {
p.vertices = make([]int32, 0, vlenrequired+(vlenrequired/2))
p.colors = make([]uint8, 0, clenrequired+(clenrequired/2))
}
}
vi := len(p.vertices)
ci := len(p.colors)
p.vertices = p.vertices[0 : vi+vlenrequired]
p.colors = p.colors[0 : ci+clenrequired]
2011-04-27 08:36:31 +00:00
var (
2011-05-18 21:23:31 +00:00
colors []uint8
vertices []int32
2011-04-27 08:36:31 +00:00
)
2011-04-27 08:06:14 +00:00
for _, s := range ss {
a := uint8((s.Alpha * p.ca / M16) >> 8)
2011-04-27 08:36:31 +00:00
colors = p.colors[ci:]
colors[0] = p.cr
colors[1] = p.cg
colors[2] = p.cb
colors[3] = a
colors[4] = p.cr
colors[5] = p.cg
colors[6] = p.cb
colors[7] = a
ci += 8
vertices = p.vertices[vi:]
vertices[0] = int32(s.X0)
vertices[1] = int32(s.Y)
vertices[2] = int32(s.X1)
vertices[3] = int32(s.Y)
vi += 4
2011-04-27 08:06:14 +00:00
}
}
2015-04-16 09:59:00 +00:00
func (p *Painter) Flush() {
2011-04-27 08:06:14 +00:00
if len(p.vertices) != 0 {
gl.EnableClientState(gl.COLOR_ARRAY)
gl.EnableClientState(gl.VERTEX_ARRAY)
gl.ColorPointer(4, gl.UNSIGNED_BYTE, 0, gl.Ptr(p.colors))
gl.VertexPointer(2, gl.INT, 0, gl.Ptr(p.vertices))
2011-04-27 08:06:14 +00:00
// draw lines
gl.DrawArrays(gl.LINES, 0, int32(len(p.vertices)/2))
2011-04-27 08:06:14 +00:00
gl.DisableClientState(gl.VERTEX_ARRAY)
gl.DisableClientState(gl.COLOR_ARRAY)
p.vertices = p.vertices[0:0]
p.colors = p.colors[0:0]
}
}
// SetColor sets the color to paint the spans.
2015-04-16 09:59:00 +00:00
func (p *Painter) SetColor(c color.Color) {
2011-04-27 08:06:14 +00:00
r, g, b, a := c.RGBA()
if a == 0 {
p.cr = 0
p.cg = 0
p.cb = 0
p.ca = a
} else {
p.cr = uint8((r * M16 / a) >> 8)
p.cg = uint8((g * M16 / a) >> 8)
p.cb = uint8((b * M16 / a) >> 8)
p.ca = a
}
}
// NewRGBAPainter creates a new RGBAPainter for the given image.
2015-04-16 09:59:00 +00:00
func NewPainter() *Painter {
p := new(Painter)
2011-04-27 08:06:14 +00:00
p.vertices = make([]int32, 0, 1024)
p.colors = make([]uint8, 0, 1024)
return p
}
type GraphicContext struct {
*draw2dbase.StackGraphicContext
2015-04-16 09:59:00 +00:00
painter *Painter
2011-04-27 08:06:14 +00:00
fillRasterizer *raster.Rasterizer
strokeRasterizer *raster.Rasterizer
glyphBuf *truetype.GlyphBuf
DPI int
2011-04-27 08:06:14 +00:00
}
2015-04-16 10:00:04 +00:00
// NewGraphicContext creates a new Graphic context from an image.
2011-04-27 08:06:14 +00:00
func NewGraphicContext(width, height int) *GraphicContext {
gc := &GraphicContext{
draw2dbase.NewStackGraphicContext(),
2015-04-16 09:59:00 +00:00
NewPainter(),
2011-04-27 08:06:14 +00:00
raster.NewRasterizer(width, height),
raster.NewRasterizer(width, height),
&truetype.GlyphBuf{},
92,
2011-04-27 08:06:14 +00:00
}
return gc
}
func (gc *GraphicContext) loadCurrentFont() (*truetype.Font, error) {
font := draw2d.GetFont(gc.Current.FontData)
if font == nil {
font = draw2d.GetFont(draw2dbase.DefaultFontData)
}
if font == nil {
return nil, errors.New("No font set, and no default font available.")
}
gc.SetFont(font)
gc.SetFontSize(gc.Current.FontSize)
return font, nil
}
func (gc *GraphicContext) drawGlyph(glyph truetype.Index, dx, dy float64) error {
if err := gc.glyphBuf.Load(gc.Current.Font, fixed.Int26_6(gc.Current.Scale), glyph, font.HintingNone); err != nil {
return err
}
e0 := 0
for _, e1 := range gc.glyphBuf.Ends {
DrawContour(gc, gc.glyphBuf.Points[e0:e1], dx, dy)
e0 = e1
}
return nil
}
// CreateStringPath creates a path from the string s at x, y, and returns the string width.
// The text is placed so that the left edge of the em square of the first character of s
// and the baseline intersect at x, y. The majority of the affected pixels will be
// above and to the right of the point, but some may be below or to the left.
// For example, drawing a string that starts with a 'J' in an italic font may
// affect pixels below and left of the point.
func (gc *GraphicContext) CreateStringPath(s string, x, y float64) float64 {
f, err := gc.loadCurrentFont()
if err != nil {
log.Println(err)
return 0.0
}
startx := x
prev, hasPrev := truetype.Index(0), false
for _, rune := range s {
index := f.Index(rune)
if hasPrev {
x += fUnitsToFloat64(f.Kern(fixed.Int26_6(gc.Current.Scale), prev, index))
}
err := gc.drawGlyph(index, x, y)
if err != nil {
log.Println(err)
return startx - x
}
x += fUnitsToFloat64(f.HMetric(fixed.Int26_6(gc.Current.Scale), index).AdvanceWidth)
prev, hasPrev = index, true
}
return x - startx
}
func (gc *GraphicContext) FillStringAt(text string, x, y float64) (width float64) {
width = gc.CreateStringPath(text, x, y)
gc.Fill()
return width
}
// GetStringBounds returns the approximate pixel bounds of the string s at x, y.
// The the left edge of the em square of the first character of s
// and the baseline intersect at 0, 0 in the returned coordinates.
// Therefore the top and left coordinates may well be negative.
func (gc *GraphicContext) GetStringBounds(s string) (left, top, right, bottom float64) {
f, err := gc.loadCurrentFont()
if err != nil {
log.Println(err)
return 0, 0, 0, 0
}
top, left, bottom, right = 10e6, 10e6, -10e6, -10e6
cursor := 0.0
prev, hasPrev := truetype.Index(0), false
for _, rune := range s {
index := f.Index(rune)
if hasPrev {
cursor += fUnitsToFloat64(f.Kern(fixed.Int26_6(gc.Current.Scale), prev, index))
}
if err := gc.glyphBuf.Load(gc.Current.Font, fixed.Int26_6(gc.Current.Scale), index, font.HintingNone); err != nil {
log.Println(err)
return 0, 0, 0, 0
}
e0 := 0
for _, e1 := range gc.glyphBuf.Ends {
ps := gc.glyphBuf.Points[e0:e1]
for _, p := range ps {
x, y := pointToF64Point(p)
top = math.Min(top, y)
bottom = math.Max(bottom, y)
left = math.Min(left, x+cursor)
right = math.Max(right, x+cursor)
}
}
cursor += fUnitsToFloat64(f.HMetric(fixed.Int26_6(gc.Current.Scale), index).AdvanceWidth)
prev, hasPrev = index, true
}
return left, top, right, bottom
}
func (gc *GraphicContext) StrokeString(text string) (width float64) {
return gc.StrokeStringAt(text, 0, 0)
}
func (gc *GraphicContext) StrokeStringAt(text string, x, y float64) (width float64) {
width = gc.CreateStringPath(text, x, y)
gc.Stroke()
return width
}
// recalc recalculates scale and bounds values from the font size, screen
// resolution and font metrics, and invalidates the glyph cache.
func (gc *GraphicContext) recalc() {
gc.Current.Scale = gc.Current.FontSize * float64(gc.DPI) * (64.0 / 72.0)
}
2011-04-27 08:06:14 +00:00
func (gc *GraphicContext) SetDPI(dpi int) {
gc.DPI = dpi
gc.recalc()
}
2011-04-27 08:06:14 +00:00
// SetFont sets the font used to draw text.
func (gc *GraphicContext) SetFont(font *truetype.Font) {
gc.Current.Font = font
}
// SetFontSize sets the font size in points (as in ``a 12 point font'').
func (gc *GraphicContext) SetFontSize(fontSize float64) {
gc.Current.FontSize = fontSize
gc.recalc()
2011-04-27 08:06:14 +00:00
}
func (gc *GraphicContext) GetDPI() int {
return gc.DPI
2011-04-27 08:06:14 +00:00
}
//TODO
2011-04-27 08:06:14 +00:00
func (gc *GraphicContext) Clear() {
panic("not implemented")
2011-04-27 08:06:14 +00:00
}
//TODO
2011-04-27 08:06:14 +00:00
func (gc *GraphicContext) ClearRect(x1, y1, x2, y2 int) {
panic("not implemented")
2011-04-27 08:06:14 +00:00
}
//TODO
2011-04-27 08:06:14 +00:00
func (gc *GraphicContext) DrawImage(img image.Image) {
panic("not implemented")
2011-04-27 08:06:14 +00:00
}
func (gc *GraphicContext) FillString(text string) (cursor float64) {
return gc.FillStringAt(text, 0, 0)
2011-04-27 08:06:14 +00:00
}
2012-01-13 09:14:12 +00:00
func (gc *GraphicContext) paint(rasterizer *raster.Rasterizer, color color.Color) {
2011-04-27 08:06:14 +00:00
gc.painter.SetColor(color)
rasterizer.Rasterize(gc.painter)
rasterizer.Clear()
gc.painter.Flush()
2015-04-29 09:16:03 +00:00
gc.Current.Path.Clear()
2011-04-27 08:06:14 +00:00
}
func (gc *GraphicContext) Stroke(paths ...*draw2d.Path) {
2011-04-27 08:06:14 +00:00
paths = append(paths, gc.Current.Path)
gc.strokeRasterizer.UseNonZeroWinding = true
2015-08-14 20:22:01 +00:00
stroker := draw2dbase.NewLineStroker(gc.Current.Cap, gc.Current.Join, draw2dbase.Transformer{Tr: gc.Current.Tr, Flattener: draw2dimg.FtLineBuilder{Adder: gc.strokeRasterizer}})
2011-04-27 08:06:14 +00:00
stroker.HalfLineWidth = gc.Current.LineWidth / 2
2015-04-29 09:16:03 +00:00
2015-04-29 15:16:15 +00:00
var liner draw2dbase.Flattener
2011-04-27 08:06:14 +00:00
if gc.Current.Dash != nil && len(gc.Current.Dash) > 0 {
liner = draw2dbase.NewDashConverter(gc.Current.Dash, gc.Current.DashOffset, stroker)
2011-04-27 08:06:14 +00:00
} else {
2015-04-29 09:16:03 +00:00
liner = stroker
}
for _, p := range paths {
2015-04-29 15:16:15 +00:00
draw2dbase.Flatten(p, liner, gc.Current.Tr.GetScale())
2011-04-27 08:06:14 +00:00
}
gc.paint(gc.strokeRasterizer, gc.Current.StrokeColor)
}
func (gc *GraphicContext) Fill(paths ...*draw2d.Path) {
2011-04-27 08:06:14 +00:00
paths = append(paths, gc.Current.Path)
gc.fillRasterizer.UseNonZeroWinding = useNonZeroWinding(gc.Current.FillRule)
2011-04-27 08:06:14 +00:00
/**** first method ****/
2015-08-14 20:22:01 +00:00
flattener := draw2dbase.Transformer{Tr: gc.Current.Tr, Flattener: draw2dimg.FtLineBuilder{Adder: gc.fillRasterizer}}
2015-04-29 09:16:03 +00:00
for _, p := range paths {
2015-04-29 15:16:15 +00:00
draw2dbase.Flatten(p, flattener, gc.Current.Tr.GetScale())
2015-04-29 09:16:03 +00:00
}
2011-04-27 08:06:14 +00:00
gc.paint(gc.fillRasterizer, gc.Current.FillColor)
}
2012-01-13 09:14:12 +00:00
func (gc *GraphicContext) FillStroke(paths ...*draw2d.Path) {
2015-04-29 09:16:03 +00:00
paths = append(paths, gc.Current.Path)
gc.fillRasterizer.UseNonZeroWinding = useNonZeroWinding(gc.Current.FillRule)
2011-04-27 08:06:14 +00:00
gc.strokeRasterizer.UseNonZeroWinding = true
2015-08-14 20:38:18 +00:00
flattener := draw2dbase.Transformer{Tr: gc.Current.Tr, Flattener: draw2dimg.FtLineBuilder{Adder: gc.fillRasterizer}}
2011-04-27 08:06:14 +00:00
2015-08-14 20:38:18 +00:00
stroker := draw2dbase.NewLineStroker(gc.Current.Cap, gc.Current.Join, draw2dbase.Transformer{Tr: gc.Current.Tr, Flattener: draw2dimg.FtLineBuilder{Adder: gc.strokeRasterizer}})
2011-04-27 08:06:14 +00:00
stroker.HalfLineWidth = gc.Current.LineWidth / 2
2015-04-29 15:16:15 +00:00
var liner draw2dbase.Flattener
2015-04-29 09:16:03 +00:00
if gc.Current.Dash != nil && len(gc.Current.Dash) > 0 {
liner = draw2dbase.NewDashConverter(gc.Current.Dash, gc.Current.DashOffset, stroker)
2015-04-29 09:16:03 +00:00
} else {
liner = stroker
}
2015-08-14 20:38:18 +00:00
demux := draw2dbase.DemuxFlattener{Flatteners: []draw2dbase.Flattener{flattener, liner}}
2015-04-29 09:16:03 +00:00
for _, p := range paths {
2015-04-29 15:16:15 +00:00
draw2dbase.Flatten(p, demux, gc.Current.Tr.GetScale())
2015-04-29 09:16:03 +00:00
}
2011-04-27 08:06:14 +00:00
2015-04-29 09:16:03 +00:00
// Fill
2011-04-27 08:06:14 +00:00
gc.paint(gc.fillRasterizer, gc.Current.FillColor)
2015-04-29 09:16:03 +00:00
// Stroke
2011-04-27 08:06:14 +00:00
gc.paint(gc.strokeRasterizer, gc.Current.StrokeColor)
}
func useNonZeroWinding(f draw2d.FillRule) bool {
switch f {
case draw2d.FillRuleEvenOdd:
return false
case draw2d.FillRuleWinding:
return true
}
return false
}