Accessing sfntly using java Streams

70 views
Skip to first unread message

Bill Schwanitz

unread,
Jan 10, 2012, 4:58:39 PM1/10/12
to sfntly-users
Hi sfntly gurus,

Is there a way to access the actual bytes of a Font?

I'm writing a class the extends InputStream to perform some
subsetting, hinting and reformatting and make the result look like an
InputStream. I figured out how to use EOTWriter and WoffWriter to
build WritableFontData. Then I can index into the WritableFontData
byte-by-byte to simulate a read(). However, I can't figure out the
seemingly simpler case where I need to get to the bytes of the Font in
ttf (native) format.

Thanks for your ideas,
Bill

Stuart Gill

unread,
Jan 10, 2012, 7:26:28 PM1/10/12
to sfntly-users
I'm not sure that I understand the full nature of what it is that you
are doing. Some of the data that makes up the font serialization
format (e.g. ttf file) is not generated by the sfntly code until
serialization happens and so this raw low level data is not available
until that happens. The EOT and Woff code causes the core sfntly code
to do this serialization or parts of it during it's work.

Tell us more about what you are doing and we should be able to give
you more specific advice.

Stuart

Bill Schwanitz

unread,
Jan 11, 2012, 12:11:50 AM1/11/12
to sfntly-users
Hi Stuart,

Thanks for the quick reply. First, I need to say how much I'm enjoying
working with the sfntly library. Nice work!

I'll try to give some code... I'd like to create a class that extends
InputStream that takes an InputStream from a ttf file and subsets and/
or outputs in a different format.

For example:

class MyFontInputStream extends InputStream {
public MyFontInputStream(InputStream isTTF, String format, String
charsInSubset);

public int read();
}

Then I can use this class like:
MyFontInputStream is = MyFontInputStream(new FileInputStream(new
File("some name")), "eot", "abcdefg");
This will take the incoming InputStream, subset it down to the
characters "abcdefg" and convert it to eot format.

Then in the constructor of MyFontInputStream I perform all the sfntly
operations to subset the incoming InputStream and convert it to a
different format. In the case of converting the incoming ttf to eot or
woff, it is easy to use the WritableFontData output from EOTWriter and
WoffWriter to implement the int read() method:
public int read() {
return writableFontData.readByte(offset++);
}

My problem is the *seemingly* simpler case where the desired output
format is ttf. For example:
MyFontInputStream is = MyFontInputStream(new FileInputStream(new
File("some name")), "ttf", "abcdefg");

In this case, I just want to do the subsetting and make the resulting
font available as a ttf. How can I step through the ttf's bytes one by
one to make them available for the MyFontInputStream's read() method?

Thanks,
Bill

Stuart Gill

unread,
Jan 11, 2012, 7:49:13 PM1/11/12
to sfntly-users
Bill,

Well, the Woff and EOT code currently rides on top of the sfntly
library so what it does is buffer up the font output into a
WritableFontData by serializing the font and it's tables. That
WritableFontData that you get from it is the final results of it
buffering.

What you can do to get the data for a ttf format font is to do the
same thing. Use the FontFactory.serializeFont() method to serialize
the font to an OutputStream. That OutputStream should buffer up the
font data into some location (WritableFontData, ByteBuffer, byte[],
etc.) and then use that buffered data in your InputStream.

Another option is to write your own more fancy OutputStream that has a
small buffer (say 4K bytes). You would give this custom OutputStream
to the FontFactory.serializeFont() method. The buffer that it uses
would also be shared by your custom InputStream. You would then need
to block on a write in the OutputStream portion when the buffer fills
up and let the InputStream read it off until it's empty enough. At
that point you'd block on the reads from the InputStream until the
buffer is again full enough. It's more complex but it will save some
memory space for the buffer. I'm not sure if it's worth it but those
are your two options.

Stuart

billsc...@comcast.net

unread,
Jan 13, 2012, 3:33:06 PM1/13/12
to sfntly-users
Great suggestions. Thanks Stuart!
> > > > Bill- Hide quoted text -
>
> - Show quoted text -
Reply all
Reply to author
Forward
0 new messages