2017-09-25 09:36:33 -07:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2022-04-29 10:23:03 -07:00
|
|
|
def gen_border(codepoint, color)
|
2023-02-08 01:38:07 -08:00
|
|
|
input = Rails.public_path.join('emoji', "#{codepoint}.svg")
|
|
|
|
dest = Rails.public_path.join('emoji', "#{codepoint}_border.svg")
|
2020-06-08 15:12:20 -07:00
|
|
|
doc = File.open(input) { |f| Nokogiri::XML(f) }
|
|
|
|
svg = doc.at_css('svg')
|
|
|
|
if svg.key?('viewBox')
|
2023-03-15 18:34:00 -07:00
|
|
|
view_box = svg['viewBox'].split.map(&:to_i)
|
2020-06-08 15:12:20 -07:00
|
|
|
view_box[0] -= 2
|
|
|
|
view_box[1] -= 2
|
|
|
|
view_box[2] += 4
|
|
|
|
view_box[3] += 4
|
|
|
|
svg['viewBox'] = view_box.join(' ')
|
|
|
|
end
|
|
|
|
g = Nokogiri::XML::Node.new 'g', doc
|
|
|
|
doc.css('svg > *').each do |elem|
|
|
|
|
border_elem = elem.dup
|
|
|
|
|
|
|
|
border_elem.delete('fill')
|
|
|
|
|
2022-04-29 10:23:03 -07:00
|
|
|
border_elem['stroke'] = color
|
2020-06-08 15:12:20 -07:00
|
|
|
border_elem['stroke-linejoin'] = 'round'
|
|
|
|
border_elem['stroke-width'] = '4px'
|
|
|
|
|
|
|
|
g.add_child(border_elem)
|
|
|
|
end
|
|
|
|
svg.prepend_child(g)
|
|
|
|
File.write(dest, doc.to_xml)
|
|
|
|
puts "Wrote bordered #{codepoint}.svg to #{dest}!"
|
|
|
|
end
|
|
|
|
|
2017-09-25 09:36:33 -07:00
|
|
|
def codepoints_to_filename(codepoints)
|
2023-06-06 05:50:51 -07:00
|
|
|
codepoints.downcase.gsub(/\A0+/, '').tr(' ', '-')
|
2017-09-25 09:36:33 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
def codepoints_to_unicode(codepoints)
|
|
|
|
if codepoints.include?(' ')
|
2023-03-15 18:34:00 -07:00
|
|
|
codepoints.split.map(&:hex).pack('U*')
|
2017-09-25 09:36:33 -07:00
|
|
|
else
|
|
|
|
[codepoints.hex].pack('U')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
namespace :emojis do
|
|
|
|
desc 'Generate a unicode to filename mapping'
|
|
|
|
task :generate do
|
2024-01-11 08:15:08 -08:00
|
|
|
source = 'http://www.unicode.org/Public/emoji/15.0/emoji-test.txt'
|
2017-09-25 09:36:33 -07:00
|
|
|
codes = []
|
2017-10-05 18:42:34 -07:00
|
|
|
dest = Rails.root.join('app', 'javascript', 'mastodon', 'features', 'emoji', 'emoji_map.json')
|
2017-09-25 09:36:33 -07:00
|
|
|
|
|
|
|
puts "Downloading emojos from source... (#{source})"
|
|
|
|
|
|
|
|
HTTP.get(source).to_s.split("\n").each do |line|
|
|
|
|
next if line.start_with? '#'
|
2020-06-08 15:12:20 -07:00
|
|
|
|
2017-09-25 09:36:33 -07:00
|
|
|
parts = line.split(';').map(&:strip)
|
|
|
|
next if parts.size < 2
|
2020-06-08 15:12:20 -07:00
|
|
|
|
2017-09-25 09:36:33 -07:00
|
|
|
codes << [parts[0], parts[1].start_with?('fully-qualified')]
|
|
|
|
end
|
|
|
|
|
|
|
|
grouped_codes = codes.reduce([]) do |agg, current|
|
|
|
|
if current[1]
|
|
|
|
agg << [current[0]]
|
|
|
|
else
|
|
|
|
agg.last << current[0]
|
|
|
|
agg
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-02-21 16:57:15 -08:00
|
|
|
existence_maps = grouped_codes.map { |c| c.index_with { |cc| Rails.public_path.join('emoji', "#{codepoints_to_filename(cc)}.svg").exist? } }
|
2017-09-25 09:36:33 -07:00
|
|
|
map = {}
|
|
|
|
|
|
|
|
existence_maps.each do |group|
|
|
|
|
existing_one = group.key(true)
|
|
|
|
|
2018-12-24 10:12:00 -08:00
|
|
|
next if existing_one.nil?
|
|
|
|
|
2017-09-25 09:36:33 -07:00
|
|
|
group.each_key do |key|
|
|
|
|
map[codepoints_to_unicode(key)] = codepoints_to_filename(existing_one)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
map = map.sort { |a, b| a[0].size <=> b[0].size }.to_h
|
|
|
|
|
|
|
|
File.write(dest, Oj.dump(map))
|
|
|
|
puts "Wrote emojo to destination! (#{dest})"
|
|
|
|
end
|
2020-06-08 15:12:20 -07:00
|
|
|
|
|
|
|
desc 'Generate emoji variants with white borders'
|
|
|
|
task :generate_borders do
|
|
|
|
src = Rails.root.join('app', 'javascript', 'mastodon', 'features', 'emoji', 'emoji_map.json')
|
2024-01-11 08:15:08 -08:00
|
|
|
emojis_light = '👽⚾🐔☁️💨🕊️👀🍥👻🐐❕❔⛸️🌩️🔊🔇📃🌧️🐏🍚🍙🐓🐑💀☠️🌨️🔉🔈💬💭🏐🏳️⚪⬜◽◻️▫️🪽🪿'
|
|
|
|
emojis_dark = '🎱🐜⚫🖤⬛◼️◾◼️✒️▪️💣🎳📷📸♣️🕶️✴️🔌💂♀️📽️🍳🦍💂🔪🕳️🕹️🕋🖊️🖋️💂♂️🎤🎓🎥🎼♠️🎩🦃📼📹🎮🐃🏴🐞🕺📱📲🚲🪮🐦⬛'
|
2020-06-08 15:12:20 -07:00
|
|
|
|
|
|
|
map = Oj.load(File.read(src))
|
|
|
|
|
2022-04-29 10:23:03 -07:00
|
|
|
emojis_light.each_grapheme_cluster do |emoji|
|
|
|
|
gen_border map[emoji], 'black'
|
|
|
|
end
|
|
|
|
emojis_dark.each_grapheme_cluster do |emoji|
|
|
|
|
gen_border map[emoji], 'white'
|
2020-06-08 15:12:20 -07:00
|
|
|
end
|
|
|
|
end
|
2017-09-25 09:36:33 -07:00
|
|
|
end
|