Skip to main content

Module graphics

Module graphics 

Source
Expand description

This module defines the graphics feature (Graphics), which provides the command \includegraphics[options]{path}. The command inserts an image, that is, an external picture file, into the document. For instance, \includegraphics[alt=A plot, width=5cm]{figures/plot.png} inserts the picture of the file figures/plot.png, 5cm wide, with the alternative text A plot. A path can also be an address, such as https://example.org/a.svg.

The library never opens a file itself. The program that embeds FLM can contribute services that find, measure and convert image files, as described below; the flm program contributes them with its feature graphics-files. Without these services, every output refers to an image by its path as written.

The language guide describes the command for authors (Graphics). The rendering guide shows what each output writes (images and how an output reaches them).

§Where the command can be written

\includegraphics is declared in the scoped package INCLUDEGRAPHICS_PACKAGE (flm.graphics.includegraphics). The command is therefore available only in the bodies and arguments whose declarations refer to this package. Elsewhere, it is an unknown command. With the floats feature, the float types figure and table refer to this package, so that an image can be written as the content of a figure:

\begin{figure}
  \includegraphics{figures/plot.png}
  \caption{A plot of the data.}
\end{figure}

With the configuration GraphicsConfig::global, \includegraphics is also declared as an ordinary command, which can be written wherever text can be written.

An image is a block when it is a direct child of the body of a float, as in the figure above. Everywhere else, an image is inline content, like a word of the text around it (GraphicsImage).

§The options and the path

The optional argument contains key-value pairs: the alternative text alt, and the size width and height. The module options reads the options and defines the diagnostics that are reported for them. An invalid option is reported as an error and ignored, and the image is written as usual.

The mandatory argument contains the path of the image file as raw text, up to the matching }. The characters %, #, ~, \ and nested braces are part of the path. The path is read when the image is lowered. Therefore, a placeholder in the path of a user definition’s template is replaced by the characters of the corresponding argument of the call. For instance, if the template of \fig is \includegraphics{figs/#1.png}, the call \fig{plot} inserts the image figs/plot.png. A path that is not plain text is reported as the error PathNotCharacters, and an empty path as the error EmptyPath. In both cases, no image is written.

§The size of an image

The size at which an image is shown is computed when the image is lowered (shown_size):

  • When both width and height are given, the image has this size.
  • When only one of them is given, the other dimension is computed from the size of the image file, so that the image keeps its proportions. If the size of the file is not known, only the given dimension is set.
  • When neither is given, the image has the size of the image file.

The size of the image file is known when an inspector, one of the services described below, has reported it. The size is then multiplied by the magnification of the output: one factor for raster images and another for vector images (GraphicsRenderOptions::magnification). Both factors are 1 by default. When no size is known, the warning UnknownSize is reported, and the image is written without a size.

§Services for image files

A service is a value that a feature, or the program that embeds FLM, contributes to a service slot declared in another feature (core::services). The graphics feature declares three service slots, for the work that needs access to files or to installed programs. None of these services is required.

§Stages and lowering rules

The feature registers two kinds of stages, which call the services:

  • The inspect stage INSPECT_STAGE (flm.graphics.inspect) calls the graphics resolver and the inspector for each image. It writes the results into the table INFO (flm.graphics.info), keyed by the address of the image’s node.
  • The publish stage flm.graphics.publish.<backend> (PublishStage) is registered for each of the backends text, html, latex-recomposed and latex-rendered. It computes the address at which the output of the backend refers to each image, and writes this address into the table flm.graphics.published.<backend> (published_table). The address depends on the strategy that the render options set for the output: the address of the file where it is (reference, the default), the address of a copy of the file that is made part of the output (copy), or a data: address that contains the content of the file (data-uri). Under the strategy content, the stage also writes the content of each image file into the table flm.graphics.content.<backend> (content_table), for an output that renders images itself. See the modules strategy and publish. A stage of the program that embeds FLM can write the published table in place of this stage (a publish stage of your own).

On the progress sheet of a build (crate::progress), the inspect stage declares no row of its own: the run counts its trees. A call of a publish stage that converts images declares a row of the kind IMAGES_KIND (flm.graphics.images), counted in the images it converts, and under it a row of the kind CONVERT_KIND (flm.graphics.convert, “Converting graphics”) for each conversion, whose scope the converter receives.

The lowering rules of the image element read these tables:

  • On the backends text, html and latex-rendered, the rule image_rule lowers an image to a flm.pres.image node. For instance, the plain-text output prints this node as [image: <alternative text or address>], and the HTML output writes it as an <img> element.
  • On the backend latex-recomposed, the rule LATEX_RULE writes the LaTeX hook \flmIncludeGraphics[<keys>]{<path>} (latex).

The stages and the rules of each backend are registered with a peer declaration of the backend (PeerName). A registry without one of these backends therefore still builds.

§Settings

The configuration GraphicsConfig is the value from which the feature is constructed, and is fixed when the registry is built. The render options GraphicsRenderOptions are set for each render request. For each output, they contain the strategy (strategy), the conversion rules (convert, ConversionRule) and the magnification (magnification, Magnification). See settings, options and configuration.

§Images in floats

The feature contributes the content handler includegraphics to the floats feature (handler). A float type that lists this handler, as figure and table do by default, accepts a body that contains exactly one image besides its caption and labels.

In a registry without the floats feature, this contribution is dropped with a note. To use images without floats, set GraphicsConfig::global.

Re-exports§

pub use convert::CONVERTERS_SLOT;
pub use convert::ConversionOptions;
pub use convert::ConversionRequest;
pub use convert::ConversionRule;
pub use convert::GraphicsConvertError;
pub use convert::GraphicsConverter;
pub use convert::NoConverter;
pub use convert::choose_converter;
pub use handler::IncludeGraphicsContent;
pub use inspect::Dimensions;
pub use inspect::GraphicsInfo;
pub use inspect::GraphicsInspectError;
pub use inspect::GraphicsInspectRequest;
pub use inspect::GraphicsInspectedInformation;
pub use inspect::GraphicsInspector;
pub use inspect::GraphicsRecord;
pub use inspect::PixelSize;
pub use publish::CONVERT_KIND;
pub use publish::GraphicsContent;
pub use publish::GraphicsPublished;
pub use publish::IMAGES_KIND;
pub use publish::ImageContent;
pub use publish::OutputImages;
pub use publish::PublishStage;
pub use publish::PublishedImage;
pub use publish::content_table;
pub use publish::output_images;
pub use publish::published_table;
pub use resolve::GraphicsResolveError;
pub use resolve::GraphicsResolveRequest;
pub use resolve::GraphicsResolver;
pub use resolve::ImageFile;
pub use resolve::ImageFormat;
pub use resolve::RESOLVER_SLOT;
pub use resolve::ResolvedImage;
pub use resolve::names_scheme;
pub use strategy::Factor;
pub use strategy::FactorError;
pub use strategy::GraphicsRenderOptions;
pub use strategy::Magnification;
pub use strategy::Strategy;
pub use rules::ImageRule;
pub use rules::UnknownSize;
pub use rules::image_rule;
pub use inspect::INFO;
pub use inspect::INSPECT_STAGE;

Modules§

convert
Turning an image of one format into another: the converters that a host or an extension supplies (GraphicsConverter), the conversion rules set for an output (ConversionRule), and the choice of a converter for a rule (choose_converter).
handler
The content handler that the graphics feature contributes to the floats feature: includegraphics, which accepts exactly one image.
inspect
This module defines the inspector of the graphics feature (GraphicsInspector), a service that reports the type and the size of image files. It also defines the inspect stage INSPECT_STAGE, which calls the inspector for each image and writes a record of each image into the table INFO, along with the error conditions that the inspect stage reports.
latex
The lowering rule of the graphics feature for the latex-recomposed backend, which writes an image as the LaTeX hook \flmIncludeGraphics, and the feature’s definition piece, which defines that hook.
options
This module reads the options of an image, which are written in the optional argument of \includegraphics[options]{path}. It also defines the diagnostic conditions that are reported for these options.
publish
This module defines the publish stages of the graphics feature (PublishStage), along with the tables that these stages write and the error conditions that they report. A publish stage computes the address at which an output refers to each image.
resolve
Finding and reading the file of an image: the graphics resolver that a host supplies (GraphicsResolver), what it returns (ImageFile, ResolvedImage), and the formats of image files (ImageFormat).
rules
This module defines the lowering rule image_rule of the graphics feature, along with what every lowering rule of the image element shares: the computation of the size at which an image is shown (shown_size), and the warning UnknownSize, which is reported when this size is unknown.
strategy
How each output publishes its images: the strategies (Strategy), and the render options (GraphicsRenderOptions) that choose, per output, a strategy, the conversion rules and the magnification of images (Magnification).

Structs§

EmptyPath
The error that is reported when the path of \includegraphics is empty (flm.graphics.empty-path).
Graphics
The graphics feature, which provides the command \includegraphics for images. See the module documentation.
GraphicsConfig
The configuration of the graphics feature, which selects where \includegraphics can be written.
GraphicsImage
The data of the element value of \includegraphics (IMAGE_ELEMENT).
PathNotCharacters
The error that is reported when the path of \includegraphics is not plain text (flm.graphics.path-not-characters).

Constants§

COMMAND
The name of the command, without the backslash, includegraphics.
FEATURE
The name under which the Graphics feature is registered: graphics.
IMAGE_ELEMENT
The name of the element of \includegraphics: flm.graphics.image. The data of its element value is GraphicsImage.
INCLUDEGRAPHICS_PACKAGE
The name of the scoped package that contains the declaration of \includegraphics: flm.graphics.includegraphics.
INSPECTOR_SLOT
The name of the service slot of the inspector: flm.graphics.inspector. A feature or the program that embeds FLM can contribute at most one GraphicsInspector to this slot (Arity::Single). The graphics feature works without an inspector. See the module inspect.
OPTIONS_ARG
The name of the optional argument of \includegraphics: options. The argument contains the options of the image as key-value pairs (see the module options).
PATH_ARG
The name of the mandatory argument of \includegraphics: path. The argument contains the path of the image file as raw text, up to the matching }.
PATH_ARG_ROLE
The name of the argument role flm.graphics.path, which marks an argument that contains the path of an image file. The path argument PATH_ARG of \includegraphics has this role (ArgDef::role).

Functions§

arguments
Returns the declarations of the two arguments of \includegraphics, in order: the optional argument OPTIONS_ARG, which contains key-value pairs, and the mandatory argument PATH_ARG, which contains the path as raw text up to the matching }.
feature
Returns the Graphics feature with the default configuration, ready to be listed in a package entry.