forked from paeryn/openvg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlibshapes.c
1566 lines (1389 loc) · 46.6 KB
/
libshapes.c
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
//
// libshapes: high-level OpenVG API
// Anthony Starks ([email protected])
//
// Additional outline / windowing functions
// Paeryn (github.com/paeryn)
//
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdbool.h>
#include <wchar.h>
#include <termios.h>
#include <assert.h>
#include <jpeglib.h>
#include <png.h>
#include "VG/openvg.h"
#include "VG/vgu.h"
#include "EGL/egl.h"
#include "bcm_host.h"
#include "DejaVuSans.inc" // font data
#include "DejaVuSansMono.inc"
#include "eglstate.h" // data structures for graphics state
#include "fontinfo.h" // font data structure
#include "shapes.h" // Needed to check prototypes
#include "fontsystem.h"
static STATE_T _state, *state = &_state; // global graphics state
static int32_t init_x = 0; // Initial window position and size
static int32_t init_y = 0;
static uint32_t init_w = 0;
static uint32_t init_h = 0;
static bool check_errors = true;
static uint32_t vg_error_code = VG_NO_ERROR;
// hold the current glyph representation of a string to print. Memory
// is allocated by the stringToGlyph function, will grow when a longer
// string is converted.
// glyph_kern holds the kerning tables of the string (2*float per glyph)
static VGuint *glyph_string = NULL;
static VGfloat *glyph_kern = NULL;
// hold the paths for basic shapes. Shapes that need a variable number
// of segments are handled by common_path.
static VGPath common_path = VG_INVALID_HANDLE;
static VGPath cbezier_path = VG_INVALID_HANDLE;
static VGPath qbezier_path = VG_INVALID_HANDLE;
static VGPath rect_path = VG_INVALID_HANDLE;
static VGPath line_path = VG_INVALID_HANDLE;
static VGPath roundrect_path = VG_INVALID_HANDLE;
static VGPath ellipse_path = VG_INVALID_HANDLE;
static VGPath dot_smooth_path = VG_INVALID_HANDLE;
static VGPath dot_rough_path = VG_INVALID_HANDLE;
static VGPaint fill_paint = VG_INVALID_HANDLE;
static VGPaint stroke_paint = VG_INVALID_HANDLE;
// Pointer cursor
static cursor_t *priv_cursor = NULL;
//
// Terminal settings
//
// terminal settings structures
static struct termios new_term_attr;
static struct termios orig_term_attr;
// saveterm saves the current terminal settings
void SaveTerm() {
tcgetattr(fileno(stdin), &orig_term_attr);
}
// Deprecated
void saveterm() {
SaveTerm();
}
// rawterm sets the terminal to raw mode
void RawTerm() {
memcpy(&new_term_attr, &orig_term_attr, sizeof(struct termios));
new_term_attr.c_lflag &= ~(ICANON | ECHO | ECHOE | ECHOK | ECHONL | ECHOPRT | ECHOKE | ICRNL);
new_term_attr.c_cc[VTIME] = 0;
new_term_attr.c_cc[VMIN] = 0;
tcsetattr(fileno(stdin), TCSANOW, &new_term_attr);
}
// Deprecated
void rawterm() {
RawTerm();
}
// restore resets the terminal to the previously saved setting
void RestoreTerm() {
tcsetattr(fileno(stdin), TCSANOW, &orig_term_attr);
}
// Deprecated
void restoreterm() {
RestoreTerm();
}
//
// Font functions
//
// loadfont loads font path data
// derived from http://web.archive.org/web/20070808195131/http://developer.hybrid.fi/font2openvg/renderFont.cpp.txt
Fontinfo LoadFont(const int *Points,
const int *PointIndices,
const unsigned char *Instructions,
const int *InstructionIndices, const int *InstructionCounts,
const int *adv, const short *cmap, int ng, int descender, int ascender) {
Fontinfo f = malloc(sizeof *f);
if (!f)
return NULL;
f->face = NULL;
VGFont font = f->vgfont = vgCreateFont(ng);
if (font == VG_INVALID_HANDLE) {
free(f);
return NULL;
}
VGint i;
for (i = 0; i < ng; i++) {
const int *p = &Points[PointIndices[i] * 2];
const unsigned char *instructions = &Instructions[InstructionIndices[i]];
VGfloat origin[2] = { 0.0f, 0.0f };
VGfloat escapement[2] = { (VGfloat) (adv[i]) / 65536.0f, 0.0f };
VGPath path = VG_INVALID_HANDLE;
int ic = InstructionCounts[i];
if (ic) {
int p_count;
if (i < ng - 1)
p_count = PointIndices[i + 1];
else
p_count = sizeof Points / sizeof *Points;
p_count -= PointIndices[i];
path = vgCreatePath(VG_PATH_FORMAT_STANDARD,
VG_PATH_DATATYPE_S_32,
1.0f / 65536.0f, 0.0f, ic, p_count, VG_PATH_CAPABILITY_APPEND_TO);
if (path != VG_INVALID_HANDLE)
vgAppendPathData(path, ic, instructions, p);
}
vgSetGlyphToPath(font, (VGuint) i, path, VG_FALSE, origin, escapement);
if (path != VG_INVALID_HANDLE)
vgDestroyPath(path);
}
f->CharacterMap = cmap;
f->GlyphAdvances = adv;
f->Count = (unsigned int)ng;
f->DescenderHeight = (VGfloat) descender / 65536.0f;
f->AscenderHeight = (VGfloat) ascender / 65536.0f;
f->Height = f->AscenderHeight - f->DescenderHeight; // Guesstimate
f->Name = "unknown";
f->Style = "unknown";
f->Kerning = 0;
return f;
}
// Deprecated
Fontinfo loadfont(const int *Points,
const int *PointIndices,
const unsigned char *Instructions,
const int *InstructionIndices, const int *InstructionCounts,
const int *adv, const short *cmap, int ng, int descender, int ascender) {
return LoadFont(Points, PointIndices, Instructions, InstructionIndices,
InstructionCounts, adv, cmap, ng, descender, ascender);
}
// unloadfont frees font path data
void UnloadFont(Fontinfo f) {
if (f) {
if (f->face)
return UnloadTTF(f);
else {
vgDestroyFont(f->vgfont);
free(f);
}
}
}
// Deprecated
void unloadfont(Fontinfo f) {
UnloadFont(f);
}
// createImageFromJpeg decompresses a JPEG image to the standard image format
// source: https://github.com/ileben/ShivaVG/blob/master/examples/test_image.c
VGImage CreateImageFromJpeg(const char *filename) {
FILE *infile;
struct jpeg_decompress_struct jdc;
struct jpeg_error_mgr jerr;
JSAMPARRAY buffer;
unsigned int bstride;
unsigned int bbpp;
VGImage img;
VGubyte *data;
unsigned int width;
unsigned int height;
unsigned int dstride;
unsigned int dbpp;
VGubyte *brow;
VGubyte *drow;
unsigned int x;
unsigned int lilEndianTest = 1;
VGImageFormat rgbaFormat;
// Check for endianness
if (((unsigned char *)&lilEndianTest)[0] == 1)
rgbaFormat = VG_sABGR_8888;
else
rgbaFormat = VG_sRGBA_8888;
// Try to open image file
infile = fopen(filename, "rb");
if (infile == NULL) {
fprintf(stderr, "Failed opening '%s' for reading!\n", filename);
return VG_INVALID_HANDLE;
}
// Setup default error handling
jdc.err = jpeg_std_error(&jerr);
jpeg_create_decompress(&jdc);
// Set input file
jpeg_stdio_src(&jdc, infile);
// Read header and start
jpeg_read_header(&jdc, TRUE);
jpeg_start_decompress(&jdc);
width = jdc.output_width;
height = jdc.output_height;
// Allocate buffer using jpeg allocator
bbpp = (unsigned int)jdc.output_components;
bstride = width * bbpp;
buffer = (*jdc.mem->alloc_sarray)
((j_common_ptr) & jdc, JPOOL_IMAGE, bstride, 1);
// Allocate image data buffer
dbpp = 4;
dstride = width * dbpp;
data = (VGubyte *) malloc(dstride * height);
// Iterate until all scanlines processed
while (jdc.output_scanline < height) {
// Read scanline into buffer
jpeg_read_scanlines(&jdc, buffer, 1);
drow = data + (height - jdc.output_scanline) * dstride;
brow = buffer[0];
// Expand to RGBA
for (x = 0; x < width; ++x, drow += dbpp, brow += bbpp) {
switch (bbpp) {
case 4:
drow[0] = brow[0];
drow[1] = brow[1];
drow[2] = brow[2];
drow[3] = brow[3];
break;
case 3:
drow[0] = brow[0];
drow[1] = brow[1];
drow[2] = brow[2];
drow[3] = 255;
break;
}
}
}
// Create VG image
img = vgCreateImage(rgbaFormat, (VGint) width, (VGint) height, VG_IMAGE_QUALITY_BETTER);
if (img != VG_INVALID_HANDLE)
vgImageSubData(img, data, (VGint) dstride, rgbaFormat, 0, 0, (VGint) width, (VGint) height);
else
vg_error_code = vgGetError();
// Cleanup
jpeg_destroy_decompress(&jdc);
fclose(infile);
free(data);
return img;
}
// Deprecated
VGImage createImageFromJpeg(const char *filename) {
return CreateImageFromJpeg(filename);
}
// makeimage makes an image from a raw raster of red, green, blue, alpha values
void MakeImage(VGfloat x, VGfloat y, VGint w, VGint h, VGubyte * data) {
VGint dstride = w * 4;
VGImageFormat rgbaFormat = VG_sABGR_8888;
VGImage img = vgCreateImage(rgbaFormat, w, h, VG_IMAGE_QUALITY_BETTER);
if (img != VG_INVALID_HANDLE) {
vgImageSubData(img, (void *)data, dstride, rgbaFormat, 0, 0, w, h);
vgSetPixels((VGint) x, (VGint) y, img, 0, 0, w, h);
vgDestroyImage(img);
} else
vg_error_code = vgGetError();
}
// Deprecated
void makeimage(VGfloat x, VGfloat y, VGint w, VGint h, VGubyte * data) {
MakeImage(x, y, w, h, data);
}
// Image places an image at the specifed location
void Image(VGfloat x, VGfloat y, VGint w, VGint h, const char *filename) {
VGImage img = CreateImageFromJpeg(filename);
if (img != VG_INVALID_HANDLE) {
vgSetPixels((VGint) x, (VGint) y, img, 0, 0, w, h);
vgDestroyImage(img);
} else
vg_error_code = vgGetError();
}
// Deprecated
void image(VGfloat x, VGfloat y, VGint w, VGint h, const char *filename) {
Image(x, y, w, h, filename);
}
// dumpscreen writes the raster
static void dumpscreen(VGuint w, VGuint h, FILE * fp) {
if (w > state->window_width)
w = state->window_width;
if (h > state->window_height)
h = state->window_height;
void *ScreenBuffer = malloc(w * h * 4);
if (ScreenBuffer) {
vgReadPixels(ScreenBuffer, (VGint) (w * 4), VG_sABGR_8888, 0, 0, (VGint) w, (VGint) h);
fwrite(ScreenBuffer, 1, w * h * 4, fp);
free(ScreenBuffer);
}
}
//Fontinfo SansTypeface, SerifTypeface, MonoTypeface;
Fontinfo SansTypeface, MonoTypeface;
// initWindowSize requests a specific window size & position, if not called
// then init() will open a full screen window.
// Done this way to preserve the original init() behaviour.
void InitWindowSize(int32_t x, int32_t y, int32_t w, int32_t h) {
init_x = x;
init_y = y;
init_w = w < 0 ? 0 : (uint32_t) w;
init_h = h < 0 ? 0 : (uint32_t) h;
}
// Deprecated
void initWindowSize(int32_t x, int32_t y, int32_t w, int32_t h) {
InitWindowSize(x, y, w, h);
}
// init sets the system to its initial state, returns false if an
// error occured.
bool InitShapes(int32_t * w, int32_t * h) {
int err_state = 0;
check_errors = false;
bcm_host_init();
memset(state, 0, sizeof(*state));
state->window_x = init_x;
state->window_y = init_y;
state->window_width = init_w;
state->window_height = init_h;
oglinit(state);
SansTypeface = LoadFont(DejaVuSans_glyphPoints,
DejaVuSans_glyphPointIndices,
DejaVuSans_glyphInstructions,
DejaVuSans_glyphInstructionIndices,
DejaVuSans_glyphInstructionCounts,
DejaVuSans_glyphAdvances,
DejaVuSans_characterMap,
DejaVuSans_glyphCount, DejaVuSans_descender_height, DejaVuSans_ascender_height);
if (SansTypeface) {
SansTypeface->Name = "DejaVu Sans";
SansTypeface->Style = "Book";
}
MonoTypeface = LoadFont(DejaVuSansMono_glyphPoints,
DejaVuSansMono_glyphPointIndices,
DejaVuSansMono_glyphInstructions,
DejaVuSansMono_glyphInstructionIndices,
DejaVuSansMono_glyphInstructionCounts,
DejaVuSansMono_glyphAdvances,
DejaVuSansMono_characterMap,
DejaVuSansMono_glyphCount, DejaVuSansMono_descender_height, DejaVuSansMono_ascender_height);
if (MonoTypeface) {
MonoTypeface->Name = "DejaVu Sans Mono";
MonoTypeface->Style = "Book";
}
*w = (int32_t) state->window_width;
*h = (int32_t) state->window_height;
if (!SansTypeface || !MonoTypeface) {
fputs("libshapes failed to load the default fonts.", stderr);
}
fill_paint = vgCreatePaint();
err_state |= fill_paint == VG_INVALID_HANDLE;
stroke_paint = vgCreatePaint();
err_state |= stroke_paint == VG_INVALID_HANDLE;
common_path =
vgCreatePath(VG_PATH_FORMAT_STANDARD, VG_PATH_DATATYPE_F, 1.0f, 0.0f, 0, 0,
VG_PATH_CAPABILITY_APPEND_TO | VG_PATH_CAPABILITY_MODIFY);
err_state |= common_path == VG_INVALID_HANDLE;
VGubyte segments[] = { VG_MOVE_TO_ABS, VG_CUBIC_TO };
VGfloat coords[] = { 0.0f, 0.0f, 1.0f, -1.0f, 2.0f, 1.0f, 3.0f, 0.0f };
cbezier_path =
vgCreatePath(VG_PATH_FORMAT_STANDARD, VG_PATH_DATATYPE_F, 1.0f, 0.0f, 2, 8,
VG_PATH_CAPABILITY_APPEND_TO | VG_PATH_CAPABILITY_MODIFY);
if (cbezier_path == VG_INVALID_HANDLE)
err_state |= 1;
else
vgAppendPathData(cbezier_path, 2, segments, coords);
qbezier_path =
vgCreatePath(VG_PATH_FORMAT_STANDARD, VG_PATH_DATATYPE_F, 1.0f, 0.0f, 2, 6,
VG_PATH_CAPABILITY_APPEND_TO | VG_PATH_CAPABILITY_MODIFY);
if (qbezier_path == VG_INVALID_HANDLE)
err_state |= 1;
else {
segments[1] = VG_QUAD_TO;
vgAppendPathData(qbezier_path, 2, segments, coords);
}
rect_path =
vgCreatePath(VG_PATH_FORMAT_STANDARD, VG_PATH_DATATYPE_F, 1.0f, 0.0f, 5, 5,
VG_PATH_CAPABILITY_APPEND_TO | VG_PATH_CAPABILITY_MODIFY);
if (rect_path == VG_INVALID_HANDLE)
err_state |= 1;
else
vguRect(rect_path, 0.0f, 0.0f, 1.0f, 1.0f);
line_path =
vgCreatePath(VG_PATH_FORMAT_STANDARD, VG_PATH_DATATYPE_F, 1.0f, 0.0f, 2, 4,
VG_PATH_CAPABILITY_APPEND_TO | VG_PATH_CAPABILITY_MODIFY);
if (line_path == VG_INVALID_HANDLE)
err_state |= 1;
else
vguLine(line_path, 0.0f, 0.0f, 1.0f, 1.0f);
roundrect_path =
vgCreatePath(VG_PATH_FORMAT_STANDARD, VG_PATH_DATATYPE_F, 1.0f, 0.0f, 10, 26,
VG_PATH_CAPABILITY_APPEND_TO | VG_PATH_CAPABILITY_MODIFY);
if (roundrect_path == VG_INVALID_HANDLE)
err_state |= 1;
else
vguRoundRect(roundrect_path, 0.0f, 0.0f, 2.0f, 2.0f, 1.0f, 1.0f);
ellipse_path =
vgCreatePath(VG_PATH_FORMAT_STANDARD, VG_PATH_DATATYPE_F, 1.0f, 0.0f, 4, 12,
VG_PATH_CAPABILITY_APPEND_TO | VG_PATH_CAPABILITY_MODIFY);
if (ellipse_path == VG_INVALID_HANDLE)
err_state |= 1;
else
vguEllipse(ellipse_path, 0.0f, 0.0f, 1.0f, 1.0f);
dot_smooth_path =
vgCreatePath(VG_PATH_FORMAT_STANDARD, VG_PATH_DATATYPE_F, 1.0f, 0.0f, 4, 12, VG_PATH_CAPABILITY_APPEND_TO);
if (dot_smooth_path == VG_INVALID_HANDLE)
err_state |= 1;
else
vguEllipse(dot_smooth_path, 0.5f, 0.5f, 1.0f, 1.0f);
dot_rough_path = vgCreatePath(VG_PATH_FORMAT_STANDARD, VG_PATH_DATATYPE_F, 1.0f, 0.0f, 4, 12, VG_PATH_CAPABILITY_APPEND_TO);
if (dot_rough_path == VG_INVALID_HANDLE)
err_state |= 1;
else
vguRect(dot_rough_path, 0.0f, 0.0f, 1.0f, 1.0f);
if (err_state) {
fputs("Failed initialising libshapes paths.\n", stderr);
FinishShapes();
return false;
}
vg_error_code = vgGetError();
if (vg_error_code != VG_NO_ERROR) {
fprintf(stderr, "OpenVG gave error %x whilst initilising libshapes.\n", vg_error_code);
FinishShapes();
return false;
}
return true;
}
// Deprecated
bool init(int32_t * w, int32_t * h) {
return InitShapes(w, h);
}
// Turn on/off checking and reporting of OpenVG errors
// For functions that already know an OpenVG error happened they will
// always set vg_error_code regardless of this setting.
void EnableOpenVGErrorCheck(bool check) {
check_errors = check;
}
// Check status of OpenVG errors, if none had been reported then check
uint32_t CheckErrorStatus() {
uint32_t err = vg_error_code;
// If no current error then check if one has happened
if (err == VG_NO_ERROR)
err = vgGetError();
// Reset the state as the error has now been reported
vg_error_code = VG_NO_ERROR;
return err;
}
// finish cleans up
void FinishShapes() {
ScreenBrightness(255);
DeleteCursor();
eglSwapBuffers(state->display, state->surface);
vgDestroyPath(dot_rough_path);
vgDestroyPath(dot_smooth_path);
vgDestroyPath(ellipse_path);
vgDestroyPath(roundrect_path);
vgDestroyPath(line_path);
vgDestroyPath(rect_path);
vgDestroyPath(qbezier_path);
vgDestroyPath(cbezier_path);
vgDestroyPaint(stroke_paint);
vgDestroyPaint(fill_paint);
vgDestroyPath(common_path);
UnloadFont(SansTypeface);
SansTypeface = NULL;
// UnloadFont(SerifTypeface);
// SerifTypeface = NULL;
UnloadFont(MonoTypeface);
MonoTypeface = NULL;
font_CloseFontSystem();
eglMakeCurrent(state->display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
eglDestroySurface(state->display, state->surface);
eglDestroyContext(state->display, state->context);
eglTerminate(state->display);
}
// Deprecated
void finish() {
FinishShapes();
}
//
// Transformations
//
// Translate the coordinate system to x,y
void Translate(VGfloat x, VGfloat y) {
vgTranslate(x, y);
}
// Rotate around angle r
void Rotate(VGfloat r) {
vgRotate(r);
}
// Shear shears the x coordinate by x degrees, the y coordinate by y degrees
void Shear(VGfloat x, VGfloat y) {
vgShear(x, y);
}
// Scale scales by x, y
void Scale(VGfloat x, VGfloat y) {
vgScale(x, y);
}
//
// Style functions
//
// setfill sets the fill color
void SetFill(VGfloat color[4]) {
vgSetParameteri(fill_paint, VG_PAINT_TYPE, VG_PAINT_TYPE_COLOR);
vgSetParameterfv(fill_paint, VG_PAINT_COLOR, 4, color);
vgSetPaint(fill_paint, VG_FILL_PATH);
}
// Deprecated
void setfill(VGfloat color[4]) {
SetFill(color);
}
// setstroke sets the stroke color
void SetStroke(VGfloat color[4]) {
vgSetParameteri(stroke_paint, VG_PAINT_TYPE, VG_PAINT_TYPE_COLOR);
vgSetParameterfv(stroke_paint, VG_PAINT_COLOR, 4, color);
vgSetPaint(stroke_paint, VG_STROKE_PATH);
}
// Deprecated
void setstroke(VGfloat color[4]) {
SetStroke(color);
}
// StrokeWidth sets the stroke width
void StrokeWidth(VGfloat width) {
vgSetf(VG_STROKE_LINE_WIDTH, width);
vgSeti(VG_STROKE_CAP_STYLE, VG_CAP_BUTT);
vgSeti(VG_STROKE_JOIN_STYLE, VG_JOIN_MITER);
}
//
// Color functions
//
//
// RGBA fills a color vectors from a RGBA quad.
void RGBA(VGuint r, VGuint g, VGuint b, VGfloat a, VGfloat color[4]) {
if (r > 255) {
r = 0;
}
if (g > 255) {
g = 0;
}
if (b > 255) {
b = 0;
}
if (a < 0.0f || a > 1.0f) {
a = 1.0f;
}
color[0] = (VGfloat) r / 255.0f;
color[1] = (VGfloat) g / 255.0f;
color[2] = (VGfloat) b / 255.0f;
color[3] = a;
}
// RGB returns a solid color from a RGB triple
void RGB(VGuint r, VGuint g, VGuint b, VGfloat color[4]) {
RGBA(r, g, b, 1.0f, color);
}
// Stroke sets the stroke color, defined as a RGB triple.
void Stroke(VGuint r, VGuint g, VGuint b, VGfloat a) {
VGfloat color[4];
RGBA(r, g, b, a, color);
SetStroke(color);
}
// Fill sets the fillcolor, defined as a RGBA quad.
void Fill(VGuint r, VGuint g, VGuint b, VGfloat a) {
VGfloat color[4];
RGBA(r, g, b, a, color);
SetFill(color);
}
// setstops sets color stops for gradients
static void setstop(VGPaint paint, VGfloat * stops, VGint n) {
VGboolean multmode = VG_FALSE;
VGColorRampSpreadMode spreadmode = VG_COLOR_RAMP_SPREAD_REPEAT;
vgSetParameteri(paint, VG_PAINT_COLOR_RAMP_SPREAD_MODE, spreadmode);
vgSetParameteri(paint, VG_PAINT_COLOR_RAMP_PREMULTIPLIED, multmode);
vgSetParameterfv(paint, VG_PAINT_COLOR_RAMP_STOPS, 5 * n, stops);
vgSetPaint(paint, VG_FILL_PATH);
}
// LinearGradient fills with a linear gradient
void FillLinearGradient(VGfloat x1, VGfloat y1, VGfloat x2, VGfloat y2, VGfloat * stops, VGint ns) {
VGfloat lgcoord[4] = { x1, y1, x2, y2 };
vgSetParameteri(fill_paint, VG_PAINT_TYPE, VG_PAINT_TYPE_LINEAR_GRADIENT);
vgSetParameterfv(fill_paint, VG_PAINT_LINEAR_GRADIENT, 4, lgcoord);
setstop(fill_paint, stops, ns);
}
// RadialGradient fills with a linear gradient
void FillRadialGradient(VGfloat cx, VGfloat cy, VGfloat fx, VGfloat fy, VGfloat radius, VGfloat * stops, VGint ns) {
VGfloat radialcoord[5] = { cx, cy, fx, fy, radius };
vgSetParameteri(fill_paint, VG_PAINT_TYPE, VG_PAINT_TYPE_RADIAL_GRADIENT);
vgSetParameterfv(fill_paint, VG_PAINT_RADIAL_GRADIENT, 5, radialcoord);
setstop(fill_paint, stops, ns);
}
// ClipRect limits the drawing area to specified rectangle
void ClipRect(VGint x, VGint y, VGint w, VGint h) {
vgSeti(VG_SCISSORING, VG_TRUE);
VGint coords[4] = { x, y, w, h };
vgSetiv(VG_SCISSOR_RECTS, 4, coords);
}
// ClipEnd stops limiting drawing area to specified rectangle
void ClipEnd() {
vgSeti(VG_SCISSORING, VG_FALSE);
}
// Text Functions
// stringToGlyphs converts a string into a list of glyphs
// It auto allocates memory to store the glyph list and reuses it
// for future convertions, growing it if need be.
// Cache: If we are passed glyph_string as the string then it's
// because TextMid & TextEnd have already called here when
// calculating the width and we don't want to parse it again.
static unsigned int stringToGlyphs(const char *s, Fontinfo f) {
static unsigned int glyph_length = 0; // number of valid glyphs in string
static unsigned int glyph_string_len = 0; // size of allocated buffer
if (s == (char *)glyph_string) {
return glyph_length;
}
mbstate_t mbstate;
memset(&mbstate, 0, sizeof mbstate);
size_t str_length = mbsrtowcs(NULL, &s, 0, &mbstate);
if ((str_length == 0) || (str_length == (size_t) - 1))
return 0;
if (str_length > glyph_string_len) {
if (glyph_string)
free(glyph_string);
if (glyph_kern)
free(glyph_kern);
glyph_string_len = str_length;
glyph_string = malloc(glyph_string_len * sizeof *glyph_string);
glyph_kern = malloc(glyph_string_len * 2 * sizeof *glyph_kern);
}
wchar_t wstr[str_length];
mbsrtowcs(wstr, &s, str_length, &mbstate);
glyph_length = 0;
unsigned int i;
if (f->CharacterMap) { // libshapes classic fonts
for (i = 0; i < str_length; i++) {
if (wstr[i] < f->Count) {
short glyph = f->CharacterMap[wstr[i]];
if (glyph != -1)
glyph_string[glyph_length++] = (VGuint) glyph;
}
}
} else { // fontsystem fonts
VGuint prev = 0xffffffff;
VGfloat *kernX = glyph_kern;
VGfloat *kernY = glyph_kern + str_length;
for (i = 0; i < str_length; i++) {
VGuint glyph = font_CharToGlyph(f->face, wstr[i]);
glyph_string[i] = glyph;
if (f->Kerning) {
font_KernData(f->face, glyph, prev, kernX++, kernY++);
prev = glyph;
}
}
glyph_length = str_length;
}
return glyph_length;
}
// Text renders a string of text at a specified location, size, using the specified font glyphs
void Text(VGfloat x, VGfloat y, const char *s, Fontinfo f, VGint pointsize) {
VGfloat size = (VGfloat) pointsize;
VGfloat matrix[9];
vgGetMatrix(matrix);
VGint mm = vgGeti(VG_MATRIX_MODE);
vgSeti(VG_MATRIX_MODE, VG_MATRIX_GLYPH_USER_TO_SURFACE);
vgLoadMatrix(matrix);
vgTranslate(x, y);
vgScale(size, size);
vgSeti(VG_MATRIX_MODE, mm);
VGfloat pos[2] = { 0.0f, 0.0f };
vgSetfv(VG_GLYPH_ORIGIN, 2, pos);
VGfloat strokew = vgGetf(VG_STROKE_LINE_WIDTH);
if (strokew != 0.0f) {
vgSetf(VG_STROKE_LINE_WIDTH, strokew / size);
}
unsigned int count = stringToGlyphs(s, f);
if (count) {
VGfloat *kernX, *kernY;
if (f->Kerning) {
kernX = &glyph_kern[0];
kernY = &glyph_kern[count];
} else {
kernX = kernY = NULL;
}
vgDrawGlyphs(f->vgfont, (VGint) count, glyph_string, kernX, kernY, VG_FILL_PATH | VG_STROKE_PATH, VG_FALSE);
}
if (strokew != 0.0f) {
vgSetf(VG_STROKE_LINE_WIDTH, strokew);
}
}
// TextWidth returns the width of a text string at the specified font and size.
VGfloat TextWidth(const char *s, Fontinfo f, VGint pointsize) {
VGfloat pos[2] = { 0.0f, 0.0f };
unsigned int count = stringToGlyphs(s, f);
if (count) {
vgSetfv(VG_GLYPH_ORIGIN, 2, pos);
VGfloat *kernX, *kernY;
if (f->Kerning) {
kernX = &glyph_kern[0];
kernY = &glyph_kern[count];
} else {
kernX = kernY = NULL;
}
vgDrawGlyphs(f->vgfont, (VGint) count, glyph_string, kernX, kernY, 0, VG_FALSE);
vgGetfv(VG_GLYPH_ORIGIN, 2, pos);
}
return pos[0] * (VGfloat) pointsize;
}
// In TextMid and TextEnd we pass glyph_string directly to Text as
// TextWidth has alreay parsed the string. Saves parsing it twice when we
// know that it cannot have changed.
// TextMid draws text, centered on (x,y)
void TextMid(VGfloat x, VGfloat y, const char *s, Fontinfo f, VGint pointsize) {
VGfloat tw = TextWidth(s, f, pointsize);
Text(x - (tw / 2.0f), y, (char *)glyph_string, f, pointsize);
}
// TextEnd draws text, with its end aligned to (x,y)
void TextEnd(VGfloat x, VGfloat y, const char *s, Fontinfo f, VGint pointsize) {
VGfloat tw = TextWidth(s, f, pointsize);
Text(x - tw, y, (char *)glyph_string, f, pointsize);
}
// TextHeight reports a font's height above baseline
VGfloat TextHeight(Fontinfo f, VGint pointsize) {
return f->AscenderHeight * (VGfloat) pointsize;
}
// TextDepth reports a font's depth (how far under the baseline it goes)
VGfloat TextDepth(Fontinfo f, VGint pointsize) {
return -f->DescenderHeight * (VGfloat) pointsize;
}
// TextLineHeight reports how far between baselines is recommended
VGfloat TextLineHeight(Fontinfo f, VGint pointsize) {
return f->Height * (VGfloat) pointsize;
}
//
// Shape functions
//
// newpath creates path data
// Changed capabilities as others not needed at the moment - allows possible
// driver optimisations.
// Changed again to just clear out and re-use a common path.
static inline VGPath newpath() {
vgClearPath(common_path, VG_PATH_CAPABILITY_APPEND_TO);
return common_path;
}
// Shapes that have constant segment sizes are drawn by just modifying
// the coordinates of the shape's global path. This saves the constant
// allocation/deallocation which seems to periodically stall the RPi's
// gpu. These routines rely on knowledge of how the RPi's vgu routines
// create paths - other implementations my differ!
// CBezier makes a quadratic bezier curve
void Cbezier(VGfloat sx, VGfloat sy, VGfloat cx, VGfloat cy, VGfloat px, VGfloat py, VGfloat ex, VGfloat ey) {
const VGfloat coords[8] = { sx, sy, cx, cy, px, py, ex, ey };
vgModifyPathCoords(cbezier_path, 0, 2, coords);
vgDrawPath(cbezier_path, VG_FILL_PATH | VG_STROKE_PATH);
}
// QBezier makes a quadratic bezier curve
void Qbezier(VGfloat sx, VGfloat sy, VGfloat cx, VGfloat cy, VGfloat ex, VGfloat ey) {
const VGfloat coords[6] = { sx, sy, cx, cy, ex, ey };
vgModifyPathCoords(qbezier_path, 0, 2, coords);
vgDrawPath(qbezier_path, VG_FILL_PATH | VG_STROKE_PATH);
}
// interleave interleaves arrays of x, y into a single array
static void interleave(VGfloat * x, VGfloat * y, VGint n, VGfloat * points) {
while (n--) {
*points++ = *x++;
*points++ = *y++;
}
}
// poly makes either a polygon or polyline
static void poly(VGfloat * x, VGfloat * y, VGint n, VGbitfield flag) {
VGfloat points[n * 2];
VGPath path = newpath();
interleave(x, y, n, points);
vguPolygon(path, points, n, VG_FALSE);
vgDrawPath(path, flag);
}
// Polygon makes a filled polygon with vertices in x, y arrays
void Polygon(VGfloat * x, VGfloat * y, VGint n) {
poly(x, y, n, VG_FILL_PATH);
}
// Polyline makes a polyline with vertices at x, y arrays
void Polyline(VGfloat * x, VGfloat * y, VGint n) {
poly(x, y, n, VG_STROKE_PATH);
}
// Rect makes a rectangle at the specified location and dimensions
void Rect(VGfloat x, VGfloat y, VGfloat w, VGfloat h) {
const VGfloat coords[5] = { x, y, w, h, -w };
vgModifyPathCoords(rect_path, 0, 4, coords);
vgDrawPath(rect_path, VG_FILL_PATH | VG_STROKE_PATH);
}
// Line makes a line from (x1,y1) to (x2,y2)
void Line(VGfloat x1, VGfloat y1, VGfloat x2, VGfloat y2) {
const VGfloat coords[4] = { x1, y1, x2, y2 };
vgModifyPathCoords(line_path, 0, 2, coords);
vgDrawPath(line_path, VG_STROKE_PATH);
}
// Roundrect makes an rounded rectangle at the specified location and dimensions
void Roundrect(VGfloat x, VGfloat y, VGfloat w, VGfloat h, VGfloat rw, VGfloat rh) {
const VGfloat coords[26] = { x + rw / 2.0f, y,
w - rw,
rw / 2.0f, rh / 2.0f, 0.0f, rw / 2.0f, rh / 2.0f,
h - rh,
rw / 2.0f, rh / 2.0f, 0.0f, -rw / 2.0f, rh / 2.0f,
-(w - rw),
rw / 2.0f, rh / 2.0f, 0.0f, -rw / 2.0f, -rh / 2.0f,
-(h - rh),
rw / 2.0f, rh / 2.0f, 0.0f, rw / 2.0f, -rh / 2.0f
};
vgModifyPathCoords(roundrect_path, 0, 9, coords);
vgDrawPath(roundrect_path, VG_FILL_PATH | VG_STROKE_PATH);
}
// Ellipse makes an ellipse at the specified location and dimensions
void Ellipse(VGfloat x, VGfloat y, VGfloat w, VGfloat h) {
const VGfloat coords[12] = { x + w / 2.0f, y,
w / 2.0f, h / 2.0f, 0.0f, -w, 0.0f,
w / 2.0f, h / 2.0f, 0.0f, w, 0.0f
};
vgModifyPathCoords(ellipse_path, 0, 3, coords);
vgDrawPath(ellipse_path, VG_FILL_PATH | VG_STROKE_PATH);
}
// Circle makes a circle at the specified location and dimensions
void Circle(VGfloat x, VGfloat y, VGfloat r) {
Ellipse(x, y, r, r);
}
// Arc makes an elliptical arc at the specified location and dimensions
void Arc(VGfloat x, VGfloat y, VGfloat w, VGfloat h, VGfloat sa, VGfloat aext) {
VGPath path = newpath();
vguArc(path, x, y, w, h, sa, aext, VGU_ARC_OPEN);
vgDrawPath(path, VG_FILL_PATH | VG_STROKE_PATH);
}
// Start begins the picture, clearing a rectangular region with a specified color
void Start(VGint width, VGint height) {
VGfloat color[4] = { 1.0f, 1.0f, 1.0f, 0.0f };
vgSetfv(VG_CLEAR_COLOR, 4, color);
vgClear(0, 0, width, height);
color[0] = color[1] = color[2] = 0.0f;
SetFill(color);
SetStroke(color);
StrokeWidth(0.0f);
vgLoadIdentity();
}
// End checks for errors (if enabled) and renders to the display,
// returns false if an error was detected.
bool End() {
bool success = true;
if (check_errors) {
vg_error_code = vgGetError();
if (vg_error_code != VG_NO_ERROR) {
font_error(vg_error_code, "***End()***");
success = false;
}
}
eglSwapBuffers(state->display, state->surface);
if (check_errors && eglGetError() != EGL_SUCCESS)
success = false;
return success;
}
// SaveEnd dumps the raster before rendering to the display, returns
// false if an error was detected (if enabled).
bool SaveEnd(const char *filename) {
bool success = true;
if (check_errors) {
vg_error_code = vgGetError();
if (vg_error_code != VG_NO_ERROR) {
font_error(vg_error_code, "***End()***");
success = false;
}
}
FILE *fp;
if (strlen(filename) == 0) {
dumpscreen(state->screen_width, state->screen_height, stdout);
} else {
fp = fopen(filename, "wb");
if (fp != NULL) {
dumpscreen(state->screen_width, state->screen_height, fp);
fclose(fp);
}
}
eglSwapBuffers(state->display, state->surface);
if (check_errors && eglGetError() != EGL_SUCCESS)