Skip to content

Commit

Permalink
Reduce scope: remove pdfPagesIndex package-global constant
Browse files Browse the repository at this point in the history
* Add pagesIndex arg to writePages()
* Specify pagesIndex in PDF.Bytes()
  • Loading branch information
balacode committed Apr 2, 2018
1 parent 2fa6d20 commit 09c1881
Showing 1 changed file with 7 additions and 10 deletions.
17 changes: 7 additions & 10 deletions one_file_pdf.go
@@ -1,6 +1,6 @@
// -----------------------------------------------------------------------------
// (c) balarabe@protonmail.com License: MIT
// :v: 2018-04-02 23:49:07 08C94B [one_file_pdf.go]
// :v: 2018-04-02 23:50:14 F455FB [one_file_pdf.go]
// -----------------------------------------------------------------------------

package pdf
Expand Down Expand Up @@ -111,7 +111,7 @@ package pdf
// writeEndobj() *PDF
// writeMode(fill ...bool) (mode string)
// writeObj(objType string) *PDF
// writePages(fontsIndex, imagesIndex int) *PDF
// writePages(pagesIndex, fontsIndex, imagesIndex int) *PDF
// writeStream(content []byte) *PDF
// writeStreamData(content []byte) *PDF
//
Expand All @@ -132,7 +132,6 @@ package pdf
// # Internal Constants
// pdfFontNames = []string
// pdfFontWidths = [][]int
// pdfPagesIndex = 3
// pdfStandardPaperSizes = []pdfPaperSize

import (
Expand Down Expand Up @@ -404,7 +403,8 @@ func (ob *PDF) AddPage() *PDF {
func (ob *PDF) Bytes() []byte {
// free any existing generated content and write PDF header
ob.reservePage()
var fontsIndex = pdfPagesIndex + len(ob.pages)*2
const pagesIndex = 3
var fontsIndex = pagesIndex + len(ob.pages)*2
var imagesIndex = fontsIndex + len(ob.fonts)
var infoIndex int // set when metadata found
var prevBuf = ob.pbuf
Expand All @@ -416,7 +416,7 @@ func (ob *PDF) Bytes() []byte {
writeObj("/Catalog").write("/Pages 2 0 R").writeEndobj()
//
// write /Pages object (2 0 obj), page count, page size and the pages
ob.writePages(fontsIndex, imagesIndex)
ob.writePages(pagesIndex, fontsIndex, imagesIndex)
//
// write fonts
for _, iter := range ob.fonts {
Expand Down Expand Up @@ -1239,12 +1239,12 @@ func (ob *PDF) writeObj(objType string) *PDF {
} // writeObj

// writePages writes all PDF pages
func (ob *PDF) writePages(fontsIndex, imagesIndex int) *PDF {
func (ob *PDF) writePages(pagesIndex, fontsIndex, imagesIndex int) *PDF {
ob.writeObj("/Pages").write("/Count %d/MediaBox[0 0 %d %d]",
len(ob.pages), int(ob.paperSize.widthPt), int(ob.paperSize.heightPt))
// write page numbers
if len(ob.pages) > 0 {
var pageObjNo = pdfPagesIndex
var pageObjNo = pagesIndex
ob.write("/Kids[")
for i := range ob.pages {
if i > 0 {
Expand Down Expand Up @@ -1872,9 +1872,6 @@ var pdfFontWidths = [][]int{
{500, 556, 556, 500, 000, 500, 444, 444, 500, 000}, // 255
} // pdfFontWidths

// pdfPagesIndex is the starting page object index (after Catalog and Pages)
const pdfPagesIndex = 3

// pdfStandardPaperSizes contains standard paper sizes in mm (width x height)
var pdfStandardPaperSizes = map[string][2]int{
"A0": {841, 1189}, "B0": {1000, 1414}, "C0": {917, 1297}, // ISO-216
Expand Down

0 comments on commit 09c1881

Please sign in to comment.