fix text clipping due to rounding

(cherry picked from commit 35500aad08fb18a904f326df2ce8c59ae6413801)
This commit is contained in:
nulano 2020-04-21 22:44:28 +02:00
parent e3450d1f6e
commit 5d57261b9c

View File

@ -612,7 +612,7 @@ font_getsize(FontObject* self, PyObject* args)
int position, advanced;
int x_max, x_min, y_max, y_min;
FT_Face face;
int xoffset, yoffset;
int x_anchor, y_anchor;
int horizontal_dir;
int mask = 0;
int load_flags;
@ -621,6 +621,7 @@ font_getsize(FontObject* self, PyObject* args)
size_t i, count;
GlyphInfo *glyph_info = NULL;
PyObject *features = Py_None;
FT_Vector pen;
/* calculate size and bearing for a given string */
@ -643,6 +644,29 @@ font_getsize(FontObject* self, PyObject* args)
FT_Glyph glyph;
face = self->face;
index = glyph_info[i].index;
if (horizontal_dir) {
pen.x = position + glyph_info[i].x_offset;
pen.y = glyph_info[i].y_offset;
position += glyph_info[i].x_advance;
advanced = PIXEL(position);
if (advanced > x_max) {
x_max = advanced;
}
} else {
pen.x = glyph_info[i].x_offset;
pen.y = position + glyph_info[i].y_offset;
position += glyph_info[i].y_advance;
advanced = PIXEL(position);
if (advanced < y_min) {
y_min = advanced;
}
}
FT_Set_Transform(face, NULL, &pen);
/* Note: bitmap fonts within ttf fonts do not work, see #891/pr#960
* Yifu Yu<root@jackyyf.com>, 2014-10-15
*/
@ -655,74 +679,24 @@ font_getsize(FontObject* self, PyObject* args)
return geterror(error);
}
FT_Get_Glyph(face->glyph, &glyph);
FT_Glyph_Get_CBox(glyph, FT_GLYPH_BBOX_SUBPIXELS, &bbox);
if (horizontal_dir) {
// adjust start position if glyph bearing extends before origin
offset = position + face->glyph->metrics.horiBearingX + glyph_info[i].x_offset;
if (offset < x_min) {
x_min = offset;
error = FT_Get_Glyph(face->glyph, &glyph);
if (error) {
return geterror(error);
}
position += glyph_info[i].x_advance;
advanced = position;
FT_Glyph_Get_CBox(glyph, FT_GLYPH_BBOX_PIXELS, &bbox);
// adjust glyph end position if bearing extends past advanced point
offset = glyph_info[i].x_advance -
glyph_info[i].x_offset -
face->glyph->metrics.horiBearingX -
face->glyph->metrics.width;
if (offset < 0) {
advanced -= offset;
}
if (advanced > x_max) {
x_max = advanced;
}
bbox.yMax += glyph_info[i].y_offset;
bbox.yMin += glyph_info[i].y_offset;
if (bbox.yMax > y_max) {
y_max = bbox.yMax;
}
if (bbox.yMin < y_min) {
y_min = bbox.yMin;
}
} else {
// NOTE: harfbuzz (called from raqm) assumes that we are using horizontal
// bearings even for vertical text and adjusts offsets to compensate as follows:
// hb-ft.cc: hb_ft_get_glyph_v_origin():
// x_offset += horiBearingX - vertBearingX;
// y_offset += horiBearingY - (-vertBearingY);
// adjust start position if glyph bearing extends before origin
offset = position + face->glyph->metrics.horiBearingY + glyph_info[i].y_offset;
if (offset > y_max) {
y_max = offset;
}
position += glyph_info[i].y_advance;
advanced = position;
// adjust glyph end position if bearing extends past advanced point
offset = glyph_info[i].y_advance -
glyph_info[i].y_offset -
face->glyph->metrics.horiBearingY +
face->glyph->metrics.height;
if (offset > 0) {
advanced -= offset;
}
if (advanced < y_min) {
y_min = advanced;
}
bbox.xMax += glyph_info[i].x_offset;
bbox.xMin += glyph_info[i].x_offset;
if (bbox.xMax > x_max) {
x_max = bbox.xMax;
}
if (bbox.xMin < x_min) {
x_min = bbox.xMin;
}
if (bbox.yMax > y_max) {
y_max = bbox.yMax;
}
if (bbox.yMin < y_min) {
y_min = bbox.yMin;
}
FT_Done_Glyph(glyph);
@ -733,20 +707,22 @@ font_getsize(FontObject* self, PyObject* args)
glyph_info = NULL;
}
x_anchor = y_anchor = 0;
if (face) {
FT_Set_Transform(face, NULL, NULL);
if (horizontal_dir) {
xoffset = 0;
yoffset = self->face->size->metrics.ascender - y_max;
x_anchor = 0;
y_anchor = PIXEL(self->face->size->metrics.ascender);
} else {
xoffset = 0;
yoffset = -y_max;
x_anchor = x_min;
y_anchor = 0;
}
}
return Py_BuildValue(
"(ii)(ii)",
PIXEL(x_max - x_min), PIXEL(y_max - y_min),
PIXEL(xoffset), PIXEL(yoffset)
(x_max - x_min), (y_max - y_min),
(-x_anchor + x_min), -(-y_anchor + y_max)
);
}
@ -777,6 +753,7 @@ font_render(FontObject* self, PyObject* args)
size_t i, count;
GlyphInfo *glyph_info;
PyObject *features = NULL;
FT_Vector pen;
if (!PyArg_ParseTuple(args, "On|izOzi:render", &string, &id, &mask, &dir, &features, &lang,
&stroke_width)) {
@ -808,10 +785,15 @@ font_render(FontObject* self, PyObject* args)
load_flags |= FT_LOAD_TARGET_MONO;
}
x = y = baseline = offset = 0;
pen.x = pen.y = x = y = baseline = offset = 0;
horizontal_dir = (dir && strcmp(dir, "ttb") == 0) ? 0 : 1;
for (i = 0; i < count; i++) {
index = glyph_info[i].index;
pen.x = x + glyph_info[i].x_offset;
pen.y = y + glyph_info[i].y_offset;
FT_Set_Transform(self->face, NULL, &pen);
error = FT_Load_Glyph(self->face, index, load_flags | FT_LOAD_RENDER);
if (error) {
return geterror(error);
@ -822,40 +804,30 @@ font_render(FontObject* self, PyObject* args)
// compute baseline and adjust start position if glyph bearing extends before origin
if (horizontal_dir) {
temp = glyph_slot->metrics.horiBearingY + glyph_info[i].y_offset;
if (temp > baseline) {
baseline = temp;
if (glyph_slot->bitmap_top > baseline) {
baseline = glyph_slot->bitmap_top;
}
temp = x + glyph_slot->metrics.horiBearingX + glyph_info[i].x_offset;
if (temp < offset) {
offset = temp;
if (glyph_slot->bitmap_left < offset) {
offset = glyph_slot->bitmap_left;
}
x += glyph_info[i].x_advance;
} else {
// NOTE: harfbuzz (called from raqm) assumes that we are using horizontal
// bearings even for vertical text and adjusts offsets to compensate as follows:
// hb-ft.cc: hb_ft_get_glyph_v_origin():
// x_offset += horiBearingX - vertBearingX;
// y_offset += horiBearingY - (-vertBearingY);
temp = -(glyph_slot->metrics.horiBearingX + glyph_info[i].x_offset);
if (temp > baseline) {
baseline = temp;
if (glyph_slot->bitmap_top > offset) {
offset = glyph_slot->bitmap_top;
}
temp = y + glyph_slot->metrics.horiBearingY + glyph_info[i].y_offset;
if (temp > offset) {
offset = temp;
if (glyph_slot->bitmap_left < baseline) {
baseline = glyph_slot->bitmap_left;
}
y += glyph_info[i].y_advance;
}
}
if (horizontal_dir) {
x = -offset;
x = (-offset + stroke_width) * 64;
y = (-baseline + (-stroke_width)) * 64;
} else {
y = -offset;
x = (-baseline + stroke_width) * 64;
y = (-offset + (-stroke_width)) * 64;
}
if (stroker == NULL) {
@ -864,6 +836,11 @@ font_render(FontObject* self, PyObject* args)
for (i = 0; i < count; i++) {
index = glyph_info[i].index;
pen.x = x + glyph_info[i].x_offset;
pen.y = y + glyph_info[i].y_offset;
FT_Set_Transform(self->face, NULL, &pen);
error = FT_Load_Glyph(self->face, index, load_flags);
if (error) {
return geterror(error);
@ -886,19 +863,12 @@ font_render(FontObject* self, PyObject* args)
bitmap_glyph = (FT_BitmapGlyph)glyph;
bitmap = bitmap_glyph->bitmap;
xx = bitmap_glyph->left;
yy = -bitmap_glyph->top;
} else {
bitmap = glyph_slot->bitmap;
}
// NOTE: harfbuzz (called from raqm) assumes that we are using horizontal
// bearings even for vertical text and adjusts offsets to compensate as follows:
// hb-ft.cc: hb_ft_get_glyph_v_origin():
// x_offset += horiBearingX - vertBearingX;
// y_offset += horiBearingY - (-vertBearingY);
if (horizontal_dir) {
xx = PIXEL(x + glyph_slot->metrics.horiBearingX + glyph_info[i].x_offset);
} else {
xx = PIXEL(baseline + glyph_slot->metrics.horiBearingX + glyph_info[i].x_offset);
xx = glyph_slot->bitmap_left;
yy = -glyph_slot->bitmap_top;
}
x0 = 0;
@ -911,17 +881,7 @@ font_render(FontObject* self, PyObject* args)
}
source = (unsigned char*) bitmap.buffer;
for (bitmap_y = 0; bitmap_y < bitmap.rows; bitmap_y++) {
// NOTE: harfbuzz (called from raqm) assumes that we are using horizontal
// bearings even for vertical text and adjusts offsets to compensate as follows:
// hb-ft.cc: hb_ft_get_glyph_v_origin():
// x_offset += horiBearingX - vertBearingX;
// y_offset += horiBearingY - (-vertBearingY);
if (horizontal_dir) {
yy = PIXEL(baseline - glyph_slot->metrics.horiBearingY - glyph_info[i].y_offset) + bitmap_y;
} else {
yy = PIXEL(-(y + glyph_slot->metrics.horiBearingY + glyph_info[i].y_offset)) + bitmap_y;
}
for (bitmap_y = 0; bitmap_y < bitmap.rows; bitmap_y++, yy++) {
if (yy >= 0 && yy < im->ysize) {
// blend this glyph into the buffer
unsigned char *target = im->image8[yy] + xx;
@ -956,6 +916,7 @@ font_render(FontObject* self, PyObject* args)
}
}
FT_Set_Transform(self->face, NULL, NULL);
FT_Stroker_Done(stroker);
PyMem_Del(glyph_info);
Py_RETURN_NONE;