Skip to content

Commit 85f7357

Browse files
committed
Update to go 1.18 and add linting to CI
1 parent 2aacc38 commit 85f7357

6 files changed

Lines changed: 46 additions & 43 deletions

File tree

.github/workflows/ci.yaml

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
name: Build Static Binaries
22
on: [push]
33

4+
concurrency:
5+
group: ${{ github.ref }}
6+
cancel-in-progress: true
7+
48
jobs:
59

610
build-linux-amd64:
@@ -10,11 +14,15 @@ jobs:
1014
- uses: actions/checkout@v2
1115
- uses: actions/setup-go@v1
1216
with:
13-
go-version: '1.13'
17+
go-version: '1.18'
1418
- name: Dependencies
1519
run: sudo apt-get install -y yasm auto{gen,conf,make}
1620
- name: Build
1721
run: make
22+
- name: Lint
23+
uses: golangci/golangci-lint-action@v3
24+
with:
25+
version: latest
1826
- uses: actions/upload-artifact@v1
1927
with:
2028
name: linux-amd64
@@ -27,8 +35,9 @@ jobs:
2735
- uses: actions/checkout@v2
2836
- name: Dependencies
2937
run: |
38+
brew list --full-name | grep '^go@' | xargs brew uninstall --ignore-dependencies
3039
brew update
31-
brew install go@1.13 yasm automake autogen coreutils
40+
brew install go@1.18 yasm automake autogen coreutils
3241
- name: Build
3342
run: make
3443
- uses: actions/upload-artifact@v1

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
BSD 3-Clause License
22

3-
Copyright (c) 2018, Mansour Behabadi
3+
Copyright (c) 2022, Mansour Behabadi
44
All rights reserved.
55

66
Redistribution and use in source and binary forms, with or without

Readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ easier arranging:
5050

5151
# Build & Development
5252

53-
You need `yasm`, `cmake`, `automake`, `autogen`, `git`, `go >= 1.13`,
53+
You need `yasm`, `cmake`, `automake`, `autogen`, `git`, `go >= 1.18`,
5454
`coreutils` and C compiler.
5555

5656
Build using `make` and the static binary will be output to `bin/pdftilecut`.

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@ module github.com/oxplot/pdftilecut
22

33
require github.com/oxplot/papersizes v0.0.0-20181129004259-76bf44043a93
44

5-
go 1.13
5+
go 1.18

go.sum

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,2 @@
1-
github.com/oxplot/papersizes v0.0.0-20181128121939-69576e6b6e2d h1:MuW0tg2MliCNFBk8jAZZyHQ1PPfrS8Ihp5lNWyoMmVA=
2-
github.com/oxplot/papersizes v0.0.0-20181128121939-69576e6b6e2d/go.mod h1:LJRTnhoARxQgMyT7T9L+ZzwR4OrmyHTy5LPxZEzE1CM=
31
github.com/oxplot/papersizes v0.0.0-20181129004259-76bf44043a93 h1:XCHmJaV53mF3srbG0AWFWZpo7rwBapPzOXgYE25tXdo=
42
github.com/oxplot/papersizes v0.0.0-20181129004259-76bf44043a93/go.mod h1:LJRTnhoARxQgMyT7T9L+ZzwR4OrmyHTy5LPxZEzE1CM=

main.go

Lines changed: 32 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,9 @@ const (
3131

3232
// Min page size in mm
3333
minPageDimension = (bleedMargin + trimMargin + trimMarkLineWidth) * 2 * mmInInch / ptsInInch
34-
35-
creditLine = "CUT WITH PDFTILECUT"
3634
)
3735

38-
type TileSizeFlag struct {
36+
type tileSizeFlag struct {
3937
name string
4038

4139
// in millimeters
@@ -45,15 +43,14 @@ type TileSizeFlag struct {
4543
isDim bool
4644
}
4745

48-
func (v *TileSizeFlag) String() string {
46+
func (v *tileSizeFlag) String() string {
4947
if v.isDim {
5048
return fmt.Sprintf("%.0fmm x %.0fmm", v.width, v.height)
51-
} else {
52-
return fmt.Sprintf("%s (%.0fmm x %.0fmm)", v.name, v.width, v.height)
5349
}
50+
return fmt.Sprintf("%s (%.0fmm x %.0fmm)", v.name, v.width, v.height)
5451
}
5552

56-
func (v *TileSizeFlag) Set(s string) error {
53+
func (v *tileSizeFlag) Set(s string) error {
5754
// unit to mm ratios
5855
unitsToMillimeter := map[string]float32{
5956
"mm": 1,
@@ -95,17 +92,17 @@ var (
9592
debugMode = flag.Bool("debug", false, "run in debug mode")
9693
longTrimMarks = flag.Bool("long-trim-marks", false, "Use full width/height trim marks")
9794
hideLogo = flag.Bool("hide-logo", false, "Hide the logo")
98-
tileSize TileSizeFlag
95+
tileSize tileSizeFlag
9996
)
10097

10198
func init() {
102-
tileSize.Set("A4")
99+
_ = tileSize.Set("A4")
103100
flag.Var(&tileSize, "tile-size",
104101
"maximum size - can be a standard paper size (eg A5), or width x height dimension with a unit (mm, cm, in, pt) (e.g. 6cm x 12in)")
105102
}
106103

107-
// getNextFreeObjectId returns the largest object id in the document + 1
108-
func getNextFreeObjectId(d string) (int, error) {
104+
// getNextFreeObjectID returns the largest object id in the document + 1
105+
func getNextFreeObjectID(d string) (int, error) {
109106
m := regexp.MustCompile(`(?m)^xref\s+\d+\s+(\d+)`).FindStringSubmatch(d)
110107
if m == nil {
111108
return 0, fmt.Errorf("cannot find the next free object id")
@@ -136,7 +133,7 @@ type page struct {
136133
trimBox rect
137134
contentIds []int
138135

139-
parentId int
136+
parentID int
140137
raw string
141138
}
142139

@@ -165,7 +162,7 @@ func (p *page) marshal() string {
165162
fmt.Fprintf(b, " %d 0 R ", cid)
166163
}
167164
fmt.Fprintf(b, " ]\n")
168-
fmt.Fprintf(b, " /Parent %d 0 R\n", p.parentId)
165+
fmt.Fprintf(b, " /Parent %d 0 R\n", p.parentID)
169166
b.WriteString(p.raw)
170167
fmt.Fprintf(b, "\n>>\nendobj\n")
171168
return b.String()
@@ -291,21 +288,21 @@ func cutPageToTiles(p *page, tileW, tileH, bleedMargin, trimMargin float32) []*p
291288
tile.cropBox = tile.mediaBox
292289
tilePages = append(tilePages, &tile)
293290

294-
tgx += 1
291+
tgx++
295292
}
296-
tgy += 1
293+
tgy++
297294
}
298295

299296
return tilePages
300297
}
301298

302299
// appendPagesToDoc appends the given pages after all the other objects
303300
// but before the xref block. It also updates the object ids as it goes
304-
// starting with startId.
305-
func appendPagesToDoc(d string, startId int, pages []*page) string {
301+
// starting with startID.
302+
func appendPagesToDoc(d string, startID int, pages []*page) string {
306303
var b strings.Builder
307304
for pi, p := range pages {
308-
p.id = pi + startId
305+
p.id = pi + startID
309306
b.WriteString(p.marshal())
310307
}
311308
return strings.Replace(d, "\nxref\n", "\n"+b.String()+"\n\nxref\n", 1)
@@ -314,16 +311,16 @@ func appendPagesToDoc(d string, startId int, pages []*page) string {
314311
// replaceAllDocPagesWith updates the first node of the page tree with array
315312
// containing references to the given pages, effectively replacing all
316313
// the existing page trees.
317-
func replaceAllDocPagesWith(d string, pages []*page, pageTreeId int) string {
314+
func replaceAllDocPagesWith(d string, pages []*page, pageTreeID int) string {
318315
b := &strings.Builder{}
319316
for _, p := range pages {
320317
fmt.Fprintf(b, "%d 0 R\n", p.id)
321318
}
322319
// Replace the count
323-
r := regexp.MustCompile(fmt.Sprintf(`(?ms)^(%d 0 obj\n.*?^\s+/Count\s+)\d+`, pageTreeId))
320+
r := regexp.MustCompile(fmt.Sprintf(`(?ms)^(%d 0 obj\n.*?^\s+/Count\s+)\d+`, pageTreeID))
324321
d = r.ReplaceAllString(d, fmt.Sprintf(`${1}%d`, len(pages)))
325322
// Replace page references
326-
r = regexp.MustCompile(fmt.Sprintf(`(?ms)^(%d 0 obj\n.*?^\s+/Kids\s+\[)[^\]]*`, pageTreeId))
323+
r = regexp.MustCompile(fmt.Sprintf(`(?ms)^(%d 0 obj\n.*?^\s+/Kids\s+\[)[^\]]*`, pageTreeID))
327324
d = r.ReplaceAllString(d, fmt.Sprintf(`${1} %s `, b.String()))
328325
return d
329326
}
@@ -369,7 +366,7 @@ func numToAlpha(n int) string {
369366
// - other printmarks such as tile/page number
370367
// This will update the contentIds of the page to include a ref
371368
// to the new overlay object.
372-
func createOverlayForPage(overlayId int, p *page) string {
369+
func createOverlayForPage(overlayID int, p *page) string {
373370
mb, bb, tb := p.mediaBox, p.bleedBox, p.trimBox
374371
// Draw opaque bleed margin
375372
stream := fmt.Sprintf(` q
@@ -462,9 +459,9 @@ func createOverlayForPage(overlayId int, p *page) string {
462459
bb.llx-logoScaledSize, bb.lly-logoScaledSize, logoScale, logoScale, logoGSCmds,
463460
)
464461
}
465-
p.contentIds = append(p.contentIds, overlayId)
462+
p.contentIds = append(p.contentIds, overlayID)
466463
return fmt.Sprintf("%d 0 obj\n<< /Length %d >> stream\n%sendstream\nendobj\n",
467-
overlayId, len(stream), stream)
464+
overlayID, len(stream), stream)
468465
}
469466

470467
func process() error {
@@ -480,9 +477,9 @@ func process() error {
480477
if m == nil {
481478
return fmt.Errorf("cannot find root page tree")
482479
}
483-
pageTreeId, _ := strconv.Atoi(m[1])
480+
pageTreeID, _ := strconv.Atoi(m[1])
484481

485-
nextId, err := getNextFreeObjectId(data)
482+
nextID, err := getNextFreeObjectID(data)
486483
if err != nil {
487484
return err
488485
}
@@ -503,7 +500,7 @@ func process() error {
503500
for _, p := range pages {
504501
ts := cutPageToTiles(p, tileW, tileH, bleedMargin, trimMargin)
505502
for _, t := range ts {
506-
t.parentId = pageTreeId
503+
t.parentID = pageTreeID
507504
}
508505
tiles = append(tiles, ts...)
509506
}
@@ -512,27 +509,27 @@ func process() error {
512509
// Wrap page content with graphics state preserving streams
513510
objs := fmt.Sprintf(
514511
"%d 0 obj\n<< /Length 1 >> stream\nqendstream\nendobj\n%d 0 obj\n<< /Length 1 >> stream\nQendstream\nendobj\n",
515-
nextId, nextId+1)
512+
nextID, nextID+1)
516513
data = strings.Replace(data, "\nxref\n", "\n"+objs+"\nxref\n", 1)
517514
for _, t := range tiles {
518-
t.contentIds = append([]int{nextId}, t.contentIds...)
519-
t.contentIds = append(t.contentIds, nextId+1)
515+
t.contentIds = append([]int{nextID}, t.contentIds...)
516+
t.contentIds = append(t.contentIds, nextID+1)
520517
}
521-
nextId += 2
518+
nextID += 2
522519
}
523520

524521
{
525522
// Create overlays and add it to the doc
526523
b := &strings.Builder{}
527524
for _, t := range tiles {
528-
b.WriteString(createOverlayForPage(nextId, t))
529-
nextId += 1
525+
b.WriteString(createOverlayForPage(nextID, t))
526+
nextID++
530527
}
531528
data = strings.Replace(data, "\nxref\n", "\n"+b.String()+"\nxref\n", 1)
532529
}
533530

534-
data = appendPagesToDoc(data, nextId, tiles)
535-
data = replaceAllDocPagesWith(data, tiles, pageTreeId)
531+
data = appendPagesToDoc(data, nextID, tiles)
532+
data = replaceAllDocPagesWith(data, tiles, pageTreeID)
536533

537534
// Write data back to temp file
538535
f, err := ioutil.TempFile("", "pdftilecut-im2-")
@@ -547,7 +544,6 @@ func process() error {
547544
return err
548545
}
549546
f.Close()
550-
data = "" // let garbage collector clean this up
551547

552548
// Fix and write back an optimized PDF
553549
if err := convertToOptimizedPDF(f.Name(), *outputFile); err != nil {

0 commit comments

Comments
 (0)