Expose subchapter paragraph heading for styling. Add capability to hide numbering, and remove from TOC. Closes #69

This commit is contained in:
Gunnsteinn Hall 2017-07-14 12:56:42 +00:00
parent dfb80a2dd1
commit ec4847d328
4 changed files with 65 additions and 6 deletions

View File

@ -413,6 +413,35 @@ func TestParagraphWrapping(t *testing.T) {
}
}
func TestParagraphWrapping2(t *testing.T) {
creator := New()
p := NewParagraph("Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt" +
"ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut " +
"aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore" +
"eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt " +
"mollit anim id est laborum.")
alignments := []TextAlignment{TextAlignmentLeft, TextAlignmentJustify, TextAlignmentCenter, TextAlignmentRight}
for j := 0; j < 25; j++ {
//p.SetAlignment(alignments[j%4])
p.SetMargins(50, 50, 50, 50)
p.SetTextAlignment(alignments[1])
err := creator.Draw(p)
if err != nil {
t.Errorf("Fail: %v\n", err)
return
}
}
err := creator.WriteToFile("/tmp/2_pwrap2.pdf")
if err != nil {
t.Errorf("Fail: %v\n", err)
return
}
}
// Test writing with various TTF fonts. Assumes MacOS system, where fonts are stored under /Library/Fonts.
func TestParagraphFonts(t *testing.T) {
creator := New()

View File

@ -6,6 +6,6 @@
//
// The creator is used for quickly generating pages and content with a simple interface.
// It is built on top of the model package to provide access to the most common
// operations such as creating, drawing, ...
// operations such as creating text and image reports and manipulating existing pages.
//
package creator

View File

@ -17,9 +17,6 @@ import (
"github.com/unidoc/unidoc/pdf/model/textencoding"
)
// XXX/TODO: Under consideration. Should allow paragraph to scale? Makes more sense to change font size.
// Alternatively can draw to a block and scale the block, if need to fit into a specific slot.
// A paragraph represents text drawn with a specified font and can wrap across lines and pages.
// By default occupies the available width in the drawing context.
type paragraph struct {

View File

@ -22,6 +22,12 @@ type subchapter struct {
contents []Drawable
// Show chapter numbering
showNumbering bool
// Include in TOC.
includeInTOC bool
// Positioning: relative / absolute.
positioning positioning
@ -50,6 +56,9 @@ func (c *Creator) NewSubchapter(ch *Chapter, title string) *subchapter {
p.SetFontSize(14)
p.SetFont(fonts.NewFontHelvetica()) // bold?
subchap.showNumbering = true
subchap.includeInTOC = true
subchap.heading = p
subchap.contents = []Drawable{}
@ -62,6 +71,28 @@ func (c *Creator) NewSubchapter(ch *Chapter, title string) *subchapter {
return subchap
}
// Set flag to indicate whether or not to show chapter numbers as part of title.
func (subchap *subchapter) SetShowNumbering(show bool) {
if show {
heading := fmt.Sprintf("%d.%d. %s", subchap.chapterNum, subchap.subchapterNum, subchap.title)
subchap.heading.SetText(heading)
} else {
heading := fmt.Sprintf("%s", subchap.title)
subchap.heading.SetText(heading)
}
subchap.showNumbering = show
}
// Set flag to indicate whether or not to include in the table of contents.
func (subchap *subchapter) SetIncludeInTOC(includeInTOC bool) {
subchap.includeInTOC = includeInTOC
}
// Get access to the heading paragraph to address style etc.
func (subchap *subchapter) GetHeading() *paragraph {
return subchap.heading
}
// Set absolute coordinates.
func (subchap *subchapter) SetPos(x, y float64) {
subchap.positioning = positionAbsolute
@ -105,8 +136,10 @@ func (subchap *subchapter) GeneratePageBlocks(ctx DrawContext) ([]*Block, DrawCo
if len(blocks) > 1 {
ctx.Page++ // did not fit - moved to next Page.
}
// Add to TOC.
subchap.toc.add(subchap.title, subchap.chapterNum, subchap.subchapterNum, ctx.Page)
if subchap.includeInTOC {
// Add to TOC.
subchap.toc.add(subchap.title, subchap.chapterNum, subchap.subchapterNum, ctx.Page)
}
for _, d := range subchap.contents {
newBlocks, c, err := d.GeneratePageBlocks(ctx)