Skip to content

Commit

Permalink
More General API
Browse files Browse the repository at this point in the history
  • Loading branch information
GabrielBRDeveloper committed Jul 18, 2024
1 parent b2504a7 commit efaf733
Show file tree
Hide file tree
Showing 12 changed files with 178 additions and 122 deletions.
2 changes: 2 additions & 0 deletions src/br/nullexcept/mux/C.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package br.nullexcept.mux;

import br.nullexcept.mux.graphics.BitmapFactory;
import br.nullexcept.mux.graphics.fonts.TypefaceFactory;
import br.nullexcept.mux.utils.Log;

import java.util.ArrayList;
Expand All @@ -11,6 +12,7 @@ public class C {
public static long VG_CONTEXT = -1;
public static long GLFW_CONTEXT = 0;
public static BitmapFactory BITMAP_FACTORY;
public static TypefaceFactory TYPEFACE_FACTORY;

public static class Config {
public static boolean SET_WINDOW_GL_HINT = true;
Expand Down
84 changes: 84 additions & 0 deletions src/br/nullexcept/mux/core/texel/TexelFont.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package br.nullexcept.mux.core.texel;

import br.nullexcept.mux.C;
import br.nullexcept.mux.graphics.Paint;
import br.nullexcept.mux.graphics.fonts.FontMetrics;
import br.nullexcept.mux.graphics.fonts.Typeface;
import br.nullexcept.mux.utils.Log;
import org.lwjgl.nanovg.NVGGlyphPosition;
import org.lwjgl.nanovg.NanoVG;

import java.nio.ByteBuffer;
import java.util.UUID;

class TexelFont extends Typeface {
protected static final float SCALE = 1024.0f;

protected final float ascent;
protected final float descent;
protected final float lineHeight;
protected final ByteBuffer buffer;

private final int font;
private final String id = UUID.randomUUID().toString();
private final short[] bounds = new short[1024*16]; //64KB of buffer for store characters bounds

TexelFont(ByteBuffer buffer){
this.buffer = buffer;
long context = C.VG_CONTEXT;
font = NanoVG.nvgCreateFontMem(context, id,buffer,false);
NanoVG.nvgFontFaceId(context, font);
NanoVG.nvgTextLetterSpacing(context, 0.0f);
NanoVG.nvgFontSize(context, SCALE);
NanoVG.nvgFontBlur(context,0);
float[] ascent = new float[1];
float[] descent = new float[1];
float[] lineHeight = new float[1];
NanoVG.nvgTextMetrics(context, ascent,descent,lineHeight);
this.ascent = ascent[0];
this.descent = descent[0];
this.lineHeight = lineHeight[0];
NVGGlyphPosition.create();
NVGGlyphPosition.Buffer b = NVGGlyphPosition.create(1);
for (int i = 0; i < this.bounds.length; i++){
String character = String.valueOf((char)i);
NanoVG.nvgTextGlyphPositions(context,0,0,character,b);
this.bounds[i] = (short) ((short) Math.abs(b.maxx()) + Math.abs(b.minx()));
}
}

protected int measureChar(char ch){
if (ch > bounds.length){
Log.log("Typeface","INVALID MEASURE OUTBOUNDS CHAR: "+(int)ch);
ch = 'Z';
}
switch (ch){
case '\t':
case '\f':
return bounds[' '];
case '\n':
case '\r':
return 0;
}
return bounds[ch];
}

public int hashCode(){
return font;
}

@Override
public String toString() {
return id;
}

@Override
protected void finalize() throws Throwable {
super.finalize();
}

@Override
public FontMetrics getMetricsFor(Paint paint) {
return new TexelFontMetrics(paint);
}
}
20 changes: 20 additions & 0 deletions src/br/nullexcept/mux/core/texel/TexelFontFactory.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package br.nullexcept.mux.core.texel;

import br.nullexcept.mux.graphics.fonts.Typeface;
import br.nullexcept.mux.graphics.fonts.TypefaceFactory;
import br.nullexcept.mux.res.AssetsManager;
import br.nullexcept.mux.utils.BufferUtils;

import java.io.InputStream;

public class TexelFontFactory extends TypefaceFactory {

public TexelFontFactory() {
Typeface.DEFAULT = decodeTypeface(TexelFontFactory.class.getResourceAsStream("/res/fonts/Roboto/Roboto-Regular.ttf"));
}

@Override
protected Typeface decodeTypeface(InputStream input) {
return new TexelFont(BufferUtils.allocateStream(input));
}
}
44 changes: 44 additions & 0 deletions src/br/nullexcept/mux/core/texel/TexelFontMetrics.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package br.nullexcept.mux.core.texel;

import br.nullexcept.mux.graphics.Paint;
import br.nullexcept.mux.graphics.fonts.FontMetrics;

public class TexelFontMetrics extends FontMetrics {
private final Paint paint;

protected TexelFontMetrics(Paint paint){
this.paint = paint;
}

private TexelFont typeface() {
return ((TexelFont)paint.getTypeface());
}

public float getAscent(){
return typeface().ascent * scale();
}
public float getDescent(){
return typeface().descent * scale();
}

public float getLineHeight() {
return typeface().lineHeight * scale();
}

public float measureChar(char ch){
return typeface().measureChar(ch) * scale();
}

private float scale(){
return (paint.getTextSize()/ TexelFont.SCALE);
}
public float measureText(CharSequence line){
float width = 0;
TexelFont typeface = typeface();
float scale = scale();
for (int i = 0; i < line.length(); i++){
width += typeface.measureChar(line.charAt(i)) * scale;
}
return width;
}
}
1 change: 1 addition & 0 deletions src/br/nullexcept/mux/core/texel/VgTexel.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public static void initialize(){
clipContext = NanoVGGLES2.nvgCreate(NanoVGGLES2.NVG_ANTIALIAS);
globalPaint.setTextSize(-1f);
C.VG_CONTEXT = globalContext;
C.TYPEFACE_FACTORY = new TexelFontFactory();
C.BITMAP_FACTORY = new TexelBitmapFactory();
GLShaderList.build();

Expand Down
11 changes: 3 additions & 8 deletions src/br/nullexcept/mux/graphics/Paint.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public class Paint {
private int color = Color.WHITE;
private float textSize = 18.0f;
private float strokeWidth = 0.0f;
private final FontMetrics metrics;
private FontMetrics metrics;
private Mode mode = Mode.FILL;

public Paint(){
Expand All @@ -20,7 +20,6 @@ public Paint(int flags){
this.flags = flags;
setTypeface(Typeface.DEFAULT);
setTextSize(18.0f);
metrics = new PaintMetrics(this);
}

public Mode getMode() {
Expand Down Expand Up @@ -57,6 +56,8 @@ public Typeface getTypeface() {

public void setTypeface(Typeface typeface) {
this.typeface = typeface;
if (typeface != null)
this.metrics = typeface.getMetricsFor(this);
}

public void setFlags(int flags) {
Expand All @@ -75,12 +76,6 @@ public FontMetrics getFontMetrics() {
return metrics;
}

private class PaintMetrics extends FontMetrics {
protected PaintMetrics(Paint paint) {
super(paint);
}
}

public void from(Paint paint){
color = paint.color;
mode = paint.mode;
Expand Down
42 changes: 6 additions & 36 deletions src/br/nullexcept/mux/graphics/fonts/FontMetrics.java
Original file line number Diff line number Diff line change
@@ -1,41 +1,11 @@
package br.nullexcept.mux.graphics.fonts;

import br.nullexcept.mux.graphics.Paint;

public abstract class FontMetrics {
private final Paint paint;

protected FontMetrics(Paint paint){
this.paint = paint;
}

public float getAscent(){
return paint.getTypeface().ascent * scale();
}

public float getDescent(){
return paint.getTypeface().descent * scale();
}

public float getLineHeight() {
return paint.getTypeface().lineHeight * scale();
}

public float measureChar(char ch){
return paint.getTypeface().measureChar(ch) * scale();
}

private float scale(){
return (paint.getTextSize()/Typeface.SCALE);
}
protected FontMetrics(){}

public float measureText(CharSequence line){
float width = 0;
Typeface typeface = paint.getTypeface();
float scale = scale();
for (int i = 0; i < line.length(); i++){
width += typeface.measureChar(line.charAt(i)) * scale;
}
return width;
}
public abstract float getAscent();
public abstract float getDescent();
public abstract float getLineHeight();
public abstract float measureChar(char ch);
public abstract float measureText(CharSequence line);
}
77 changes: 3 additions & 74 deletions src/br/nullexcept/mux/graphics/fonts/Typeface.java
Original file line number Diff line number Diff line change
@@ -1,83 +1,12 @@
package br.nullexcept.mux.graphics.fonts;

import br.nullexcept.mux.C;
import br.nullexcept.mux.utils.Log;
import org.lwjgl.nanovg.NVGGlyphPosition;
import org.lwjgl.nanovg.NanoVG;
import org.lwjgl.system.MemoryUtil;

import java.nio.ByteBuffer;
import java.util.UUID;

public class Typeface {
protected static final float SCALE = 1024.0f;
import br.nullexcept.mux.graphics.Paint;

public abstract class Typeface {
public static final int STYLE_NORMAL = 0;
public static final int STYLE_BOLD = 1;
public static final int STYLE_ITALIC = 2;

public static Typeface DEFAULT;

protected final float ascent;
protected final float descent;
protected final float lineHeight;
protected final ByteBuffer buffer;

private final int font;
private final String id = UUID.randomUUID().toString();
private final short[] bounds = new short[1024*16]; //64KB of buffer for store characters bounds

Typeface(ByteBuffer buffer){
this.buffer = buffer;
long context = C.VG_CONTEXT;
font = NanoVG.nvgCreateFontMem(context, id,buffer,false);
NanoVG.nvgFontFaceId(context, font);
NanoVG.nvgTextLetterSpacing(context, 0.0f);
NanoVG.nvgFontSize(context, SCALE);
NanoVG.nvgFontBlur(context,0);
float[] ascent = new float[1];
float[] descent = new float[1];
float[] lineHeight = new float[1];
NanoVG.nvgTextMetrics(context, ascent,descent,lineHeight);
this.ascent = ascent[0];
this.descent = descent[0];
this.lineHeight = lineHeight[0];
NVGGlyphPosition.create();
NVGGlyphPosition.Buffer b = NVGGlyphPosition.create(1);
for (int i = 0; i < this.bounds.length; i++){
String character = String.valueOf((char)i);
NanoVG.nvgTextGlyphPositions(context,0,0,character,b);
this.bounds[i] = (short) ((short) Math.abs(b.maxx()) + Math.abs(b.minx()));
}
}

protected int measureChar(char ch){
if (ch > bounds.length){
Log.log("Typeface","INVALID MEASURE OUTBOUNDS CHAR: "+(int)ch);
ch = 'Z';
}
switch (ch){
case '\t':
case '\f':
return bounds[' '];
case '\n':
case '\r':
return 0;
}
return bounds[ch];
}

public int hashCode(){
return font;
}

@Override
public String toString() {
return id;
}

@Override
protected void finalize() throws Throwable {
super.finalize();
}
public abstract FontMetrics getMetricsFor(Paint paint);
}
7 changes: 5 additions & 2 deletions src/br/nullexcept/mux/graphics/fonts/TypefaceFactory.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package br.nullexcept.mux.graphics.fonts;

import br.nullexcept.mux.C;
import br.nullexcept.mux.utils.BufferUtils;

import java.io.InputStream;

public class TypefaceFactory {
public abstract class TypefaceFactory {

protected abstract Typeface decodeTypeface(InputStream input);

public static Typeface create(InputStream stream){
return new Typeface(BufferUtils.allocateStream(stream));
return C.TYPEFACE_FACTORY.decodeTypeface(stream);
}
}
2 changes: 1 addition & 1 deletion src/br/nullexcept/mux/res/FallbackLanguage.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import java.util.HashMap;

class FallbackLanguage {
private HashMap<String, String> values = new HashMap<>();
private final HashMap<String, String> values = new HashMap<>();

public String getString(String id) {
return values.getOrDefault(id, id);
Expand Down
6 changes: 5 additions & 1 deletion src/br/nullexcept/mux/text/TextLayout.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public class TextLayout {
private final FontMetrics font;
private final Size viewport = new Size();
private final Selection selection;
private final TextRenderer drawer;
private TextRenderer drawer;

private int gravity;
// [INTERNAL LINE] => LINE | START | END | WIDTH
Expand All @@ -26,6 +26,10 @@ public TextLayout(Editable text, Paint paint, TextRenderer drawer) {
this.drawer = drawer;
}

public void setRenderer(TextRenderer drawer) {
this.drawer = drawer;
}

public int getWidth() {
return viewport.width;
}
Expand Down
Loading

0 comments on commit efaf733

Please sign in to comment.