misterspeedy/FsExcel

Tutorial uses Windows specific font names

Closed this issue · 7 comments

  An unhandled exception has occurred while executing the request.
  System.ArgumentException: Unable to find font Calibri or fallback font Microsoft Sans Serif. Install missing fonts or specify a different fallback font through 'LoadOptions.DefaultGraphicEngine = new DefaultGraphicEngine("Fallback font name")'. Additional information is available at https://closedxml.readthedocs.io/en/latest/tips/missing-font.html page.
     at ClosedXML.Graphics.DefaultGraphicEngine.LoadFont(MetricId metricId)
     at System.Collections.Concurrent.ConcurrentDictionary`2.GetOrAdd(TKey key, Func`2 valueFactory)
     at ClosedXML.Graphics.DefaultGraphicEngine.GetFont(MetricId metricId)
     at ClosedXML.Graphics.DefaultGraphicEngine.GetFont(IXLFontBase fontBase)
     at ClosedXML.Graphics.DefaultGraphicEngine.GetTextWidth(String text, IXLFontBase fontBase, Double dpiX)
     at ClosedXML.Excel.XLColumn.GetWidthInCharacters(String text, IXLFontBase font)
     at ClosedXML.Excel.XLColumn.AdjustToContents(Int32 startRow, Int32 endRow, Double minWidth, Double maxWidth)
     at ClosedXML.Excel.XLColumn.AdjustToContents(Int32 startRow, Int32 endRow)
     at ClosedXML.Excel.XLColumn.AdjustToContents(Int32 startRow)
     at ClosedXML.Excel.XLColumn.AdjustToContents()
     at ClosedXML.Excel.XLColumns.<>c.<AdjustToContents>b__13_0(XLColumn c)
     at ClosedXML.Excel.EnumerableExtensions.ForEach[T](IEnumerable`1 source, Action`1 action)
     at ClosedXML.Excel.XLColumns.AdjustToContents()
     at FsExcel.Render.AsWorkBook(FSharpList`1 items)
     at FsExcel.Render.AsFile(String path, FSharpList`1 items)
     at server.Planilha.gerarPlanilha(DateTime dataInicial, DateTime dataFinal, FSharpList`1 list) in /app/lib/Planilha.fs:line 125
     at server.Rules.gerarPlanilha(DateTime dataInicial, DateTime dataFinal) in /app/lib/Rules.fs:line 229
     at server.Handlers.RelatorioHandlers.consultar(HttpContext ctx) in /app/lib/Handlers.fs:line 47
     at server.Program.main@52-5.Invoke(HttpContext ctx)
     at Microsoft.AspNetCore.Routing.EndpointMiddleware.Invoke(HttpContext httpContext)
  --- End of stack trace from previous location ---
     at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddlewareImpl.Invoke(HttpContext context)

Looks like there is currently no cross platform way to enumerate available fonts! Currently investigating whether there is a set of font names we can rely on normally existing on major linux distros.

What about to add a default font?

ClosedXML uses SixLabors.Fonts as default:

Graphic engine is responsible for extracting information about images and measuring size of a text in a workbook. The exception indicates that ClosedXML (more precisely SixLabors.Fonts that is used by the default engine) wasn’t enable to find a font that could be used to measure size of a text.

Calculating a correct size of a text rendered by a font is a nontrivial task and there is a dearth of maintained open source .NET font libraries. That is why ClosedXML depends on prerelease version of SixLabors.Fonts.

DefaultGraphicEngine tries to use a requested font (e.g. font of a cell) and if the requested font isn’t present on the machine, it will use fallback font (Microsoft Sans Serif). If not even fallback font is found, it throws the aforementioned exception.

Source

@adelarsq Ah - thanks, you set me on the right track. It turns out the SixLabors lets you enumerate fonts so we can just take the first few results from that rather than having hardwired fonts. Please could you try out the Tutorial.dib I've just pushed and see if it works on Linux? Many thanks!

@adelarsq Although I fixed the example where we enumerate font names, I see that autosizing also causes an exception - which I think is what you were originally getting at with this issue. I will work to fix that too.

(I've created an Ubuntu instance on VirtualBox so I can do some basic crossplat testing myself.)

Cool! I did take a look on the Tutorial.dib but I didn't have time to do some tests. Are you thinking into configure a default font or take the first available?

(I've created an Ubuntu instance on VirtualBox so I can do some basic crossplat testing myself.)

Nice. I will create a Docker config, so some CI/CD tests can be made. What do you think?

Closing as fixed - we have a separate issue for the default font issue with sizing.

@misterspeedy Nice! Can I send the code example as a pull request on this repository within an examples directory?