Skip to content

Commit 2e8a99f

Browse files
committed
带上更多辅助支持,可以正确解析
1 parent e71a933 commit 2e8a99f

2 files changed

Lines changed: 57 additions & 4 deletions

File tree

src/MediaConverters/SkiaWmfRenderer/samples/sample/Program.cs

Lines changed: 55 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,22 @@
11
// See https://aka.ms/new-console-template for more information
22

3+
using DocSharp.Markdown;
4+
5+
using HarfBuzzSharp;
6+
7+
using SkiaSharp;
8+
9+
using SkiaWmfRenderer;
10+
311
using System.Diagnostics;
412
using System.Drawing;
513
using System.Drawing.Imaging;
14+
using System.Globalization;
15+
using System.Runtime.CompilerServices;
16+
using System.Runtime.InteropServices;
617
using System.Text;
718

8-
using DocSharp.Markdown;
9-
using SkiaSharp;
10-
using SkiaWmfRenderer;
19+
using Buffer = HarfBuzzSharp.Buffer;
1120

1221
var markdownText = new StringBuilder();
1322
var outputFolder = Path.Join(AppContext.BaseDirectory, $"Output_{Path.GetRandomFileName()}");
@@ -42,17 +51,59 @@
4251
var symbolFontFile = Path.Join(AppContext.BaseDirectory, "StandardSymbolsPS.ttf");
4352
var skTypeface =
4453
SKFontManager.Default.CreateTypeface(symbolFontFile);
54+
4555
//skTypeface = SKTypeface.FromFamilyName("Symbol");
4656
Console.WriteLine($"Font='{symbolFontFile}' SKTypeface={skTypeface.FamilyName} GlyphCount={skTypeface.GlyphCount}");
4757
Console.WriteLine($"ContainsGlyph={skTypeface.ContainsGlyph('p')} {skTypeface.GetGlyph('p')}");
48-
58+
4959
skPaint.Typeface = skTypeface;
5060
var skFont = skTypeface.ToFont(50);
5161
skPaint.Color = SKColors.Black;
5262
skPaint.IsAntialias = true;
5363
var skTextBlob = SKTextBlob.Create("p",skFont);
5464
skCanvas.DrawText(skTextBlob, 50, 100, skPaint);
5565

66+
using (var buffer = new Buffer())
67+
{
68+
buffer.AddUtf16("p");
69+
buffer.GuessSegmentProperties();
70+
buffer.Language = new Language(CultureInfo.CurrentCulture);
71+
72+
var face = new HarfBuzzSharp.Face(GetTable);
73+
74+
Blob? GetTable(Face f, Tag tag)
75+
{
76+
var size = skTypeface.GetTableSize(tag);
77+
var data = Marshal.AllocCoTaskMem(size);
78+
if (skTypeface.TryGetTableData(tag, 0, size, data))
79+
{
80+
return new Blob(data, size, MemoryMode.ReadOnly, () => Marshal.FreeCoTaskMem(data));
81+
}
82+
else
83+
{
84+
return null;
85+
}
86+
}
87+
88+
var font = new HarfBuzzSharp.Font(face);
89+
font.SetFunctionsOpenType();
90+
91+
var tryGetGlyph = font.TryGetGlyph('p',out uint glyph);
92+
Console.WriteLine($"TryGetGlyph={tryGetGlyph} {glyph}");
93+
94+
if (tryGetGlyph)
95+
{
96+
var bytes = BitConverter.GetBytes(glyph);
97+
//SKTextEncoding.GlyphId
98+
99+
100+
skTextBlob = SKTextBlob.Create(bytes,SKTextEncoding.GlyphId,skFont);
101+
skCanvas.DrawText(skTextBlob, 100, 100, skPaint);
102+
}
103+
104+
font.Shape(buffer);
105+
}
106+
56107
var outputFile = Path.Join(outputFolder, $"{DateTime.Now:HHmmss}.png");
57108
using var outputStream = File.OpenWrite(outputFile);
58109
skBitmap.Encode(outputStream, SKEncodedImageFormat.Png, 100);

src/MediaConverters/SkiaWmfRenderer/samples/sample/Sample.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
</ItemGroup>
1414

1515
<ItemGroup>
16+
<PackageReference Include="HarfBuzzSharp" Version="8.3.1.1" />
17+
<PackageReference Include="HarfBuzzSharp.NativeAssets.Linux" Version="8.3.1.1" />
1618
<PackageReference Include="System.Drawing.Common" Version="9.0.7" />
1719
<PackageReference Include="System.Text.Encoding.CodePages" Version="9.0.7" />
1820
<PackageReference Include="DocSharp.Markdown" Version="0.8.5" />

0 commit comments

Comments
 (0)