github.com/pdfcpu/pdfcpu@v0.11.1/pkg/cli/test/cut_test.go (about)

     1  /*
     2  Copyright 2023 The pdfcpu Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8  	http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package test
    18  
    19  import (
    20  	"path/filepath"
    21  	"testing"
    22  
    23  	"github.com/pdfcpu/pdfcpu/pkg/cli"
    24  	"github.com/pdfcpu/pdfcpu/pkg/pdfcpu"
    25  	"github.com/pdfcpu/pdfcpu/pkg/pdfcpu/types"
    26  )
    27  
    28  func testCut(t *testing.T, msg, inFile, outFile string, unit types.DisplayUnit, cutConf string) {
    29  	t.Helper()
    30  
    31  	cut, err := pdfcpu.ParseCutConfig(cutConf, unit)
    32  	if err != nil {
    33  		t.Fatalf("%s: %v\n", msg, err)
    34  	}
    35  
    36  	inFile = filepath.Join(inDir, inFile)
    37  
    38  	cmd := cli.CutCommand(inFile, outDir, outFile, nil, cut, nil)
    39  	if _, err := cli.Process(cmd); err != nil {
    40  		t.Fatalf("%s %s: %v\n", msg, outFile, err)
    41  	}
    42  }
    43  
    44  func TestCut(t *testing.T) {
    45  	for _, tt := range []struct {
    46  		msg             string
    47  		inFile, outFile string
    48  		unit            types.DisplayUnit
    49  		cutConf         string
    50  	}{
    51  		{"TestRotatedCutHor",
    52  			"testRot.pdf",
    53  			"cutHorRot",
    54  			types.CENTIMETRES,
    55  			"hor:.5, margin:1, border:on"},
    56  
    57  		{"TestCutHor",
    58  			"test.pdf",
    59  			"cutHor",
    60  			types.CENTIMETRES,
    61  			"hor:.5, margin:1, bgcol:#E9967A, border:on"},
    62  
    63  		{"TestCutVer",
    64  			"test.pdf",
    65  			"cutVer",
    66  			types.CENTIMETRES,
    67  			"ver:.5, margin:1, bgcol:#E9967A"},
    68  
    69  		{"TestCutHorAndVerQuarter",
    70  			"test.pdf",
    71  			"cutHorAndVerQuarter",
    72  			types.POINTS,
    73  			"h:.5, v:.5"},
    74  
    75  		{"TestCutHorAndVerThird",
    76  			"test.pdf",
    77  			"cutHorAndVerThird",
    78  			types.POINTS,
    79  			"h:.33333, h:.66666, v:.33333, v:.66666"},
    80  
    81  		{"Test",
    82  			"test.pdf",
    83  			"cutCustom",
    84  			types.POINTS,
    85  			"h:.25, v:.5"},
    86  	} {
    87  		testCut(t, tt.msg, tt.inFile, tt.outFile, tt.unit, tt.cutConf)
    88  	}
    89  }
    90  
    91  func testNDown(t *testing.T, msg, inFile, outFile string, n int, unit types.DisplayUnit, cutConf string) {
    92  	t.Helper()
    93  
    94  	cut, err := pdfcpu.ParseCutConfigForN(n, cutConf, unit)
    95  	if err != nil {
    96  		t.Fatalf("%s: %v\n", msg, err)
    97  	}
    98  
    99  	inFile = filepath.Join(inDir, inFile)
   100  
   101  	cmd := cli.NDownCommand(inFile, outDir, outFile, nil, n, cut, nil)
   102  	if _, err := cli.Process(cmd); err != nil {
   103  		t.Fatalf("%s %s: %v\n", msg, outFile, err)
   104  	}
   105  }
   106  
   107  func TestNDown(t *testing.T) {
   108  	for _, tt := range []struct {
   109  		msg     string
   110  		inFile  string
   111  		outFile string
   112  		n       int
   113  		unit    types.DisplayUnit
   114  		cutConf string
   115  	}{
   116  		{"TestNDownRot2",
   117  			"testRot.pdf",
   118  			"ndownRot2",
   119  			2,
   120  			types.CENTIMETRES,
   121  			"margin:1, bgcol:#E9967A"},
   122  
   123  		{"TestNDown2",
   124  			"test.pdf",
   125  			"ndown2",
   126  			2,
   127  			types.CENTIMETRES,
   128  			"margin:1, border:on"},
   129  
   130  		{"TestNDown9",
   131  			"test.pdf",
   132  			"ndown9",
   133  			9,
   134  			types.CENTIMETRES,
   135  			"margin:1, bgcol:#E9967A, border:on"},
   136  
   137  		{"TestNDown16",
   138  			"test.pdf",
   139  			"ndown16",
   140  			16,
   141  			types.CENTIMETRES,
   142  			""}, // optional border, margin, bgcolor
   143  	} {
   144  		testNDown(t, tt.msg, tt.inFile, tt.outFile, tt.n, tt.unit, tt.cutConf)
   145  	}
   146  }
   147  
   148  func testPoster(t *testing.T, msg, inFile, outFile string, unit types.DisplayUnit, cutConf string) {
   149  	t.Helper()
   150  
   151  	cut, err := pdfcpu.ParseCutConfigForPoster(cutConf, unit)
   152  	if err != nil {
   153  		t.Fatalf("%s: %v\n", msg, err)
   154  	}
   155  
   156  	inFile = filepath.Join(inDir, inFile)
   157  
   158  	cmd := cli.PosterCommand(inFile, outDir, outFile, nil, cut, nil)
   159  	if _, err := cli.Process(cmd); err != nil {
   160  		t.Fatalf("%s %s: %v\n", msg, outFile, err)
   161  	}
   162  }
   163  
   164  func TestPoster(t *testing.T) {
   165  	for _, tt := range []struct {
   166  		msg     string
   167  		inFile  string
   168  		outFile string
   169  		unit    types.DisplayUnit
   170  		cutConf string
   171  	}{
   172  		{"TestPoster", // 2x2 grid of A6 => A4
   173  			"test.pdf", // A4
   174  			"poster",
   175  			types.POINTS,
   176  			"f:A6"},
   177  
   178  		{"TestPosterScaled", // 4x4 grid of A6 => A2
   179  			"test.pdf", // A4
   180  			"posterScaled",
   181  			types.CENTIMETRES,
   182  			"f:A6, scale:2.0, margin:1, bgcol:#E9967A"},
   183  
   184  		{"TestPosterDim", // grid made up of 15x10 cm tiles => A4
   185  			"test.pdf", // A4
   186  			"posterDim",
   187  			types.CENTIMETRES,
   188  			"dim:15 10, margin:1, border:on"},
   189  
   190  		{"TestPosterDimScaled", // grid made up of 15x10 cm tiles => A2
   191  			"test.pdf", // A4
   192  			"posterDimScaled",
   193  			types.CENTIMETRES,
   194  			"dim:15 10, scale:2.0, margin:1, bgcol:#E9967A, border:on"},
   195  	} {
   196  		testPoster(t, tt.msg, tt.inFile, tt.outFile, tt.unit, tt.cutConf)
   197  	}
   198  }