...

Source file src/gonum.org/v1/plot/vg/vggio/context.go

Documentation: gonum.org/v1/plot/vg/vggio

     1  // Copyright ©2020 The Gonum Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package vggio // import "gonum.org/v1/plot/vg/vggio"
     6  
     7  import (
     8  	"image/color"
     9  
    10  	"gioui.org/f32"
    11  	"gioui.org/op"
    12  
    13  	"gonum.org/v1/plot/vg"
    14  )
    15  
    16  // ctxops holds a stack of Gio operations.
    17  type ctxops struct {
    18  	ops *op.Ops   // ops is the Gio operations vggio is drawing on.
    19  	ctx []context // ctx is the stack of Gio operations vggio is manipulating.
    20  
    21  	w   vg.Length // w is the canvas window width.
    22  	h   vg.Length // h is the canvas window height.
    23  	dpi float64   // dpi is the canvas window dots per inch resolution.
    24  }
    25  
    26  // context holds state about the Gio backing store.
    27  // context provides methods to translate between Gio values (reference frame,
    28  // operations and stack) and their plot/vg counterparts.
    29  type context struct {
    30  	color   color.Color // color is the current color.
    31  	linew   vg.Length   // linew is the current line width.
    32  	pattern []vg.Length // pattern is the current line style.
    33  	offset  vg.Length   // offset is the current line style.
    34  
    35  	trans op.TransformStack // trans is the Gio transform context stack.
    36  }
    37  
    38  func (ctx *ctxops) cur() *context {
    39  	return &ctx.ctx[len(ctx.ctx)-1]
    40  }
    41  
    42  func (ctx *ctxops) push() {
    43  	ctx.ctx = append(ctx.ctx, *ctx.cur())
    44  	ctx.cur().trans = op.TransformOp{}.Push(ctx.ops)
    45  }
    46  
    47  func (ctx *ctxops) pop() {
    48  	ctx.cur().trans.Pop()
    49  	ctx.ctx = ctx.ctx[:len(ctx.ctx)-1]
    50  }
    51  
    52  func (ctx *ctxops) scale(x, y float64) {
    53  	op.Affine(f32.Affine2D{}.Scale(
    54  		f32.Pt(0, 0),
    55  		f32.Pt(float32(x), float32(y)),
    56  	)).Add(ctx.ops)
    57  }
    58  
    59  func (ctx *ctxops) translate(x, y float64) {
    60  	op.Affine(f32.Affine2D{}.Offset(
    61  		f32.Pt(float32(x), float32(y)),
    62  	)).Add(ctx.ops)
    63  }
    64  
    65  func (ctx *ctxops) rotate(rad float64) {
    66  	op.Affine(f32.Affine2D{}.Rotate(
    67  		f32.Pt(0, 0), float32(rad),
    68  	)).Add(ctx.ops)
    69  }
    70  
    71  func (ctx *ctxops) invertY() {
    72  	ctx.translate(0, ctx.h.Dots(ctx.dpi))
    73  	ctx.scale(1, -1)
    74  }
    75  
    76  func (ctx *ctxops) pt32(p vg.Point) f32.Point {
    77  	return f32.Point{
    78  		X: float32(p.X.Dots(ctx.dpi)),
    79  		Y: float32(p.Y.Dots(ctx.dpi)),
    80  	}
    81  }
    82  

View as plain text