Helpers.cs 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. using System;
  2. using System.Collections.Concurrent;
  3. using System.IO;
  4. using SkiaSharp;
  5. namespace QuestPDF.ReportSample
  6. {
  7. public static class Helpers
  8. {
  9. public static Random Random { get; } = new Random(1);
  10. public static string GetTestItem(string path) => Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Resources", path);
  11. public static byte[] GetImage(string name)
  12. {
  13. var photoPath = GetTestItem(name);
  14. return SKImage.FromEncodedData(photoPath).EncodedData.ToArray();
  15. }
  16. public static Location RandomLocation()
  17. {
  18. return new Location
  19. {
  20. Longitude = Helpers.Random.NextDouble() * 360f - 180f,
  21. Latitude = Helpers.Random.NextDouble() * 180f - 90f
  22. };
  23. }
  24. private static readonly ConcurrentDictionary<int, string> RomanNumeralCache = new ConcurrentDictionary<int, string>();
  25. public static string FormatAsRomanNumeral(this int number)
  26. {
  27. if (number < 0 || number > 3999)
  28. throw new ArgumentOutOfRangeException("Number should be in range from 1 to 3999");
  29. return RomanNumeralCache.GetOrAdd(number, x =>
  30. {
  31. if (x >= 1000) return "M" + FormatAsRomanNumeral(x - 1000);
  32. if (x >= 900) return "CM" + FormatAsRomanNumeral(x - 900);
  33. if (x >= 500) return "D" + FormatAsRomanNumeral(x - 500);
  34. if (x >= 400) return "CD" + FormatAsRomanNumeral(x - 400);
  35. if (x >= 100) return "C" + FormatAsRomanNumeral(x - 100);
  36. if (x >= 90) return "XC" + FormatAsRomanNumeral(x - 90);
  37. if (x >= 50) return "L" + FormatAsRomanNumeral(x - 50);
  38. if (x >= 40) return "XL" + FormatAsRomanNumeral(x - 40);
  39. if (x >= 10) return "X" + FormatAsRomanNumeral(x - 10);
  40. if (x >= 9) return "IX" + FormatAsRomanNumeral(x - 9);
  41. if (x >= 5) return "V" + FormatAsRomanNumeral(x - 5);
  42. if (x >= 4) return "IV" + FormatAsRomanNumeral(x - 4);
  43. if (x >= 1) return "I" + FormatAsRomanNumeral(x - 1);
  44. return string.Empty;
  45. });
  46. }
  47. }
  48. }