clip.go 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. // SPDX-License-Identifier: Unlicense OR MIT
  2. package clip
  3. import (
  4. "encoding/binary"
  5. "hash/maphash"
  6. "image"
  7. "math"
  8. "gioui.org/f32"
  9. f32internal "gioui.org/internal/f32"
  10. "gioui.org/internal/ops"
  11. "gioui.org/internal/scene"
  12. "gioui.org/internal/stroke"
  13. "gioui.org/op"
  14. )
  15. // Op represents a clip area. Op intersects the current clip area with
  16. // itself.
  17. type Op struct {
  18. path PathSpec
  19. outline bool
  20. width float32
  21. }
  22. // Stack represents an Op pushed on the clip stack.
  23. type Stack struct {
  24. ops *ops.Ops
  25. id ops.StackID
  26. macroID uint32
  27. }
  28. var pathSeed maphash.Seed
  29. func init() {
  30. pathSeed = maphash.MakeSeed()
  31. }
  32. // Push saves the current clip state on the stack and updates the current
  33. // state to the intersection of the current p.
  34. func (p Op) Push(o *op.Ops) Stack {
  35. id, macroID := ops.PushOp(&o.Internal, ops.ClipStack)
  36. p.add(o)
  37. return Stack{ops: &o.Internal, id: id, macroID: macroID}
  38. }
  39. func (p Op) add(o *op.Ops) {
  40. path := p.path
  41. if !path.hasSegments && p.width > 0 {
  42. switch p.path.shape {
  43. case ops.Rect:
  44. b := f32internal.FRect(path.bounds)
  45. var rect Path
  46. rect.Begin(o)
  47. rect.MoveTo(b.Min)
  48. rect.LineTo(f32.Pt(b.Max.X, b.Min.Y))
  49. rect.LineTo(b.Max)
  50. rect.LineTo(f32.Pt(b.Min.X, b.Max.Y))
  51. rect.Close()
  52. path = rect.End()
  53. case ops.Path:
  54. // Nothing to do.
  55. default:
  56. panic("invalid empty path for shape")
  57. }
  58. }
  59. bo := binary.LittleEndian
  60. if path.hasSegments {
  61. data := ops.Write(&o.Internal, ops.TypePathLen)
  62. data[0] = byte(ops.TypePath)
  63. bo.PutUint64(data[1:], path.hash)
  64. path.spec.Add(o)
  65. }
  66. bounds := path.bounds
  67. if p.width > 0 {
  68. // Expand bounds to cover stroke.
  69. half := int(p.width*.5 + .5)
  70. bounds.Min.X -= half
  71. bounds.Min.Y -= half
  72. bounds.Max.X += half
  73. bounds.Max.Y += half
  74. data := ops.Write(&o.Internal, ops.TypeStrokeLen)
  75. data[0] = byte(ops.TypeStroke)
  76. bo := binary.LittleEndian
  77. bo.PutUint32(data[1:], math.Float32bits(p.width))
  78. }
  79. data := ops.Write(&o.Internal, ops.TypeClipLen)
  80. data[0] = byte(ops.TypeClip)
  81. bo.PutUint32(data[1:], uint32(bounds.Min.X))
  82. bo.PutUint32(data[5:], uint32(bounds.Min.Y))
  83. bo.PutUint32(data[9:], uint32(bounds.Max.X))
  84. bo.PutUint32(data[13:], uint32(bounds.Max.Y))
  85. if p.outline {
  86. data[17] = byte(1)
  87. }
  88. data[18] = byte(path.shape)
  89. }
  90. func (s Stack) Pop() {
  91. ops.PopOp(s.ops, ops.ClipStack, s.id, s.macroID)
  92. data := ops.Write(s.ops, ops.TypePopClipLen)
  93. data[0] = byte(ops.TypePopClip)
  94. }
  95. type PathSpec struct {
  96. spec op.CallOp
  97. // hasSegments tracks whether there are any segments in the path.
  98. hasSegments bool
  99. bounds image.Rectangle
  100. shape ops.Shape
  101. hash uint64
  102. }
  103. // Path constructs a Op clip path described by lines and
  104. // Bézier curves, where drawing outside the Path is discarded.
  105. // The inside-ness of a pixel is determines by the non-zero winding rule,
  106. // similar to the SVG rule of the same name.
  107. //
  108. // Path generates no garbage and can be used for dynamic paths; path
  109. // data is stored directly in the Ops list supplied to Begin.
  110. type Path struct {
  111. ops *ops.Ops
  112. contour int
  113. pen f32.Point
  114. macro op.MacroOp
  115. start f32.Point
  116. hasSegments bool
  117. bounds f32internal.Rectangle
  118. hash maphash.Hash
  119. }
  120. // Pos returns the current pen position.
  121. func (p *Path) Pos() f32.Point { return p.pen }
  122. // Begin the path, storing the path data and final Op into ops.
  123. //
  124. // Caller must also call End to finish the drawing.
  125. // Forgetting to call it will result in a "panic: cannot mix multi ops with single ones".
  126. func (p *Path) Begin(o *op.Ops) {
  127. *p = Path{
  128. ops: &o.Internal,
  129. macro: op.Record(o),
  130. contour: 1,
  131. }
  132. p.hash.SetSeed(pathSeed)
  133. ops.BeginMulti(p.ops)
  134. data := ops.WriteMulti(p.ops, ops.TypeAuxLen)
  135. data[0] = byte(ops.TypeAux)
  136. }
  137. // End returns a PathSpec ready to use in clipping operations.
  138. func (p *Path) End() PathSpec {
  139. p.gap()
  140. c := p.macro.Stop()
  141. ops.EndMulti(p.ops)
  142. return PathSpec{
  143. spec: c,
  144. hasSegments: p.hasSegments,
  145. bounds: p.bounds.Round(),
  146. hash: p.hash.Sum64(),
  147. }
  148. }
  149. // Move moves the pen by the amount specified by delta.
  150. func (p *Path) Move(delta f32.Point) {
  151. to := delta.Add(p.pen)
  152. p.MoveTo(to)
  153. }
  154. // MoveTo moves the pen to the specified absolute coordinate.
  155. func (p *Path) MoveTo(to f32.Point) {
  156. if p.pen == to {
  157. return
  158. }
  159. p.gap()
  160. p.end()
  161. p.pen = to
  162. p.start = to
  163. }
  164. func (p *Path) gap() {
  165. if p.pen != p.start {
  166. // A closed contour starts and ends in the same point.
  167. // This move creates a gap in the contour, register it.
  168. data := ops.WriteMulti(p.ops, scene.CommandSize+4)
  169. bo := binary.LittleEndian
  170. bo.PutUint32(data[0:], uint32(p.contour))
  171. p.cmd(data[4:], scene.Gap(p.pen, p.start))
  172. }
  173. }
  174. // end completes the current contour.
  175. func (p *Path) end() {
  176. p.contour++
  177. }
  178. // Line moves the pen by the amount specified by delta, recording a line.
  179. func (p *Path) Line(delta f32.Point) {
  180. to := delta.Add(p.pen)
  181. p.LineTo(to)
  182. }
  183. // LineTo moves the pen to the absolute point specified, recording a line.
  184. func (p *Path) LineTo(to f32.Point) {
  185. if to == p.pen {
  186. return
  187. }
  188. data := ops.WriteMulti(p.ops, scene.CommandSize+4)
  189. bo := binary.LittleEndian
  190. bo.PutUint32(data[0:], uint32(p.contour))
  191. p.cmd(data[4:], scene.Line(p.pen, to))
  192. p.pen = to
  193. p.expand(to)
  194. }
  195. func (p *Path) cmd(data []byte, c scene.Command) {
  196. ops.EncodeCommand(data, c)
  197. p.hash.Write(data)
  198. }
  199. func (p *Path) expand(pt f32.Point) {
  200. if !p.hasSegments {
  201. p.hasSegments = true
  202. p.bounds = f32internal.Rectangle{Min: pt, Max: pt}
  203. } else {
  204. b := p.bounds
  205. if pt.X < b.Min.X {
  206. b.Min.X = pt.X
  207. }
  208. if pt.Y < b.Min.Y {
  209. b.Min.Y = pt.Y
  210. }
  211. if pt.X > b.Max.X {
  212. b.Max.X = pt.X
  213. }
  214. if pt.Y > b.Max.Y {
  215. b.Max.Y = pt.Y
  216. }
  217. p.bounds = b
  218. }
  219. }
  220. // Quad records a quadratic Bézier from the pen to end
  221. // with the control point ctrl.
  222. func (p *Path) Quad(ctrl, to f32.Point) {
  223. ctrl = ctrl.Add(p.pen)
  224. to = to.Add(p.pen)
  225. p.QuadTo(ctrl, to)
  226. }
  227. // QuadTo records a quadratic Bézier from the pen to end
  228. // with the control point ctrl, with absolute coordinates.
  229. func (p *Path) QuadTo(ctrl, to f32.Point) {
  230. if ctrl == p.pen && to == p.pen {
  231. return
  232. }
  233. data := ops.WriteMulti(p.ops, scene.CommandSize+4)
  234. bo := binary.LittleEndian
  235. bo.PutUint32(data[0:], uint32(p.contour))
  236. p.cmd(data[4:], scene.Quad(p.pen, ctrl, to))
  237. p.pen = to
  238. p.expand(ctrl)
  239. p.expand(to)
  240. }
  241. // ArcTo adds an elliptical arc to the path. The implied ellipse is defined
  242. // by its focus points f1 and f2.
  243. // The arc starts in the current point and ends angle radians along the ellipse boundary.
  244. // The sign of angle determines the direction; positive being counter-clockwise,
  245. // negative clockwise.
  246. func (p *Path) ArcTo(f1, f2 f32.Point, angle float32) {
  247. m, segments := stroke.ArcTransform(p.pen, f1, f2, angle)
  248. for i := 0; i < segments; i++ {
  249. p0 := p.pen
  250. p1 := m.Transform(p0)
  251. p2 := m.Transform(p1)
  252. ctl := p1.Mul(2).Sub(p0.Add(p2).Mul(.5))
  253. p.QuadTo(ctl, p2)
  254. }
  255. }
  256. // Arc is like ArcTo where f1 and f2 are relative to the current position.
  257. func (p *Path) Arc(f1, f2 f32.Point, angle float32) {
  258. f1 = f1.Add(p.pen)
  259. f2 = f2.Add(p.pen)
  260. p.ArcTo(f1, f2, angle)
  261. }
  262. // Cube records a cubic Bézier from the pen through
  263. // two control points ending in to.
  264. func (p *Path) Cube(ctrl0, ctrl1, to f32.Point) {
  265. p.CubeTo(p.pen.Add(ctrl0), p.pen.Add(ctrl1), p.pen.Add(to))
  266. }
  267. // CubeTo records a cubic Bézier from the pen through
  268. // two control points ending in to, with absolute coordinates.
  269. func (p *Path) CubeTo(ctrl0, ctrl1, to f32.Point) {
  270. if ctrl0 == p.pen && ctrl1 == p.pen && to == p.pen {
  271. return
  272. }
  273. data := ops.WriteMulti(p.ops, scene.CommandSize+4)
  274. bo := binary.LittleEndian
  275. bo.PutUint32(data[0:], uint32(p.contour))
  276. p.cmd(data[4:], scene.Cubic(p.pen, ctrl0, ctrl1, to))
  277. p.pen = to
  278. p.expand(ctrl0)
  279. p.expand(ctrl1)
  280. p.expand(to)
  281. }
  282. // Close closes the path contour.
  283. func (p *Path) Close() {
  284. if p.pen != p.start {
  285. p.LineTo(p.start)
  286. }
  287. p.end()
  288. }
  289. // Stroke represents a stroked path.
  290. type Stroke struct {
  291. Path PathSpec
  292. // Width of the stroked path.
  293. Width float32
  294. }
  295. // Op returns a clip operation representing the stroke.
  296. func (s Stroke) Op() Op {
  297. return Op{
  298. path: s.Path,
  299. width: s.Width,
  300. }
  301. }
  302. // Outline represents the area inside of a path, according to the
  303. // non-zero winding rule.
  304. type Outline struct {
  305. Path PathSpec
  306. }
  307. // Op returns a clip operation representing the outline.
  308. func (o Outline) Op() Op {
  309. return Op{
  310. path: o.Path,
  311. outline: true,
  312. }
  313. }