Last active
April 10, 2019 03:56
-
-
Save nanjizal/9f1ee3ff36a944dc46a70fb96df57e43 to your computer and use it in GitHub Desktop.
Writing TTF tables using abstract
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
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