You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
SKTypeface.FromFamilyName does not change the font or text style in SkiaSharp.Views.Blazor #1902
Unexpected behavior when changing the text font when drawing text. If you query what type of text it should have after assigning Typeface, it is always typeFace: Noto Mono.
The following example illustrates the supposed issue,
@page "/drawing-text"
@using SkiaSharp@usingSkiaSharp.Views.Blazor<SKCanvasView OnPaintSurface="OnPaintSurface"/>@code{voidOnPaintSurface(SKPaintSurfaceEventArgse){varcanvas= e.Surface.Canvas;var fontFamily = "Tahoma";// "Arial" | "Calibri" | ... none works?
var typeFace = SKTypeface.FromFamilyName(fontFamily, SKFontStyle.BoldItalic);// query
Console.WriteLine("typeFace: {0}",typeFace.FamilyName);//? typeFace: Noto MonousingSKPaintpaint=new(){Style=SKPaintStyle.Fill,TextSize=26,IsAntialias=true,Typeface=typeFace};canvas.DrawText("Fancy Text",30,30,paint);//? draw with Noto Mono}}
The expected behavior is that the text is drawn with the text font and text style. I have tried the equivalent code in other scenarios and it works fine.
Skia's font manager is highly limited under wasm so you can't expect a functional font manager. It is better to load fonts explicitly via stream in a sandbox environment.
True Gillibald. Studying the «issue », it can be deduced that from WASM we do not have access to the Browser resources, so SKTypeface returns the Default, without warning. - Although I guess should be possible to do so, just like CSS you can use "Arial", to name something.
If anything, that's correct, I had already found a way to fix it.
The file TTF file is added as an embedded resource (ideal)
The font is loaded with SKTypeface.FromStream, reading the resource with Reflection.
To improve the matter, it is provided as a Singleton service in the application. Issue resolved
This is a deal killer for using SkiaSharp on WASM if every font needs to be an embedded resource. We have a drawing package we're porting to Blazor WASM with SkiaSharp wherein the user picks as many fonts as they want in the design portion, with the runtime portion showing the designed graphics.
MAUI does the same there is no way around it and that's how the web works. Usually those assets are provided via a webservice so font assets can be cached by the browser you don't have to bloat up your application size.
I'm fine pulling the fonts from a webservice, as long as there's some way to do it. So you're saying a WASM app pulls the fonts down in some capacity it can be generally loaded with "SKTypeface.FromStream" ?
Yes exactly. Javascript fetch API is available via JSInterop or directly via Emscripten so you will get caching out of the box as long as your service provides proper headers.
Can't you also hack the system and have a separate assembly with just fonts. And then use the feature of Blazor to load assemblies on demand? Is that even possible?
Web fonts are fonts that are hosted on a web server. You do not have rights to:
copy fonts from a Windows installation to a web server, a process known as web font “self-hosting”.
convert the font to the formats typically associated with web fonts, such as the WOFF or WOFF2 format.
This is a major pain point. I work on a diagramming library and we're hoping to add Blazor as a target. We need to provide our users with flexibility in fonts. How would one go about creating an SKTypeface dynamically, from a given family name, where we don't control what potential families may be used?
Any news or ideas on this? I am running into this issue working on a WASM Blazor app where I'd like to use OxyPlot via SkiaSharp such as done in this library. Right now, it all works, but only renders using a monospaced default font. Setting DefaultFont on the PlotModel has no effect at all. Is there a solution other than embedding fonts in my app?
Actually, the comment by @harveytrianahere solves this issue. Embedding a font in my WASM Blazor app and loading it via a stream does indeed work.
A different solution seems unlikely, given that WASM has no direct access to browser resources, as far as I understand. Unless there is some clean way to do this via JS interop inside the SkiaSharp.Views.Blazor library.
In my opion, unless a potential better solution is doable some time soon, this issue should be closed for clarity.
Activity
Gillibald commentedon Dec 23, 2021
Skia's font manager is highly limited under wasm so you can't expect a functional font manager. It is better to load fonts explicitly via stream in a sandbox environment.
harveytriana commentedon Dec 23, 2021
True Gillibald. Studying the «issue », it can be deduced that from WASM we do not have access to the Browser resources, so SKTypeface returns the Default, without warning. - Although I guess should be possible to do so, just like CSS you can use "Arial", to name something.
If anything, that's correct, I had already found a way to fix it.
RChrisCoble commentedon Feb 22, 2022
This is a deal killer for using SkiaSharp on WASM if every font needs to be an embedded resource. We have a drawing package we're porting to Blazor WASM with SkiaSharp wherein the user picks as many fonts as they want in the design portion, with the runtime portion showing the designed graphics.
@mattleibow do we have options here?
Gillibald commentedon Feb 22, 2022
MAUI does the same there is no way around it and that's how the web works. Usually those assets are provided via a webservice so font assets can be cached by the browser you don't have to bloat up your application size.
RChrisCoble commentedon Feb 22, 2022
I'm fine pulling the fonts from a webservice, as long as there's some way to do it. So you're saying a WASM app pulls the fonts down in some capacity it can be generally loaded with "SKTypeface.FromStream" ?
Gillibald commentedon Feb 22, 2022
Yes exactly. Javascript fetch API is available via JSInterop or directly via Emscripten so you will get caching out of the box as long as your service provides proper headers.
harveytriana commentedon Feb 22, 2022
In this post, https://www.blazorspread.net/blogview/82, I show possibly one of the best ways to handle fonts in Blazor, of course, it also covers MAUI

mattleibow commentedon Feb 23, 2022
Can't you also hack the system and have a separate assembly with just fonts. And then use the feature of Blazor to load assemblies on demand? Is that even possible?
harveytriana commentedon Feb 23, 2022
Yes
RChrisCoble commentedon Feb 23, 2022
Is this even legal though?
Font redistribution FAQ - Typography | Microsoft Docs
Web fonts are fonts that are hosted on a web server. You do not have rights to:
copy fonts from a Windows installation to a web server, a process known as web font “self-hosting”.
convert the font to the formats typically associated with web fonts, such as the WOFF or WOFF2 format.
jonchardy commentedon Apr 22, 2022
This is a major pain point. I work on a diagramming library and we're hoping to add Blazor as a target. We need to provide our users with flexibility in fonts. How would one go about creating an SKTypeface dynamically, from a given family name, where we don't control what potential families may be used?
qweraou commentedon Jan 12, 2024
cannot access
mennowo commentedon Jan 7, 2025
Any news or ideas on this? I am running into this issue working on a WASM Blazor app where I'd like to use OxyPlot via SkiaSharp such as done in this library. Right now, it all works, but only renders using a monospaced default font. Setting
DefaultFonton thePlotModelhas no effect at all. Is there a solution other than embedding fonts in my app?mennowo commentedon Jan 17, 2025
Actually, the comment by @harveytriana here solves this issue. Embedding a font in my WASM Blazor app and loading it via a stream does indeed work.
A different solution seems unlikely, given that WASM has no direct access to browser resources, as far as I understand. Unless there is some clean way to do this via JS interop inside the SkiaSharp.Views.Blazor library.
In my opion, unless a potential better solution is doable some time soon, this issue should be closed for clarity.
ai-triage: classify #1902
ai-repro: reproduce #1902
ai-triage: backfill #3279, #1902, #3015, #2862, #3005, #3638, #2832, #…