File: tl_c_02.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 (194 lines) | stat: -rw-r--r-- 5,066 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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#!/usr/bin/perl

use strict;
use warnings;
use utf8;

use lib "../lib";
use PDF::API2;
use Text::Layout;
use Text::Layout::FontConfig;
use HarfBuzz::Shaper 0.026;

# Create document and graphics environment.
my $pdf = PDF::API2->new();
$pdf->mediabox( 595, 842 );	# A4

# Set up page and get the text context.
my $page = $pdf->page;
my $text = $page->text;
my $gfx  = $page->gfx;

# Create a layout instance.
my $layout = Text::Layout->new($pdf);

my $PANGO_SCALE;

sub main {
    # Select a font.
    my $font = Text::Layout::FontConfig->from_string("Sans 44");
    $layout->set_font_description($font);

    # Start...
    my $x = 0;
    my $y = 700;

    # Text to render.
    $layout->set_markup( q{ Áhe <i><span foreground="red">quick</span> <span size="20"><b>brown</b></span></i> fox } );

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

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

    $y -= 100;

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

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

    $y -= 100;

    $text->font( $font->{font}, 44);
    $text->translate( $x, $y-50 );
    my $txt_nomarkup = q{Áhe quick brown fox};
    my $dx = $text->text($txt_nomarkup);
    if ( $font->{font}->can("extents") ) {
	my $e = $font->{font}->extents( $txt_nomarkup, 44 );
	printf( "EXT: %.2f %.2f %.2f %.2f\n", @$e{qw( x y width height )} );
	$gfx->save;
	$gfx->translate( $x, $y-50 );
	# PDF::API2 text is baseline oriented, so are the extents.
	# So we can draw the BB at the same origin as the text.
	$gfx->rect( $e->{x}, $e->{y}, $e->{width}, $e->{height} );
	$gfx->linewidth(0.5);
	$gfx->strokecolor("cyan");
	$gfx->stroke;
	$gfx->restore;
    }
    # Draw baseline.
    $gfx->save;
    $gfx->translate( $x, $y-50 );
    $gfx->move( 0, 0 );
    $gfx->line( $dx, 0 );
    $gfx->linewidth(0.5);
    $gfx->strokecolor("magenta");
    $gfx->stroke;
    $gfx->restore;

    $y -= 100;

    # Text to render.
#    $layout->set_markup( q{Áhe quick brown fox} );

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

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

    $y -= 100;

    # This will only work properly with the HarfBuzz driver.
    $font = Text::Layout::FontConfig->from_string("Deva 60");
    $layout->set_font_description($font);
    $layout->set_width( 595 * $PANGO_SCALE );
    # Nepali is LTR.
    $layout->set_alignment("left");

    # This text consists of 6 characters but will render 4 glyphs.
    my $phrase =
      "\N{DEVANAGARI LETTER TA}".
      "\N{DEVANAGARI LETTER MA}".
      "\N{DEVANAGARI VOWEL SIGN AA}".
      "\N{DEVANAGARI LETTER NGA}".
      "\N{DEVANAGARI SIGN VIRAMA}".
      "\N{DEVANAGARI LETTER GA}".
      qq{ <span font="sans 20"> this should look like THIS</span>};
    $layout->set_markup($phrase);
    showlayout( $x, $y );

    # Ship out.
    $pdf->saveas("tl_c_02.pdf");
}

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

sub showlayout {
    my ( $x, $y ) = @_;
    $layout->show( $x, $y, $text);
    $layout->showbb($gfx);
}

sub setup_fonts {
    $^O =~ /mswin/i ? setup_fonts_windows() : setup_fonts_linux();
}

sub setup_fonts_linux {
    # Register all corefonts. Useful for fallback.
    # Not required, skip if you have your own fonts.
    my $fd = Text::Layout::FontConfig->new;
    # $fd->register_corefonts;

    # Add font dir and register fonts.
    $fd->add_fontdirs( ".", $ENV{HOME}."/.fonts", "/usr/share/fonts/" );

    # Add a Sans family.
    $fd->register_font( "DejaVuSans.ttf",             "Sans"               );
    $fd->register_font( "DejaVuSans-Bold.ttf",        "Sans", "Bold"       );
    $fd->register_font( "DejaVuSans-Oblique.ttf",     "Sans", "Italic"     );
    $fd->register_font( "DejaVuSans-BoldOblique.ttf", "Sans", "BoldItalic" );

    # Add Devanagari. Requires shaping.
    # Note that Nepali is a LTR language.
    $fd->register_font( "Lohit-Devanagari.ttf",
			"Deva", "", "",
			{ shaping => 1,
			  language => 'nepali'
			} );

}

sub setup_fonts_windows {
    # Register all corefonts. Useful for fallback.
    # Not required, skip if you have your own fonts.
    my $fd = Text::Layout::FontConfig->new;
    # $fd->register_corefonts;

    # Add font dir and register fonts.
    $fd->add_fontdirs( ".", "C:\\Windows\\Fonts" );
    $fd->register_font( "arial.ttf",   "sans", "" );
    $fd->register_font( "arialbd.ttf",  "sans", "bold" );
    $fd->register_font( "ariali.ttf",  "sans", "italic" );
    $fd->register_font( "arialbi.ttf", "sans", "bolditalic" );

    # Add Devanagari. Requires shaping.
    # Note that Nepali is a LTR language.
    $fd->register_font( "Lohit-Devanagari.ttf",
			"Deva", "", "",
			{ shaping => 1,
			  language => 'nepali'
			} );

}

################ Main entry point ################

# Setup the fonts.
setup_fonts();

if ( @ARGV ) {
    # For compliancy, use Pango units;
    $PANGO_SCALE = $layout->set_pango_mode("on");
}
else {
    $PANGO_SCALE = 1;
}

main();