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

     1  /*
     2  Copyright 2020 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  package test
    17  
    18  import (
    19  	"path/filepath"
    20  	"testing"
    21  
    22  	"github.com/pdfcpu/pdfcpu/pkg/api"
    23  	"github.com/pdfcpu/pdfcpu/pkg/cli"
    24  	"github.com/pdfcpu/pdfcpu/pkg/pdfcpu/types"
    25  )
    26  
    27  func TestListBoxesCommand(t *testing.T) {
    28  	msg := "TestListBoxesCommand"
    29  	inFile := filepath.Join(inDir, "5116.DCT_Filter.pdf")
    30  
    31  	// List all page boundaries for all pages.
    32  	cmd := cli.ListBoxesCommand(inFile, nil, nil, conf)
    33  	if _, err := cli.Process(cmd); err != nil {
    34  		t.Fatalf("%s: %v\n", msg, err)
    35  	}
    36  
    37  	// List crop box for all pages.
    38  	pb, err := api.PageBoundariesFromBoxList("crop")
    39  	if err != nil {
    40  		t.Fatalf("%s: %v\n", msg, err)
    41  	}
    42  	cmd.PageBoundaries = pb
    43  	if _, err := cli.Process(cmd); err != nil {
    44  		t.Fatalf("%s: %v\n", msg, err)
    45  	}
    46  }
    47  
    48  func TestCropCommand(t *testing.T) {
    49  	msg := "TestCropCommand"
    50  	inFile := filepath.Join(inDir, "test.pdf")
    51  	outFile := filepath.Join(outDir, "out.pdf")
    52  
    53  	for _, tt := range []struct {
    54  		s string
    55  		u types.DisplayUnit
    56  	}{
    57  		{"0 0 10 0", types.POINTS},
    58  		{"[0 0 5 5]", types.CENTIMETRES},
    59  		{"100", types.POINTS},
    60  		{"20% 40%", types.POINTS},
    61  		{"dim:30 30", types.POINTS},
    62  		{"dim:50% 50%", types.POINTS},
    63  		{"pos:bl, dim:50% 50%", types.POINTS},
    64  		{"pos:tl, off: 10 -10, dim:50% 50%", types.POINTS},
    65  		{"-1", types.INCHES},
    66  		{"-25%", types.POINTS},
    67  	} {
    68  		box, err := api.Box(tt.s, tt.u)
    69  		if err != nil {
    70  			t.Fatalf("%s: %v\n", msg, err)
    71  		}
    72  
    73  		cmd := cli.CropCommand(inFile, outFile, nil, box, conf)
    74  		if _, err := cli.Process(cmd); err != nil {
    75  			t.Fatalf("%s: %v\n", msg, err)
    76  		}
    77  
    78  	}
    79  }
    80  
    81  func TestAddBoxesCommand(t *testing.T) {
    82  	msg := "TestAddBoxesCommand"
    83  	inFile := filepath.Join(inDir, "test.pdf")
    84  	outFile := filepath.Join(outDir, "out.pdf")
    85  
    86  	for _, tt := range []struct {
    87  		s string
    88  		u types.DisplayUnit
    89  	}{
    90  		{"crop:[0 0 5 5]", types.CENTIMETRES}, // Crop 5 x 5 cm at bottom left corner
    91  		{"crop:10", types.POINTS},
    92  		{"crop:-10", types.POINTS},
    93  		{"crop:10 20, trim:crop, art:bleed, bleed:art", types.POINTS},
    94  		{"crop:10 20, trim:crop, art:bleed, bleed:media", types.POINTS},
    95  		{"c:10 20, t:c, a:b, b:m", types.POINTS},
    96  		{"crop:10, trim:20, art:trim", types.POINTS},
    97  	} {
    98  		pb, err := api.PageBoundaries(tt.s, tt.u)
    99  		if err != nil {
   100  			t.Fatalf("%s: %v\n", msg, err)
   101  		}
   102  
   103  		cmd := cli.AddBoxesCommand(inFile, outFile, nil, pb, conf)
   104  		if _, err := cli.Process(cmd); err != nil {
   105  			t.Fatalf("%s: %v\n", msg, err)
   106  		}
   107  	}
   108  }
   109  
   110  func TestAddRemoveBoxesCommand(t *testing.T) {
   111  	msg := "TestAddRemoveBoxesCommand"
   112  	inFile := filepath.Join(inDir, "test.pdf")
   113  	outFile := filepath.Join(outDir, "out.pdf")
   114  
   115  	pb, err := api.PageBoundaries("crop:[0 0 100 100]", types.POINTS)
   116  	if err != nil {
   117  		t.Fatalf("%s: %v\n", msg, err)
   118  	}
   119  	cmd := cli.AddBoxesCommand(inFile, outFile, nil, pb, conf)
   120  	if _, err := cli.Process(cmd); err != nil {
   121  		t.Fatalf("%s: %v\n", msg, err)
   122  	}
   123  
   124  	pb, err = api.PageBoundariesFromBoxList("crop")
   125  	if err != nil {
   126  		t.Fatalf("%s: %v\n", msg, err)
   127  	}
   128  	cmd = cli.RemoveBoxesCommand(inFile, outFile, nil, pb, conf)
   129  	if _, err := cli.Process(cmd); err != nil {
   130  		t.Fatalf("%s: %v\n", msg, err)
   131  	}
   132  }