Skip to content

Architecture ​

Layers ​

  • Interpreter (foresight_script, foresight_tokens, foresight_datafile, foresight_expression, foresight_smooth, foresight_hulls, foresight_time, foresight_fit, foresight_stats): turns gnuplot statements into calls of the figure API. It lives in the library, so the command line tool is a thin shell and Fortran code can run scripts too. Errors are returned, not stopped on, which lets a watch loop survive a bad cycle.
  • Model (foresight_figure, foresight_axes, foresight_axis, foresight_series, foresight_style, foresight_theme, foresight_annotations): panels, axes with gnuplot range semantics, series with their style, labels, arrows and objects, the page colors of a theme.
  • 3D (foresight_view3d, foresight_pm3d, foresight_contour, foresight_dgrid3d): gnuplot's 3D transform and layout (graph3d), the pm3d quadrangles in gnuplot's order with its z clipping and lighting (pm3d.c), the contour lines (contour.c), the gridding of scattered data (dgrid3d). A 3D view is drawn as gnuplot's painter does: series projected to the page, the pm3d, zerrorfill, boxes and polygons of a view in one depth queue (pm3d_queue) sorted farthest first.
  • Math (foresight_mathlib, foresight_specfun, foresight_gammafun, foresight_bessel): gnuplot's special functions to its digits.
  • Rules (foresight_ticks, foresight_format, foresight_readout, foresight_palette, foresight_colormaps, foresight_enhanced, foresight_font): tick placement and labels, number formatting, the seven-segment cells a readout format makes and the segments a value lights, the palettes from a value to a color (gnuplot's formulae, gradients and cubehelix; the tables of the named palettes, generated); gnuplot's enhanced text and the font metrics of its layout.
  • Devices (foresight_backend and its extensions): an abstract drawing interface with two coordinate spaces — page pixels for decorations, the unit square of the plot area for data. Readouts are a semantic primitive (readout, readout_extent), not polygons: each device draws digits its own way, hexagons in SVG, _ and | in text. foresight_png encodes the pixels of an image as a base64 PNG for the SVG.
  • System (foresight_sys, foresight_fingerprint): the libc calls standard Fortran lacks (rename for atomic rewrites, nanosleep between the cycles of the watch mode and for pause, popen for system() and backquotes), and the cheap file fingerprints that tell the watch mode a file changed.

Design decisions ​

Expressions compiled once. A using expression is parsed by recursive descent into stack code, once per plot item; each row then runs that code, without parsing again. A user function is compiled inline where it is called; a recursive one is compiled once after the main code and run on a call stack; a function block calls back into the interpreter, which runs its lines. Values carry gnuplot's integer, real, string or undefined type. Every floating point operation is checked before it runs (zero divisor, domain, overflow), because Fortran's .and. does not short-circuit. So evaluation never raises an IEEE exception, and the debug build, trapping them, proves it.

Data in the unit square. Data are mapped to [0, 1] per axis (after the log transform) and drawn inside a clipped area. An interactive device zooms by changing one view transform; the text device rasterises the same coordinates. Coordinates are written with 6 decimals, which allows zooming by about 10⁵ before the steps show.

Exact labels. A tick value is the integer pair (n, e) meaning n * 10**e; its label is assembled from the digits of n. Every number written to a file goes through integer arithmetic, because the processor-dependent parts of Fortran edit descriptors differ between compilers: golden files are byte-exact.

One set of tick rules. The viewer regenerates ticks after a zoom with a line-by-line JavaScript port of foresight_ticks, including the single-rounding power-of-ten scaling, so both sides agree to the last bit; the same test cases run on both (src/tests/foresight_ticks_test.F90, src/js/viewer_test.js).

Device-measured text. Margins depend on label widths; each device reports them (text_width): estimated for vector formats, whose viewer renders the glyphs, exact for character cells.

Decorations regenerated, data never re-plotted. Grid, ticks and error bar caps are named groups the viewer rebuilds; data geometry is written once. Each series is a numbered group, which is all the viewer needs to hide it from its key entry, or to find the points it shows when following a live run.

One text layout, two resolutions. The block device extends the dumb one: same cells, same text, but data drawn through two overridable primitives (px_segment, px_point) onto a bitmap of 2 x 4 dots at most per cell, written as Unicode block or Braille characters.

Atomic output. Write to <file>.tmp, then rename: live viewers never read partial files.

Bounded work. Tick generation stops at 1000 ticks; the text device clips segments before rasterising. Degenerate ranges and far outliers cannot turn into runaway loops or allocations.

The viewer script ​

src/js/viewer.js is the source; scripts/embed_js.sh (fobis rule --ex embed-js) generates src/lib/foresight_viewer_js.F90, one write statement per line, which is committed. Regenerate after every edit of the script; the generator rejects lines longer than 100 characters, so that each generated line fits 132 columns.

Released under the GPL v3, BSD 2-Clause, BSD 3-Clause or MIT License.