Fix issues with rendering of glyphs wider than their cell
Some checks are pending
CI / Linux (python=3.14 cc=clang sanitize=1) (push) Waiting to run
CI / Linux (python=3.12 cc=gcc sanitize=0) (push) Waiting to run
CI / Linux (python=3.13 cc=gcc sanitize=1) (push) Waiting to run
CI / Linux package (push) Waiting to run
CI / Bundle test (macos-latest) (push) Waiting to run
CI / Bundle test (ubuntu-latest) (push) Waiting to run
CI / macOS Brew (push) Waiting to run
CI / Test ./dev.sh and benchmark (push) Waiting to run
CodeQL / CodeQL-Build (actions, ubuntu-latest) (push) Waiting to run
CodeQL / CodeQL-Build (c, macos-latest) (push) Waiting to run
CodeQL / CodeQL-Build (c, ubuntu-latest) (push) Waiting to run
CodeQL / CodeQL-Build (go, ubuntu-latest) (push) Waiting to run
CodeQL / CodeQL-Build (python, ubuntu-latest) (push) Waiting to run
Depscan / Scan dependencies for vulnerabilities (push) Waiting to run

Reduce the allowance for cropping glyphs wider than their cell, rather
than rescaling them, to 2/10 of the cell width, since cropping removes
ink, and test that cropped glyphs fill their cell.

FreeType: Move combining marks with their base glyph when it is rescaled
and moved into its cell, so that they stay in place relative to it.

CoreText: Rescale glyphs individually, as with FreeType, so that a
combining mark wider than the cell does not cause its base glyph to be
shrunk. Measure the width of glyphs in whole pixels, as rasterized, as
with FreeType, so that both renderers make the same decision. Never crop
colored glyphs, as with FreeType.

concat_cells: Produce RGBA bytes for 8 bit sprites, as for 32 bit ones,
and handle empty cells.
This commit is contained in:
Kovid Goyal 2026-11-16 17:28:23 +05:30
parent 2e77ef3d4b
commit 906627df8a
No known key found for this signature in database
GPG key ID: 06BC317B515ACE7C
4 changed files with 146 additions and 114 deletions

View file

@ -868,8 +868,9 @@ setup_ctx_for_alpha_mask(CGContextRef render_ctx) {
CGContextSetTextMatrix(render_ctx, CGAffineTransformIdentity);
}
// fonts, if not NULL, holds the font for each glyph, NULL meaning font
static void
render_glyphs(CTFontRef font, unsigned int width, unsigned int height, unsigned int baseline, unsigned int num_glyphs) {
render_glyphs(CTFontRef font, CTFontRef *fonts, unsigned int width, unsigned int height, unsigned int baseline, unsigned int num_glyphs) {
memset(buffers.render_buf, 0, width * height);
CGColorSpaceRef gray_color_space = CGColorSpaceCreateDeviceGray();
if (gray_color_space == NULL) fatal("Out of memory");
@ -879,7 +880,13 @@ render_glyphs(CTFontRef font, unsigned int width, unsigned int height, unsigned
if (render_ctx == NULL) fatal("Out of memory");
setup_ctx_for_alpha_mask(render_ctx);
CGContextSetTextPosition(render_ctx, 0, height - baseline);
CTFontDrawGlyphs(font, buffers.glyphs, buffers.positions, num_glyphs, render_ctx);
if (fonts) {
// Draw runs of consecutive glyphs that use the same font together
for (unsigned start = 0, end; start < num_glyphs; start = end) {
for (end = start + 1; end < num_glyphs && fonts[end] == fonts[start]; end++);
CTFontDrawGlyphs(fonts[start] ? fonts[start] : font, buffers.glyphs + start, buffers.positions + start, end - start, render_ctx);
}
} else CTFontDrawGlyphs(font, buffers.glyphs, buffers.positions, num_glyphs, render_ctx);
CGContextRelease(render_ctx);
}
@ -903,7 +910,7 @@ render_simple_text_impl(PyObject *s, const char *text, unsigned int baseline) {
}
StringCanvas ans = {.width = (size_t)ceil(x), .height = (size_t)(2 * bounding_box.size.height)};
ensure_render_space(ans.width, ans.height, num_chars);
render_glyphs(font, ans.width, ans.height, baseline, num_chars);
render_glyphs(font, NULL, ans.width, ans.height, baseline, num_chars);
ans.canvas = malloc(ans.width * ans.height);
if (ans.canvas) memcpy(ans.canvas, buffers.render_buf, ans.width * ans.height);
return ans;
@ -934,7 +941,7 @@ render_codepoint(CTFace *self, PyObject *args) {
if (CTFontSupportsColorGlyphs(self->ct_font)) {
render_color_glyph(self->ct_font, (uint8_t *)PyBytes_AS_STRING(pbuf), buffers.glyphs[0], ans.width, ans.height, baseline);
} else {
render_glyphs(self->ct_font, ans.width, ans.height, baseline, num_chars);
render_glyphs(self->ct_font, NULL, ans.width, ans.height, baseline, num_chars);
uint8_t r = (fg >> 16) & 0xff, g = (fg >> 8) & 0xff, b = fg & 0xff;
const uint8_t *last_pixel = (uint8_t *)PyBytes_AS_STRING(pbuf) + PyBytes_GET_SIZE(pbuf) - sizeof(pixel);
const uint8_t *s_limit = buffers.render_buf + canvas_width * canvas_height;
@ -1001,7 +1008,7 @@ render_sample_text(CTFace *self, PyObject *args) {
buffers.glyphs[i] = info[i].codepoint;
num_glyphs++;
}
render_glyphs(font, canvas_width, canvas_height, fcm.baseline, num_glyphs);
render_glyphs(font, NULL, canvas_width, canvas_height, fcm.baseline, num_glyphs);
uint8_t r = (fg >> 16) & 0xff, g = (fg >> 8) & 0xff, b = fg & 0xff;
const uint8_t *last_pixel = (uint8_t *)PyBytes_AS_STRING(pbuf) + PyBytes_GET_SIZE(pbuf) - sizeof(pixel);
const uint8_t *s_limit = buffers.render_buf + canvas_width * canvas_height;
@ -1178,8 +1185,6 @@ do_render(
unsigned int num_cells,
unsigned int baseline,
bool *was_colored,
bool allow_resize,
FONTS_DATA_HANDLE fg,
GlyphRenderInfo *ri) {
unsigned int canvas_width = cell_width * num_cells;
ensure_render_space(canvas_width, cell_height, num_glyphs);
@ -1204,73 +1209,83 @@ do_render(
x += hb_positions[i].x_advance * scale;
y += hb_positions[i].y_advance * scale;
}
// The horizontal extent of the ink of the glyphs, as positioned in the
// canvas, and the width of the widest glyph
CGFloat left = canvas_width, right = 0, max_width = 0;
// Resize glyphs that would bleed into neighboring cells, by drawing them
// with a smaller font. Glyphs are checked and resized individually, as in
// the FreeType renderer, so that a combining mark does not cause its base
// glyph to be shrunk, or the other way around. fonts[i] is the font
// glyph i is drawn with, NULL meaning ct_font.
RAII_ALLOC(CTFontRef, fonts, NULL);
// The left edge of the ink of the resized glyphs, as positioned in the canvas
CGFloat resized_left = 0;
if (!bold && !italic) {
for (unsigned i = 0; i < num_glyphs; i++) {
const CGRect box = buffers.boxes[i];
if (box.size.width <= 0) continue;
// The width of the glyph in whole pixels, as rasterized, which is
// what the FreeType renderer uses
const CGFloat extra = ceil(box.origin.x + box.size.width) - floor(box.origin.x) - canvas_width;
if (extra <= 1) continue;
// there exist fonts that have glyphs a little wider than their
// advances, such as wide letters that borrow space from a narrow
// neighbor. Rather than rescale, which looks bad, we just crop the
// glyph to the cell. The allowance is 2/10 of the cell width, with
// a minimum of 2px, the same as in the FreeType renderer. Colored
// glyphs are always rescaled, as with FreeType.
// See https://github.com/kovidgoyal/kitty/issues/352
if (!*was_colored && num_cells == 1 && (extra <= 2 || extra * 10 <= cell_width * 2)) continue;
if (!fonts) {
fonts = calloc(num_glyphs, sizeof(fonts[0]));
if (!fonts) fatal("Out of memory");
}
const CGFloat sz = CTFontGetSize(ct_font) * canvas_width / box.size.width;
if (debug_rendering) printf("resizing glyph: %u width: %f canvas_width: %u size: %f\n", i, box.size.width, canvas_width, sz);
fonts[i] = CTFontCreateCopyWithAttributes(ct_font, sz, NULL, NULL);
if (!fonts[i]) fatal("Out of memory");
CTFontGetBoundingRectsForGlyphs(fonts[i], kCTFontOrientationHorizontal, buffers.glyphs + i, buffers.boxes + i, 1);
resized_left = MIN(resized_left, buffers.positions[i].x + buffers.boxes[i].origin.x);
}
}
if (fonts) {
br = CGRectNull;
for (unsigned i = 0; i < num_glyphs; i++) {
if (buffers.boxes[i].size.width > 0) br = CGRectUnion(br, buffers.boxes[i]);
}
if (CGRectIsNull(br)) br = CGRectZero;
}
// The horizontal extent of the ink of the glyphs, as positioned in the canvas
CGFloat left = canvas_width, right = 0;
for (unsigned i = 0; i < num_glyphs; i++) {
if (buffers.boxes[i].size.width <= 0) continue;
left = MIN(left, buffers.positions[i].x + buffers.boxes[i].origin.x);
right = MAX(right, buffers.positions[i].x + buffers.boxes[i].origin.x + buffers.boxes[i].size.width);
max_width = MAX(max_width, buffers.boxes[i].size.width);
}
CGFloat dx = 0;
if (right > left) {
// Glyphs are checked individually, so that combining marks hanging
// outside the cell dont cause the base glyph to be shrunk
CGFloat extra = max_width - canvas_width;
// there exist fonts that have glyphs a little wider than their
// advances, such as wide letters that borrow space from a narrow
// neighbor. Rather than rescale, which looks bad, we just crop the
// glyph to the cell. The allowance is 2/9 of the cell width, with a
// minimum of 2px, the same as in the FreeType renderer.
// See https://github.com/kovidgoyal/kitty/issues/352
const bool crop = num_cells == 1 && (extra <= 2 || extra * 9 <= cell_width * 2);
if (allow_resize && !bold && !italic && extra > 1 && !crop) {
// Resize glyphs that would bleed into neighboring cells, by scaling the font size
if (debug_rendering) printf("resizing glyphs, max_width: %f canvas_width: %u\n", max_width, canvas_width);
CGFloat sz = CTFontGetSize(ct_font);
sz *= canvas_width / max_width;
CTFontRef new_font = CTFontCreateCopyWithAttributes(ct_font, sz, NULL, NULL);
bool ret = do_render(
new_font,
CTFontGetUnitsPerEm(new_font),
bold,
italic,
info,
hb_positions,
num_glyphs,
canvas,
cell_width,
cell_height,
num_cells,
baseline,
was_colored,
false,
fg,
ri);
CFRelease(new_font);
return ret;
}
// Colored glyphs are placed by render_color_glyph()
if (!*was_colored) {
if (!allow_resize && left < 0) {
// A rescaled glyph, such as a letter with marks that hang to
// the left of its cell, shift it so the marks are not cut off
dx = -left;
} else if (right > canvas_width && left > 0 && num_glyphs < 4) {
// Move the glyphs back into the canvas, as far as their left bearing allows
dx = -MIN(left, right - canvas_width);
}
for (unsigned i = 0; i < num_glyphs; i++) buffers.positions[i].x += dx;
// Colored glyphs are placed by render_color_glyph()
if (right > left && !*was_colored) {
if (resized_left < 0) {
// A resized glyph, such as a letter with marks that hang to the
// left of its cell, shift it so the marks are not cut off. The
// glyphs are shifted together, so that combining marks stay in
// place relative to their base glyph.
dx = -resized_left;
} else if (right > canvas_width && left > 0 && num_glyphs < 4) {
// Move the glyphs back into the canvas, as far as their left bearing allows
dx = -MIN(left, right - canvas_width);
}
for (unsigned i = 0; i < num_glyphs; i++) buffers.positions[i].x += dx;
}
if (*was_colored) {
render_color_glyph(ct_font, (uint8_t *)canvas, info[0].codepoint, cell_width * num_cells, cell_height, baseline);
render_color_glyph(fonts && fonts[0] ? fonts[0] : ct_font, (uint8_t *)canvas, info[0].codepoint, cell_width * num_cells, cell_height, baseline);
} else {
render_glyphs(ct_font, canvas_width, cell_height, baseline, num_glyphs);
render_glyphs(ct_font, fonts, canvas_width, cell_height, baseline, num_glyphs);
Region src = {.bottom = cell_height, .right = canvas_width}, dest = {.bottom = cell_height, .right = canvas_width};
render_alpha_mask(buffers.render_buf, canvas, &src, &dest, canvas_width, canvas_width, 0xffffff);
}
if (fonts) {
for (unsigned i = 0; i < num_glyphs; i++) {
if (fonts[i]) CFRelease(fonts[i]);
}
}
ri->canvas_width = canvas_width;
ri->rendered_width = (unsigned)ceil(x);
ri->x = 0;
@ -1293,7 +1308,7 @@ render_glyphs_in_cells(
unsigned int num_cells,
unsigned int baseline,
bool *was_colored,
FONTS_DATA_HANDLE fg,
FONTS_DATA_HANDLE fg UNUSED,
GlyphRenderInfo *ri) {
CTFace *self = (CTFace *)s;
// Emoji presentation codepoints rendered with a monochrome font must use the foreground color
@ -1301,22 +1316,7 @@ render_glyphs_in_cells(
ensure_render_space(128, 128, num_glyphs);
for (unsigned i = 0; i < num_glyphs; i++) buffers.glyphs[i] = info[i].codepoint;
return do_render(
self->ct_font,
self->units_per_em,
bold,
italic,
info,
hb_positions,
num_glyphs,
canvas,
cell_width,
cell_height,
num_cells,
baseline,
was_colored,
true,
fg,
ri);
self->ct_font, self->units_per_em, bold, italic, info, hb_positions, num_glyphs, canvas, cell_width, cell_height, num_cells, baseline, was_colored, ri);
}
// Font tables {{{

View file

@ -2615,21 +2615,6 @@ test_render_line(PyObject UNUSED *self, PyObject *args) {
Py_RETURN_NONE;
}
static uint32_t
alpha_blend(uint32_t fg, uint32_t bg) {
uint32_t r1 = (fg >> 16) & 0xFF, g1 = (fg >> 8) & 0xFF, b1 = fg & 0xFF, a = (fg >> 24) & 0xff;
uint32_t r2 = (bg >> 16) & 0xFF, g2 = (bg >> 8) & 0xFF, b2 = bg & 0xFF;
float alpha = a / 255.f;
#define mix(x) uint32_t x = ((uint32_t)(alpha * x##1 + (1.0f - alpha) * x##2)) & 0xff;
mix(r);
mix(g);
mix(b);
#undef mix
// Combine components into result color
return (0xff000000) | (r << 16) | (g << 8) | b;
}
static PyObject *
render_decoration(PyObject *self UNUSED, PyObject *args) {
const char *which;
@ -2669,7 +2654,9 @@ concat_cells(PyObject UNUSED *self, PyObject *args) {
size_t num_cells = PyTuple_GET_SIZE(cells), r, c, i;
PyObject *ans = PyBytes_FromStringAndSize(NULL, (size_t)4 * cell_width * cell_height * num_cells);
if (ans == NULL) return PyErr_NoMemory();
if (!cell_width || !cell_height || !num_cells) return ans;
pixel *dest = (pixel *)PyBytes_AS_STRING(ans);
const uint8_t bg_r = (bgcolor >> 16) & 0xff, bg_g = (bgcolor >> 8) & 0xff, bg_b = bgcolor & 0xff;
RAII_ALLOC(uint8_t, rgba, is_32_bit ? malloc((size_t)4 * cell_width) : NULL);
if (is_32_bit && !rgba) {
Py_DECREF(ans);
@ -2687,16 +2674,25 @@ concat_cells(PyObject UNUSED *self, PyObject *args) {
rgba[4 * i + 1] = (src[i] >> 16) & 0xff;
rgba[4 * i + 2] = (src[i] >> 8) & 0xff;
rgba[4 * i + 3] = src[i] & 0xff;
d[4 * i] = (bgcolor >> 16) & 0xff;
d[4 * i + 1] = (bgcolor >> 8) & 0xff;
d[4 * i + 2] = bgcolor & 0xff;
d[4 * i] = bg_r;
d[4 * i + 1] = bg_g;
d[4 * i + 2] = bg_b;
d[4 * i + 3] = 0xff;
}
blend_over_opaque(d, 4, rgba, cell_width);
dest += cell_width;
} else {
// white text with the sprite pixels as its alpha, blended onto bgcolor
uint8_t *src = (uint8_t *)s + cell_width * r;
for (i = 0; i < cell_width; i++, dest++) dest[0] = alpha_blend(0x00ffffff | ((src[i] & 0xff) << 24), bgcolor);
uint8_t *d = (uint8_t *)dest;
for (i = 0; i < cell_width; i++) {
const uint32_t a = src[i];
d[4 * i] = (uint8_t)((255 * a + bg_r * (255 - a)) / 255);
d[4 * i + 1] = (uint8_t)((255 * a + bg_g * (255 - a)) / 255);
d[4 * i + 2] = (uint8_t)((255 * a + bg_b * (255 - a)) / 255);
d[4 * i + 3] = 0xff;
}
dest += cell_width;
}
}
}

View file

@ -769,14 +769,14 @@ render_bitmap(
size_t extra = ans->width - max_width;
if (italic && extra < cell_width / 2) {
trim_borders(ans, extra);
} else if (num_cells == 1 && (extra <= 2 || extra * 9 <= cell_width * 2)) {
} else if (num_cells == 1 && (extra <= 2 || extra * 10 <= cell_width * 2)) {
// there exist fonts that have glyphs a little wider than their
// advances, such as wide letters that borrow space from a narrow
// neighbor. Rather than rescale, which looks bad, we just crop the
// bitmap to the cell. The allowance is 2/9 of the cell width,
// which is 2px for the 9px cell of a typical monospace font at
// 11pt and 96 dpi, the conditions this was originally tuned for,
// with a minimum of 2px for smaller cells.
// bitmap to the cell. The allowance is 2/10 of the cell width,
// with a minimum of 2px for smaller cells, which is what this was
// originally tuned for. Cropping removes ink, so the allowance is
// kept small.
// See https://github.com/kovidgoyal/kitty/issues/352
} else if (rescale && self->is_scalable && extra > 1) {
FT_F26Dot6 char_width = self->char_width, char_height = self->char_height;
@ -1179,9 +1179,6 @@ place_bitmap_in_canvas(
// Calculate column bounds
int32_t xoff = (int32_t)(x_offset + bm->bitmap_left);
if (debug_placement) printf(" bitmap_left: %d xoff: %d", bm->bitmap_left, xoff);
// A rescaled glyph, such as a letter with marks that hang to the left of
// its cell, shift it so the marks are not cut off
if (bm->rescaled && xoff < 0) xoff = 0;
if (xoff < 0) src.left += -xoff;
else dest.left = xoff;
// Move the dest start column back if the width overflows because of it, but only if we are not in a very long/infinite ligature
@ -1231,7 +1228,7 @@ render_glyphs_in_cells(
Face *self = (Face *)f;
bool is_emoji = *was_colored;
*was_colored = is_emoji && self->has_color;
float x = 0.f, y = 0.f;
float x = 0.f, y = 0.f, shift = 0.f;
ProcessedBitmap bm;
unsigned int canvas_width = cell_width * num_cells;
GlyphColorType colored;
@ -1256,6 +1253,13 @@ render_glyphs_in_cells(
}
}
float x_offset = x + (float)positions[i].x_offset / 64.0f;
// A rescaled glyph, such as a letter with marks that hang to the left
// of its cell, is shifted so that the marks are not cut off. The
// combining marks that follow it, which have no advance, are shifted
// with it, so that they stay in place relative to it.
if (positions[i].x_advance) shift = 0;
if (bm.rescaled && x_offset + shift + (float)bm.bitmap_left < 0) shift = -(x_offset + (float)bm.bitmap_left);
x_offset += shift;
y = (float)positions[i].y_offset / 64.0f;
if (debug_placement) printf("%d: x=%f canvas: %u", i, x_offset, canvas_width);
if ((*was_colored || self->face->glyph->metrics.width > 0) && bm.width > 0) {

View file

@ -513,17 +513,25 @@ class Rendering(FontBaseTest):
self.ae(concat_cells(3, 1, True, (cell,)), bytes((0x80, 0, 0, 0xFF, 0, 0, 0, 0xFF, 0, 0xFF, 0, 0xFF)))
self.ae(concat_cells(1, 1, True, (cell[:4], cell[4:8])), bytes((0x80, 0, 0, 0xFF, 0, 0, 0, 0xFF)))
self.ae(concat_cells(2, 1, True, (cell[:8],), 0x0000FF), bytes((0x80, 0, 0x7F, 0xFF, 0, 0, 0xFF, 0xFF)))
# alpha mask pixels are white text and the result is RGBA bytes, as above
self.ae(concat_cells(2, 1, False, (bytes((0xFF, 0)),), 0x0000FF), bytes((0xFF, 0xFF, 0xFF, 0xFF, 0, 0, 0xFF, 0xFF)))
self.ae(concat_cells(1, 1, False, (bytes((0x80,)),), 0x102030), bytes((0x87, 0x8F, 0x97, 0xFF)))
self.ae(concat_cells(0, 1, True, (b'',)), b'')
def glyph_ink(self, font_name, char, ppem):
"The number of pixel rows and columns char occupies when drawn in a single cell, and the cell width"
def cell_alpha(self, font_name, text, ppem):
"The alpha of the pixels of the first cell of text, and the cell width and height"
with setup_for_testing(size=ppem, dpi=72.0, main_face_path=self.path_for_font(font_name)) as (sprites, cell_width, cell_height):
s = Screen(None, 1, 4)
s.draw(char)
s.draw(text)
line = s.line(0)
test_render_line(line)
pos = sprite_idx_to_pos(line.sprite_at(0) & 0x7FFFFFFF, setup_for_testing.xnum, setup_for_testing.ynum)
# the low byte of each pixel is its alpha, the color bits are set even for empty pixels on macOS
alpha = bytes(p & 0xFF for p in array.array('I', sprites[pos]))
return bytes(p & 0xFF for p in array.array('I', sprites[pos])), cell_width, cell_height
def glyph_ink(self, font_name, char, ppem):
"The number of pixel rows and columns char occupies when drawn in a single cell, and the cell width"
alpha, cell_width, cell_height = self.cell_alpha(font_name, char, ppem)
rows = [y for y in range(cell_height) if any(alpha[y * cell_width : (y + 1) * cell_width])]
cols = [x for x in range(cell_width) if any(alpha[x::cell_width])]
height = rows[-1] - rows[0] + 1 if rows else 0
@ -533,9 +541,11 @@ class Rendering(FontBaseTest):
def test_small_glyph_overhang_is_not_rescaled(self):
# Fonts have glyphs whose outlines overhang their advance a little, such
# as the texture healing alternates of Monaspace and 0xProto. These must
# be drawn at full size, not shrunk to fit the cell. The expected height
# is the height of the glyph's bounding box in font units, times
# ppem / units per em, rounded down.
# be drawn at full size, not shrunk to fit the cell, and cropped to
# it, which they then fill. The expected height is the height of the
# glyph's bounding box in font units, times ppem / units per em,
# rounded down. Cropping removes ink, so it is only done for glyphs
# at most 2/10 wider than the cell.
for font_name, char, ppem, full_height in (
('CascadiaCode-Regular.otf', 'ď', 40, 31), # 1588 * 40 / 2048, outline is 7% wider than the cell
('LiberationMono-Regular.ttf', 'Ƴ', 32, 21), # 1370 * 32 / 2048, 10% wider
@ -544,7 +554,9 @@ class Rendering(FontBaseTest):
('iosevka-regular.ttf', 'ꭦ', 120, 112),
):
with self.subTest(font=font_name, char=char):
self.assertGreaterEqual(self.glyph_ink(font_name, char, ppem)[0], full_height)
height, width, cell_width = self.glyph_ink(font_name, char, ppem)
self.assertGreaterEqual(height, full_height)
self.assertEqual(width, cell_width)
def test_large_glyph_overhang_is_rescaled(self):
# Ὦ has its breathing mark and accent to the left of the letter, outside
@ -559,6 +571,26 @@ class Rendering(FontBaseTest):
# not cut off and it spans the whole cell.
self.assertGreaterEqual(width, cell_width - 1)
def test_marks_move_with_rescaled_glyph(self):
# When Ὦ is rescaled and moved into its cell, a combining dot below it
# must move with it, so that it stays centered under the letter
font = 'LiberationMono-Regular.ttf'
base, cell_width, cell_height = self.cell_alpha(font, 'Ὦ', 32)
combined = self.cell_alpha(font, 'Ὦ\u0323', 32)[0]
def ink_columns(alpha, rows):
return [x for x in range(cell_width) if any(alpha[y * cell_width + x] for y in rows)]
def center(cols):
return (cols[0] + cols[-1]) / 2
rows_with_ink = [y for y in range(cell_height) if any(base[y * cell_width : (y + 1) * cell_width])]
# the bottom row of Ω spans its width
letter = ink_columns(base, rows_with_ink[-1:])
dot = ink_columns(combined, range(rows_with_ink[-1] + 1, cell_height))
self.assertTrue(dot)
self.assertLessEqual(abs(center(dot) - center(letter)), cell_width / 8, f'dot: {dot} letter: {letter}')
def test_shaping(self):
def ss(text, font=None):