github.com/andybalholm/giopdf@v0.0.0-20220317170119-aad9a095ad48/state.go (about)

     1  package giopdf
     2  
     3  import (
     4  	"image/color"
     5  
     6  	"gioui.org/f32"
     7  	"gioui.org/op"
     8  	"gioui.org/op/clip"
     9  )
    10  
    11  type graphicsState struct {
    12  	fillColor   color.NRGBA
    13  	strokeColor color.NRGBA
    14  
    15  	lineWidth  float32
    16  	lineCap    int
    17  	lineJoin   int
    18  	miterLimit float32
    19  
    20  	dashes    []float32
    21  	dashPhase float32
    22  
    23  	font              Font
    24  	fontSize          float32
    25  	hScale            float32
    26  	textMatrix        f32.Affine2D
    27  	lineMatrix        f32.Affine2D
    28  	textRenderingMode int
    29  
    30  	transforms    []op.TransformStack
    31  	clippingPaths []clip.Stack
    32  }
    33  
    34  func rgbColor(r, g, b float32, alpha byte) color.NRGBA {
    35  	return color.NRGBA{
    36  		R: uint8(r * 255),
    37  		G: uint8(g * 255),
    38  		B: uint8(b * 255),
    39  		A: alpha,
    40  	}
    41  }
    42  
    43  func gray(g float32, alpha byte) color.NRGBA {
    44  	return color.NRGBA{
    45  		R: uint8(g * 255),
    46  		G: uint8(g * 255),
    47  		B: uint8(g * 255),
    48  		A: alpha,
    49  	}
    50  }
    51  
    52  // SetRGBStrokeColor sets the color to be used for stroking (outlining) shapes.
    53  // The RGB values must be in the range from 0 to 1.
    54  func (s *graphicsState) SetRGBStrokeColor(r, g, b float32) {
    55  	s.strokeColor = rgbColor(r, g, b, s.strokeColor.A)
    56  }
    57  
    58  // SetRGBStrokeColor sets the color to be used for filling shapes.
    59  // The RGB values must be in the range from 0 to 1.
    60  func (s *graphicsState) SetRGBFillColor(r, g, b float32) {
    61  	s.fillColor = rgbColor(r, g, b, s.fillColor.A)
    62  }
    63  
    64  // SetFillGray sets the fill color to a gray in the range from 0 (black) to
    65  // 1 (white).
    66  func (s *graphicsState) SetFillGray(g float32) {
    67  	s.fillColor = gray(g, s.fillColor.A)
    68  }
    69  
    70  // SetStrokeGray sets the stroke color to a gray in the range from 0 (black) to
    71  // 1 (white).
    72  func (s *graphicsState) SetStrokeGray(g float32) {
    73  	s.strokeColor = gray(g, s.strokeColor.A)
    74  }
    75  
    76  // SetLineWidth sets the width of the lines to use for stroking (outlining)
    77  // shapes.
    78  func (s *graphicsState) SetLineWidth(w float32) {
    79  	s.lineWidth = w
    80  }
    81  
    82  // SetLineCap sets the style for the caps at the end of lines:
    83  // 0 (butt cap), 1 (round cap), or 2 (square cap).
    84  func (s *graphicsState) SetLineCap(c int) {
    85  	s.lineCap = c
    86  }
    87  
    88  // SetLineJoin sets the style for the joins at corners of stroked paths:
    89  // 0 (miter join), 1 (round join), or 2 (bevel join).
    90  func (s *graphicsState) SetLineJoin(j int) {
    91  	s.lineJoin = j
    92  }
    93  
    94  // SetMiterLimit sets the limit for the length of a mitered join.
    95  func (s *graphicsState) SetMiterLimit(m float32) {
    96  	s.miterLimit = m
    97  }
    98  
    99  // SetDash sets the dash pattern for stroking paths.
   100  func (s *graphicsState) SetDash(lengths []float32, phase float32) {
   101  	s.dashes = lengths
   102  	s.dashPhase = phase
   103  }
   104  
   105  // SetStrokeAlpha sets the opacity for stroking paths.
   106  func (s *graphicsState) SetStrokeAlpha(alpha float32) {
   107  	s.strokeColor.A = uint8(alpha * 255)
   108  }
   109  
   110  // SetFillAlpha sets the opacity for filling paths.
   111  func (s *graphicsState) SetFillAlpha(alpha float32) {
   112  	s.fillColor.A = uint8(alpha * 255)
   113  }
   114  
   115  // SetFont sets the font and size for text.
   116  func (s *graphicsState) SetFont(f Font, size float32) {
   117  	s.font = f
   118  	s.fontSize = size
   119  }
   120  
   121  // SetTextMatrix sets the text matrix and the text line matrix.
   122  func (s *graphicsState) SetTextMatrix(a, b, c, d, e, f float32) {
   123  	s.lineMatrix = f32.NewAffine2D(a, c, e, b, d, f)
   124  	s.textMatrix = s.lineMatrix
   125  }
   126  
   127  // TextMove starts a new line of text offset by x and y from the start of the
   128  // current line.
   129  func (s *graphicsState) TextMove(x, y float32) {
   130  	s.lineMatrix = f32.NewAffine2D(1, 0, x, 0, 1, y).Mul(s.lineMatrix)
   131  	s.textMatrix = s.lineMatrix
   132  }
   133  
   134  // SetHScale sets the horizontal scaling percent for text.
   135  func (s *graphicsState) SetHScale(scale float32) {
   136  	s.hScale = scale
   137  }
   138  
   139  // SetTextRendering sets the text rendering mode.
   140  func (s *graphicsState) SetTextRendering(mode int) {
   141  	s.textRenderingMode = mode
   142  }