Add fonts from resource in .NET Core

Stimulsoft Reports.WEB discussion
johan.gorter
Posts: 9
Joined: Thu Oct 17, 2019 6:41 am

Add fonts from resource in .NET Core

Post by johan.gorter »

Hello,

In my report I need Roboto regular, Roboto Bold and Roboto Light. These fonts may not be installed on all our servers, so I want to ship them using dll-resources. I downloaded 3 files from the internet: Roboto-regular.ttf, Roboto-Bold.ttf and Roboto-Light.ttf. How do I register them properly so they will all get picked up? This is what I have so far:

Stimulsoft.Base.StiFontCollection.AddResourceFont("Roboto-Regular.ttf", regularBytes, "ttf", "Roboto");
Stimulsoft.Base.StiFontCollection.AddResourceFont("Roboto-Bold.ttf", boldBytes, "ttf", "Roboto");
Stimulsoft.Base.StiFontCollection.AddResourceFont("Roboto-Light.ttf", lightBytes, "ttf", "Roboto Light");

The strange thing here is that both Roboto and Roboto-Bold are listed as <Font>Roboto ,...</Font> in the mrt file.
How do I make sure that Stimulsoft uses Roboto-Bold.ttf instead of just 'blowing up' Roboto-Regular.ttf?

Thanks in advance.
Lech Kulikowski
Posts: 6197
Joined: Tue Mar 20, 2018 5:34 am

Re: Add fonts from resource in .NET Core

Post by Lech Kulikowski »

Hello,

Firstly, - in the second line font with the name Roboto is registered, and RobotoBold should:
AddResourceFont("Roboto-Bold.ttf", boldBytes, "ttf", "Roboto Bold");

secondly, there is a problem with some font families. e.g. Roboto and SourceSansPro. In these fonts, the internal name of all fonts is the same("preferredFamily").
for example, Roboto, Roboto-Italic and Roboto-Bold fonts have the same internal name "Roboto", and unfortunately, this name is still stored in our product.
the solution is only this: use fonts with different "preferredFamily" and "preferredSubfamily". For example, modify these fonts in the font editor.

Thank you.
johan.gorter
Posts: 9
Joined: Thu Oct 17, 2019 6:41 am

Re: Add fonts from resource in .NET Core

Post by johan.gorter »

Thanks for the quick reply.

If I understand correctly: I have to change the Roboto-Bold.ttf to change to a different preferredFamily,
Then I get a new font in the designer "Roboto Bold" and I have to select that font if I want to make things bold. Just pressing ctrl-b on a piece of text will still use the Roboto-Regular.ttf?

I noticed that when I used roboto-light that the font would automatically be registered in the mrt as <Font>Roboto Light,...</Font>, this is what I also want for Bold, how can I achieve this?
Lech Kulikowski
Posts: 6197
Joined: Tue Mar 20, 2018 5:34 am

Re: Add fonts from resource in .NET Core

Post by Lech Kulikowski »

Hello,

At the current moment, it is limitation of our product:
fonts are stores in template with internal name.

Roboto, Roboto-Italic and Roboto-Bold fonts have the same internal name "Roboto", and unfortunately, stored in template with name "Roboto". Roboto-Light have internal name "Roboto Light", and stored correctly.
The solution is only this: use fonts with different "preferredFamily" and "preferredSubfamily". For example, modify these fonts in the font editor.

Thank you.
johan.gorter
Posts: 9
Joined: Thu Oct 17, 2019 6:41 am

Re: Add fonts from resource in .NET Core

Post by johan.gorter »

I have one more question. I have data with embedded HTML tags to do some markup, like make things bold. This works fine in the preview tab of the designer.
In our runtime with the embedded roboto resource fonts, bold does not work. How do I register the resource fonts in such a way that embedded html bold tag works?
Lech Kulikowski
Posts: 6197
Joined: Tue Mar 20, 2018 5:34 am

Re: Add fonts from resource in .NET Core

Post by Lech Kulikowski »

Hello,

Please clarify which version are you use?

Also, please send us a sample report with test data that reproduces the issue for analysis.

Thank you.
rafael_at
Posts: 5
Joined: Sun May 03, 2020 9:40 am

Re: Add fonts from resource in .NET Core

Post by rafael_at »

hi, I'm having the same issue with "Open Sans" (seems to apply to all Google Fonts...):
Regular and Bold fonts have the same font family name "Open Sans" and Regular and Bold in two separate ttf files.
using this, the pdf exporter doesn't recognize the correct regular/bold style. So what I did was hacking the font files and renaming the font family to "Open Sans Regular" and "Open Sans Bold". If I specify these fonts in the components everything works fine!
BUT!!! :(
If I have a text component with font "Open Sans Regular" with HTML tags <b>, the pdf doesn't pick the real Bold font!
As a side note: I experience many bugs with HTML tags and .Net Core when exporting to pdf:
- bullet lists (<ul>) don't show with correct indentation when there are line wraps
- font colors (<font-color>) doesn't change the color (I'm writing another forum post about this...)
Lech Kulikowski
Posts: 6197
Joined: Tue Mar 20, 2018 5:34 am

Re: Add fonts from resource in .NET Core

Post by Lech Kulikowski »

Hello,

> If I have a text component with font "Open Sans Regular" with HTML tags <b>, the pdf doesn't pick the real Bold font!

Unfortunately, at the current moment, as is. There are many issues in the fonts' methods of the NetFramework.

> As a side note: I experience many bugs with HTML tags and .Net Core when exporting to pdf:

These tags are not working in the Net Core version.
In the Net engine are uses system Windows libraries.
It is not possible to use it in the NetCore. We are adding it to our engine, but not all at that moment.

Thank you.
johan.gorter
Posts: 9
Joined: Thu Oct 17, 2019 6:41 am

Re: Add fonts from resource in .NET Core

Post by johan.gorter »

Hello,

You asked for a repro case showing the problems with HTML tags and the roboto font. Below is the C# .NET core code I use and the mrt file created with the 2020.3 designer. The result is that the roboto-bold font is not used, the emoji disappears and the link is not clickable.

.NET Core code

Code: Select all

    static void Main(string[] args)
    {
      StiFontCollection.AddResourceFont("Roboto-Regular.ttf", LoadResource("Roboto-Regular.ttf"), "ttf", "Roboto");
      StiFontCollection.AddResourceFont("Roboto-Bold.ttf", LoadResource("Roboto-Bold.ttf"), "ttf", "Roboto");


      var report = new StiReport();
      report.Load("report.mrt");
      report.Render();
      report.ExportDocument(StiExportFormat.Pdf, "output.pdf");
    }
MRT:

Code: Select all

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<StiSerializer version="1.02" type="Net" application="StiReport">
  <CalculationMode>Interpretation</CalculationMode>
  <Dictionary Ref="1" type="Dictionary" isKey="true">
    <BusinessObjects isList="true" count="0" />
    <Databases isList="true" count="0" />
    <DataSources isList="true" count="0" />
    <Relations isList="true" count="0" />
    <Report isRef="0" />
    <Resources isList="true" count="0" />
    <Variables isList="true" count="0" />
  </Dictionary>
  <EngineVersion>EngineV2</EngineVersion>
  <GlobalizationStrings isList="true" count="0" />
  <Key>b60351cbe9914b2c90df6a89c6392ebb</Key>
  <MetaTags isList="true" count="0" />
  <Pages isList="true" count="1">
    <Page1 Ref="2" type="Page" isKey="true">
      <Border>None;Black;2;Solid;False;4;Black</Border>
      <Brush>Transparent</Brush>
      <Components isList="true" count="1">
        <Text1 Ref="3" type="Text" isKey="true">
          <AllowHtmlTags>True</AllowHtmlTags>
          <Brush>Transparent</Brush>
          <ClientRectangle>4.2,3,13.2,4</ClientRectangle>
          <Conditions isList="true" count="0" />
          <Font>Roboto,20</Font>
          <Margins>0,0,0,0</Margins>
          <Name>Text1</Name>
          <Page isRef="2" />
          <Parent isRef="2" />
          <Text>Some &lt;strong&gt;strong&lt;/strong&gt;, &lt;em&gt;em&lt;/em&gt;, &lt;del&gt;del&lt;/del&gt; and &lt;ins&gt;ins&lt;/ins&gt; texts.&lt;div&gt;&lt;br&gt;&lt;/div&gt;&lt;div&gt;&lt;a href="https://google.com"&gt;Hyperlink&lt;/a&gt;&lt;p&gt;😊&lt;/p&gt;&lt;/div&gt;</Text>
          <TextBrush>Black</TextBrush>
          <TextQuality>Wysiwyg</TextQuality>
          <Type>Expression</Type>
        </Text1>
      </Components>
      <Conditions isList="true" count="0" />
      <Guid>e169c22f510d4d108c38c693d3b3d5fc</Guid>
      <Margins>1,1,1,1</Margins>
      <Name>Page1</Name>
      <PageHeight>29.69</PageHeight>
      <PageWidth>21.01</PageWidth>
      <Report isRef="0" />
    </Page1>
  </Pages>
  <ReferencedAssemblies isList="true" count="8">
    <value>System.Dll</value>
    <value>System.Drawing.Dll</value>
    <value>System.Windows.Forms.Dll</value>
    <value>System.Data.Dll</value>
    <value>System.Xml.Dll</value>
    <value>Stimulsoft.Controls.Dll</value>
    <value>Stimulsoft.Base.Dll</value>
    <value>Stimulsoft.Report.Dll</value>
  </ReferencedAssemblies>
  <ReportAlias>Report</ReportAlias>
  <ReportChanged>5/12/2020 12:45:51 PM</ReportChanged>
  <ReportCreated>5/12/2020 12:45:51 PM</ReportCreated>
  <ReportGuid>342480d97ac34193ae3913c09d943872</ReportGuid>
  <ReportName>Report</ReportName>
  <ReportUnit>Centimeters</ReportUnit>
  <ReportVersion>2020.2.3.0</ReportVersion>
  <Script>using System;
using System.Drawing;
using System.Windows.Forms;
using System.Data;
using Stimulsoft.Controls;
using Stimulsoft.Base.Drawing;
using Stimulsoft.Report;
using Stimulsoft.Report.Dialogs;
using Stimulsoft.Report.Components;

namespace Reports
{
    public class Report : Stimulsoft.Report.StiReport
    {
        public Report()        {
            this.InitializeComponent();
        }

        #region StiReport Designer generated code - do not modify
        #endregion StiReport Designer generated code - do not modify
    }
}
</Script>
  <ScriptLanguage>CSharp</ScriptLanguage>
  <Styles isList="true" count="0" />
</StiSerializer>
johan.gorter
Posts: 9
Joined: Thu Oct 17, 2019 6:41 am

Re: Add fonts from resource in .NET Core

Post by johan.gorter »

When using the Arial font instead of Roboto, the hyperlink still does not work and the emoji also disappears. Not all HTML tags (ins, del) seem to be working as well.

Which of these problems can be fixed from your side? A modified Roboto font solution could work for us if it this would also fix the bold text problem described by rafael_at.
Post Reply