Skip to content

Commit be09e49

Browse files
CopilotScarletKuro
andauthored
revert font packaging changes and document preload guidance
Agent-Logs-Url: https://github.com/MudBlazor/MudBlazor.Icons/sessions/4c3c5150-d93a-4655-9c4c-44609d45003f Co-authored-by: ScarletKuro <19953225+ScarletKuro@users.noreply.github.com>
1 parent 203ee06 commit be09e49

5 files changed

Lines changed: 25 additions & 52 deletions

File tree

documentation/material_symbols_usage.md

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,25 @@ To use the icons in your MudBlazor project, you can add the following CSS link t
1515
<link href="_content/MudBlazor.FontIcons.MaterialSymbols/css/font.min.css" rel="stylesheet" />
1616
```
1717

18-
The package is self-hosted and serves its font assets from `_content/MudBlazor.FontIcons.MaterialSymbols/`.
19-
20-
For above-the-fold icons, preloading the style(s) you use can reduce first-paint delay:
18+
To reduce first-load flicker, preload the Material Symbols font files before loading the stylesheet:
2119

2220
```html
23-
<link rel="preload" href="_content/MudBlazor.FontIcons.MaterialSymbols/font/MaterialSymbolsRounded.woff2" as="font" type="font/woff2" crossorigin />
21+
<link rel="preload"
22+
href="_content/MudBlazor.FontIcons.MaterialSymbols/font/MaterialSymbolsRounded.woff2"
23+
as="font"
24+
type="font/woff2"
25+
crossorigin>
26+
<link rel="preload"
27+
href="_content/MudBlazor.FontIcons.MaterialSymbols/font/MaterialSymbolsOutlined.woff2"
28+
as="font"
29+
type="font/woff2"
30+
crossorigin>
31+
<link rel="preload"
32+
href="_content/MudBlazor.FontIcons.MaterialSymbols/font/MaterialSymbolsSharp.woff2"
33+
as="font"
34+
type="font/woff2"
35+
crossorigin>
36+
<link href="_content/MudBlazor.FontIcons.MaterialSymbols/css/font.min.css" rel="stylesheet" />
2437
```
2538

2639
Alternatively, you can use the following CDN links:
@@ -56,10 +69,3 @@ This allows you to access the icons like this:
5669
```
5770

5871
**NB!** Please note that aliases do not work in normal Razor pages (https://github.com/dotnet/razor/issues/7670)!
59-
60-
## Loading tradeoffs
61-
62-
Material Symbols constants in this package are ligature-based strings (for example the ligature name `weight`).
63-
Ligature rendering can still show readable fallback text briefly on very first load in some environments.
64-
65-
If your UI requires a strict "never show fallback text" behavior, prefer SVG icons.

src/GoogleMaterialDesignIconsGenerator/Service/IconHttpClientService.cs

Lines changed: 8 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,20 @@
11
using System.Text.Json;
2-
using System.Text.RegularExpressions;
32
using GoogleMaterialDesignIconsGenerator.Model.Google;
43

54
namespace GoogleMaterialDesignIconsGenerator.Service;
65

76
public class IconHttpClientService : IDisposable
87
{
98
public const string GoogleFontUrl = "http://fonts.google.com/";
10-
public const string GoogleFontsCssApiUrl = "https://fonts.googleapis.com/css2";
11-
private static readonly Regex Woff2UrlRegex = new(@"url\((['""]?)(?<href>https://[^)'""]+?\.woff2)\1\)", RegexOptions.Compiled | RegexOptions.IgnoreCase);
9+
public const string GoogleMaterialDesignIconsRawUrl = "https://github.com/google/material-design-icons/raw/refs/heads/master/variablefont/";
1210

1311
private readonly HttpClient _httpClient;
1412
private readonly JsonSerializerOptions _jsonSerializerOptions;
15-
private static readonly (string FamilyName, string TargetFileName)[] MaterialSymbolsFontFiles =
13+
private static readonly (string SourceFileName, string TargetFileName)[] MaterialSymbolsFontFiles =
1614
[
17-
("Material Symbols Outlined", "MaterialSymbolsOutlined.woff2"),
18-
("Material Symbols Rounded", "MaterialSymbolsRounded.woff2"),
19-
("Material Symbols Sharp", "MaterialSymbolsSharp.woff2")
15+
("MaterialSymbolsOutlined[FILL,GRAD,opsz,wght].woff2", "MaterialSymbolsOutlined.woff2"),
16+
("MaterialSymbolsRounded[FILL,GRAD,opsz,wght].woff2", "MaterialSymbolsRounded.woff2"),
17+
("MaterialSymbolsSharp[FILL,GRAD,opsz,wght].woff2", "MaterialSymbolsSharp.woff2")
2018
];
2119

2220
public IconHttpClientService()
@@ -25,7 +23,6 @@ public IconHttpClientService()
2523
{
2624
BaseAddress = new Uri(GoogleFontUrl)
2725
};
28-
_httpClient.DefaultRequestHeaders.UserAgent.ParseAdd("Mozilla/5.0 (compatible; MudBlazor.FontIcons.MaterialSymbols generator)");
2926
_jsonSerializerOptions = new JsonSerializerOptions
3027
{
3128
TypeInfoResolver = IconMetadataJsonSerializerContext.Default
@@ -63,52 +60,22 @@ public async Task DownloadMaterialSymbolsFontsAsync(string destinationFolderPath
6360
throw new InvalidOperationException($"Failed to prepare destination folder '{destinationFolderPath}' for Material Symbols fonts.", ex);
6461
}
6562

66-
foreach (var (familyName, targetFileName) in MaterialSymbolsFontFiles)
63+
foreach (var (sourceFileName, targetFileName) in MaterialSymbolsFontFiles)
6764
{
65+
var fileUrl = new Uri(new Uri(GoogleMaterialDesignIconsRawUrl), Uri.EscapeDataString(sourceFileName));
6866
try
6967
{
70-
var cssFileUrl = BuildMaterialSymbolsCssUri(familyName);
71-
var cssContent = await _httpClient.GetStringAsync(cssFileUrl, cancellationToken).ConfigureAwait(false);
72-
var fileUrl = ResolveWoff2Url(cssContent, familyName);
7368
var fileContent = await _httpClient.GetByteArrayAsync(fileUrl, cancellationToken).ConfigureAwait(false);
7469
var destinationPath = Path.Combine(destinationFolderPath, targetFileName);
7570
await File.WriteAllBytesAsync(destinationPath, fileContent, cancellationToken).ConfigureAwait(false);
7671
}
7772
catch (Exception ex) when (ex is HttpRequestException or IOException)
7873
{
79-
throw new InvalidOperationException($"Failed to download and save Material Symbols font for '{familyName}'.", ex);
74+
throw new InvalidOperationException($"Failed to download and save Material Symbols font '{sourceFileName}' from '{fileUrl}'.", ex);
8075
}
8176
}
8277
}
8378

84-
private static Uri BuildMaterialSymbolsCssUri(string familyName)
85-
{
86-
var encodedFamily = Uri.EscapeDataString(familyName);
87-
return new Uri($"{GoogleFontsCssApiUrl}?family={encodedFamily}:opsz,wght,FILL,GRAD@24,400,0,0&display=block", UriKind.Absolute);
88-
}
89-
90-
private static Uri ResolveWoff2Url(string cssContent, string familyName)
91-
{
92-
var match = Woff2UrlRegex.Match(cssContent);
93-
if (!match.Success)
94-
{
95-
throw new InvalidOperationException($"Failed to resolve .woff2 URL from Google Fonts CSS for '{familyName}'.");
96-
}
97-
98-
if (!Uri.TryCreate(match.Groups["href"].Value, UriKind.Absolute, out var uri))
99-
{
100-
throw new InvalidOperationException($"Resolved an invalid .woff2 URL from Google Fonts CSS for '{familyName}'.");
101-
}
102-
103-
if (!uri.Scheme.Equals(Uri.UriSchemeHttps, StringComparison.OrdinalIgnoreCase) ||
104-
!uri.Host.Equals("fonts.gstatic.com", StringComparison.OrdinalIgnoreCase))
105-
{
106-
throw new InvalidOperationException($"Resolved an unexpected .woff2 URL host '{uri.Host}' from Google Fonts CSS for '{familyName}'.");
107-
}
108-
109-
return uri;
110-
}
111-
11279
public void Dispose()
11380
{
11481
Dispose(true);
Binary file not shown.
Binary file not shown.
Binary file not shown.

0 commit comments

Comments
 (0)