From 9fd462077a01ff9550936b0f8a9f29bebfb5477c Mon Sep 17 00:00:00 2001 From: ismed Date: Sat, 8 Sep 2018 20:22:48 +0100 Subject: [PATCH 1/2] Add support for embedded bitmap glyphs --- kitty/freetype.c | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/kitty/freetype.c b/kitty/freetype.c index 8d12711fc..c73d8d490 100644 --- a/kitty/freetype.c +++ b/kitty/freetype.c @@ -17,6 +17,7 @@ #endif #include FT_FREETYPE_H +#include FT_BITMAP_H typedef struct { PyObject_HEAD @@ -231,6 +232,30 @@ load_glyph(Face *self, int glyph_index, int load_type) { int flags = get_load_flags(self->hinting, self->hintstyle, load_type); int error = FT_Load_Glyph(self->face, glyph_index, flags); if (error) { set_freetype_error("Failed to load glyph, with error:", error); return false; } + + // Embedded bitmap glyph? + if(self->face->glyph->bitmap.pixel_mode == FT_PIXEL_MODE_MONO) { + FT_Bitmap bitmap; + FT_Bitmap_Init(&bitmap); + + // This also sets pixel_mode to FT_PIXEL_MODE_GRAY so we don't have to + error = FT_Bitmap_Convert(library, &self->face->glyph->bitmap, &bitmap, 1); + if (error) { set_freetype_error("Failed to convert bitmap, with error:", error); return false; } + + // Normalize gray levels to the range [0..255] + bitmap.num_grays = 256; + for (uint i = 0; i < bitmap.rows; ++i) { + for (uint j = 0; j < bitmap.width; ++j) + { + unsigned char *p = &bitmap.buffer[i*bitmap.width+j]; + // We only have 2 levels + *p = *p ? 255 : 0; + } + } + error = FT_Bitmap_Copy(library, &bitmap, &self->face->glyph->bitmap); + if (error) { set_freetype_error("Failed to copy bitmap, with error:", error); return false; } + FT_Bitmap_Done(library, &bitmap); + } return true; } From 739ed906f074c8c6826fdddbecad4b8a82957b30 Mon Sep 17 00:00:00 2001 From: ismed Date: Sat, 8 Sep 2018 21:07:38 +0100 Subject: [PATCH 2/2] Use the old FT_Bitmap_New for backward compatibility --- kitty/freetype.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/kitty/freetype.c b/kitty/freetype.c index c73d8d490..a531b9492 100644 --- a/kitty/freetype.c +++ b/kitty/freetype.c @@ -236,7 +236,7 @@ load_glyph(Face *self, int glyph_index, int load_type) { // Embedded bitmap glyph? if(self->face->glyph->bitmap.pixel_mode == FT_PIXEL_MODE_MONO) { FT_Bitmap bitmap; - FT_Bitmap_Init(&bitmap); + FT_Bitmap_New(&bitmap); // This also sets pixel_mode to FT_PIXEL_MODE_GRAY so we don't have to error = FT_Bitmap_Convert(library, &self->face->glyph->bitmap, &bitmap, 1);