1
$\begingroup$

Questions abound on the MSE about how to get a certain import / export feature to work as desired. For instance, here's one that popped up recently: How can I standardize the coordinate origin for exported/imported outlines?

Generally this requires digging into the actual source data. So how can I do that? Even if I look in the Names (e.g. Names["*`*Export*PDF*", IgnoreCase -> True]) I find that the functions aren't loaded until I use them (which I may not want to do) and it's not entirely clear how to get them to load otherwise.

So how can I look at how these types of import / export formats are actually implemented.

$\endgroup$
1
$\begingroup$

This is really a companion post to this question and I'll make use of the scrapers I set up there.

Looking at the PDF case, we can scrape it's export data like so;

getFormatExportData["PDF"]

{"FormatName" -> "PDF", "DocumentedElements" -> None, 
 "Function" -> System`Convert`PDFDump`ExportPDF, 
 "HiddenElements" -> None, "Options" -> {"AllowRasterization"}, 
 "Sources" -> "Convert`PDF`", "BinaryFormat" -> True, 
 "File" -> 
  "/Applications/Mathematica.app/Contents/SystemFiles/Formats/PDF/Export.m"}

And we see this odd "Sources" element which maps to a context that can't be loaded with Get or Needs:

<< Convert`PDF`

Get::noopen: Cannot open Convert`PDF`.

$Failed

Digging further though into ImportExport`RegisterExport we find a curious function called installExport which later calls a function installSource. This is really just a forked Get for the different "Sources" types, which generally just means call Get on the package found on the System`ConvertersDump`$PackagePath.

This is the key function for us, as calling:

System`ConvertersDump`installSource["Convert`PDF`", Automatic];

causes our export function to get loaded:

System`Convert`PDFDump`ExportPDF // DownValues // Length

4

And then we can start to dig, which shows us two interesting things:

First, PDF export really just goes through the FE. In essence it calls:

First@FrontEndExecute@
  ExportPacket[
   System`ConvertersDump`createVectorExportPacketExpr[expr, opts],
   file,
    "ConversionOptions" -> attachments
   ]

Second, we can figure out what some of those opts can be by looking at System`Convert`PDFDump`getOptions. For example, we can control the "OpacityRenderingMethod", "RasterizationResolution", and "VertexColorRenderingMethod" directly in the Export call.

Unfortunately, what these values can be is unclear, as this is pulled directly from the FE, but it gives a sense of how you can spelunk these I/E functions

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.