File: pango00.pl

package info (click to toggle)
libtext-layout-perl 0.045-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 548 kB
  • sloc: perl: 4,117; makefile: 2
file content (82 lines) | stat: -rw-r--r-- 1,835 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#! perl

use strict;

################ Subroutines ################

my $cr;
my $layout;
my $PANGO_SCALE = 1024;

sub showlayout {
    my ( $_cr, $_layout, $x, $y ) = @_;
    $cr = $_cr;
    $layout = $_layout;
    $cr->move_to( $x, $y );
    $cr->set_source_rgba( 0, 0, 0, 1 );
    Pango::Cairo::show_layout( $cr, $layout );
    my $dx = ($layout->get_size)[0]/$PANGO_SCALE;
    showbb( $x, $y );
    return $dx;
}

# Shows the bounding box of the last piece of text that was rendered.
sub showbb {
    my ( $x, $y ) = @_;

    # Show origin.
    _showloc( $x, $y );

    # Bounding box, top-left coordinates.
    my @e = $layout->get_pixel_extents;
    for ( 1, 0 ) {
	printf( "%-7s %6.2f %6.2f %6.2f %6.2f\n",
		(qw(Ink: Layout:))[$_],
		@{%{e[$_]}}{qw( x y width height )} );
    }

    # NOTE: Some fonts include natural spacing in the bounding box.
    # NOTE: Some fonts exclude accents on capitals from the bounding box.

    # Show baseline.
    $cr->save;
    $cr->set_source_rgb(1,0,1);
    $cr->set_line_width( 0.25 );
    $cr->translate( $x, $y );

    my %e = %{$e[1]};
    _line( $e{x}, $layout->get_baseline/$PANGO_SCALE, $e{width}, 0 );
    # Show BBox.
    $cr->rectangle( $e{x}, $e{y}, $e{width}, $e{height} );;
    $cr->stroke;
    %e = %{$e[0]};
    $cr->set_source_rgb(0,1,1);
    $cr->rectangle( $e{x}, $e{y}, $e{width}, $e{height} );;
    $cr->stroke;
    $cr->restore;

}

sub _showloc {
    my ( $x, $y, $d ) = @_;
    $x ||= 0; $y ||= 0; $d ||= 50;
    $cr->save;
    $cr->set_source_rgb(0,0,1);
    _line( $x-$d, $y, 2*$d, 0 );
    _line( $x, $y-$d, 0, 2*$d );
    $cr->restore;
}

sub _line {
    my ( $x, $y, $w, $h, $lw ) = @_;
    $lw ||= 0.5;
    $y = $y;
    $cr->save;
    $cr->move_to( $x, $y );
    $cr->rel_line_to( $w, $h );
    $cr->set_line_width($lw);
    $cr->stroke;
    $cr->restore;
}

1;