Skip to content

Commit

Permalink
New source files
Browse files Browse the repository at this point in the history
  • Loading branch information
ryancheung committed Apr 13, 2024
1 parent 8f7eb26 commit 48eb33f
Show file tree
Hide file tree
Showing 117 changed files with 1,439 additions and 2,588 deletions.
25 changes: 24 additions & 1 deletion FreeTypeSharp.Generator/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,8 @@ public static int Main(string[] args)
{
foreach (var _func in translationUnit.Functions)
{
if (_func.Name == "FTC_Manager_New") // Skip method has function pointer parameter
continue;
var modifiers = new SyntaxTokenList();
modifiers = modifiers.Add(SyntaxFactory.Token(SyntaxKind.PublicKeyword));
modifiers = modifiers.Add(SyntaxFactory.Token(SyntaxKind.StaticKeyword));
Expand Down Expand Up @@ -281,6 +283,8 @@ private static void InitTypes(ASTContext context)
AddPreprocessedEnumeration(context, "FT_STYLE_FLAG");

typeOverrides.Add("load_flags", predefinedEnumerations["FT_LOAD"]);
typeOverrides.Add("face_flags", predefinedEnumerations["FT_FACE_FLAG"]);
typeOverrides.Add("style_flags", predefinedEnumerations["FT_STYLE_FLAG"]);
typeOverrides.Add("pixel_mode", enumerations["FT_Pixel_Mode_"]);
}

Expand Down Expand Up @@ -313,7 +317,7 @@ private static void AddPreprocessedEnumeration(ASTContext context, string starts
predefinedEnumerations.Add(startsWith, new Enumeration()
{
Name = startsWith,
Type = new BuiltinType() { Type = PrimitiveType.Long },
Type = new BuiltinType() { Type = PrimitiveType.Int },
Items = definations.Select(t => new Enumeration.Item() { Name = t.Key, Expression = t.Value }).ToList()
});
}
Expand Down Expand Up @@ -348,13 +352,32 @@ private static void RegisterType(ASTContext context, string typeName)
_struct = _struct.AddAttributeLists(SyntaxFactory.AttributeList(new SeparatedSyntaxList<AttributeSyntax>().Add(attribute)));
foreach (var field in _class.Fields)
{
switch (typeName)
{
// Skip structs having unsupported fields, like union and function pointers
case "FT_COLR_Paint_":
case "FT_Generic_":
case "FT_MemoryRec_":
case "FT_Outline_Funcs_":
case "FT_Raster_Params_":
case "FT_StreamRec_":
continue;
default:
break;
}

TypeSyntax typeSyntax;
if (typeOverrides.ContainsKey(field.Name))
{
typeSyntax = GetTypeSyntax(context, new TagType()
{
Declaration = typeOverrides[field.Name]
});
// These fields types should not be override
if (field.Name == "face_flags" || field.Name == "style_flags")
{
typeSyntax = GetTypeSyntax(context, field.Type);
}
}
else
typeSyntax = GetTypeSyntax(context, field.Type);
Expand Down
153 changes: 153 additions & 0 deletions FreeTypeSharp/DllMap.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
using System;
using System.IO;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Runtime.CompilerServices;

namespace FreeTypeSharp
{
public static partial class FT
{
#if __IOS__
public const string LibName = "__Internal";
#else
public const string LibName = "freetype";
#endif

#if NETCOREAPP3_1_OR_GREATER && !__IOS__
static FT()
{
NativeLibrary.SetDllImportResolver(typeof(FT).Assembly, ImportResolver);
}

private static IntPtr ImportResolver(string libraryName, Assembly assembly, DllImportSearchPath? searchPath)
{
if (libraryName != LibName) return default;

IntPtr handle = default;
bool success = false;

bool isWindows = false, isMacOS = false, isLinux = false, isAndroid = false;
#if NETCOREAPP3_1
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
isWindows = true;
else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
isMacOS = true;
else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
isLinux = true;
#else
if (OperatingSystem.IsWindows())
isWindows = true;
else if (OperatingSystem.IsMacOS())
isMacOS = true;
else if (OperatingSystem.IsLinux())
isLinux = true;
else if (OperatingSystem.IsAndroid())
isAndroid = true;
#endif

string ActualLibraryName;
if (isWindows)
ActualLibraryName = "freetype.dll";
else if (isMacOS)
ActualLibraryName = "libfreetype.dylib";
else if (isLinux)
ActualLibraryName = "libfreetype.so";
else if (isAndroid)
ActualLibraryName = "libfreetype.so";
else
throw new PlatformNotSupportedException();

string rootDirectory = AppContext.BaseDirectory;

if (isWindows)
{
string arch = Environment.Is64BitProcess ? "win-x64" : "win-x86";
var searchPaths = new[]
{
// This is where native libraries in our nupkg should end up
Path.Combine(rootDirectory, "runtimes", arch, "native", ActualLibraryName),
Path.Combine(rootDirectory, Environment.Is64BitProcess ? "x64" : "x86", ActualLibraryName),
Path.Combine(rootDirectory, ActualLibraryName)
};

foreach (var path in searchPaths)
{
success = NativeLibrary.TryLoad(path, out handle);

if (success)
return handle;
}

// Fallback to system installed freetype
success = NativeLibrary.TryLoad(libraryName, typeof(FT).Assembly,
DllImportSearchPath.ApplicationDirectory | DllImportSearchPath.UserDirectories | DllImportSearchPath.UseDllDirectoryForDependencies,
out handle);

if (success)
return handle;

throw new FileLoadException("Failed to load native freetype library!");
}

if (isLinux || isMacOS)
{
string arch = isMacOS ? "osx" : "linux-" + (Environment.Is64BitProcess ? "x64" : "x86");

var searchPaths = new[]
{
// This is where native libraries in our nupkg should end up
Path.Combine(rootDirectory, "runtimes", arch, "native", ActualLibraryName),
// The build output folder
Path.Combine(rootDirectory, ActualLibraryName),
Path.Combine("/usr/local/lib", ActualLibraryName),
Path.Combine("/usr/lib", ActualLibraryName)
};

foreach (var path in searchPaths)
{
success = NativeLibrary.TryLoad(path, out handle);

if (success)
return handle;
}

// Fallback to system installed freetype
success = NativeLibrary.TryLoad(libraryName, typeof(FT).Assembly,
DllImportSearchPath.ApplicationDirectory | DllImportSearchPath.UserDirectories | DllImportSearchPath.UseDllDirectoryForDependencies,
out handle);

if (success)
return handle;

throw new FileLoadException("Failed to load native freetype library!");
}

if (isAndroid)
{
success = NativeLibrary.TryLoad(ActualLibraryName, typeof(FT).Assembly,
DllImportSearchPath.ApplicationDirectory | DllImportSearchPath.UserDirectories | DllImportSearchPath.UseDllDirectoryForDependencies,
out handle);

if (!success)
success = NativeLibrary.TryLoad(ActualLibraryName, out handle);

if (success)
return handle;

// Fallback to system installed freetype
success = NativeLibrary.TryLoad(libraryName, typeof(FT).Assembly,
DllImportSearchPath.ApplicationDirectory | DllImportSearchPath.UserDirectories | DllImportSearchPath.UseDllDirectoryForDependencies,
out handle);

if (success)
return handle;

throw new FileLoadException("Failed to load native freetype library!");
}

return handle;
}
#endif
}
}
18 changes: 0 additions & 18 deletions FreeTypeSharp/FreeTypeBlendMode.cs

This file was deleted.

31 changes: 0 additions & 31 deletions FreeTypeSharp/FreeTypeCalc.cs

This file was deleted.

1 change: 0 additions & 1 deletion FreeTypeSharp/FreeTypeException.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using FreeTypeSharp.Native;

namespace FreeTypeSharp
{
Expand Down
Loading

0 comments on commit 48eb33f

Please sign in to comment.