% $Id: test-luagraphlib.tex 3 2024-02-16 00:36:49Z reinhard $
% Copyright (C) 2019 Reinhard Kotucha <reinhard.kotucha@web.de>
%
% You may freely use, modify, and/or distribute this file.
\font\sf=cmss10
\input luagraphlib
\luagraphicfile{test-luagraphlib.lua}
\luagraphicfile{antpattern.lua} % Antenna radiation patterns
\directlua{default = {
antpattern = {
gainref = 'dipole',
dB_ref = 0,
lambda = function (a) return a[2], a[1] end,
bgcolor = {1, 1, .9},
fillcolor = {0.9, 0.9, 1},
strokecolor = {0.561, 0.078, 0.008},
},
}
}
\def\uncompressed{
\pdfvariable minorversion 4
\pdfvariable compresslevel 0
\pdfvariable objcompresslevel 0
}
%\uncompressed
\begindocument
The box below is entirely typeset with \TeX. Lua is not involved.
We don't use the luagraphic library at all. The markers are from a
special Type~1 font created with MetaType1 where all glyphs have zero
width, height, and depth.
\input mpmarkers
\begin{canvas}(40mm, 17mm)
\at(20mm,10mm) {\MTcross\llap{left}\rlap{right}}
\at(20mm, 5mm) {\MTcross\clap{center}}
\end{canvas}
Now we combine \TeX\ and Lua. Markers are inserted by \TeX, lines by Lua.
\begin{canvas}(100mm, 50mm)
\at(0mm,0mm) {\MTcircle}
\at(50mm,50mm) {\MTcircle}
\at(100mm,0mm) {\MTcircle}
\at(0mm,0mm) {\luagraphic{lines()}}
\end{canvas}
Units. Default unit is 1 millimeter. Other units can be used as well.
\begin{canvas}(70mm-60pt/2, 60mm)
\at(10mm, 0mm) {\luagraphic{units()}}
\end{canvas}
Colors can be specified by a list of components. The color model is
derived from the number of list items: Gray(1), RGB(3), and CMYK(4).
Instead of a list, named colors can be used as defined in {\tt
namedcolors.lua}, for instance {\tt dvips.BrickRed}.
% Instead of \at(0mm, 0mm) we can write \at\origin and instead of
% \at(\w/2, h/2) we can write \at\center.
\begin{canvas}(100mm,50mm)
\at\origin{\luagraphic{colors()}}
\end{canvas}
\newpage
PDF supports operators to fill and stroke objects. Objects are always
first filled and then stroked. We can reverse the order though.
Example~(1) utilizes PDF's operator for {\tt fill} and {\tt stroke},
(2) calls {\tt fill} and {\tt stroke} separately in the same and (3)
in reverse order.
\begin{canvas}(100mm,50mm)
\at\origin{\luagraphic{fill_stroke()}}
\end{canvas}
Some geometric objects can be described more conveniently in polar
coordinates. The example below shows the difference between the
``nonzero winding number rule'' and ``even-odd rule'' filling.
\begin{canvas}(100mm,50mm)
\at\origin {\luagraphic{polar_star()}}
\end{canvas}
Ellipses can be created by changing the transformation matrix. This
leads to non-uniform linewidths (middle). But we can also scale the
path before it is painted (right).
\begin{canvas}(100mm,50mm)
\at\origin{\luagraphic{ellipses()}}
\end{canvas}
%Some Bézier curves:
Some cubic B\'ezier curves. They are described by one start and one
end point and two control points.
\begin{canvas}(100mm,50mm)
\at\origin {\luagraphic{bezier_curves()}}
\end{canvas}
\newpage
Dash patterns. No PDF viewer supports them properly. Missing
elements are marked red. Ghostscript and Adobe Reader are best
(though not perfect), Okular is worse, and Evince is completely
unusable.
\begin{canvas}(100mm,100mm)
\at\origin {\luagraphic{dashpatterns()}}
\end{canvas}
Arcs are implemented as a concatenation of four B\'ezier curves.
Though angles in Lua are in radians we allow users to specify them in
degrees. This is in conformance with PostScript. Usually paths are
specified counterclockwise but$\ldots$
\begin{canvas}(100mm, 50mm)
\at\origin {\luagraphic{arcs()}}
\end{canvas}
$\ldots$we need ``negative arcs'' in some situations too. Paths supposed to
be filled must be contiguous. This means that {\tt moveto()} is only
allowed once at the beginning of a path. In the example below the
lower left arc is running clockwise.
\begin{canvas}(100mm, 50mm)
\at\origin {\luagraphic{negative_arcs()}}
\end{canvas}
\newpage
Now we add text inserted by Lua. Coordinate pairs are still in
millmeters but text size is in pt. Thus text size is the same of
surrounding text (if not deliberately scaled).
Text can be scaled and {\sf dvips} options {\tt slantfont} and {\tt
extendfont} are supported. Text can be left or right aligned or
centered (default). Vertical alignment refers to the base line.
\begin{canvas}(100mm,50mm)
\at\origin{\luagraphic{text_scaling()}}
\end{canvas}
Framed text. The height of the box depends on the shape of the glyphs
therein. If all boxes are supposed to have the same height and depth
you have to add a strut. A strut is an empty box with zero width.
The height is determined by the glyphs therein, in our case `{\tt
Ay}'.
It's obvious that some matrix operations are deprecated because they
cause undesired results.
\begin{canvas}(100mm,50mm)
\at\origin {\luagraphic{framed_text()}}
\end{canvas}
Now we read data from a file. The path is then replaced by a smooth
curve.
\begin{canvas}(150mm, 80mm)
\at\origin{\luagraphic{metartest_1()}}
\end{canvas}
\newpage
Here is a clock like those seen at German railway stations.
\begin{canvas}(190mm,100mm)
\at\origin{\luagraphic{clock()}}
\end{canvas}
Antenna radiation patterns are usually provided in polar coordinates.
\begin{canvas}(100mm, 100mm)
\at(100mm/2, 100mm/2){\luagraphic{antpattern('data/ant/sm701_902h.data')}}
\end{canvas}
\newpage
Are the horizontal lines straight?
\begin{canvas}(116mm, 100mm)
\at\origin {\luagraphic{squares()}}
\end{canvas}
Jiggling Squares (Kees van der Laan at Euro\TeX\ 2012)
\begin{canvas}(100mm, 100mm)
\at\origin{\luagraphic{jiggling()}}
\end{canvas}
\newpage
Arrow heads
\begin{canvas}(100mm, 50mm)
\at\origin{\luagraphic{arrowheads()}}
\end{canvas}
\bye
% Local Variables:
% mode: TeX
% TeX-master: t
% TeX-engine: luatex
% indent-tabs-mode: nil
% coding: utf-8-unix
% End:
% vim:set tabstop=2 expandtab: