prawnpdf / ttfunk

Font Metrics Parser for Prawn
http://prawnpdf.org
Other
125 stars 75 forks source link

Parse bitmap data from sbix tables #22

Closed javan closed 10 years ago

javan commented 10 years ago

I set out to find an effective way of extracting emoji images from Apple's "Apple Color Emoji.ttf" font and found that the data necessary data is in the sbix table.

This table provides access to bitmap data in a standard graphics format (such as PNG, JPEG, TIFF).

emoji_font = Pathname.new("/System/Library/Fonts/Apple Color Emoji.ttf")
ttf = TTFunk::File.open(emoji_font)

char_code = "\u{1f4a9}".codepoints.first
glyph_id = ttf.cmap.unicode.first.code_map[char_code]

ttf.sbix.all_bitmap_data_for(glyph_id).each do |bitmap|
  filename = "glyph-#{glyph_id}-#{bitmap.ppem}.#{bitmap.type}"
  File.write(filename, bitmap.data.read)
end

glyph-569-160glyph-569-96glyph-569-64glyph-569-48glyph-569-40glyph-569-32glyph-569-20

practicingruby commented 10 years ago

Please disregard my original comment. I was looking at some outdated information on wikipedia -- I should have been looking at the page from the Apple reference manual you linked instead. :blush:

https://developer.apple.com/fonts/TrueType-Reference-Manual/RM06/Chap6sbix.html

With that in mind, I think we probably could accept this patch, but please add some tests for it.

javan commented 10 years ago

Ok, cool. Happy to add some tests. It's probably best to test against a file with an sbix table and the only one that I know of is Apple's emoji font. Do you have any copyright concerns about bundling it in the test suite? If so, do you happen to know of an alternative?

practicingruby commented 10 years ago

@javan: Maybe give this one a try? https://github.com/MorbZ/OpenSansEmoji

We definitely can't bundle non-free fonts, unfortunately.

javan commented 10 years ago

OpenSansEmoji doesn't have an sbix table, but I found a sample font here - http://typophile.com/node/103268 - that does. Added some tests.

practicingruby commented 10 years ago

Merged, thank you!

javan commented 10 years ago

:metal:

practicingruby commented 10 years ago

@javan: Because your pull request was accepted, you now have commit access to all prawnpdf repositories. See the contribution guidelines for more details, and thanks for the patch!

javan commented 10 years ago

Cool. Already putting this to work https://github.com/github/gemoji/pull/63 :grin:

practicingruby commented 10 years ago

@javan: Nice! I'll try to make sure to get a release cut soon, so you can switch back to the rubygems.org version of TTFunk when that happens.

tremby commented 9 years ago

I'm trying to shoehorn the Android emoji font into this. I'm not having any luck. I don't know anything about TTF fonts: is it maybe the same issue as above, with this font not having an sbix table?

DanielZanchi commented 8 years ago

I'm trying to do extract all the emojis with TTFunk but can't find the way. Can someone help me. What do I have to run from the terminal after installing the gem TTFunk?

pointlessone commented 8 years ago

@Danny182 You can take a look at sbix table specs

DanielZanchi commented 8 years ago

@pointlessone , thank you for the reply. I didn't understand how to run the code. If I run "ruby file_spec.rb" I get the missing "spec_helper.rb". If I run that one I get another missing one. Sorry but I'm having problems to run the "Rakefile".

pointlessone commented 8 years ago

@Danny182 That code won't extract anything for you. That is merely an example of usage. You would need to write your own code to actually extract emoji.

The file is not a "Rakefile". It's a spec file meant to be run with RSpec. Try rspec spec/integration/file_spec.rb to run it but I'm not sure how that might be helpful to you.

DanielZanchi commented 8 years ago

I found a way. By running "gemoji" by terminal running "rake images:extract" it extracts the emojis but it doesn't extracts some of them. "🙄 1F644" doesn't get extracted. I updated manually the "db/NamesList.txt" (the info of that emoji is in there). Can someone help me?

Thanks in advance @pointlessone @javan