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

     1  /*
     2  Copyright 2023 The pdf 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/api"
    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, outDir, 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  	outDir = filepath.Join(samplesDir, outDir)
    38  
    39  	if err := api.CutFile(inFile, outDir, outFile, nil, cut, nil); err != nil {
    40  		t.Fatalf("%s: %v\n", msg, err)
    41  	}
    42  }
    43  
    44  func TestCut(t *testing.T) {
    45  	for _, tt := range []struct {
    46  		msg             string
    47  		inFile          string
    48  		outDir, outFile string
    49  		unit            types.DisplayUnit
    50  		cutConf         string
    51  	}{
    52  		{"TestRotatedCutHor",
    53  			"testRot.pdf",
    54  			"cut",
    55  			"cutHorRot",
    56  			types.CENTIMETRES,
    57  			"hor:.5, margin:1, border:on"},
    58  
    59  		{"TestCutHor",
    60  			"test.pdf",
    61  			"cut",
    62  			"cutHor",
    63  			types.CENTIMETRES,
    64  			"hor:.5, margin:1, bgcol:#E9967A, border:on"},
    65  
    66  		{"TestCutVer",
    67  			"test.pdf",
    68  			"cut",
    69  			"cutVer",
    70  			types.CENTIMETRES,
    71  			"ver:.5, margin:1, bgcol:#E9967A"},
    72  
    73  		{"TestCutHorAndVerQuarter",
    74  			"test.pdf",
    75  			"cut",
    76  			"cutHorAndVerQuarter",
    77  			types.POINTS,
    78  			"h:.5, v:.5"},
    79  
    80  		{"TestCutHorAndVerThird",
    81  			"test.pdf",
    82  			"cut",
    83  			"cutHorAndVerThird",
    84  			types.POINTS,
    85  			"h:.33333, h:.66666, v:.33333, v:.66666"},
    86  
    87  		{"Test",
    88  			"test.pdf",
    89  			"cut",
    90  			"cutCustom",
    91  			types.POINTS,
    92  			"h:.25, v:.5"},
    93  	} {
    94  		testCut(t, tt.msg, tt.inFile, tt.outDir, tt.outFile, tt.unit, tt.cutConf)
    95  	}
    96  }
    97  
    98  func testNDown(t *testing.T, msg, inFile, outDir, outFile string, n int, unit types.DisplayUnit, cutConf string) {
    99  	t.Helper()
   100  
   101  	cut, err := pdfcpu.ParseCutConfigForN(n, cutConf, unit)
   102  	if err != nil {
   103  		t.Fatalf("%s: %v\n", msg, err)
   104  	}
   105  
   106  	inFile = filepath.Join(inDir, inFile)
   107  	outDir = filepath.Join(samplesDir, outDir)
   108  
   109  	if err := api.NDownFile(inFile, outDir, outFile, nil, n, cut, nil); err != nil {
   110  		t.Fatalf("%s: %v\n", msg, err)
   111  	}
   112  }
   113  
   114  func TestNDown(t *testing.T) {
   115  	for _, tt := range []struct {
   116  		msg             string
   117  		inFile          string
   118  		outDir, outFile string
   119  		n               int
   120  		unit            types.DisplayUnit
   121  		cutConf         string
   122  	}{
   123  		{"TestNDownRot2",
   124  			"testRot.pdf",
   125  			"cut",
   126  			"ndownRot2",
   127  			2,
   128  			types.CENTIMETRES,
   129  			"margin:1, bgcol:#E9967A"},
   130  
   131  		{"TestNDown2",
   132  			"test.pdf",
   133  			"cut",
   134  			"ndown2",
   135  			2,
   136  			types.CENTIMETRES,
   137  			"margin:1, border:on"},
   138  
   139  		{"TestNDown9",
   140  			"test.pdf",
   141  			"cut",
   142  			"ndown9",
   143  			9,
   144  			types.CENTIMETRES,
   145  			"margin:1, bgcol:#E9967A, border:on"},
   146  
   147  		{"TestNDown16",
   148  			"test.pdf",
   149  			"cut",
   150  			"ndown16",
   151  			16,
   152  			types.CENTIMETRES,
   153  			""}, // optional border, margin, bgcolor
   154  	} {
   155  		testNDown(t, tt.msg, tt.inFile, tt.outDir, tt.outFile, tt.n, tt.unit, tt.cutConf)
   156  	}
   157  }
   158  
   159  func testPoster(t *testing.T, msg, inFile, outDir, outFile string, unit types.DisplayUnit, cutConf string) {
   160  	t.Helper()
   161  
   162  	cut, err := pdfcpu.ParseCutConfigForPoster(cutConf, unit)
   163  	if err != nil {
   164  		t.Fatalf("%s: %v\n", msg, err)
   165  	}
   166  
   167  	inFile = filepath.Join(inDir, inFile)
   168  	outDir = filepath.Join(samplesDir, outDir)
   169  
   170  	if err := api.PosterFile(inFile, outDir, outFile, nil, cut, nil); err != nil {
   171  		t.Fatalf("%s: %v\n", msg, err)
   172  	}
   173  }
   174  
   175  func TestPoster(t *testing.T) {
   176  	for _, tt := range []struct {
   177  		msg             string
   178  		inFile          string
   179  		outDir, outFile string
   180  		unit            types.DisplayUnit
   181  		cutConf         string
   182  	}{
   183  		{"TestPoster", // 2x2 grid of A6 => A4
   184  			"test.pdf", // A4
   185  			"cut",
   186  			"poster",
   187  			types.POINTS,
   188  			"f:A6"},
   189  
   190  		{"TestPosterScaled", // 4x4 grid of A6 => A2
   191  			"test.pdf", // A4
   192  			"cut",
   193  			"posterScaled",
   194  			types.CENTIMETRES,
   195  			"f:A6, scale:2.0, margin:1, bgcol:#E9967A"},
   196  
   197  		{"TestPosterDim", // grid made up of 15x10 cm tiles => A4
   198  			"test.pdf", // A4
   199  			"cut",
   200  			"posterDim",
   201  			types.CENTIMETRES,
   202  			"dim:15 10, margin:1, border:on"},
   203  
   204  		{"TestPosterDimScaled", // grid made up of 15x10 cm tiles => A2
   205  			"test.pdf", // A4
   206  			"cut",
   207  			"posterDimScaled",
   208  			types.CENTIMETRES,
   209  			"dim:15 10, scale:2.0, margin:1, bgcol:#E9967A, border:on"},
   210  	} {
   211  		testPoster(t, tt.msg, tt.inFile, tt.outDir, tt.outFile, tt.unit, tt.cutConf)
   212  	}
   213  }