Skip to content

Commit d386eba

Browse files
authored
Merge pull request #14 from veudal/new-font
New font added
2 parents a1643f5 + 8a3d5a0 commit d386eba

File tree

5 files changed

+30
-6
lines changed

5 files changed

+30
-6
lines changed

src/Captcha.Core/Captcha.Core.csproj

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,23 @@
77
<RootNamespace>Captcha.Core</RootNamespace>
88
</PropertyGroup>
99

10+
<ItemGroup>
11+
<None Remove="Resources\Fonts\Caveat-Bold.ttf" />
12+
<None Remove="Resources\Fonts\Caveat-Regular.ttf" />
13+
<None Remove="Resources\Fonts\Caveat-SemiBold.ttf" />
14+
</ItemGroup>
15+
1016
<ItemGroup>
1117
<PackageReference Include="SkiaSharp" Version="3.119.0" />
1218
<PackageReference Include="SkiaSharp.NativeAssets.Linux.NoDependencies" Version="3.119.0" />
1319
</ItemGroup>
1420

21+
<ItemGroup>
22+
<EmbeddedResource Include="Resources\Fonts\Caveat-Bold.ttf" />
23+
<EmbeddedResource Include="Resources\Fonts\Caveat-Regular.ttf">
24+
<CopyToOutputDirectory>Never</CopyToOutputDirectory>
25+
</EmbeddedResource>
26+
<EmbeddedResource Include="Resources\Fonts\Caveat-SemiBold.ttf" />
27+
</ItemGroup>
28+
1529
</Project>
251 KB
Binary file not shown.
385 KB
Binary file not shown.
252 KB
Binary file not shown.

src/Captcha.Core/Services/CaptchaImageService.cs

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -110,17 +110,27 @@ private static void FillInTheBackground(CaptchaConfigurationData config, SKRect
110110

111111
private static SKTypeface GetTypefaceThatCanRenderText(string text)
112112
{
113-
var mainTypeface = SKTypeface.FromFamilyName(Constants.DefaultCaptchaFontName);
114-
var mainFont = new SKFont(mainTypeface);
115-
116-
if (!mainFont.ContainsGlyphs(text))
113+
// Try to load Caveat font from embedded resource
114+
var assembly = typeof(CaptchaImageService).Assembly;
115+
using var stream = assembly.GetManifestResourceStream("Captcha.Core.Resources.Fonts.Caveat-SemiBold.ttf");
116+
if (stream != null)
117117
{
118-
return SKTypeface.FromFamilyName(Constants.DefaultCaptchaFallbackFontName);
118+
var typeface = SKTypeface.FromStream(stream);
119+
if (typeface != null)
120+
{
121+
var font = new SKFont(typeface);
122+
if (font.ContainsGlyphs(text))
123+
{
124+
return typeface;
125+
}
126+
}
119127
}
120128

121-
return mainTypeface;
129+
// Fallback if Caveat is missing or doesn't support text
130+
return SKTypeface.Default;
122131
}
123132

133+
124134
/// <summary>
125135
/// This method applies similar logic to GraphicsPath.Warp() from System.Drawing.Common
126136
/// https://stackoverflow.com/questions/48416118/perspective-transform-in-skia

0 commit comments

Comments
 (0)