Skip to content
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

Add resources of blocks created from pages to the output page resources #104

Merged
merged 1 commit into from
Jul 2, 2019
Merged
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: 28 additions & 0 deletions creator/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,12 @@ func (blk *Block) drawToPage(page *model.PdfPage) error {
return err
}

// Merge resources for blocks which were created from pages.
// Necessary for adding resources which do not appear in the block contents.
if err = mergeResources(blk.resources, page.Resources); err != nil {
return err
}

err = page.SetContentStreams([]string{string(ops.Bytes())}, core.NewFlateEncoder())
if err != nil {
return err
Expand Down Expand Up @@ -583,6 +589,28 @@ func mergeContents(contents *contentstream.ContentStreamOperations, resources *m
return nil
}

// mergeResources adds all resources from src which are missing from dst.
// For now, the method only merges colorspaces.
func mergeResources(src, dst *model.PdfPageResources) error {
// Merge colorspaces.
colorspaces, _ := src.GetColorspaces()
if colorspaces != nil && len(colorspaces.Colorspaces) > 0 {
for name, colorspace := range colorspaces.Colorspaces {
colorspaceName := *core.MakeName(name)
if dst.HasColorspaceByName(colorspaceName) {
continue
}

err := dst.SetColorspaceByName(colorspaceName, colorspace)
if err != nil {
return err
}
}
}

return nil
}

func resourcesNextUnusedFontName(name string, font core.PdfObject, resources *model.PdfPageResources) core.PdfObjectName {
prefix := strings.TrimRightFunc(strings.TrimSpace(name), func(r rune) bool {
return unicode.IsNumber(r)
Expand Down