Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

📝 docs(Watermark): load font resource file #2163

Merged
merged 1 commit into from
Sep 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 26 additions & 18 deletions docs/Masa.Blazor.Docs/Examples/labs/watermark/Chinese.razor
Original file line number Diff line number Diff line change
@@ -1,26 +1,34 @@
@using SkiaSharp

<MWatermark Text="雅黑" Typeface="@_typeface">
@* Needs register HttpClient with builder.HostEnvironment.BaseAddress in Program.cs *@
@inject IHttpClientFactory HttpClientFactory

@* uncomment the following line when hosting on server side *@
@* @inject IWebHostEnvironment Environment *@

<MWatermark Text="中文" Typeface="@_typeface">
<div style="height: 300px;"></div>
</MWatermark>

@code {

private SKTypeface? _typeface = SKTypeface.FromFamilyName("Microsoft YaHei");

// protected override void OnAfterRender(bool firstRender)
// {
// if (firstRender)
// {
// _typeface = SKTypeface.FromFile(GetFontPath());
// StateHasChanged();
// }
// }

// private static string GetFontPath()
// {
// // Server side
// var dir = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
// return Path.Combine(dir, "wwwroot", "ABC.ttf");
// }
private SKTypeface? _typeface;

protected override async Task OnInitializedAsync()
{
_typeface = await GetFontAsync();
}

private async Task<SKTypeface> GetFontAsync()
{
// Server side
// var path = Path.Combine(Environment.WebRootPath, "SourceHanSansCN-Normal.otf");
// return SKTypeface.FromFile(path);

// Client side
var httpClient = HttpClientFactory.CreateClient("masa-docs");
await using var stream = await httpClient.GetStreamAsync("_content/Masa.Blazor.Docs/fonts/SourceHanSansCN-Normal.otf");
return SKTypeface.FromStream(stream);
}

}
6 changes: 6 additions & 0 deletions docs/Masa.Blazor.Docs/Masa.Blazor.Docs.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@
<ExamplesSourceFile Include="Examples\**\*.razor" />
</ItemGroup>

<ItemGroup>
<Content Update="wwwroot\fonts\SourceHanSansCN-Normal.otf">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
</ItemGroup>

<Target Name="CopyExmaplesToTxt" AfterTargets="Build">
<Copy SourceFiles="@(ExamplesSourceFile)" DestinationFiles="wwwroot\pages\%(RecursiveDir)\examples\%(Filename).txt" />
</Target>
Expand Down
Binary file not shown.
1 change: 1 addition & 0 deletions src/Masa.Blazor/Masa.Blazor.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<TargetFrameworks>net6.0;net7.0;net8.0</TargetFrameworks>
<Nullable>enable</Nullable>
<Version>0.1.0</Version>
<Description>Blazor UI component library based on Material Design</Description>
<RepositoryUrl>https://github.com/masastack/MASA.Blazor</RepositoryUrl>
<RepositoryType>git</RepositoryType>
<PackageProjectUrl>https://blazor.masastack.com</PackageProjectUrl>
Expand Down
Loading