Cairo/Layout.pm

filter: Statementstmt 85.7% 12 / 14 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 5 / 5 Podpod n/a totaltotal 89.4% 17 / 19 CC 1 SCAR 0.6
CC
1
Cov
86%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
« Cairo/Font.pm ↑ Summary Cairo.pm »
1 package PDF::Cairo::Layout;
2  
3 =encoding utf8
4  
5 =cut
6  
7 1 ▶use 5.016;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use Cairo;
Subroutine BEGIN: called CC=1 SCAR=2.6
12 use Pango;
13  
14 our $VERSION = "1.03";
15 $VERSION = eval $VERSION;
16 =head1 NAME
17  
18 PDF::Cairo::Layout - wrapper for Pango layouts
19  
20 =head1 SYNOPSIS
21  
22 Simplified interface for rendering L<Pango> layouts with markup in
23 L<Cairo>. All of the method arguments are in standard Cairo units; you
24 only use 'Pango units' (1/1024pt) inside markup tags (specifically,
25 size, rise, and letter_spacing). Methods that do not return an explicit
26 value return $self so they can be chained.
27  
28 use PDF::Cairo qw(cm);
29 use PDF::Cairo::Layout;
30 $pdf = PDF::Cairo->new(
31 file => "output.pdf",
32 paper => "a4",
33 landscape => 1,
34 );
35 $layout = PDF::Cairo::Layout->new($pdf);
36 $markup = qq(<big><b>big bold text</b></big>\n);
37 $markup .= qq(<span font='Ms Mincho 32' lang='ja'>日本語</span>);
38 $layout->markup($markup);
39 $layout->width(cm(8))->indent(16);
40 $pdf->move(cm(3), cm(5));
41 $layout->show;
42  
43 =head1 MARKUP
44  
45 The Pango markup format supports the following HTML-like tags: <b>,
46 <big>, <i>, <s>, <span>, <sub>, <sup>, <small>, <tt>, and <u>. Span
47 tags are the primary way to markup text, with the others used as
48 shortcuts for common styles.
49  
50 To set the font, you can either use the C<font> attribute with a
51 space-separated list of family, point size, style, variant, weight,
52 and stretch (all of which are optional), or set them individually with
53 the C<face>, C<size>, C<style>, C<weight>, C<variant>, and C<stretch>
54 attributes. Both of these methods are quirky and unpredictable, and
55 your best bet is to select a quoted string from the output of
56 F<pango-list>, add a size in points to the end, and use that as the
57 argument to C<font>.
58  
59 Size is either integers in 1/1024pt, or one of: xx-small, x-small,
60 small, medium, large, x-large, xx-large, smaller, larger. Style is
61 either normal or italic. Weight is either a numeric weight or one of:
62 ultralight, light, normal, bold, ultrabold, heavy. Variant is either
63 normal or smallcaps. Stretch is one of: ultracondensed,
64 extracondensed, condensed, semicondensed, normal, semiexpanded,
65 expanded, extraexpanded, ultraexpanded.
66  
67 Additional attributes include color (name or hex string), bgcolor,
68 alpha (1-65536 or percentage string), bgalpha, underline (none,
69 single, double, low, error), underline_color, rise (1/1024pt),
70 strikethrough (true, false), strikethrough_color, lang, and
71 letter_spacing (1/1024pt).
72  
73 Newlines are used to indicate line breaks.
74  
75 Full documentation on the markup language can be found at:
76  
77 https://developer.gnome.org/pango/unstable/PangoMarkupFormat.html
78  
79 =head1 DESCRIPTION
80  
81 =cut
82  
83 our (
84 @ISA,
85 @EXPORT,
86 @EXPORT_OK,
87 %EXPORT_TAGS,
88 );
89 BEGIN {
90 require Exporter;
91 @ISA = qw(Exporter);
92 @EXPORT = qw();
93 @EXPORT_OK = qw();
94 %EXPORT_TAGS = (all => \@EXPORT_OK);
95 }
96  
97 =head2 Methods
98  
99 =over 4
100  
101 =item B<new> $pdf_cairo_ref, %options
102  
103 =over 4
104  
105 =item alignment => 'left|center|right'
106  
107 =item ellipsize => 'none|start|middle|end'
108  
109 =item height => $height
110  
111 =item indent => $indent
112  
113 =item justify => 1|0
114  
115 =item size => [$width, $height]
116  
117 =item spacing => $spacing
118  
119 =item tabs => [tab1,...]
120  
121 =item width => $width
122  
123 =item wrap => 'word|char|word-char'
124  
125 Create a new layout. The first argument must be a L<PDF::Cairo>
126 object, in order for Pango to locate the Cairo context. Options behave
127 the same as the methods below, except that an array reference must be
128 passed for size and tabs.
129  
130 =cut
131  
132 sub new {
133 my $class = shift;
134 my $pcref = shift;
135 my %options = @_;
136 my $self = {
137 _context => $pcref->{context},
138 };
139 $self->{_layout} = Pango::Cairo::create_layout($pcref->{context});
140 bless($self, $class);
141 foreach my $key (sort keys %options) {
142 if (ref $options{$key} eq 'ARRAY') {
143 $self->$key(@{$options{$key}});
144 }else{
145 $self->$key($options{$key});
146 }
147 }
148 return $self;
149 }
150  
151 =item B<alignment> ['left|center|right']
152  
153 Sets/gets the paragraph alignment for the current layout.
154  
155 =cut
156  
157 sub alignment {
158 my $self = shift;
159 my $alignment = shift;
160 if (defined $alignment) {
161 $self->{_layout}->set_alignment($alignment);
162 }else{
163 return $self->{_layout}->get_alignment;
164 }
165 return $self;
166 }
167  
168 =item B<baseline>
169  
170 Returns the Y offset from the top of the current layout to the baseline
171 of the first line of text. Negative.
172  
173 =cut
174  
175 sub baseline {
176 my $self = shift;
177 return -1 * $self->{_layout}->get_baseline / 1024;
178 }
179  
180 =item B<ellipsize> ['none|start|middle|end']
181  
182 Get/set the ellipsis settings for the current layout.
183  
184 =cut
185  
186 sub ellipsize {
187 my $self = shift;
188 my $ellipsize = shift;
189 if (defined $ellipsize) {
190 $self->{_layout}->set_ellipsize($ellipsize);
191 }else{
192 return $self->{_layout}->get_ellipsize;
193 }
194 return $self;
195 }
196  
197 =item B<height> [$height]
198  
199 Sets/gets the height of the current layout. If negative, instead
200 sets the number of lines per paragraph to display, ellipsizing
201 the others. If zero, render exactly one line.
202  
203 =cut
204  
205 sub height {
206 my $self = shift;
207 my $height = shift;
208 my %options = @_;
209 if (defined $height) {
210 if ($height < 0) {
211 $self->{_layout}->set_height($height);
212 }else{
213 $self->{_layout}->set_height($height * 1024);
214 }
215 }else{
216 my $tmp = $self->{_layout}->get_height / 1024;
217 return $tmp < 0 ? $tmp : $tmp / 1024;
218 }
219 return $self;
220 }
221  
222 =item B<indent> [$indent]
223  
224 Sets/gets the indentation for the current layout. If negative,
225 produces a hanging indent.
226  
227 =cut
228  
229 sub indent {
230 my $self = shift;
231 my $indent = shift;
232 if (defined $indent) {
233 $self->{_layout}->set_indent($indent * 1024);
234 }else{
235 return $self->{_layout}->get_indent / 1024;
236 }
237 return $self;
238 }
239  
240 =item B<ink>
241  
242 Returns the ink extents of the current layout as a hash reference
243 containing the scalars x, y, width, height, and the array reference
244 bbox containing all four in that order with the height negated to
245 reflect that (x,y) is the offset from the upper-left corner of the
246 layout:
247  
248 {
249 x => $x,
250 y => $y,
251 width => $width,
252 height => $height,
253 bbox => [$x, $y, $width, -$height],
254 }
255  
256 Note that a trailing newline in the markup will count as extra "ink".
257  
258 =cut
259  
260 sub ink {
261 my $self = shift;
262 my ($ink, $logical) = $self->{_layout}->get_pixel_extents;
263 $ink->{y} *= -1;
264 $ink->{bbox} = [ $ink->{x}, $ink->{y}, $ink->{width}, - $ink->{height} ];
265 return $ink;
266 }
267  
268 =item B<justify> [1|0]
269  
270 Get/set the justification for the current layout.
271  
272 =cut
273  
274 sub justify {
275 my $self = shift;
276 my $justify = shift;
277 if (defined $justify) {
278 $self->{_layout}->set_justify($justify);
279 }else{
280 return $self->{_layout}->get_justify;
281 }
282 return $self;
283 }
284  
285 =item B<markup> $text
286  
287 Set the contents of the current layout to $text, which will be
288 parsed for Pango markup tags.
289  
290 =cut
291  
292 sub markup {
293 my $self = shift;
294 my $text = shift;
295 $self->{_layout}->set_markup($text);
296 return $self;
297 }
298  
299 =item B<path>
300  
301 Add the outlines of the glyphs in the current layout to the current path.
302  
303 =cut
304  
305 sub path {
306 my $self = shift;
307 Pango::Cairo::layout_path($self->{_context}, $self->{_layout});
308 return $self;
309 }
310  
311 =item B<show>
312  
313 Render layout with the upper-left corner starting at the current position.
314  
315 =cut
316  
317 sub show {
318 my $self = shift;
319 Pango::Cairo::update_layout($self->{_context}, $self->{_layout});
320 Pango::Cairo::show_layout($self->{_context}, $self->{_layout});
321 return $self;
322 }
323  
324 =item B<size> [$width, $height]
325  
326 Sets/gets the width and height of the current layout.
327  
328 =cut
329  
330 sub size {
331 my $self = shift;
332 if (@_ == 2) {
333 $self->width(shift);
334 $self->height(shift);
335 }else{
336 return ($self->width, $self->height);
337 }
338 return $self;
339 }
340  
341 =item B<spacing> $spacing
342  
343 Get/set the inter-line spacing for the current layout.
344  
345 =cut
346  
347 sub spacing {
348 my $self = shift;
349 my $spacing = shift;
350 if (defined $spacing) {
351 $self->{_layout}->set_spacing($spacing * 1024);
352 }else{
353 return $self->{_layout}->get_spacing;
354 }
355 return $self;
356 }
357  
358 =item B<tabs> [tab1,...]
359  
360 Sets/gets the tabs for the current layout. Defaults to the width of
361 eight space characters in the current font.
362  
363 =cut
364  
365 sub tabs {
366 my $self = shift;
367 my @tabs;
368 if (@_ > 0) {
369 my @tabarray;
370 foreach (@_) {
371 push(@tabarray, 0, $_ * 1024, 'left');
372 }
373 my $tmp = Pango::TabArray->new_with_positions(@tabarray);
374 $self->{_layout}->set_tabs($tmp);
375 }else{
376 my @tabarray = $self->{_layout}->get_tabs->get_tabs;
377 my @tabs;
378 foreach my $tab (0..@tabarray/2) {
379 push(@tabs, $tabarray[$tab * 2] / 1024);
380 }
381 return @tabs;
382 }
383 return $self;
384 }
385  
386 =item B<width> [$width]
387  
388 Sets/gets the wrap width of the current layout. If set to -1, no width
389 is set (default).
390  
391 =cut
392  
393 sub width {
394 my $self = shift;
395 my $width = shift;
396 if (defined $width) {
397 if ($width == -1) {
398 $self->{_layout}->set_width($width);
399 }else{
400 $self->{_layout}->set_width($width * 1024);
401 }
402 }else{
403 return $self->{_layout}->get_width / 1024;
404 }
405 return $self;
406 }
407  
408 =item B<wrap> ['word|char|word-char']
409  
410 Sets/gets the word-wrap settings for the current layout.
411  
412 =cut
413  
414 sub wrap {
415 my $self = shift;
416 my $wrap = shift;
417 if (defined $wrap) {
418 $self->{_layout}->set_wrap($wrap);
419 }else{
420 return $self->{_layout}->get_wrap;
421 }
422 return $self;
423 }
424  
425 =back
426  
427 =head1 AUTHOR
428  
429 J Greely, C<< <jgreely at cpan.org> >>
430  
431 =head1 SEE ALSO
432  
433 L<Pango>, L<Pango::Layout>,
434 L<Pango Markup Format|https://developer.gnome.org/pango/unstable/PangoMarkupFormat.html>
435  
436 =cut
437  
438 1;
« Cairo/Font.pm ↑ Summary Cairo.pm »