Skip to content

Commit

Permalink
Support newline wrapping in paragraph
Browse files Browse the repository at this point in the history
  • Loading branch information
gunnsth committed Jul 18, 2018
1 parent be8d19b commit b36101a
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 5 deletions.
19 changes: 14 additions & 5 deletions pdf/creator/creator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -972,9 +972,9 @@ func TestTableCellWrapping(t *testing.T) {

cell = table.NewCell()
p = NewParagraph("C 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.")
p.SetEnableWrap(true)
cell.SetContent(p)
cell.SetBorder(CellBorderStyleBox, 1)
p.SetEnableWrap(true)

cell = table.NewCell()
p = NewParagraph("1,4")
Expand Down Expand Up @@ -1019,12 +1019,21 @@ func TestTableCellWrapping(t *testing.T) {
cell.SetContent(p)
cell.SetBackgroundColor(ColorRGBFrom8bit(255, 0, 0))

c.Draw(table)
table.SkipRows(1)
cell = table.NewCell()
cell.SetBorder(CellBorderStyleBox, 1)
p = NewParagraph("This is\nnewline\nwrapped")
p.SetEnableWrap(true)
cell.SetContent(p)

err := c.WriteToFile("/tmp/tablecell_wrap.pdf")
err := c.Draw(table)
if err != nil {
t.Errorf("Fail: %v\n", err)
return
t.Fatalf("Error drawing: %v", err)
}

err = c.WriteToFile("/tmp/tablecell_wrap.pdf")
if err != nil {
t.Fatalf("Fail: %v\n", err)
}
}

Expand Down
19 changes: 19 additions & 0 deletions pdf/creator/paragraph.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,11 @@ func (p *Paragraph) getTextWidth() float64 {
return -1 // XXX/FIXME: return error.
}

// Ignore newline for this.. Handles as if all in one line.
if glyph == "controlLF" {
continue
}

metrics, found := p.textFont.GetGlyphCharMetrics(glyph)
if !found {
common.Log.Debug("Glyph char metrics not found! %s\n", glyph)
Expand Down Expand Up @@ -254,6 +259,17 @@ func (p *Paragraph) wrapText() error {
return errors.New("Glyph not found for rune") // XXX/FIXME: return error.
}

// Newline wrapping.
if glyph == "controlLF" {
// Moves to next line.
p.textLines = append(p.textLines, string(line))
line = []rune{}
lineWidth = 0
widths = []float64{}
glyphs = []string{}
continue
}

metrics, found := p.textFont.GetGlyphCharMetrics(glyph)
if !found {
common.Log.Debug("Glyph char metrics not found! %s\n", glyph)
Expand Down Expand Up @@ -428,6 +444,9 @@ func drawParagraphOnBlock(blk *Block, p *Paragraph, ctx DrawContext) (DrawContex
spaces++
continue
}
if glyph == "controlLF" {
continue
}
metrics, found := p.textFont.GetGlyphCharMetrics(glyph)
if !found {
common.Log.Debug("Unsupported glyph %s in font\n", glyph)
Expand Down

0 comments on commit b36101a

Please sign in to comment.