App-Music-ChordPro

 view release on metacpan or  search on metacpan

lib/ChordPro/Output/PDF/Writer.pm  view on Meta::CPAN

#! perl

package main;

our $config;

package ChordPro::Output::PDF::Writer;

use strict;
use warnings;
use Text::Layout;
use IO::String;
use Carp;
use utf8;

use ChordPro::Files;
use ChordPro::Paths;
use ChordPro::Utils qw( expand_tilde demarkup min is_corefont maybe is_true is_odd );
use ChordPro::Output::Common qw( fmt_subst prep_outlines );
use Ref::Util qw( is_arrayref is_hashref );
use feature 'state';
use Unicode::Collate;
use Unicode::Normalize;

# For regression testing, run perl with PERL_HASH_SEED set to zero.
# This eliminates the arbitrary order of font definitions and triggers
# us to pinpoint some other data that would otherwise be varying.
my $regtest = defined($ENV{PERL_HASH_SEED}) && $ENV{PERL_HASH_SEED} == 0;
my $faketime = 1465041600;

my %fontcache;			# speeds up 2 seconds per song

sub new {
    my ( $pkg, $ps, $pdfapi ) = @_;
    my $self = bless { ps => $ps }, $pkg;
    $self->{pdfapi} = $pdfapi;
    $self->{pdf} = $pdfapi->new;
    $self->{pdf}->{forcecompress} = 0 if $regtest;
    $self->{pdf}->mediabox( $ps->{papersize}->[0],
			    $ps->{papersize}->[1] );
    $self->{pdf}->page_layout( $ps->{page_layout} )
      if $ps->{page_layout};
    $self->{layout} = Text::Layout->new( $self->{pdf} );
    $self->{tmplayout} = undef;

    no strict 'refs';
    # Patches and enhancements to PDF library.
    *{$pdfapi . '::Resource::XObject::Form::width' } = \&_xo_width;
    *{$pdfapi . '::Resource::XObject::Form::height'} = \&_xo_height;

    if ( $pdfapi eq 'PDF::API2' ) {
	my $apiversion = ${$pdfapi . '::VERSION'};
	no warnings 'redefine';

	# Fix date validation.
	*{$pdfapi . '::_is_date'} = sub { 1 }
	  if $apiversion < 2.045;

	# Enhanced version that allows named destinations.
	eval "use $pdfapi" . "::Annotation";
	*{$pdfapi . '::Annotation::pdf'     } = \&pdfapi_annotation_pdf
	  if $apiversion < 999; # no milestone yet

	# Enhanced version that doesn't blow up.
	eval "use $pdfapi" . "::Basic::PDF::Array";
	*{$pdfapi . '::Basic::PDF::Array::outobjdeep' } = \&pdfapi_outobjdeep
	  if $apiversion < 999; # no milestone yet
    }
    elsif ( $pdfapi eq 'PDF::Builder' ) {
	my $apiversion = ${$pdfapi . '::VERSION'};
	no warnings 'redefine';

	# Enhanced version that allows named destinations.
	eval "use $pdfapi" . "::Annotation";
	*{$pdfapi . '::Annotation::pdf'     } = \&pdfapi_annotation_pdf
	  if $apiversion < 999; # no milestone yet
    }

    # Text::Layout hooks.
    *{$pdfapi . '::named_dest_register' } = \&pdfapi_named_dest_register;



( run in 0.539 second using v1.01-cache-2.11-cpan-ceb78f64989 )