| | 1 | | using System; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using System.IO; |
| | 4 | | using System.Text.RegularExpressions; |
| | 5 | | using SkiaSharp; |
| | 6 | | using SkiaSharp.HarfBuzz; |
| | 7 | |
|
| | 8 | | namespace Jellyfin.Drawing.Skia; |
| | 9 | |
|
| | 10 | | /// <summary> |
| | 11 | | /// Used to build collages of multiple images arranged in vertical strips. |
| | 12 | | /// </summary> |
| | 13 | | public partial class StripCollageBuilder |
| | 14 | | { |
| | 15 | | private readonly SkiaEncoder _skiaEncoder; |
| | 16 | |
|
| | 17 | | /// <summary> |
| | 18 | | /// Initializes a new instance of the <see cref="StripCollageBuilder"/> class. |
| | 19 | | /// </summary> |
| | 20 | | /// <param name="skiaEncoder">The encoder to use for building collages.</param> |
| | 21 | | public StripCollageBuilder(SkiaEncoder skiaEncoder) |
| | 22 | | { |
| 0 | 23 | | _skiaEncoder = skiaEncoder; |
| 0 | 24 | | } |
| | 25 | |
|
| | 26 | | [GeneratedRegex(@"[^\p{IsCJKUnifiedIdeographs}\p{IsCJKUnifiedIdeographsExtensionA}\p{IsKatakana}\p{IsHiragana}\p{IsH |
| | 27 | | private static partial Regex NonCjkPatternRegex(); |
| | 28 | |
|
| | 29 | | [GeneratedRegex(@"\p{IsArabic}|\p{IsArmenian}|\p{IsHebrew}|\p{IsSyriac}|\p{IsThaana}")] |
| | 30 | | private static partial Regex IsRtlTextRegex(); |
| | 31 | |
|
| | 32 | | /// <summary> |
| | 33 | | /// Check which format an image has been encoded with using its filename extension. |
| | 34 | | /// </summary> |
| | 35 | | /// <param name="outputPath">The path to the image to get the format for.</param> |
| | 36 | | /// <returns>The image format.</returns> |
| | 37 | | public static SKEncodedImageFormat GetEncodedFormat(string outputPath) |
| | 38 | | { |
| 0 | 39 | | ArgumentNullException.ThrowIfNull(outputPath); |
| | 40 | |
|
| 0 | 41 | | var ext = Path.GetExtension(outputPath.AsSpan()); |
| | 42 | |
|
| 0 | 43 | | if (ext.Equals(".jpg", StringComparison.OrdinalIgnoreCase) |
| 0 | 44 | | || ext.Equals(".jpeg", StringComparison.OrdinalIgnoreCase)) |
| | 45 | | { |
| 0 | 46 | | return SKEncodedImageFormat.Jpeg; |
| | 47 | | } |
| | 48 | |
|
| 0 | 49 | | if (ext.Equals(".webp", StringComparison.OrdinalIgnoreCase)) |
| | 50 | | { |
| 0 | 51 | | return SKEncodedImageFormat.Webp; |
| | 52 | | } |
| | 53 | |
|
| 0 | 54 | | if (ext.Equals(".gif", StringComparison.OrdinalIgnoreCase)) |
| | 55 | | { |
| 0 | 56 | | return SKEncodedImageFormat.Gif; |
| | 57 | | } |
| | 58 | |
|
| 0 | 59 | | if (ext.Equals(".bmp", StringComparison.OrdinalIgnoreCase)) |
| | 60 | | { |
| 0 | 61 | | return SKEncodedImageFormat.Bmp; |
| | 62 | | } |
| | 63 | |
|
| | 64 | | // default to png |
| 0 | 65 | | return SKEncodedImageFormat.Png; |
| | 66 | | } |
| | 67 | |
|
| | 68 | | /// <summary> |
| | 69 | | /// Create a square collage. |
| | 70 | | /// </summary> |
| | 71 | | /// <param name="paths">The paths of the images to use in the collage.</param> |
| | 72 | | /// <param name="outputPath">The path at which to place the resulting collage image.</param> |
| | 73 | | /// <param name="width">The desired width of the collage.</param> |
| | 74 | | /// <param name="height">The desired height of the collage.</param> |
| | 75 | | public void BuildSquareCollage(IReadOnlyList<string> paths, string outputPath, int width, int height) |
| | 76 | | { |
| 0 | 77 | | using var bitmap = BuildSquareCollageBitmap(paths, width, height); |
| 0 | 78 | | using var outputStream = new SKFileWStream(outputPath); |
| 0 | 79 | | using var pixmap = new SKPixmap(new SKImageInfo(width, height), bitmap.GetPixels()); |
| 0 | 80 | | pixmap.Encode(outputStream, GetEncodedFormat(outputPath), 90); |
| 0 | 81 | | } |
| | 82 | |
|
| | 83 | | /// <summary> |
| | 84 | | /// Create a thumb collage. |
| | 85 | | /// </summary> |
| | 86 | | /// <param name="paths">The paths of the images to use in the collage.</param> |
| | 87 | | /// <param name="outputPath">The path at which to place the resulting image.</param> |
| | 88 | | /// <param name="width">The desired width of the collage.</param> |
| | 89 | | /// <param name="height">The desired height of the collage.</param> |
| | 90 | | /// <param name="libraryName">The name of the library to draw on the collage.</param> |
| | 91 | | public void BuildThumbCollage(IReadOnlyList<string> paths, string outputPath, int width, int height, string? library |
| | 92 | | { |
| 0 | 93 | | using var bitmap = BuildThumbCollageBitmap(paths, width, height, libraryName); |
| 0 | 94 | | using var outputStream = new SKFileWStream(outputPath); |
| 0 | 95 | | using var pixmap = new SKPixmap(new SKImageInfo(width, height), bitmap.GetPixels()); |
| 0 | 96 | | pixmap.Encode(outputStream, GetEncodedFormat(outputPath), 90); |
| 0 | 97 | | } |
| | 98 | |
|
| | 99 | | private SKBitmap BuildThumbCollageBitmap(IReadOnlyList<string> paths, int width, int height, string? libraryName) |
| | 100 | | { |
| 0 | 101 | | var bitmap = new SKBitmap(width, height); |
| | 102 | |
|
| 0 | 103 | | using var canvas = new SKCanvas(bitmap); |
| 0 | 104 | | canvas.Clear(SKColors.Black); |
| | 105 | |
|
| 0 | 106 | | using var backdrop = SkiaHelper.GetNextValidImage(_skiaEncoder, paths, 0, out _); |
| 0 | 107 | | if (backdrop is null) |
| | 108 | | { |
| 0 | 109 | | return bitmap; |
| | 110 | | } |
| | 111 | |
|
| | 112 | | // resize to the same aspect as the original |
| 0 | 113 | | var backdropHeight = Math.Abs(width * backdrop.Height / backdrop.Width); |
| 0 | 114 | | using var residedBackdrop = SkiaEncoder.ResizeImage(backdrop, new SKImageInfo(width, backdropHeight, backdrop.Co |
| | 115 | | // draw the backdrop |
| 0 | 116 | | canvas.DrawImage(residedBackdrop, 0, 0); |
| | 117 | |
|
| | 118 | | // draw shadow rectangle |
| 0 | 119 | | using var paintColor = new SKPaint |
| 0 | 120 | | { |
| 0 | 121 | | Color = SKColors.Black.WithAlpha(0x78), |
| 0 | 122 | | Style = SKPaintStyle.Fill |
| 0 | 123 | | }; |
| 0 | 124 | | canvas.DrawRect(0, 0, width, height, paintColor); |
| | 125 | |
|
| 0 | 126 | | var typeFace = SKTypeface.FromFamilyName("sans-serif", SKFontStyleWeight.Bold, SKFontStyleWidth.Normal, SKFontSt |
| | 127 | |
|
| | 128 | | // use the system fallback to find a typeface for the given CJK character |
| 0 | 129 | | var filteredName = NonCjkPatternRegex().Replace(libraryName ?? string.Empty, string.Empty); |
| 0 | 130 | | if (!string.IsNullOrEmpty(filteredName)) |
| | 131 | | { |
| 0 | 132 | | typeFace = SKFontManager.Default.MatchCharacter(null, SKFontStyleWeight.Bold, SKFontStyleWidth.Normal, SKFon |
| | 133 | | } |
| | 134 | |
|
| | 135 | | // draw library name |
| 0 | 136 | | using var textPaint = new SKPaint |
| 0 | 137 | | { |
| 0 | 138 | | Color = SKColors.White, |
| 0 | 139 | | Style = SKPaintStyle.Fill, |
| 0 | 140 | | TextSize = 112, |
| 0 | 141 | | TextAlign = SKTextAlign.Center, |
| 0 | 142 | | Typeface = typeFace, |
| 0 | 143 | | IsAntialias = true |
| 0 | 144 | | }; |
| | 145 | |
|
| | 146 | | // scale down text to 90% of the width if text is larger than 95% of the width |
| 0 | 147 | | var textWidth = textPaint.MeasureText(libraryName); |
| 0 | 148 | | if (textWidth > width * 0.95) |
| | 149 | | { |
| 0 | 150 | | textPaint.TextSize = 0.9f * width * textPaint.TextSize / textWidth; |
| | 151 | | } |
| | 152 | |
|
| 0 | 153 | | if (string.IsNullOrWhiteSpace(libraryName)) |
| | 154 | | { |
| 0 | 155 | | return bitmap; |
| | 156 | | } |
| | 157 | |
|
| 0 | 158 | | if (IsRtlTextRegex().IsMatch(libraryName)) |
| | 159 | | { |
| 0 | 160 | | canvas.DrawShapedText(libraryName, width / 2f, (height / 2f) + (textPaint.FontMetrics.XHeight / 2), textPain |
| | 161 | | } |
| | 162 | | else |
| | 163 | | { |
| 0 | 164 | | canvas.DrawText(libraryName, width / 2f, (height / 2f) + (textPaint.FontMetrics.XHeight / 2), textPaint); |
| | 165 | | } |
| | 166 | |
|
| 0 | 167 | | return bitmap; |
| 0 | 168 | | } |
| | 169 | |
|
| | 170 | | private SKBitmap BuildSquareCollageBitmap(IReadOnlyList<string> paths, int width, int height) |
| | 171 | | { |
| 0 | 172 | | var bitmap = new SKBitmap(width, height); |
| 0 | 173 | | var imageIndex = 0; |
| 0 | 174 | | var cellWidth = width / 2; |
| 0 | 175 | | var cellHeight = height / 2; |
| | 176 | |
|
| 0 | 177 | | using var canvas = new SKCanvas(bitmap); |
| 0 | 178 | | for (var x = 0; x < 2; x++) |
| | 179 | | { |
| 0 | 180 | | for (var y = 0; y < 2; y++) |
| | 181 | | { |
| 0 | 182 | | using var currentBitmap = SkiaHelper.GetNextValidImage(_skiaEncoder, paths, imageIndex, out int newIndex |
| 0 | 183 | | imageIndex = newIndex; |
| | 184 | |
|
| 0 | 185 | | if (currentBitmap is null) |
| | 186 | | { |
| 0 | 187 | | continue; |
| | 188 | | } |
| | 189 | |
|
| | 190 | | // Scale image. The FromBitmap creates a copy |
| 0 | 191 | | var imageInfo = new SKImageInfo(cellWidth, cellHeight, currentBitmap.ColorType, currentBitmap.AlphaType, |
| 0 | 192 | | using var resizeImage = SkiaEncoder.ResizeImage(currentBitmap, imageInfo); |
| | 193 | |
|
| | 194 | | // draw this image into the strip at the next position |
| 0 | 195 | | var xPos = x * cellWidth; |
| 0 | 196 | | var yPos = y * cellHeight; |
| 0 | 197 | | canvas.DrawImage(resizeImage, xPos, yPos); |
| | 198 | | } |
| | 199 | | } |
| | 200 | |
|
| 0 | 201 | | return bitmap; |
| 0 | 202 | | } |
| | 203 | | } |