Helpers.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. using System;
  2. using System.Collections.Concurrent;
  3. using System.IO;
  4. using System.Text;
  5. using QuestPDF.Fluent;
  6. using QuestPDF.Infrastructure;
  7. using SkiaSharp;
  8. namespace QuestPDF.ReportSample
  9. {
  10. public static class Helpers
  11. {
  12. public static Random Random { get; } = new Random();
  13. public static string GetTestItem(string path) => Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Resources", path);
  14. public static byte[] GetImage(string name)
  15. {
  16. var photoPath = GetTestItem(name);
  17. return SKImage.FromEncodedData(photoPath).EncodedData.ToArray();
  18. }
  19. public static Location RandomLocation()
  20. {
  21. return new Location
  22. {
  23. Longitude = Helpers.Random.NextDouble() * 360f - 180f,
  24. Latitude = Helpers.Random.NextDouble() * 180f - 90f
  25. };
  26. }
  27. private static readonly ConcurrentDictionary<int, string> RomanNumeralCache = new ConcurrentDictionary<int, string>();
  28. public static string FormatAsRomanNumeral(this int number)
  29. {
  30. if (number < 0 || number > 3999)
  31. throw new ArgumentOutOfRangeException(nameof(number), "Number should be in range from 1 to 3999");
  32. return RomanNumeralCache.GetOrAdd(number, x =>
  33. {
  34. if (x >= 1000) return "M" + FormatAsRomanNumeral(x - 1000);
  35. if (x >= 900) return "CM" + FormatAsRomanNumeral(x - 900);
  36. if (x >= 500) return "D" + FormatAsRomanNumeral(x - 500);
  37. if (x >= 400) return "CD" + FormatAsRomanNumeral(x - 400);
  38. if (x >= 100) return "C" + FormatAsRomanNumeral(x - 100);
  39. if (x >= 90) return "XC" + FormatAsRomanNumeral(x - 90);
  40. if (x >= 50) return "L" + FormatAsRomanNumeral(x - 50);
  41. if (x >= 40) return "XL" + FormatAsRomanNumeral(x - 40);
  42. if (x >= 10) return "X" + FormatAsRomanNumeral(x - 10);
  43. if (x >= 9) return "IX" + FormatAsRomanNumeral(x - 9);
  44. if (x >= 5) return "V" + FormatAsRomanNumeral(x - 5);
  45. if (x >= 4) return "IV" + FormatAsRomanNumeral(x - 4);
  46. if (x >= 1) return "I" + FormatAsRomanNumeral(x - 1);
  47. return string.Empty;
  48. });
  49. }
  50. public static void SkiaSharpCanvas(this IContainer container, Action<SKCanvas, Size> drawOnCanvas)
  51. {
  52. container.Svg(size =>
  53. {
  54. using var stream = new MemoryStream();
  55. using (var canvas = SKSvgCanvas.Create(new SKRect(0, 0, size.Width, size.Height), stream))
  56. drawOnCanvas(canvas, size);
  57. var svgData = stream.ToArray();
  58. return Encoding.UTF8.GetString(svgData);
  59. });
  60. }
  61. public static void SkiaSharpRasterized(this IContainer container, Action<SKCanvas, Size> drawOnCanvas)
  62. {
  63. container.Image(payload =>
  64. {
  65. using var bitmap = new SKBitmap(payload.ImageSize.Width, payload.ImageSize.Height);
  66. using (var canvas = new SKCanvas(bitmap))
  67. {
  68. var scalingFactor = payload.Dpi / (float)DocumentSettings.DefaultRasterDpi;
  69. canvas.Scale(scalingFactor);
  70. drawOnCanvas(canvas, payload.AvailableSpace);
  71. }
  72. return bitmap.Encode(SKEncodedImageFormat.Png, 100).ToArray();
  73. });
  74. }
  75. }
  76. }