misterspeedy / FsExcel

An F# Excel spreadsheet generator
MIT License
143 stars 18 forks source link

Tutorial uses Windows specific font names #39

Closed misterspeedy closed 1 year ago

misterspeedy commented 1 year ago
  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)
misterspeedy commented 1 year ago

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.

adelarsq commented 1 year ago

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

misterspeedy commented 1 year ago

@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!

misterspeedy commented 1 year ago

@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.)

adelarsq commented 1 year ago

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?

misterspeedy commented 1 year ago

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

adelarsq commented 1 year ago

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