SVGPDF.pm

filter: Statementstmt 67.2% 289 / 430 Branchbran 37.5% 66 / 176 Conditioncond 33.3% 23 / 69 MC/DCmcdc 2.3% 1 / 42 Subroutinesub 78.1% 43 / 55 Podpod 27.5% 8 / 29 totaltotal 53.6% 430 / 801 CC 126 SCAR 42.9
CC
126
Cov
56%
handle_svg
165.1
combine_svg
156.0
draw_grid
156.0
CRAP 72.9
« SVGPDF/Svg.pm ↑ Summary SVGPDF/Parser.pm »
1 #! perl
2  
3 2 ▶use v5.26;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use Object::Pad;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 class SVGPDF;
9  
10 2 our $VERSION = '1.001';
11  
12 =head1 NAME
13  
14 SVGPDF - Create PDF XObject from SVG data
15  
16 =head1 SYNOPSIS
17  
18 my $pdf = PDF::API2->new;
19 my $svg = SVGPDF->new($pdf);
20 my $xof = $svg->process("demo.svg");
21  
22 # If all goes well, $xof is an array of hashes, each representing an
23 # XObject corresponding to the <svg> elements in the file.
24 # Get a page and graphics context.
25 my $page = $pdf->page;
26 $page->bbox( 0, 0, 595, 842 );
27 my $gfx = $pdf->gfx;
28  
29 # Place the objects.
30 my $y = 832;
31 foreach my $xo ( @$xof ) {
32 my @bb = @{$xo->{vbox}};
33 my $h = $bb[3];
34 $gfx->object( $xo->{xo}, 10, $y-$h, 1 );
35 $y -= $h;
36 }
37  
38 $pdf->save("demo.pdf");
39  
40 =head1 DESCRIPTION
41  
42 This module processes SVG data and produces one or more PDF XObjects
43 to be placed in a PDF document. This module is intended to be used
44 with L<PDF::Builder>, L<PDF::API2> and compatible PDF packages.
45  
46 The main method is process(). It takes the SVG from an input source, see
47 L</INPUT>.
48  
49 =head1 COORDINATES & UNITS
50  
51 SVG coordinates run from top-left to bottom-right.
52  
53 Dimensions without units are B<pixels>, at 96 pixels / inch. E.g.,
54 C<width="96"> means 96px (pixels) and is equal to 72pt (points) or 1in (inch).
55  
56 For font sizes, CSS defines C<em> to be equal to the font size, and
57 C<ex> is half of the font size.
58  
59 =head1 CONSTRUCTORS
60  
61 =head2 SVGPDF->new($pdf)
62  
63 In its most simple form, a new SVGPDF object can be created with a
64 single argument, the PDF document.
65  
66 There are a few optional arguments, these can be specified as
67 key/value pairs.
68  
69 =over 8
70  
71 =item C<fc>
72  
73 A reference to a callback routine to handle fonts.
74 See L</FONT HANDLER CALLBACK>.
75  
76 It may also be an array of routines which will be called in
77 sequence until one of them succeeds (returns a 'true' result).
78  
79 =item C<fontsize>
80  
81 The font size to be used for dimensions in 'ex' and 'em' units.
82  
83 Note that CSS defines 'em' to be the font size, and 'ex' half of the
84 font size.
85  
86 =item C<pagesize>
87  
88 An array reference containing the maximum width and height of the
89 resultant image.
90  
91 There is no widely accepted default for this, so we use C<[595,842]>
92 which corresponds to an ISO A4 page.
93  
94 =item C<grid>
95  
96 If not zero, a grid will be added to the image. This is mostly for
97 developing and debugging.
98  
99 The value determines the grid spacing.
100  
101 =item C<verbose>
102  
103 Verbosity of informational messages. Set to zero to silence all but
104 fatal errors.
105  
106 =item C<debug>
107  
108 Internal use only.
109  
110 =back
111  
112 For convenience, the mandatory PDF argument can also be specified with
113 a key/value pair:
114  
115 $svg = SVGPDF->new( pdf => $pdf, grid => 1, fc => \&fonthandler );
116  
117 =cut
118  
119  
120 26 ▶field $pdf :accessor :param;
Subroutine pdf: called CC=2 SCAR=7.1
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
@_ and $pdf = shift() 0 26 26
121 field $atts :accessor :param = undef;
122  
123 # Callback for font and text handling.
124 0 ▶field $fc :accessor :param = undef;
Subroutine atts: not called CC=2 SCAR=14.0
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
@_ and $atts = shift() 0 0 0
125 35 ▶field $tc :accessor :param = undef;
Subroutine fc: called CC=2 SCAR=7.1
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
@_ and $fc = shift() 0 35 35
126  
127 # If an SVG file contains more than a single SVG, the CSS applies to all.
128 336 ▶field $css :accessor;
Subroutine css: called CC=2 SCAR=7.1
Subroutine tc: called CC=2 SCAR=7.1
Pod undocumented
Pod undocumented
Branch: 2 branches
BranchTrueFalseTotal
@_ and $css = shift() 0 336 336
@_ and $tc = shift() 0 28 28
129  
130 # Font manager.
131 51 ▶field $fontmanager :accessor;
Subroutine fontmanager: called CC=2 SCAR=7.1
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
@_ and $fontmanager = shift() 0 51 51
132  
133 400 ▶field $xoforms :accessor;
Subroutine xoforms: called CC=2 SCAR=7.1
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
@_ and $xoforms = shift() 0 400 400
134 13 ▶field $defs :accessor;
Subroutine defs: called CC=2 SCAR=7.1
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
@_ and $defs = shift() 0 13 13
135  
136 # Defaults for rendering.
137 0 ▶field $pagesize :accessor;
Subroutine pagesize: not called CC=2 SCAR=14.0
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
@_ and $pagesize = shift() 0 0 0
138 0 ▶field $fontsize :accessor;
Subroutine fontsize: not called CC=2 SCAR=14.0
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
@_ and $fontsize = shift() 0 0 0
139 834 field $pxpi :mutator = 96; # pixels per inch
140 834 ▶field $ptpi :accessor = 72; # points per inch
Subroutine pxpi: called CC=1 SCAR=0.0
Pod undocumented
141  
142 # For debugging/development.
143 826 ▶field $verbose :accessor;
Subroutine ptpi: called CC=2 SCAR=7.1
Subroutine verbose: called CC=2 SCAR=7.1
Pod undocumented
Pod documented
Branch: 2 branches
BranchTrueFalseTotal
@_ and $ptpi = shift() 0 826 826
@_ and $verbose = shift() 0 3 3
144 2 ▶field $debug :accessor;
Subroutine debug: called CC=2 SCAR=7.1
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
@_ and $debug = shift() 0 2 2
145 0 ▶field $grid :accessor;
Subroutine grid: not called CC=2 SCAR=14.0
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
@_ and $grid = shift() 0 0 0
146 0 ▶field $prog :accessor;
Subroutine prog: not called CC=2 SCAR=14.0
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
@_ and $prog = shift() 0 0 0
147 0 ▶field $debug_styles :accessor;
Subroutine debug_styles: not called CC=2 SCAR=14.0
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
@_ and $debug_styles = shift() 0 0 0
148 0 ▶field $trace :accessor;
Subroutine trace: not called CC=2 SCAR=14.0
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
@_ and $trace = shift() 0 0 0
149 0 ▶field $wstokens :accessor;
Subroutine wstokens: not called CC=2 SCAR=14.0
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
@_ and $wstokens = shift() 0 0 0
150  
151 2 our $indent = "";
152  
153 2 ▶use SVGPDF::Parser;
Subroutine BEGIN: called CC=1 SCAR=0.0
154 2 ▶use SVGPDF::Element;
Subroutine BEGIN: called CC=1 SCAR=0.0
155 2 ▶use SVGPDF::CSS;
Subroutine BEGIN: called CC=1 SCAR=0.0
156 2 ▶use SVGPDF::FontManager;
Subroutine BEGIN: called CC=1 SCAR=0.0
157 2 ▶use SVGPDF::PAST;
Subroutine BEGIN: called CC=1 SCAR=0.0
158 2 ▶use SVGPDF::Colour;
Subroutine BEGIN: called CC=1 SCAR=0.0
159  
160 # The SVG elements.
161 2 ▶use SVGPDF::Circle;
Subroutine BEGIN: called CC=1 SCAR=0.0
162 2 ▶use SVGPDF::Defs;
Subroutine BEGIN: called CC=1 SCAR=0.0
163 2 ▶use SVGPDF::Ellipse;
Subroutine BEGIN: called CC=1 SCAR=0.0
164 2 ▶use SVGPDF::G;
Subroutine BEGIN: called CC=1 SCAR=0.0
165 2 ▶use SVGPDF::Image;
Subroutine BEGIN: called CC=1 SCAR=0.0
166 2 ▶use SVGPDF::Line;
Subroutine BEGIN: called CC=1 SCAR=0.0
167 2 ▶use SVGPDF::Path;
Subroutine BEGIN: called CC=1 SCAR=0.0
168 2 ▶use SVGPDF::Polygon;
Subroutine BEGIN: called CC=1 SCAR=0.0
169 2 ▶use SVGPDF::Polyline;
Subroutine BEGIN: called CC=1 SCAR=0.0
170 2 ▶use SVGPDF::Rect;
Subroutine BEGIN: called CC=1 SCAR=0.0
171 2 ▶use SVGPDF::Style;
Subroutine BEGIN: called CC=1 SCAR=0.0
172 2 ▶use SVGPDF::Svg;
Subroutine BEGIN: called CC=1 SCAR=0.0
173 2 ▶use SVGPDF::Text;
Subroutine BEGIN: called CC=1 SCAR=0.0
174 2 ▶use SVGPDF::Tspan;
Subroutine BEGIN: called CC=1 SCAR=0.0
175 2 ▶use SVGPDF::Use;
Subroutine BEGIN: called CC=1 SCAR=0.0
176  
177 ################ General methods ################
178  
179  
180 =head1 METHODS
181  
182 =cut
183  
184 # $pdf [ , fc => $callback ] [, atts => { ... } ] [, foo => bar ]
185 # pdf => $pdf [ , fc => $callback ] [, atts => { ... } ] [, foo => bar ]
186  
187 1 ▶sub BUILDARGS ( @args ) {
Subroutine BUILDARGS: called CC=5 SCAR=16.2
Pod undocumented
188 1 my $cls = shift(@args);
189  
190 # Assume first is pdf if uneven.
191 1 ▶ unshift( @args, "pdf" ) if @args % 2;
Branch: 1 branch
BranchTrueFalseTotal
if @args % 2 0 1 1
192  
193 1 my %args = @args;
194 1 @args = ();
195 1 push( @args, $_, delete $args{$_} ) for qw( pdf fc tc );
196  
197 # Flatten everything else into %atts.
198 1 ▶ my %x = %{ delete($args{atts}) // {} };
Condition50%1/2
delete $args{"atts"} // {}
l!l
10
MC/DC0%0/1
delete $args{"atts"} // {}
delete $args{"atts"}
Truth table50%1/2
A
A= delete $args{"atts"}
Aresult
11
00
199 1 $x{$_} = $args{$_} for keys(%args);
200  
201 # And store as ref.
202 1 push( @args, "atts", \%x );
203  
204 # Return new argument list.
205 1 @args;
206 }
207  
208 BUILD {
209 $debug = $atts->{debug} || 0;
210 $verbose = $atts->{verbose} // $debug;
211 $grid = $atts->{grid} || 0;
212 $prog = $atts->{prog} || 0;
213 $debug_styles = $atts->{debug_styles} || $debug > 1;
214 $trace = $atts->{trace} || 0;
215 $pagesize = $atts->{pagesize} || [ 595, 842 ];
216 $fontsize = $atts->{fontsize} || 12;
217 $wstokens = $atts->{wstokens} || 0;
218 $indent = "";
219 $xoforms = [];
220 $defs = {};
221 $fontmanager = SVGPDF::FontManager->new( svg => $self );
222 $self;
223 }
224  
225 2 =head2 process
226  
227 $xof = $svg->process( $data, %options )
228  
229 This methods gets SVG data from C<$data> and returns an array reference
230 with rendered images. See L</OUTPUT> for details.
231  
232 The input is read using File::LoadLines. See L</INPUT> for details.
233  
234 Recognized attributes in C<%options> are:
235  
236 =over 4
237  
238 =item fontsize
239  
240 The font size to be used for dimensions in 'ex' and 'em' units.
241  
242 This value overrides the value set in the constructor.
243  
244 =item combine
245  
246 An SVG can produce multiple XObjects, but sometimes these should be
247 kept as a single image.
248  
249 There are two ways to combine the image objects. This can be selected
250 by setting $opts{combine} to either C<"stacked"> or C<"bbox">.
251  
252 Type C<"stacked"> (default) stacks the images on top of each other,
253 left sides aligned. The bounding box of each object is only used to
254 obtain the width and height.
255  
256 Type C<"bbox"> stacks the images using the bounding box details. The
257 origins of the images are vertically aligned and images may protrude
258 other images when the image extends below the origin.
259  
260 =item sep
261  
262 When combining images, add additional vertical space between the
263 individual images.
264  
265 =back
266  
267 =cut
268  
269 34 ▶method process ( $data, %options ) {
Subroutine process: called CC=9 SCAR=25.0
Pod documented
270  
271 34 ▶ if ( $options{reset} ) { # for testing, mostly
Branch: 1 branch
BranchTrueFalseTotal
if ($options{"reset"}) 34 0 34
272 34 $xoforms = [];
273 }
274  
275 34 my $save_fontsize = $fontsize;
276 34 ▶ $fontsize = $options{fontsize} if $options{fontsize};
Branch: 1 branch
BranchTrueFalseTotal
if $options{"fontsize"} 0 34 34
277 # TODO: Page size
278  
279 # Load the SVG data.
280 34 my $svg = SVGPDF::Parser->new;
281 my $tree = $svg->parse_file
282 ( $data,
283 34 ▶ whitespace_tokens => $wstokens||$options{whitespace_tokens} );
Condition33%1/3
$wstokens || $options{'whitespace_tokens'}
l!l&&r!l&&!r
3400
MC/DC0%0/2
$wstokens || $options{'whitespace_tokens'}
$wstokens $options{'whitespace_tokens'}
Truth table33%1/3
A || B
A= $wstokensB= $options{'whitespace_tokens'}
ABresult
1X1
011
000
284 34 ▶ return unless $tree;
Branch: 1 branch
BranchTrueFalseTotal
unless $tree 0 34 34
285  
286 # CSS persists over svgs, but not over files.
287 34 $css = SVGPDF::CSS->new;
288  
289 # Search for svg elements and process them.
290 34 $self->search($tree);
291  
292 # Restore.
293 34 $fontsize = $save_fontsize;
294  
295 34 ▶ my $combine = $options{combine} // "none";
Condition50%1/2
$options{"combine"} // "none"
l!l
034
MC/DC0%0/1
$options{"combine"} // "none"
$options{"combine"}
Truth table50%1/2
A
A= $options{"combine"}
Aresult
11
00
296 34 ▶ if ( $combine ne "none" && @$xoforms > 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($combine ne "none" and @$xoforms > 1) 0 34 34
Condition33%1/3
$combine ne "none" and @$xoforms > 1
!ll&&!rl&&r
3400
MC/DC0%0/2
$combine ne "none" and @$xoforms > 1
$combine ne "none" @$xoforms > 1
Truth table33%1/3
A and B
A= $combine ne "none"B= @$xoforms > 1
ABresult
0X0
100
111
297 0 ▶ my $sep = $options{sep} || 0;
Condition0%0/2
$options{'sep'} || 0
l!l
00
MC/DC0%0/1
$options{'sep'} || 0
$options{'sep'}
Truth table0%0/2
A
A= $options{'sep'}
Aresult
11
00
298 0 $xoforms = $self->combine_svg( $xoforms,
299 type => $combine, sep => $sep );
300 }
301  
302 # Return (hopefully a stack of XObjects).
303 34 return $xoforms;
304 2 }
305  
306 2039 ▶method _dbg ( @args ) {
Subroutine _dbg: called CC=11 SCAR=24.2
307 2039 ▶ return unless $debug;
Branch: 1 branch
BranchTrueFalseTotal
unless $debug 0 2039 2039
308 2039 my $msg;
309 2039 ▶ if ( $args[0] =~ /\%/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args[0] =~ /\%/u) { } 590 1449 2039
310 590 $msg = sprintf( $args[0], @args[1..$#args] );
311 }
312 else {
313 1449 $msg = join( "", @args );
314 }
315 2039 ▶ if ( $msg =~ /^\+\s*(.*)/ ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($msg =~ /^\+\s*(.*)/u) { } 566 1473 2039
elsif ($msg =~ /^\-\s*(.*)/u) { } 566 907 1473
elsif ($msg =~ /^\^\s*(.*)/u) { } 34 873 907
316 566 $indent = $indent . " ";
317 566 ▶ warn( $indent, $1, "\n") if $1;
Branch: 1 branch
BranchTrueFalseTotal
if $1 566 0 566
318 }
319 elsif ( $msg =~ /^\-\s*(.*)/ ) {
320 566 ▶ warn( $indent, $1, "\n") if $1;
Branch: 1 branch
BranchTrueFalseTotal
if $1 374 192 566
321 566 ▶confess("oeps") if length($indent) < 2;
Branch: 1 branch
BranchTrueFalseTotal
if length $SVGPDF::indent < 2 0 566 566
322 566 $indent = substr( $indent, 2 );
323 }
324 elsif ( $msg =~ /^\^\s*(.*)/ ) {
325 34 $indent = "";
326 34 ▶ warn( $indent, $1, "\n") if $1;
Branch: 1 branch
BranchTrueFalseTotal
if $1 34 0 34
327 }
328 else {
329 873 ▶ warn( $indent, $msg, "\n") if $msg;
Branch: 1 branch
BranchTrueFalseTotal
if $msg 873 0 873
330 }
331 2 }
332  
333 34 ▶method search ( $content ) {
Subroutine search: called CC=5 SCAR=17.8
Pod undocumented
334  
335 # In general, we'll have an XHTML tree with one or more <sgv>
336 # elements.
337  
338 34 for ( @$content ) {
339 34 ▶ next if $_->{type} eq 't';
Branch: 1 branch
BranchTrueFalseTotal
if $_->{"type"} eq "t" 0 34 34
340 34 my $name = $_->{name};
341 34 ▶ if ( $name eq "svg" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name eq 'svg') { } 34 0 34
342 34 $indent = "";
343 34 $self->handle_svg($_);
344 # Adds results to $self->{xoforms}.
345 }
346 else {
347 # Skip recursively.
348 0 ▶ $self->_dbg( "$name (ignored)" ) unless $name eq "<>"; # top
Branch: 1 branch
BranchTrueFalseTotal
unless $name eq "<>" 0 0 0
349 0 $self->search($_->{content});
350 }
351 }
352 2 }
353  
354 34 ▶method handle_svg ( $e ) {
Subroutine handle_svg: called CC=54 SCAR=51.1
Pod undocumented
355  
356 34 $self->_dbg( "^ ==== start ", $e->{name}, " ====" );
357  
358 34 my $xo;
359 34 ▶ if ( $prog ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($prog) { } 0 34 34
360 0 $xo = SVGPDF::PAST->new( pdf => $pdf );
361 }
362 else {
363 34 $xo = $pdf->xo_form;
364 }
365 34 push( @$xoforms, { xo => $xo } );
366  
367 34 $self->_dbg("XObject #", scalar(@$xoforms) );
368 my $svg = SVGPDF::Element->new
369 ( name => $e->{name},
370 128 atts => { map { lc($_) => $e->{attrib}->{$_} } keys %{$e->{attrib}} },
371 content => $e->{content},
372 34 root => $self,
373 );
374  
375 # If there are <style> elements, these must be processed first.
376 34 my $cdata = "";
377 34 for ( $svg->get_children ) {
378 272 ▶ next unless ref($_) eq "SVGPDF::Style";
Branch: 1 branch
BranchTrueFalseTotal
unless ref $_ eq "SVGPDF::Style" 270 2 272
379 # DDumper($_->get_children) unless scalar($_->get_children) == 1;
380 2 ▶ croak("ASSERT: 1 child") unless scalar($_->get_children) == 1;
Branch: 1 branch
BranchTrueFalseTotal
unless scalar $_->get_children == 1 0 2 2
381 2 for my $t ( $_->get_children ) {
382 2 ▶ croak("# ASSERT: non-text child in style")
Branch: 1 branch
BranchTrueFalseTotal
unless ref $t eq "SVGPDF::TextElement" 0 2 2
383 unless ref($t) eq "SVGPDF::TextElement";
384 2 $cdata .= $t->content;
385 }
386 }
387 34 ▶ if ( $cdata =~ /\S/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cdata =~ /\S/u) 2 32 34
388 2 $css->read_string($cdata);
389 }
390  
391 34 my $atts = $svg->atts;
392  
393 # The viewport, llx lly width height.
394 34 my $vbox = delete $atts->{viewbox};
395  
396 # Width and height are the display size of the viewport.
397 # Resolve em, ex and percentages.
398 34 ▶ my $vwidth = delete $atts->{width} // 0;
Condition50%1/2
delete $atts->{"width"} // 0
l!l
340
MC/DC0%0/1
delete $atts->{"width"} // 0
delete $atts->{"width"}
Truth table50%1/2
A
A= delete $atts->{"width"}
Aresult
11
00
399 34 ▶ my $vheight = delete $atts->{height} // 0;
Condition50%1/2
delete $atts->{"height"} // 0
l!l
340
MC/DC0%0/1
delete $atts->{"height"} // 0
delete $atts->{"height"}
Truth table50%1/2
A
A= delete $atts->{"height"}
Aresult
11
00
400 34 for ( $vwidth ) {
401 34 $_ = $svg->u( $_, width => $pagesize->[0],
402 fontsize => $fontsize );
403 }
404 34 for ( $vheight ) {
405 34 $_ = $svg->u( $_, width => $pagesize->[1],
406 fontsize => $fontsize );
407 }
408  
409 34 delete $atts->{$_} for qw( xmlns:xlink xmlns:svg xmlns version );
410 34 my $style = $svg->css_push($atts);
411  
412 # If there is min-width the image must be scaled if the available
413 # width is smaller. If the width is larger we render to the
414 # available space.
415 34 ▶ my $minw = $style->{'min-width'} // 0;
Condition50%1/2
$style->{"min-width"} // 0
l!l
034
MC/DC0%0/1
$style->{"min-width"} // 0
$style->{"min-width"}
Truth table50%1/2
A
A= $style->{"min-width"}
Aresult
11
00
416 34 ▶ $minw = $svg->u( $minw, width => $pagesize->[0],
Branch: 1 branch
BranchTrueFalseTotal
if $minw 0 34 34
417 fontsize => $fontsize ) if $minw;
418  
419 # vertical-align is needed later when the XObject is placed.
420 34 ▶ my $valign = $style->{'vertical-align'} // 0;
Condition50%1/2
$style->{"vertical-align"} // 0
l!l
034
MC/DC0%0/1
$style->{"vertical-align"} // 0
$style->{"vertical-align"}
Truth table50%1/2
A
A= $style->{"vertical-align"}
Aresult
11
00
421 34 ▶ $valign = $svg->u( $valign, fontsize => $fontsize ) if $valign;
Branch: 1 branch
BranchTrueFalseTotal
if $valign 0 34 34
422  
423 34 my @vb; # viewBox: llx lly width height
424 my @bb; # bbox: llx lly urx ury
425  
426 # We rely on the <svg> to supply the correct viewBox.
427 34 my $width; # width of the vbox
428 34 my $height; # height of the vbox
429 34 ▶ if ( $vbox ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($vbox) { } 4 30 34
430 4 @vb = $svg->getargs($vbox);
431 4 $width = $svg->u( $vb[2],
432 width => $pagesize->[0],
433 fontsize => $fontsize );
434 4 $height = $svg->u( $vb[3],
435 width => $pagesize->[1],
436 fontsize => $fontsize );
437 4 ▶ if ( $minw && $minw > $width ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($minw and $minw > $width) 0 4 4
Condition33%1/3
$minw and $minw > $width
!ll&&!rl&&r
400
MC/DC0%0/2
$minw and $minw > $width
$minw $minw > $width
Truth table33%1/3
A and B
A= $minwB= $minw > $width
ABresult
0X0
100
111
438 0 $width = $minw;
439 0 $vb[2] = $minw / $vheight * $height;
440 0 $self->_dbg("minw: ", $style->{'min-width'}, " ",
441 "width -> $width, \$vb[2] -> $vb[2]\n");
442 }
443 4 ▶ if ( $valign ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($valign) 0 4 4
444 # Verify valign against the vbox.
445 0 my $va = sprintf("%.2f", -$valign/$vheight);
446 0 my $vb = sprintf("%.2f", ($vb[3]+$vb[1])/$vb[3]);
447 0 ▶ warn("Vertical align = $va, but vbox says $vb\n")
Branch: 1 branch
BranchTrueFalseTotal
unless $va eq $vb 0 0 0
448 unless $va eq $vb;
449 }
450 4 ▶ if ( $vwidth && !$vheight ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($vwidth and not $vheight) 0 4 4
Condition33%1/3
$vwidth and not $vheight
!ll&&!rl&&r
040
MC/DC0%0/2
$vwidth and not $vheight
$vwidth not $vheight
Truth table33%1/3
A and not(B)
A= $vwidthB= not $vheight
ABresult
0X0
100
111
451 0 $vheight = $vwidth * $height / $width;
452 }
453 4 ▶ if ( $vheight && !$vwidth ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($vheight and not $vwidth) 0 4 4
Condition33%1/3
$vheight and not $vwidth
!ll&&!rl&&r
040
MC/DC0%0/2
$vheight and not $vwidth
$vheight not $vwidth
Truth table33%1/3
A and not(B)
A= $vheightB= not $vwidth
ABresult
0X0
100
111
454 0 $vwidth = $vheight * $width / $height;
455 }
456 }
457 else {
458 # Use to width/height, falling back to pagesize.
459 30 ▶ $width = $svg->u( $vwidth ||$pagesize->[0],
Condition33%1/3
$vwidth || $pagesize->[0]
l!l&&r!l&&!r
3000
MC/DC0%0/2
$vwidth || $pagesize->[0]
$vwidth $pagesize->[0]
Truth table33%1/3
A || B
A= $vwidthB= $pagesize->[0]
ABresult
1X1
011
000
460 width => $pagesize->[0],
461 fontsize => $fontsize );
462 30 ▶ $height = $svg->u( $vheight ||$pagesize->[1],
Condition33%1/3
$vheight || $pagesize->[1]
l!l&&r!l&&!r
3000
MC/DC0%0/2
$vheight || $pagesize->[1]
$vheight $pagesize->[1]
Truth table33%1/3
A || B
A= $vheightB= $pagesize->[1]
ABresult
1X1
011
000
463 width => $pagesize->[1],
464 fontsize => $fontsize );
465 30 ▶ if ( $minw && $minw > $width ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($minw and $minw > $width) 0 30 30
Condition33%1/3
$minw and $minw > $width
!ll&&!rl&&r
3000
MC/DC0%0/2
$minw and $minw > $width
$minw $minw > $width
Truth table33%1/3
A and B
A= $minwB= $minw > $width
ABresult
0X0
100
111
466 0 ▶ $width = $minw if $minw > $width;
Branch: 1 branch
BranchTrueFalseTotal
if $minw > $width 0 0 0
467 0 $self->_dbg("minw: ", $style->{'min-width'}, " ",
468 "width -> $width");
469 }
470 30 @vb = ( 0, 0, $width, $height );
471 30 ▶ if ( $valign ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($valign) 0 30 30
472 0 $vb[1] = -( $vb[3] + $valign );
473 0 $self->_dbg("valign: ", $style->{'vertical-align'}, " ",
474 "\$vb[1] -> $vb[1]");
475 }
476 30 $vbox = sprintf("%.2f %.2f %.2f %.2f (inferred)", @vb);
477 }
478  
479 34 ▶ $svg->nfi("disproportional vbox/width/height")
Branch: 1 branch
BranchTrueFalseTotal
if $vheight and $width / $height / ($vwidth / $vheight) > "1.05" || $width / $height / ($vwidth / $vheight) < "0.95" 1 33 34
Condition66%4/6
$vheight and $width / $height / ($vwidth / $vheight) > "1.05" || $width / $height / ($vwidth / $vheight) < "0.95"
!ll&&!rl&&r
0331
$width / $height / ($vwidth / $vheight) > "1.05" or $width / $height / ($vwidth / $vheight) < "0.95"
l!l&&r!l&&!r
1033
MC/DC33%1/3
$vheight and $width / $height / ($vwidth / $vheight) > "1.05" || $width / $height / ($vwidth / $vheight) < "0.95"
$vheight $width / $height / ($vwidth / $vheight) > "1.05" $width / $height / ($vwidth / $vheight) < "0.95"
Truth table50%2/4
A and B or C
A= $vheightB= $width / $height / ($vwidth / $vheight) > "1.05"C= $width / $height / ($vwidth / $vheight) < "0.95"
ABCresult
0XX0
1000
11X1
1011
480 if $vheight &&
481 ( (( $width/$height) / ($vwidth/$vheight) > 1.05)
482 || (( $width/$height) / ($vwidth/$vheight) < 0.95) );
483  
484 # Get llx lly urx ury bounding box rectangle.
485 34 @bb = $self->vb2bb(@vb);
486 34 $self->_dbg( "vb $vbox => bb %.2f %.2f %.2f %.2f", @bb );
487 34 ▶ warn( sprintf("vb $vbox => bb %.2f %.2f %.2f %.2f\n", @bb ))
Branch: 1 branch
BranchTrueFalseTotal
if $verbose and not $debug 0 34 34
Condition33%1/3
$verbose and not $debug
!ll&&!rl&&r
0340
MC/DC0%0/2
$verbose and not $debug
$verbose not $debug
Truth table33%1/3
A and not(B)
A= $verboseB= not $debug
ABresult
0X0
100
111
488 if $verbose && !$debug;
489 34 $xo->bbox(@bb);
490  
491 34 ▶ if ( my $c = $style->{"background-color"} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $c = $style->{"background-color"}) 34 0 34
492 34 ▶ if ( $c ne "none" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c ne "none") 2 32 34
493 2 $xo->fill_color($c);
494 2 $xo->rectangle(@bb);
495 2 $xo->fill;
496 }
497 }
498  
499 # Set up result forms.
500 34 ▶ $xoforms->[-1] =
Condition33%2/6
$vwidth || $vb[2]
l!l&&r!l&&!r
3400
$vheight || $vb[3]
l!l&&r!l&&!r
3400
MC/DC0%0/2
$vwidth || $vb[2]
$vwidth $vb[2]
MC/DC0%0/2
$vheight || $vb[3]
$vheight $vb[3]
Truth table33%1/3
A || B
A= $vwidthB= $vb[2]
ABresult
1X1
011
000
Truth table33%1/3
A || B
A= $vheightB= $vb[3]
ABresult
1X1
011
000
501 { xo => $xo,
502 # Design (desired) width and height.
503 vwidth => $vwidth || $vb[2],
504 vheight => $vheight || $vb[3],
505 # viewBox (SVG coordinates)
506 vbox => [ @vb ],
507 width => $vb[2],
508 height => $vb[3],
509 # See e.g. https://oreillymedia.github.io/Using_SVG/extras/ch05-percentages.html
510 diag => sqrt( $vb[2]**2 + $vb[3]**2 ) / sqrt(2),
511 # bbox (PDF coordinates)
512 bbox => [ @bb ],
513 };
514 # Not sure if this is ever needed.
515 34 ▶ $xoforms->[-1]->{valign} = $valign if $valign;
Branch: 1 branch
BranchTrueFalseTotal
if $valign 0 34 34
516  
517 # use DDumper; DDumper( { %{$xoforms->[-1]}, xo => 'XO' } );
518 # <svg> coordinates are topleft down, so translate.
519 34 $self->_dbg( "matrix( 1 0 0 -1 0 0)" );
520 34 $xo->matrix( 1, 0, 0, -1, 0, 0 );
521  
522 34 ▶ if ( $debug ) { # show bb
Branch: 1 branch
BranchTrueFalseTotal
if ($debug) 34 0 34
523 34 $xo->save;
524 34 $self->_dbg( "vb rect( %.2f %.2f %.2f %.2f)",
525 $vb[0], $vb[1], $vb[2]+$vb[0], $vb[1]+$vb[3]);
526 34 $xo->rectangle( $vb[0], $vb[1], $vb[2]+$vb[0], $vb[1]+$vb[3]);
527 34 $xo->fill_color("#ffffc0");
528 34 $xo->fill;
529 34 $xo->move( $vb[0], 0 );
530 34 $xo->hline( $vb[0]+$vb[2]);
531 34 $xo->move( 0, $vb[1] );
532 34 $xo->vline( $vb[1]+$vb[3] );
533 34 $xo->line_width(0.5);
534 34 $xo->stroke_color( "red" );
535 34 $xo->stroke;
536 34 $xo->restore;
537 }
538 34 ▶ $self->draw_grid( $xo, \@vb ) if $grid;
Branch: 1 branch
BranchTrueFalseTotal
if $grid 0 34 34
539  
540  
541 # Establish currentColor.
542 34 for ( $css->find("fill") ) {
543 34 ▶ next if $_ eq 'none' or $_ eq 'transparent';
Branch: 1 branch
BranchTrueFalseTotal
if $_ eq "none" or $_ eq "transparent" 0 34 34
Condition33%1/3
$_ eq "none" or $_ eq "transparent"
l!l&&r!l&&!r
0034
MC/DC0%0/2
$_ eq "none" or $_ eq "transparent"
$_ eq "none" $_ eq "transparent"
Truth table33%1/3
A or B
A= $_ eq "none"B= $_ eq "transparent"
ABresult
1X1
011
000
544 34 ▶ $self->_dbg( "xo fill_color ",
Branch: 1 branch
BranchTrueFalseTotal
$_ eq 'currentColor' ? : 34 0 34
545 $_ eq 'currentColor' ? 'black' : $_,
546 " (initial)");
547 34 ▶ $xo->fill_color( $_ eq 'currentColor' ? 'black' : $_ );
Branch: 1 branch
BranchTrueFalseTotal
$_ eq 'currentColor' ? : 34 0 34
548 }
549 34 for ( $css->find("stroke") ) {
550 34 ▶ next if $_ eq 'none' or $_ eq 'transparent';
Branch: 1 branch
BranchTrueFalseTotal
if $_ eq "none" or $_ eq "transparent" 34 0 34
Condition33%1/3
$_ eq "none" or $_ eq "transparent"
l!l&&r!l&&!r
3400
MC/DC0%0/2
$_ eq "none" or $_ eq "transparent"
$_ eq "none" $_ eq "transparent"
Truth table33%1/3
A or B
A= $_ eq "none"B= $_ eq "transparent"
ABresult
1X1
011
000
551 0 ▶ $self->_dbg( "xo stroke_color ",
Branch: 1 branch
BranchTrueFalseTotal
$_ eq 'currentColor' ? : 0 0 0
552 $_ eq 'currentColor' ? 'black' : $_,
553 " (initial)");
554 0 ▶ $xo->stroke_color( $_ eq 'currentColor' ? 'black' : $_ );
Branch: 1 branch
BranchTrueFalseTotal
$_ eq 'currentColor' ? : 0 0 0
555 }
556 34 $svg->traverse;
557  
558 34 $svg->css_pop;
559  
560 34 $self->_dbg( "==== end ", $e->{name}, " ====" );
561 2 }
562  
563 0 ▶sub min( $a, $b ) { $a < $b ? $a : $b }
Subroutine min: not called CC=2 SCAR=17.9
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
$a < $b ? : 0 0 0
564 0 ▶sub max( $a, $b ) { $a > $b ? $a : $b }
Subroutine max: not called CC=2 SCAR=17.9
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
$a > $b ? : 0 0 0
565  
566 0 ▶method combine_svg( $forms, %opts ) {
Subroutine combine_svg: not called CC=12 SCAR=50.5
Pod undocumented
567 0 ▶ my $type = $opts{type} // "stacked";
Condition0%0/2
$opts{"type"} // "stacked"
l!l
00
MC/DC0%0/1
$opts{"type"} // "stacked"
$opts{"type"}
Truth table0%0/2
A
A= $opts{"type"}
Aresult
11
00
568 0 ▶ return $forms if $type eq "none";
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "none" 0 0 0
569  
570 0 my ( $xmin, $ymin, $xmax, $ymax );
571 0 my $y = 0;
572 0 my $x = 0;
573 0 ▶ my $sep = $opts{sep} || 0;
Condition0%0/2
$opts{'sep'} || 0
l!l
00
MC/DC0%0/1
$opts{'sep'} || 0
$opts{'sep'}
Truth table0%0/2
A
A= $opts{'sep'}
Aresult
11
00
574 0 my $nx;
575  
576 0 ▶ if ( $type eq "bbox" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq 'bbox') { } 0 0 0
577 0 ▶ warn("Combining ", scalar(@$forms), " XObjects\n")
Branch: 1 branch
BranchTrueFalseTotal
if $verbose 0 0 0
578 if $verbose;
579 0 ...;
580 0 $nx->bbox( $xmin, $ymax, $xmax, 0 );
581 }
582 else {
583 0 my $i = 0;
584 0 for my $xo ( @$forms ) {
585 0 $xo = $xo->{xo};
586 0 my @bb = $xo->bbox;
587 0 my $w = abs( $bb[2] - $bb[0] );
588 0 my $h = abs( $bb[3] - $bb[1] );
589  
590 0 ▶ $nx //= $pdf->xo_form;
Condition0%0/3
$nx //= $pdf->xo_form
l!l&&r!l&&!r
000
MC/DC0%0/2
$nx //= $pdf->xo_form
$nx $pdf->xo_form
Truth table0%0/3
A //= B
A= $nxB= $pdf->xo_form
ABresult
1X1
011
000
591 0 my @xy = ( $x - min($bb[0],$bb[2]), $y - max($bb[1],$bb[3]) );
592 0 ▶ warn(sprintf("Stack obj %2d: %.2f %.2f %.2f %.2f \@ %.2f %.2f\n",
Branch: 1 branch
BranchTrueFalseTotal
if $verbose 0 0 0
593 ++$i, @bb, @xy ) ) if $verbose;
594 0 $nx->object( $xo, @xy );
595 0 $y -= $h;
596  
597 0 ▶ if ( defined $xmax ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $xmax) { } 0 0 0
598 0 ▶ $xmax = $w if $w > $xmax;
Branch: 1 branch
BranchTrueFalseTotal
if $w > $xmax 0 0 0
599 }
600 else {
601 0 $xmax = $w;
602 0 $xmin = 0;
603 }
604 0 $ymax = $y;
605 0 $y -= $sep;
606 }
607 warn("Stacked ", scalar(@$forms), " XObjects => bb",
608 0 ▶ ( map { sprintf(" %.2f", $_ ) } $xmin, $ymax, $xmax, 0 ),
Branch: 1 branch
BranchTrueFalseTotal
if $verbose 0 0 0
609 "\n")
610 if $verbose;
611 0 $nx->bbox( $xmin, $ymax, $xmax, 0 );
612 }
613  
614  
615 0 return [ { xo => $nx,
616 width => $xmax - $xmin,
617 height => -$ymax,
618 vwidth => $xmax - $xmin,
619 vheight => -$ymax,
620 bbox => [ $xmin, $ymax, $xmax, 0 ],
621 vbox => [ $xmin, -$ymax, $xmax-$xmin, $ymax ],
622 } ];
623 2 }
624  
625 ################ Service ################
626  
627 34 ▶method vb2bb( @vb ) {
Subroutine vb2bb: called CC=3 SCAR=11.4
Pod undocumented
628 # Calculate bounding box from viewBox
629 0 ▶ @vb = @{$vb[0]} if ref($vb[0]) eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $vb[0] eq "ARRAY" 0 34 34
630 34 my @bb = ( $vb[0], -$vb[1]-$vb[3],
631 $vb[0]+$vb[2], -$vb[1] );
632  
633 34 ▶ wantarray ? @bb : \@bb;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 34 0 34
634 2 }
635  
636 1 ▶method vb2bb_noflip( @vb ) {
Subroutine vb2bb_noflip: called CC=3 SCAR=11.4
Pod undocumented
637 # Calculate bounding box from viewBox
638 0 ▶ @vb = @{$vb[0]} if ref($vb[0]) eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $vb[0] eq "ARRAY" 0 1 1
639 1 my @bb = ( $vb[0], $vb[1],
640 $vb[2]-$vb[0], $vb[3]-$vb[1] );
641  
642 1 ▶ wantarray ? @bb : \@bb;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 1 0 1
643 2 }
644  
645 0 ▶method draw_grid ( $xo, $vb ) {
Subroutine draw_grid: not called CC=12 SCAR=50.5
Pod undocumented
646 0 ▶ my $d = $grid >= 5 ? $grid : 10;
Branch: 1 branch
BranchTrueFalseTotal
$grid >= 5 ? : 0 0 0
647 0 my @bb = @$vb;
648  
649 # Note that this methos is called *after* the yflip.
650 0 $bb[2] += $bb[0];
651 0 $bb[3] += $bb[1];
652  
653 0 my $w = -$bb[0]+$bb[2];
654 0 my $h = -$bb[1]+$bb[3];
655 0 my $thick = 1;
656 0 my $thin = 0.2;
657 0 my $maxlines = 100;
658  
659 # Avoid too many grid lines.
660 0 ▶ while ( $h/$d > $maxlines || $w/$d > $maxlines ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($h / $d > $maxlines or $w / $d > $maxlines) 0 0 0
Condition0%0/3
$h / $d > $maxlines or $w / $d > $maxlines
l!l&&r!l&&!r
000
MC/DC0%0/2
$h / $d > $maxlines or $w / $d > $maxlines
$h / $d > $maxlines $w / $d > $maxlines
Truth table0%0/3
A or B
A= $h / $d > $maxlinesB= $w / $d > $maxlines
ABresult
1X1
011
000
661 0 $d += $d;
662 }
663  
664 0 $xo->save;
665  
666 # Show boundary points.
667 0 my $dd = $d/2;
668 0 $xo->rectangle( $bb[0]-$dd, $bb[1]-$dd, $bb[0]+$dd, $bb[1]+$dd);
669 0 $xo->fill_color("cyan");
670 0 $xo->fill;
671 0 $xo->rectangle( $bb[2]-$dd, $bb[3]-$dd, $bb[2]+$dd, $bb[3]+$dd);
672 0 $xo->fill_color("magenta");
673 0 $xo->fill;
674 # Show origin. This will cover the bb corner unless it is offset.
675 0 $xo->rectangle( -$dd, $dd, $dd, -$dd );
676 0 $xo->fill_color("red");
677 0 $xo->fill;
678  
679 0 $xo->stroke_color("#bbbbbb");
680  
681 # Draw the grid (thick lines).
682 0 $xo->line_width($thick);
683 0 ▶ for ( my $x = 0; $x <= $bb[2]; $x += 5*$d ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x <= $bb[2]) 0 0 0
684 0 $xo->move( $x, $bb[1] );
685 0 $xo->vline($bb[3]);
686 0 $xo->stroke;
687 }
688 0 ▶ for ( my $x = -5*$d; $x > $bb[0]; $x -= 5*$d ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x > $bb[0]) 0 0 0
689 0 next;
690 0 $xo->move( $x, $bb[1] );
691 0 $xo->vline($bb[3]);
692 0 $xo->stroke;
693 }
694 0 ▶ for ( my $y = 0; $y <= $bb[3]; $y += 5*$d ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($y <= $bb[3]) 0 0 0
695 0 $xo->move( $bb[0], $y );
696 0 $xo->hline($bb[2]);
697 0 $xo->stroke;
698 }
699 0 ▶ for ( my $y = -5*$d; $y > $bb[0]; $y -= 5*$d ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($y > $bb[0]) 0 0 0
700 0 next;
701 0 $xo->move( $bb[0], $y );
702 0 $xo->hline($bb[2]);
703 0 $xo->stroke;
704 }
705 # Draw the grid (thin lines).
706 0 $xo->line_width($thin);
707 0 ▶ for ( my $x = 0; $x <= $w; $x += $d ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x <= $w) 0 0 0
708 0 $xo->move( $x, $bb[1] );
709 0 $xo->vline($bb[3]);
710 0 $xo->stroke;
711 }
712 0 ▶ for ( my $x = -$d; $x > $bb[0]; $x -= $d ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x > $bb[0]) 0 0 0
713 0 $xo->move( $x, $bb[1] );
714 0 $xo->vline($bb[3]);
715 0 $xo->stroke;
716 }
717 0 ▶ for ( my $y = 0; $y <= $h; $y += $d ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($y <= $h) 0 0 0
718 0 $xo->move( $bb[0], $y );
719 0 $xo->hline($bb[2]);
720 0 $xo->stroke;
721 }
722 0 ▶ for ( my $y = -$d; $y > $bb[1]; $y -= $d ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($y > $bb[1]) 0 0 0
723 0 $xo->move( $bb[0], $y );
724 0 $xo->hline($bb[2]);
725 0 $xo->stroke;
726 }
727 0 $xo->restore;
728 2 }
729  
730 =head1 INPUT
731  
732 The input SVG data B<must> be correct XML data.
733 The data can be a single C<< <svg> >> element, or a container
734 element (e.g. C<< <html> >> or C<< <xml> >>) with one or more
735 C<< <svg> >> elements among its children.
736  
737 The SVG data can come from several sources:
738  
739 =over 4
740  
741 =item *
742  
743 An SVG document on disk, specified as the name of the document.
744  
745 =item *
746  
747 A file handle, opened on a SVG document, specified as a glob
748 reference. You can use C<\*DATA> to append the SVG data after a
749 C<__DATA__> separator at the end of the program.
750  
751 =item *
752  
753 A string containing SVG data, specified as a reference to a scalar.
754  
755 =back
756  
757 The input is read using L<File::LoadLines>. See its documentation for
758 details.
759  
760 =head1 OUTPUT
761  
762 The result from calling process() is a reference to an array
763 containing hashes that describe the XObjects. Each hash has the
764 following keys:
765  
766 =over 8
767  
768 =item C<vbox>
769  
770 The viewBox as specified in the SVG element.
771  
772 If no viewBox is specified it is set to C<0 0> I<W H>, where I<W> and
773 I<H> are the width and the height.
774  
775 =item C<bbox>
776  
777 Same as the C<vbox>, but using bottom-left and top-right coordinates.
778  
779 =item C<width>
780  
781 The width of the XObject, as specified in the SVG element or derived
782 from its viewBox.
783  
784 =item C<height>
785  
786 The height of the XObject, as specified in the SVG element or derived
787 from its viewBox.
788  
789 =item C<vwidth>
790  
791 The desired width, as specified in the SVG element or derived
792 from its viewBox.
793  
794 =item C<vheight>
795  
796 The desired height, as specified in the SVG element or derived
797 from its viewBox.
798  
799 =item C<xo>
800  
801 The XObject itself.
802  
803 =back
804  
805 =head1 FONT HANDLER CALLBACK
806  
807 In SVG fonts are designated by style attributes C<font-family>,
808 C<font-style>, C<font-weight>, and C<font-size>. How these translate
809 to a PDF font is system dependent. SVGPDF provides a callback
810 mechanism to handle this. As described at L<CONSTRUCTOR>, constructor
811 argument C<fc> can be set to designate a user routine.
812  
813 When a font is required at the PDF level, SVGPDF first checks if a
814 C<@font-face> CSS rule has been set up with matching properties. If a
815 match is found, it is resolved and the font is set. If there is no
816 appropriate CSS rule for this font, the callback is called with the
817 following arguments:
818  
819 ( $self, %args )
820  
821 where C<%args> contains at least:
822  
823 =over
824  
825 =item pdf
826  
827 A reference to the current PDF document.
828  
829 =item style
830  
831 A hash reference that contains values for C<font-family>,
832 C<font-style>, C<font-weight>, and C<font-size>.
833  
834 =back
835  
836 Do not touch C<$self>, it is undocumented for a reason.
837  
838 The callback function can use the contents of C<< $args->{style} >> to
839 select an appropriate font and return it.
840  
841 SVGPDF will try to call the font handler callback only once for each
842 combination of family, style and weight. If the callback function
843 returns a 'false' result SVGPDF will try other alternatives to find a
844 font.
845  
846 Example of an (extremely simplified) callback.
847 It yields the corefont Helvetica when the requested font-family is
848 C<sans>, and Times-Roman for everything else. Styles and weights are
849 ignored.
850  
851 sub simple_font_handler {
852 my ( $self, %args ) = @_;
853 my $pdf = $args->{pdf};
854  
855 my $family = $args->{style}->{'font-family'};
856  
857 my $font;
858 if ( $family eq 'sans' ) {
859 $font = $pdf->font('Helvetica');
860 }
861 else {
862 $font = $pdf->font('Times-Roman');
863 }
864  
865 return $font;
866 }
867  
868 If no callback function is set, SVGPDF will recognize the 14 standard
869 PDF corefonts, and aliases C<serif>, C<sans> and C<mono>.
870  
871 B<IMPORTANT: With the standard corefonts only characters of the
872 ISO-8859-1 set (Latin-1) can be used. No greek, no chinese, no cyrillic.
873 You have been warned.>
874  
875 =head1 LIMITATIONS
876  
877 The following SVG elements are implemented.
878  
879 =over 3
880  
881 =item *
882  
883 C<svg>, but not nested.
884  
885 =item *
886  
887 C<style>, as a child of the outer C<svg>.
888  
889 Many style attributes are understood, including but not limited to:
890  
891 color,
892 stroke, stroke-width, stroke-linecap, stroke-linejoin, stroke-dasharray,
893 fill, stroke-width, stroke-linecap, stroke-linejoin,
894 transform (translate, scale, skewX, skewY, rotate, matrix)
895 font-family, font-style, font-weight, font-size,
896 text-anchor.
897  
898 Partially implemented: @font-face (src url data and local file only).
899  
900 =item *
901  
902 circle,
903 ellipse,
904 g,
905 image,
906 line,
907 path,
908 polygon,
909 polyline,
910 rect,
911 text and tspan (no white-space styles).
912  
913 =item *
914  
915 defs and use,
916  
917 =back
918  
919 The following SVG features are partially implemented.
920  
921 =over 3
922  
923 =item *
924  
925 Percentage units. For most "y", "h" or "height" attributes the result
926 will be the percentage of the viewBox height.
927  
928 Similar for "x", "w" and "width".
929  
930 Everything else will result in a percentage of the viewBox diagonal
931 (according to the specs).
932  
933 =item *
934  
935 Embedded SVG elements and preserveAspectRatio.
936  
937 =item *
938  
939 Standalone T-path elements.
940  
941 =back
942  
943 The following SVG features are not (yet) implemented.
944  
945 =over 3
946  
947 =item *
948  
949 title, desc elements
950  
951 =back
952  
953 The following SVG features are not planned to be implemented.
954  
955 =over 3
956  
957 =item *
958  
959 Shades, gradients, patterns and animations.
960  
961 =item *
962  
963 Shape rendering attributes.
964  
965 =item *
966  
967 Transparency.
968  
969 =item *
970  
971 Text paths.
972  
973 =item *
974  
975 Clipping and masking.
976  
977 =back
978  
979 What is supported, then? Most SVG files generated by any of the
980 following tools seem to produce good if not perfect results:
981  
982 =over 3
983  
984 =item *
985  
986 C<abc2svg> (ABC music notation tool)
987  
988 =item *
989  
990 MathJax, inline and display without tag
991  
992 =item *
993  
994 GNUplot
995  
996 =item *
997  
998 QRcode and barcode generating tools
999  
1000 =item *
1001  
1002 XTerm SVG screen dumps
1003  
1004 =back
1005  
1006 =head1 AUTHOR
1007  
1008 Johan Vromans C<< < jvromans at squirrel dot nl > >>
1009  
1010 Code for circular and elliptic arcs donated by Phil Perry.
1011  
1012 =head1 SUPPORT
1013  
1014 SVGPDF development is hosted on GitHub, repository
1015 L<https://github.com/sciurius/perl-SVGPDF>.
1016  
1017 Please report any bugs or feature requests to the GitHub issue tracker,
1018 L<https://github.com/sciurius/perl-SVGPDF/issues>.
1019  
1020 =head1 LICENSE
1021  
1022 Copyright (C) 2022,2026 Johan Vromans,
1023  
1024 Redistribution and use in source and binary forms, with or without
1025 modification, are permitted provided under the terms of the Simplified
1026 BSD License.
1027  
1028 =cut
1029  
1030 2 1; # End of SVGPDF
« SVGPDF/Svg.pm ↑ Summary SVGPDF/Parser.pm »