8000 Creator optimize drawing blocks by adrg · Pull Request #136 · unidoc/unipdf · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Creator optimize drawing blocks #136

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 31, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 23 additions & 5 deletions creator/creator.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import (
// content onto imported PDF pages, etc.
type Creator struct {
pages []*model.PdfPage
pageBlocks map[*model.PdfPage]*Block

activePage *model.PdfPage

pagesize PageSize
Expand Down Expand Up @@ -116,6 +118,7 @@ type margins struct {
func New() *Creator {
c := &Creator{}
c.pages = []*model.PdfPage{}
c.pageBlocks = map[*model.PdfPage]*Block{}
c.SetPageSize(PageSizeLetter)

m := 0.1 * c.pageWidth
Expand Down Expand Up @@ -513,6 +516,15 @@ func (c *Creator) finalize() error {
return err
}
}

// Draw blocks to pages.
block, ok := c.pageBlocks[page]
if !ok {
return errors.New("could not find page block")
}
if err := block.drawToPage(page); err != nil {
return err
}
}

c.finalized = true
Expand Down Expand Up @@ -559,15 +571,21 @@ func (c *Creator) Draw(d Drawable) error {
return err
}

for idx, blk := range blocks {
for idx, block := range blocks {
if idx > 0 {
c.NewPage()
}

p := c.getActivePage()
err := blk.drawToPage(p)
if err != nil {
return err
page := c.getActivePage()
if pageBlock, ok := c.pageBlocks[page]; ok {
if err := pageBlock.mergeBlocks(block); err != nil {
return err
}
if err := mergeResources(block.resources, pageBlock.resources); err != nil {
return err
}
} else {
c.pageBlocks[page] = block
}
}

Expand Down
24 changes: 16 additions & 8 deletions creator/creator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2740,11 +2740,17 @@ func TestCompressStreams(t *testing.T) {
c.Draw(p)
//c.NewPage()

page := c.pages[0]
// Need to add Arial to the page resources to avoid generating invalid PDF (avoid build fail).
times := model.NewStandard14FontMustCompile(model.TimesRomanName)
page.Resources.SetFontByName("Times", times.ToPdfObject())
rawContent := `
c.SetPdfWriterAccessFunc(func(w *model.PdfWriter) error {
page := c.pages[0]

// Need to add Times to the page resources as it is used in the raw content stream.
times, err := model.NewStandard14Font(model.TimesRomanName)
if err != nil {
return err
}
page.Resources.SetFontByName("Times", times.ToPdfObject())

rawContent := `
BT
/Times 56 Tf
20 600 Td
Expand All @@ -2762,15 +2768,17 @@ BT
(example text)'
ET
`
{

cstreams, err := page.GetContentStreams()
if err != nil {
panic(err)
return err
}
cstreams = append(cstreams, rawContent)

// Set streams with raw encoder (not encoded).
page.SetContentStreams(cstreams, core.NewRawEncoder())
}
return nil
})

return c
}
Expand Down
0