-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b2504a7
commit efaf733
Showing
12 changed files
with
178 additions
and
122 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.