Skip to content

Instantly share code, notes, and snippets.

@hectorm
Created June 13, 2014 10:41
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save hectorm/606ab17edb8ce7793987 to your computer and use it in GitHub Desktop.
Save hectorm/606ab17edb8ce7793987 to your computer and use it in GitHub Desktop.
#!/bin/bash
#
# License:
# * Copyright (C) 2014 Héctor Molinero Fernández
# *
# * This program is free software: you can redistribute it and/or modify
# * it under the terms of the GNU General Public License as published by
# * the Free Software Foundation, either version 3 of the License, or
# * (at your option) any later version.
# *
# * This program is distributed in the hope that it will be useful,
# * but WITHOUT ANY WARRANTY; without even the implied warranty of
# * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# * GNU General Public License for more details.
# *
# * You should have received a copy of the GNU General Public License
# * along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# Dependencies:
# * fonttools
# * git
# * ruby
#
#=======================================#
infoMsg() {
echo -e "\e[1;33m + \e[1;32m$1 \e[0m"
}
#=======================================#
infoMsg "Removing previous files..."
rm -rf ./emoji ./emoji-temp ./ZNTEmoji.js ./ZNTEmoji.css
#=======================================#
infoMsg "Creating a new temporary directory..."
mkdir ./emoji-temp
cd ./emoji-temp
#=======================================#
infoMsg "Cloning noto-fonts Git repository..."
git clone https://android.googlesource.com/platform/external/noto-fonts
#=======================================#
infoMsg "Converting NotoColorEmoji.ttf to TTX format..."
ttx ./noto-fonts/NotoColorEmoji.ttf
cp ./noto-fonts/NotoColorEmoji.* ./
#=======================================#
infoMsg "Extracting font images using \"emoji_extractor.rb\"..."
cat > ./emoji_extractor.rb <<'EOF'
# https://github.com/zant95/emoji-extractor/blob/master/emoji_extractor.rb
# 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("./NotoColorEmoji.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
EOF
ruby ./emoji_extractor.rb
#=======================================#
infoMsg "Creating ZNTEmoji JavaScript file..."
cat > ../ZNTEmoji.js <<'EOF'
var ZNTEmoji = {
version: '1.0.0',
replace: function(string) {
'use strict';
return string
// Encode astral symbols.
.replace(/[\uD800-\uDBFF][\uDC00-\uDFFF]/g, function($0) {
// http://mathiasbynens.be/notes/javascript-encoding#surrogate-formulae
var high = $0.charCodeAt(0),
low = $0.charCodeAt(1),
codePoint = (high - 0xD800) * 0x400 + low - 0xDC00 + 0x10000,
astralSymbol = codePoint.toString(16);
var emojiElement = document.createElement('i');
emojiElement.className = 'ZNTEmoji ZNTEmoji-uni' + astralSymbol;
return emojiElement.outerHTML;
});
}
};
EOF
#=======================================#
infoMsg "Creating ZNTEmoji stylesheet file..."
cat > ../ZNTEmoji.css <<'EOF'
.ZNTEmoji {
display: inline-block;
vertical-align: middle;
width: 26px;
height: 24px;
background-size: 26px 24px;
background-color: transparent;
background-repeat: no-repeat;
}
EOF
#=======================================#
infoMsg "Renaming each image with its corresponding Unicode character and adding it to the stylesheet..."
# 136x128 or 68x64
for FILE in ./images/136x128/*.png
do
ID=$(basename $FILE .png)
NAME=$(grep -o -P "(?<=<GlyphID id=\"$ID\" name=\").*(?=\"/>)" NotoColorEmoji.ttx)
echo ".ZNTEmoji-$NAME{background-image:url(emoji/$NAME.png);}" >> ../ZNTEmoji.css
mv -fv $FILE ./images/136x128/$NAME.png
done
#=======================================#
infoMsg "Moving images to \"emoji\" directory..."
mv -fv ./images/136x128 ../emoji
#=======================================#
infoMsg "Exiting from temporary directory and deleting it..."
cd ../
rm -rf ./emoji-temp
exit 0
@LPhat
Copy link

LPhat commented Oct 17, 2014

Incredible work and use of the command line my friend.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment