...

Source file src/gonum.org/v1/plot/vg/tee.go

Documentation: gonum.org/v1/plot/vg

     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 vg
     6  
     7  import (
     8  	"image"
     9  	"image/color"
    10  
    11  	"gonum.org/v1/plot/font"
    12  )
    13  
    14  // MultiCanvas creates a canvas that duplicates its drawing operations to all
    15  // the provided canvases, similar to the Unix tee(1) command.
    16  //
    17  // Each drawing operation is sent to each listed canvas, one at a time.
    18  func MultiCanvas(cs ...Canvas) Canvas {
    19  	return teeCanvas{cs}
    20  }
    21  
    22  type teeCanvas struct {
    23  	cs []Canvas
    24  }
    25  
    26  // SetLineWidth sets the width of stroked paths.
    27  // If the width is not positive then stroked lines
    28  // are not drawn.
    29  func (tee teeCanvas) SetLineWidth(w Length) {
    30  	for _, c := range tee.cs {
    31  		c.SetLineWidth(w)
    32  	}
    33  }
    34  
    35  // SetLineDash sets the dash pattern for lines.
    36  // The pattern slice specifies the lengths of
    37  // alternating dashes and gaps, and the offset
    38  // specifies the distance into the dash pattern
    39  // to start the dash.
    40  func (tee teeCanvas) SetLineDash(pattern []Length, offset Length) {
    41  	for _, c := range tee.cs {
    42  		c.SetLineDash(pattern, offset)
    43  	}
    44  }
    45  
    46  // SetColor sets the current drawing color.
    47  // Note that fill color and stroke color are
    48  // the same, so if you want different fill
    49  // and stroke colors then you must set a color,
    50  // draw fills, set a new color and then draw lines.
    51  func (tee teeCanvas) SetColor(c color.Color) {
    52  	for _, canvas := range tee.cs {
    53  		canvas.SetColor(c)
    54  	}
    55  }
    56  
    57  // Rotate applies a rotation transform to the context.
    58  // The parameter is specified in radians.
    59  func (tee teeCanvas) Rotate(rad float64) {
    60  	for _, c := range tee.cs {
    61  		c.Rotate(rad)
    62  	}
    63  }
    64  
    65  // Translate applies a translational transform to the context.
    66  func (tee teeCanvas) Translate(pt Point) {
    67  	for _, c := range tee.cs {
    68  		c.Translate(pt)
    69  	}
    70  }
    71  
    72  // Scale applies a scaling transform to the context.
    73  func (tee teeCanvas) Scale(x, y float64) {
    74  	for _, c := range tee.cs {
    75  		c.Scale(x, y)
    76  	}
    77  }
    78  
    79  // Push saves the current line width, the
    80  // current dash pattern, the current
    81  // transforms, and the current color
    82  // onto a stack so that the state can later
    83  // be restored by calling Pop().
    84  func (tee teeCanvas) Push() {
    85  	for _, c := range tee.cs {
    86  		c.Push()
    87  	}
    88  }
    89  
    90  // Pop restores the context saved by the
    91  // corresponding call to Push().
    92  func (tee teeCanvas) Pop() {
    93  	for _, c := range tee.cs {
    94  		c.Pop()
    95  	}
    96  }
    97  
    98  // Stroke strokes the given path.
    99  func (tee teeCanvas) Stroke(p Path) {
   100  	for _, c := range tee.cs {
   101  		c.Stroke(p)
   102  	}
   103  }
   104  
   105  // Fill fills the given path.
   106  func (tee teeCanvas) Fill(p Path) {
   107  	for _, c := range tee.cs {
   108  		c.Fill(p)
   109  	}
   110  }
   111  
   112  // FillString fills in text at the specified
   113  // location using the given font.
   114  // If the font size is zero, the text is not drawn.
   115  func (tee teeCanvas) FillString(f font.Face, pt Point, text string) {
   116  	for _, c := range tee.cs {
   117  		c.FillString(f, pt, text)
   118  	}
   119  }
   120  
   121  // DrawImage draws the image, scaled to fit
   122  // the destination rectangle.
   123  func (tee teeCanvas) DrawImage(rect Rectangle, img image.Image) {
   124  	for _, c := range tee.cs {
   125  		c.DrawImage(rect, img)
   126  	}
   127  }
   128  
   129  var _ Canvas = (*teeCanvas)(nil)
   130  

View as plain text