1
0
mirror of https://github.com/twitter/twemoji.git synced 2024-12-28 06:18:30 +00:00
Emoji for everyone. https://twemoji.twitter.com/
Go to file
Joen Asmussen 18f4beeb76 Make size and position of face emoji more consistent
This pull request starts work on #60. Should it be approved we'd want to export new PNGs and SVGs.

**Observations during work**

All twemoji are drawn on a 38x38 canvas in the .ai source files. All SVGs exported from these .ai files are scaled down to a 24x24px canvas. This explains the weirdly un-rounded size values such as 22,737px in the current SVGs.

Sizing and positioning issues are most noticeable with the "yellow faces". About half the "yellow face" emoji have a head-size of 36x36px inside the 38px canvas, and the other half had a smaller 34x34px head size.

**Specifics for the 1f60a to 1f62f range**

Some of those yellow face emoji are deliberately sized down, so as to make room for artwork. For example "Face With Stuck-Out Tongue And Winking Eye" has a 34x34px head size to allow for the tongue to stick out 2px below. Compare: https://cloudup.com/c0rSvWfAh5i. Since this is a deliberate creative choice, there's nothing to fix here, except push the emoji down 1px.

1f62c had a 34px head size. I compared the eye position with 1f62e and sized up the head of 1f62c. Compare: https://cloudup.com/cD6vRRdaJGJ

**Specifics for the 1f600 to 1f637 range**

There's a fair bit more variance in sizing and positioning of these yellow faces compared to the previous range. Head sizes range from 34px to 36px, and many are off centre. Compare: https://cloudup.com/ckZltnxmCy1

A lot of the smileys in this range almost hug the top of the canvas. I'm tempted to think this may have been in an effort to better align them with text, whose glyphs are traditionally shifted upwards. However since it's not consistent, and the base head size rule is broken often to make room for other creative choices, I think properly centering is probably the more desirable outcome here.

The devil horn smileys are shifted downwards and have 34px base heads, to make room for their horns. There's not much to fix here.

Some of the shades of yellow varied ever so slightly in these emoji. But this could be a creative choice, since some of the "lighter" yellow faces were angry. Such as 1f616 and 1f612.

**Summary and fixes applied**

As a summary, the set is in pretty good shape. Among the problematic yellow-face emoji, some of the nonstandard sizes were a creative choice (devil horns), in those cases I've done nothing.

The two most standard head sizes were 34px and 36px inside the 38px canvas. Specifically in the 1f600 to 1f637 range, it became clear that the 34px head size was both the most flexible (allowed more artwork like hearts and tears to go outside of the base shape), but also the most balanced in eye-to-forehead ratio.

For that reason, I've done the following to standardize:

- In cases where this size was off (such as 36px), I've standardized on the 34px head size
- In cases where emoji were off centre for no seemingly good reason, I've centered them.
- In a very few cases I had to move an eyebrow so as to keep the balanced with the slightly smaller head size, f.x. 1f62f

Where I've performed these fixes I've taken great care in ensuring that eyes, mouth and expressions didn't suddenly fall off the pixel grid, and made sure to compare with eye-sizes of similar emoji.

Here are some before/after's: https://cloudup.com/cCyeVpudNRd
2015-04-09 10:05:44 +02:00
16x16 fixed wrong 1f567 2014-11-15 00:42:35 +00:00
36x36 fixed wrong 1f567 2014-11-15 00:42:35 +00:00
72x72 fixed wrong 1f567 2014-11-15 00:42:35 +00:00
assets Make size and position of face emoji more consistent 2015-04-09 10:05:44 +02:00
svg updated svg flags too 2014-11-21 18:41:12 -05:00
.gitignore Add component.json 2014-11-08 01:31:51 +09:00
.npmignore bumped version after ignoring svg.sh 2014-12-01 10:36:21 +00:00
.travis.yml Add missing dot files 2014-11-06 16:44:23 -06:00
bower.json added SELECT to the list of ignored nodes 2015-03-13 10:26:31 +00:00
component.json added SELECT to the list of ignored nodes 2015-03-13 10:26:31 +00:00
CONTRIBUTING.md Initial open source release 2014-11-06 16:32:19 -06:00
index.html Fix GitHub spelling. [ci skip] 2015-01-09 01:16:23 +08:00
LICENSE Initial open source release 2014-11-06 16:32:19 -06:00
LICENSE-GRAPHICS Initial open source release 2014-11-06 16:32:19 -06:00
package.json added SELECT to the list of ignored nodes 2015-03-13 10:26:31 +00:00
preview-generator.js Initial open source release 2014-11-06 16:32:19 -06:00
preview-svg.html optimized SVGs size + added responsive viewport 2014-11-13 10:48:03 +00:00
preview-template.html optimized SVGs size + added responsive viewport 2014-11-13 10:48:03 +00:00
preview.html optimized SVGs size + added responsive viewport 2014-11-13 10:48:03 +00:00
README.md added ex-Twitter under my name 2015-04-08 15:19:38 +01:00
svg.sh improved the example to exclude ®, © and ™ 2014-12-19 14:56:16 +00:00
test.html Initial open source release 2014-11-06 16:32:19 -06:00
test.js do not change nodes if not needed 2015-03-12 20:41:20 +00:00
testrunner.js Initial open source release 2014-11-06 16:32:19 -06:00
twemoji-dist.js avoid errors when required via node and no location is present 2014-11-07 10:02:24 +00:00
twemoji-generator.js added SELECT to the list of ignored nodes 2015-03-13 10:25:20 +00:00
twemoji.amd.js added SELECT to the list of ignored nodes 2015-03-13 10:25:20 +00:00
twemoji.js added SELECT to the list of ignored nodes 2015-03-13 10:25:20 +00:00
twemoji.min.js updated minified version 2015-03-29 17:23:28 +02:00
twemoji.npm.js added SELECT to the list of ignored nodes 2015-03-13 10:25:20 +00:00

Twitter Emoji (Twemoji) Build Status

A simple library that provides standard Unicode emoji support across all platforms.

CDN Support

The folks over at MaxCDN graciously provide CDN support.

Just use the following in the <head> tag of your HTML document(s):

<script src="//twemoji.maxcdn.com/twemoji.min.js"></script>

API

Following all methods exposed through the twemoji namespace.

twemoji.parse( ... )

This is the main parsing utility and has 3 overloads per each parsing type.

There are mainly two kind of parsing: string parsing and DOM parsing.

Each of them accept a callback to generate each image source or an options object with parsing info.

Here is a walk through all parsing possibilities:

string parsing

Given a generic string, it will replace all emoji with an <img> tag.

While this can be used to inject via innerHTML emoji image tags, please note that this method does not sanitize the string or prevent malicious code from being executed. As an example, if the text contains a <script> tag, this will not be converted into &lt;script&gt; since it's out of this method scope to prevent these kind of attacks.

However, for already sanitized strings, this method can be considered safe enough. Please see DOM parsing if security is one of your major concerns.

twemoji.parse('I \u2764\uFE0F emoji!');

// will produce
/*
I <img
  class="emoji"
  draggable="false"
  alt="❤️"
  src="https://twemoji.maxcdn.com/36x36/2764.png"> emoji!
*/

string parsing + callback If a callback is passed, the src attribute will be the one returned by the same callback.

twemoji.parse(
  'I \u2764\uFE0F emoji!',
  function(icon, options, variant) {
    return '/assets/' + options.size + '/' + icon + '.gif';
  }
);

// will produce
/*
I <img
  class="emoji"
  draggable="false"
  alt="❤️"
  src="/assets/36x36/2764.gif"> emoji!
*/

By default, the options.size parameter will be the string "36x36" and the variant will be an optional \uFE0F char that is usually ignored by default. If your assets include or distinguish between \u2764\uFE0F and \u2764, you might want to use such a variable.

string parsing + callback returning falsy If the callback returns "falsy values" such null, undefined, 0, false, or an empty string, nothing will change for that specific emoji.

var i = 0;
twemoji.parse(
  'emoji, m\u2764\uFE0Fn am\u2764\uFE0Fur',
  function(icon, options, variant) {
    if (i++ === 0) {
      return; // no changes made first call
    }
    return '/assets/' + icon + options.ext;
  }
);

// will produce
/*
emoji, m❤n am<img
  class="emoji"
  draggable="false"
  alt="❤️"
  src="/assets/2764.png">ur
*/

string parsing + object In case an object is passed as second parameter, the passed options object will reflect its properties.

twemoji.parse(
  'I \u2764\uFE0F emoji!',
  {
    callback: function(icon, options) {
      return '/assets/' + options.size + '/' + icon + '.gif';
    },
    size: 128
  }
);

// will produce
/*
I <img
  class="emoji"
  draggable="false"
  alt="❤️"
  src="/assets/128x128/2764.gif"> emoji!
*/
DOM parsing

Differently from string parsing, if the first argument is a HTMLElement generated image tags will replace emoji that are inside #text node only without compromising surrounding nodes or listeners, and avoiding completely the usage of innerHTML.

If security is a major concern, this parsing can be considered the safest option but with a slightly penalized performance gap due DOM operations that are inevitably costy compared to basic strings.

var div = document.createElement('div');
div.textContent = 'I \u2764\uFE0F emoji!';
document.body.appendChild(div);

twemoji.parse(document.body);

var img = div.querySelector('img');

// note the div is preserved
img.parentNode === div; // true

img.src;        // https://twemoji.maxcdn.com/36x36/2764.png
img.alt;        // \u2764\uFE0F
img.className;  // emoji
img.draggable;  // false

All other overloads described for string are available exactly same way for DOM parsing.

Object as parameter

Here the list of properties accepted by the optional object that could be passed to parse.

  {
    callback: Function,   // default the common replacer
    base: string,         // default MaxCDN
    ext: string,          // default ".png"
    className: string,    // default "emoji"
    size: string|number,  // default "36x36"
    folder: string        // in case it's specified
                          // it replaces .size info, if any
  }
callback

The function to invoke in order to generate images src.

By default it is a function like the following one:

function imageSourceGenerator(icon, options) {
  return ''.concat(
    options.base, // by default Twitter Inc. CDN
    options.size, // by default "36x36" string
    '/',
    icon,         // the found emoji as code point
    options.ext   // by default ".png"
  );
}
base

The default url is the same as twemoji.base, so if you modify the former, it will reflect as default for all parsed strings or nodes.

ext

The default image extension is the same as twemoji.ext which is ".png".

If you modify the former, it will reflect as default for all parsed strings or nodes.

className

The default class per each generated image is emoji. It is possible to specify a different one through this property.

size

The default assets size is the same as twemoji.size which is "36x36".

If you modify the former, it will reflect as default for all parsed strings or nodes.

folder

In case there is no need to specify a size. It is possible to chose a folder, as is the case of SVG emoji.

twemoji.parse(genericNode, {
  folder: 'svg',
  ext: '.svg'
});

This will generate urls such https://twemoji.maxcdn.com/svg/2764.svg instead of using a specific size based one.

Tips

Inline Styles

If you'd like to size the emoji according to the surrounding text, you can add the following CSS to your stylesheet:

img.emoji {
   height: 1em;
   width: 1em;
   margin: 0 .05em 0 .1em;
   vertical-align: -0.1em;
}

This will make sure emoji derive their width and height from the font-size of the text they're shown with. It also adds just a little bit of space before and after each emoji, and pulls them upwards a little bit for better optical alignment.

UTF-8 Character Set

To properly support emoji, the document character must be set to UTF-8. This can done by including the following meta tag in the document <head>

<meta charset="utf-8">

Exclude Characters

To exclude certain characters from being replaced by twemoji.js, call twemoji.parse() with a callback, returning false for the specific unicode icon. For example:

twemoji.parse(document.body, {
    callback: function(icon, options, variant) {
        switch ( icon ) {
            case 'a9':      // © copyright
            case 'ae':      // ® registered trademark
            case '2122':    // ™ trademark
                return false;
        }
        return ''.concat(options.base, options.size, '/', icon, options.ext);
    }
});

Build

In order to build the Unicode based standard emoji RegExp, probably the most important core feature of this library, the twemoji-generator.js file needs to perform few online and offline operations.

Once executed through node twemoji-generator.js, and after installing via npm install dependencies, this file will create a fresh new copy of all libraries.

This means that if you'd like to change or improve twemoji, you should probably do it inside the createTwemoji function at the bottom of twemoji-generator.js instead of directly in the library, unless it's not just for testing purpose.

Future Plans: Unicode 8.0 and Diversity

The twemoji project currently adheres to the 7.0 Unicode version and supports 872 different emoji. In the future, we hope to work with the community to support Unicode 8.0 additions like Diversity.

Attribution Requirements

As an open source project, attribution is critical from a legal, practical and motivational perspective in our opinion. The graphics are licensed under the CC-BY 4.0 which has a pretty good guide on best practices for attribution.

However, we consider the guide a bit onerous and as a project, will accept a mention in a project README or an 'About' section or footer on a website. In mobile applications, a common place would be in the Settings/About section (for example, see the mobile Twitter application Settings->About->Legal section). We would consider a mention in the HTML/JS source sufficient also.

Community Projects

Committers and Contributors

  • Andrea Giammarchi (ex-Twitter)
  • Chris Aniszczyk (Twitter)
  • Tom Wuttke (Twitter)
  • Joen Asmussen (WordPress)
  • Marcus Kazmierczak (WordPress)

The goal of this project is to simply provide emoji for everyone. We definitely welcome improvements and fixes, but we may not merge every pull request suggested by the community due to the simple nature of the project.

The rules for contributing are available at CONTRIBUTING.md file.

Thank you to all of our contributors.

License

Copyright 2014 Twitter, Inc and other contributors

Code licensed under the MIT License: http://opensource.org/licenses/MIT

Graphics licensed under CC-BY 4.0: https://creativecommons.org/licenses/by/4.0/