Mirror of
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

try logging to find out why fonts are not working

deploy

+21 -28
+5 -5
app/ReleaseImageGenerator.API/Program.cs
··· 1 - using SkiaSharp; 2 - using System; 3 - using Microsoft.AspNetCore.Mvc; 1 + using Microsoft.JSInterop; 4 2 using ReleaseImageGenerator.Domain.Implementations; 5 3 6 4 var builder = WebApplication.CreateBuilder(args); ··· 21 19 app.UseHttpsRedirection(); 22 20 23 21 app.MapGet("/", 24 - (string? text, int? width, int? height, SupportedFontFamily? fontFamily, SupportedFontWeight? fontWeight) => 22 + (HttpContext context, string? text, int? width, int? height, SupportedFontFamily? fontFamily, SupportedFontWeight? fontWeight) => 25 23 { 26 24 var options = new ImageGeneratorOptions(text, width ?? 1920, height ?? 1080, 27 25 fontFamily ?? SupportedFontFamily.readexpro, fontWeight ?? SupportedFontWeight.bold); 28 26 var imageGenerator = new ImageGenerator(options); 29 - return Results.File(imageGenerator.GenerateImage().ToArray(), "image/png"); 27 + var result = imageGenerator.GenerateImage(); 28 + result.Item2.ForEach(s => context.Response.Headers.Append("X-Debug-Log", s)); 29 + return Results.File(result.Item1.ToArray(), "image/png"); 30 30 }) 31 31 .WithName("ImageGenerator") 32 32 .WithOpenApi();
+1 -4
app/ReleaseImageGenerator.Domain/BackgroundGenerator.cs
··· 5 5 6 6 public static class BackgroundGenerator 7 7 { 8 - public static Unicolour GenerateBackground(SKCanvas canvas, int width, int height, Random random) 8 + public static Unicolour GenerateBackground(SKCanvas canvas, int width, int height, Random random, Unicolour primaryColor) 9 9 { 10 10 // Generate a harmonious color palette around the primary color 11 - var primaryColor = ColorGenerator.GetRandomColor(ColorGenerator.ColorLimitation.NEUTRAL_LIGHTNESS, 12 - ColorGenerator.ColorLimitation.NEUTRAL_SATURATION); 13 - 14 11 var colorPalette = ColorGenerator.GetRandomPalette(primaryColor, 8).Select(ColorGenerator.UnicolourToSKColor).ToArray(); 15 12 16 13 var backgroundRotationIsClockwise = random.Next(2) == 0;
+8 -5
app/ReleaseImageGenerator.Domain/Implementations/ImageGenerator.cs
··· 21 21 FontWeight = options.fontWeight; 22 22 } 23 23 24 - public MemoryStream GenerateImage() 24 + public Tuple<MemoryStream, List<string>> GenerateImage() 25 25 { 26 + List<string> logger = new List<string>(); 26 27 using var surface = SKSurface.Create(new SKImageInfo(Width, Height)); 27 28 var canvas = surface.Canvas; 28 29 var random = new Random(); 29 30 30 - var primaryColor = BackgroundGenerator.GenerateBackground(canvas, Width, Height, random); 31 + var primaryColor = ColorGenerator.GetRandomColor(ColorGenerator.ColorLimitation.NEUTRAL_LIGHTNESS, 32 + ColorGenerator.ColorLimitation.NEUTRAL_SATURATION); 33 + BackgroundGenerator.GenerateBackground(canvas, Width, Height, random, primaryColor); 31 34 PatternGenerator.GeneratePattern(canvas, Width, Height, random, primaryColor); 32 35 NoiseGenerator.GenerateNoise(canvas, Width, Height, random); 33 - if (Text != null) TextGenerator.GenerateText(canvas, Text, Width, Height, FontFamily, FontWeight, primaryColor); 36 + if (Text != null) TextGenerator.GenerateText(canvas, Text, Width, Height, FontFamily, FontWeight, primaryColor, logger); 34 37 35 - // Return as JPEG 38 + // Return as PNG 36 39 var stream = new MemoryStream(); 37 40 using var image = surface.Snapshot(); 38 41 using var data = image.Encode(SKEncodedImageFormat.Png, 100); 39 42 data.SaveTo(stream); 40 - return stream; 43 + return new Tuple<MemoryStream, List<string>>(stream, logger); 41 44 } 42 45 }
+1 -1
app/ReleaseImageGenerator.Domain/Interfaces/IImageGenerator.cs
··· 4 4 5 5 public interface IImageGenerator 6 6 { 7 - MemoryStream GenerateImage(); 7 + Tuple<MemoryStream, List<string>> GenerateImage(); 8 8 }
+6 -13
app/ReleaseImageGenerator.Domain/TextGenerator.cs
··· 9 9 public static class TextGenerator 10 10 { 11 11 public static void GenerateText(SKCanvas canvas, string text, int width, int height, SupportedFontFamily fontFamily, 12 - SupportedFontWeight fontWeight, Unicolour primaryColor) 12 + SupportedFontWeight fontWeight, Unicolour primaryColor, List<string> logger) 13 13 { 14 - var typeface = LoadFont(fontFamily.ToString(), fontWeight.ToString()); 14 + var typeface = LoadFont(fontFamily.ToString(), fontWeight.ToString(), logger); 15 + logger.Add("Family Name: " + typeface.FamilyName); 15 16 var fontsize = GetMaxFontSize(width - width / 3, typeface, text, 1f, width > height ? height / 3 : width / 3); 17 + logger.Add("Font size: " + fontsize); 16 18 17 19 // Calculate text size and position 18 20 var textPaint = new SKPaint ··· 91 93 canvas.DrawText(text, textX, textY, textPaint); 92 94 } 93 95 94 - private static SKTypeface LoadFont(string fontFamily, string fontWeight) 96 + private static SKTypeface LoadFont(string fontFamily, string fontWeight, List<string> logger) 95 97 { 96 98 var fontName = $"{fontFamily}-{fontWeight}.ttf"; 97 - var resourceName = $"YourNamespace.Resources.{fontName}"; 99 + logger.Add("File name: " + fontName); 98 100 99 - using (var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(resourceName)) 100 - { 101 - if (stream != null) 102 - { 103 - return SKTypeface.FromStream(stream); 104 - } 105 - } 106 - 107 - // Fallback to a default font if not found 108 101 return SKTypeface.FromFile($"./fonts/{fontFamily}-{fontWeight}.ttf") ?? 109 102 SKTypeface.FromFile($"./fonts/readexpro-bold.ttf") ?? SKTypeface.Default; 110 103 }