DrawText + Translate + Push + Pop anomaly

Erwin Driessens avatarErwin Driessens created an issue

Positioning a text with Translate() and DrawText() works, but when Push() and Pop() are used, the results are not as expected. DrawText() seems to position the text at the current text cursor, but there is no way to set that cursor's coordinate? It is possible that the text cursor isn't part of the canvas state that Push() and Pop() take care of?

I tried some code as follows: (a function that scales the canvas so that units are millimeters is called first)

canvas.Translate(10, 10)
for y := pdf.Unit(0.0); y < 100.0; y += 10.0 {
    for x := pdf.Unit(0.0); x < 100.0; x += 10.0 {
        text.Text(fmt.Sprintf("[%3.0f, %3.0f]", x, y))
        canvas.Push()
        canvas.Translate(x, y)
        canvas.DrawText(text)
        canvas.Pop()
    }
}

Comments (6)

  1. Ross Light

    This is a limitation of PDF, not the library. A text object is supposed to be added only once. A variant of this works fine for me:

    package main
    
    import (
        "bitbucket.org/zombiezen/gopdf/pdf"
        "fmt"
        "log"
        "os"
    )
    
    func main() {
        doc := pdf.New()
        canvas := doc.NewPage(pdf.USLetterWidth, pdf.USLetterHeight)
        canvas.Translate(10, 10)
        for y := pdf.Unit(0.0); y < 8*pdf.Inch; y += pdf.Inch {
            for x := pdf.Unit(0.0); x < 8*pdf.Inch; x += pdf.Inch {
                text := new(pdf.Text)
                text.SetFont(pdf.Helvetica, 14)
                text.Text(fmt.Sprintf("[%3.0f, %3.0f]", x, y))
                canvas.Push()
                canvas.Translate(x, y)
                canvas.DrawText(text)
                canvas.Pop()
            }
        }
        canvas.Close()
        if err := doc.Encode(os.Stdout); err != nil {
            log.Fatal(err)
        }
    }
    
  2. Erwin Driessens

    Aha, i didn't know a Text object was supposed to be used just once. That should be added to the documentation then? So if I understand it correctly, Text NextLine(), NextLineOffset(), SetLeading(), X() and Y() methods are used after DrawText(), after which you allocate a new Text object that will then use the updated text cursor position?

    Another related question: I have been making pdf documents with a Text object that has been reused many times. Are such documents technically invalid, violating the standard?

  3. Log in to comment
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.