Skip to content

Commit

Permalink
Merge branch 'mbrgm-add-charspacing-support'
Browse files Browse the repository at this point in the history
  • Loading branch information
oneplus1000 committed May 10, 2023
2 parents c1f2ceb + 11aa243 commit be2f485
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 8 deletions.
16 changes: 12 additions & 4 deletions cache_content_text.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ type cacheContentText struct {
fontCountIndex int //Curr.FontFontCount+1
fontSize float64
fontStyle int
charSpacing float64
setXCount int //จำนวนครั้งที่ใช้ setX
x, y float64
fontSubset *SubsetFontObj
Expand All @@ -53,6 +54,7 @@ func (c *cacheContentText) isSame(cache cacheContentText) bool {
c.fontCountIndex == cache.fontCountIndex &&
c.fontSize == cache.fontSize &&
c.fontStyle == cache.fontStyle &&
c.charSpacing == cache.charSpacing &&
c.setXCount == cache.setXCount &&
c.y == cache.y {
return true
Expand Down Expand Up @@ -149,7 +151,7 @@ func (c *cacheContentText) write(w io.Writer, protection *PDFProtection) error {
}

fmt.Fprintf(w, "%0.2f %0.2f TD\n", x, y)
fmt.Fprintf(w, "/F%d %s Tf\n", c.fontCountIndex, FormatFloatTrim(c.fontSize))
fmt.Fprintf(w, "/F%d %s Tf %s Tc\n", c.fontCountIndex, FormatFloatTrim(c.fontSize), FormatFloatTrim(c.charSpacing))

if c.txtColorMode == "color" {
c.textColor.write(w, protection)
Expand Down Expand Up @@ -291,7 +293,7 @@ func (c *cacheContentText) underline(w io.Writer) error {

func (c *cacheContentText) createContent() (float64, float64, error) {

cellWidthPdfUnit, cellHeightPdfUnit, textWidthPdfUnit, err := createContent(c.fontSubset, c.text, c.fontSize, c.rectangle)
cellWidthPdfUnit, cellHeightPdfUnit, textWidthPdfUnit, err := createContent(c.fontSubset, c.text, c.fontSize, c.charSpacing, c.rectangle)
if err != nil {
return 0, 0, err
}
Expand All @@ -301,7 +303,7 @@ func (c *cacheContentText) createContent() (float64, float64, error) {
return cellWidthPdfUnit, cellHeightPdfUnit, nil
}

func createContent(f *SubsetFontObj, text string, fontSize float64, rectangle *Rect) (float64, float64, float64, error) {
func createContent(f *SubsetFontObj, text string, fontSize float64, charSpacing float64, rectangle *Rect) (float64, float64, float64, error) {

unitsPerEm := int(f.ttfp.UnitsPerEm())
var leftRune rune
Expand All @@ -328,7 +330,11 @@ func createContent(f *SubsetFontObj, text string, fontSize float64, rectangle *R
return 0, 0, 0, err
}

sumWidth += int(width) + int(pairvalPdfUnit)
unitsPerPt := float64(unitsPerEm) / fontSize
spaceWidthInPt := unitsPerPt * charSpacing
spaceWidthPdfUnit := convertTTFUnit2PDFUnit(int(spaceWidthInPt), unitsPerEm)

sumWidth += int(width) + int(pairvalPdfUnit) + spaceWidthPdfUnit
leftRune = r
leftRuneIndex = glyphindex
}
Expand Down Expand Up @@ -381,6 +387,7 @@ func (c *CacheContent) Setup(rectangle *Rect,
fontCountIndex int, //Curr.FontFontCount+1
fontSize float64,
fontStyle int,
charSpacing float64,
setXCount int, //จำนวนครั้งที่ใช้ setX
x, y float64,
fontSubset *SubsetFontObj,
Expand All @@ -397,6 +404,7 @@ func (c *CacheContent) Setup(rectangle *Rect,
fontCountIndex: fontCountIndex,
fontSize: fontSize,
fontStyle: fontStyle,
charSpacing: charSpacing,
setXCount: setXCount,
x: x,
y: y,
Expand Down
4 changes: 4 additions & 0 deletions content_obj.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ func (c *ContentObj) AppendStreamText(text string) error {
fontCountIndex := c.getRoot().curr.FontFontCount + 1
fontSize := c.getRoot().curr.FontSize
fontStyle := c.getRoot().curr.FontStyle
charSpacing := c.getRoot().curr.CharSpacing
x := c.getRoot().curr.X
y := c.getRoot().curr.Y
setXCount := c.getRoot().curr.setXCount
Expand All @@ -122,6 +123,7 @@ func (c *ContentObj) AppendStreamText(text string) error {
fontCountIndex: fontCountIndex,
fontSize: fontSize,
fontStyle: fontStyle,
charSpacing: charSpacing,
setXCount: setXCount,
x: x,
y: y,
Expand Down Expand Up @@ -149,6 +151,7 @@ func (c *ContentObj) AppendStreamSubsetFont(rectangle *Rect, text string, cellOp
fontCountIndex := c.getRoot().curr.FontFontCount + 1
fontSize := c.getRoot().curr.FontSize
fontStyle := c.getRoot().curr.FontStyle
charSpacing := c.getRoot().curr.CharSpacing
x := c.getRoot().curr.X
y := c.getRoot().curr.Y
setXCount := c.getRoot().curr.setXCount
Expand All @@ -162,6 +165,7 @@ func (c *ContentObj) AppendStreamSubsetFont(rectangle *Rect, text string, cellOp
fontCountIndex: fontCountIndex,
fontSize: fontSize,
fontStyle: fontStyle,
charSpacing: charSpacing,
setXCount: setXCount,
x: x,
y: y,
Expand Down
2 changes: 2 additions & 0 deletions current.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ type Current struct {
FontFontCount int
FontType int // CURRENT_FONT_TYPE_IFONT or CURRENT_FONT_TYPE_SUBSET

CharSpacing float64

FontISubset *SubsetFontObj // FontType == CURRENT_FONT_TYPE_SUBSET

//page
Expand Down
15 changes: 11 additions & 4 deletions gopdf.go
Original file line number Diff line number Diff line change
Expand Up @@ -940,6 +940,13 @@ func (gp *GoPdf) SetFontSize(fontSize float64) error {
return nil
}

// SetCharSpacing : set the character spacing of the currently active font
func (gp *GoPdf) SetCharSpacing(charSpacing float64) error {
gp.UnitsToPointsVar(&charSpacing)
gp.curr.CharSpacing = charSpacing
return nil
}

// WritePdf : write pdf file
func (gp *GoPdf) WritePdf(pdfPath string) error {
return ioutil.WriteFile(pdfPath, gp.GetBytesPdf(), 0644)
Expand Down Expand Up @@ -1098,7 +1105,7 @@ func (gp *GoPdf) MultiCell(rectangle *Rect, text string) error {
if err != nil {
return err
}
_, lineHeight, _, err := createContent(gp.curr.FontISubset, text, gp.curr.FontSize, nil)
_, lineHeight, _, err := createContent(gp.curr.FontISubset, text, gp.curr.FontSize, gp.curr.CharSpacing, nil)
if err != nil {
return err
}
Expand Down Expand Up @@ -1141,7 +1148,7 @@ func (gp *GoPdf) IsFitMultiCell(rectangle *Rect, text string) (bool, float64, er
if err != nil {
return false, totalLineHeight, err
}
_, lineHeight, _, err := createContent(gp.curr.FontISubset, text, gp.curr.FontSize, nil)
_, lineHeight, _, err := createContent(gp.curr.FontISubset, text, gp.curr.FontSize, gp.curr.CharSpacing, nil)

if err != nil {
return false, totalLineHeight, err
Expand Down Expand Up @@ -1212,7 +1219,7 @@ func (gp *GoPdf) MultiCellWithOption(rectangle *Rect, text string, opt CellOptio
if err != nil {
return err
}
_, lineHeight, _, err := createContent(gp.curr.FontISubset, text, gp.curr.FontSize, nil)
_, lineHeight, _, err := createContent(gp.curr.FontISubset, text, gp.curr.FontSize, gp.curr.CharSpacing, nil)
if err != nil {
return err
}
Expand Down Expand Up @@ -1666,7 +1673,7 @@ func (gp *GoPdf) MeasureTextWidth(text string) (float64, error) {
return 0, err
}

_, _, textWidthPdfUnit, err := createContent(gp.curr.FontISubset, text, gp.curr.FontSize, nil)
_, _, textWidthPdfUnit, err := createContent(gp.curr.FontISubset, text, gp.curr.FontSize, gp.curr.CharSpacing, nil)
if err != nil {
return 0, err
}
Expand Down

0 comments on commit be2f485

Please sign in to comment.