Skip to content

Commit

Permalink
[STB] Added STBTrueType; [Core] Update error message of RuntimeHelper…
Browse files Browse the repository at this point in the history
….load
  • Loading branch information
squid233 committed Jan 29, 2024
1 parent 2efa449 commit 4a7637c
Show file tree
Hide file tree
Showing 12 changed files with 2,096 additions and 111 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* MIT License
*
* Copyright (c) 2023 Overrun Organization
* Copyright (c) 2023-2024 Overrun Organization
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,9 @@
import overrungl.OverrunGL;
import overrungl.util.MemoryUtil;

import java.io.File;
import java.io.IOException;
import java.lang.foreign.*;
import java.lang.invoke.MethodHandle;
import java.net.URI;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Map;
Expand All @@ -50,7 +49,7 @@ public final class RuntimeHelper {
* The native linker.
*/
public static final Linker LINKER = Linker.nativeLinker();
private static final File tmpdir = new File(System.getProperty("java.io.tmpdir"));
private static final Path tmpdir = Path.of(System.getProperty("java.io.tmpdir"));
/**
* An unbounded address layout.
*/
Expand Down Expand Up @@ -143,41 +142,44 @@ public static SymbolLookup load(String module, String basename, String version)
final Platform os = Platform.current();
final var suffix = os.sharedLibrarySuffix();
final var path = os.sharedLibraryName(basename);
URI uri;
Path uri;
// 1. Load from natives directory
var localFile = new File(System.getProperty("overrungl.natives", "."), path);
if (localFile.exists()) {
uri = localFile.toURI();
var localFile = Path.of(System.getProperty("overrungl.natives", "."), path);
if (Files.exists(localFile)) {
uri = localFile;
} else {
// 2. Load from classpath
var file = new File(tmpdir, STR."overrungl\{System.getProperty("user.name")}");
if (!file.exists()) {
// Create directory
file.mkdir();
} else if (file.isFile()) {
// Remove
file.delete();
// Create directory
file.mkdir();
var file = tmpdir.resolve(STR."overrungl\{System.getProperty("user.name")}");
try {
if (!Files.exists(file)) {
// Create directory
Files.createDirectories(file);
} else if (!Files.isDirectory(file)) {
// Remove
Files.delete(file);
// Create directory
Files.createDirectories(file);
}
} catch (IOException e) {
throw new IllegalStateException(STR."Couldn't create directory: \{file}; try setting -Doverrungl.natives to a valid path", e);
}
var libFile = new File(file, STR."\{basename}-\{version}\{suffix}");
if (!libFile.exists()) {
var libFile = file.resolve(STR."\{basename}-\{version}\{suffix}");
if (!Files.exists(libFile)) {
// Extract
final String fromPath = STR."\{module}/\{os.familyName()}/\{Architecture.current()}/\{path}";
try (var is = STACK_WALKER.getCallerClass().getClassLoader().getResourceAsStream(fromPath)) {
Files.copy(Objects.requireNonNull(is, STR."File not found: \{fromPath}"),
Path.of(libFile.getAbsolutePath()));
Files.copy(Objects.requireNonNull(is, STR."File not found in classpath: \{fromPath}"), libFile);
} catch (Exception e) {
throw new IllegalStateException(STR."File not found: \{file}; try setting property -Doverrungl.natives to a valid path", e);
throw new IllegalStateException(STR."Couldn't load file: \{libFile} or \{localFile}; try setting -Doverrungl.natives to a valid path", e);
}
}
uri = libFile.toURI();
uri = libFile;
}
if (Configurations.DEBUG.get()) {
OverrunGL.apiLog(STR."[OverrunGL] Loading native library from: \{uri}");
}
// Load the library by the path with the global arena
return SymbolLookup.libraryLookup(Path.of(uri), Arena.global());
return SymbolLookup.libraryLookup(uri, Arena.global());
}

/**
Expand Down
21 changes: 0 additions & 21 deletions modules/overrungl.stb/src/main/java/overrungl/stb/Handles.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,10 @@
package overrungl.stb;

import overrungl.Configurations;
import overrungl.FunctionDescriptors;
import overrungl.OverrunGL;
import overrungl.internal.RuntimeHelper;

import java.lang.foreign.FunctionDescriptor;
import java.lang.foreign.Linker;
import java.lang.foreign.SymbolLookup;
import java.lang.invoke.MethodHandle;
import java.util.function.Supplier;

/**
Expand All @@ -45,21 +41,4 @@ final class Handles {
private Handles() {
//no instance
}

static MethodHandle downcall(String name,
FunctionDescriptors function,
Linker.Option... options) {
return RuntimeHelper.downcallThrow(lookup.find(name), function, options);
}

static MethodHandle downcall(String name,
FunctionDescriptor function,
Linker.Option... options) {
return RuntimeHelper.downcallThrow(lookup.find(name), function, options);
}

static MethodHandle downcallTrivial(String name,
FunctionDescriptors function) {
return downcall(name, function, Linker.Option.critical(false));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,37 +54,19 @@ public final class STBTTAlignedQuad extends Struct {
JAVA_FLOAT.withName("t1")
);
/**
* x0
* top-left
*/
public final StructHandle.Float x0 = StructHandle.ofFloat(this, "x0");
public final StructHandle.Float x0 = StructHandle.ofFloat(this, "x0"),
y0 = StructHandle.ofFloat(this, "y0"),
s0 = StructHandle.ofFloat(this, "s0"),
t0 = StructHandle.ofFloat(this, "t0");
/**
* y0
* bottom-right
*/
public final StructHandle.Float y0 = StructHandle.ofFloat(this, "y0");
/**
* s0
*/
public final StructHandle.Float s0 = StructHandle.ofFloat(this, "s0");
/**
* t0
*/
public final StructHandle.Float t0 = StructHandle.ofFloat(this, "t0");
/**
* x1
*/
public final StructHandle.Float x1 = StructHandle.ofFloat(this, "x1");
/**
* y1
*/
public final StructHandle.Float y1 = StructHandle.ofFloat(this, "y1");
/**
* s1
*/
public final StructHandle.Float s1 = StructHandle.ofFloat(this, "s1");
/**
* t1
*/
public final StructHandle.Float t1 = StructHandle.ofFloat(this, "t1");
public final StructHandle.Float x1 = StructHandle.ofFloat(this, "x1"),
y1 = StructHandle.ofFloat(this, "y1"),
s1 = StructHandle.ofFloat(this, "s1"),
t1 = StructHandle.ofFloat(this, "t1");

/**
* Creates a struct with the given layout.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,21 +53,12 @@ public final class STBTTBakedChar extends Struct {
JAVA_FLOAT.withName("xadvance")
);
/**
* x0
* coordinates of bbox in bitmap
*/
public final StructHandle.Short x0 = StructHandle.ofShort(this, "x0");
/**
* y0
*/
public final StructHandle.Short y0 = StructHandle.ofShort(this, "y0");
/**
* x1
*/
public final StructHandle.Short x1 = StructHandle.ofShort(this, "x1");
/**
* y1
*/
public final StructHandle.Short y1 = StructHandle.ofShort(this, "y1");
public final StructHandle.Short x0 = StructHandle.ofShort(this, "x0"),
y0 = StructHandle.ofShort(this, "y0"),
x1 = StructHandle.ofShort(this, "x1"),
y1 = StructHandle.ofShort(this, "y1");
/**
* xoff
*/
Expand Down
38 changes: 38 additions & 0 deletions modules/overrungl.stb/src/main/java/overrungl/stb/STBTTBuf.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* MIT License
*
* Copyright (c) 2024 Overrun Organization
*
* 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.
*/

package overrungl.stb;

import java.lang.foreign.MemoryLayout;
import java.lang.foreign.StructLayout;
import java.lang.foreign.ValueLayout;

/**
* private structure
*
* @author squid233
* @since 0.1.0
*/
public final class STBTTBuf {
/**
* The layout.
*/
public static final StructLayout LAYOUT = MemoryLayout.structLayout(
ValueLayout.ADDRESS.withName("data"),
ValueLayout.JAVA_INT.withName("cursor"),
ValueLayout.JAVA_INT.withName("size")
);
}
100 changes: 100 additions & 0 deletions modules/overrungl.stb/src/main/java/overrungl/stb/STBTTFontInfo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
/*
* MIT License
*
* Copyright (c) 2024 Overrun Organization
*
* 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.
*/

package overrungl.stb;

import overrun.marshal.struct.Struct;

import java.lang.foreign.MemoryLayout;
import java.lang.foreign.MemorySegment;
import java.lang.foreign.SegmentAllocator;
import java.lang.foreign.StructLayout;

import static java.lang.foreign.ValueLayout.ADDRESS;
import static java.lang.foreign.ValueLayout.JAVA_INT;

/**
* The following structure is defined publicly so you can declare one on
* the stack or as a global or etc, but you should treat it as opaque.
*
* @author squid233
* @since 0.1.0
*/
public final class STBTTFontInfo extends Struct {
/**
* The layout.
*/
public static final StructLayout LAYOUT = MemoryLayout.structLayout(
ADDRESS.withName("userdata"),
ADDRESS.withName("data"),
JAVA_INT.withName("fontstart"),
JAVA_INT.withName("numGlyphs"),
JAVA_INT.withName("loca"),
JAVA_INT.withName("head"),
JAVA_INT.withName("glyf"),
JAVA_INT.withName("hhea"),
JAVA_INT.withName("hmtx"),
JAVA_INT.withName("kern"),
JAVA_INT.withName("gpos"),
JAVA_INT.withName("svg"),
JAVA_INT.withName("index_map"),
JAVA_INT.withName("indexToLocFormat"),
STBTTBuf.LAYOUT.withName("cff"),
STBTTBuf.LAYOUT.withName("charstrings"),
STBTTBuf.LAYOUT.withName("gsubrs"),
STBTTBuf.LAYOUT.withName("subrs"),
STBTTBuf.LAYOUT.withName("fontdicts"),
STBTTBuf.LAYOUT.withName("fdselect")
);

/**
* Creates a struct with the given layout.
*
* @param segment the segment
* @param elementCount the element count
*/
public STBTTFontInfo(MemorySegment segment, long elementCount) {
super(segment, elementCount, LAYOUT);
}

/**
* Allocates a struct with the given layout.
*
* @param allocator the allocator
* @param elementCount the element count
*/
public STBTTFontInfo(SegmentAllocator allocator, long elementCount) {
super(allocator, elementCount, LAYOUT);
}

/**
* Creates a struct with the given layout.
*
* @param segment the segment
*/
public STBTTFontInfo(MemorySegment segment) {
super(segment, LAYOUT);
}

/**
* Allocates a struct with the given layout.
*
* @param allocator the allocator
*/
public STBTTFontInfo(SegmentAllocator allocator) {
super(allocator, LAYOUT);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public final class STBTTKerningEntry extends Struct {
ValueLayout.JAVA_INT.withName("advance")
);
/**
* glyph1
* use stbtt_FindGlyphIndex
*/
public final StructHandle.Int glyph1 = StructHandle.ofInt(this, "glyph1");
/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,30 +66,27 @@ public final class STBTTPackRange extends Struct {
*/
public final StructHandle.Float fontSize = StructHandle.ofFloat(this, "font_size");
/**
* first_unicode_codepoint_in_range
* if non-zero, then the chars are continuous, and this is the first codepoint
*/
public final StructHandle.Int firstUnicodeCodepointInRange = StructHandle.ofInt(this, "first_unicode_codepoint_in_range");
/**
* array_of_unicode_codepoints
* if non-zero, then this is an array of unicode codepoints
*/
public final StructHandle.Array<int[]> arrayOfUnicodeCodepoints = StructHandle.ofArray(this, "array_of_unicode_codepoints", Marshal::marshal, Unmarshal::unmarshalAsIntArray);
/**
* num_chars
*/
public final StructHandle.Int numChars = StructHandle.ofInt(this, "num_chars");
/**
* chardata_for_range
* output
*/
public final StructHandle.Array<STBTTPackedChar[]> chardataForRange = StructHandle.ofArray(this, "chardata_for_range", Marshal::marshal,
segment -> Unmarshal.unmarshal(ADDRESS, segment, STBTTPackedChar[]::new, s -> new STBTTPackedChar(s.get(ADDRESS.withTargetLayout(STBTTPackedChar.LAYOUT), 0L), 1L)));
/**
* h_oversample
* don't set these, they're used internally
*/
public final StructHandleView.Byte hOversample = StructHandle.ofByte(this, "h_oversample");
/**
* v_oversample
*/
public final StructHandleView.Byte vOversample = StructHandle.ofByte(this, "v_oversample");
public final StructHandleView.Byte hOversample = StructHandle.ofByte(this, "h_oversample"),
vOversample = StructHandle.ofByte(this, "v_oversample");

/**
* Creates a struct with the given layout.
Expand Down
Loading

0 comments on commit 4a7637c

Please sign in to comment.