Make sure we can create a typeface from non-seekable streams

This commit is contained in:
Matthew Leibowitz 2017-10-03 22:29:18 +02:00
Родитель cae3758edd
Коммит 6920c9390d
2 изменённых файлов: 41 добавлений и 0 удалений

Просмотреть файл

@ -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)

Просмотреть файл

@ -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);
}
}
}
}