Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Creation of Table abstracts for read/writing. #5

Open
nanjizal opened this issue Apr 10, 2019 · 3 comments
Open

Creation of Table abstracts for read/writing. #5

nanjizal opened this issue Apr 10, 2019 · 3 comments

Comments

@nanjizal
Copy link
Contributor

nanjizal commented Apr 10, 2019

Cambiata

I propose a cleaner approach to font table construction by creating a folder table and create an abstract for reading and writing each table.

For instance start to move function in the reader such as readHmtxTable as an abstract that is setup to make reading and writing simpler. I am not overly sure on the write but imagine it might look a bit like the code below.

The concept initially would be to work towards being able to modify fonts ( so store construction tables to write back to ) then later perhaps creating tables from scratch. At which stage we may need some more thought into structures so they are rational to create from scratch but initially can assume a simpler state were we read write.

Obviously at this stage I am unsure of the exact detail on the write to output with offsets but I think if we assume just direct writing we can tweak that when we have all the tables setup to write.

package format.ttf.tables;
// HMTX
typedef Metric = {
	advanceWidth:Int,
	leftSideBearing:Int
}
// new typedef to store some more properties for writing.
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
    static public 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();
        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;
    }
}

Please let me know if this approach seems sensible. If you have questions I can try to explain better, perhaps even implement. I know it is quite nice to have everything structured in reader and data for 'format' compatibility but I think table subfolder might work but open to your thoughts on this but I think the abstract approach within that structure is clean.

@nanjizal
Copy link
Contributor Author

nanjizal commented Apr 10, 2019

on some of them can use @:from but this read has three parameters.

@nanjizal
Copy link
Contributor Author

nanjizal commented Apr 26, 2019

Sorry did not get to this earlier, I have started this branch in relation to splitting read into Tables so that abstracts support Write and toString ( for Tools ), for the and game of creating ttf write.

https://github.com/nanjizal/HxTrueType/blob/save/src/format/ttf/tables/TODO.md

If you have any thoughts on approach then welcome feedback, it's going to take a lot of effort but I think it will be worth it, if you have time and keen we can split the work.

Aim get the tables roughly right then try to reintegrate one by one perhaps partially merging with current Reader prior to swapping reader.

Once it works same as currently again, then look at details of wiring up Write and considering if the table writes need adjustment for actual writting.

The new code is in the 'save' branch and far from useful.

@nanjizal
Copy link
Contributor Author

nanjizal commented Apr 29, 2019

The Reads for lots of the tables have been abstracted and now seem to run with current code, see italic entries.
Glyphs, Kern, Cmap still need integration.
https://github.com/nanjizal/HxTrueType/blob/save/src/format/ttf/tablesDev/TODO.md
The Reader changed in Table creations:
https://github.com/nanjizal/HxTrueType/blob/save/src/format/ttf/Reader.hx#L40
The code that needs integrating is in tableDev folder.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant