Draw header and footer on blocks implemented to simply syntax. Closes #68

This commit is contained in:
Gunnsteinn Hall 2017-07-17 12:20:06 +00:00
parent e91f65a641
commit 0029d4cc8e
2 changed files with 36 additions and 15 deletions

View File

@ -35,8 +35,8 @@ type Creator struct {
genFrontPageFunc func(args FrontpageFunctionArgs)
genTableOfContentFunc func(toc *TableOfContents) (*Chapter, error)
drawHeaderFunc func(args HeaderFunctionArgs)
drawFooterFunc func(args FooterFunctionArgs)
drawHeaderFunc func(header *Block, args HeaderFunctionArgs)
drawFooterFunc func(footer *Block, args FooterFunctionArgs)
finalized bool
@ -152,12 +152,12 @@ func (c *Creator) SetPageSize(size PageSize) {
}
// Set a function to draw a header on created output pages.
func (c *Creator) DrawHeader(drawHeaderFunc func(args HeaderFunctionArgs)) {
func (c *Creator) DrawHeader(drawHeaderFunc func(header *Block, args HeaderFunctionArgs)) {
c.drawHeaderFunc = drawHeaderFunc
}
// Set a function to draw a footer on created output pages.
func (c *Creator) DrawFooter(drawFooterFunc func(args FooterFunctionArgs)) {
func (c *Creator) DrawFooter(drawFooterFunc func(footer *Block, args FooterFunctionArgs)) {
c.drawFooterFunc = drawFooterFunc
}
@ -343,18 +343,39 @@ func (c *Creator) finalize() error {
for idx, page := range c.pages {
c.setActivePage(page)
if c.drawHeaderFunc != nil {
// Prepare a block to draw on.
// Header is drawn on the top of the page. Has width of the page, but height limited to the page
// margin top height.
headerBlock := NewBlock(c.pageWidth, c.pageMargins.top)
args := HeaderFunctionArgs{
PageNum: idx + 1,
TotalPages: totPages,
}
c.drawHeaderFunc(args)
c.drawHeaderFunc(headerBlock, args)
headerBlock.SetPos(0, 0)
err := c.Draw(headerBlock)
if err != nil {
common.Log.Debug("Error drawing header: %v", err)
return err
}
}
if c.drawFooterFunc != nil {
// Prepare a block to draw on.
// Footer is drawn on the bottom of the page. Has width of the page, but height limited to the page
// margin bottom height.
footerBlock := NewBlock(c.pageWidth, c.pageMargins.bottom)
args := FooterFunctionArgs{
PageNum: idx + 1,
TotalPages: totPages,
}
c.drawFooterFunc(args)
c.drawFooterFunc(footerBlock, args)
footerBlock.SetPos(0, c.pageHeight-footerBlock.height)
err := c.Draw(footerBlock)
if err != nil {
common.Log.Debug("Error drawing footer: %v", err)
return err
}
}
}

View File

@ -1071,7 +1071,7 @@ func TestTableInSubchapter(t *testing.T) {
// Add headers and footers via creator.
func addHeadersAndFooters(c *Creator) {
c.DrawHeader(func(args HeaderFunctionArgs) {
c.DrawHeader(func(header *Block, args HeaderFunctionArgs) {
/*
if pageNum == 1 {
// Skip on front Page.
@ -1081,8 +1081,8 @@ func addHeadersAndFooters(c *Creator) {
// Add Page number
p := NewParagraph(fmt.Sprintf("Page %d / %d", args.PageNum, args.TotalPages))
p.SetPos(0.8*c.pageWidth, 20)
c.Draw(p)
p.SetPos(0.8*header.Width(), 20)
header.Draw(p)
// Draw on the template...
img, err := NewImageFromFile(testImageFile1)
@ -1092,10 +1092,10 @@ func addHeadersAndFooters(c *Creator) {
img.ScaleToHeight(0.4 * c.pageMargins.top)
img.SetPos(20, 10)
c.Draw(img)
header.Draw(img)
})
c.DrawFooter(func(args FooterFunctionArgs) {
c.DrawFooter(func(footer *Block, args FooterFunctionArgs) {
/*
if pageNum == 1 {
// Skip on front Page.
@ -1106,12 +1106,12 @@ func addHeadersAndFooters(c *Creator) {
// Add company name.
companyName := "Company inc."
p := NewParagraph(companyName)
p.SetPos(0.1*c.pageWidth, c.pageHeight-c.pageMargins.bottom+10)
c.Draw(p)
p.SetPos(0.1*footer.Width(), 10)
footer.Draw(p)
p = NewParagraph("July 2017")
p.SetPos(0.8*c.pageWidth, c.pageHeight-c.pageMargins.bottom+10)
c.Draw(p)
p.SetPos(0.8*footer.Width(), 10)
footer.Draw(p)
})
}