![]() |
Images in PDF and PostScript output; PDF attachments | ![]() |
How the printers are created and how pages are drawn is explained in renderers at work, and the general structure of the PDF renderer in implementations, new renderers and pitfalls and in PDF export. This page describes how images end up in the output files, and how TeXmacs documents are embedded in, and recovered from, PDF files.
All image related state of pdf_hummus_renderer_rep (
Plugins/Pdf/pdf_hummus_renderer.cpp
) lives in four tables:
Image files, keyed by URL, each with a pdf_image, that is, the file, its size in points and the object identifier which the image will have in the PDF file.
Images used as background patterns and the PDF patterns built on them.
Temporary PNG files for pictures, keyed by the unique identifier of the picture.
The temporary files to remove when the renderer is destroyed.
Drawing an image only writes a reference to an object which does not exist yet; the objects themselves are written when the document is finished (flush_images and flush_patterns, called from the destructor). Each image file is therefore embedded only once, however often it appears.
PDF files are merged directly: the first page of the file is copied into a form object with PDFHummus (CreatePDFCopyingContext, MergePDFPageToFormXObject), using the crop box and rotation of the page (pdf_image_info). If the preference texmacs->pdf:distill inclusion (Distill encapsulated Pdf files) is set, the file is first re-distilled with Ghostscript so that all its fonts are embedded (gs_PDF_EmbedAllFonts). A warning is issued when the included file has a higher PDF version than the one selected for the output.
JPEG files are embedded as they are (flush_jpg, CreateImageXObjectFromJPGFile), and PNG files through PDFHummus' PNG reader (flush_png).
Any other format is converted to a temporary PDF file with image_to_pdf at a maximum of 300 dots per inch, and merged like a PDF file. Vector formats which can be converted to PDF stay vectorial.
If the size of the merged page differs from the size recorded when the image was registered, the transformation is rescaled.
printer_rep (
Graphics/Renderer/printer.cpp
) includes images as encapsulated PostScript, between @beginspecial and @endspecial in the style of dvips:
draw_scalable loads the image as PostScript code with ps_load, which converts other formats through image_to_eps, takes its bounding box (from the size cache for EPS files, from the generated code otherwise), and inserts it scaled to the size of the box;
draw_picture encodes the picture with picture_as_eps and inserts it in the same way.
Plugins/Cairo/
contains a cairo_renderer_rep, which loads the Cairo library dynamically (
tm_cairo.cpp
) and draws PNG images directly, converting PostScript and PDF images with convert. It is only compiled with USE_CAIRO, which the CMake build never sets, and has not followed the evolution of the renderer interface (see older renderers).
A PDF file exported with File\(\rightarrow\)Export\(\rightarrow\)Pdf with embedded document contains the TeXmacs source of the document, and the external files it uses, as embedded files. File\(\rightarrow\)Import\(\rightarrow\)Pdf with embedded document recovers them. The C++ side is in
Plugins/Pdf/pdf_hummus_make_attachment.cpp
and
pdf_hummus_extract_attachment.cpp
; without PDF_RENDERER all functions are stubs which fail.
Open pdf for modification with PDFHummus (PDFWriter::ModifyPDF), add each file as an embedded file stream named after the last component of its path, register them in the /Names/EmbeddedFiles tree of the catalog (PDFAttachmentWriter::OnCatalogWrite), and write the result to out. When out is pdf, PDFHummus appends an incremental update to the file. All files must exist; otherwise nothing is done and the function returns false. Exported as pdf-make-attachments.
Read the embedded files of pdf and write each of them, under its embedded name, in the directory of the PDF file (relative (pdf, name)), appending the paths to names. scm_extract_attachments (exported as extract-attachments) does the same without returning the names, and get_main_tm (pdf-get-attached-main-tm) extracts again and returns the first file, which by convention is the main document.
Two helpers operate on document trees: get_linked_file_paths (t, path) (pdf-get-linked-file-paths) collects the files referenced by image and include tags and the style files which are not part of TeXmacs; replace_with_relative_path (t, path) (pdf-replace-linked-path) rewrites these references as bare file names, so that they point to the extracted copies.
On the Scheme side, export is done by wrapped-print-to-pdf-embeded-with-tm and attach-doc-to-exported-pdf (
texmacs/texmacs/tm-print.scm
). The latter prints the document, saves a copy of it (with its attachments and auxiliary data) in a temporary buffer named after the PDF file, and calls pdf-make-attachments with this copy followed by the linked files. Import is done by wrapped-import-pdf-embeded-with-tm (
texmacs/menus/file-menu.scm
), which extracts the attachments, rewrites the paths of the main document, saves it as extracted.tm in a temporary directory and opens it. The export of a selection as PDF (embbed-tm-selection-in-pdf in
convert/images/tmimage.scm
) attaches the selection in the same way.
extract_attachments_from_pdf (Plugins/Pdf/pdf_hummus_extract_attachment.cpp:131-135) writes every embedded file next to the PDF file, under the name stored in the PDF. Importing paper.pdf which was exported from paper.tm in the same directory therefore overwrites paper.tm (and the linked images) with the embedded copies, without asking; the temporary directory created by wrapped-import-pdf-embeded-with-tm is only used for the final extracted.tm. The embedded names are not sanitized either: a name containing .. or an absolute path writes outside that directory, which is a security problem for PDF files of unknown origin.
get_main_tm (pdf_hummus_extract_attachment.cpp:385-390) extracts all attachments a second time and returns attachments_paths[0] without checking that the extraction succeeded; on a PDF without attachments the list is empty and the access fails.
get_linked_file_paths also returns referenced files which do not exist, and pdf_hummus_make_attachments refuses to do anything as soon as one of its files is not a regular file. A single missing image therefore prevents the document from being attached at all (the user only sees “Fail to attach tm to pdf”).
In pdf_image_rep::flush (pdf_hummus_renderer.cpp:1765-1777), the fallback to a PNG file is never reached, because image_to_pdf copies unknown.pdf to its destination when it fails: an image which could be displayed as a bitmap but not converted to PDF is printed as the “unknown” placeholder. When the fallback is taken, its temporary PNG file is not removed.
The PostScript printer ignores effects: printer_rep::draw_scalable (Graphics/Renderer/printer.cpp:929-946) only checks the kind of the scalable and embeds the original file, so images with effects print without them, unlike in the PDF renderer. It also calls ps_load, and thus a conversion, each time an image is drawn, with no cache.
The PDFHummus renderer writes pictures and pattern images as temporary PNG files, with QImage under Qt and with save_picture under Cocoa (the two ports for which it is compiled); the MuPDF writer (mupdf_pdf_renderer_rep, the one of Vue and SDL) embeds them itself.