3

I used Pango with Perl and It succeed rendering a right to left text perfectly (This mission is a nightmare ):

The code :

#!/usr/bin/perl -wT

use strict;
use warnings;
use Pango;
use Encode;

my $surface = Cairo::ImageSurface->create('argb32', 400, 100);
my $cr      = Cairo::Context->create($surface);
my $layout  = Pango::Cairo::create_layout($cr);

my $text    = decode('utf8','测试');
$layout->set_text("$text");

my $font    = Pango::FontDescription->from_string ('Serif Bold 50');
$layout->set_font_description($font);

Pango::Cairo::show_layout($cr, $layout);

$surface->write_to_png('pango.png');

However, the only problem I had was within the text alignment. I have no idea how I centralize the text. I read Pango documents, but I didn't find much information. Does anyone know how to do it?

1 Answers1

2

Based on my reading of the docs I would say $layout->set_alignment('center'); but I haven't used Pango and haven't tried it.

And it seems the layout's default size wraps the content tightly so to get the centering to do something you can see you need to set the width of the layout to something that allows it to happen, for example - $layout->set_width(400)

EDIT Add set_width() paragraph

AFresh1
  • 406
  • 2
  • 5
  • Thank you AFresh1, I tried "set_alignment" before but for some reason the text alignment doesn't change and stills the same. –  Oct 06 '11 at 16:43
  • @Barakat perhaps the default width for a layout fits tightly around the text instead of fitting to the image surface and you need to `$layout->set_width(400)`? – AFresh1 Oct 06 '11 at 17:30
  • Thank you so much! now something change. The text is moved. I just need to fix the layout position becouse half the text is outside the image –  Oct 06 '11 at 18:46