File: pango01.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 (90 lines) | stat: -rw-r--r-- 2,179 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
83
84
85
86
87
88
89
90
#!/usr/bin/perl

# This example created a PDF document using pure Pango. This is
# intended to be a reference for the documents created by the
# tl_p_01.pl test programs.

use strict;
use warnings;
use utf8;

use Pango;
use Cairo;
require "./pango00.pl";	   # subs

# Create document and graphics environment.
my $surface = Cairo::PdfSurface->create( 'pango01.pdf', 595, 842 ); # A4
my $cr = Cairo::Context->create($surface);
my $layout = Pango::Cairo::create_layout($cr);

# Scale from Cairo (PDF) units to Pango.
my $PANGO_SCALE = Pango->scale;

# Scale from Cairo (PDF) font size to Pango.
my $PANGO_FONT_SCALE = 0.75 * $PANGO_SCALE;

# Font sizes used, scaled.
my $realfontsize = 60;
my $fontsize = $realfontsize * $PANGO_FONT_SCALE;
my $tinysize = 20 * $PANGO_FONT_SCALE;

# Select a font.
my $font = Pango::FontDescription->from_string('freeserif 12');
$font->set_size($fontsize);
$layout->set_font_description($font);

# Start...
my $x = 0;
my $y = 842-500;		# Cairo goes down

# Text to render.
my $txt = qq{ Áhe <i><span foreground="red">quick</span> }.
  # $tinysize = 15360 for a 20pt font.
  qq{<span size="$tinysize"><b>brown</b></span></i> }.
  # rise is in 1/1024 units.
  qq{<span rise="10240">fox</span>}.
  # 10240/1024 units = 10pt.
  qq{<span rise="10pt" }.
  # size=46080 (45*1024) for a 60pt font.
  qq{size="46080">x</span>}.
  # size=45pt for a 60pt font.
  qq{<span rise="10pt" size="45pt">x</span> };
my $txt_nomarkup = "Áhe quick brown fox ";

$layout->set_markup($txt);

# Left align text.
$layout->set_width( 595 * $PANGO_SCALE );
$layout->set_alignment("left");

# Render it.
showlayout( $cr, $layout, $x, $y );

$y += 100;

# Right align text.
$layout->set_width( 595 * $PANGO_SCALE );
$layout->set_alignment("right");

# Render it.
showlayout( $cr, $layout, $x, $y );

$y += 100;

# Plain Cairo, no Pango.
$cr->select_font_face( "freeserif", "normal", "normal" );
$cr->set_font_size($realfontsize);
$cr->move_to( $x, $y+50 );
$cr->show_text($txt_nomarkup);

$y += 100;

# Right align text.
$layout->set_width( 595 * $PANGO_SCALE );
$layout->set_alignment("center");

# Render it.
showlayout( $cr, $layout, $x, $y );

# Ship out.
$cr->show_page;