Cairo/Layout.pm untested

filter: Statementstmt 0.0% 0 / 133 Branchbran 0.0% 0 / 28 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 24 Podpod 0.0% 0 / 17 totaltotal 0.0% 0 / 202 CC 15 SCAR 18.8
CC
15
Cov
0%
height
20.0
width
12.0
new
6.0
CRAP 6.6
1 package PDF::Cairo::Layout;
2  
3 =encoding utf8
4  
5 =cut
6  
7 use 5.016;
8 use utf8;
9 use strict;
10 use warnings;
11 use Cairo;
12 use Pango;
13  
14 our $VERSION = "1.04";
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 =back
126  
127 Create a new layout. The first argument must be a L<PDF::Cairo>
128 object, in order for Pango to locate the Cairo context. Options behave
129 the same as the methods below, except that an array reference must be
130 passed for size and tabs.
131  
132 =cut
133  
134 sub new {
135 my $class = shift;
136 my $pcref = shift;
137 my %options = @_;
138 my $self = {
139 _context => $pcref->{context},
140 };
141 $self->{_layout} = Pango::Cairo::create_layout($pcref->{context});
142 bless($self, $class);
143 foreach my $key (sort keys %options) {
144 if (ref $options{$key} eq 'ARRAY') {
145 $self->$key(@{$options{$key}});
146 }else{
147 $self->$key($options{$key});
148 }
149 }
150 return $self;
151 }
152  
153 =item B<alignment> ['left|center|right']
154  
155 Sets/gets the paragraph alignment for the current layout.
156  
157 =cut
158  
159 sub alignment {
160 my $self = shift;
161 my $alignment = shift;
162 if (defined $alignment) {
163 $self->{_layout}->set_alignment($alignment);
164 }else{
165 return $self->{_layout}->get_alignment;
166 }
167 return $self;
168 }
169  
170 =item B<baseline>
171  
172 Returns the Y offset from the top of the current layout to the baseline
173 of the first line of text. Negative.
174  
175 =cut
176  
177 sub baseline {
178 my $self = shift;
179 return -1 * $self->{_layout}->get_baseline / 1024;
180 }
181  
182 =item B<ellipsize> ['none|start|middle|end']
183  
184 Get/set the ellipsis settings for the current layout.
185  
186 =cut
187  
188 sub ellipsize {
189 my $self = shift;
190 my $ellipsize = shift;
191 if (defined $ellipsize) {
192 $self->{_layout}->set_ellipsize($ellipsize);
193 }else{
194 return $self->{_layout}->get_ellipsize;
195 }
196 return $self;
197 }
198  
199 =item B<height> [$height]
200  
201 Sets/gets the height of the current layout. If negative, instead
202 sets the number of lines per paragraph to display, ellipsizing
203 the others. If zero, render exactly one line.
204  
205 =cut
206  
207 sub height {
208 my $self = shift;
209 my $height = shift;
210 my %options = @_;
211 if (defined $height) {
212 if ($height < 0) {
213 $self->{_layout}->set_height($height);
214 }else{
215 $self->{_layout}->set_height($height * 1024);
216 }
217 }else{
218 my $tmp = $self->{_layout}->get_height / 1024;
219 return $tmp < 0 ? $tmp : $tmp / 1024;
220 }
221 return $self;
222 }
223  
224 =item B<indent> [$indent]
225  
226 Sets/gets the indentation for the current layout. If negative,
227 produces a hanging indent.
228  
229 =cut
230  
231 sub indent {
232 my $self = shift;
233 my $indent = shift;
234 if (defined $indent) {
235 $self->{_layout}->set_indent($indent * 1024);
236 }else{
237 return $self->{_layout}->get_indent / 1024;
238 }
239 return $self;
240 }
241  
242 =item B<ink>
243  
244 Returns the ink extents of the current layout as a hash reference
245 containing the scalars x, y, width, height, and the array reference
246 bbox containing all four in that order with the height negated to
247 reflect that (x,y) is the offset from the upper-left corner of the
248 layout:
249  
250 {
251 x => $x,
252 y => $y,
253 width => $width,
254 height => $height,
255 bbox => [$x, $y, $width, -$height],
256 }
257  
258 Note that a trailing newline in the markup will count as extra "ink".
259  
260 =cut
261  
262 sub ink {
263 my $self = shift;
264 my ($ink, $logical) = $self->{_layout}->get_pixel_extents;
265 $ink->{y} *= -1;
266 $ink->{bbox} = [ $ink->{x}, $ink->{y}, $ink->{width}, - $ink->{height} ];
267 return $ink;
268 }
269  
270 =item B<justify> [1|0]
271  
272 Get/set the justification for the current layout.
273  
274 =cut
275  
276 sub justify {
277 my $self = shift;
278 my $justify = shift;
279 if (defined $justify) {
280 $self->{_layout}->set_justify($justify);
281 }else{
282 return $self->{_layout}->get_justify;
283 }
284 return $self;
285 }
286  
287 =item B<markup> $text
288  
289 Set the contents of the current layout to $text, which will be
290 parsed for Pango markup tags.
291  
292 =cut
293  
294 sub markup {
295 my $self = shift;
296 my $text = shift;
297 $self->{_layout}->set_markup($text);
298 return $self;
299 }
300  
301 =item B<path>
302  
303 Add the outlines of the glyphs in the current layout to the current path.
304  
305 =cut
306  
307 sub path {
308 my $self = shift;
309 Pango::Cairo::layout_path($self->{_context}, $self->{_layout});
310 return $self;
311 }
312  
313 =item B<show>
314  
315 Render layout with the upper-left corner starting at the current position.
316  
317 =cut
318  
319 sub show {
320 my $self = shift;
321 Pango::Cairo::update_layout($self->{_context}, $self->{_layout});
322 Pango::Cairo::show_layout($self->{_context}, $self->{_layout});
323 return $self;
324 }
325  
326 =item B<size> [$width, $height]
327  
328 Sets/gets the width and height of the current layout.
329  
330 =cut
331  
332 sub size {
333 my $self = shift;
334 if (@_ == 2) {
335 $self->width(shift);
336 $self->height(shift);
337 }else{
338 return ($self->width, $self->height);
339 }
340 return $self;
341 }
342  
343 =item B<spacing> $spacing
344  
345 Get/set the inter-line spacing for the current layout.
346  
347 =cut
348  
349 sub spacing {
350 my $self = shift;
351 my $spacing = shift;
352 if (defined $spacing) {
353 $self->{_layout}->set_spacing($spacing * 1024);
354 }else{
355 return $self->{_layout}->get_spacing;
356 }
357 return $self;
358 }
359  
360 =item B<tabs> [tab1,...]
361  
362 Sets/gets the tabs for the current layout. Defaults to the width of
363 eight space characters in the current font.
364  
365 =cut
366  
367 sub tabs {
368 my $self = shift;
369 my @tabs;
370 if (@_ > 0) {
371 my @tabarray;
372 foreach (@_) {
373 push(@tabarray, 0, $_ * 1024, 'left');
374 }
375 my $tmp = Pango::TabArray->new_with_positions(@tabarray);
376 $self->{_layout}->set_tabs($tmp);
377 }else{
378 my @tabarray = $self->{_layout}->get_tabs->get_tabs;
379 my @tabs;
380 foreach my $tab (0..@tabarray/2) {
381 push(@tabs, $tabarray[$tab * 2] / 1024);
382 }
383 return @tabs;
384 }
385 return $self;
386 }
387  
388 =item B<width> [$width]
389  
390 Sets/gets the wrap width of the current layout. If set to -1, no width
391 is set (default).
392  
393 =cut
394  
395 sub width {
396 my $self = shift;
397 my $width = shift;
398 if (defined $width) {
399 if ($width == -1) {
400 $self->{_layout}->set_width($width);
401 }else{
402 $self->{_layout}->set_width($width * 1024);
403 }
404 }else{
405 return $self->{_layout}->get_width / 1024;
406 }
407 return $self;
408 }
409  
410 =item B<wrap> ['word|char|word-char']
411  
412 Sets/gets the word-wrap settings for the current layout.
413  
414 =cut
415  
416 sub wrap {
417 my $self = shift;
418 my $wrap = shift;
419 if (defined $wrap) {
420 $self->{_layout}->set_wrap($wrap);
421 }else{
422 return $self->{_layout}->get_wrap;
423 }
424 return $self;
425 }
426  
427 =back
428  
429 =head1 AUTHOR
430  
431 J Greely, C<< <jgreely at cpan.org> >>
432  
433 =head1 SEE ALSO
434  
435 L<Pango>, L<Pango::Layout>,
436 L<Pango Markup Format|https://developer.gnome.org/pango/unstable/PangoMarkupFormat.html>
437  
438 =cut
439  
440 1;