rkusa / ttfjs

TTFjs is a TrueType font parser entirely written in JavaScript and compatible to both Node.js and the Browser
MIT License
21 stars 4 forks source link

Add usage example :) #2

Closed raphaelokon closed 9 years ago

raphaelokon commented 9 years ago

Hi @rkusa, great work on that one! Can you give a short example usage how to use the subsetter? Thanks.

rkusa commented 9 years ago

Sure

var fs = require('fs')
var TTFFont = require('ttfjs')

var font = new TTFFont(fs.readFileSync(__dirname + '/Corbel.ttf'))
var subset = font.subset()

subset.use('abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ 1234567890')
console.log(subset.encode('abcdefghijklmnopqrstuvwxyz'))

subset.embed()

fs.writeFileSync('./test.ttf', toBuffer(subset.save()), { encoding: 'binary' })

function toBuffer(ab) {
  var buffer = new Buffer(ab.byteLength)
  var view = new Uint8Array(ab)
  for (var i = 0; i < buffer.length; ++i) {
    buffer[i] = view[i]
  }
  return buffer
}
raphaelokon commented 9 years ago

Cheers. I actually got around it myself. Three things I noticed with subsetting tho:

1) Char mapping is wrong for me when subsettet https://github.com/rkusa/ttfjs/blob/master/lib/subset.js##L12-L13

2) The overall file size is still bigger (compared to the same subset done with sfntly)

3) The .notdef char is bloated with a lot of beziers

rkusa commented 9 years ago

Thanks for pointing these issues out!

1) What would be the correct mapping for you? It is possible that I am doing the subsetting wrong and compensate this issue in the actual subset usage in rkusa/pdfjs

I've but 2) and 3) on my todo and try to have a look at them.

raphaelokon commented 9 years ago

No probs at all. Your lib seems to be the first real treat of that problem in Node.js. And I am happy about it.

Well, the correct mapping would be so that each char complements to it respective charCode. For me the charList always started with

{ 32 : xx, 33, xx, … }

Opening the resulting ttf in Glyphs.app shown me the same wrong unicodes, regardless which chars I subsettet.

I fixed this for me by doing this.subset[code] = code

But maybe I was missing something.

raphaelokon commented 9 years ago

this.subsetyields { '32': 32, '33': 109, '34': 111 } for me when using:

var TTF = new ttfjs(data), 
    Subs = TTF.subset();
    Subs.use("moo");
    Subs.embed();

See the image attached: The upper window is when I use my fix … the lower when using it without the fix. The unicodes are wrong.

test

rkusa commented 9 years ago

Ah, I see. I am remapping the characters for the PDF usage. I'll add an option to decide whether to remap indices or keep the original ones.

Danke

raphaelokon commented 9 years ago

Kein Thema :)

If you like I can add the switch in there. And then do a pull request. Any preferences how to call the option?

rkusa commented 9 years ago

A PR would be very welcome! I think .subset({ remap: false }) would be nice - true should also be the default value to match the current behavior.

3) Hm, looks fine for me (also in Glyphs):

screen shot 2015-03-16 at 10 34 00

I've just tagged 0.22 which contains a fix from August 2014. I am not sure, but maybe this was the reason for this issue.

rkusa commented 9 years ago

2) Could you send me a subset created with sfntly per mail (for address, see github profile)?

raphaelokon commented 9 years ago

Sure. Let me send you it. Also, just doing the PR.