![]() |
Image files: sizes and conversions | ![]() |
System/Files/image_files.cpp
is the single entry point for everything that concerns image files: their original size and their conversion into the three formats which the rest of TeXmacs needs (PNG for the screen, PDF and EPS for printing). Each function tries the available tools in a fixed order, depending on the build options QTTEXMACS, MUPDF_RENDERER, USE_RESVG, USE_GS, PDF_RENDERER, USE_IMLIB2 and MACOSX_EXTENSIONS, and on the external programs found at run time.
Return the size of an image in points (1/72 inch), from the size cache img_box if possible. Otherwise call image_size_sub and cache the result. If no size can be determined, report an error and use \(35 \times 35\) points.
image_size_sub dispatches on the suffix and on the available tools, in this order:
PDF: pdf_image_size, which uses the crop box of the first page as read by PDFHummus (hummus_pdf_image_size), or, without the PDF renderer, Ghostscript (gs_PDFimage_size), or else ImageMagick. Rotated pages have their width and height exchanged.
SVG: svg_image_size, which tries resvg, then Qt, and finally reads the width and height attributes of the svg element. Both libraries assume 96 pixels per inch.
PostScript and EPS: the %%BoundingBox comment (ps_bounding_box, ps_read_bbox), which also stores the origin of the box in the size cache.
On macOS builds without Qt 6: mac_image_size.
Formats readable by Qt (qt_supports, which excludes PDF and PostScript on purpose, because Qt renders them blurred): qt_image_size, which converts pixels to points with the resolution stored in the file (QImage::dotsPerMeterX).
Imlib2 (X11 builds only).
Ghostscript, for PostScript files without a bounding box: gs_image_size runs the bbox device.
ImageMagick: imagemagick_image_size runs identify and converts with the density recorded in the file; an undefined density is taken as 90 pixels per inch.
Two other functions give sizes in pixels. native_image_size returns the natural size of a raster or SVG image (with resvg or Qt), and otherwise the size in points scaled to 300 pixels per inch. qt_pretty_image_size (in
Plugins/Qt/qt_utilities.cpp
) proposes a width and height for a newly inserted image: its size in points, or 1par if it is wider than the paragraph.
Convert an image to PNG at \(w \times h\) pixels. The tools are tried in this order: the macOS frameworks (builds without Qt 6), Qt (qt_convert_image, which renders SVG files with QtSvg), Ghostscript (gs_to_png, for PostScript and PDF), a Scheme converter (call_scm_converter), and finally ImageMagick. If no output file was produced, unknown.png is copied instead.
Convert an image to a vector format, sized \(w\_\operatorname{pt} \times h\_\operatorname{pt}\) points. Both functions first try to keep vector images vectorial: an SVG file which Qt cannot read goes through a Scheme converter (in practice Inkscape), and PostScript or PDF files go through Ghostscript (gs_to_eps, gs_to_pdf). Raster images are then wrapped with Qt (qt_image_to_pdf, which prints the image with a QPrinter, downsampling it to dpi if it is larger), and as a last resort converted with ImageMagick. image_to_pdf also tries a Scheme converter before ImageMagick. On failure, unknown.eps or unknown.pdf is copied to the destination.
image_to_psdoc (image) converts an image to a temporary EPS file and returns its contents as a string; ps_load (image) returns the contents of PostScript files directly and calls image_to_psdoc for other formats. image_to_psdoc is also exported to Scheme as image->psdoc, and serves as the converter from bitmap formats to postscript-document in
init-images.scm
.
call_scm_converter (image, dest) asks the Scheme converter graph whether a converter exists between the two suffixes (file-converter-exists? on dummy names x.suf) and, if so, runs it with file-convert. It returns whether the destination exists afterwards. The converter graph itself is described in converters to other data formats.
The image formats and their converters are declared in $TEXMACS_PATH/progs/convert/images/init-images.scm with define-format and converter. Each converter has a :require clause which tests for the external program, so the graph adapts to the installation. The main ones are:
to PostScript with Ghostscript (gs-convert, device eps2write); to PNG, JPEG and TIFF with pdftocairo, Ghostscript or ImageMagick, at the resolution given by the preference texmacs->image:raster-resolution (300 by default); to SVG with pdftocairo or pdf2svg.
to PDF with ps2pdf.
to PostScript, PDF and PNG with Inkscape; to PNG with rsvg-convert when Inkscape is absent; to a PostScript document with image->psdoc when Qt 5 or later is available.
Xfig (fig2ps), Xmgrace and Geogebra, to PostScript (and SVG or PNG for Geogebra).
JPEG, TIFF, GIF, PNG and PNM to a PostScript document with image->psdoc, and between each other with ImageMagick's convert.
The same file defines gs-binary, has-gs?, has-convert? and has-pdftocairo?. On Windows, convert is never used, because a system command of that name exists.
Plugins/Ghostscript/gs_utilities.cpp
(compiled with USE_GS, which the CMake build always sets) runs the Ghostscript executable; nothing is linked. The executable is gs (on Windows, the first gswin*c.exe found under C:\Program Files*\gs), or the copy shipped with TeXmacs when GS_EXE is defined. The functions are:
Availability, the quoted command, and the EPS device, eps2write for version 9.14 and later and epswrite before.
True for ps, eps and pdf.
Sizes: the bounding box comment or the bbox device for PostScript; the crop box (or media box) and rotation reported by pdf_info.ps for PDF.
Conversions of images. PostScript inputs are translated by the origin of their bounding box with inline PostScript code, rather than with -dEPSCrop, which mishandles boxes not starting at the origin. gs_to_eps restores the original bounding box afterwards (gs_fix_bbox), because the EPS devices compute their own.
Conversions of whole printed documents, used by edit_main_rep::print_doc when printing to the format which the native printer does not produce.
Re-distill a PDF file with all fonts embedded (see images in PDF output).
Run a document through the nullpage device and report errors.
The PDF version of a file, or the version selected in the preference texmacs->pdf:version (1.4 by default).
Plugins/Ghostscript/ghostscript.cpp
is only used by the X11 port: ghostscript_run renders a PostScript image into an X11 pixmap through the GHOSTVIEW protocol.
ImageMagick is the last resort of all conversions. has_image_magick tests for convert in the path (always false on Windows), and call_imagemagick_convert runs convert, downsampling raster images which are larger than needed for the requested size and resolution. If neither Ghostscript nor ImageMagick is installed, the first failed conversion prints a recommendation to install them (inform_about_dependencies).
The Inkscape converters of
init-images.scm
(lines 152-160) use the command line options -z, -f, -P, -A and --export-png, which were removed in Inkscape 1.0. With a current Inkscape these conversions fail. Since image_to_eps and image_to_pdf try them first for SVG files that Qt cannot read, such images then fall back on rasterization or on the placeholder image.
pdf-file->gs-raster (init-images.scm:95-104) always uses the pngalpha device, although it is registered as the converter from PDF to JPEG and to TIFF as well (lines 311-319): these conversions write PNG data into files with the wrong suffix.
gs_to_eps (Plugins/Ghostscript/gs_utilities.cpp:402-406) passes the input file directly after a -c PostScript fragment, without -f. Ghostscript then interprets the file name as PostScript code, so the conversion of PostScript and EPS inputs fails. gs_to_png and gs_to_pdf use -f correctly.
qt_convert_image (
Plugins/Qt/qt_utilities.cpp:556
) saves im.scaled (w, h) even when w or h is zero, the default of image_to_png; QImage::scaled then returns a null image and nothing is written. All current callers pass a positive size.
The sizes of SVG files depend on the library which reads them (resvg and QtSvg assume 96 pixels per inch, ImageMagick 90), and the first successful answer is cached. The same file may therefore get different sizes on different installations.
The size cache is keyed by URL only. It is invalidated when the picture cache notices that a file has changed, but the typesetter's image boxes keep the old size until the document is retypeset.