From 6920c9390d4b8d6070b2c595f890abdcc7da2cbe Mon Sep 17 00:00:00 2001 From: Matthew Leibowitz Date: Tue, 3 Oct 2017 22:29:18 +0200 Subject: [PATCH] Make sure we can create a typeface from non-seekable streams --- binding/Binding/SKTypeface.cs | 20 ++++++++++++++++++++ tests/Tests/SKTypefaceTest.cs | 21 +++++++++++++++++++++ 2 files changed, 41 insertions(+) diff --git a/binding/Binding/SKTypeface.cs b/binding/Binding/SKTypeface.cs index 613c8717..830c27ef 100644 --- a/binding/Binding/SKTypeface.cs +++ b/binding/Binding/SKTypeface.cs @@ -62,6 +62,21 @@ namespace SkiaSharp { if (stream == null) throw new ArgumentNullException (nameof (stream)); + + if (!stream.CanSeek) + { + var fontStream = new MemoryStream (); + stream.CopyTo (fontStream); + fontStream.Flush (); + fontStream.Position = 0; + + stream.Dispose (); + stream = null; + + stream = fontStream; + fontStream = null; + } + return FromStream (new SKManagedStream (stream, true), index); } @@ -74,6 +89,11 @@ namespace SkiaSharp return typeface; } + public static SKTypeface FromData (SKData data, int index = 0) + { + return SKTypeface.FromStream (new SKMemoryStream (data), index); + } + public int CountGlyphs (string str) { if (str == null) diff --git a/tests/Tests/SKTypefaceTest.cs b/tests/Tests/SKTypefaceTest.cs index 9fbe64d9..bb39ad52 100644 --- a/tests/Tests/SKTypefaceTest.cs +++ b/tests/Tests/SKTypefaceTest.cs @@ -142,5 +142,26 @@ namespace SkiaSharp.Tests } } + [Test] + public void CanReadData() + { + var bytes = File.ReadAllBytes(Path.Combine(PathToFonts, "Distortable.ttf")); + using (var data = SKData.CreateCopy(bytes)) + using (var typeface = SKTypeface.FromData(data)) + { + Assert.IsNotNull(typeface); + } + } + + [Test] + public void CanReadNonSeekableStream() + { + using (var stream = File.OpenRead(Path.Combine(PathToFonts, "Distortable.ttf"))) + using (var nonSeekable = new NonSeekableReadOnlyStream(stream)) + using (var typeface = SKTypeface.FromStream(nonSeekable)) + { + Assert.IsNotNull(typeface); + } + } } }