Fonts after plot export as pdf

Currently, I’m trying to export a plot to a pdf file using PlotlyJS. This works as expected as long as the fonts aren’t changed.
The following example works fine (except I’ve to rebuild Cairo every time I run julia):

using PlotlyJS
p = plot(scatter(; y = rand(10)))
savefig(p, "test.pdf")

However, this one doesn’t:

p = plot(scatter(; y = rand(10)), Layout(font_family = "Computer Modern"))
savefig(p, "test2.pdf")

While the fonts shown in the plot figure are modified as expected, the fonts inside the exported pdf document won’t be adjusted accordingly.

Note: This issue doesn’t occur exporting to png. However, using eps or svg as the output format leads to the same result.

Thank you in advance!

EDIT: Now it seems like it doesn’t work using png either…

Well, PNG is a raster format, so fonts are rendered prior to export. While PDF can be a vector format ( and eps and svg always are) , which means that the fonts are rendered on the viewing client. PDF has a way to embed custom fonts inside the file. How are you converting to PDF? Can you tell your conversion software to embed the fonts?

Thank you for your prompt response! As far as I know Cairo is used for the pdf export. I assume one has to find a way of providing Cairo with the fonts which shall be used…

It can be tricky to know which font name is to be supplied to Cairo for it to be found successfully. I’ve investigated this a bit for Cairo on macOS (some notes here), but I’ve yet to come up with a good understanding of how to select fonts yet.

(The only Computer Modern font I can find on my computer is “Computer Modern Unicode”, which I can access (via Cairo’s Toy text API) as “CMUSerif-Roman”. There’s also the “Latin Modern” series, which is e.g. “LMRoman10-Bold”.)

Can you please give more context?

Actually the situation is far more complicated:
plotlyjs exports svg
svg is interpreted by Rsvg to a Cairo surface
Cairo surface is rendered into pdf.

My recomendation would be to work on the first step and include the font into the svg. As mentioned above, trying get your local font situation into Cairo is a major undertaking.

Thank you very much for your responses!
Then I’ll proceed by digging into Rsvg and will see what I can do. If the issue remains I’ll take a closer look at Cairo.

Regarding the Cairo issue:

using PlotlyJS


julia> p = ans

julia> savefig(p, "test.pdf")
ERROR: MethodError: no method matching handle_new_from_data(::String)
The applicable method may be too new: running in world age 21859, while current world is 21880.
Closest candidates are:
  handle_new_from_data(::AbstractString) at ~\.julia\v0.6\Rsvg\src\calls.jl:69 (method too new to be called from this world context.)
  handle_new_from_data(::AbstractString, ::Gtk.GLib.GError) at ~\.julia\v0.6\Rsvg\src\calls.jl:61 (method too new to be called from this world context.)
 [1] #savefig#202(::Symbol, ::Function, ::PlotlyJS.SyncPlot{PlotlyJS.ElectronDisplay}, ::String) at ~\.julia\v0.6\PlotlyJS\src\savefig.jl:157
 [2] savefig(::PlotlyJS.SyncPlot{PlotlyJS.ElectronDisplay}, ::String) at ~\.julia\v0.6\PlotlyJS\src\savefig.jl:106

It seems like this issue is related to Glib. However, it can be resolved by rebuilding Cairo. I’m using Julia 0.6 on Windows 10 and Fedora 26.

I’ve finally been successful:
While Electron seems to know the font “Latin Modern” (and Chrome does so as well), other tools like Inkscape and probably Rsvg do not recognize it. I had a look at the available fonts in Inkscape and noticed that “Latin Modern” cannot be selected. Instead, “CMU Serif” seemed to be the way to go. Choosing “CMU Serif” instead of “Latin Modern” while creating the plot lead to the pdf file containing the correct fonts.

Thank you very much !

1 Like