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

🐛 fix(Watermark): Chinese characters cannot be displayed #2157

Merged
merged 2 commits 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
26 changes: 26 additions & 0 deletions docs/Masa.Blazor.Docs/Examples/labs/watermark/Chinese.razor
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
@using SkiaSharp

<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");
// }
}
6 changes: 6 additions & 0 deletions docs/Masa.Blazor.Docs/wwwroot/pages/labs/watermark/en-US.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ Use `Grayscale` to make the image gray.

<masa-example file="Examples.labs.watermark.Color"></masa-example>

#### Chinese {released-on=v1.7.3}

To display Chinese, you need to provide a font that supports Chinese.

<masa-example file="Examples.labs.watermark.Chinese"></masa-example>

### Misc

#### Fallback to text
Expand Down
20 changes: 13 additions & 7 deletions docs/Masa.Blazor.Docs/wwwroot/pages/labs/watermark/zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,31 @@ desc: "给某个区域加上水印。"

> This component is in [Masa.Blazor.SomethingSkia](https://www.nuget.org/packages/Masa.Blazor.SomethingSkia) package.

## 使用
## 使用 {#usage}

<watermark-usage></watermark-usage>

## 示例
## 示例 {#examples}

### 属性
### 属性 {#props}

#### 图片
#### 图片 {#image}

<masa-example file="Examples.labs.watermark.Image"></masa-example>

#### 颜色
#### 颜色 {#color}

<masa-example file="Examples.labs.watermark.Color"></masa-example>

### 其他
#### 中文 {#chinese released-on=v1.7.3}

#### 回退到文字
设置中文需要提供支持中文的字体。

<masa-example file="Examples.labs.watermark.Chinese"></masa-example>

### 其他 {#misc}

#### 回退到文字 {#fallback-to-text}

当图片加载失败时,回退到文字水印。

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public partial class MWatermark
private int __prevGapX;
private int __prevGapY;
private bool __prevIsGrayscale;
private SKTypeface? __prevTypeface;

private bool AnyParameterChanged(params string[] parameterNames)
{
Expand Down Expand Up @@ -84,6 +85,12 @@ private bool AnyParameterChanged(params string[] parameterNames)
__prevIsGrayscale = Grayscale;
anyChanged = parameterNames.Contains(nameof(Grayscale));
}

if (__prevTypeface != Typeface)
{
__prevTypeface = Typeface;
anyChanged = parameterNames.Contains(nameof(Typeface));
}

return anyChanged;
}
Expand Down
10 changes: 8 additions & 2 deletions src/Masa.Blazor.SomethingSkia/Watermark/MWatermark.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ [Parameter] [MasaApiParameter("new SKColor(0, 0, 0, 38)")]

[Parameter] public int GapY { get; set; }

[MasaApiParameter(ReleasedOn = "v1.7.3")]
[Parameter]
public SKTypeface? Typeface { get; set; }

/// <summary>
/// Determines whether the watermark is grayscale. Only works when <see cref="Image"/> is not null.
/// </summary>
Expand Down Expand Up @@ -58,7 +62,8 @@ protected override async Task OnParametersSetAsync()
nameof(Top),
nameof(GapX),
nameof(GapY),
nameof(Grayscale)))
nameof(Grayscale),
nameof(Typeface)))
{
await UpdateWatermark();
}
Expand Down Expand Up @@ -127,7 +132,8 @@ private void DrawTextWatermark()
TextSize = TextSize,
Color = Color,
IsAntialias = true,
Style = SKPaintStyle.Fill
Style = SKPaintStyle.Fill,
Typeface = Typeface
};

SKRect textBounds = new();
Expand Down
Loading