Audit + refactor: guard image decode/surface, clamp scrim & font size, Lazy typeface, dedupe backdrop lookup, drop unwired special-views scaffold
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
using System;
|
||||
using System.Reflection;
|
||||
using SkiaSharp;
|
||||
|
||||
@@ -6,62 +7,45 @@ namespace Jellyfin.Plugin.Placard;
|
||||
/// <summary>Composites a library name onto a backdrop, matching Jellyfin's default cover style.</summary>
|
||||
public static class CardRenderer
|
||||
{
|
||||
private static SKTypeface? _typeface;
|
||||
private const float FitWidthFraction = 0.90f; // label spans at most this much of the width
|
||||
private const float ShadowBlurDivisor = 20f; // blur radius = fontSize / this
|
||||
private const float ShadowOffsetDivisor = 28f; // shadow offset = fontSize / this
|
||||
private const int MinFontSize = 12;
|
||||
private const int JpegQuality = 92;
|
||||
|
||||
private static SKTypeface Typeface
|
||||
private static readonly Lazy<SKTypeface> LazyTypeface = new(LoadTypeface);
|
||||
|
||||
private static SKTypeface Typeface => LazyTypeface.Value;
|
||||
|
||||
private static SKTypeface LoadTypeface()
|
||||
{
|
||||
get
|
||||
var asm = Assembly.GetExecutingAssembly();
|
||||
using var stream = asm.GetManifestResourceStream("Jellyfin.Plugin.Placard.Fonts.NotoSans-Bold.ttf");
|
||||
if (stream is null)
|
||||
{
|
||||
if (_typeface != null)
|
||||
{
|
||||
return _typeface;
|
||||
}
|
||||
|
||||
var asm = Assembly.GetExecutingAssembly();
|
||||
using var s = asm.GetManifestResourceStream("Jellyfin.Plugin.Placard.Fonts.NotoSans-Bold.ttf");
|
||||
_typeface = s != null
|
||||
? SKTypeface.FromStream(s)
|
||||
: SKTypeface.FromFamilyName("sans-serif", SKFontStyle.Bold);
|
||||
return _typeface;
|
||||
return SKTypeface.FromFamilyName("sans-serif", SKFontStyle.Bold);
|
||||
}
|
||||
|
||||
// Copy into SKData so the typeface does not depend on the (disposed) resource stream.
|
||||
using var data = SKData.Create(stream);
|
||||
return SKTypeface.FromData(data) ?? SKTypeface.FromFamilyName("sans-serif", SKFontStyle.Bold);
|
||||
}
|
||||
|
||||
/// <summary>Render <paramref name="label"/> centered on the image at <paramref name="backdropPath"/>.</summary>
|
||||
/// <param name="scrim">Darkening overlay opacity, 0-255.</param>
|
||||
/// <param name="fontHeightPct">Label height as a percent of image height.</param>
|
||||
public static byte[] Render(string backdropPath, string label, int scrim, int fontHeightPct)
|
||||
{
|
||||
using var input = SKBitmap.Decode(backdropPath);
|
||||
return RenderBitmap(input, label, scrim, fontHeightPct);
|
||||
}
|
||||
using var input = SKBitmap.Decode(backdropPath)
|
||||
?? throw new InvalidOperationException($"Placard could not decode backdrop image: {backdropPath}");
|
||||
|
||||
/// <summary>Generate SMPTE color bars and label them (for Live TV, which has no media backdrop).</summary>
|
||||
public static byte[] RenderColorBars(string label, int scrim)
|
||||
{
|
||||
const int w = 1920;
|
||||
const int h = 1080;
|
||||
using var bmp = new SKBitmap(w, h);
|
||||
using (var canvas = new SKCanvas(bmp))
|
||||
{
|
||||
var cols = new[]
|
||||
{
|
||||
new SKColor(192, 192, 192), new SKColor(192, 192, 0), new SKColor(0, 192, 192),
|
||||
new SKColor(0, 192, 0), new SKColor(192, 0, 192), new SKColor(192, 0, 0), new SKColor(0, 0, 192)
|
||||
};
|
||||
float bw = (float)w / cols.Length;
|
||||
for (int i = 0; i < cols.Length; i++)
|
||||
{
|
||||
using var p = new SKPaint { Color = cols[i] };
|
||||
canvas.DrawRect(i * bw, 0, bw + 1, h, p);
|
||||
}
|
||||
}
|
||||
|
||||
return RenderBitmap(bmp, label, scrim, 20);
|
||||
}
|
||||
|
||||
private static byte[] RenderBitmap(SKBitmap input, string label, int scrim, int fontHeightPct)
|
||||
{
|
||||
int w = input.Width;
|
||||
int h = input.Height;
|
||||
using var surface = SKSurface.Create(new SKImageInfo(w, h));
|
||||
scrim = Math.Clamp(scrim, 0, 255);
|
||||
fontHeightPct = Math.Clamp(fontHeightPct, 5, 40);
|
||||
|
||||
using var surface = SKSurface.Create(new SKImageInfo(w, h))
|
||||
?? throw new InvalidOperationException($"Placard could not create a {w}x{h} drawing surface");
|
||||
var canvas = surface.Canvas;
|
||||
canvas.DrawBitmap(input, 0, 0);
|
||||
|
||||
@@ -70,20 +54,22 @@ public static class CardRenderer
|
||||
canvas.DrawRect(0, 0, w, h, scrimPaint);
|
||||
}
|
||||
|
||||
using var text = new SKPaint { Typeface = Typeface, IsAntialias = true, Color = SKColors.White };
|
||||
|
||||
// Shrink until the label fits within FitWidthFraction of the width.
|
||||
float size = h * fontHeightPct / 100f;
|
||||
using var measure = new SKPaint { Typeface = Typeface, IsAntialias = true };
|
||||
for (; size > 12; size -= 4)
|
||||
for (; size > MinFontSize; size -= 4)
|
||||
{
|
||||
measure.TextSize = size;
|
||||
if (measure.MeasureText(label) <= w * 0.90f)
|
||||
text.TextSize = size;
|
||||
if (text.MeasureText(label) <= w * FitWidthFraction)
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
measure.TextSize = size;
|
||||
text.TextSize = size;
|
||||
SKRect bounds = default;
|
||||
measure.MeasureText(label, ref bounds);
|
||||
text.MeasureText(label, ref bounds);
|
||||
float x = ((w - bounds.Width) / 2f) - bounds.Left;
|
||||
float y = ((h - bounds.Height) / 2f) - bounds.Top;
|
||||
|
||||
@@ -93,25 +79,16 @@ public static class CardRenderer
|
||||
TextSize = size,
|
||||
IsAntialias = true,
|
||||
Color = new SKColor(0, 0, 0, 205),
|
||||
MaskFilter = SKMaskFilter.CreateBlur(SKBlurStyle.Normal, size / 20f)
|
||||
MaskFilter = SKMaskFilter.CreateBlur(SKBlurStyle.Normal, size / ShadowBlurDivisor)
|
||||
})
|
||||
{
|
||||
canvas.DrawText(label, x, y + (size / 28f), shadow);
|
||||
canvas.DrawText(label, x, y + (size / ShadowOffsetDivisor), shadow);
|
||||
}
|
||||
|
||||
using (var text = new SKPaint
|
||||
{
|
||||
Typeface = Typeface,
|
||||
TextSize = size,
|
||||
IsAntialias = true,
|
||||
Color = SKColors.White
|
||||
})
|
||||
{
|
||||
canvas.DrawText(label, x, y, text);
|
||||
}
|
||||
canvas.DrawText(label, x, y, text);
|
||||
|
||||
using var image = surface.Snapshot();
|
||||
using var data = image.Encode(SKEncodedImageFormat.Jpeg, 92);
|
||||
using var data = image.Encode(SKEncodedImageFormat.Jpeg, JpegQuality);
|
||||
return data.ToArray();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user