Warning: this is an htmlized version!
The original is here, and
the conversion rules are here.
/*
 * This file:
 *   http://anggtwu.net/myqdraw/topdf2.mac.html
 *   http://anggtwu.net/myqdraw/topdf2.mac
 *          (find-angg "myqdraw/topdf2.mac")
 * Author: Eduardo Ochs <eduardoochs@gmail.com>
 *
 * This is a variant of topdf1.mac with better redefiners,
 * and that should also work for mydraw3d.
 *
 * See:
 *   http://anggtwu.net/eev-qdraw.html
 * Author: Eduardo Ochs <eduardoochs@gmail.com>
 * Version: 2025apr19
 * Public domain.
 *
 * This file defines a variant of `myqdraw', called `myqdrawp', that
 * can be configured to work in three modes:
 *
 *   myqdrawp_to_screen()      makes myqdrawp draw to the screen,
 *   myqdrawp_to_single_pdf()  makes myqdrawp always draw into the same PDF,
 *   myqdrawp_to_new_pdf()     makes each myqdrawp draw into a new PDF.
 *
 * See: http://anggtwu.net/eev-maxima.html
 *      (find-windows-beginner-intro "12. Install qdraw")
 *      (find-angg ".maxima/maxima-init.mac" "load_qdraw")
 *      (find-myqdraw "mkanim1.sh")
 *      (find-myqdraw "myqdraw-tex.lisp" "includegraphics")
 *
 * «.myqdrawp»			(to "myqdrawp")
 * «.modes»			(to "modes")
 * «.modes-test»		(to "modes-test")
 * «.filenames»			(to "filenames")
 * «.filenames-test»		(to "filenames-test")
 * «.includegraphics»		(to "includegraphics")
 * «.includegraphics-test»	(to "includegraphics-test")
 * «.ret»			(to "ret")
 * «.ret-test»			(to "ret-test")
 * «.more»			(to "more")
 * «.more-test»			(to "more-test")
 * «.flipbook»			(to "flipbook")
 * «.animation»			(to "animation")
*/

/* «myqdrawp»  (to ".myqdrawp")
 * `myqdrawp' is a wrapper around `myqdraw'
 * that lets three behaviours be redefined...
 *
 *
 * "Screen" mode
 * -------------
 * In "screen" mode `myqdrawp' works like `myqdraw' -
 * it does not increase any counters,
 * it draws to the screen,
 * and it returns a list like this:
 *
 *   [gr2d(parametric)]
 *
 *
 * "New PDF" mode
 * --------------
 * In the "new pdf" mode `myqdrawp':
 * increases a counter,
 * adds a
 *
 *   more(terminal=pdf, file_name="foo_001.pdf")
 *
 * to the drawing arguments to make qdraw draw to a file,
 * and it returns something like this:
 *
 *   \includegraphics[height=5cm]{foo_001.pdf}
 *
 *
 * "Single PDF" mode
 * -----------------
 * There is also a "single pdf" mode, in which `myqdrawp'
 * does not increase any counters and it uses filenames
 * like "foo.pdf" instead of "foo_001.pdf".
*/

/* This part of the code is top-down - it mentions lots of functions
 * that will be defined below.
*/
myqdraw  ([qargs]) := apply('qdraw, flatten([qargs]));
myqdrawp ([qargs]) := (topdf_incr(), topdf_ret(myqdraw(topdf_more(), qargs)));

topdf_ret (o) := o;      /* default: return o instead of an includegraphics */
topdf_more () := [];     /* default: do not add a more(...) to the qargs    */
topdf_incr () := "nop";  /* default: do not increase the counter            */

/* «modes»  (to ".modes")
*/
topdf_to_screen    () := (topdf_use_incr_0(),
                          topdf_ret_o(),
                          topdf_use_dummy_more(),
                          "Will draw to the screen.");
topdf_to_new_pdf   () := (topdf_use_incr_1(),
                          topdf_ret_includegraphics(),
                          topdf_use_name_with_n(),
                          topdf_use_real_more(),
                          "Will always draw to a new PDF.");
topdf_to_single_pdf() := (topdf_use_incr_0(),
                          topdf_ret_includegraphics(),
                          topdf_use_name_single(),
                          topdf_use_real_more(),
                          "Will always draw to the same PDF.");

myqdrawp_to_screen    () := topdf_to_screen    ();
myqdrawp_to_new_pdf   () := topdf_to_new_pdf   ();
myqdrawp_to_single_pdf() := topdf_to_single_pdf();

/* «modes-test»  (to ".modes-test")
* (eepitch-shell)
* (eepitch-kill)
* (eepitch-shell)
ls -lAF /tmp/frame*
rm   -v /tmp/frame*

* (eepitch-maxima)
* (eepitch-kill)
* (eepitch-maxima)
** (find-myqdraw "myqdraw3.mac")
load_myqdraw();
load("topdf2.mac");
[topdf_a,topdf_b,topdf_c] : ["/tmp/", "", "frame"];
drawing1(Dx) := mypara([cos(t),sin(t)]+[Dx,0], t,0,2*%pi);
drawing (Dx) := [xyrange(), drawing1(Dx)];

myqdrawp_to_screen();      myqdrawp(drawing(0));
myqdrawp_to_single_pdf();  myqdrawp(drawing(0.5));
                           myqdrawp(drawing(1.0));
myqdrawp_to_new_pdf();     myqdrawp(drawing(1.5));
                           myqdrawp(drawing(2.0));
topdf_more();

** Inspect:
** (find-fline "/tmp/" "frame_")

*/




/* «filenames»  (to ".filenames")
*/
topdf_a : "~/LATEX/";
topdf_b : "2024-1-C3/";
topdf_c : "test";
topdf_n : 0;
topdf_incr  () := topdf_n : topdf_n + 1;          /* either increase a counter */
topdf_incr  () := "nop";                          /* or a no-op (default)      */
topdf_c_cn  () := format("~a_~3,'0d", topdf_c, topdf_n);
topdf_c     () := topdf_c_cn();                   /* either "foo_001"   */
topdf_c     () := topdf_c;                        /* or "foo" (default) */
topdf_abc   () := format("~a~a~a", topdf_a, topdf_b, topdf_c());
topdf_bcpdf () := format("~a~a.pdf",        topdf_b, topdf_c());

/* Redefiners:
*/
topdf_use_name_single () := (topdf_c   () := topdf_c);
topdf_use_name_with_n () := (topdf_c   () := topdf_c_cn());
topdf_use_incr_1      () := (topdf_incr() := topdf_n : topdf_n + 1);
topdf_use_incr_0      () := (topdf_incr() := "nop");

/*
 * «filenames-test»  (to ".filenames-test")
* (eepitch-maxima)
* (eepitch-kill)
* (eepitch-maxima)
load("topdf2.mac");
[topdf_a,topdf_b,topdf_c] : ["~/LATEX/","2024-1-C3/","test"];
                                    topdf_abc();
topdf_use_name_with_n()$            topdf_bcpdf();
topdf_use_incr_1()$ topdf_incr()$   topdf_bcpdf();
topdf_use_incr_0()$ topdf_incr()$   topdf_bcpdf();
topdf_use_name_single()$            topdf_bcpdf();

*/



/* «includegraphics»  (to ".includegraphics")
 * New: (find-myqdraw "myqdraw-tex.lisp" "includegraphics")
 * Old: topdf_includegraphics() := format("\\includegraphics[~a]{~a}",
 *                                 topdf_opts, topdf_bcpdf());
*/
topdf_opts                   : "height=5cm";
topdf_includegraphics     () := includegraphics(topdf_opts, topdf_bcpdf());

/* «includegraphics-test»  (to ".includegraphics-test")
* (eepitch-maxima)
* (eepitch-kill)
* (eepitch-maxima)
load("topdf2.mac");
o : includegraphics("height=2cm", "foo.pdf");
                           tex1(o);
load("myqdraw-tex.lisp");  tex1(o);

*/


/* «ret»  (to ".ret")
*/
topdf_ret_includegraphics () := (topdf_ret(o) := topdf_includegraphics());
topdf_ret_o               () := (topdf_ret(o) := o);

/* «ret-test»  (to ".ret-test")
* (eepitch-maxima)
* (eepitch-kill)
* (eepitch-maxima)
load("topdf2.mac");
topdf_ret_includegraphics()$   topdf_ret(42);
topdf_ret_o()$                 topdf_ret(42);

*/



/* «more»  (to ".more")
 * (find-es "qdraw" "more")
*/

/* Old: */
topdf_more_body     () := [terminal=pdf, file_name=topdf_abc()];
topdf_use_real_more () := (topdf_more():=apply('more,topdf_more_body()));
topdf_use_dummy_more() := (topdf_more():=[]);

/* New: */
topdf_more00         () := [terminal=pdf, file_name=topdf_abc()];
topdf_more0          () := topdf_more00();
topdf_more           () := apply('more, flatten([topdf_more0()]));
topdf_use_real_more  () := (topdf_more0():=topdf_more00());
topdf_use_dummy_more () := (topdf_more0():=[]);

/* «more-test»  (to ".more-test")
* (eepitch-maxima)
* (eepitch-kill)
* (eepitch-maxima)
load("topdf2.mac");
topdf_use_dummy_more()$ topdf_more();
topdf_use_real_more ()$ topdf_more();

*/





/*  _____ _ _       _                 _    
 * |  ___| (_)_ __ | |__   ___   ___ | | __
 * | |_  | | | '_ \| '_ \ / _ \ / _ \| |/ /
 * |  _| | | | |_) | |_) | (_) | (_) |   < 
 * |_|   |_|_| .__/|_.__/ \___/ \___/|_|\_\
 *           |_|                           
 *
 * «flipbook»  (to ".flipbook")
 * This needs the following Debian packages: qpdf.

** Delete the frames from a previous test:
* (find-sh0 "ls -lAF /tmp/frame*")
* (find-sh0 "rm   -v /tmp/frame*")
*
* (eepitch-maxima)
* (eepitch-kill)
* (eepitch-maxima)
** (find-myqdraw "myqdraw3.mac")
load_myqdraw();
load("topdf2.mac");
[topdf_a,topdf_b,topdf_c,topdf_n] : ["/tmp/", "", "frame", 0];
myqdrawp_to_screen ();
myqdrawp_to_new_pdf();    "Skip this line to draw to the screen"$

[xmin,xmax, ymin,ymax] : [-2,5, -2,3];
drawing1(Dx) := mypara([cos(t),sin(t)]+[Dx,0], t,0,2*%pi);
drawing (Dx) := [xyrange(), drawing1(Dx)];

** Create the frames 001..004 as .pdfs -
** if we're not in debugging/screen mode!
myqdrawp(drawing(0));
myqdrawp(drawing(0.5));
myqdrawp(drawing(1.0));
myqdrawp(drawing(1.5));

** Inspect the frames with:
** (find-fline "/tmp/" "frame_")

* (eepitch-bash)
* (eepitch-kill)
* (eepitch-bash)
add_1zs () { for i in $*; do echo $i 1-z; done; }
                       add_1zs /tmp/frame_*.pdf
qpdf --empty --pages $(add_1zs /tmp/frame_*.pdf) -- /tmp/frames.pdf

ls -lAF /tmp/frame*

** Inspect the flipbook with:
** (find-pdf-page "/tmp/frames.pdf")
*/



/*     _          _                 _   _             
 *    / \   _ __ (_)_ __ ___   __ _| |_(_) ___  _ __  
 *   / _ \ | '_ \| | '_ ` _ \ / _` | __| |/ _ \| '_ \ 
 *  / ___ \| | | | | | | | | | (_| | |_| | (_) | | | |
 * /_/   \_\_| |_|_|_| |_| |_|\__,_|\__|_|\___/|_| |_|
 *                                                    
 * «animation»  (to ".animation")
 * This needs the following Debian packages:
 *   imagemagick   (for convert),
 *   poppler-utils (for pdftoppm),
 *   gifsicle.
 * It converts the flipbook from the previous section to an animated gif.

* (eepitch-shell)
* (eepitch-kill)
* (eepitch-shell)
cutext   () { echo $1 | rev | cut -f2- -d. | rev; }
jpgtogif () { echo convert $1 $(cutext $1).gif;
                   convert $1 $(cutext $1).gif; }

rm -fv   /tmp/frame_*.jpg
rm -fv   /tmp/frame*.gif
ls -lAF  /tmp/frame*
pdftoppm -jpeg -jpegopt quality=80 -r 150 -sep _ /tmp/frames.pdf /tmp/frame
for i in /tmp/frame*.jpg; do jpgtogif $i; done
ls -lAF  /tmp/frame*
gifsicle --colors 256 --loop --delay 20 /tmp/frame_*.gif > /tmp/frames.gif
ls -lAF  /tmp/frame*
rm -fv   /tmp/frame_*.jpg
rm -fv   /tmp/frame_*.gif
ls -lAF  /tmp/frame*

# To open the animated gif in the browser, do:
# (brg "file:///tmp/frames.gif")
#       file:///tmp/frames.gif

# To inspect the gif in Emacs, use `image-mode-map':
#   (find-fline "/tmp/frames.gif")
#   (find-elocus-links "f" 'image-mode-map "image-next-frame")
#   (find-elocus-links "b" 'image-mode-map "image-previous-frame")

*/