forked from darmie/nanovgo-gles
-
Notifications
You must be signed in to change notification settings - Fork 2
/
nanovgo.go
1554 lines (1391 loc) · 46 KB
/
nanovgo.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package nanovgo
import (
"bytes"
"image"
_ "image/jpeg" // to read jpeg
_ "image/png" // to read png
"log"
"os"
"github.com/shibukawa/nanovgo/fontstashmini"
)
// Context is an entry point object to use NanoVGo API and created by NewContext() function.
//
// State Handling
//
// NanoVG contains state which represents how paths will be rendered.
// The state contains transform, fill and stroke styles, text and font styles,
// and scissor clipping.
//
// Render styles
//
// Fill and stroke render style can be either a solid color or a paint which is a gradient or a pattern.
// Solid color is simply defined as a color value, different kinds of paints can be created
// using LinearGradient(), BoxGradient(), RadialGradient() and ImagePattern().
//
// Current render style can be saved and restored using Save() and Restore().
//
// Transforms
//
// The paths, gradients, patterns and scissor region are transformed by an transformation
// matrix at the time when they are passed to the API.
// The current transformation matrix is a affine matrix:
// [sx kx tx]
// [ky sy ty]
// [ 0 0 1]
// Where: sx,sy define scaling, kx,ky skewing, and tx,ty translation.
// The last row is assumed to be 0,0,1 and is not stored.
//
// Apart from ResetTransform(), each transformation function first creates
// specific transformation matrix and pre-multiplies the current transformation by it.
//
// Current coordinate system (transformation) can be saved and restored using Save() and Restore().
//
// Images
//
// NanoVG allows you to load jpg, png, psd, tga, pic and gif files to be used for rendering.
// In addition you can upload your own image. The image loading is provided by stb_image.
// The parameter imageFlags is combination of flags defined in ImageFlags.
//
// Paints
//
// NanoVG supports four types of paints: linear gradient, box gradient, radial gradient and image pattern.
// These can be used as paints for strokes and fills.
//
// Scissoring
//
// Scissoring allows you to clip the rendering into a rectangle. This is useful for various
// user interface cases like rendering a text edit or a timeline.
//
// Paths
//
// Drawing a new shape starts with BeginPath(), it clears all the currently defined paths.
// Then you define one or more paths and sub-paths which describe the shape. The are functions
// to draw common shapes like rectangles and circles, and lower level step-by-step functions,
// which allow to define a path curve by curve.
//
// NanoVG uses even-odd fill rule to draw the shapes. Solid shapes should have counter clockwise
// winding and holes should have counter clockwise order. To specify winding of a path you can
// call PathWinding(). This is useful especially for the common shapes, which are drawn CCW.
//
// Finally you can fill the path using current fill style by calling Fill(), and stroke it
// with current stroke style by calling Stroke().
//
// The curve segments and sub-paths are transformed by the current transform.
//
// Text
//
// NanoVG allows you to load .ttf files and use the font to render text.
//
// The appearance of the text can be defined by setting the current text style
// and by specifying the fill color. Common text and font settings such as
// font size, letter spacing and text align are supported. Font blur allows you
// to create simple text effects such as drop shadows.
//
// At render time the font face can be set based on the font handles or name.
//
// Font measure functions return values in local space, the calculations are
// carried in the same resolution as the final rendering. This is done because
// the text glyph positions are snapped to the nearest pixels sharp rendering.
//
// The local space means that values are not rotated or scale as per the current
// transformation. For example if you set font size to 12, which would mean that
// line height is 16, then regardless of the current scaling and rotation, the
// returned line height is always 16. Some measures may vary because of the scaling
// since aforementioned pixel snapping.
//
// While this may sound a little odd, the setup allows you to always render the
// same way regardless of scaling. I.e. following works regardless of scaling:
//
// vg.TextBounds(x, y, "Text me up.", bounds)
// vg.BeginPath()
// vg.RoundedRect(bounds[0],bounds[1], bounds[2]-bounds[0], bounds[3]-bounds[1])
// vg.Fill()
//
// Note: currently only solid color fill is supported for text.
type Context struct {
params nvgParams
commands []float32
commandX float32
commandY float32
states []nvgState
cache nvgPathCache
tessTol float32
distTol float32
fringeWidth float32
devicePxRatio float32
fs *fontstashmini.FontStash
fontImages []int32
fontImageIdx int32
drawCallCount int32
fillTriCount int32
strokeTriCount int32
textTriCount int32
}
// Delete is called when tearing down NanoVGo context
func (c *Context) Delete() {
for i, fontImage := range c.fontImages {
if fontImage != 0 {
c.DeleteImage(fontImage)
c.fontImages[i] = 0
}
}
c.params.renderDelete()
}
// BeginFrame begins drawing a new frame
// Calls to NanoVGo drawing API should be wrapped in Context.BeginFrame() & Context.EndFrame()
// Context.BeginFrame() defines the size of the window to render to in relation currently
// set viewport (i.e. glViewport on GL backends). Device pixel ration allows to
// control the rendering on Hi-DPI devices.
// For example, GLFW returns two dimension for an opened window: window size and
// frame buffer size. In that case you would set windowWidth/Height to the window size
// devicePixelRatio to: frameBufferWidth / windowWidth.
func (c *Context) BeginFrame(windowWidth, windowHeight int32, devicePixelRatio float32) {
c.states = c.states[:0]
c.Save()
c.Reset()
c.setDevicePixelRatio(devicePixelRatio)
c.params.renderViewport(windowWidth, windowHeight)
c.drawCallCount = 0
c.fillTriCount = 0
c.strokeTriCount = 0
c.textTriCount = 0
}
// CancelFrame cancels drawing the current frame.
func (c *Context) CancelFrame() {
c.params.renderCancel()
}
// EndFrame ends drawing flushing remaining render state.
func (c *Context) EndFrame() {
c.params.renderFlush()
if c.fontImageIdx != 0 {
fontImage := c.fontImages[c.fontImageIdx]
if fontImage == 0 {
return
}
iw, ih, _ := c.ImageSize(fontImage)
j := 0
for i := 0; int32(i) < c.fontImageIdx; i++ {
nw, nh, _ := c.ImageSize(c.fontImages[i])
if nw < iw || nh < ih {
c.DeleteImage(c.fontImages[i])
} else {
c.fontImages[j] = c.fontImages[i]
j++
}
}
// make current font image to first
c.fontImages[j] = c.fontImages[0]
j++
c.fontImages[0] = fontImage
c.fontImageIdx = 0
// clear all image after j
for i := j; i < nvgMaxFontImages; i++ {
c.fontImages[i] = 0
}
}
}
// Save pushes and saves the current render state into a state stack.
// A matching Restore() must be used to restore the state.
func (c *Context) Save() {
if len(c.states) >= nvgMaxStates {
return
}
if len(c.states) > 0 {
c.states = append(c.states, c.states[len(c.states)-1])
} else {
c.states = append(c.states, nvgState{})
}
}
// Restore pops and restores current render state.
func (c *Context) Restore() {
nStates := len(c.states)
if nStates > 1 {
c.states = c.states[:nStates-1]
}
}
// Block makes Save/Restore block.
func (c *Context) Block(block func()) {
c.Save()
defer c.Restore()
block()
}
// Reset resets current render state to default values. Does not affect the render state stack.
func (c *Context) Reset() {
c.getState().reset()
}
// SetStrokeWidth sets the stroke width of the stroke style.
func (c *Context) SetStrokeWidth(width float32) {
c.getState().strokeWidth = width
}
// StrokeWidth gets the stroke width of the stroke style.
func (c *Context) StrokeWidth() float32 {
return c.getState().strokeWidth
}
// SetMiterLimit sets the miter limit of the stroke style.
// Miter limit controls when a sharp corner is beveled.
func (c *Context) SetMiterLimit(limit float32) {
c.getState().miterLimit = limit
}
// MiterLimit gets the miter limit of the stroke style.
func (c *Context) MiterLimit() float32 {
return c.getState().miterLimit
}
// SetLineCap sets how the end of the line (cap) is drawn,
// Can be one of: Butt (default), Round, Squre.
func (c *Context) SetLineCap(cap LineCap) {
c.getState().lineCap = cap
}
// LineCap gets how the end of the line (cap) is drawn,
func (c *Context) LineCap() LineCap {
return c.getState().lineCap
}
// SetLineJoin sets how sharp path corners are drawn.
// Can be one of Miter (default), Round, Bevel.
func (c *Context) SetLineJoin(joint LineCap) {
c.getState().lineJoin = joint
}
// LineJoin gets how sharp path corners are drawn.
func (c *Context) LineJoin() LineCap {
return c.getState().lineJoin
}
// SetGlobalAlpha sets the transparency applied to all rendered shapes.
// Already transparent paths will get proportionally more transparent as well.
func (c *Context) SetGlobalAlpha(alpha float32) {
c.getState().alpha = alpha
}
// GlobalAlpha gets the transparency applied to all rendered shapes.
func (c *Context) GlobalAlpha() float32 {
return c.getState().alpha
}
// SetTransform premultiplies current coordinate system by specified matrix.
func (c *Context) SetTransform(t TransformMatrix) {
state := c.getState()
state.xform = state.xform.PreMultiply(t)
}
// SetTransformByValue premultiplies current coordinate system by specified matrix.
// The parameters are interpreted as matrix as follows:
// [a c e]
// [b d f]
// [0 0 1]
func (cx *Context) SetTransformByValue(a, b, c, d, e, f float32) {
t := TransformMatrix{a, b, c, d, e, f}
state := cx.getState()
state.xform = state.xform.PreMultiply(t)
}
// ResetTransform resets current transform to a identity matrix.
func (c *Context) ResetTransform() {
state := c.getState()
state.xform = IdentityMatrix()
}
// Translate translates current coordinate system.
func (c *Context) Translate(x, y float32) {
state := c.getState()
state.xform = state.xform.PreMultiply(TranslateMatrix(x, y))
}
// Rotate rotates current coordinate system. Angle is specified in radians.
func (c *Context) Rotate(angle float32) {
state := c.getState()
state.xform = state.xform.PreMultiply(RotateMatrix(angle))
}
// SkewX skews the current coordinate system along X axis. Angle is specified in radians.
func (c *Context) SkewX(angle float32) {
state := c.getState()
state.xform = state.xform.PreMultiply(SkewXMatrix(angle))
}
// SkewY skews the current coordinate system along Y axis. Angle is specified in radians.
func (c *Context) SkewY(angle float32) {
state := c.getState()
state.xform = state.xform.PreMultiply(SkewYMatrix(angle))
}
// Scale scales the current coordinate system.
func (c *Context) Scale(x, y float32) {
state := c.getState()
state.xform = state.xform.PreMultiply(ScaleMatrix(x, y))
}
// CurrentTransform returns the top part (a-f) of the current transformation matrix.
// [a c e]
// [b d f]
// [0 0 1]
// There should be space for 6 floats in the return buffer for the values a-f.
func (c *Context) CurrentTransform() TransformMatrix {
return c.getState().xform
}
// SetStrokeColor sets current stroke style to a solid color.
func (c *Context) SetStrokeColor(color Color) {
c.getState().stroke.setPaintColor(color)
}
// SetStrokePaint sets current stroke style to a paint, which can be a one of the gradients or a pattern.
func (c *Context) SetStrokePaint(paint Paint) {
state := c.getState()
state.stroke = paint
state.stroke.xform = state.stroke.xform.Multiply(state.xform)
}
// SetFillColor sets current fill style to a solid color.
func (c *Context) SetFillColor(color Color) {
c.getState().fill.setPaintColor(color)
}
// SetFillPaint sets current fill style to a paint, which can be a one of the gradients or a pattern.
func (c *Context) SetFillPaint(paint Paint) {
state := c.getState()
state.fill = paint
state.fill.xform = state.fill.xform.Multiply(state.xform)
}
// CreateImage creates image by loading it from the disk from specified file name.
// Returns handle to the image.
func (c *Context) CreateImage(filePath string, flags ImageFlags) int32 {
file, err := os.Open(filePath)
defer file.Close()
if err != nil {
return 0
}
img, _, err := image.Decode(file)
if err != nil {
return 0
}
return c.CreateImageFromGoImage(flags, img)
}
// CreateImageFromMemory creates image by loading it from the specified chunk of memory.
// Returns handle to the image.
func (c *Context) CreateImageFromMemory(flags ImageFlags, data []byte) int32 {
reader := bytes.NewReader(data)
img, _, err := image.Decode(reader)
if err != nil {
return 0
}
return c.CreateImageFromGoImage(flags, img)
}
// CreateImageFromGoImage creates image by loading it from the specified image.Image object.
// Returns handle to the image.
func (c *Context) CreateImageFromGoImage(imageFlag ImageFlags, img image.Image) int32 {
bounds := img.Bounds()
size := bounds.Size()
rgba, ok := img.(*image.RGBA)
if ok {
return c.CreateImageRGBA(int32(size.X), int32(size.Y), imageFlag, rgba.Pix)
}
rgba = image.NewRGBA(bounds)
for x := 0; x < size.X; x++ {
for y := 0; y < size.Y; y++ {
rgba.Set(x, y, img.At(x, y))
}
}
return c.CreateImageRGBA(int32(size.X), int32(size.Y), imageFlag, rgba.Pix)
}
// CreateImageRGBA creates image from specified image data.
// Returns handle to the image.
func (c *Context) CreateImageRGBA(w, h int32, imageFlags ImageFlags, data []byte) int32 {
return c.params.renderCreateTexture(nvgTextureRGBA, w, h, imageFlags, data)
}
// UpdateImage updates image data specified by image handle.
func (c *Context) UpdateImage(img int32, data []byte) error {
w, h, err := c.params.renderGetTextureSize(img)
if err != nil {
return err
}
return c.params.renderUpdateTexture(img, 0, 0, w, h, data)
}
// ImageSize returns the dimensions of a created image.
func (c *Context) ImageSize(img int32) (int32, int32, error) {
return c.params.renderGetTextureSize(img)
}
// DeleteImage deletes created image.
func (c *Context) DeleteImage(img int32) {
c.params.renderDeleteTexture(img)
}
// Scissor sets the current scissor rectangle.
// The scissor rectangle is transformed by the current transform.
func (c *Context) Scissor(x, y, w, h float32) {
state := c.getState()
w = maxF(0.0, w)
h = maxF(0.0, h)
state.scissor.xform = TranslateMatrix(x+w*0.5, y+h*0.5).Multiply(state.xform)
state.scissor.extent = [2]float32{w * 0.5, h * 0.5}
}
// IntersectScissor calculates intersects current scissor rectangle with the specified rectangle.
// The scissor rectangle is transformed by the current transform.
// Note: in case the rotation of previous scissor rect differs from
// the current one, the intersection will be done between the specified
// rectangle and the previous scissor rectangle transformed in the current
// transform space. The resulting shape is always rectangle.
func (c *Context) IntersectScissor(x, y, w, h float32) {
state := c.getState()
if state.scissor.extent[0] < 0 {
c.Scissor(x, y, w, h)
return
}
pXform := state.scissor.xform.Multiply(state.xform.Inverse())
ex := state.scissor.extent[0]
ey := state.scissor.extent[1]
teX := ex * absF(pXform[0]) * ey * absF(pXform[2])
teY := ex * absF(pXform[1]) * ey * absF(pXform[3])
rect := intersectRects(pXform[4]-teX, pXform[5]-teY, teX*2, teY*2, x, y, w, h)
c.Scissor(rect[0], rect[1], rect[2], rect[3])
}
// ResetScissor resets and disables scissoring.
func (c *Context) ResetScissor() {
state := c.getState()
state.scissor.xform = TransformMatrix{0, 0, 0, 0, 0, 0}
state.scissor.extent = [2]float32{-1.0, -1.0}
}
// BeginPath clears the current path and sub-paths.
func (c *Context) BeginPath() {
c.commands = c.commands[:0]
c.cache.clearPathCache()
}
// MoveTo starts new sub-path with specified point as first point.
func (c *Context) MoveTo(x, y float32) {
c.appendCommand([]float32{float32(nvgMOVETO), x, y})
}
// LineTo adds line segment from the last point in the path to the specified point.
func (c *Context) LineTo(x, y float32) {
c.appendCommand([]float32{float32(nvgLINETO), x, y})
}
// BezierTo adds cubic bezier segment from last point in the path via two control points to the specified point.
func (c *Context) BezierTo(c1x, c1y, c2x, c2y, x, y float32) {
c.appendCommand([]float32{float32(nvgBEZIERTO), c1x, c1y, c2x, c2y, x, y})
}
// QuadTo adds quadratic bezier segment from last point in the path via a control point to the specified point.
func (c *Context) QuadTo(cx, cy, x, y float32) {
x0 := c.commandX
y0 := c.commandY
c.appendCommand([]float32{float32(nvgBEZIERTO),
x0 + 2.0/3.0*(cx-x0), y0 + 2.0/3.0*(cy-y0),
x + 2.0/3.0*(cx-x), y + 2.0/3.0*(cy-y),
x, y,
})
}
// Arc creates new circle arc shaped sub-path. The arc center is at cx,cy, the arc radius is r,
// and the arc is drawn from angle a0 to a1, and swept in direction dir (CounterClockwise, or Clockwise).
// Angles are specified in radians.
func (c *Context) Arc(cx, cy, r, a0, a1 float32, dir Direction) {
var move nvgCommands
if len(c.commands) > 0 {
move = nvgLINETO
} else {
move = nvgMOVETO
}
// Clamp angles
da := a1 - a0
if dir == Clockwise {
if absF(da) >= PI*2 {
da = PI * 2
} else {
for da < 0.0 {
da += PI * 2
}
}
} else {
if absF(da) >= PI*2 {
da = -PI * 2
} else {
for da > 0.0 {
da -= PI * 2
}
}
}
// Split arc into max 90 degree segments.
nDivs := clampI(int(absF(da)/(PI*0.5)+0.5), 1, 5)
hda := da / float32(nDivs) / 2.0
sin, cos := sinCosF(hda)
kappa := absF(4.0 / 3.0 * (1.0 - cos) / sin)
if dir == CounterClockwise {
kappa = -kappa
}
values := make([]float32, 0, 3+5*7+100)
var px, py, pTanX, pTanY float32
for i := 0; i <= nDivs; i++ {
a := a0 + da*float32(i)/float32(nDivs)
dy, dx := sinCosF(a)
x := cx + dx*r
y := cy + dy*r
tanX := -dy * r * kappa
tanY := dx * r * kappa
if i == 0 {
values = append(values, float32(move), x, y)
} else {
values = append(values, float32(nvgBEZIERTO), px+pTanX, py+pTanY, x-tanX, y-tanY, x, y)
}
px = x
py = y
pTanX = tanX
pTanY = tanY
}
c.appendCommand(values)
}
// ArcTo adds an arc segment at the corner defined by the last path point, and two specified points.
func (c *Context) ArcTo(x1, y1, x2, y2, radius float32) {
if len(c.commands) == 0 {
return
}
x0 := c.commandX
y0 := c.commandY
// Handle degenerate cases.
if ptEquals(x0, y0, x1, y1, c.distTol) ||
ptEquals(x1, y1, x2, y2, c.distTol) ||
distPtSeg(x1, y1, x0, y0, x2, y2) < c.distTol*c.distTol ||
radius < c.distTol {
c.LineTo(x1, y1)
return
}
// Calculate tangential circle to lines (x0,y0)-(x1,y1) and (x1,y1)-(x2,y2).
dx0 := x0 - x1
dy0 := y0 - y1
dx1 := x2 - x1
dy1 := y2 - y1
_, dx0, dy0 = normalize(dx0, dy0)
_, dx1, dy1 = normalize(dx1, dy1)
a := acosF(dx0*dx1 + dy0*dy1)
d := radius / tanF(a/2.0)
if d > 10000.0 {
c.LineTo(x1, y1)
return
}
var cx, cy, a0, a1 float32
var dir Direction
if cross(dx0, dy0, dx1, dy1) > 0.0 {
cx = x1 + dx0*d + dy0*radius
cy = y1 + dy0*d + -dx0*radius
a0 = atan2F(dx0, -dy0)
a1 = atan2F(-dx1, dy1)
dir = Clockwise
} else {
cx = x1 + dx0*d + -dy0*radius
cy = y1 + dy0*d + dx0*radius
a0 = atan2F(-dx0, dy0)
a1 = atan2F(dx1, -dy1)
dir = CounterClockwise
}
c.Arc(cx, cy, radius, a0, a1, dir)
}
// Rect creates new rectangle shaped sub-path.
func (c *Context) Rect(x, y, w, h float32) {
c.appendCommand([]float32{
float32(nvgMOVETO), x, y,
float32(nvgLINETO), x, y + h,
float32(nvgLINETO), x + w, y + h,
float32(nvgLINETO), x + w, y,
float32(nvgCLOSE),
})
}
// RoundedRect creates new rounded rectangle shaped sub-path.
func (c *Context) RoundedRect(x, y, w, h, r float32) {
if r < 0.1 {
c.Rect(x, y, w, h)
} else {
rx := minF(r, absF(w)*0.5) * signF(w)
ry := minF(r, absF(h)*0.5) * signF(h)
c.appendCommand([]float32{
float32(nvgMOVETO), x, y + ry,
float32(nvgLINETO), x, y + h - ry,
float32(nvgBEZIERTO), x, y + h - ry*(1-Kappa90), x + rx*(1-Kappa90), y + h, x + rx, y + h,
float32(nvgLINETO), x + w - rx, y + h,
float32(nvgBEZIERTO), x + w - rx*(1-Kappa90), y + h, x + w, y + h - ry*(1-Kappa90), x + w, y + h - ry,
float32(nvgLINETO), x + w, y + ry,
float32(nvgBEZIERTO), x + w, y + ry*(1-Kappa90), x + w - rx*(1-Kappa90), y, x + w - rx, y,
float32(nvgLINETO), x + rx, y,
float32(nvgBEZIERTO), x + rx*(1-Kappa90), y, x, y + ry*(1-Kappa90), x, y + ry,
float32(nvgCLOSE),
})
}
}
// Ellipse creates new ellipse shaped sub-path.
func (c *Context) Ellipse(cx, cy, rx, ry float32) {
c.appendCommand([]float32{
float32(nvgMOVETO), cx - rx, cy,
float32(nvgBEZIERTO), cx - rx, cy + ry*Kappa90, cx - rx*Kappa90, cy + ry, cx, cy + ry,
float32(nvgBEZIERTO), cx + rx*Kappa90, cy + ry, cx + rx, cy + ry*Kappa90, cx + rx, cy,
float32(nvgBEZIERTO), cx + rx, cy - ry*Kappa90, cx + rx*Kappa90, cy - ry, cx, cy - ry,
float32(nvgBEZIERTO), cx - rx*Kappa90, cy - ry, cx - rx, cy - ry*Kappa90, cx - rx, cy,
float32(nvgCLOSE),
})
}
// Circle creates new circle shaped sub-path.
func (c *Context) Circle(cx, cy, r float32) {
c.Ellipse(cx, cy, r, r)
}
// ClosePath closes current sub-path with a line segment.
func (c *Context) ClosePath() {
c.appendCommand([]float32{float32(nvgCLOSE)})
}
// PathWinding sets the current sub-path winding, see Winding.
func (c *Context) PathWinding(winding Winding) {
c.appendCommand([]float32{float32(nvgWINDING), float32(winding)})
}
// DebugDumpPathCache prints cached path information to console
func (c *Context) DebugDumpPathCache() {
log.Printf("Dumping %d cached paths\n", len(c.cache.paths))
for i := 0; i < len(c.cache.paths); i++ {
path := &c.cache.paths[i]
log.Printf(" - Path %d\n", i)
if len(path.fills) > 0 {
log.Printf(" - fill: %d\n", len(path.fills))
for _, fill := range path.fills {
log.Printf("%f\t%f\n", fill.x, fill.y)
}
}
if len(path.strokes) > 0 {
log.Printf(" - strokes: %d\n", len(path.strokes))
for _, stroke := range path.strokes {
log.Printf("%f\t%f\n", stroke.x, stroke.y)
}
}
}
}
// Fill fills the current path with current fill style.
func (c *Context) Fill() {
state := c.getState()
fillPaint := state.fill
c.flattenPaths()
if c.params.edgeAntiAlias() {
c.cache.expandFill(c.fringeWidth, Miter, 2.4, c.fringeWidth)
} else {
c.cache.expandFill(0.0, Miter, 2.4, c.fringeWidth)
}
// Apply global alpha
fillPaint.innerColor.A *= state.alpha
fillPaint.outerColor.A *= state.alpha
c.params.renderFill(&fillPaint, &state.scissor, c.fringeWidth, c.cache.bounds, c.cache.paths)
// Count triangles
for i := 0; i < len(c.cache.paths); i++ {
path := &c.cache.paths[i]
c.fillTriCount += int32(len(path.fills) - 2)
c.strokeTriCount += int32(len(path.strokes) - 2)
c.drawCallCount += 2
}
}
// Stroke draws the current path with current stroke style.
func (c *Context) Stroke() {
state := c.getState()
scale := state.xform.getAverageScale()
strokeWidth := clampF(state.strokeWidth*scale, 0.0, 200.0)
strokePaint := state.stroke
if strokeWidth < c.fringeWidth {
// If the stroke width is less than pixel size, use alpha to emulate coverage.
// Since coverage is area, scale by alpha*alpha.
alpha := clampF(strokeWidth/c.fringeWidth, 0.0, 1.0)
strokePaint.innerColor.A *= alpha * alpha
strokePaint.outerColor.A *= alpha * alpha
strokeWidth = c.fringeWidth
}
// Apply global alpha
strokePaint.innerColor.A *= state.alpha
strokePaint.outerColor.A *= state.alpha
c.flattenPaths()
for _, path := range c.cache.paths {
if path.count == 1 {
panic("")
}
}
if c.params.edgeAntiAlias() {
c.cache.expandStroke(strokeWidth*0.5+c.fringeWidth*0.5, state.lineCap, state.lineJoin, state.miterLimit, c.fringeWidth, c.tessTol)
} else {
c.cache.expandStroke(strokeWidth*0.5, state.lineCap, state.lineJoin, state.miterLimit, c.fringeWidth, c.tessTol)
}
c.params.renderStroke(&strokePaint, &state.scissor, c.fringeWidth, strokeWidth, c.cache.paths)
// Count triangles
for i := 0; i < len(c.cache.paths); i++ {
path := &c.cache.paths[i]
c.strokeTriCount += int32(len(path.strokes) - 2)
c.drawCallCount += 2
}
}
// CreateFont creates font by loading it from the disk from specified file name.
// Returns handle to the font.
func (c *Context) CreateFont(name, filePath string) int {
return c.fs.AddFont(name, filePath)
}
// CreateFontFromMemory creates image by loading it from the specified memory chunk.
// Returns handle to the font.
func (c *Context) CreateFontFromMemory(name string, data []byte, freeData uint8) int {
return c.fs.AddFontFromMemory(name, data, freeData)
}
// FindFont finds a loaded font of specified name, and returns handle to it, or -1 if the font is not found.
func (c *Context) FindFont(name string) int {
return c.fs.GetFontByName(name)
}
// SetFontSize sets the font size of current text style.
func (c *Context) SetFontSize(size float32) {
if size < 0 {
panic("Context.SetFontSize: negative font size is invalid")
}
c.getState().fontSize = size
}
// FontSize gets the font size of current text style.
func (c *Context) FontSize() float32 {
return c.getState().fontSize
}
// SetFontBlur sets the font blur of current text style.
func (c *Context) SetFontBlur(blur float32) {
c.getState().fontBlur = blur
}
// FontBlur gets the font blur of current text style.
func (c *Context) FontBlur() float32 {
return c.getState().fontBlur
}
// SetTextLetterSpacing sets the letter spacing of current text style.
func (c *Context) SetTextLetterSpacing(spacing float32) {
c.getState().letterSpacing = spacing
}
// TextLetterSpacing gets the letter spacing of current text style.
func (c *Context) TextLetterSpacing() float32 {
return c.getState().letterSpacing
}
// SetTextLineHeight sets the line height of current text style.
func (c *Context) SetTextLineHeight(lineHeight float32) {
c.getState().lineHeight = lineHeight
}
// TextLineHeight gets the line height of current text style.
func (c *Context) TextLineHeight() float32 {
return c.getState().lineHeight
}
// SetTextAlign sets the text align of current text style.
func (c *Context) SetTextAlign(align Align) {
c.getState().textAlign = align
}
// TextAlign gets the text align of current text style.
func (c *Context) TextAlign() Align {
return c.getState().textAlign
}
// SetFontFaceID sets the font face based on specified id of current text style.
func (c *Context) SetFontFaceID(font int) {
c.getState().fontID = font
}
// FontFaceID gets the font face id of current text style.
func (c *Context) FontFaceID() int {
return c.getState().fontID
}
// SetFontFace sets the font face based on specified name of current text style.
func (c *Context) SetFontFace(font string) {
c.getState().fontID = c.fs.GetFontByName(font)
}
// FontFace gets the font face name of current text style.
func (c *Context) FontFace() string {
return c.fs.GetFontName()
}
// Text draws text string at specified location. If end is specified only the sub-string up to the end is drawn.
func (c *Context) Text(x, y float32, str string) float32 {
return c.TextRune(x, y, []rune(str))
}
// TextRune is an alternate version of Text that accepts rune slice.
func (c *Context) TextRune(x, y float32, runes []rune) float32 {
state := c.getState()
scale := state.getFontScale() * c.devicePxRatio
invScale := 1.0 / scale
if state.fontID == fontstashmini.INVALID {
return 0
}
c.fs.SetSize(state.fontSize * scale)
c.fs.SetSpacing(state.letterSpacing * scale)
c.fs.SetBlur(state.fontBlur * scale)
c.fs.SetAlign(fontstashmini.FONSAlign(state.textAlign))
c.fs.SetFont(state.fontID)
vertexCount := maxI(2, len(runes)) * 4 // conservative estimate.
vertexes := c.cache.allocVertexes(vertexCount)
iter := c.fs.TextIterForRunes(x*scale, y*scale, runes)
prevIter := iter
index := 0
for {
quad, ok := iter.Next()
if !ok {
break
}
if iter.PrevGlyph == nil || iter.PrevGlyph.Index == -1 {
if !c.allocTextAtlas() {
break // no memory :(
}
if index != 0 {
c.renderText(vertexes[:index])
index = 0
}
iter = prevIter
quad, _ = iter.Next() // try again
if iter.PrevGlyph == nil || iter.PrevGlyph.Index == -1 {
// still can not find glyph?
break
}
}
prevIter = iter
// Transform corners.
c0, c1 := state.xform.TransformPoint(quad.X0*invScale, quad.Y0*invScale)
c2, c3 := state.xform.TransformPoint(quad.X1*invScale, quad.Y0*invScale)
c4, c5 := state.xform.TransformPoint(quad.X1*invScale, quad.Y1*invScale)
c6, c7 := state.xform.TransformPoint(quad.X0*invScale, quad.Y1*invScale)
//log.Printf("quad(%c) x0=%d, x1=%d, y0=%d, y1=%d, s0=%d, s1=%d, t0=%d, t1=%d\n", iter.CodePoint, int(quad.X0), int(quad.X1), int(quad.Y0), int(quad.Y1), int(1024*quad.S0), int(quad.S1*1024), int(quad.T0*1024), int(quad.T1*1024))
// Create triangles
if index+4 <= vertexCount {
(&vertexes[index]).set(c2, c3, quad.S1, quad.T0)
(&vertexes[index+1]).set(c0, c1, quad.S0, quad.T0)
(&vertexes[index+2]).set(c4, c5, quad.S1, quad.T1)
(&vertexes[index+3]).set(c6, c7, quad.S0, quad.T1)
index += 4
}
}
c.flushTextTexture()
c.renderText(vertexes[:index])
return iter.X
}
// TextBox draws multi-line text string at specified location wrapped at the specified width. If end is specified only the sub-string up to the end is drawn.
// White space is stripped at the beginning of the rows, the text is split at word boundaries or when new-line characters are encountered.
// Words longer than the max width are slit at nearest character (i.e. no hyphenation).
// Draws text string at specified location. If end is specified only the sub-string up to the end is drawn.
func (c *Context) TextBox(x, y, breakRowWidth float32, str string) {
state := c.getState()
if state.fontID == fontstashmini.INVALID {
return
}
runes := []rune(str)
oldAlign := state.textAlign
var hAlign Align
if state.textAlign&AlignLeft != 0 {
hAlign = AlignLeft
} else if state.textAlign&AlignCenter != 0 {
hAlign = AlignCenter
} else if state.textAlign&AlignRight != 0 {
hAlign = AlignRight
}
vAlign := state.textAlign & (AlignTop | AlignMiddle | AlignBottom | AlignBaseline)
state.textAlign = AlignLeft | vAlign
_, _, lineH := c.TextMetrics()
state.textAlign = oldAlign
for _, row := range c.TextBreakLinesRune(runes, breakRowWidth) {
text := string(runes[row.StartIndex:row.EndIndex])
switch hAlign {
case AlignLeft:
c.Text(x, y, text)
case AlignCenter:
c.Text(x+breakRowWidth*0.5-row.Width*0.5, y, text)
case AlignRight:
c.Text(x+breakRowWidth-row.Width, y, text)
}
y += lineH * state.lineHeight
}
}
// TextBounds measures the specified text string. Parameter bounds should be a pointer to float[4],
// if the bounding box of the text should be returned. The bounds value are [xmin,ymin, xmax,ymax]
// Returns the horizontal advance of the measured text (i.e. where the next character should drawn).
// Measured values are returned in local coordinate space.
func (c *Context) TextBounds(x, y float32, str string) (float32, []float32) {
state := c.getState()
scale := state.getFontScale() * c.devicePxRatio
invScale := 1.0 / scale
if state.fontID == fontstashmini.INVALID {
return 0, nil
}
c.fs.SetSize(state.fontSize * scale)
c.fs.SetSpacing(state.letterSpacing * scale)
c.fs.SetBlur(state.fontBlur * scale)
c.fs.SetAlign(fontstashmini.FONSAlign(state.textAlign))
c.fs.SetFont(state.fontID)
width, bounds := c.fs.TextBounds(x*scale, y*scale, str)
if bounds != nil {
bounds[1], bounds[3] = c.fs.LineBounds(y * scale)
bounds[0] *= invScale
bounds[1] *= invScale
bounds[2] *= invScale
bounds[3] *= invScale