Files
QuestPDF/Source/QuestPDF.ReportSample/Helpers.cs
T

56 lines
2.3 KiB
C#
Raw Normal View History

2021-01-16 01:31:39 +01:00
using System;
2022-02-21 00:57:53 +01:00
using System.Collections.Concurrent;
2021-01-16 01:31:39 +01:00
using System.IO;
using SkiaSharp;
namespace QuestPDF.ReportSample
{
public static class Helpers
{
2021-09-06 21:43:12 +02:00
public static Random Random { get; } = new Random(1);
2021-01-16 01:31:39 +01:00
public static string GetTestItem(string path) => Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Resources", path);
2021-04-20 12:55:07 +02:00
2021-01-16 01:31:39 +01:00
public static byte[] GetImage(string name)
{
var photoPath = GetTestItem(name);
return SKImage.FromEncodedData(photoPath).EncodedData.ToArray();
}
public static Location RandomLocation()
{
return new Location
{
Longitude = Helpers.Random.NextDouble() * 360f - 180f,
Latitude = Helpers.Random.NextDouble() * 180f - 90f
};
}
2022-02-21 00:57:53 +01:00
private static readonly ConcurrentDictionary<int, string> RomanNumeralCache = new ConcurrentDictionary<int, string>();
public static string FormatAsRomanNumeral(this int number)
{
if (number < 0 || number > 3999)
2023-01-26 14:31:44 +00:00
throw new ArgumentOutOfRangeException(nameof(number), "Number should be in range from 1 to 3999");
2022-02-21 00:57:53 +01:00
return RomanNumeralCache.GetOrAdd(number, x =>
{
if (x >= 1000) return "M" + FormatAsRomanNumeral(x - 1000);
if (x >= 900) return "CM" + FormatAsRomanNumeral(x - 900);
if (x >= 500) return "D" + FormatAsRomanNumeral(x - 500);
if (x >= 400) return "CD" + FormatAsRomanNumeral(x - 400);
if (x >= 100) return "C" + FormatAsRomanNumeral(x - 100);
if (x >= 90) return "XC" + FormatAsRomanNumeral(x - 90);
if (x >= 50) return "L" + FormatAsRomanNumeral(x - 50);
if (x >= 40) return "XL" + FormatAsRomanNumeral(x - 40);
if (x >= 10) return "X" + FormatAsRomanNumeral(x - 10);
if (x >= 9) return "IX" + FormatAsRomanNumeral(x - 9);
if (x >= 5) return "V" + FormatAsRomanNumeral(x - 5);
if (x >= 4) return "IV" + FormatAsRomanNumeral(x - 4);
if (x >= 1) return "I" + FormatAsRomanNumeral(x - 1);
return string.Empty;
});
}
2021-01-16 01:31:39 +01:00
}
}