summaryrefslogtreecommitdiffstats
path: root/webapp/make-emojis
blob: 2279934f672963c97d0a24f4da7b4c9e26c69697 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#!/usr/bin/env ruby

gem_name, *gem_ver_reqs = 'gemoji', '~> 3.0.0'
gdep = Gem::Dependency.new(gem_name, *gem_ver_reqs)
found_gspec = gdep.matching_specs.max_by(&:version)

if !found_gspec
    abort("ERROR: gemoji #{gdep.requirement} is required: `gem install gemoji -v '#{gdep.requirement}'`")
end

puts "using gemoji #{found_gspec.version}"

gem 'gemoji', found_gspec.version
require 'emoji'
require 'fileutils'
require 'json'

emoji_path = File.join(File.dirname(__FILE__), 'images', 'emoji')
if File.directory? emoji_path
    FileUtils.remove_dir emoji_path
end
Dir.mkdir emoji_path

Emoji.create('mattermost')
FileUtils.cp(File.join(File.dirname(__FILE__), 'images', 'icon64x64.png'), File.join(emoji_path, 'mattermost.png'))

custom_aliases = {
    "ca" => "canada",
    "pk" => "pakistan",
    "za" => "south_africa"
}
custom_aliases.each do |custom, original|
    emoji = Emoji.find_by_alias original

    Emoji.edit_emoji(emoji) do |char|
      char.add_alias custom
    end
end

blacklist = ['basecamp', 'basecampy']

categories = []

File.open(File.join(File.dirname(__FILE__), 'utils', 'emoji.jsx'), 'w') do |f|
    emojis = []
    emoji_indices_by_alias = []
    emoji_indices_by_unicode = []
    emoji_indices_by_category = []

    Emoji.all.each do |emoji|
        if blacklist.include? emoji.name
            next
        end
        emoji.aliases.each do |emoji_alias|
            emoji_indices_by_alias << [emoji_alias, emojis.length]
        end
        emoji.unicode_aliases.each do |unicode_alias|
            emoji_indices_by_unicode << [emoji.class.hex_inspect(unicode_alias), emojis.length]
        end
        category = emoji.category ? emoji.category.downcase : 'custom'
        if !categories.include? category
            categories << category
            emoji_indices_by_category << [category, []]
        end
        emoji_indices_by_category[categories.index(category)][1] << emojis.length
        filename = emoji.custom? ? emoji.name : emoji.hex_inspect
        emojis << {
            'aliases' => emoji.aliases,
            'filename' => filename,
            'category' => category,
        }
        emoji.image_filename = filename + '.png'
    end

    f.write("// This file is automatically generated via `make emojis`. Do not modify it manually.\n\n")
    f.write("/* eslint-disable */\n\n")
    f.write("export const Emojis = #{JSON.generate emojis};\n\n")
    f.write("export const EmojiIndicesByAlias = new Map(#{JSON.generate emoji_indices_by_alias});\n\n")
    f.write("export const EmojiIndicesByUnicode = new Map(#{JSON.generate emoji_indices_by_unicode});\n\n")
    f.write("export const CategoryNames = #{JSON.generate categories};\n\n")
    f.write("export const EmojiIndicesByCategory = new Map(#{JSON.generate emoji_indices_by_category});\n\n")
    f.write("/* eslint-enable */")

    puts "wrote #{emojis.length} emojis to utils/emoji.jsx"
end

require 'emoji/extractor'

Emoji::Extractor.new(64, emoji_path).extract!

Dir["#{Emoji.images_path}/*.png"].each do |png|
    if blacklist.include? File.basename(png, '.png')
        next
    end
    FileUtils.cp(png, File.join(emoji_path, File.basename(png)))
end

puts "images written to images/emoji"

### Spritesheet Generation ###

['sprite-factory', 'chunky_png'].each do |name|
    gdep = Gem::Dependency.new(name, '>1.0')
    found_gspec = gdep.matching_specs.max_by(&:version)

    if !found_gspec
        abort("ERROR: #{name} is required for spritesheet generation: `gem install #{name}'`")
    end
end

require 'sprite_factory'

css_rules = [
    '@charset "UTF-8";',
    '.emojisprite-preview {',
    '    @include transform(scale(.55));',
    '    background-repeat: no-repeat;',
    '    cursor: pointer;',
    '    height: 64px;',
    '    max-width: none;',
    '    transform-origin: 0 0;',
    '    width: 64px;',
    '    padding: 0 10px 0 0;',
    '}',
    '.emojisprite {',
    '    @include transform(scale(.35));',
    '    background-repeat: no-repeat;',
    '    border-radius: 18px;',
    '    cursor: pointer;',
    '    height: 64px;',
    '    max-width: none;',
    '    transform-origin: 0 0;',
    '    width: 64px;',
    '}',
    '.emojisprite-loading {',
    '    background-image: none !important;',
    '    @include transform(scale(.35));',
    '    background-repeat: no-repeat;',
    '    border-radius: 18px;',
    '    cursor: pointer;',
    '    height: 64px;',
    '    max-width: none;',
    '    transform-origin: 0 0;',
    '    width: 64px;',
    '}',
]

spritesheet_path = File.join(File.dirname(__FILE__), 'images', 'emoji-sheets')
if File.directory? spritesheet_path
    FileUtils.remove_dir spritesheet_path
end
Dir.mkdir spritesheet_path

categories.each do |category|
    count = 0

    tmp_directory = File.join(spritesheet_path, 'temp')
    Dir.mkdir tmp_directory
    Emoji.all.each do |emoji|
        if blacklist.include? emoji.name
            next
        end
        if category == (emoji.category ? emoji.category.downcase : 'custom')
            FileUtils.cp(File.join(emoji_path, emoji.image_filename), File.join(tmp_directory, emoji.image_filename))
            count += 1
        end
    end

    css_rules << ".emoji-category-#{category} { background-image: url('../images/emoji-sheets/#{category}.png'); }"

    SpriteFactory.run!(
        tmp_directory,
        :layout => :packed,
        :library => 'chunkypng',
        :output_image => File.join(spritesheet_path, category + '.png'),
        :nocomments => true,
        :nocss => true,
    ) do |images|
        images.each do |name, image|
            if image[:cssw] != 64 || image[:cssh] != 64
                css_rules << ".emoji-#{name} { background-position: -#{image[:cssx]}px -#{image[:cssy]}px; width: #{image[:cssw]}px; height: #{image[:cssh]}px; }"
            else
                css_rules << ".emoji-#{name} { background-position: -#{image[:cssx]}px -#{image[:cssy]}px; }"
            end
        end
    end

    FileUtils.remove_dir tmp_directory

    puts "#{count} emojis written to images/emoji-sheets/#{category}.png"
end

File.write(File.join(File.dirname(__FILE__), 'sass', 'components', '_emojisprite.scss'), css_rules.join("\n"))
puts "sprite stylesheet written to sass/components/_emojisprite.scss"