admin/src/Oms.Tests/LabelPdfFontTests.cs
zzh 7b54eff764
All checks were successful
Build and Deploy / deploy (push) Successful in 1m10s
fix: 面单 PDF 嵌入 CJK 字体,避免 Bold 回退导致中文 ???
将黑体随程序嵌入 QuestPDF;页级默认中文字体,禁止对单 Regular 调 Bold。

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-10-23 14:26:24 +08:00

57 lines
1.9 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using FluentAssertions;
using Oms.Application.Labeling.Hiprint;
namespace Oms.Tests;
public class LabelPdfFontTests
{
[Fact]
public void EnsureRegistered_ShouldLoadEmbeddedCjkFont()
{
var family = LabelPdfFontRegistrar.EnsureRegistered();
family.Should().Be(LabelPdfFontRegistrar.FamilyName);
LabelPdfFontRegistrar.RegisteredSource.Should().NotBeNullOrWhiteSpace();
LabelPdfFontRegistrar.RegisteredSource.Should().StartWith("embedded:");
}
[Fact]
public async Task Render_ShouldEmbedChineseWithoutThrowing()
{
LabelPdfFontRegistrar.EnsureRegistered().Should().NotBeNull();
const string templateJson = """
{
"panels": [{
"width": 100,
"height": 50,
"printElements": [{
"options": {
"left": 2,
"top": 2,
"width": 90,
"height": 20,
"title": "红米手机 摘要×25",
"fontSize": 12
},
"printElementType": { "type": "text" }
}]
}]
}
""";
var renderer = new QuestPdfLabelRenderer(new HiprintTemplateParser());
var pdf = await renderer.RenderAsync(
templateJson,
new Dictionary<string, object?> { ["skuSummary"] = "红米手机 512g×25" });
pdf.Should().NotBeNull();
pdf.Length.Should().BeGreaterThan(500);
// RegisterFontWithCustomName 后 PDF 仍写入真实 PostScript 名(如 SimHei)
var ascii = System.Text.Encoding.ASCII.GetString(pdf);
ascii.Should().Match(s => s.Contains("SimHei", StringComparison.OrdinalIgnoreCase)
|| s.Contains("OmsLabelSans", StringComparison.OrdinalIgnoreCase)
|| s.Contains("Noto", StringComparison.OrdinalIgnoreCase));
}
}