]> git.sur5r.net Git - pdfstitch/blobdiff - pdfstitch
New parameter --defaultcrop to define genmeta crop factor
[pdfstitch] / pdfstitch
index 2a46d0e9c71b3a903ceafd9059e3d79c98f5a06b..eb82b6273805221e41d9df376ec2fcfc3e53b650 100755 (executable)
--- a/pdfstitch
+++ b/pdfstitch
@@ -1,4 +1,4 @@
-#!/usr/bin/perl -w
+#!/usr/bin/env perl
 
 use 5.20.0;
 use strict;
@@ -16,6 +16,7 @@ my $genmeta = '';
 my $preview = '';
 my $crop = '';
 my $stitch = '';
+my $defaultcrop = '0.9';
 
 Getopt::Long::Configure("bundling");
 
@@ -23,16 +24,17 @@ Getopt::Long::Configure("bundling");
 my $usage = <<ENDUSAGE;
 pdfstitch - Copyright (C) 2017 by Jakob Haufe <sur5r\@sur5r.net>
 
-Usage: $0 [-hgpcs] [--genmeta] [--preview] [--crop] [--stitch] {PDF file|.stitch file}
+Usage: $0 [-hgpcs] [--genmeta] [--defaultcrop=<factor>] [--preview] [--crop] [--stitch] {PDF file|.stitch file}
 
- -h, --help     Display this message
- -g, --genmeta  Generate .stitch file for stitching based on given PDF
-                (default when called with a PDF)
- -p, --preview  Generate preview PDF containing overlays to analyze
-                cropping
- -c, --crop     Generate cropped PDF according to given .stitch
- -s, --stitch   Generate stitched PDF
-                (default when called with a .stitch file)
+ -h, --help             Display this message
+ -g, --genmeta          Generate .stitch file for stitching based on given PDF
+                        (default when called with a PDF)
+ -d, --defaultcrop=0.9  Set default crop factor for genmeta (defaults to 0.9)
+ -p, --preview          Generate preview PDF containing overlays to analyze
+                        cropping
+ -c, --crop             Generate cropped PDF according to given .stitch
+ -s, --stitch           Generate stitched PDF
+                        (default when called with a .stitch file)
 
 pdfstitch is free software under the GNU AGPL version 3. See LICENSE for details.
 ENDUSAGE
@@ -41,6 +43,7 @@ GetOptions
 (
     'h|help' => \$help,
     'g|genmeta' => \$genmeta,
+    'd|defaultcrop=s' => \$defaultcrop,
     'p|preview' => \$preview,
     'c|crop' => \$crop,
     's|stitch' => \$stitch
@@ -48,7 +51,9 @@ GetOptions
 
 die $usage if $help;
 
-die "--genmeta can not be combined with other actions!\n" if($genmeta and ($preview or $crop or $stitch));
+die "--genmeta can only be combined with --defaultcrop!\n" if($genmeta and ($preview or $crop or $stitch));
+
+die "--defaultcrop can only be combined with --genmeta!\n" if($defaultcrop and ($preview or $crop or $stitch));
 
 die "No input file specified!\n" unless $ARGV[0];
 
@@ -60,14 +65,23 @@ die "$infile is not readable!\n" unless -r $infile;
 if(not ($genmeta or $preview or $crop or $stitch))
 {
     my $magic = File::LibMagic->new();
+    my $mime_type;
 
-    my $info = $magic->info_from_filename($infile);
-    if($info->{mime_type} eq "application/pdf")
+    if($magic->can('info_from_filename'))
+    {
+        $mime_type = $magic->info_from_filename($infile)->{mime_type}
+    }
+    else
+    {
+        # Fallback for File::Libmagic below 1.06
+        $mime_type = $magic->checktype_filename($infile);
+    }
+    if($mime_type =~ "^application/pdf")
     {
         print "Detected PDF, turning on --genmeta\n";
         $genmeta = 1;
     }
-    elsif ($info->{mime_type} eq  "text/plain")
+    elsif ($mime_type =~  "^text/plain")
     {
         YAML::LoadFile($infile) or die "Failed to parse $infile as YAML!\n";
         print "Detected YAML, turning on --stitch\n";
@@ -75,7 +89,7 @@ if(not ($genmeta or $preview or $crop or $stitch))
     }
     else
     {
-        die "$infile has unsupported type: $info->{mime_type}\n";
+        die "$infile has unsupported type: $mime_type\n";
     }
 }
 
@@ -94,10 +108,10 @@ if($genmeta)
 
     my $meta = {
         input => basename($infile),
-        x => (($urx - $llx)*0.1)/2,
-        y => (($ury - $lly)*0.1)/2,
-        width => ($urx - $llx)*0.9,
-        height => ($ury - $lly)*0.9,
+        x => (($urx - $llx)*(1-$defaultcrop))/2,
+        y => (($ury - $lly)*(1-$defaultcrop))/2,
+        width => ($urx - $llx)*$defaultcrop,
+        height => ($ury - $lly)*$defaultcrop,
         columns => int(sqrt($pdf->pages)),
         rows => int(sqrt($pdf->pages)),
         pageorder => [(1 .. $pdf->pages)],
@@ -166,11 +180,11 @@ else
         my $stitchedpdf = PDF::API2->new();
 
         my $page = $stitchedpdf->page();
-        $page->mediabox($width + 100, $height + 100);
+        $page->mediabox($width, $height);
 
         my $content = $page->gfx();
-        my $column = 1;
-        my $row = 1;
+        my $column = 0;
+        my $row = 0;
 
         foreach my $pagenr (@{$meta->{pageorder}})
         {
@@ -185,15 +199,15 @@ else
 
                 $xo->bbox($llx, $lly, $urx, $ury);
 
-                my $xpos = ($column - 1) * $meta->{width};
-                my $ypos = $height - ($row * $meta->{height});
+                my $xpos = ($column) * $meta->{width} - ($meta->{x} + $meta->{pageoffsets}->{$pagenr}->{x});
+                my $ypos = $height - (($row+1) * $meta->{height}) - ($meta->{y} + $meta->{pageoffsets}->{$pagenr}->{y});
                 $content->formimage($xo, $xpos, $ypos);
             }
             $column++;
-            if($column > $meta->{columns})
+            if($column == $meta->{columns})
             {
                 $row++;
-                $column=1;
+                $column=0;
             }
         }