Skip to content

Commit

Permalink
Refactor meme generator
Browse files Browse the repository at this point in the history
  • Loading branch information
ronnygunawan committed Nov 25, 2023
1 parent 19b38f6 commit 1b7ed9b
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 18 deletions.
33 changes: 18 additions & 15 deletions BotNet.Services/Meme/MemeGenerator.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using BotNet.Services.Typography;
using SkiaSharp;

Expand All @@ -16,38 +15,42 @@ BotNetFontService botNetFontService
}

public byte[] CaptionRamad(string text) {
using Stream templateStream = typeof(MemeGenerator).Assembly.GetManifestResourceStream(Templates.RAMAD.ImageResourceName)!;
using SKBitmap template = SKBitmap.Decode(templateStream);
using SKSurface surface = SKSurface.Create(new SKImageInfo(template.Width, template.Height));
return CaptionMeme(Templates.RAMAD, text);
}

private byte[] CaptionMeme(Template template, string text) {
using Stream templateStream = typeof(MemeGenerator).Assembly.GetManifestResourceStream(template.ImageResourceName)!;
using SKBitmap templateBitmap = SKBitmap.Decode(templateStream);
using SKSurface surface = SKSurface.Create(new SKImageInfo(templateBitmap.Width, templateBitmap.Height));
using SKCanvas canvas = surface.Canvas;

SKRect templateRect = SKRect.Create(template.Width, template.Height);
SKRect templateRect = SKRect.Create(templateBitmap.Width, templateBitmap.Height);
canvas.DrawBitmap(
bitmap: template,
bitmap: templateBitmap,
source: templateRect,
dest: templateRect
);

canvas.Save();
canvas.RotateDegrees(1.4f);
using Stream fontStream = _botNetFontService.GetFontStyleById("Inter-Regular").OpenStream();
canvas.RotateDegrees(template.Rotation);
using Stream fontStream = _botNetFontService.GetFontStyleById(template.FontStyleId).OpenStream();
using SKTypeface typeface = SKTypeface.FromStream(fontStream);
using SKPaint paint = new() {
TextAlign = SKTextAlign.Left,
Color = new SKColor(0x00, 0x00, 0x00, 0xcc),
TextAlign = template.TextAlign,
Color = template.TextColor,
Typeface = typeface,
TextSize = 17f,
TextSize = template.TextSize,
IsAntialias = true
};
float offset = 0f;
foreach (string line in WrapWords(text, 110f, paint)) {
foreach (string line in WrapWords(text, template.MaxWidth, paint)) {
canvas.DrawText(
text: line,
x: 120f,
y: 100f + offset,
x: template.X,
y: template.Y + offset,
paint: paint
);
offset += 20f; // line height
offset += template.LineHeight;
}
canvas.Restore();

Expand Down
28 changes: 25 additions & 3 deletions BotNet.Services/Meme/Templates.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,31 @@
namespace BotNet.Services.Meme {
using SkiaSharp;

namespace BotNet.Services.Meme {
internal sealed record Template(
string ImageResourceName
string ImageResourceName,
string FontStyleId,
SKTextAlign TextAlign,
SKColor TextColor,
float TextSize,
float LineHeight,
float MaxWidth,
float Rotation,
float X,
float Y
);

internal static class Templates {
public static readonly Template RAMAD = new("BotNet.Services.Meme.Images.Ramad.jpg");
public static readonly Template RAMAD = new(
ImageResourceName: "BotNet.Services.Meme.Images.Ramad.jpg",
FontStyleId: "Inter-Regular",
TextAlign: SKTextAlign.Left,
TextColor: new SKColor(0x00, 0x00, 0x00, 0xcc),
TextSize: 17f,
LineHeight: 20f,
MaxWidth: 110f,
Rotation: 1.4f,
X: 120f,
Y: 100f
);
}
}

0 comments on commit 1b7ed9b

Please sign in to comment.