forked from jheinen/GR.jl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GR.jl
4300 lines (3661 loc) · 132 KB
/
GR.jl
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
__precompile__()
"""
GR is a universal framework for cross-platform visualization applications.
It offers developers a compact, portable and consistent graphics library
for their programs. Applications range from publication quality 2D graphs
to the representation of complex 3D scenes.
See https://gr-framework.org/julia.html for full documentation.
Basic usage:
```julia
using GR
GR.init() # optional
plot(
[0, 0.2, 0.4, 0.6, 0.8, 1.0],
[0.3, 0.5, 0.4, 0.2, 0.6, 0.7]
)
# GR.show() # Use if in a Jupyter Notebook
```
"""
module GR
@static if isdefined(Base, :Experimental) &&
isdefined(Base.Experimental, Symbol("@optlevel"))
Base.Experimental.@optlevel 1
end
if Sys.KERNEL == :NT
const os = :Windows
else
const os = Sys.KERNEL
end
const depsfile = joinpath(dirname(@__DIR__), "deps", "deps.jl")
const depsfile_succeeded = Ref(true)
# Include Builder module in case we need to rebuild
const buildfile = joinpath(dirname(@__DIR__), "deps", "build.jl")
if os == :Windows
const libGR = "libGR.dll"
const libGR3 = "libGR3.dll"
const libGRM = "libGRM.dll"
elseif os == :Darwin
const libGR = "libGR.dylib"
const libGR3 = "libGR3.dylib"
const libGRM = "libGRM.dylib"
else
const libGR = "libGR.so"
const libGR3 = "libGR3.so"
const libGRM = "libGRM.so"
end
const attempt_to_rebuild = Ref(true)
const libGR_handle = Ref{Ptr{Nothing}}()
const libGR3_handle = Ref{Ptr{Nothing}}()
const libGRM_handle = Ref{Ptr{Nothing}}()
const gr_provider = Ref("Unknown")
import Base64
import Libdl
export
init,
initgr,
opengks,
closegks,
inqdspsize,
openws,
closews,
activatews,
deactivatews,
clearws,
updatews,
polyline,
polymarker,
text,
inqtext,
fillarea,
cellarray,
nonuniformcellarray,
polarcellarray,
nonuniformpolarcellarray,
gdp,
path,
spline,
gridit,
setlinetype,
setlinewidth,
setlinecolorind,
setmarkertype,
setmarkersize,
setmarkercolorind,
settextfontprec,
setcharexpan,
setcharspace,
settextcolorind,
setcharheight,
inqcharheight,
setcharup,
settextpath,
settextalign,
setfillintstyle,
setfillstyle,
setfillcolorind,
setcolorrep,
setscale,
inqscale,
setwindow,
inqwindow,
setviewport,
inqviewport,
selntran,
setclip,
setwswindow,
setwsviewport,
createseg,
copyseg,
redrawseg,
setsegtran,
closeseg,
emergencyclosegks,
updategks,
setspace,
textext,
inqtextext,
axes2d, # to avoid WARNING: both GR and Base export "axes"
axeslbl,
grid,
grid3d,
verrorbars,
herrorbars,
polyline3d,
polymarker3d,
axes3d,
titles3d,
surface,
volume,
contour,
hexbin,
setcolormap,
colorbar,
inqcolor,
inqcolorfromrgb,
hsvtorgb,
tick,
validaterange,
adjustlimits,
adjustrange,
beginprint,
beginprintext,
endprint,
ndctowc,
wctondc,
wc3towc,
drawrect,
fillrect,
drawarc,
fillarc,
drawpath,
setarrowstyle,
setarrowsize,
drawarrow,
readimage,
drawimage,
importgraphics,
setshadow,
settransparency,
setcoordxform,
begingraphics,
endgraphics,
getgraphics,
drawgraphics,
mathtex,
inqmathtex,
selectcontext,
destroycontext,
delaunay,
interp2,
trisurface,
tricontour,
# gradient, # deprecated, but still in Base
quiver,
reducepoints,
version,
check_for_updates,
openmeta,
sendmeta,
sendmetaref,
closemeta,
shadepoints,
shadelines,
setcolormapfromrgb,
setborderwidth,
setbordercolorind,
setprojectiontype,
setperspectiveprojection,
setorthographicprojection,
settransformationparameters,
setresamplemethod,
setwindow3d,
setspace3d,
text3d,
inqtext3d,
settextencoding,
inqtextencoding,
loadfont,
inqvpsize,
# Convenience functions
jlgr,
colormap,
figure,
gcf,
hold,
usecolorscheme,
subplot,
plot,
oplot,
semilogx,
semilogy,
loglog,
stairs,
scatter,
stem,
barplot,
histogram,
polarhistogram,
contourf,
heatmap,
polarheatmap,
nonuniformpolarheatmap,
wireframe,
plot3,
scatter3,
redraw,
title,
xlabel,
ylabel,
drawgrid,
xticks,
yticks,
zticks,
xticklabels,
yticklabels,
legend,
xlim,
ylim,
savefig,
meshgrid,
peaks,
imshow,
isosurface,
cart2sph,
sph2cart,
polar,
trisurf,
tricont,
shade,
panzoom,
setpanzoom,
libGR3,
gr3,
libGRM,
isinline,
inline,
displayname,
mainloop
const ENCODING_LATIN1 = 300
const ENCODING_UTF8 = 301
const grdir = Ref("")
const grdir_default = joinpath(dirname(@__FILE__), "..", "deps", "gr")
const display_name = Ref("")
const mime_type = Ref("")
const file_path = Ref("")
const figure_count = Ref(-1)
const send_c = Ref(C_NULL)
const recv_c = Ref(C_NULL)
const text_encoding = Ref(ENCODING_UTF8)
const check_env = Ref(true)
isijulia() = isdefined(Main, :IJulia) && Main.IJulia isa Module && isdefined(Main.IJulia, :clear_output)
isatom() = isdefined(Main, :Atom) && Main.Atom isa Module && Main.Atom.isconnected() && (isdefined(Main.Atom, :PlotPaneEnabled) ? Main.Atom.PlotPaneEnabled[] : true)
ispluto() = isdefined(Main, :PlutoRunner) && Main.PlutoRunner isa Module
isvscode() = isdefined(Main, :VSCodeServer) && Main.VSCodeServer isa Module && (isdefined(Main.VSCodeServer, :PLOT_PANE_ENABLED) ? Main.VSCodeServer.PLOT_PANE_ENABLED[] : true)
# Load function pointer caching mechanism
include("funcptrs.jl")
function __init__()
#= __init__ Objectives:
1. Set gr_provider[] to either "BinaryBuilder" or "GR" based on depsfile
2. Set grdir[] global variable, defer to load_libs if gr_provider[] == "BinaryBuilder"
3. If grdir[] cannot be set, try to rebuild.
=#
# depsfile (deps/deps.jl) should contain some parseable Julia code
contents = nothing
# Initially depsfile_succeeded[] is true
if depsfile_succeeded[]
try
contents = isfile(depsfile) ? strip( read(depsfile, String) ) : ""
catch err
depsfile_succeeded[] = false
@debug "Parsing depsfile failed" depsfile contents err
end
end
# If the first line is either "import GR_jll" or "using GR_jll" then
# we are using the BinaryBuilder method
if contents == "import GR_jll" || contents == "using GR_jll"
gr_provider[] = "BinaryBuilder"
else
gr_provider[] = "GR"
end
@debug "GR Binaries:" GR.gr_provider[] GR.libGR GR.libGR3 GR.libGRM
# Determine grdir
# The environmental variable GRDIR can override the binary provider
# If GRDIR is "", then it will force a rebuild.
if "GRDIR" in keys(ENV)
grdir[] = ENV["GRDIR"]
elseif gr_provider[] == "BinaryBuilder"
# Defer until load_libs() when we import GR_jll
#grdir = joinpath(dirname(GR_jll.libGR_path), "..")
grdir[] = "deferred"
else
grdir[] = ""
for d in ("/opt", "/usr/local", "/usr")
if isdir(joinpath(d, "gr", "fonts"))
grdir[] = joinpath(d, "gr")
break
end
end
end
if grdir[] == "" || !depsfile_succeeded[]
grdir[] = grdir_default
if isdir(grdir[]) && gr_provider[] == "GR"
# No need to rebuild, just use grdir_default as grdir
else
# Rebuild if no file at grdir_default or gr_provider[] == "BinaryBuilder"
if attempt_to_rebuild[]
attempt_to_rebuild[] = false # Avoid infinite loop
println("Your GR installation is incomplete. Rerunning build step for GR package.")
delete!(ENV, "GRDIR")
if !depsfile_succeeded[]
# If depsfile failed, then GR_jll failed.
# Switch to GR on next build.
ENV["JULIA_GR_PROVIDER"] = "GR"
@info "Switching provider to GR due to error in depsfile" depsfile
end
@eval GR begin
include(buildfile)
Builder.build()
end
# Try to read depsfile again
depsfile_succeeded[] = true
GR.__init__()
@info "GR was successfully rebuilt"
return
end
error("""
GR was not built correctly and could not be automatically rebuilt.
$(grdir[]) is not a directory.
GR_jll could not be loaded.
Run the following commands:
ENV["GRDIR"] = ""
using Pkg; Pkg.build("GR")
""")
end
end
# init(true) is deferred until load_libs
end
"""
function set_callback()
callback_c = @cfunction(callback, Cstring, (Cstring, ))
ccall(libGR_ptr(:gr_setcallback),
Nothing,
(Ptr{Cvoid}, ),
callback_c)
end
"""
"""
init(always::Bool = false)
Initialize GR's environmental variables before plotting and ensure that the
binary shared libraries are loaded. Initialization usually only needs to be
done once, but reinitialized may be required when settings change.
The `always` argument is true if initialization should be forced in the
current and subsequent calls. It is `false` by default so that
initialization only is done once.
This is distinct from the GR module's `__init__` which only locates the
shared binary libraries to be used.
# Extended Help
Environmental variables which influence `init`:
GRDISPLAY - if "js" or "pluto", javascript support is initialized
GKS_NO_GUI - no initialization is done
GKS_IGNORE_ENCODING - Force use of UTF-8 for font encoding, ignore GKS_ENCODING
Environmental variables set by `init`:
GRDIR - directory of GR shared library files
GKS_FONTPATH - path to GR fonts, often the same as GRDIR
GKS_USE_CAIRO_PNG
GKSwstype - Graphics workstation type, see help for `openws`
GKS_QT - Command to start QT backend via gksqt executable
GKS_ENCODING - Sets the text encoding (e.g. Latin1 or UTF-8)
"""
function init(always::Bool = false)
if !libs_loaded[]
load_libs(always)
return
end
if check_env[] || always
ENV["GRDIR"] = grdir[]
ENV["GKS_FONTPATH"] = grdir[]
ENV["GKS_USE_CAIRO_PNG"] = "true"
if "GRDISPLAY" in keys(ENV)
display_name[] = ENV["GRDISPLAY"]
if display_name[] == "js" || display_name[] == "pluto"
send_c[], recv_c[] = js.initjs()
end
@debug "Found GRDISPLAY in ENV" display_name[]
elseif "GKS_NO_GUI" in keys(ENV)
@debug "Found GKS_NO_GUI in ENV, returning"
return
elseif isijulia() || ispluto() || isvscode() || isatom()
mime_type[] = "svg"
file_path[] = tempname() * ".svg"
ENV["GKSwstype"] = "svg"
ENV["GKS_FILEPATH"] = file_path[]
@debug "Found an embedded environment" mime_type[] file_path[] ENV["GKSwstype"] ENV["GKS_FILEPATH"]
elseif gr_provider[] == "BinaryBuilder"
if !haskey(ENV, "GKSwstype")
ENV["GKSwstype"] = "gksqt"
end
if os == :Windows
if !haskey(ENV, "GKS_QT")
ENV["GKS_QT"] = string("set PATH=", GR_jll.LIBPATH[], " & \"", GR_jll.gksqt_path, "\"")
elseif ENV["GKS_QT"] == ""
ENV["PATH"] = GR_jll.LIBPATH[]
gkqst = run(`$(GR_jll.gksqt_path)`; wait = false)
# gksqt(gkscmd -> run(`gksqt`; wait=false))
end
else
env = (os == :Darwin) ? "DYLD_FALLBACK_LIBRARY_PATH" : "LD_LIBRARY_PATH"
ENV["GKS_QT"] = string("env $env=", GR_jll.LIBPATH[], " ", GR_jll.gksqt_path)
end
@debug "BinaryBuilder Setup" ENV["GKSwstype"] os ENV["GKS_QT"] ENV["PATH"] GR_jll.LIBPATH[] GR_jll.gksqt_path
end
if "GKS_IGNORE_ENCODING" in keys(ENV)
text_encoding[] = ENCODING_UTF8
@debug "Found GKS_IGNORE_ENCODING in ENV" text_encoding[]
elseif "GKS_ENCODING" in keys(ENV)
if ENV["GKS_ENCODING"] == "latin1" || ENV["GKS_ENCODING"] == "latin-1"
text_encoding[] = ENCODING_LATIN1
else
text_encoding[] = ENCODING_UTF8
end
@debug "Found GKS_ENCODING in ENV" text_encoding[]
else
ENV["GKS_ENCODING"] = "utf8"
@debug "Default GKS_ENCODING" ENV["GKS_ENCODING"]
end
check_env[] = always
end
end
function initgr()
ccall( libGR_ptr(:gr_initgr),
Nothing,
()
)
end
function opengks()
ccall( libGR_ptr(:gr_opengks),
Nothing,
()
)
end
function closegks()
ccall( libGR_ptr(:gr_closegks),
Nothing,
()
)
end
# (Information taken from <https://www.iterm2.com/utilities/imgcat>.)
# tmux requires unrecognized OSC sequences to be wrapped with DCS
# tmux; <sequence> ST, and for all ESCs in <sequence> to be replaced
# with ESC ESC. It only accepts ESC backslash for ST. We use TERM
# instead of TMUX because TERM gets passed through ssh.
function osc_seq()
if startswith(get(ENV, "TERM", ""), "screen")
"\033Ptmux;\033\033]"
else
"\033]"
end
end
function st_seq()
if startswith(get(ENV, "TERM", ""), "screen")
"\a\033\\"
else
"\a"
end
end
function inqdspsize()
mwidth = Cdouble[0]
mheight = Cdouble[0]
width = Cint[0]
height = Cint[0]
ccall( libGR_ptr(:gr_inqdspsize),
Nothing,
(Ptr{Cdouble}, Ptr{Cdouble}, Ptr{Cint}, Ptr{Cint}),
mwidth, mheight, width, height)
return mwidth[1], mheight[1], width[1], height[1]
end
"""
openws(workstation_id::Int, connection, workstation_type::Int)
Open a graphical workstation.
**Parameters:**
`workstation_id` :
A workstation identifier.
`connection` :
A connection identifier.
`workstation_type` :
The desired workstation type.
Available workstation types:
+-------------+------------------------------------------------------+
| 5|Workstation Independent Segment Storage |
+-------------+------------------------------------------------------+
| 7, 8|Computer Graphics Metafile (CGM binary, clear text) |
+-------------+------------------------------------------------------+
| 41|Windows GDI |
+-------------+------------------------------------------------------+
| 51|Mac Quickdraw |
+-------------+------------------------------------------------------+
| 61 - 64|PostScript (b/w, color) |
+-------------+------------------------------------------------------+
| 101, 102|Portable Document Format (plain, compressed) |
+-------------+------------------------------------------------------+
| 210 - 213|X Windows |
+-------------+------------------------------------------------------+
| 214|Sun Raster file (RF) |
+-------------+------------------------------------------------------+
| 215, 218|Graphics Interchange Format (GIF87, GIF89) |
+-------------+------------------------------------------------------+
| 216|Motif User Interface Language (UIL) |
+-------------+------------------------------------------------------+
| 320|Windows Bitmap (BMP) |
+-------------+------------------------------------------------------+
| 321|JPEG image file |
+-------------+------------------------------------------------------+
| 322|Portable Network Graphics file (PNG) |
+-------------+------------------------------------------------------+
| 323|Tagged Image File Format (TIFF) |
+-------------+------------------------------------------------------+
| 370|Xfig vector graphics file |
+-------------+------------------------------------------------------+
| 371|Gtk |
+-------------+------------------------------------------------------+
| 380|wxWidgets |
+-------------+------------------------------------------------------+
| 381|Qt4 |
+-------------+------------------------------------------------------+
| 382|Scaleable Vector Graphics (SVG) |
+-------------+------------------------------------------------------+
| 390|Windows Metafile |
+-------------+------------------------------------------------------+
| 400|Quartz |
+-------------+------------------------------------------------------+
| 410|Socket driver |
+-------------+------------------------------------------------------+
| 415|0MQ driver |
+-------------+------------------------------------------------------+
| 420|OpenGL |
+-------------+------------------------------------------------------+
| 430|HTML5 Canvas |
+-------------+------------------------------------------------------+
"""
function openws(workstation_id::Int, connection, workstation_type::Int)
ccall( libGR_ptr(:gr_openws),
Nothing,
(Int32, Ptr{Cchar}, Int32),
workstation_id, connection, workstation_type)
end
"""
closews(workstation_id::Int)
Close the specified workstation.
**Parameters:**
`workstation_id` :
A workstation identifier.
"""
function closews(workstation_id::Int)
ccall( libGR_ptr(:gr_closews),
Nothing,
(Int32, ),
workstation_id)
end
"""
activatews(workstation_id::Int)
Activate the specified workstation.
**Parameters:**
`workstation_id` :
A workstation identifier.
"""
function activatews(workstation_id::Int)
ccall( libGR_ptr(:gr_activatews),
Nothing,
(Int32, ),
workstation_id)
end
"""
deactivatews(workstation_id::Int)
Deactivate the specified workstation.
**Parameters:**
`workstation_id` :
A workstation identifier.
"""
function deactivatews(workstation_id::Int)
ccall( libGR_ptr(:gr_deactivatews),
Nothing,
(Int32, ),
workstation_id)
end
function clearws()
ccall( libGR_ptr(:gr_clearws),
Nothing,
()
)
end
function updatews()
ccall( libGR_ptr(:gr_updatews),
Nothing,
()
)
end
"""
polyline(x, y)
Draw a polyline using the current line attributes, starting from the
first data point and ending at the last data point.
**Parameters:**
`x` :
A list containing the X coordinates
`y` :
A list containing the Y coordinates
The values for `x` and `y` are in world coordinates. The attributes that
control the appearance of a polyline are linetype, linewidth and color
index.
"""
function polyline(x, y)
@assert length(x) == length(y)
n = length(x)
ccall( libGR_ptr(:gr_polyline),
Nothing,
(Int32, Ptr{Float64}, Ptr{Float64}),
n, convert(Vector{Float64}, x), convert(Vector{Float64}, y))
end
"""
polymarker(x, y)
Draw marker symbols centered at the given data points.
**Parameters:**
`x` :
A list containing the X coordinates
`y` :
A list containing the Y coordinates
The values for `x` and `y` are in world coordinates. The attributes that
control the appearance of a polymarker are marker type, marker size
scale factor and color index.
"""
function polymarker(x, y)
@assert length(x) == length(y)
n = length(x)
ccall( libGR_ptr(:gr_polymarker),
Nothing,
(Int32, Ptr{Float64}, Ptr{Float64}),
n, convert(Vector{Float64}, x), convert(Vector{Float64}, y))
end
function latin1(string)
if text_encoding[] == ENCODING_UTF8
# add null character '\0' for SubString types (see GR.jl SubString issue #336)
if typeof(string) == SubString{String}
return string * "\0"
else
return string
end
end
b = unsafe_wrap(Array{UInt8,1}, pointer(string), sizeof(string))
s = zeros(UInt8, sizeof(string) * 2)
len = 0
mask = 0
for c in b
if mask == -1
mask = 0
continue
end
if c == 0xce || c == 0xcf
len += 1
s[len] = 0x3f
mask = -1
continue
end
if c != 0xc2 && c != 0xc3
len += 1
s[len] = c | mask
end
if c == 0xc3
mask = 0x40
else
mask = 0
end
end
return s[1:len]
end
"""
text(x::Real, y::Real, string)
Draw a text at position `x`, `y` using the current text attributes.
**Parameters:**
`x` :
The X coordinate of starting position of the text string
`y` :
The Y coordinate of starting position of the text string
`string` :
The text to be drawn
The values for `x` and `y` are in normalized device coordinates.
The attributes that control the appearance of text are text font and precision,
character expansion factor, character spacing, text color index, character
height, character up vector, text path and text alignment.
"""
function text(x::Real, y::Real, string)
ccall( libGR_ptr(:gr_text),
Nothing,
(Float64, Float64, Ptr{UInt8}),
x, y, latin1(string))
end
function inqtext(x, y, string)
tbx = Cdouble[0, 0, 0, 0]
tby = Cdouble[0, 0, 0, 0]
ccall( libGR_ptr(:gr_inqtext),
Nothing,
(Float64, Float64, Ptr{UInt8}, Ptr{Cdouble}, Ptr{Cdouble}),
x, y, latin1(string), tbx, tby)
return tbx, tby
end
"""
fillarea(x, y)
Allows you to specify a polygonal shape of an area to be filled.
**Parameters:**
`x` :
A list containing the X coordinates
`y` :
A list containing the Y coordinates
The attributes that control the appearance of fill areas are fill area interior
style, fill area style index and fill area color index.
"""
function fillarea(x, y)
@assert length(x) == length(y)
n = length(x)
ccall( libGR_ptr(:gr_fillarea),
Nothing,
(Int32, Ptr{Float64}, Ptr{Float64}),
n, convert(Vector{Float64}, x), convert(Vector{Float64}, y))
end
"""
cellarray(xmin::Real, xmax::Real, ymin::Real, ymax::Real, dimx::Int, dimy::Int, color)
Display rasterlike images in a device-independent manner. The cell array
function partitions a rectangle given by two corner points into DIMX X DIMY
cells, each of them colored individually by the corresponding color index
of the given cell array.
**Parameters:**
`xmin`, `ymin` :
Lower left point of the rectangle
`xmax`, `ymax` :
Upper right point of the rectangle
`dimx`, `dimy` :
X and Y dimension of the color index array
`color` :
Color index array
The values for `xmin`, `xmax`, `ymin` and `ymax` are in world coordinates.
"""
function cellarray(xmin::Real, xmax::Real, ymin::Real, ymax::Real, dimx::Int, dimy::Int, color)
if ndims(color) == 2
color = reshape(color, dimx * dimy)
end
ccall( libGR_ptr(:gr_cellarray),
Nothing,
(Float64, Float64, Float64, Float64, Int32, Int32, Int32, Int32, Int32, Int32, Ptr{Int32}),
xmin, xmax, ymin, ymax, dimx, dimy, 1, 1, dimx, dimy, convert(Vector{Int32}, color))
end
"""
nonuniformcellarray(x, y, dimx::Int, dimy::Int, color)
Display a two dimensional color index array with nonuniform cell sizes.
**Parameters:**
`x`, `y` :
X and Y coordinates of the cell edges
`dimx`, `dimy` :
X and Y dimension of the color index array
`color` :
Color index array
The values for `x` and `y` are in world coordinates. `x` must contain `dimx` + 1 elements
and `y` must contain `dimy` + 1 elements. The elements i and i+1 are respectively the edges
of the i-th cell in X and Y direction.
"""
function nonuniformcellarray(x, y, dimx::Int, dimy::Int, color)
@assert dimx <= length(x) <= dimx+1 && dimy <= length(y) <= dimy+1
if ndims(color) == 2
color = reshape(color, dimx * dimy)
end
nx = dimx == length(x) ? -dimx : dimx
ny = dimy == length(y) ? -dimy : dimy
ccall( libGR_ptr(:gr_nonuniformcellarray),
Nothing,
(Ptr{Float64}, Ptr{Float64}, Int32, Int32, Int32, Int32, Int32, Int32, Ptr{Int32}),
convert(Vector{Float64}, x), convert(Vector{Float64}, y), nx, ny, 1, 1, dimx, dimy, convert(Vector{Int32}, color))
end
"""
polarcellarray(xorg::Real, yorg::Real, phimin::Real, phimax::Real, rmin::Real, rmax::Real, imphi::Int, dimr::Int, color)
Display a two dimensional color index array mapped to a disk using polar
coordinates.
**Parameters:**
`xorg` :
X coordinate of the disk center in world coordinates
`yorg` :
Y coordinate of the disk center in world coordinates
`phimin` :
start angle of the disk sector in degrees
`phimax` :
end angle of the disk sector in degrees
`rmin` :
inner radius of the punctured disk in world coordinates
`rmax` :
outer radius of the punctured disk in world coordinates
`dimiphi`, `dimr` :
Phi (X) and iR (Y) dimension of the color index array
`color` :
Color index array
The two dimensional color index array is mapped to the resulting image by
interpreting the X-axis of the array as the angle and the Y-axis as the radius.
The center point of the resulting disk is located at `xorg`, `yorg` and the
radius of the disk is `rmax`.
"""
function polarcellarray(xorg::Real, yorg::Real, phimin::Real, phimax::Real, rmin::Real, rmax::Real,
dimphi::Int, dimr::Int, color)
if ndims(color) == 2
color = reshape(color, dimphi * dimr)
end
ccall( libGR_ptr(:gr_polarcellarray),
Nothing,
(Float64, Float64, Float64, Float64, Float64, Float64, Int32, Int32, Int32, Int32, Int32, Int32, Ptr{Int32}),
xorg, yorg, phimin, phimax, rmin, rmax, dimphi, dimr, 1, 1, dimphi, dimr, convert(Vector{Int32}, color))
end
"""
nonuniformpolarcellarray(x, y, dimx::Int, dimy::Int, color)
Display a two dimensional color index array mapped to a disk using nonuniform
polar coordinates.
**Parameters:**
`x`, `y` :
X and Y coordinates of the cell edges
`dimx`, `dimy` :
X and Y dimension of the color index array
`color` :
Color index array
The two dimensional color index array is mapped to the resulting image by
interpreting the X-axis of the array as the angle and the Y-axis as the radius.
"""
function nonuniformpolarcellarray(x, y, dimx::Int, dimy::Int, color)
@assert dimx <= length(x) <= dimx+1 && dimy <= length(y) <= dimy+1
if ndims(color) == 2
color = reshape(color, dimx * dimy)
end
nx = dimx == length(x) ? -dimx : dimx
ny = dimy == length(y) ? -dimy : dimy
ccall( libGR_ptr(:gr_nonuniformpolarcellarray),
Nothing,
(Float64, Float64, Ptr{Float64}, Ptr{Float64}, Int32, Int32, Int32, Int32, Int32, Int32, Ptr{Int32}),
0, 0, convert(Vector{Float64}, x), convert(Vector{Float64}, y), nx, ny, 1, 1, dimx, dimy, convert(Vector{Int32}, color))
end
"""
gdp(x, y, primid, datrec)
Generates a generalized drawing primitive (GDP) of the type you specify,
using specified points and any additional information contained in a data
record.
**Parameters:**
`x` :
A list containing the X coordinates
`y` :
A list containing the Y coordinates
`primid` :
Primitive identifier
`datrec` :
Primitive data record
"""
function gdp(x, y, primid, datrec)
@assert length(x) == length(y)
n = length(x)
ldr = length(datrec)
ccall( libGR_ptr(:gr_gdp),
Nothing,
(Int32, Ptr{Float64}, Ptr{Float64}, Int32, Int32, Ptr{Int32}),
n, convert(Vector{Float64}, x), convert(Vector{Float64}, y),
primid, ldr, convert(Vector{Int32}, datrec))
end
"""
path(x, y, codes)
Draw paths using the given vertices and path codes.