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

     1  /*
     2  Copyright 2019 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  	"os"
    21  	"path/filepath"
    22  	"testing"
    23  
    24  	"github.com/pdfcpu/pdfcpu/pkg/api"
    25  	"github.com/pdfcpu/pdfcpu/pkg/pdfcpu/model"
    26  	"github.com/pdfcpu/pdfcpu/pkg/pdfcpu/types"
    27  )
    28  
    29  func listBoxes(t *testing.T, fileName string, pb *model.PageBoundaries) ([]string, error) {
    30  	t.Helper()
    31  
    32  	msg := "listBoxes"
    33  
    34  	f, err := os.Open(fileName)
    35  	if err != nil {
    36  		t.Fatalf("%s open: %v\n", msg, err)
    37  	}
    38  	defer f.Close()
    39  
    40  	ctx, err := api.ReadValidateAndOptimize(f, conf)
    41  	if err != nil {
    42  		t.Fatalf("%s ReadValidateAndOptimize: %v\n", msg, err)
    43  	}
    44  
    45  	if pb == nil {
    46  		pb = &model.PageBoundaries{}
    47  		pb.SelectAll()
    48  	}
    49  
    50  	return ctx.ListPageBoundaries(nil, pb)
    51  }
    52  
    53  func TestListBoxes(t *testing.T) {
    54  	msg := "TestListBoxes"
    55  	inFile := filepath.Join(inDir, "5116.DCT_Filter.pdf")
    56  
    57  	if _, err := listBoxes(t, inFile, nil); err != nil {
    58  		t.Fatalf("%s: %v\n", msg, err)
    59  	}
    60  
    61  	// List crop box for all pages.
    62  	pb, err := api.PageBoundariesFromBoxList("crop")
    63  	if err != nil {
    64  		t.Fatalf("%s: %v\n", msg, err)
    65  	}
    66  	if _, err := listBoxes(t, inFile, pb); err != nil {
    67  		t.Fatalf("%s: %v\n", msg, err)
    68  	}
    69  }
    70  
    71  func TestCrop(t *testing.T) {
    72  	msg := "TestCrop"
    73  	inFile := filepath.Join(inDir, "test.pdf")
    74  	outFile := filepath.Join(outDir, "out.pdf")
    75  
    76  	for _, tt := range []struct {
    77  		s string
    78  		u types.DisplayUnit
    79  	}{
    80  		{"[0 0 5 5]", types.CENTIMETRES},
    81  		{"100", types.POINTS},
    82  		{"20% 40%", types.POINTS},
    83  		{"dim:30 30", types.POINTS},
    84  		{"dim:50% 50%", types.POINTS},
    85  		{"pos:bl, dim:50% 50%", types.POINTS},
    86  		{"pos:tl, off: 10 -10, dim:50% 50%", types.POINTS},
    87  		{"pos:tl, dim:.5 1 rel", types.POINTS},
    88  		{"-1", types.INCHES},
    89  		{"-25%", types.POINTS},
    90  	} {
    91  		box, err := api.Box(tt.s, tt.u)
    92  		if err != nil {
    93  			t.Fatalf("%s: %v\n", msg, err)
    94  		}
    95  
    96  		if err := api.CropFile(inFile, outFile, nil, box, nil); err != nil {
    97  			t.Fatalf("%s: %v\n", msg, err)
    98  		}
    99  	}
   100  }
   101  
   102  func TestAddBoxes(t *testing.T) {
   103  	msg := "TestAddBoxes"
   104  	inFile := filepath.Join(inDir, "test.pdf")
   105  	outFile := filepath.Join(outDir, "out.pdf")
   106  
   107  	for _, tt := range []struct {
   108  		s string
   109  		u types.DisplayUnit
   110  	}{
   111  		{"art:10%", types.POINTS}, // When using relative positioning unit is irrelevant.
   112  		{"trim:10", types.POINTS},
   113  		{"crop:[0 0 5 5]", types.CENTIMETRES}, // Crop 5 x 5 cm at bottom left corner
   114  		{"crop:10", types.POINTS},
   115  		{"crop:-10", types.POINTS},
   116  		{"crop:10 20, trim:crop, art:bleed, bleed:art", types.POINTS},
   117  		{"crop:10 20, trim:crop, art:bleed, bleed:media", types.POINTS},
   118  		{"c:10 20, t:c, a:b, b:m", types.POINTS},
   119  		{"crop:10, trim:20, art:trim", types.POINTS},
   120  	} {
   121  		pb, err := api.PageBoundaries(tt.s, tt.u)
   122  		if err != nil {
   123  			t.Fatalf("%s: %v\n", msg, err)
   124  		}
   125  
   126  		if err := api.AddBoxesFile(inFile, outFile, nil, pb, nil); err != nil {
   127  			t.Fatalf("%s: %v\n", msg, err)
   128  		}
   129  	}
   130  }
   131  
   132  func TestAddRemoveBoxes(t *testing.T) {
   133  	msg := "TestAddRemoveBoxes"
   134  	inFile := filepath.Join(inDir, "test.pdf")
   135  	outFile := filepath.Join(outDir, "out.pdf")
   136  
   137  	pb, err := api.PageBoundaries("crop:[0 0 100 100]", types.POINTS)
   138  	if err != nil {
   139  		t.Fatalf("%s: %v\n", msg, err)
   140  	}
   141  	if err := api.AddBoxesFile(inFile, outFile, nil, pb, nil); err != nil {
   142  		t.Fatalf("%s: %v\n", msg, err)
   143  	}
   144  
   145  	pb, err = api.PageBoundariesFromBoxList("crop")
   146  	if err != nil {
   147  		t.Fatalf("%s: %v\n", msg, err)
   148  	}
   149  	if err := api.RemoveBoxesFile(outFile, outFile, nil, pb, nil); err != nil {
   150  		t.Fatalf("%s: %v\n", msg, err)
   151  	}
   152  }