Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
782 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
/* | ||
Copyright 2024 The pdf Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package test | ||
|
||
import ( | ||
"path/filepath" | ||
"testing" | ||
|
||
"github.com/pdfcpu/pdfcpu/pkg/api" | ||
"github.com/pdfcpu/pdfcpu/pkg/pdfcpu" | ||
"github.com/pdfcpu/pdfcpu/pkg/pdfcpu/types" | ||
) | ||
|
||
func TestZoomInByFactor(t *testing.T) { | ||
msg := "TestZoomInByFactor" | ||
|
||
inFile := filepath.Join(inDir, "test.pdf") | ||
|
||
zoom, err := pdfcpu.ParseZoomConfig("factor:2", types.POINTS) | ||
if err != nil { | ||
t.Fatalf("%s invalid zoom configuration: %v\n", msg, err) | ||
} | ||
outFile := filepath.Join(samplesDir, "zoom", "zoomInByFactor2.pdf") | ||
if err := api.ZoomFile(inFile, outFile, nil, zoom, nil); err != nil { | ||
t.Fatalf("%s zoom: %v\n", msg, err) | ||
} | ||
|
||
zoom, err = pdfcpu.ParseZoomConfig("factor:4", types.POINTS) | ||
if err != nil { | ||
t.Fatalf("%s invalid zoom configuration: %v\n", msg, err) | ||
} | ||
outFile = filepath.Join(samplesDir, "zoom", "zoomInByFactor4.pdf") | ||
if err := api.ZoomFile(inFile, outFile, nil, zoom, nil); err != nil { | ||
t.Fatalf("%s zoom: %v\n", msg, err) | ||
} | ||
} | ||
|
||
func TestZoomOutByFactor(t *testing.T) { | ||
msg := "TestZoomOutByFactor" | ||
|
||
inFile := filepath.Join(inDir, "test.pdf") | ||
|
||
zoom, err := pdfcpu.ParseZoomConfig("factor:.5", types.POINTS) | ||
if err != nil { | ||
t.Fatalf("%s invalid zoom configuration: %v\n", msg, err) | ||
} | ||
outFile := filepath.Join(samplesDir, "zoom", "zoomOutByFactor05.pdf") | ||
if err := api.ZoomFile(inFile, outFile, nil, zoom, nil); err != nil { | ||
t.Fatalf("%s zoom: %v\n", msg, err) | ||
} | ||
|
||
zoom, err = pdfcpu.ParseZoomConfig("factor:.25, border:true", types.POINTS) | ||
if err != nil { | ||
t.Fatalf("%s invalid zoom configuration: %v\n", msg, err) | ||
} | ||
outFile = filepath.Join(samplesDir, "zoom", "zoomOutByFactor025.pdf") | ||
if err := api.ZoomFile(inFile, outFile, nil, zoom, nil); err != nil { | ||
t.Fatalf("%s zoom: %v\n", msg, err) | ||
} | ||
} | ||
|
||
func TestZoomOutByHorizontalMargin(t *testing.T) { | ||
// Zoom out of page content resulting in a preferred horizontal margin. | ||
msg := "TestZoomOutByHMargin" | ||
inFile := filepath.Join(inDir, "test.pdf") | ||
|
||
zoom, err := pdfcpu.ParseZoomConfig("hmargin:149", types.POINTS) | ||
if err != nil { | ||
t.Fatalf("%s invalid zoom configuration: %v\n", msg, err) | ||
} | ||
outFile := filepath.Join(samplesDir, "zoom", "zoomOutByHMarginPoints.pdf") | ||
if err := api.ZoomFile(inFile, outFile, nil, zoom, nil); err != nil { | ||
t.Fatalf("%s zoom: %v\n", msg, err) | ||
} | ||
|
||
zoom, err = pdfcpu.ParseZoomConfig("hmargin:1, border:true, bgcol:lightgray", types.CENTIMETRES) | ||
if err != nil { | ||
t.Fatalf("%s invalid zoom configuration: %v\n", msg, err) | ||
} | ||
outFile = filepath.Join(samplesDir, "zoom", "zoomOutByHMarginCm.pdf") | ||
if err := api.ZoomFile(inFile, outFile, nil, zoom, nil); err != nil { | ||
t.Fatalf("%s zoom: %v\n", msg, err) | ||
} | ||
} | ||
|
||
func TestZoomOutByVerticalMargin(t *testing.T) { | ||
// Zoom out of page content resulting in a preferred vertical margin. | ||
msg := "TestZoomOutByVMargin" | ||
inFile := filepath.Join(inDir, "test.pdf") | ||
|
||
zoom, err := pdfcpu.ParseZoomConfig("vmargin:1", types.INCHES) | ||
if err != nil { | ||
t.Fatalf("%s invalid zoom configuration: %v\n", msg, err) | ||
} | ||
outFile := filepath.Join(samplesDir, "zoom", "zoomOutByVMarginInches.pdf") | ||
if err := api.ZoomFile(inFile, outFile, nil, zoom, nil); err != nil { | ||
t.Fatalf("%s zoom: %v\n", msg, err) | ||
} | ||
|
||
zoom, err = pdfcpu.ParseZoomConfig("vmargin:30, border:false, bgcol:lightgray", types.MILLIMETRES) | ||
if err != nil { | ||
t.Fatalf("%s invalid zoom configuration: %v\n", msg, err) | ||
} | ||
outFile = filepath.Join(samplesDir, "zoom", "zoomOutByVMarginMm.pdf") | ||
if err := api.ZoomFile(inFile, outFile, nil, zoom, nil); err != nil { | ||
t.Fatalf("%s zoom: %v\n", msg, err) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
/* | ||
Copyright 2024 The pdfcpu Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package api | ||
|
||
import ( | ||
"io" | ||
"os" | ||
|
||
"github.com/pdfcpu/pdfcpu/pkg/log" | ||
"github.com/pdfcpu/pdfcpu/pkg/pdfcpu" | ||
"github.com/pdfcpu/pdfcpu/pkg/pdfcpu/model" | ||
"github.com/pkg/errors" | ||
) | ||
|
||
// Zoom applies resizeConf for selected pages of rs and writes result to w. | ||
func Zoom(rs io.ReadSeeker, w io.Writer, selectedPages []string, zoom *model.Zoom, conf *model.Configuration) error { | ||
if rs == nil { | ||
return errors.New("pdfcpu: Zoom: missing rs") | ||
} | ||
|
||
if conf == nil { | ||
conf = model.NewDefaultConfiguration() | ||
} | ||
conf.Cmd = model.ZOOM | ||
|
||
ctx, err := ReadValidateAndOptimize(rs, conf) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
pages, err := PagesForPageSelection(ctx.PageCount, selectedPages, true, true) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if err = pdfcpu.Zoom(ctx, pages, zoom); err != nil { | ||
return err | ||
} | ||
|
||
return Write(ctx, w, conf) | ||
} | ||
|
||
// ZoomFile applies zoomConf for selected pages of inFile and writes result to outFile. | ||
func ZoomFile(inFile, outFile string, selectedPages []string, zoom *model.Zoom, conf *model.Configuration) (err error) { | ||
if log.CLIEnabled() { | ||
log.CLI.Printf("zooming %s\n", inFile) | ||
} | ||
|
||
tmpFile := inFile + ".tmp" | ||
if outFile != "" && inFile != outFile { | ||
tmpFile = outFile | ||
logWritingTo(outFile) | ||
} else { | ||
logWritingTo(inFile) | ||
} | ||
|
||
var ( | ||
f1, f2 *os.File | ||
) | ||
|
||
if f1, err = os.Open(inFile); err != nil { | ||
return err | ||
} | ||
|
||
if f2, err = os.Create(tmpFile); err != nil { | ||
f1.Close() | ||
return err | ||
} | ||
|
||
defer func() { | ||
if err != nil { | ||
f2.Close() | ||
f1.Close() | ||
os.Remove(tmpFile) | ||
return | ||
} | ||
if err = f2.Close(); err != nil { | ||
return | ||
} | ||
if err = f1.Close(); err != nil { | ||
return | ||
} | ||
if outFile == "" || inFile == outFile { | ||
err = os.Rename(tmpFile, inFile) | ||
} | ||
}() | ||
|
||
if conf == nil { | ||
conf = model.NewDefaultConfiguration() | ||
} | ||
conf.Cmd = model.ZOOM | ||
|
||
return Zoom(f1, f2, selectedPages, zoom, conf) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.