Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

load a custom font from file (ttf, woff, ...others) #438

Open
jaguarxii opened this issue Jan 22, 2024 · 9 comments
Open

load a custom font from file (ttf, woff, ...others) #438

jaguarxii opened this issue Jan 22, 2024 · 9 comments

Comments

@jaguarxii
Copy link

Description

Currently only installed fonts are available for styling charts, it would be very helpful to load custom fonts from its source such as TTF, WOFF fonts (other formats are also welcome).

Repro steps

I created a c# project using dotnet8, plptly.net 4.2, Plotly.NET.ImageExport 5.0.1, this is the code for a basic chart:

`
// Creating a bar trace
Plotly.NET.Trace barTrace = new Plotly.NET.Trace("bar");

        barTrace.SetValue("name", "cantidad n");
        barTrace.SetValue("x", xValues);
        barTrace.SetValue("y", yValues);

        // Creating a layout
        LinearAxis xAxis = new LinearAxis();
        xAxis.SetValue("title", "xAxis");
        xAxis.SetValue("zerolinecolor", "#ffff");
        xAxis.SetValue("gridcolor", "#ffff");
        xAxis.SetValue("showline", true);
        xAxis.SetValue("zerolinewidth", 2);

        LinearAxis yAxis = new LinearAxis();
        yAxis.SetValue("title", "yAxis");
        yAxis.SetValue("zerolinecolor", "#ffff");
        yAxis.SetValue("gridcolor", "#ffff");
        yAxis.SetValue("showline", true);
        yAxis.SetValue("zerolinewidth", 2);

        Layout layout = new Layout();
        layout.SetValue("xaxis", xAxis);
        layout.SetValue("yaxis", yAxis);
        layout.SetValue("title", "A Figure Specified by DynamicObj");
        layout.SetValue("paper_bgcolor", "#00000000");
        layout.SetValue("plot_bgcolor", "#00000000");
        //layout.SetValue("font", Font.init(Family:FontFamily.NewCustom("Poppins")));
        //layout.SetValue("font", Font.init(Family:FontFamily.NewCustom("DejaVu Serif")));
        layout.SetValue("showlegend", true);

        Config config = new Config();
        config.SetValue("plot_bgcolor", "#00000000");

        GenericChart
            .ofTraceObject(true, barTrace)
            .WithLayout(layout)
            .WithConfig(config)
            .ToSVGString()

`

*. in a windows environment there is no trouble to get the chart with its labels
*. In a linux environment there are no labels, instead empty squares
image

Expected behavior

It would be very useful to load an arbitrary font from its source file, similar to a Font-face setting in a web page.

for instance: layout.SetValue("font", Font.init(Family:FontFamily.NewCustom("DejaVu Serif", "font_file_path")))

Related information

  • Operating system: Windows 11, Debian Linux 12
  • Branch
  • .NET Runtime 8 LTS
  • Performance information, links to performance testing scripts
@jaguarxii jaguarxii changed the title load a custom font from file (ttf. woff) load a custom font from file (ttf, woff, ...others) Jan 22, 2024
@kMutagene
Copy link
Collaborator

You should be able to do this as long as there is a way to load the font in the html enclosing the chart.

A quick search for me turned out that this must be done via css (https://stackoverflow.com/questions/64508137/how-to-use-ttf-as-font-family-in-html). You can include such a css file in your chart output head tags by usingh the DisplayOptions API just as it is done here for the bulma stylesheet.

@jaguarxii
Copy link
Author

jaguarxii commented Jan 25, 2024

Hi, in my use case there is no web page for the chart, I need the svg-rendered chart so it can be embedded into a pdf file (server-side).

it is really strange because using a proper linux distro (debian 12) there is no font for the charts (squares instead of letters), using a wsl instance (debian 11) i got proper letters (dejavusansmono)

image

somehow it takes a "default font" from the wsl instance eventhough x-server is not installed.

@kMutagene
Copy link
Collaborator

Hi, in my use case there is no web page for the chart, I need the svg-rendered chart so it can be embedded into a pdf file (server-side).

You will still need the html page to display properly, because rendering as svg is done in a headless browser. So i would expect everything to work once you manage the chart to reference the font via css

@kMutagene
Copy link
Collaborator

could you maybe attach the svg that has no font as well? maybe that helps narrowing it down

@jaguarxii
Copy link
Author

jaguarxii commented Feb 4, 2024

Here is the sample SVG

it seems a valid file, but when it is attached to the PDF it has wrong letters (font)

image

out_svg

@kMutagene
Copy link
Collaborator

it seems a valid file, but when it is attached to the PDF it has wrong letters (font)

Are you creating that pdf programmatically? You can include fonts in pdfs, so that might be a solution for you.

I did some digging and this is not natively supported in plotly.js either (see here for reference). This means you have to either patch the generated SVG or the PDF with your custom font if you want to make the files self contained.

@jaguarxii
Copy link
Author

Hi @kMutagene, thanks for the references, will test and come back with the results

The PDF file is created programmatically using QuestPDF (already created an issue about this behavior), it includes the fonts that are required. QuestPDF Issue

Using Plotly.js (client side) I had no troubles creating PDFs using pdfmake, the approach was the same, render the chart in SVG format then embed in the PDF document.

@kMutagene
Copy link
Collaborator

Using Plotly.js (client side) I had no troubles creating PDFs using pdfmake, the approach was the same, render the chart in SVG format then embed in the PDF document.

Could you maybe also attach that pdf that is working here? maybe we can spot a difference between the 2

@sproott
Copy link

sproott commented Feb 10, 2024

As I've mentioned in the linked QuestPDF issue, I have the same issue with QuestPDF and ScottPlot 5, so it might be an issue on the QuestPDF side.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants