Fix annotation copy on paragraph chunk split

This commit is contained in:
Adrian-George Bostan 2019-01-11 19:19:31 +02:00
parent d340ce57e0
commit affe2f74c0
3 changed files with 34 additions and 11 deletions

View File

@ -327,8 +327,15 @@ func (p *StyledParagraph) wrapText() error {
return nil
}
annotation := *src
return &annotation
var annotation *model.PdfAnnotation
switch t := src.GetContext().(type) {
case *model.PdfAnnotationLink:
if annot := copyLinkAnnotation(t); annot != nil {
annotation = annot.PdfAnnotation
}
}
return annotation
}
for _, chunk := range p.chunks {
@ -698,8 +705,7 @@ func drawStyledParagraphOnBlock(blk *Block, p *StyledParagraph, ctx DrawContext)
// Process annotation.
if !chunk.annotationProcessed {
annotCtx := chunk.annotation.GetContext()
switch t := annotCtx.(type) {
switch t := chunk.annotation.GetContext().(type) {
case *model.PdfAnnotationLink:
// Initialize annotation rectangle.
annotRect = core.MakeArray()

View File

@ -76,3 +76,20 @@ func newInternalLinkAnnotation(page int64, x, y, zoom float64) *model.PdfAnnotat
return annotation.PdfAnnotation
}
// copyLinkAnnotation returns a new link annotation based on an existing one.
func copyLinkAnnotation(link *model.PdfAnnotationLink) *model.PdfAnnotationLink {
if link == nil {
return nil
}
annotation := model.NewPdfAnnotationLink()
annotation.BS = link.BS
annotation.A = link.A
if annotDest, ok := link.Dest.(*core.PdfObjectArray); ok {
annotation.Dest = core.MakeArray(annotDest.Elements()...)
}
return annotation
}

View File

@ -158,8 +158,8 @@ func (tl *TOCLine) SetLink(page int64, x, y float64) {
tl.SetStyle(tl.sp.defaultLinkStyle)
}
// getLink returns a new annotation if the line has a link set.
func (tl *TOCLine) getLink() *model.PdfAnnotation {
// getLineLink returns a new annotation if the line has a link set.
func (tl *TOCLine) getLineLink() *model.PdfAnnotation {
if tl.linkPage <= 0 {
return nil
}
@ -186,17 +186,17 @@ func (tl *TOCLine) prepareParagraph(sp *StyledParagraph, ctx DrawContext) {
{
Text: tl.Number.Text,
Style: tl.Number.Style,
annotation: tl.getLink(),
annotation: tl.getLineLink(),
},
{
Text: title,
Style: tl.Title.Style,
annotation: tl.getLink(),
annotation: tl.getLineLink(),
},
{
Text: page,
Style: tl.Page.Style,
annotation: tl.getLink(),
annotation: tl.getLineLink(),
},
}
@ -216,7 +216,7 @@ func (tl *TOCLine) prepareParagraph(sp *StyledParagraph, ctx DrawContext) {
chunk := sp.Insert(2, sepText)
chunk.Style = sepStyle
chunk.annotation = tl.getLink()
chunk.annotation = tl.getLineLink()
// Push page numbers to the end of the line.
availWidth = availWidth - float64(sepCount)*sepWidth
@ -230,7 +230,7 @@ func (tl *TOCLine) prepareParagraph(sp *StyledParagraph, ctx DrawContext) {
chunk = sp.Insert(2, strings.Repeat(" ", spaces))
chunk.Style = style
chunk.annotation = tl.getLink()
chunk.annotation = tl.getLineLink()
}
}
}