github.com/egonelbre/exp@v0.0.0-20240430123955-ed1d3aa93911/zerostream/op/buffer.go (about)

     1  package op
     2  
     3  import "unsafe"
     4  
     5  type Type byte
     6  
     7  const (
     8  	EOF = Type(iota)
     9  	TypeStart
    10  	TypeClose
    11  	TypeMoveTo
    12  	TypeLineTo
    13  	TypeStroke
    14  	TypeFill
    15  	TypeLineWidth
    16  	TypeTranslate
    17  	_TypeLength
    18  )
    19  
    20  type Start struct{}
    21  type Close struct{}
    22  
    23  type Translate struct{ Dx, Dy int32 }
    24  
    25  type MoveTo struct{ X, Y int32 }
    26  type LineTo struct{ X, Y int32 }
    27  
    28  type LineWidth struct{ Width int32 }
    29  type Stroke struct{}
    30  type Fill struct{}
    31  
    32  type Stream struct {
    33  	Base uintptr
    34  	Data []byte
    35  }
    36  
    37  func NewStream(max int) Stream {
    38  	data := make([]byte, max, max)
    39  	return Stream{
    40  		Base: uintptr(unsafe.Pointer(&data[0])),
    41  		Data: data,
    42  	}
    43  }
    44  
    45  func (s Stream) Iterate() *Iterator {
    46  	return &Iterator{s.Base}
    47  }
    48  
    49  type Iterator struct {
    50  	Head uintptr
    51  }
    52  
    53  func (it *Iterator) EOF() bool { return it.Type() == EOF }
    54  
    55  func (it *Iterator) Type() Type { return Type(*(*byte)(unsafe.Pointer(it.Head))) }
    56  
    57  func (it *Iterator) Start() *Start {
    58  	*(*byte)(unsafe.Pointer(it.Head)) = byte(TypeStart)
    59  	r := (*Start)(unsafe.Pointer(it.Head + 1))
    60  	it.Head += 1 + unsafe.Sizeof(Start{})
    61  	return r
    62  }
    63  
    64  func (it *Iterator) Close() *Close {
    65  	*(*byte)(unsafe.Pointer(it.Head)) = byte(TypeClose)
    66  	r := (*Close)(unsafe.Pointer(it.Head + 1))
    67  	it.Head += 1 + unsafe.Sizeof(Close{})
    68  	return r
    69  }
    70  
    71  func (it *Iterator) Translate() *Translate {
    72  	*(*byte)(unsafe.Pointer(it.Head)) = byte(TypeTranslate)
    73  	r := (*Translate)(unsafe.Pointer(it.Head + 1))
    74  	it.Head += 1 + unsafe.Sizeof(Translate{})
    75  	return r
    76  }
    77  
    78  func (it *Iterator) MoveTo() *MoveTo {
    79  	*(*byte)(unsafe.Pointer(it.Head)) = byte(TypeMoveTo)
    80  	r := (*MoveTo)(unsafe.Pointer(it.Head + 1))
    81  	it.Head += 1 + unsafe.Sizeof(MoveTo{})
    82  	return r
    83  }
    84  
    85  func (it *Iterator) LineTo() *LineTo {
    86  	*(*byte)(unsafe.Pointer(it.Head)) = byte(TypeLineTo)
    87  	r := (*LineTo)(unsafe.Pointer(it.Head + 1))
    88  	it.Head += 1 + unsafe.Sizeof(LineTo{})
    89  	return r
    90  }
    91  
    92  func (it *Iterator) LineWidth() *LineWidth {
    93  	*(*byte)(unsafe.Pointer(it.Head)) = byte(TypeLineWidth)
    94  	r := (*LineWidth)(unsafe.Pointer(it.Head + 1))
    95  	it.Head += 1 + unsafe.Sizeof(LineWidth{})
    96  	return r
    97  }
    98  
    99  func (it *Iterator) Stroke() *Stroke {
   100  	*(*byte)(unsafe.Pointer(it.Head)) = byte(TypeStroke)
   101  	r := (*Stroke)(unsafe.Pointer(it.Head + 1))
   102  	it.Head += 1 + unsafe.Sizeof(Stroke{})
   103  	return r
   104  }
   105  
   106  func (it *Iterator) Fill() *Fill {
   107  	*(*byte)(unsafe.Pointer(it.Head)) = byte(TypeFill)
   108  	r := (*Fill)(unsafe.Pointer(it.Head + 1))
   109  	it.Head += 1 + unsafe.Sizeof(Fill{})
   110  	return r
   111  }