Skip to content
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
10 changes: 8 additions & 2 deletions src/Captcha.Core/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,17 @@ public static class Constants
public const int DefaultCaptchaHeight = 100;
public const float DefaultFrequency = 100F;
public const int FrequencyScalingFactor = 40000; // on a 400 x 100 image
public const string DefaultCaptchaFontName = "Captcha.Core.Resources.Fonts.Caveat-SemiBold.ttf";
public const string DefaultCaptchaFallbackFontName = "Captcha.Core.Resources.Fonts.Unifont.ttf";
public static readonly SKColor DefaultPrimaryColor = SKColor.Parse("FFD3D3D3");
public static readonly SKColor DefaultSecondaryColor = SKColor.Parse("FFFFFFFF");
public const string CaptchaContentType = "image/jpeg";
public const float WarpCaptchaTextFrequency = 4F;
public const int CaptchaNoise = 50;

// According to https://learn.microsoft.com/en-us/dotnet/api/skiasharp.sktypeface, this should be thread-safe
public static SKTypeface MainFontTypeface { get; } =
SKTypeface.FromStream(typeof(Constants).Assembly.GetManifestResourceStream("Captcha.Core.Resources.Fonts.Caveat-SemiBold.ttf"));

// According to https://learn.microsoft.com/en-us/dotnet/api/skiasharp.sktypeface, this should be thread-safe
public static SKTypeface FallbackFontTypeface { get; } =
SKTypeface.FromStream(typeof(Constants).Assembly.GetManifestResourceStream("Captcha.Core.Resources.Fonts.Unifont.ttf"));
}
26 changes: 5 additions & 21 deletions src/Captcha.Core/Services/CaptchaImageService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,6 @@ namespace Captcha.Core.Services;

public class CaptchaImageService : ICaptchaImageService
{
private readonly SKTypeface _mainFontTypeface;
private readonly SKTypeface _fallbackFontTypeface;

public CaptchaImageService()
{
var assembly = typeof(CaptchaImageService).Assembly;

_mainFontTypeface = SKTypeface.FromStream(
assembly.GetManifestResourceStream(Constants.DefaultCaptchaFontName)
);

_fallbackFontTypeface = SKTypeface.FromStream(
assembly.GetManifestResourceStream(Constants.DefaultCaptchaFallbackFontName)
);
}

public SKBitmap CreateCaptchaImage(CaptchaConfigurationData config)
{
var bitmap = new SKBitmap(new SKImageInfo(config.Width, config.Height));
Expand Down Expand Up @@ -89,7 +73,7 @@ private static void DrawWarpedText(CaptchaConfigurationData config, SKFont font,
graphics.DrawPath(path, fillPaint);
}

private SKFont GetFontThatFitsRectangle(CaptchaConfigurationData config, SKRect rectangle)
private static SKFont GetFontThatFitsRectangle(CaptchaConfigurationData config, SKRect rectangle)
{
var typeface = GetTypefaceThatCanRenderText(config.Text);

Expand Down Expand Up @@ -123,16 +107,16 @@ private static void FillInTheBackground(CaptchaConfigurationData config, SKRect
graphics.DrawRect(rectangle, paint);
}

private SKTypeface GetTypefaceThatCanRenderText(string text)
private static SKTypeface GetTypefaceThatCanRenderText(string text)
{
using var mainFont = new SKFont(_mainFontTypeface);
using var mainFont = new SKFont(Constants.MainFontTypeface);

if (mainFont.ContainsGlyphs(text))
{
return _mainFontTypeface;
return Constants.MainFontTypeface;
}

return _fallbackFontTypeface;
return Constants.FallbackFontTypeface;
}

/// <summary>
Expand Down
6 changes: 0 additions & 6 deletions tests/Captcha.UnitTests/ConstantsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,6 @@ public class ConstantsTests
[Test]
public void DefaultFrequencyShouldBe100() => Assert.That(Constants.DefaultFrequency, Is.EqualTo(100F));

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

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

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

Expand Down
Loading