GNU TeXmacsUser manualReferenceAboutEnglish
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.

1.Images in the PDF renderer

All image related state of pdf_hummus_renderer_rep (

Plugins/Pdf/pdf_hummus_renderer.cpp

) lives in four tables:

image_pool

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.

pattern_image_pool, pattern_pool

Images used as background patterns and the PDF patterns built on them.

picture_cache

Temporary PNG files for pictures, keyed by the unique identifier of the picture.

temp_images

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.

Scalable images.
draw_scalable embeds image files directly. If the scalable has an effect, it falls back on the default implementation, which rasterizes the image at the current resolution and calls draw_picture. Otherwise it calls the private method image (u, w, h, x, y, alpha), which allocates the pdf_image if needed and writes, in the page content, a transformation which scales the object from its natural size to the requested size, followed by a Do operator for the object, with the requested opacity.
Pictures.
draw_picture saves the picture as a temporary PNG file (with Qt; other builds report an error), caches the file name by the unique identifier of the picture, and then calls image like for a file. Pictures come from effects and from shadows, which the printer rasterizes at a fixed resolution (shadow sets the zoom factor to 5.0 * PICTURE_ZOOM).
Writing the image objects.
pdf_image_rep::flush turns the file into a PDF form object with the allocated identifier:
  1. 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.

  2. JPEG files are embedded as they are (flush_jpg, CreateImageXObjectFromJPGFile), and PNG files through PDFHummus' PNG reader (flush_png).

  3. 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.

Patterns.
register_pattern_image loads a background image at the size of one tile with get_image (the Qt image loader, so effects of patterns are applied), saves it as a temporary PNG file and creates a tiling pattern which draws it. flush_for_pattern writes the image as a raw RGB image with a separate soft mask for the alpha channel (qt_image_data).
Sizes.
hummus_pdf_image_size (image, w, h) parses the first page of a PDF file with PDFHummus and returns the size of its crop box, in points, with width and height exchanged for pages rotated by 90 or 270 degrees. It is the preferred way of measuring PDF images (see image files).

2.Images in the PostScript renderer

printer_rep (

Graphics/Renderer/printer.cpp

) includes images as encapsulated PostScript, between @beginspecial and @endspecial in the style of dvips:

3.The Cairo renderer

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).

4.PDF attachments

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.

bool pdf_hummus_make_attachments (url pdf, array<url> files, url out)
(embed files)

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.

bool extract_attachments_from_pdf (url pdf, list<url>& names)
(extract files)

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.

5.Pitfalls

Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.1 or any later version published by the Free Software Foundation; with no Invariant Sections, with no Front-Cover Texts, and with no Back-Cover Texts. A copy of the license is included in the section entitled "GNU Free Documentation License".