Skip to content

Commit

Permalink
Merge branch 'liuben-master'
Browse files Browse the repository at this point in the history
  • Loading branch information
oneplus1000 committed Sep 18, 2022
2 parents 58fc4ac + 89a4e7a commit 2dec1a9
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 0 deletions.
62 changes: 62 additions & 0 deletions gopdf.go
Expand Up @@ -13,6 +13,7 @@ import (
"math"
"os"
"strconv"
"strings"
"time"

"github.com/phpdave11/gofpdi"
Expand Down Expand Up @@ -1092,6 +1093,67 @@ func (gp *GoPdf) MultiCell(rectangle *Rect, text string) error {
return nil
}

// IsFitMultiCell : check whether the rectangle's area is big enough for the text
func (gp *GoPdf) IsFitMultiCell(rectangle *Rect, text string) (bool, float64, error) {
var line []rune
var totalLineHeight float64
length := len([]rune(text))

// get lineHeight
text, err := gp.curr.FontISubset.AddChars(text)
if err != nil {
return false, totalLineHeight, err
}
_, lineHeight, _, err := createContent(gp.curr.FontISubset, text, gp.curr.FontSize, nil)

if err != nil {
return false, totalLineHeight, err
}
gp.PointsToUnitsVar(&lineHeight)

for i, v := range []rune(text) {
if totalLineHeight+lineHeight > rectangle.H {
return false, totalLineHeight, nil
}
lineWidth, _ := gp.MeasureTextWidth(string(line))
runeWidth, _ := gp.MeasureTextWidth(string(v))

if lineWidth+runeWidth > rectangle.W {
totalLineHeight += lineHeight
line = nil
}

line = append(line, v)

if i == length-1 {
totalLineHeight += lineHeight
}
}

ok := true
if totalLineHeight > rectangle.H {
ok = false
}

return ok, totalLineHeight, nil
}

// IsFitMultiCellWithNewline : similar to IsFitMultiCell, but process char newline as Br
func (gp *GoPdf) IsFitMultiCellWithNewline(rectangle *Rect, text string) (bool, float64, error) {
r := *rectangle
strs := strings.Split(text, "\n")

for _, s := range strs {
ok, height, err := gp.IsFitMultiCell(&r, s)
if err != nil || !ok {
return false, 0, err
}
r.H -= height
}

return true, rectangle.H - r.H, nil
}

// MultiCellWithOption create of text with line breaks ( use current x,y is upper-left corner of cell)
func (gp *GoPdf) MultiCellWithOption(rectangle *Rect, text string, opt CellOption) error {
transparency, err := gp.getCachedTransparency(opt.Transparency)
Expand Down
3 changes: 3 additions & 0 deletions page_sizes.go
Expand Up @@ -36,6 +36,9 @@ var PageSizeA3 = &Rect{W: 842, H: 1190, unitOverride: UnitPT}
// PageSizeA4 page format
var PageSizeA4 = &Rect{W: 595, H: 842, unitOverride: UnitPT}

// PageSizeA4Landscape page format
var PageSizeA4Landscape = &Rect{W: 842, H: 595, unitOverride: UnitPT}

// PageSizeA4Small page format
var PageSizeA4Small = &Rect{W: 595, H: 842, unitOverride: UnitPT}

Expand Down

0 comments on commit 2dec1a9

Please sign in to comment.