diff --git a/src/ft2font.cpp b/src/ft2font.cpp index 94c554cf9f63..20fe9a083bb6 100644 --- a/src/ft2font.cpp +++ b/src/ft2font.cpp @@ -104,6 +104,16 @@ void FT2Image::resize(long width, long height) } } +static std::array const alpha_cov_merge_table{[]() { + auto table = std::array{}; + for (auto dst = 0; dst < 0x100; ++dst) { + for (auto src = 0; src < 0x100; ++src) { + table[(dst << 8) + src] = dst + src - (dst * src + 0x7f) / 0xff; + } + } + return table; +} ()}; + void FT2Image::draw_bitmap(FT_Bitmap *bitmap, FT_Int x, FT_Int y) { FT_Int image_width = (FT_Int)m_width; @@ -124,7 +134,8 @@ void FT2Image::draw_bitmap(FT_Bitmap *bitmap, FT_Int x, FT_Int y) unsigned char *dst = m_buffer + (i * image_width + x1); unsigned char *src = bitmap->buffer + (((i - y_offset) * bitmap->pitch) + x_start); for (FT_Int j = x1; j < x2; ++j, ++dst, ++src) - *dst |= *src; + // Provide a fast-path for the common case of black background. + *dst = *dst ? alpha_cov_merge_table[(*dst << 8) + *src] : *src; } } else if (bitmap->pixel_mode == FT_PIXEL_MODE_MONO) { for (FT_Int i = y1; i < y2; ++i) {