Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitaa1f4cb

Browse files
committed
Fix alpha compositing in ft2font's draw_bitmap.
The old formula (`*dst |= *src`) works fine when either dst or src isfull transparent or fully opaque, but not for compositing intermediatevalues. Fix that (while keeping a fast-path for the common case ofwriting on an empty buffer).Example (note the more uniform gray zone between the two letters):```from matplotlib import pyplot as plt, ft2font as f, cbookimport numpy as npfont = f.FT2Font(str(cbook._get_data_path("fonts/ttf/DejaVuSans.ttf")))font.set_size(24, 72)im = f.FT2Image(30, 30)ga = font.load_char(ord("A"))gv = font.load_char(ord("V"))font.draw_glyph_to_bitmap(im, 2, 2, ga)font.draw_glyph_to_bitmap(im, 12, 2, gv)(plt.figure(layout="constrained", figsize=(3, 3)) .add_subplot(xticks=[], yticks=[]) .imshow(np.asarray(im), cmap="gray"))plt.show()```
1 parentdef8fa4 commitaa1f4cb

File tree

1 file changed

+14
-4
lines changed

1 file changed

+14
-4
lines changed

‎src/ft2font.cpp

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,11 +73,20 @@ FT2Image::~FT2Image()
7373
free(m_buffer);
7474
}
7575

76-
voiddraw_bitmap(
77-
py::array_t<uint8_t, py::array::c_style> im, FT_Bitmap *bitmap, FT_Int x, FT_Int y)
76+
static std::array<uint8_t,0x10000>const alpha_cov_merge_table{[]() {
77+
auto table = std::array<uint8_t,0x10000>{};
78+
for (auto dst =0; dst <0x100; ++dst) {
79+
for (auto src =0; src <0x100; ++src) {
80+
table[(dst <<8) + src] = dst + src - (dst * src +0x7f) /0xff;
81+
}
82+
}
83+
return table;
84+
} ()};
85+
86+
void
87+
draw_bitmap(py::array_t<uint8_t, py::array::c_style> im, FT_Bitmap *bitmap, FT_Int x, FT_Int y)
7888
{
7989
auto buf = im.mutable_data(0);
80-
8190
FT_Int image_width = (FT_Int)im.shape(1);
8291
FT_Int image_height = (FT_Int)im.shape(0);
8392
FT_Int char_width = bitmap->width;
@@ -96,7 +105,8 @@ void draw_bitmap(
96105
unsignedchar *dst = buf + (i * image_width + x1);
97106
unsignedchar *src = bitmap->buffer + (((i - y_offset) * bitmap->pitch) + x_start);
98107
for (FT_Int j = x1; j < x2; ++j, ++dst, ++src)
99-
*dst |= *src;
108+
// Provide a fast-path for the common case of black background.
109+
*dst = *dst ? alpha_cov_merge_table[(*dst <<8) + *src] : *src;
100110
}
101111
}elseif (bitmap->pixel_mode == FT_PIXEL_MODE_MONO) {
102112
for (FT_Int i =y1; i < y2; ++i) {

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp