diff --git a/pdf/creator/block.go b/pdf/creator/block.go index f4d242f2..4afad62a 100644 --- a/pdf/creator/block.go +++ b/pdf/creator/block.go @@ -266,6 +266,11 @@ func (blk *Block) translate(tx, ty float64) { // drawToPage draws the block on a PdfPage. Generates the content streams and appends to the PdfPage's content // stream and links needed resources. func (blk *Block) drawToPage(page *model.PdfPage) error { + + // TODO(gunnsth): Appears very wasteful to do this all the time. + // Possibly create another wrapper around model.PdfPage (creator.page) which can keep track of whether + // this has already been done. + // Check if Page contents are wrapped - if not wrap it. content, err := page.GetAllContentStreams() if err != nil { @@ -374,6 +379,10 @@ func (blk *Block) mergeBlocks(toAdd *Block) error { func mergeContents(contents *contentstream.ContentStreamOperations, resources *model.PdfPageResources, contentsToAdd *contentstream.ContentStreamOperations, resourcesToAdd *model.PdfPageResources) error { + // TODO(gunnsth): It seems rather expensive to mergeContents all the time. A lot of repetition. + // It would be more efficient to perform the merge at the very and when we have all the "blocks" + // for each page. + // To properly add contents from a block, we need to handle the resources that the block is // using and make sure it is accessible in the modified Page. // diff --git a/pdf/creator/paragraph_test.go b/pdf/creator/paragraph_test.go new file mode 100644 index 00000000..e210bf1f --- /dev/null +++ b/pdf/creator/paragraph_test.go @@ -0,0 +1,35 @@ +/* + * This file is subject to the terms and conditions defined in + * file 'LICENSE.md', which is part of this source code package. + */ + +package creator + +import ( + "bytes" + "fmt" + "testing" + + "github.com/stretchr/testify/require" +) + +func benchmarkParagraphAdding(b *testing.B, loops int) { + for n := 0; n < b.N; n++ { + c := New() + for i := 0; i < loops; i++ { + p := c.NewParagraph(fmt.Sprintf("Paragraph %d - %d", n, i)) + err := c.Draw(p) + require.NoError(b, err) + } + + var buf bytes.Buffer + err := c.Write(&buf) + require.NoError(b, err) + } +} + +// Benchmark adding multiple paragraphs and writing out. +// Check the effects of varying number of paragraphs written out. +func BenchmarkParagraphAdding1(b *testing.B) { benchmarkParagraphAdding(b, 1) } +func BenchmarkParagraphAdding10(b *testing.B) { benchmarkParagraphAdding(b, 10) } +func BenchmarkParagraphAdding100(b *testing.B) { benchmarkParagraphAdding(b, 100) }