Mirror of
0
fork

Configure Feed

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

make font loader secure?

+38 -7
+5
.changeset/rude-vans-send.md
··· 1 + --- 2 + "release-image-generator": patch 3 + --- 4 + 5 + Try making font loader more secure (because fonts are not working)
+33 -7
app/ReleaseImageGenerator.Domain/TextGenerator.cs
··· 1 1 using System.Net.Mime; 2 + using System.Reflection; 2 3 using ReleaseImageGenerator.Domain.Implementations; 3 4 using SkiaSharp; 4 5 using Wacton.Unicolour; ··· 7 8 8 9 public static class TextGenerator 9 10 { 10 - public static void GenerateText(SKCanvas canvas, string text, int width, int height, SupportedFontFamily fontFamily, SupportedFontWeight fontWeight, Unicolour primaryColor) 11 + public static void GenerateText(SKCanvas canvas, string text, int width, int height, SupportedFontFamily fontFamily, 12 + SupportedFontWeight fontWeight, Unicolour primaryColor) 11 13 { 12 - var typeface = SKTypeface.FromFile($"./fonts/{fontFamily.ToString()}-{fontWeight.ToString()}.ttf") ?? SKTypeface.FromFile($"./fonts/readexpro-bold.ttf") ?? SKTypeface.Default; 14 + var typeface = LoadFont(fontFamily.ToString(), fontWeight.ToString()); 13 15 var fontsize = GetMaxFontSize(width - width / 3, typeface, text, 1f, width > height ? height / 3 : width / 3); 14 16 15 17 // Calculate text size and position ··· 27 29 28 30 float textX = (width - textwidth) / 2; 29 31 float textY = height / 2 + textBounds.Height / 2; 30 - 32 + 31 33 // Add light text shadow 32 34 var textShadowPaint = new SKPaint 33 35 { ··· 44 46 textPaint.Shader = SKShader.CreateLinearGradient( 45 47 new SKPoint(textX, textY + textBounds.Top), 46 48 new SKPoint(textX + textwidth, textY + textBounds.Bottom), 47 - new[] { SKColors.White, ColorGenerator.UnicolourToSKColor(new Unicolour(ColourSpace.Oklch, (0.8 + Math.Max(0, (primaryColor.Oklch.L - 0.8) * 0.5), 0, 0), 1D)) }, 49 + new[] 50 + { 51 + SKColors.White, 52 + ColorGenerator.UnicolourToSKColor(new Unicolour(ColourSpace.Oklch, 53 + (0.8 + Math.Max(0, (primaryColor.Oklch.L - 0.8) * 0.5), 0, 0), 1D)) 54 + }, 48 55 SKShaderTileMode.Clamp 49 56 ); 50 57 ··· 79 86 80 87 canvas.DrawRoundRect(rect, fontsize / 4, fontsize / 4, bgPaint); 81 88 canvas.DrawRoundRect(rect, fontsize / 4, fontsize / 4, borderPaint); 82 - 89 + 83 90 // Draw the text 84 91 canvas.DrawText(text, textX, textY, textPaint); 85 92 } 86 - 87 - private static float GetMaxFontSize(double sectorSize, SKTypeface typeface, string text, float degreeOfCertainty = 1f, 93 + 94 + private static SKTypeface LoadFont(string fontFamily, string fontWeight) 95 + { 96 + var fontName = $"{fontFamily}-{fontWeight}.ttf"; 97 + var resourceName = $"YourNamespace.Resources.{fontName}"; 98 + 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 + return SKTypeface.FromFile($"./fonts/{fontFamily}-{fontWeight}.ttf") ?? 109 + SKTypeface.FromFile($"./fonts/readexpro-bold.ttf") ?? SKTypeface.Default; 110 + } 111 + 112 + private static float GetMaxFontSize(double sectorSize, SKTypeface typeface, string text, 113 + float degreeOfCertainty = 1f, 88 114 float maxFont = 100f) 89 115 { 90 116 var max = maxFont; // The upper bound. We know the font size is below this value