Different PangoLayout size when draw text in different languages
Hi, I'm working on a project which I need to draw text in multi languages using PangoCairo. The issue is with different languages (but same Y position, font family, font size, no scale), PangoLayout (as shown as blue line in the image, drawn by using width, height from logical extent from pango_layout_get_pixels_extent()) seem to have different height, this lead to the text of different languages have different Y position even they have the same Y axis position. How can I make text of different languages to have the same layout size when they have same Y position, font family and font size? Thank you!
The current result I'm getting from PangoCairo:
Here is the code to draw the image above:
void TextShape::fillTextTest(Canvas &ctx) {
PangoLayout* layout = pango_cairo_create_layout(ctx.getCairoContext());
PangoFontDescription* fontDescription = pango_font_description_new();
PangoRectangle inkExtent, logicalExtent;
ctx.save();
// Set up Pango
pango_layout_set_text(layout, this->text.c_str(), -1);
pango_font_description_set_family(fontDescription, this->fontFamily.c_str());
pango_font_description_set_style(fontDescription, PANGO_STYLE_NORMAL);
pango_font_description_set_weight(fontDescription, PANGO_WEIGHT_NORMAL);
pango_font_description_set_absolute_size(fontDescription, this->fontSize*PANGO_SCALE);
pango_layout_set_font_description(layout, fontDescription);
pango_font_description_free(fontDescription);
pango_layout_set_alignment(layout, PANGO_ALIGN_LEFT);
pango_layout_get_pixel_extents(layout, &inkExtent, &logicalExtent);
// Fill text
cairo_set_source_rgba(ctx.getCairoContext(), 0, 0, 0, 1.0);
cairo_move_to(ctx.getCairoContext(), this->left, this->top);
pango_cairo_show_layout(ctx.getCairoContext(), layout);
// Draw the PangoLayout
cairo_set_source_rgba(ctx.getCairoContext(), 0, 0, 255, 1);
this->drawLayout(ctx, this->left, this->top, logicalExtent.width, logicalExtent.height);
ctx.restore();
// Free memory
g_object_unref(layout);
}
void TextShape::drawLayout(Canvas &ctx, double leftCorner, double topCorner, double boundingWidth, double boundingHeight) {
cairo_set_line_width(ctx.getCairoContext(), 0.2);
cairo_move_to(ctx.getCairoContext(), leftCorner, topCorner);
cairo_line_to(ctx.getCairoContext(), leftCorner + boundingWidth, topCorner);
cairo_stroke(ctx.getCairoContext());
cairo_move_to(ctx.getCairoContext(), leftCorner, topCorner);
cairo_line_to(ctx.getCairoContext(), leftCorner, topCorner + boundingHeight);
cairo_stroke(ctx.getCairoContext());
cairo_move_to(ctx.getCairoContext(), leftCorner + boundingWidth, topCorner);
cairo_line_to(ctx.getCairoContext(), leftCorner + boundingWidth, topCorner + boundingHeight);
cairo_stroke(ctx.getCairoContext());
cairo_move_to(ctx.getCairoContext(), leftCorner, topCorner + boundingHeight);
cairo_line_to(ctx.getCairoContext(), leftCorner + boundingWidth, topCorner + boundingHeight);
cairo_stroke(ctx.getCairoContext());
}
The font I'm using is Arial, font size 12.
This is what I want to achieve: