Skip to content

Commit

Permalink
Merge pull request HeapsIO#5 from lusius/JsonFontParser
Browse files Browse the repository at this point in the history
Json font parser
  • Loading branch information
lbergman authored Feb 6, 2025
2 parents d6adb4b + 0d722c7 commit 02a3e48
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 0 deletions.
7 changes: 7 additions & 0 deletions hxd/fmt/bfnt/FontParser.hx
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,13 @@ class FontParser {

font.baseLine = 0;

// Json Format generated by https://github.com/Chlumsky/msdf-atlas-gen
if(bytes.getString(0,1) == "{") {
resolveTileSameName();
return hxd.fmt.bfnt.JsonFontParser.parse(bytes,tile);
}


switch( bytes.getInt32(0) ) {
case 0x544E4642: // Internal BFNT
return hxd.fmt.bfnt.Reader.parse(bytes, function( tp : String ) { resolveTileWithFallback(tp); return tile; });
Expand Down
85 changes: 85 additions & 0 deletions hxd/fmt/bfnt/JsonFontParser.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package hxd.fmt.bfnt;

import h2d.Font.FontChar;
import haxe.Json;
using thx.Arrays;

typedef JsonFontAtlas = {
var type:String;
var distanceRange:Int;
var size:Int;
var width:Int;
var height:Int;
}

typedef JsonFontMetrics = {
var emSize:Float;
var lineHeight:Float;
var ascender:Float;
var descender:Float;
var underlineY:Float;
var underlineThickness:Float;
}

typedef JsonFontBounds = {
var left:Float;
var bottom:Float;
var right:Float;
var top:Float;
}

typedef JsonFontGlyph = {
var unicode:Int;
var advance:Float;
var planeBounds:JsonFontBounds;
var atlasBounds:JsonFontBounds;
}


typedef JsonFontFormat = {
var atlas:JsonFontAtlas;
var metrics:JsonFontMetrics;
var glyphs:Array<JsonFontGlyph>;
}

@:access(h2d.Font)
class JsonFontParser {

public static inline function parse(bytes : haxe.io.Bytes,tile) : h2d.Font {
final font = new h2d.Font(null,0);
final glyphs = font.glyphs;
font.tile = tile;

final data:JsonFontFormat = Json.parse(bytes.getString(0,bytes.length));

font.size = data.atlas.size;
font.initSize = font.size;
font.distanceRange = data.atlas.distanceRange;
font.fieldType = data.atlas.type;

font.lineHeight = data.metrics.lineHeight * data.atlas.size * 0.5;

for(glyph in data.glyphs) {
final xadvance = data.atlas.size * glyph.advance;
if( glyph.atlasBounds != null && glyph.planeBounds != null ) {
final x = glyph.atlasBounds.left;
final y = data.atlas.height - glyph.atlasBounds.top;
final width = glyph.atlasBounds.right - glyph.atlasBounds.left;
final height = glyph.atlasBounds.top - glyph.atlasBounds.bottom;

final offsetX = glyph.planeBounds.left * data.atlas.size;
final offsetY = glyph.planeBounds.top * data.atlas.size;

final subTile = tile.sub(x,y,width,height,offsetX,-offsetY + data.atlas.size);
glyphs.set(glyph.unicode,new FontChar(subTile,xadvance));
} else {
final subTile = tile.sub(0,0,1,1,0,0);
glyphs.set(glyph.unicode,new FontChar(subTile,xadvance));
}

}

return font;
}

}

0 comments on commit 02a3e48

Please sign in to comment.