Skip to content

Commit

Permalink
enable color emoji support in freetype
Browse files Browse the repository at this point in the history
  • Loading branch information
allefant authored and SiegeLord committed Dec 28, 2024
1 parent 39d01eb commit ec00a96
Show file tree
Hide file tree
Showing 7 changed files with 519 additions and 11 deletions.
4 changes: 4 additions & 0 deletions LICENSE.txt
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,10 @@ Some Allegro examples use a font from DejaVu fonts, their license can
be found here:
<http://dejavu-fonts.org/wiki/index.php?title=License>

Some Allegro examples use a font from NotoColorEmoji fonts, their license can
be found here:
<https://fonts.google.com/noto/specimen/Noto+Color+Emoji/license>

-------------------------------------------------------------------------------

The Cosmic Protector demo game graphics were made by Tony Huisman, and are
Expand Down
54 changes: 43 additions & 11 deletions addons/ttf/ttf.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

#include <ft2build.h>
#include FT_FREETYPE_H
#include FT_TRUETYPE_TABLES_H

#include <stdlib.h>

Expand Down Expand Up @@ -289,7 +290,7 @@ static unsigned char *alloc_glyph_region(ALLEGRO_TTF_FONT_DATA *data,
}

if (lock) {
char *ptr;
unsigned char *ptr;
int i;

data->page_lr = al_lock_bitmap_region(page,
Expand All @@ -308,7 +309,7 @@ static unsigned char *alloc_glyph_region(ALLEGRO_TTF_FONT_DATA *data,
* FIXME We could clear just the border
*/
for (i = 0; i < lock_rect.h; i++) {
ptr = (char *)(data->page_lr->data) + (i * data->page_lr->pitch);
ptr = (unsigned char *)(data->page_lr->data) + (i * data->page_lr->pitch);
int j;
for (j = 0; j < lock_rect.w; ++j) {
*ptr++ = 255;
Expand All @@ -324,7 +325,7 @@ static unsigned char *alloc_glyph_region(ALLEGRO_TTF_FONT_DATA *data,
* would be faster (yet)
*/
for (i = 0; i < lock_rect.h; i++) {
ptr = (char *)(data->page_lr->data) + (i * data->page_lr->pitch);
ptr = (unsigned char *)(data->page_lr->data) + (i * data->page_lr->pitch);
memset(ptr, 0, lock_rect.w * 4);
}
}
Expand All @@ -350,7 +351,17 @@ static void copy_glyph_mono(ALLEGRO_TTF_FONT_DATA *font_data, FT_Face face,
unsigned char *dptr = glyph_data + pitch * y;
int bit = 0;

if (font_data->flags & ALLEGRO_NO_PREMULTIPLIED_ALPHA) {
if (face->glyph->bitmap.pixel_mode == FT_PIXEL_MODE_BGRA) {
for (x = 0; x < (int)face->glyph->bitmap.width; x++) {
unsigned char set = ((ptr[3] >> (7-bit)) & 1) ? 255 : 0;
*dptr++ = set;
*dptr++ = set;
*dptr++ = set;
*dptr++ = set;
ptr += 4;
}
}
else if (font_data->flags & ALLEGRO_NO_PREMULTIPLIED_ALPHA) {
/* FIXME We could just set the alpha byte since the
* region was cleared above when allocated
*/
Expand Down Expand Up @@ -393,7 +404,16 @@ static void copy_glyph_color(ALLEGRO_TTF_FONT_DATA *font_data, FT_Face face,
unsigned char const *ptr = face->glyph->bitmap.buffer + face->glyph->bitmap.pitch * y;
unsigned char *dptr = glyph_data + pitch * y;

if (font_data->flags & ALLEGRO_NO_PREMULTIPLIED_ALPHA) {
if (face->glyph->bitmap.pixel_mode == FT_PIXEL_MODE_BGRA) {
for (x = 0; x < (int)face->glyph->bitmap.width; x++) {
*dptr++ = ptr[2];
*dptr++ = ptr[1];
*dptr++ = ptr[0];
*dptr++ = ptr[3];
ptr += 4;
}
}
else if (font_data->flags & ALLEGRO_NO_PREMULTIPLIED_ALPHA) {
/* FIXME We could just set the alpha byte since the
* region was cleared above when allocated
*/
Expand Down Expand Up @@ -443,10 +463,19 @@ static void cache_glyph(ALLEGRO_TTF_FONT_DATA *font_data, FT_Face face,

// FIXME: make this a config setting? FT_LOAD_FORCE_AUTOHINT

// FIXME: Investigate why some fonts don't work without the
// NO_BITMAP flags. Supposedly using that flag makes small sizes
// look bad so ideally we would not used it.
ft_load_flags = FT_LOAD_RENDER | FT_LOAD_NO_BITMAP;
ft_load_flags = FT_LOAD_RENDER;
if (face->num_fixed_sizes) {
// This is a bitmap font with fixed sizes, let's load them in
// color if available.
ft_load_flags |= FT_LOAD_COLOR;
}
else {
// FIXME: Investigate why some fonts don't work without the
// NO_BITMAP flags. Supposedly using that flag makes small sizes
// look bad so ideally we would not used it.
ft_load_flags |= FT_LOAD_NO_BITMAP ;
}

if (font_data->flags & ALLEGRO_TTF_MONOCHROME)
ft_load_flags |= FT_LOAD_TARGET_MONO;
if (font_data->flags & ALLEGRO_TTF_NO_AUTOHINT)
Expand All @@ -472,7 +501,7 @@ static void cache_glyph(ALLEGRO_TTF_FONT_DATA *font_data, FT_Face face,
/* Even though this glyph has no "region", include the 2-pixel border in the size */
glyph->region.w = w + 4;
glyph->region.h = h + 4;
ALLEGRO_DEBUG("Glyph %d has zero size.\n", ft_index);
ALLEGRO_DEBUG("Glyph %d has zero size. (pixel mode %d)\n", ft_index, face->glyph->bitmap.pixel_mode);
return;
}

Expand Down Expand Up @@ -950,7 +979,10 @@ ALLEGRO_FONT *al_load_ttf_font_stretch_f(ALLEGRO_FILE *file,
}
al_destroy_path(path);

if (h > 0) {
if (face->num_fixed_sizes) {
// TODO: we always pick the first size
FT_Select_Size(face, 0);
} else if (h > 0) {
FT_Set_Pixel_Sizes(face, w, h);
}
else {
Expand Down
2 changes: 2 additions & 0 deletions examples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ set(DATA_IMAGES

set(DATA_TTF
DejaVuSans.ttf
NotoColorEmoji_Animals.ttf
a4_font.fnt
)

Expand Down Expand Up @@ -145,6 +146,7 @@ example(ex_draw ${FONT} ${IMAGE} ${COLOR} ${PRIM} ${DATA_IMAGES})
example(ex_draw_bitmap ${IMAGE} ${FONT} ${DATA_IMAGES})
example(ex_drawpixels)
example(ex_dualies ${IMAGE})
example(ex_emoji ${TTF} ${COLOR} ${PRIM} ${IMAGE} DATA ${DATA_TTF})
example(ex_expose ${IMAGE} ${PRIM} ${DATA_IMAGES})
example(ex_filter ${FONT} ${IMAGE} ${COLOR} ${DATA_IMAGES})
example(ex_fs_resize ${IMAGE} ${PRIM} ${DATA_IMAGES})
Expand Down
38 changes: 38 additions & 0 deletions examples/data/NotoColorEmoji.LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
Noto Color Emoji
License
Copyright 2021 Google Inc. All Rights Reserved.

This Font Software is licensed under the SIL Open Font License, Version 1.1 . This license is copied below, and is also available with a FAQ at: https://openfontlicense.org

SIL OPEN FONT LICENSE Version 1.1 - 26 February 2007
PREAMBLE

The goals of the Open Font License (OFL) are to stimulate worldwide development of collaborative font projects, to support the font creation efforts of academic and linguistic communities, and to provide a free and open framework in which fonts may be shared and improved in partnership with others.

The OFL allows the licensed fonts to be used, studied, modified and redistributed freely as long as they are not sold by themselves. The fonts, including any derivative works, can be bundled, embedded, redistributed and/or sold with any software provided that any reserved names are not used by derivative works. The fonts and derivatives, however, cannot be released under any other type of license. The requirement for fonts to remain under this license does not apply to any document created using the fonts or their derivatives.
DEFINITIONS

"Font Software" refers to the set of files released by the Copyright Holder(s) under this license and clearly marked as such. This may include source files, build scripts and documentation.

"Reserved Font Name" refers to any names specified as such after the copyright statement(s).
"Original Version" refers to the collection of Font Software components as distributed by the Copyright Holder(s).

"Modified Version" refers to any derivative made by adding to, deleting, or substituting -- in part or in whole -- any of the components of the Original Version, by changing formats or by porting the Font Software to a new environment.

"Author" refers to any designer, engineer, programmer, technical writer or other person who contributed to the Font Software.
PERMISSION & CONDITIONS

Permission is hereby granted, free of charge, to any person obtaining a copy of the Font Software, to use, study, copy, merge, embed, modify, redistribute, and sell modified and unmodified copies of the Font Software, subject to the following conditions:

Neither the Font Software nor any of its individual components, in Original or Modified Versions, may be sold by itself.
Original or Modified Versions of the Font Software may be bundled, redistributed and/or sold with any software, provided that each copy contains the above copyright notice and this license. These can be included either as stand-alone text files, human-readable headers or in the appropriate machine-readable metadata fields within text or binary files as long as those fields can be easily viewed by the user.
No Modified Version of the Font Software may use the Reserved Font Name(s) unless explicit written permission is granted by the corresponding Copyright Holder. This restriction only applies to the primary font name as presented to the users.
The name(s) of the Copyright Holder(s) or the Author(s) of the Font Software shall not be used to promote, endorse or advertise any Modified Version, except to acknowledge the contribution(s) of the Copyright Holder(s) and the Author(s) or with their explicit written permission.
The Font Software, modified or unmodified, in part or in whole, must be distributed entirely under this license, and must not be distributed under any other license. The requirement for fonts to remain under this license does not apply to any document created using the Font Software.

TERMINATION

This license becomes null and void if any of the above conditions are not met.
DISCLAIMER

THE FONT SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO ANY WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF COPYRIGHT, PATENT, TRADEMARK, OR OTHER RIGHT. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, INCLUDING ANY GENERAL, SPECIAL, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF THE USE OR INABILITY TO USE THE FONT SOFTWARE OR FROM OTHER DEALINGS IN THE FONT SOFTWARE.
Binary file added examples/data/NotoColorEmoji_Animals.ttf
Binary file not shown.
Loading

0 comments on commit ec00a96

Please sign in to comment.