-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[tools] TTF/OTF support in asset sources and assets transformation an…
…d hot reload improvements
- Loading branch information
Showing
29 changed files
with
616 additions
and
155 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,4 @@ | ||
plugin: | ||
name: Font | ||
tools: tools.FontPlugin | ||
|
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,72 @@ | ||
package tools; | ||
|
||
import haxe.io.Path; | ||
import sys.FileSystem; | ||
import sys.io.File; | ||
import tools.Context; | ||
import tools.Helpers.*; | ||
|
||
using StringTools; | ||
|
||
@:keep | ||
class FontPlugin { | ||
|
||
/// Tools | ||
|
||
public function new() {} | ||
|
||
public function init(context:Context):Void { | ||
|
||
// Add tasks | ||
context.addTask('font', new tools.tasks.font.Font()); | ||
|
||
// Add assets transformers | ||
context.assetsTransformers.push({ | ||
transform: transformAssets | ||
}); | ||
|
||
} | ||
|
||
public function transformAssets(assets:Array<tools.Asset>, transformedAssetsPath:String, changedPaths:Array<String>):Array<tools.Asset> { | ||
|
||
var result:Array<tools.Asset> = []; | ||
|
||
for (asset in assets) { | ||
final lowerCaseName = asset.name.toLowerCase(); | ||
if (lowerCaseName.endsWith('.ttf') || lowerCaseName.endsWith('.otf')) { | ||
// Transform TTF/OTF to MSDF Bitmap Font | ||
|
||
// Compute destination .fnt path | ||
var baseName = asset.name.substring(0, asset.name.length - 4); | ||
var fntName = baseName + '.fnt'; | ||
var pngName = baseName + '.png'; | ||
var dstFntPath = Path.join([transformedAssetsPath, fntName]); | ||
var dstPngPath = Path.join([transformedAssetsPath, pngName]); | ||
|
||
if (!Files.haveSameLastModified(asset.absolutePath, dstFntPath) || !Files.haveSameLastModified(asset.absolutePath, dstPngPath)) { | ||
FontUtils.createBitmapFont({ | ||
fontPath: asset.absolutePath, | ||
outputPath: transformedAssetsPath, | ||
msdf: true, | ||
quiet: true | ||
}); | ||
Files.setToSameLastModified(asset.absolutePath, dstFntPath); | ||
Files.setToSameLastModified(asset.absolutePath, dstPngPath); | ||
|
||
changedPaths.push(dstFntPath); | ||
changedPaths.push(dstPngPath); | ||
} | ||
|
||
result.push(new tools.Asset(fntName, transformedAssetsPath)); | ||
result.push(new tools.Asset(pngName, transformedAssetsPath)); | ||
} | ||
else { | ||
result.push(asset); | ||
} | ||
} | ||
|
||
return result; | ||
|
||
} | ||
|
||
} |
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,80 @@ | ||
package tools.tasks.font; | ||
|
||
import haxe.Json; | ||
import haxe.io.Path; | ||
import sys.FileSystem; | ||
import sys.io.File; | ||
import tools.Helpers.*; | ||
|
||
using StringTools; | ||
|
||
class Font extends Task { | ||
|
||
/// Lifecycle | ||
|
||
override public function help(cwd:String):Array<Array<String>> { | ||
|
||
return [ | ||
['--font <path to font>', 'The ttf/otf font file to convert'], | ||
['--out <output directory>', 'The output directory'], | ||
['--msdf', 'If used, export with multichannel distance field'], | ||
['--msdf-range', 'Sets the distance field range in pixels (default: 2)'], | ||
['--size <font size>', 'The font size to export (default: 42)'], | ||
['--factor <factor>', 'A precision factor (advanced usage, default: 4)'], | ||
['--charset', 'Characters to use as charset'], | ||
['--charset-file', 'A text file containing characters to use as charset'], | ||
['--offset-x', 'Move every character by this X offset'], | ||
['--offset-y', 'Move every character by this Y offset'] | ||
]; | ||
|
||
} | ||
|
||
override public function info(cwd:String):String { | ||
|
||
return "Utility to convert ttf/otf font to bitmap font compatible with ceramic"; | ||
|
||
} | ||
|
||
override function run(cwd:String, args:Array<String>):Void { | ||
|
||
var fontPath = extractArgValue(args, 'font'); | ||
var outputPath = extractArgValue(args, 'out'); | ||
var charset = extractArgValue(args, 'charset'); | ||
var quiet = extractArgFlag(args, 'quiet'); | ||
var charsetFile = extractArgValue(args, 'charset-file'); | ||
var msdf = extractArgFlag(args, 'msdf'); | ||
var msdfRange = extractArgValue(args, 'msdf-range') != null ? Math.round(Std.parseFloat(extractArgValue(args, 'msdf-range'))) : 2; | ||
var bold = extractArgFlag(args, 'bold'); | ||
var italic = extractArgFlag(args, 'italic'); | ||
var padding:Int = extractArgValue(args, 'padding') != null ? Math.round(Std.parseFloat(extractArgValue(args, 'padding'))) : 2; | ||
var size:Float = extractArgValue(args, 'size') != null ? Std.parseFloat(extractArgValue(args, 'size')) : 42; | ||
var offsetX:Float = extractArgValue(args, 'offset-x') != null ? Std.parseFloat(extractArgValue(args, 'offset-x')) : 0; | ||
var offsetY:Float = extractArgValue(args, 'offset-y') != null ? Std.parseFloat(extractArgValue(args, 'offset-y')) : 0; | ||
|
||
if (fontPath == null) { | ||
fail('--font argument is required'); | ||
} | ||
|
||
if (outputPath == null) { | ||
outputPath = cwd; | ||
} | ||
|
||
FontUtils.createBitmapFont({ | ||
fontPath: fontPath, | ||
outputPath: outputPath, | ||
charset: charset, | ||
quiet: quiet, | ||
charsetFile: charsetFile, | ||
msdf: msdf, | ||
msdfRange: msdfRange, | ||
bold: bold, | ||
italic: italic, | ||
padding: padding, | ||
size: size, | ||
offsetX: offsetX, | ||
offsetY: offsetY | ||
}); | ||
|
||
} | ||
|
||
} |
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.