Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added streaming support to Face #112

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions Source/SharpFont/FTStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,14 @@ namespace SharpFont
/// <returns>The number of bytes effectively read by the stream.</returns>
[CLSCompliant(false)]
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
public delegate uint StreamIOFunc(NativeReference<FTStream> stream, uint offset, IntPtr buffer, uint count);
public delegate uint StreamIOFunc(IntPtr stream, uint offset, IntPtr buffer, uint count);

/// <summary>
/// A function used to close a given input stream.
/// </summary>
/// <param name="stream">A handle to the target stream.</param>
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
public delegate void StreamCloseFunc(NativeReference<FTStream> stream);
public delegate void StreamCloseFunc(IntPtr stream);

/// <summary>
/// A handle to an input stream.
Expand Down Expand Up @@ -84,7 +84,7 @@ public IntPtr Base
return rec.@base;
}
}

/// <summary>
/// Gets the stream size in bytes.
/// </summary>
Expand Down
93 changes: 92 additions & 1 deletion Source/SharpFont/Face.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE

using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.InteropServices;

using SharpFont.Bdf;
Expand Down Expand Up @@ -53,6 +54,12 @@ public sealed class Face : NativeObject, IDisposable
private Library parentLibrary;
private List<FTSize> childSizes;

GCHandle customStreamHandle;
IntPtr customStreamPtr;
private IntPtr openArgsPtr;
private StreamIOFunc streamIOFunc;
private StreamCloseFunc streamCloseFunc;

#endregion

#region Constructors
Expand All @@ -67,6 +74,8 @@ public Face(Library library, string path)
{
}



/// <summary>
/// Initializes a new instance of the <see cref="Face"/> class.
/// </summary>
Expand All @@ -85,7 +94,85 @@ public Face(Library library, string path, int faceIndex)
Reference = reference;
}

//TODO make an overload with a FileStream instead of a byte[]

/// <summary>
/// Initializes a new instance of the <see cref="Face"/> class.
/// </summary>
/// <param name="library">The parent library.</param>
/// <param name="stream">The stream of the font file.</param>
/// <param name="faceIndex">The index of the face to take from the file.</param>
/// <param name="takeStreamOwnership">The stream is automatically disposed with the face.</param>
public Face(Library library, Stream stream, int faceIndex, bool takeStreamOwnership)
: this(library, new StreamAccessor(stream, takeStreamOwnership), faceIndex)
{
}

/// <summary>
/// Initializes a new instance of the <see cref="Face"/> class.
/// </summary>
/// <param name="library">The parent library.</param>
/// <param name="streamAccessor">Custom object for handling stream access to the font file.</param>
/// <param name="faceIndex">The index of the face to take from the file.</param>
public Face(Library library, ICustomStreamAccessor streamAccessor, int faceIndex)
: this(library)
{
IntPtr reference;

streamIOFunc = new StreamIOFunc(StreamIOFunc);
streamCloseFunc = new StreamCloseFunc(StreamCloseFunc);

customStreamHandle = GCHandle.Alloc(streamAccessor);

StreamRec streamRec = new StreamRec();
streamRec.size = (UIntPtr)streamAccessor.Length;
streamRec.descriptor.pointer = GCHandle.ToIntPtr(customStreamHandle);
streamRec.read = streamIOFunc;
streamRec.close = streamCloseFunc;

customStreamPtr = Marshal.AllocHGlobal(Marshal.SizeOf(streamRec));
Marshal.StructureToPtr(streamRec, customStreamPtr, false);


OpenArgsRec openArgs = new OpenArgsRec();
openArgs.flags = OpenFlags.Stream;
openArgs.stream = customStreamPtr;

openArgsPtr = Marshal.AllocHGlobal(Marshal.SizeOf(openArgs));
Marshal.StructureToPtr(openArgs, openArgsPtr, false);


Error err = FT.FT_Open_Face(library.Reference, openArgsPtr, faceIndex, out reference);
if (err != Error.Ok)
throw new FreeTypeException(err);

Reference = reference;
}

private uint StreamIOFunc(IntPtr streamPtr, uint offset, IntPtr buffer, uint count)
{
FTStream ftStream = new FTStream(streamPtr);
ICustomStreamAccessor streamAccessor = ((GCHandle)ftStream.Descriptor.Pointer).Target as ICustomStreamAccessor;
if (streamAccessor != null)
{
streamAccessor.Seek((int)offset);
if (count > 0)
{
byte[] readbuffer = new byte[count];
int bytesread = streamAccessor.Read(readbuffer);
Marshal.Copy(readbuffer, 0, buffer, (int)count);
return (uint)bytesread;
}
}
return 0;
}

private void StreamCloseFunc(IntPtr streamPtr)
{
FTStream ftStream = new FTStream(streamPtr);
ICustomStreamAccessor streamAccessor = ((GCHandle)ftStream.Descriptor.Pointer).Target as ICustomStreamAccessor;
if (streamAccessor != null) streamAccessor.Close();
}


/// <summary>
/// Initializes a new instance of the <see cref="Face"/> class from a file that's already loaded into memory.
Expand Down Expand Up @@ -2401,6 +2488,10 @@ private void Dispose(bool disposing)
if (memoryFaceHandle.IsAllocated)
memoryFaceHandle.Free();

if (openArgsPtr != IntPtr.Zero) Marshal.FreeHGlobal(openArgsPtr);
if (customStreamPtr != IntPtr.Zero) Marshal.FreeHGlobal(customStreamPtr);
if (customStreamHandle.IsAllocated) customStreamHandle.Free();

EventHandler handler = Disposed;
if (handler != null)
handler(this, EventArgs.Empty);
Expand Down
56 changes: 56 additions & 0 deletions Source/SharpFont/ICustomStreamAccessor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#region MIT License
/*Copyright (c) 2015-2016 Robert Rouhani <[email protected]>

SharpFont based on Tao.FreeType, Copyright (c) 2003-2007 Tao Framework Team

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.*/
#endregion

namespace SharpFont
{
/// <summary>
/// Interface for providing access to a custom stream representing a font file.
/// </summary>
public interface ICustomStreamAccessor
{
/// <summary>
/// Seeks to a specified position within a stream.
/// </summary>
/// <param name="position">Position from the beginning of the stream to seek to.</param>
/// <returns>The new position within the stream.</returns>
int Seek(int position);

/// <summary>
/// Reads bytes from the stream. The maximum number of bytes to read is specified by the buffer length.
/// </summary>
/// <param name="buffer">Buffer to receive read bytes.</param>
/// <returns>The actual number of bytes read from the stream.</returns>
int Read(byte[] buffer);

/// <summary>
/// Closes the stream. Called by freetype once the font face is destroyed.
/// </summary>
void Close();

/// <summary>
/// Returns total length of the stream in bytes.
/// </summary>
int Length { get; }
}
}
2 changes: 2 additions & 0 deletions Source/SharpFont/SharpFont.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@
<Compile Include="GlyphMetrics.cs" />
<Compile Include="GlyphSlot.cs" />
<Compile Include="GlyphToScriptMapProperty.cs" />
<Compile Include="ICustomStreamAccessor.cs" />
<Compile Include="IncreaseXHeightProperty.cs" />
<Compile Include="Internal\BitmapGlyphRec.cs" />
<Compile Include="Internal\BitmapRec.cs" />
Expand Down Expand Up @@ -205,6 +206,7 @@
<Compile Include="SizeRequest.cs" />
<Compile Include="SizeRequestType.cs" />
<Compile Include="Span.cs" />
<Compile Include="StreamAccessor.cs" />
<Compile Include="StreamDesc.cs" />
<Compile Include="Stroker.cs" />
<Compile Include="StrokerBorder.cs" />
Expand Down
76 changes: 76 additions & 0 deletions Source/SharpFont/StreamAccessor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
#region MIT License
/*Copyright (c) 2015-2016 Robert Rouhani <[email protected]>

SharpFont based on Tao.FreeType, Copyright (c) 2003-2007 Tao Framework Team

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.*/
#endregion

using System;
using System.IO;

namespace SharpFont
{
/// <summary>
/// Custom font stream accessor based on a generic Stream object.
/// </summary>
internal class StreamAccessor : ICustomStreamAccessor
{
private Stream _stream;
private bool _takeStreamOwnership;

public StreamAccessor(Stream stream, bool takeStreamOwnership)
{
if (stream == null)
throw new ArgumentException("Stream cannot be null", "stream");

if (!stream.CanRead || !stream.CanSeek)
throw new ArgumentException("Stream must support reading and seeking", "stream");

_stream = stream;
_takeStreamOwnership = takeStreamOwnership;
}

public void Close()
{
if (_takeStreamOwnership)
{
_stream.Dispose();
}
}

public int Read(byte[] buffer)
{
return _stream.Read(buffer, 0, buffer.Length);
}

public int Seek(int position)
{
return (int)_stream.Seek(position, SeekOrigin.Begin);
}

public int Length
{
get
{
return (int)_stream.Length;
}
}
}
}