From 49bb61d215b10a4b441ed89d624360e4af607d61 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C4=81ris=20Narti=C5=A1s?= Date: Fri, 24 Feb 2023 15:47:22 +0200 Subject: [PATCH] d.vect: label vertical alignment workaround Fixes #2854 --- display/d.vect/label.c | 29 +++++++++++++++++++---------- 1 file changed, 19 insertions(+), 10 deletions(-) diff --git a/display/d.vect/label.c b/display/d.vect/label.c index cd74b3f0550..3ca28ebcc55 100644 --- a/display/d.vect/label.c +++ b/display/d.vect/label.c @@ -151,12 +151,25 @@ int process_line(int ltype, const struct line_pnts *Points, void show_label(double *px, double *py, LATTR *lattr, const char *text) { double X = *px, Y = *py; - int Xoffset, Yoffset; + int Xoffset; double xarr[5], yarr[5]; double T, B, L, R; + double ysize; + + /* Vertical alignment has to be performed in advance as returned box + * height will depend on letters in use. Shifting of Y location + * ensures that text baseline is aligned instead of just bounding box. + * This logic will break down for any vertical texts. + * A proper fix would be to obtain baseline and shift it around. */ + ysize = D_get_d_to_u_yconv() * lattr->size; + if (lattr->yref == LBOTTOM) + Y = Y + ysize * 1.4; + else if (lattr->yref == LCENTER) + Y = Y + ysize * 0.35; + else + Y = Y - ysize * 0.6; X = X + D_get_d_to_u_xconv() * 0.5 * lattr->size; - Y = Y + D_get_d_to_u_yconv() * 1.5 * lattr->size; D_pos_abs(X, Y); D_get_text_box(text, &T, &B, &L, &R); @@ -168,21 +181,17 @@ void show_label(double *px, double *py, LATTR *lattr, const char *text) R = R + D_get_d_to_u_xconv() * lattr->size / 2; Xoffset = 0; - Yoffset = 0; + if (lattr->xref == LCENTER) Xoffset = -(R - L) / 2; if (lattr->xref == LRIGHT) Xoffset = -(R - L); - if (lattr->yref == LCENTER) - Yoffset = -(B - T) / 2; - if (lattr->yref == LBOTTOM) - Yoffset = -(B - T); if (lattr->has_bgcolor || lattr->has_bcolor) { xarr[0] = xarr[1] = xarr[4] = L + Xoffset; xarr[2] = xarr[3] = R + Xoffset; - yarr[0] = yarr[3] = yarr[4] = B + Yoffset; - yarr[1] = yarr[2] = T + Yoffset; + yarr[0] = yarr[3] = yarr[4] = B; + yarr[1] = yarr[2] = T; if (lattr->has_bgcolor) { D_RGB_color(lattr->bgcolor.R, lattr->bgcolor.G, lattr->bgcolor.B); @@ -196,7 +205,7 @@ void show_label(double *px, double *py, LATTR *lattr, const char *text) D_RGB_color(lattr->color.R, lattr->color.G, lattr->color.B); } - D_pos_abs(X + Xoffset, Y + Yoffset); + D_pos_abs(X + Xoffset, Y); D_text(text); }