Created
June 14, 2016 06:55
-
-
Save Bayonetta/889b90bed69fc10720aeb0cee331cb6e to your computer and use it in GitHub Desktop.
Fetch Mac OS inner emoij pngs
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
# encoding: US-ASCII | |
# stolen largely from http://www.ruby-forum.com/topic/140784 | |
require 'stringio' | |
require 'fileutils' | |
def extract_chunk(input, output) | |
lenword = input.read(4) | |
length = lenword.unpack('N')[0] | |
type = input.read(4) | |
data = length>0 ? input.read(length) : "" | |
crc = input.read(4) | |
return nil if length<0 || !(('A'..'z')===type[0,1]) | |
#return nil if validate_crc(type+data, crc) | |
output.write lenword | |
output.write type | |
output.write data | |
output.write crc | |
return [type, data] | |
end | |
def extract_png(input) | |
buf = StringIO.new | |
hdr = input.read(8) | |
raise "Not a PNG File" if hdr[0,4]!= "\211PNG" | |
raise "file not in binary mode" if hdr[4,4]!="\r\n\032\n" | |
buf.write(hdr) | |
height, width = 0, 0 | |
loop do | |
chunk_type, chunk_data = extract_chunk(input,buf) | |
height, width = chunk_data.unpack('NN') if chunk_type == 'IHDR' | |
break if chunk_type.nil? || chunk_type == 'IEND' | |
end | |
FileUtils.mkdir_p(dir = "images/#{height}x#{width}") | |
if @prev != dir | |
@n = 0 | |
@prev = dir | |
end | |
buf.rewind | |
ofp = File.new("#{dir}/#{@n+=1}.png","wb") | |
ofp.write buf.read | |
ofp.close | |
end | |
ttf = File.new("/System/Library/Fonts/Apple Color Emoji.ttf","rb") | |
ttf_data = ttf.read | |
pos = 0 | |
while m = /\211PNG/.match(ttf_data[pos..-1]) | |
raise "no PNG found" if !m | |
pos += m.begin(0) + 1 | |
ttf.seek(pos-1) | |
extract_png(ttf) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment