Gnome2 :: Canvas上的垂直文本(Perl / Gtk2)

时间:2018-08-20 11:57:53

标签: perl canvas gtk2

在Gnome2 :: Canvas上绘制,我想绘制X和Y轴,并带有水平标签和垂直标签。

Gnome2 :: Canvas :: Text对象允许Pango标记。这样的Pango标签应该可以,但不能。

<span gravity="west">...</span>

有什么方法可以克服Gtk不愿绘制旋转文本的情况?

#!/usr/bin/perl
package vertical;

use strict;
use diagnostics;
use warnings;

use Gtk2 '-init';
use Gnome2::Canvas;

my $window = Gtk2::Window->new('toplevel');
$window->set_title('Hello world');
$window->set_position('center');
$window->set_default_size(500, 500);
$window->signal_connect('delete-event' => sub {

    Gtk2->main_quit();
    exit;
});

my $canvasFrame = Gtk2::Frame->new(undef);
$window->add($canvasFrame);
$canvasFrame->set_border_width(3);

my $canvasScroller = Gtk2::ScrolledWindow->new();
$canvasFrame->add($canvasScroller);
$canvasScroller->set_border_width(3);
$canvasScroller->set_policy('always','always');

my $canvas = Gnome2::Canvas->new();
$canvasScroller->add($canvas);
$canvas->set_scroll_region(0, 0, 400, 400);
$canvas->set_center_scroll_region(1);

# White background
my $canvasObj = Gnome2::Canvas::Item->new (
    $canvas->root(),
    'Gnome2::Canvas::Rect',
    x1 => 0,
    y1 => 0,
    x2 => 400,
    y2 => 400,
    fill_color => '#FFFFFF',
    outline_color => '#FFFFFF',
);
$canvasObj->lower_to_bottom();

# Black text
my $canvasObj2 = Gnome2::Canvas::Item->new(
    $canvas->root(),
    'Gnome2::Canvas::Text',
    x => 100,
    y => 300,
    fill_color => '#000000',
    font => 'Sans',
    size => 20000,
    anchor => 'GTK_ANCHOR_W',
    markup => "this is <span gravity=\"east\">not vertical</span>",
);
$canvasObj2->raise_to_top();

$window->show_all();

Gtk2->main();

1 个答案:

答案 0 :(得分:0)

我认为不支持在Pango标记中使用gravity属性,至少我无法使其工作。

但是,您可以尝试使用Gnome2::Canvas中的rotate()函数来代替Cairo::Context。这是使用Gtk3的示例:

use strict;
use warnings;
use Gtk3 '-init';
use Glib qw/TRUE FALSE/;
use Cairo;
use Pango;

use constant PI     => 3.1415927;

my $window = Gtk3::Window->new('toplevel');
$window->set_title("Cairo Basics");
$window->set_position("mouse");
$window->set_default_size(600, 400);
$window->set_border_width(5);
$window->signal_connect (delete_event => sub { Gtk3->main_quit });

my $frame = Gtk3::Frame->new("Cairo Drawings");
$window->add($frame);
my $drawable = Gtk3::DrawingArea->new;
$drawable->signal_connect( draw => sub { cairo_draw($drawable, @_ ) });
$frame->add($drawable);
$window->show_all;
Gtk3->main;

sub cairo_draw {
    my ( $drawable, $widget, $context, $ref_status ) = @_;

    my $pango_layout = $drawable->create_pango_layout("");
    $pango_layout->set_markup(
        "<span foreground = '#FF4444'"
        ." size = '20000' face='sans' style='normal' weight='bold'>"
        . "This is vertical</span>"
    );
    my $pango_context = $pango_layout->get_context();
    $context->move_to(200, 100);
    $context->rotate(PI*(90/180));
    Pango::Cairo::show_layout( $context, $pango_layout );    
    return FALSE;
}

enter image description here

或者,您可以完全跳过Pango标记,而仅使用开罗:

sub cairo_draw {
    my ( $drawable, $widget, $context, $ref_status ) = @_;

    $context->set_source_rgba(1, 0.4, 0.4, 1);
    $context->select_font_face( "Sans", "normal", "bold" );
    $context->set_font_size( 35 );
    $context->move_to(220, 50);
    $context->rotate(PI*(90/180));
    $context->show_text( "This is vertical");
    return FALSE;
}