123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676 |
- #region MIT License
- #endregion
- using System;
- using System.Collections.Generic;
- using SharpFont.Cache;
- using SharpFont.Internal;
- using SharpFont.TrueType;
- namespace SharpFont
- {
-
-
-
-
-
-
-
-
-
- public sealed class Library : IDisposable
- {
- #region Fields
- private IntPtr reference;
- private bool customMemory;
- private bool disposed;
- private List<Face> childFaces;
- private List<Glyph> childGlyphs;
- private List<Outline> childOutlines;
- private List<Stroker> childStrokers;
- private List<Manager> childManagers;
- #endregion
- #region Constructors
-
-
-
- public Library()
- : this(false)
- {
- IntPtr libraryRef;
- Error err = FT.FT_Init_FreeType(out libraryRef);
- if (err != Error.Ok)
- throw new FreeTypeException(err);
- Reference = libraryRef;
- }
-
-
-
-
- public Library(Memory memory)
- : this(false)
- {
- IntPtr libraryRef;
- Error err = FT.FT_New_Library(memory.Reference, out libraryRef);
- if (err != Error.Ok)
- throw new FreeTypeException(err);
- Reference = libraryRef;
- customMemory = true;
- }
- private Library(bool duplicate)
- {
- childFaces = new List<Face>();
- childGlyphs = new List<Glyph>();
- childOutlines = new List<Outline>();
- childStrokers = new List<Stroker>();
- childManagers = new List<Manager>();
- }
-
-
-
- ~Library()
- {
- Dispose(false);
- }
- #endregion
- #region Properties
-
-
-
- public bool IsDisposed
- {
- get
- {
- return disposed;
- }
- }
-
-
-
- public Version Version
- {
- get
- {
- if (disposed)
- throw new ObjectDisposedException("Version", "Cannot access a disposed object.");
- int major, minor, patch;
- FT.FT_Library_Version(Reference, out major, out minor, out patch);
- return new Version(major, minor, patch);
- }
- }
- internal IntPtr Reference
- {
- get
- {
- if (disposed)
- throw new ObjectDisposedException("Reference", "Cannot access a disposed object.");
- return reference;
- }
- set
- {
- if (disposed)
- throw new ObjectDisposedException("Reference", "Cannot access a disposed object.");
- reference = value;
- }
- }
- #endregion
- #region Methods
- #region Base Interface
-
-
-
-
-
-
-
-
-
- public Face NewFace(string path, int faceIndex)
- {
- if (disposed)
- throw new ObjectDisposedException("Library", "Cannot access a disposed object.");
- return new Face(this, path, faceIndex);
- }
-
-
-
-
-
-
-
-
-
-
-
-
- public Face NewMemoryFace(byte[] file, int faceIndex)
- {
- if (disposed)
- throw new ObjectDisposedException("Library", "Cannot access a disposed object.");
- return new Face(this, file, faceIndex);
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- public Face OpenFace(OpenArgs args, int faceIndex)
- {
- if (disposed)
- throw new ObjectDisposedException("Library", "Cannot access a disposed object.");
- IntPtr faceRef;
- Error err = FT.FT_Open_Face(Reference, args.Reference, faceIndex, out faceRef);
- if (err != Error.Ok)
- throw new FreeTypeException(err);
- return new Face(faceRef, this);
- }
- #endregion
- #region Mac Specific Interface
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- public Face NewFaceFromFond(IntPtr fond, int faceIndex)
- {
- if (disposed)
- throw new ObjectDisposedException("Library", "Cannot access a disposed object.");
- IntPtr faceRef;
- Error err = FT.FT_New_Face_From_FOND(Reference, fond, faceIndex, out faceRef);
- if (err != Error.Ok)
- throw new FreeTypeException(err);
- return new Face(faceRef, this);
- }
-
-
-
-
-
-
-
-
-
-
- public Face NewFaceFromFSSpec(IntPtr spec, int faceIndex)
- {
- if (disposed)
- throw new ObjectDisposedException("Library", "Cannot access a disposed object.");
- IntPtr faceRef;
- Error err = FT.FT_New_Face_From_FSSpec(Reference, spec, faceIndex, out faceRef);
- if (err != Error.Ok)
- throw new FreeTypeException(err);
- return new Face(faceRef, this);
- }
-
-
-
-
-
-
-
-
-
-
- public Face NewFaceFromFSRef(IntPtr @ref, int faceIndex)
- {
- if (disposed)
- throw new ObjectDisposedException("Library", "Cannot access a disposed object.");
- IntPtr faceRef;
- Error err = FT.FT_New_Face_From_FSRef(Reference, @ref, faceIndex, out faceRef);
- if (err != Error.Ok)
- throw new FreeTypeException(err);
- return new Face(faceRef, this);
- }
- #endregion
- #region Module Management
-
-
-
-
-
-
-
-
- public void AddModule(ModuleClass clazz)
- {
- if (disposed)
- throw new ObjectDisposedException("Library", "Cannot access a disposed object.");
- Error err = FT.FT_Add_Module(Reference, clazz.Reference);
- if (err != Error.Ok)
- throw new FreeTypeException(err);
- }
-
-
-
-
-
-
-
-
-
- public Module GetModule(string moduleName)
- {
- if (disposed)
- throw new ObjectDisposedException("Library", "Cannot access a disposed object.");
- return new Module(FT.FT_Get_Module(Reference, moduleName));
- }
-
-
-
-
-
-
-
- public void RemoveModule(Module module)
- {
- if (disposed)
- throw new ObjectDisposedException("Library", "Cannot access a disposed object.");
- if (module == null)
- throw new ArgumentNullException("module");
- Error err = FT.FT_Remove_Module(Reference, module.Reference);
- if (err != Error.Ok)
- throw new FreeTypeException(err);
- }
-
-
-
-
-
-
-
-
-
-
-
-
- [CLSCompliant(false)]
- public void SetDebugHook(uint hookIndex, IntPtr debugHook)
- {
- if (disposed)
- throw new ObjectDisposedException("Library", "Cannot access a disposed object.");
- FT.FT_Set_Debug_Hook(Reference, hookIndex, debugHook);
- }
-
-
-
-
- public void AddDefaultModules()
- {
- if (disposed)
- throw new ObjectDisposedException("Library", "Cannot access a disposed object.");
- FT.FT_Add_Default_Modules(Reference);
- }
-
-
-
-
-
-
-
-
-
-
-
-
- [CLSCompliant(false)]
- public Renderer GetRenderer(GlyphFormat format)
- {
- if (disposed)
- throw new ObjectDisposedException("Library", "Cannot access a disposed object.");
- return new Renderer(FT.FT_Get_Renderer(Reference, format));
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- [CLSCompliant(false)]
- public unsafe void SetRenderer(Renderer renderer, uint numParams, Parameter[] parameters)
- {
- if (disposed)
- throw new ObjectDisposedException("Library", "Cannot access a disposed object.");
- if (renderer == null)
- throw new ArgumentNullException("renderer");
- if (parameters == null)
- throw new ArgumentNullException("parameters");
- ParameterRec[] paramRecs = Array.ConvertAll<Parameter, ParameterRec>(parameters, p => p.Record);
- fixed (void* ptr = paramRecs)
- {
- Error err = FT.FT_Set_Renderer(Reference, renderer.Reference, numParams, (IntPtr)ptr);
- if (err != Error.Ok)
- throw new FreeTypeException(err);
- }
- }
- #endregion
- #region LCD Filtering
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- public void SetLcdFilter(LcdFilter filter)
- {
- if (disposed)
- throw new ObjectDisposedException("Library", "Cannot access a disposed object.");
- Error err = FT.FT_Library_SetLcdFilter(Reference, filter);
- if (err != Error.Ok)
- throw new FreeTypeException(err);
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- public void SetLcdFilterWeights(byte[] weights)
- {
- if (disposed)
- throw new ObjectDisposedException("Library", "Cannot access a disposed object.");
- if (weights == null)
- throw new ArgumentNullException("weights");
- Error err = FT.FT_Library_SetLcdFilterWeights(Reference, weights);
- if (err != Error.Ok)
- throw new FreeTypeException(err);
- }
- #endregion
- #region The TrueType Engine
-
-
-
-
-
- public EngineType GetTrueTypeEngineType()
- {
- if (disposed)
- throw new ObjectDisposedException("Library", "Cannot access a disposed object.");
- return FT.FT_Get_TrueType_Engine_Type(Reference);
- }
- #endregion
-
-
-
- public void Dispose()
- {
- Dispose(true);
- GC.SuppressFinalize(this);
- }
- internal void AddChildFace(Face child)
- {
- childFaces.Add(child);
- }
- internal void RemoveChildFace(Face child)
- {
- childFaces.Remove(child);
- }
- internal void AddChildGlyph(Glyph child)
- {
- childGlyphs.Add(child);
- }
- internal void RemoveChildGlyph(Glyph child)
- {
- childGlyphs.Remove(child);
- }
- internal void AddChildOutline(Outline child)
- {
- childOutlines.Add(child);
- }
- internal void RemoveChildOutline(Outline child)
- {
- childOutlines.Remove(child);
- }
- internal void AddChildStroker(Stroker child)
- {
- childStrokers.Add(child);
- }
- internal void RemoveChildStroker(Stroker child)
- {
- childStrokers.Remove(child);
- }
- internal void AddChildManager(Manager child)
- {
- childManagers.Add(child);
- }
- internal void RemoveChildManager(Manager child)
- {
- childManagers.Remove(child);
- }
- private void Dispose(bool disposing)
- {
- if (!disposed)
- {
- disposed = true;
-
- foreach (Face f in childFaces)
- f.Dispose();
- foreach (Glyph g in childGlyphs)
- g.Dispose();
- foreach (Outline o in childOutlines)
- o.Dispose();
- foreach (Stroker s in childStrokers)
- s.Dispose();
- foreach (Manager m in childManagers)
- m.Dispose();
- childFaces.Clear();
- childGlyphs.Clear();
- childOutlines.Clear();
- childStrokers.Clear();
- childManagers.Clear();
- Error err = customMemory ? FT.FT_Done_Library(reference) : FT.FT_Done_FreeType(reference);
- if (err != Error.Ok)
- throw new FreeTypeException(err);
- reference = IntPtr.Zero;
- }
- }
- #endregion
- }
- }
|