@@ -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
10198func 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 >>\n endobj\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 , "\n xref\n " , "\n " + b .String ()+ "\n \n xref\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\n endobj\n " ,
467- overlayId , len (stream ), stream )
464+ overlayID , len (stream ), stream )
468465}
469466
470467func 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\n qendstream\n endobj\n %d 0 obj\n << /Length 1 >> stream\n Qendstream\n endobj\n " ,
515- nextId , nextId + 1 )
512+ nextID , nextID + 1 )
516513 data = strings .Replace (data , "\n xref\n " , "\n " + objs + "\n xref\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 , "\n xref\n " , "\n " + b .String ()+ "\n xref\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