Skip to content

Commit

Permalink
Improve natives building process, update FreeType (#82)
Browse files Browse the repository at this point in the history
  • Loading branch information
vddCore committed May 13, 2024
1 parent 2acadcb commit 8ec5f3e
Show file tree
Hide file tree
Showing 16 changed files with 259 additions and 67 deletions.
4 changes: 1 addition & 3 deletions .github/workflows/build-linux-natives.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ jobs:

strategy:
fail-fast: false
matrix:
build_type: [ Release, Debug, RelWithDebInfo ]

steps:
- uses: actions/checkout@v4
Expand All @@ -25,7 +23,7 @@ jobs:
- name: Pack artifact
uses: actions/upload-artifact@v3.1.3
with:
name: chroma-natives-linux-${{ matrix.build_type }}
name: chroma-natives-linux
path: Dependencies/build_env/artifacts
if-no-files-found: error
retention-days: 90
34 changes: 34 additions & 0 deletions .github/workflows/build-macos-natives.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
name: Build macOS natives

on:
push:
branches: [ "master" ]
pull_request:
branches: [ "master" ]
workflow_dispatch:

jobs:
build:
runs-on: macos-latest

strategy:
fail-fast: false

steps:
- uses: actions/checkout@v4
- uses: Symbitic/install-cmake@v0.1.0
env:
ACTIONS_ALLOW_UNSECURE_COMMANDS: 'true'
with:
platform: mac

- name: Build
run: cd Dependencies && ./build_natives --with-ft && cd ..

- name: Pack artifact
uses: actions/upload-artifact@v3.1.3
with:
name: chroma-natives-macos
path: Dependencies/build_env/artifacts
if-no-files-found: error
retention-days: 90
Binary file modified Chroma.Natives/Binaries/windows_64/SDL2.bz2
Binary file not shown.
Binary file modified Chroma.Natives/Binaries/windows_64/SDL2_gpu.bz2
Binary file not shown.
Binary file modified Chroma.Natives/Binaries/windows_64/SDL2_sound.bz2
Binary file not shown.
Binary file modified Chroma.Natives/Binaries/windows_64/freetype.bz2
Binary file not shown.
31 changes: 17 additions & 14 deletions Chroma.Natives/Bindings/FreeType/FT2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -202,23 +202,23 @@ public enum FT_Glyph_BBox_Mode
[StructLayout(LayoutKind.Sequential)]
public struct FT_Generic
{
public IntPtr data;
public IntPtr finalizer;
public nint data;
public nint finalizer;
}

[StructLayout(LayoutKind.Sequential)]
public struct FT_Glyph_Metrics
{
public IntPtr width;
public IntPtr height;
public long width;
public long height;

public IntPtr horiBearingX;
public IntPtr horiBearingY;
public IntPtr horiAdvance;
public long horiAdvance;

public IntPtr vertBearingX;
public IntPtr vertBearingY;
public IntPtr vertAdvance;
public long vertBearingX;
public long vertBearingY;
public long vertAdvance;
}

[StructLayout(LayoutKind.Sequential)]
Expand Down Expand Up @@ -264,7 +264,6 @@ public struct FT_GlyphSlotRec
public FT_Generic generic;

public FT_Glyph_Metrics metrics;

public IntPtr linearHoriAdvance;
public IntPtr linearVertAdvance;
public FT_Vector advance;
Expand All @@ -287,6 +286,7 @@ public struct FT_GlyphSlotRec
public IntPtr rsb_delta;

public IntPtr other;

private IntPtr @internal;
}

Expand Down Expand Up @@ -355,13 +355,13 @@ private struct FT_ListRec
[StructLayout(LayoutKind.Sequential)]
public struct FT_FaceRec
{
public IntPtr num_faces;
public IntPtr face_index;
public long num_faces;
public long face_index;

public IntPtr face_flags;
public IntPtr style_flags;
public long face_flags;
public long style_flags;

public IntPtr num_glyphs;
public long num_glyphs;

public IntPtr family_name;
public IntPtr style_name;
Expand Down Expand Up @@ -394,9 +394,12 @@ public struct FT_FaceRec
private IntPtr driver;
private IntPtr memory;
private IntPtr stream;

private FT_ListRec sizes_list;

private FT_Generic autohint;
private IntPtr extensions;

private IntPtr @internal;
}

Expand Down
10 changes: 10 additions & 0 deletions Chroma/Graphics/TextRendering/TrueType/FreeTypeException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace Chroma.Graphics.TextRendering.TrueType
{
public class FreeTypeException : FrameworkException
{
public FreeTypeException(string message)
: base(message)
{
}
}
}
18 changes: 15 additions & 3 deletions Chroma/Graphics/TextRendering/TrueType/TrueTypeFont.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ public class TrueTypeFont : DisposableResource, IFontProvider
private byte[] _ttfData;
private Texture _atlas;

private FT_Error _lastFtError;

public static TrueTypeFont Default => EmbeddedAssets.DefaultFont;

public string FamilyName { get; private set; }
Expand Down Expand Up @@ -112,7 +114,12 @@ out var patch

static TrueTypeFont()
{
FT_Init_FreeType(out _libraryHandle);
var error = FT_Init_FreeType(out _libraryHandle);

if (error != FT_Error.FT_Err_Ok)
{
throw new FreeTypeException("Failed to initialize FreeType library.");
}
}

public TrueTypeFont(string fileName, int height, string alphabet = null)
Expand Down Expand Up @@ -264,15 +271,20 @@ private void InvalidateFont()

private unsafe void LoadTtf()
{
fixed (byte* fontPtr = &_ttfData[0])
fixed (byte* fontPtr = _ttfData)
{
FT_New_Memory_Face(
_lastFtError = FT_New_Memory_Face(
_libraryHandle,
fontPtr,
_ttfData.Length,
0,
out _face
);

if (_lastFtError != FT_Error.FT_Err_Ok)
{
throw new FreeTypeException($"Failed to load typeface into memory: {_lastFtError}");
}
}

FamilyName = Marshal.PtrToStringAnsi(_face->family_name);
Expand Down
1 change: 1 addition & 0 deletions Dependencies/build_natives
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ fi

pack_artifacts() {
cd $ARTIFACT_DIR
ls -l
if [ "$SKIP_FT" -eq "0" ]; then
mv $FT_ARTIFACT.bz2 libfreetype.bz2
fi
Expand Down
36 changes: 23 additions & 13 deletions Dependencies/build_natives_linux
Original file line number Diff line number Diff line change
Expand Up @@ -15,27 +15,31 @@ SDL_ARTIFACT=libSDL2.so
SDL_GPU_SOPATH=$SDL_GPU_BUILDROOT/SDL_gpu/lib/libSDL2_gpu.so
SDL_GPU_ARTIFACT=libSDL2_gpu.so

SDL_SOUND_SOPATH=$SDL_SOUND_BUILDROOT/libSDL2_sound.so.2.0.3
SDL_SOUND_SOPATH=$SDL_SOUND_BUILDROOT/libSDL2_sound.so.$SDLSOUND_VER
SDL_SOUND_ARTIFACT=libSDL2_sound.so

if [ "$SKIP_FT" -eq "0" ]; then
echo
echo "-- [[ BUILDING FREETYPE ]] --"
$CMAKE $CMAKE_FLAGS -B $FT_BUILDROOT $FT_PATH -G "$CMAKE_GENERATOR" \
-DCMAKE_BUILD_TYPE=$FT_BUILD_TYPE \
-DCMAKE_DISABLE_FIND_PACKAGE_HarfBuzz=1 \
-DCMAKE_DISABLE_FIND_PACKAGE_ZLIB=1 \
-DBUILD_SHARED_LIBS=1 \
-DFT_WITH_ZLIB=0 \
-DFT_WITH_HARFBUZZ=0 \
-DBUILD_SHARED_LIBS=ON \
-DFT_ENABLE_ERROR_STRINGS=ON \
-DFT_DISABLE_ZLIB=ON \
-DFT_DISABLE_BZIP2=ON \
-DFT_DISABLE_BROTLI=ON \
-DFT_DISABLE_PNG=ON \
-DFT_DISABLE_HARFBUZZ=ON \
&& cd $FT_BUILDROOT && $MAKE -j$THREADCOUNT \
&& mv $FT_SOPATH $ARTIFACT_DIR/$FT_ARTIFACT \
\
&& $CMAKE $CMAKE_FLAGS -B $HB_BUILDROOT $HB_PATH -G "$CMAKE_GENERATOR" \
-DCMAKE_BUILD_TYPE=$HB_BUILD_TYPE \
-DCMAKE_CXX_FLAGS=-fPIC \
-DCMAKE_C_FLAGS=-fPIC \
-DHB_BUILD_SUBSET=0 \
-DHB_HAVE_FREETYPE=1 \
-DFREETYPE_FOUND=1 \
-DHB_BUILD_SUBSET=FALSE \
-DHB_HAVE_FREETYPE=TRUE \
-DFREETYPE_FOUND=TRUE \
-DFREETYPE_INCLUDE_DIRS=$FT_PATH/include \
-DFREETYPE_LIBRARY=$ARTIFACT_DIR/$FT_ARTIFACT \
&& cd $HB_BUILDROOT && $MAKE -j$THREADCOUNT \
Expand All @@ -44,16 +48,22 @@ if [ "$SKIP_FT" -eq "0" ]; then
\
&& $CMAKE $CMAKE_FLAGS -B $FT_BUILDROOT $FT_PATH -G "$CMAKE_GENERATOR" \
-DCMAKE_BUILD_TYPE=$FT_BUILD_TYPE \
-DCMAKE_DISABLE_FIND_PACKAGE_ZLIB=1 \
-DBUILD_SHARED_LIBS=1 \
-DFT_WITH_HARFBUZZ=1 \
-DFT_WITH_ZLIB=0 \
-DBUILD_SHARED_LIBS=ON \
-DFT_ENABLE_ERROR_STRINGS=ON \
-DFT_DISABLE_ZLIB=ON \
-DFT_DISABLE_BZIP2=ON \
-DFT_DISABLE_BROTLI=ON \
-DFT_DISABLE_PNG=ON \
-DFT_DISABLE_HARFBUZZ=OFF \
-DFT_REQUIRE_HARFBUZZ=TRUE \
-DHarfBuzz_INCLUDE_DIR=$HB_PATH/src \
-DHarfBuzz_LIBRARY=$ARTIFACT_DIR/$HB_ARTIFACT \
&& cd $FT_BUILDROOT && $MAKE -j$THREADCOUNT \
&& mv $FT_SOPATH $ARTIFACT_DIR/$FT_ARTIFACT
fi

echo
echo "-- [[ BUILDING SDL ]] --"
$CMAKE $CMAKE_FLAGS -B $SDL_BUILDROOT $SDL_PATH -G "$CMAKE_GENERATOR" \
-DCMAKE_BUILD_TYPE=$SDL_BUILD_TYPE \
-DSDL_STATIC=0 \
Expand Down
49 changes: 34 additions & 15 deletions Dependencies/build_natives_mingw
Original file line number Diff line number Diff line change
Expand Up @@ -16,40 +16,59 @@ SDL_GPU_ARTIFACT=SDL2_gpu.dll
SDL_SOUND_SOPATH=$SDL_SOUND_BUILDROOT/SDL2_sound.dll
SDL_SOUND_ARTIFACT=SDL2_sound.dll

if [ "$SKIP_FT" -eq "0" ]; then
$CMAKE $CMAKE_FLAGS -B $FT_BUILDROOT $FT_PATH -G "$CMAKE_GENERATOR" \
if [ "$SKIP_FT" -eq "0" ]; then
echo
echo "-- [[ BUILDING FREETYPE ]] --"
cd $FT_PATH \
&& (patch -p0 --forward < $PATCH_DIR/FreeType_Win64.patch || true) \
&& cd $SCRIPT_DIR \
\
&& $CMAKE $CMAKE_FLAGS -B $FT_BUILDROOT $FT_PATH -G "$CMAKE_GENERATOR" \
-DCMAKE_BUILD_TYPE=$FT_BUILD_TYPE \
-DCMAKE_DISABLE_FIND_PACKAGE_HarfBuzz=1 \
-DCMAKE_DISABLE_FIND_PACKAGE_ZLIB=1 \
-DBUILD_SHARED_LIBS=1 \
-DFT_WITH_ZLIB=0 \
-DFT_WITH_HARFBUZZ=0 \
-DBUILD_SHARED_LIBS=ON \
-DFT_ENABLE_ERROR_STRINGS=ON \
-DFT_DISABLE_ZLIB=ON \
-DFT_DISABLE_BZIP2=ON \
-DFT_DISABLE_BROTLI=ON \
-DFT_DISABLE_PNG=ON \
-DFT_DISABLE_HARFBUZZ=ON \
&& cd $FT_BUILDROOT && $MAKE -j$THREADCOUNT \
&& mv $FT_SOPATH $ARTIFACT_DIR/$FT_ARTIFACT \
&& rm -rf $FT_BUILDROOT \
\
&& cd $HB_PATH \
&& (patch -p0 --forward < $PATCH_DIR/HarfBuzz_MinGW.patch || true) \
&& cd $SCRIPT_DIR \
\
&& $CMAKE $CMAKE_FLAGS -B $HB_BUILDROOT $HB_PATH -G "$CMAKE_GENERATOR" \
-DCMAKE_BUILD_TYPE=$HB_BUILD_TYPE \
-DHB_BUILD_SUBSET=0 \
-DHB_HAVE_FREETYPE=1 \
-DFREETYPE_FOUND=1 \
-DHB_BUILD_SUBSET=FALSE \
-DHB_HAVE_FREETYPE=TRUE \
-DFREETYPE_FOUND=TRUE \
-DFREETYPE_INCLUDE_DIRS=$FT_PATH/include \
-DFREETYPE_LIBRARY=$ARTIFACT_DIR/$FT_ARTIFACT \
&& cd $HB_BUILDROOT && $MAKE -j$THREADCOUNT \
&& mv $HB_ARPATH $ARTIFACT_DIR/$HB_ARTIFACT \
&& rm -rf $FT_BUILDROOT \
\
&& $CMAKE $CMAKE_FLAGS -B $FT_BUILDROOT $FT_PATH -G "$CMAKE_GENERATOR" \
-DCMAKE_BUILD_TYPE=$FT_BUILD_TYPE \
-DCMAKE_DISABLE_FIND_PACKAGE_ZLIB=1 \
-DBUILD_SHARED_LIBS=1 \
-DFT_WITH_HARFBUZZ=1 \
-DFT_WITH_ZLIB=0 \
-DCMAKE_C_FLAGS="-static-libgcc" \
-DBUILD_SHARED_LIBS=ON \
-DFT_ENABLE_ERROR_STRINGS=ON \
-DFT_DISABLE_ZLIB=ON \
-DFT_DISABLE_BZIP2=ON \
-DFT_DISABLE_BROTLI=ON \
-DFT_DISABLE_PNG=ON \
-DFT_DISABLE_HARFBUZZ=OFF \
-DFT_REQUIRE_HARFBUZZ=TRUE \
-DHarfBuzz_INCLUDE_DIR=$HB_PATH/src \
-DHarfBuzz_LIBRARY=$ARTIFACT_DIR/$HB_ARTIFACT \
&& cd $FT_BUILDROOT && $MAKE -j$THREADCOUNT \
&& mv $FT_SOPATH $ARTIFACT_DIR/$FT_ARTIFACT
fi

echo
echo "-- [[ BUILDING SDL ]] --"
$CMAKE $CMAKE_FLAGS -B $SDL_BUILDROOT $SDL_PATH -G "$CMAKE_GENERATOR" \
-DCMAKE_BUILD_TYPE=$SDL_BUILD_TYPE \
-DSDL_STATIC=OFF \
Expand Down
Loading

0 comments on commit 8ec5f3e

Please sign in to comment.