diff --git a/QuestPDF.Examples/BarCode.cs b/QuestPDF.Examples/BarCode.cs new file mode 100644 index 0000000..dd83261 --- /dev/null +++ b/QuestPDF.Examples/BarCode.cs @@ -0,0 +1,32 @@ +using System.IO; +using NUnit.Framework; +using QuestPDF.Drawing; +using QuestPDF.Examples.Engine; +using QuestPDF.Fluent; +using QuestPDF.Helpers; +using QuestPDF.Infrastructure; + +namespace QuestPDF.Examples +{ + public class BarCode + { + [Test] + public void Example() + { + FontManager.RegisterFontType("LibreBarcode39", File.OpenRead("LibreBarcode39-Regular.ttf")); + + RenderingTest + .Create() + .PageSize(400, 100) + .FileName() + .Render(container => + { + container + .Background(Colors.White) + .AlignCenter() + .AlignMiddle() + .Text("*QuestPDF*", TextStyle.Default.FontType("LibreBarcode39").Size(64)); + }); + } + } +} \ No newline at end of file diff --git a/QuestPDF.Examples/LibreBarcode39-Regular.ttf b/QuestPDF.Examples/LibreBarcode39-Regular.ttf new file mode 100644 index 0000000..5fcfc55 Binary files /dev/null and b/QuestPDF.Examples/LibreBarcode39-Regular.ttf differ diff --git a/QuestPDF.Examples/QuestPDF.Examples.csproj b/QuestPDF.Examples/QuestPDF.Examples.csproj index 2e9494a..fb5be83 100644 --- a/QuestPDF.Examples/QuestPDF.Examples.csproj +++ b/QuestPDF.Examples/QuestPDF.Examples.csproj @@ -16,4 +16,10 @@ + + + PreserveNewest + + + diff --git a/QuestPDF/Drawing/CanvasCache.cs b/QuestPDF/Drawing/FontManager.cs similarity index 69% rename from QuestPDF/Drawing/CanvasCache.cs rename to QuestPDF/Drawing/FontManager.cs index 9037309..4bdace2 100644 --- a/QuestPDF/Drawing/CanvasCache.cs +++ b/QuestPDF/Drawing/FontManager.cs @@ -1,15 +1,23 @@ -using System.Collections.Concurrent; +using System; +using System.Collections.Concurrent; +using System.IO; using QuestPDF.Drawing.SpacePlan; using QuestPDF.Infrastructure; using SkiaSharp; namespace QuestPDF.Drawing { - internal static class CanvasCache + public static class FontManager { + private static ConcurrentDictionary Typefaces = new ConcurrentDictionary(); private static ConcurrentDictionary Paints = new ConcurrentDictionary(); private static ConcurrentDictionary ColorPaint = new ConcurrentDictionary(); + public static void RegisterFontType(string fontName, Stream stream) + { + Typefaces.TryAdd(fontName, SKTypeface.FromStream(stream)); + } + internal static SKPaint ColorToPaint(this string color) { return ColorPaint.GetOrAdd(color, Convert); @@ -29,12 +37,10 @@ namespace QuestPDF.Drawing static SKPaint Convert(TextStyle style) { - var slant = style.IsItalic ? SKFontStyleSlant.Italic : SKFontStyleSlant.Upright; - return new SKPaint { Color = SKColor.Parse(style.Color), - Typeface = SKTypeface.FromFamilyName(style.FontType, (int)style.FontWeight, (int)SKFontStyleWidth.Normal, slant), + Typeface = GetTypeface(style), TextSize = style.Size, TextEncoding = SKTextEncoding.Utf32, @@ -47,6 +53,17 @@ namespace QuestPDF.Drawing } }; } + + static SKTypeface GetTypeface(TextStyle style) + { + if (Typefaces.TryGetValue(style.FontType, out var result)) + return result; + + var slant = style.IsItalic ? SKFontStyleSlant.Italic : SKFontStyleSlant.Upright; + + return SKTypeface.FromFamilyName(style.FontType, (int)style.FontWeight, (int)SKFontStyleWidth.Normal, slant) + ?? throw new ArgumentException($"The typeface {style.FontType} could not be found."); + } } internal static TextMeasurement BreakText(this TextStyle style, string text, float availableWidth)