emojis.rake 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. # frozen_string_literal: true
  2. def gen_border(codepoint, color)
  3. input = Rails.public_path.join('emoji', "#{codepoint}.svg")
  4. dest = Rails.public_path.join('emoji', "#{codepoint}_border.svg")
  5. doc = File.open(input) { |f| Nokogiri::XML(f) }
  6. svg = doc.at_css('svg')
  7. if svg.key?('viewBox')
  8. view_box = svg['viewBox'].split.map(&:to_i)
  9. view_box[0] -= 2
  10. view_box[1] -= 2
  11. view_box[2] += 4
  12. view_box[3] += 4
  13. svg['viewBox'] = view_box.join(' ')
  14. end
  15. g = Nokogiri::XML::Node.new 'g', doc
  16. doc.css('svg > *').each do |elem|
  17. border_elem = elem.dup
  18. border_elem.delete('fill')
  19. border_elem['stroke'] = color
  20. border_elem['stroke-linejoin'] = 'round'
  21. border_elem['stroke-width'] = '4px'
  22. g.add_child(border_elem)
  23. end
  24. svg.prepend_child(g)
  25. File.write(dest, doc.to_xml)
  26. puts "Wrote bordered #{codepoint}.svg to #{dest}!"
  27. end
  28. def codepoints_to_filename(codepoints)
  29. codepoints.downcase.gsub(/\A0+/, '').tr(' ', '-')
  30. end
  31. def codepoints_to_unicode(codepoints)
  32. if codepoints.include?(' ')
  33. codepoints.split.map(&:hex).pack('U*')
  34. else
  35. [codepoints.hex].pack('U')
  36. end
  37. end
  38. namespace :emojis do
  39. desc 'Generate a unicode to filename mapping'
  40. task :generate do
  41. source = 'http://www.unicode.org/Public/emoji/14.0/emoji-test.txt'
  42. codes = []
  43. dest = Rails.root.join('app', 'javascript', 'mastodon', 'features', 'emoji', 'emoji_map.json')
  44. puts "Downloading emojos from source... (#{source})"
  45. HTTP.get(source).to_s.split("\n").each do |line|
  46. next if line.start_with? '#'
  47. parts = line.split(';').map(&:strip)
  48. next if parts.size < 2
  49. codes << [parts[0], parts[1].start_with?('fully-qualified')]
  50. end
  51. grouped_codes = codes.reduce([]) do |agg, current|
  52. if current[1]
  53. agg << [current[0]]
  54. else
  55. agg.last << current[0]
  56. agg
  57. end
  58. end
  59. existence_maps = grouped_codes.map { |c| c.index_with { |cc| Rails.public_path.join('emoji', "#{codepoints_to_filename(cc)}.svg").exist? } }
  60. map = {}
  61. existence_maps.each do |group|
  62. existing_one = group.key(true)
  63. next if existing_one.nil?
  64. group.each_key do |key|
  65. map[codepoints_to_unicode(key)] = codepoints_to_filename(existing_one)
  66. end
  67. end
  68. map = map.sort { |a, b| a[0].size <=> b[0].size }.to_h
  69. File.write(dest, Oj.dump(map))
  70. puts "Wrote emojo to destination! (#{dest})"
  71. end
  72. desc 'Generate emoji variants with white borders'
  73. task :generate_borders do
  74. src = Rails.root.join('app', 'javascript', 'mastodon', 'features', 'emoji', 'emoji_map.json')
  75. emojis_light = '👽⚾🐔☁️💨🕊️👀🍥👻🐐❕❔⛸️🌩️🔊🔇📃🌧️🐏🍚🍙🐓🐑💀☠️🌨️🔉🔈💬💭🏐🏳️⚪⬜◽◻️▫️'
  76. emojis_dark = '🎱🐜⚫🖤⬛◼️◾◼️✒️▪️💣🎳📷📸♣️🕶️✴️🔌💂‍♀️📽️🍳🦍💂🔪🕳️🕹️🕋🖊️🖋️💂‍♂️🎤🎓🎥🎼♠️🎩🦃📼📹🎮🐃🏴🐞🕺📱📲🚲'
  77. map = Oj.load(File.read(src))
  78. emojis_light.each_grapheme_cluster do |emoji|
  79. gen_border map[emoji], 'black'
  80. end
  81. emojis_dark.each_grapheme_cluster do |emoji|
  82. gen_border map[emoji], 'white'
  83. end
  84. end
  85. end