horizontal text - cleanup and bugfixes

This commit is contained in:
nulano 2020-04-14 01:23:53 +02:00
parent 1ff90e7bdb
commit e8dcbff64e

View File

@ -658,9 +658,9 @@ font_getsize(FontObject* self, PyObject* args)
if (i == 0) { if (i == 0) {
if (horizontal_dir) { if (horizontal_dir) {
if (face->glyph->metrics.horiBearingX < 0) { if (face->glyph->metrics.horiBearingX + glyph_info[i].x_offset < 0) {
xoffset = face->glyph->metrics.horiBearingX; x_min = x_position = face->glyph->metrics.horiBearingX +
x_position -= xoffset; glyph_info[i].x_offset;
} }
} else { } else {
if (face->glyph->metrics.vertBearingY < 0) { if (face->glyph->metrics.vertBearingY < 0) {
@ -677,6 +677,7 @@ font_getsize(FontObject* self, PyObject* args)
x_advanced = x_position; x_advanced = x_position;
offset = glyph_info[i].x_advance - offset = glyph_info[i].x_advance -
glyph_info[i].x_offset -
face->glyph->metrics.width - face->glyph->metrics.width -
face->glyph->metrics.horiBearingX; face->glyph->metrics.horiBearingX;
if (offset < 0) { if (offset < 0) {
@ -694,11 +695,6 @@ font_getsize(FontObject* self, PyObject* args)
if (bbox.yMin < y_min) { if (bbox.yMin < y_min) {
y_min = bbox.yMin; y_min = bbox.yMin;
} }
// find max distance of baseline from top
if (face->glyph->metrics.horiBearingY > yoffset) {
yoffset = face->glyph->metrics.horiBearingY;
}
} else { } else {
y_max -= glyph_info[i].y_advance; y_max -= glyph_info[i].y_advance;
@ -731,16 +727,8 @@ font_getsize(FontObject* self, PyObject* args)
if (face) { if (face) {
if (horizontal_dir) { if (horizontal_dir) {
// left bearing xoffset = x_min;
if (xoffset < 0) { yoffset = self->face->size->metrics.ascender - y_max;
x_max -= xoffset;
} else {
xoffset = 0;
}
/* difference between the font ascender and the distance of
* the baseline from the top */
yoffset = PIXEL(self->face->size->metrics.ascender - yoffset);
} else { } else {
// top bearing // top bearing
if (yoffset < 0) { if (yoffset < 0) {
@ -754,7 +742,7 @@ font_getsize(FontObject* self, PyObject* args)
return Py_BuildValue( return Py_BuildValue(
"(ii)(ii)", "(ii)(ii)",
PIXEL(x_max - x_min), PIXEL(y_max - y_min), PIXEL(x_max - x_min), PIXEL(y_max - y_min),
PIXEL(xoffset), yoffset PIXEL(xoffset), PIXEL(yoffset)
); );
} }
@ -764,7 +752,7 @@ font_render(FontObject* self, PyObject* args)
int x; int x;
unsigned int y; unsigned int y;
Imaging im; Imaging im;
int index, error, ascender, horizontal_dir; int index, error, baseline, horizontal_dir;
int load_flags; int load_flags;
unsigned char *source; unsigned char *source;
FT_Glyph glyph; FT_Glyph glyph;
@ -819,7 +807,8 @@ font_render(FontObject* self, PyObject* args)
load_flags |= FT_LOAD_TARGET_MONO; load_flags |= FT_LOAD_TARGET_MONO;
} }
ascender = 0; x = y = baseline = 0;
horizontal_dir = (dir && strcmp(dir, "ttb") == 0) ? 0 : 1;
for (i = 0; i < count; i++) { for (i = 0; i < count; i++) {
index = glyph_info[i].index; index = glyph_info[i].index;
error = FT_Load_Glyph(self->face, index, load_flags | FT_LOAD_RENDER); error = FT_Load_Glyph(self->face, index, load_flags | FT_LOAD_RENDER);
@ -830,10 +819,25 @@ font_render(FontObject* self, PyObject* args)
glyph_slot = self->face->glyph; glyph_slot = self->face->glyph;
bitmap = glyph_slot->bitmap; bitmap = glyph_slot->bitmap;
if (horizontal_dir) {
temp = glyph_slot->metrics.horiBearingY + glyph_info[i].y_offset;
if (temp > baseline) {
baseline = temp;
}
if (i == 0) {
temp = glyph_slot->metrics.horiBearingX + glyph_info[i].x_offset;
if (temp < 0) {
x = -temp;
}
}
} else {
// TODO
// baseline was called ascender and measured the descender
temp = bitmap.rows - glyph_slot->bitmap_top; temp = bitmap.rows - glyph_slot->bitmap_top;
temp -= PIXEL(glyph_info[i].y_offset); temp -= PIXEL(glyph_info[i].y_offset);
if (temp > ascender) { if (temp > baseline)
ascender = temp; baseline = temp;
} }
} }
@ -841,8 +845,6 @@ font_render(FontObject* self, PyObject* args)
load_flags |= FT_LOAD_RENDER; load_flags |= FT_LOAD_RENDER;
} }
x = y = 0;
horizontal_dir = dir && strcmp(dir, "ttb") == 0 ? 0 : 1;
for (i = 0; i < count; i++) { for (i = 0; i < count; i++) {
index = glyph_info[i].index; index = glyph_info[i].index;
error = FT_Load_Glyph(self->face, index, load_flags); error = FT_Load_Glyph(self->face, index, load_flags);
@ -874,11 +876,7 @@ font_render(FontObject* self, PyObject* args)
} }
if (horizontal_dir) { if (horizontal_dir) {
if (i == 0 && glyph_slot->metrics.horiBearingX < 0) { xx = PIXEL(x + glyph_info[i].x_offset) + left + stroke_width;
x = -glyph_slot->metrics.horiBearingX;
}
xx = PIXEL(x) + left;
xx += PIXEL(glyph_info[i].x_offset) + stroke_width;
} else { } else {
if (glyph_slot->metrics.vertBearingX < 0) { if (glyph_slot->metrics.vertBearingX < 0) {
x = -glyph_slot->metrics.vertBearingX; x = -glyph_slot->metrics.vertBearingX;
@ -898,10 +896,10 @@ font_render(FontObject* self, PyObject* args)
source = (unsigned char*) bitmap.buffer; source = (unsigned char*) bitmap.buffer;
for (bitmap_y = 0; bitmap_y < bitmap.rows; bitmap_y++) { for (bitmap_y = 0; bitmap_y < bitmap.rows; bitmap_y++) {
if (horizontal_dir) { if (horizontal_dir) {
yy = bitmap_y + im->ysize - (PIXEL(glyph_slot->metrics.horiBearingY) + ascender); yy = PIXEL(baseline - glyph_slot->metrics.horiBearingY - glyph_info[i].y_offset) +
yy -= PIXEL(glyph_info[i].y_offset) + stroke_width * 2; stroke_width + bitmap_y;
} else { } else {
yy = bitmap_y + PIXEL(y + glyph_slot->metrics.vertBearingY) + ascender; yy = bitmap_y + PIXEL(y + glyph_slot->metrics.vertBearingY) + baseline;
yy += PIXEL(glyph_info[i].y_offset); yy += PIXEL(glyph_info[i].y_offset);
} }
if (yy >= 0 && yy < im->ysize) { if (yy >= 0 && yy < im->ysize) {