Skip to content

Instantly share code, notes, and snippets.

@nanjizal
Last active April 10, 2019 03:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nanjizal/9f1ee3ff36a944dc46a70fb96df57e43 to your computer and use it in GitHub Desktop.
Save nanjizal/9f1ee3ff36a944dc46a70fb96df57e43 to your computer and use it in GitHub Desktop.
Writing TTF tables using abstract
package format.ttf.tables;
// HMTX
typedef Metric = {
advanceWidth:Int,
leftSideBearing:Int
}
typedef THmtxTable = {
metrics: Array<Metric>,
numberOfHMetrics: Int,
numGlyphs: Int
}
@:forward
abstract HmtxTable( THmtxTable ) to THmtxTable {
public
function new( tHmtxTable: THmtxTable ){
this = tHmtxTable;
}
// hmtx (horizontal metrics) table
public static inline
function read( bytes, maxp, hhea ):Array<Metric> {
if (bytes == null)
throw 'no hmtx table found';
var input = new BytesInput(bytes);
input.bigEndian = true;
var metrics = new ArrayMetric();//:Array<Metric> = new Array();
for (i in 0...hhea.numberOfHMetrics) {
metrics.push({
advanceWidth: input.readUInt16(),
leftSideBearing: input.readInt16() // FWord
});
}
var len = maxp.numGlyphs - hhea.numberOfHMetrics;
var lastAdvanceWidth = metrics[metrics.length - 1].advanceWidth;
for (i in 0...len) {
metrics.push({advanceWidth: lastAdvanceWidth, leftSideBearing: input.readInt16()});
}
return new HmtxTable( { metrics: metrics
, numberOfHMetrics: hhea.numberOfHMetrics
, numGlyhs: maxp.numGlyhs } );
}
public inline
function write( o: haxe.io.Output ): haxe.io.Output {
var j = 0;
var m: Metric;
for( i in 0...this.numberOfHMetrics ){
m = this.metrics[ j ];
o.writeUInt16( m.advanceWidth );
o.writeInt16( m.advanceWidth );
j++;
}
var len = this.numGlyphs - this.numberOfHMetrics;
for( i in 0...len ){
m = this.metrics[ j ];
o.writeInt16( m.leftSideBearing );
j++;
}
return o;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment