File: prog.tex

package info (click to toggle)
gle-graphics-manual 4.3.8-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 1,876 kB
  • sloc: perl: 67; sh: 24; makefile: 14
file content (610 lines) | stat: -rw-r--r-- 20,237 bytes parent folder | download | duplicates (3)
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
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%                                                                      %
% GLE - Graphics Layout Engine <http://glx.sourceforge.io/>            %
%                                                                      %
% Modified BSD License                                                 %
%                                                                      %
% Copyright (C) 2009 GLE.                                              %
%                                                                      %
% Redistribution and use in source and binary forms, with or without   %
% modification, are permitted provided that the following conditions   %
% are met:                                                             %
%                                                                      %
%    1. Redistributions of source code must retain the above copyright %
% notice, this list of conditions and the following disclaimer.        %
%                                                                      %
%    2. Redistributions in binary form must reproduce the above        %
% copyright notice, this list of conditions and the following          %
% disclaimer in the documentation and/or other materials provided with %
% the distribution.                                                    %
%                                                                      %
%    3. The name of the author may not be used to endorse or promote   %
% products derived from this software without specific prior written   %
% permission.                                                          %
%                                                                      %
% THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR   %
% IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED       %
% WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE   %
% ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY       %
% DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL   %
% DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE    %
% GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS        %
% INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER %
% IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR      %
% OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN  %
% IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.                        %
%                                                                      %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\chapter{Programming Facilities}

\section{Expressions}
\index{variables}
\index{expressions}
Wherever GLE is expecting a number it can be replaced with an expression.  For
example

\preglecode{}
\begin{Verbatim}
     rline 3 2
\end{Verbatim}
\postglecode{}

and

\preglecode{}
\begin{Verbatim}
     rline 9/3 sqrt(4)
\end{Verbatim}
\postglecode{}

will produce the same result.

An expression in GLE is delimited by white space, so it may not contain any
spaces - `{\sf rline 3*3 2}' is valid but `{\sf rline 3 * 3 2}' will not work.

Or `{\sf let d2 = 3+sin(d1)}' will work and `{\sf let d2= 3 + sin(d1)}' won't.

Expressions may contain numbers, arithmetic operators ({\sf +}, {\sf -}, {\sf *},
{\sf /}, \verb+^+ (to the power of)), relational operators ($>$, $<$, $=>$, $<=$,
$=$, $<>$) Boolean operators ({\sf and}, {\sf or}), variables and built-in functions.

When GLE is expecting a colour or marker name (like `green' or `circle')
it can be given a string variable, or an expression enclosed in braces.

\section{Functions Inside Expressions}

\begin{figure}[tb]
\centering
\mbox{\includegraphics{prog/fig/axisformat}}
\caption{\label{axformat:fig}Examples of number formatting options.}
\end{figure}

\begin{commanddescription}

\item[{\sf eval(str)}]
\index{eval()}
Evaluates the given string as if it was a GLE expression and returns the result. E.g., \texttt{eval("3+4")} returns 7.

\item[{\sf arg(i), arg\$(i), nargs()}]
\label{args:cmd}
\index{arg()}
\index{arg\$()}
\index{nargs()}
Provide access to the command line arguments that are passed to GLE. This is useful for generating multiple similar plots from a single script. arg(i) returns the i-the argument (as a number), arg\$(i) returns the i-the argument as a string, and nargs() returns the number of arguments. Only arguments that come after the name of the GLE script are counted. For example, if GLE is run as:

\begin{Verbatim}
     gle -o graph-1.eps graph.gle "Title" 0.5
\end{Verbatim}

\noindent{}then nargs() returns 2, arg\$(1) returns ``Title'', and arg(2) returns 0.5.

The typical use of these functions is to create a script ``graph.gle'' as follows:

\begin{Verbatim}
size 10 10
begin graph
   title arg$(1)
   data arg$(2)
   d1 line color red
end graph
\end{Verbatim}

\noindent{}and then creating different graphs by running GLE multiple times:

\begin{Verbatim}
     gle -o beans.eps graph.gle "Beans" "beans.csv"
     gle -o peas.eps graph.gle "Peas" "peas.csv"
\end{Verbatim}

\noindent{}This will create two graphs: ``beans.eps'' and ``peas.eps''. The arg() functions can be used at all places in the script where an expression is expected. They can even be used in place of GLE commands in a graph block by means of the $\backslash{}$expr() function. For example,

\begin{Verbatim}
     data "file.csv"
     d\expr{arg(1)} line color red
\end{Verbatim}

\noindent{}in the graph block will allow one to draw different datasets from a single file on multiple graphs. To do so, run:

\begin{Verbatim}
     gle -o d1.eps graph.gle 1
     gle -o d2.eps graph.gle 2
\end{Verbatim}

\item[{\sf dataxvalue({\it ds},{\it i}), datayvalue({\it ds},{\it i}), ndata({\it ds})}]
\index{dataxvalue}\label{dataxvalue}
\index{datayvalue}\label{datayvalue}
\index{ndata}\label{ndata}

These functions can be used to retrieve the data values from a given dataset. They only work after data has been loaded by means of a graph block (Ch.~\ref{graph:chap}).

A dataset is specified with a dataset identifier {\it ds} (a string of the form ``d$i$'', with $i$ an integer). The function `{\sf ndata}' returns the number of points in the dataset, and the functions `{\sf dataxvalue}' and `{\sf datayvalue}' return the $x$ and $y$ values of point {\it i}, where {\it i} ranges from 1 to {\sf ndata({\it ds})}.

The following example shows how these functions can be used to compute the maximum of a dataset:

\begin{Verbatim}
sub dmaxy ds$
   local crmax = datayvalue(ds$,1)
   for i = 2 to ndata(ds$)
      crmax = max(crmax, datayvalue(ds$,i))
   next i
   return crmax
end sub
\end{Verbatim}

This subroutine together with other subroutines for computing the minimum, mean, area, etc. of a dataset are defined in the include file `graphutil.gle', which is distributed together with GLE.

\item[{\sf format\$({\it exp},{\it format})}]
\index{format\$()} \label{formatnum:pg}

Returns a string representation of {\it exp} formatted as specified in {\it format}.

Basic formats:
\begin{itemize}
\item {\sf append {\it s}}: appends the string $s$ after the formatted number. This can be used to add a unit.

\item {\sf dec}, {\sf hex [upper|lower]}, {\sf bin}: format as decimal, hexadecimal (upper-case or lower-case), or binary.

\item {\sf fix} {\it places}: format with {\it places} decimal places.

\item {\sf percent} {\it places}: format as ({\it exp} $\cdot 100$)\% with {\it places} decimal places.

\item {\sf sci} {\it sig} {\sf [e,E,10]} {\sf [expdigits {\it num}]} {\sf [expsign]}: format in scientific notation with {\it sig} significant digits. Use `e', `E', or `10' as notation for the exponent. With the option {\sf expdigits} the number of digits in the exponent can be set and {\sf expsign} forces a sign in the exponent.

\item {\sf eng} {\it digits} {\sf [e,E,10]} {\sf [expdigits {\it num}]} {\sf [expsign]} {\sf [num]}: format in engineering notation. The options are similar to `sci'. If the option `num' is given, then numeric notation is used instead of, e.g., $\mu$, m, k, M.

\item {\sf round} {\it sig}: format a number with {\it sig} significant digits.

\item {\sf frac}: format the number as a fraction.

\item {\sf pi}: format the number as a fraction times $\pi$ (E.g., xaxis labels of Fig.~\ref{grscale:fig}).
\end{itemize}

Options for all formats:
\begin{itemize}
\item {\sf nozeroes}: remove unnecessary zeroes at the end of the number.

\item {\sf sign}: also include a sign for positive numbers.

\item {\sf pad {\it nb} [left] [right]}: pad the result with spaces from the left or right.

\item {\sf prefix {\it nb}}: prefix the number with zeroes so that {\it nb} digits are obtained.

\item {\sf prepend {\it s}}: prepends the string $s$ before the formatted number.

\item {\sf min {\it val}}: use format for numbers $\ge$ {\it val}.

\item {\sf max {\it val}}: use format for numbers $\le$ {\it val}.
\end{itemize}

Examples:
\begin{itemize}
\item format\$(3.1415, ``fix 2'') = 3.14
\item format\$(3756, ``round 2'') = 3800
\item format\$(3756, ``sci 2 10 expdigits 2'') = $3.8\cdot10^{03}$
\end{itemize}

Several formats can be combined into one string: "sci 2 10 min 1e2 fix 0" uses scientific notations for numbers above $10^2$ and decimal notation for smaller numbers. See Fig.~\ref{axformat:fig} for more examples.

\item[{\sf pagewidth(), pageheight()}]
\index{pagewidth()} \index{pageheight()}
These functions return the width and height of the output. These are the values set with the `\texttt{size}' command.

\item[{\sf pointx(), pointy()}]\label{fct:pointxy}
These functions return the x and y values of a named point.

\preglecode{}
\begin{Verbatim}
     begin box add 0.1 name mybox
         write "Hello"
     end box
     amove pointx(mybox.bc) pointy(mybox.bc)
     rline 0 -2 arrow end
\end{Verbatim}
\postglecode{}

\item[{\sf twidth(str), theight(str), tdepth(str)}]
\index{twidth()} \index{theight()}
These functions return the width, depth and height of a string, if it was
printed in the current font and size.

\item[{\sf width(obj), height(obj)}]\label{fct:wdhi}
\index{width()} \index{height()}
These functions return the width and height of a named object.

\item[{\sf xend(), yend()}]
\index{xend()} \index{yend()}
These functions return the end point of the last thing drawn. This is
of particular interest when drawing text.

\preglecode{}
\begin{Verbatim}
     text abc
     set color blue
     text def
\end{Verbatim}
\postglecode{}

This would draw the {\sf def} on top of the {\sf abc}.  To draw the {\sf def}
immediately following the {\sf abc} simply do the following (Note that
absolute move is used, not relative move):

\begin{minipage}[c]{8cm}
\begin{Verbatim}
set just left
text abc
set color gray20 
amove xend() yend() 
text def            
\end{Verbatim}
\end{minipage}
\hfill
\begin{minipage}[c]{7cm}
\mbox{\includegraphics{primitives/fig/gc_xend}}
\end{minipage}

\item[{\sf xg(), yg()}]
\index{xg()} \index{yg()}
\index{functions}
With these functions it is possible to move to a position on a graph
using the graph's axis units.
To draw a filled box on a graph, at position x=948, y=.004  measured
on the graph axis:

\preglecode{}
\begin{Verbatim}
     begin graph
        xaxis min 100 max 2000
        yaxis min -.01 max .01
        ...
     end graph
     amove xg(948) yg(.004)
     box 2 2 fill gray10
\end{Verbatim}
\postglecode{}

\item[{\sf xpos(), ypos()}]
\index{xpos()} \index{ypos()}
Returns the current x and y points.
\end{commanddescription}

\noindent{}See Appendix~\ref{fct:sec} for an overview of all functions provided by GLE.

\section{Using Variables}
\index{variables}
\index{string!variables}

GLE has two types of variables,  floating point and string. String 
variables always end with a dollar sign.  A string variable contains
text like ``Hello, this is text'', a floating point variable can
only store numbers like 1234.234.

\preglecode{}
\begin{Verbatim}
name$ = "Joe"
height = 6.5   ! Height of person
shoe = 0.05    ! shoe adds to height of person
amove 1 1
box 0.2 height+shoe
write name$
\end{Verbatim}
\postglecode{}

\section{String constants}
\index{string!constants}

String constants can be double quoted or single quoted. To include a double quote character in a double quoted string it should be doubled. The same holds for a single quoted string. Backslash characters are not interpreted in a special way by GLE's parser. (They are, however, interpreted by the built-in \TeX{} system used in the `{\sf write}' command.) Here are some examples:\\

\textbf{(11/2022) NOTE that including a double quote character as described below throws an error.  This is a known bug in GLE.}

\preglecode{}
\begin{Verbatim}
print "Double quoted string"
print 'Single quoted string'
print "Between ""double quotes"""
print "{\it hello}"
print "Three double quotes """""""
print """"
\end{Verbatim}
\postglecode{}

The result of these print commands is:

\preglecode{}
\begin{Verbatim}
Double quoted string
Single quoted string
Between "double quotes"
{\it hello}
Three double quotes """
"
\end{Verbatim}
\postglecode{}

\section{Programming Loops}
\index{loops}
\index{for}
\index{next}

The simple way to draw a 6 $\times$ 8 grid would be to use a whole mass 
of line commands:

\preglecode{}
\begin{Verbatim}
amove 0 0 
rline 0 8 
amove 1 0 
rline 1 8 
...
amove 6 0 
rline 6 8
\end{Verbatim}
\postglecode{}

this would be laborious to type in, and would become 
impossible to manage with several grids.  By using a simple loop
this can be avoided:

\preglecode{}
\begin{Verbatim}
for x = 0 to 6
   amove x 0 
   rline x 8 
next x
for y = 0 to 8
   amove 0 y 
   rline 6 y 
next y 
\end{Verbatim}
\postglecode{}

For-next loops, and all other control constructs, can also be used among others inside graph and key blocks. This is useful for drawing many similar functions (Fig.~\ref{fig:sqroot}). Besides for-next loops, GLE also supports while and until loops:

\index{while}
\preglecode{}
\begin{Verbatim}
i = 0
while i <= 10
   print "Value: " i
   i = i + 1
next
\end{Verbatim}
\postglecode{}

\index{until}
\preglecode{}
\begin{Verbatim}
i = 0
until i > 10
   print "Value: " i
   i = i + 1
next
\end{Verbatim}
\postglecode{}

\section{If-then-else}
\index{if}
\index{then}
\index{else}

GLE supports if-then-else statements as follows:

\preglecode{}
\begin{Verbatim}
if a < 1 then print a "is smaller than 1"
else if a < 2 then print a "is smaller than 2 but larger than 1"
else if a < 3 then print a "is smaller than 3 but larger than 2"
else print a "is larger than 3"
\end{Verbatim}
\postglecode{}

\noindent{}to create blocks of code for the `then' and `else' branches, instead use:

\preglecode{}
\begin{Verbatim}
if a < 1 then
   print a "is smaller than 1"
   ...
else
   ...
end if
\end{Verbatim}
\postglecode{}

More complex conditions can be created with the logic connectives `{\sf and}', `{\sf or}', and `{\sf not}' (note the parenthesis around the logical expressions):

\preglecode{}
\begin{Verbatim}
if (a >= 1) and (a <= 10) then print "a is between 1 and 10"
\end{Verbatim}
\postglecode{}

\section{Subroutines}
\index{subroutines}

To draw lots of grids all of different dimensions a subroutine can
be defined and then used again and again:

\preglecode{}
\begin{Verbatim}
sub grid nx ny 
   local x, y
   begin origin
      for x = 0 to nx
         amove x 0 
         aline x ny 
      next x
      for y = 0 to ny
         amove 0 y 
         aline nx y 
      next y 
   end origin
end sub
     
amove 2 4
grid  6 8
amove 2 2
grid  9 5
\end{Verbatim}
\postglecode{}

Inside a subroutine, the keyword `\texttt{local}'\index{local} can be used to define local variables. E.g., `\texttt{local x = 3}', defines the local variable `x' and assigns it the value 3. It is also possible to define several local variables at once, as is shown in the `grid' example above.

The keyword `\texttt{return}'\index{return} can be used to return a value from a subroutine. E.g.,

\preglecode{}
\begin{Verbatim}
sub gaussian x mu sigma
   return 1/(sigma*sqrt(2*pi))*exp(-((x-mu)^2)/(2*sigma^2))
end sub
\end{Verbatim}
\postglecode{}

The main GLE file will be much easier to manage if subroutine definitions are moved into a separate file:

\preglecode{}
\begin{Verbatim}
include "griddef.gle"

amove 2 4
grid  2 4
amove 2 2
grid  9 5
\end{Verbatim}
\postglecode{}

More information about the ``include''\index{include} command can be found on page~\pageref{incl:cmnd}.

\subsection{Default Arguments}

Given the following subroutine definition:

\preglecode{}
\begin{Verbatim}
sub mysub x y color$ fill$
   default color  "black"
   default fill   "clear"
   print "Color: " color$
   print "Fill:  " fill$
end sub
\end{Verbatim}
\postglecode{}

\noindent{}the following calls are valid:

\preglecode{}
\begin{Verbatim}
mysub 1 0
mysub 1 0 red
mysub 1 0 red green
mysub 1 0 fill blue
mysub 1 0 color red
mysub 1 0 color red fill blue
\end{Verbatim}
\postglecode{}

\section{Forward Declarations}

A forward declaration of a subroutine is possible with:

\preglecode{}
\begin{Verbatim}
declare sub mysub x y
\end{Verbatim}
\postglecode{}

\noindent{}Forward declarations are required for declaring mutually recursive subroutines.

\section{I/O Functions}
\index{I/O-functions}
\index{fopen} \index{fread} \index{freadln}
\index{fclose} \index{fwrite} \index{fwriteln}
\index{fgetline} \index{ftokenizer}

The following I/O functions are available:

\begin{commanddescription}
\item[{\sf fopen {\it name} {\it file} [read$|$write]}]

Open the file ``{\it name}'' for reading or for writing. The resulting file handle is stored in variable ``{\it file}'' and must be passed to all other I/O functions.

\item[{\sf fclose {\it file}}]

Close the given file.

\item[{\sf fread {\it file} {\it x1} $\ldots$}]
\item[{\sf freadln {\it file} {\it x1} $\ldots$}]

Read entries from ``{\it file}'' into given arguments {\it x1} $\ldots$

\item[{\sf fwrite {\it file} {\it x1} $\ldots$}]

Write given arguments to ``{\it file}''.

\item[{\sf fwriteln {\it file} {\it x1} $\ldots$}]

Write given arguments to ``{\it file}'' and start a new line.

\item[{\sf fgetline {\it file} {\it line\$}}]

Read an entire line from ``{\it file}'' and store it in the string ``{\it line\$}''.

\item[{\sf ftokenizer {\it file} {\it commentchar spacetokens singletokens}}]

Sets up the parameters of the tokenizer that controls the reading of ``{\it file}''. The {\it commentchar} parameter specifies the characters that are to be interpreted as line comments. It is a string, but each character of the string is assumed to be a separate comment character. The default is ``!''. If one would write ``!\%'', then both ``!'' and ``\%'' would be comment indicators. The {\sf fread} functions skip everything after a comment character to the end of the line. The {\it spacetokens} string represents the set of characters that are interpreted as spaces or delimiters. The default value is `` ,$\backslash$t$\backslash$r$\backslash$n'', i.e., space, comma, tab, carriage return, and newline are delimiters by default. Finally, the {\it singletokens} string identifies characters that should be returned as separate tokens, even if they are glued to other tokens. For example, if ``@'' would be a single char token, then the string ``me@myself.com'' would be returned in three tokens: ``me'', ``@'', and ``myself.com''. 

\end{commanddescription}

For example:

\preglecode{}
\begin{Verbatim}
fopen "file.dat" f1 read
fopen "file.out" f2 write
until feof(f1)
        fread f1 x y z
        aline x y
        rline x z
        fwriteln f2 x*2 "y =" y
next
fclose f1
fclose f2
\end{Verbatim}
\postglecode{}

\section{Device Dependent Control}
\index{device control}
A built in function which returns a string describing 
the device is available. \\e.g. \verb# DEVICE$() = "HARDCOPY, PS,"#\\
on the postscript driver.

This can be used to use particular fonts etc on appropriate
devices. E.g.:

\preglecode{}
\begin{Verbatim}
                if pos(device$(),"PS,",1)>0 then
                        set font psncsb
                end if 
\end{Verbatim}
\postglecode{}