Skip to content

Commit 21e13a4

Browse files
authored
feat(fonts): embed fonts into the solution (#16)
- Embed `Captcha.Core.Resources.Fonts.Unifont.ttf` from assembly. - Remove Arimo/Unifont installation steps from Dockerfile. - Trim README prerequisites accordingly.
1 parent 079db22 commit 21e13a4

File tree

9 files changed

+29
-39
lines changed

9 files changed

+29
-39
lines changed

README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,6 @@ curl -X 'POST' \
6363
- Visual Studio 2022
6464
- With ASP.NET and web development installed from the Visual Studio Installer
6565
- .NET 9 SDK
66-
- Arimo and Unifont fonts
6766

6867
#### How to Run
6968
1. Open the solution in Visual Studio 2022.

src/Captcha.Core/Captcha.Core.csproj

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,8 @@
88
</PropertyGroup>
99

1010
<ItemGroup>
11-
<None Remove="Resources\Fonts\Caveat-Bold.ttf" />
12-
<None Remove="Resources\Fonts\Caveat-Regular.ttf" />
1311
<None Remove="Resources\Fonts\Caveat-SemiBold.ttf" />
12+
<None Remove="Resources\Fonts\Unifont.ttf" />
1413
</ItemGroup>
1514

1615
<ItemGroup>
@@ -19,11 +18,8 @@
1918
</ItemGroup>
2019

2120
<ItemGroup>
22-
<EmbeddedResource Include="Resources\Fonts\Caveat-Bold.ttf" />
23-
<EmbeddedResource Include="Resources\Fonts\Caveat-Regular.ttf">
24-
<CopyToOutputDirectory>Never</CopyToOutputDirectory>
25-
</EmbeddedResource>
2621
<EmbeddedResource Include="Resources\Fonts\Caveat-SemiBold.ttf" />
22+
<EmbeddedResource Include="Resources\Fonts\Unifont.ttf" />
2723
</ItemGroup>
2824

2925
</Project>

src/Captcha.Core/Constants.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ public static class Constants
1010
public const int DefaultCaptchaHeight = 100;
1111
public const float DefaultFrequency = 100F;
1212
public const int FrequencyScalingFactor = 40000; // on a 400 x 100 image
13-
public const string DefaultCaptchaFontName = "Arimo";
14-
public const string DefaultCaptchaFallbackFontName = "Unifont";
13+
public const string DefaultCaptchaFontName = "Captcha.Core.Resources.Fonts.Caveat-SemiBold.ttf";
14+
public const string DefaultCaptchaFallbackFontName = "Captcha.Core.Resources.Fonts.Unifont.ttf";
1515
public static readonly SKColor DefaultPrimaryColor = SKColor.Parse("FFD3D3D3");
1616
public static readonly SKColor DefaultSecondaryColor = SKColor.Parse("FFFFFFFF");
1717
public const string CaptchaContentType = "image/jpeg";
-251 KB
Binary file not shown.
-385 KB
Binary file not shown.
11.7 MB
Binary file not shown.

src/Captcha.Core/Services/CaptchaImageService.cs

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,22 @@ namespace Captcha.Core.Services;
66

77
public class CaptchaImageService : ICaptchaImageService
88
{
9+
private readonly SKTypeface _mainFontTypeface;
10+
private readonly SKTypeface _fallbackFontTypeface;
11+
12+
public CaptchaImageService()
13+
{
14+
var assembly = typeof(CaptchaImageService).Assembly;
15+
16+
_mainFontTypeface = SKTypeface.FromStream(
17+
assembly.GetManifestResourceStream(Constants.DefaultCaptchaFontName)
18+
);
19+
20+
_fallbackFontTypeface = SKTypeface.FromStream(
21+
assembly.GetManifestResourceStream(Constants.DefaultCaptchaFallbackFontName)
22+
);
23+
}
24+
925
public SKBitmap CreateCaptchaImage(CaptchaConfigurationData config)
1026
{
1127
var bitmap = new SKBitmap(new SKImageInfo(config.Width, config.Height));
@@ -73,7 +89,7 @@ private static void DrawWarpedText(CaptchaConfigurationData config, SKFont font,
7389
graphics.DrawPath(path, fillPaint);
7490
}
7591

76-
private static SKFont GetFontThatFitsRectangle(CaptchaConfigurationData config, SKRect rectangle)
92+
private SKFont GetFontThatFitsRectangle(CaptchaConfigurationData config, SKRect rectangle)
7793
{
7894
var typeface = GetTypefaceThatCanRenderText(config.Text);
7995

@@ -96,7 +112,6 @@ private static SKFont GetFontThatFitsRectangle(CaptchaConfigurationData config,
96112
return font;
97113
}
98114

99-
100115
private static void FillInTheBackground(CaptchaConfigurationData config, SKRect rectangle, SKCanvas graphics)
101116
{
102117
using var paint = new SKPaint
@@ -108,29 +123,18 @@ private static void FillInTheBackground(CaptchaConfigurationData config, SKRect
108123
graphics.DrawRect(rectangle, paint);
109124
}
110125

111-
private static SKTypeface GetTypefaceThatCanRenderText(string text)
126+
private SKTypeface GetTypefaceThatCanRenderText(string text)
112127
{
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)
128+
using var mainFont = new SKFont(_mainFontTypeface);
129+
130+
if (mainFont.ContainsGlyphs(text))
117131
{
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-
}
132+
return _mainFontTypeface;
127133
}
128134

129-
// Fallback if Caveat is missing or doesn't support text
130-
return SKTypeface.FromFamilyName(Constants.DefaultCaptchaFallbackFontName);
135+
return _fallbackFontTypeface;
131136
}
132137

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

src/Captcha.WebApi/Dockerfile

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,6 @@
33
# This stage is used when running from VS in fast mode (Default for Debug configuration)
44
FROM mcr.microsoft.com/dotnet/aspnet:9.0-noble AS base
55

6-
# -- INSTALL FONTS USED TO RENDER CAPTCHA IMAGES --
7-
RUN apt-get update -y
8-
# Install Arimo
9-
RUN apt-get --no-install-recommends install -y fonts-croscore
10-
# Install Unifont
11-
RUN apt-get --no-install-recommends install -y fonts-unifont
12-
# -------------------------------------------------
13-
146
USER $APP_UID
157
WORKDIR /app
168
EXPOSE 8080

tests/Captcha.UnitTests/ConstantsTests.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,10 @@ public class ConstantsTests
2323
public void DefaultFrequencyShouldBe100() => Assert.That(Constants.DefaultFrequency, Is.EqualTo(100F));
2424

2525
[Test]
26-
public void DefaultCaptchaFontNameShouldBeArimo() => Assert.That(Constants.DefaultCaptchaFontName, Is.EqualTo("Arimo"));
26+
public void DefaultCaptchaFontNameShouldBeCaveat() => Assert.That(Constants.DefaultCaptchaFontName, Is.EqualTo("Captcha.Core.Resources.Fonts.Caveat-SemiBold.ttf"));
2727

2828
[Test]
29-
public void FallbackCaptchaFontNameShouldBeUnifont() => Assert.That(Constants.DefaultCaptchaFallbackFontName, Is.EqualTo("Unifont"));
30-
29+
public void FallbackCaptchaFontNameShouldBeUnifont() => Assert.That(Constants.DefaultCaptchaFallbackFontName, Is.EqualTo("Captcha.Core.Resources.Fonts.Unifont.ttf"));
3130

3231
[Test]
3332
public void DefaultPrimaryColorShouldBeDarkGray() => Assert.That(Constants.DefaultPrimaryColor, Is.EqualTo(SKColor.Parse("FFD3D3D3")));

0 commit comments

Comments
 (0)