Skip to content

Commit

Permalink
More bytes.Buffer removal
Browse files Browse the repository at this point in the history
  • Loading branch information
CaptainCodeman committed Sep 25, 2017
1 parent 29752b7 commit e32af7e
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 19 deletions.
45 changes: 31 additions & 14 deletions cache_content_text.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package gopdf

import (
"bytes"
"errors"
"fmt"
"io"
Expand Down Expand Up @@ -29,9 +28,8 @@ type cacheContentText struct {
contentType int
cellOpt CellOption
lineWidth float64
text string
//---result---
content bytes.Buffer
text bytes.Buffer
cellWidthPdfUnit, textWidthPdfUnit float64
cellHeightPdfUnit float64
}
Expand Down Expand Up @@ -137,7 +135,33 @@ func (c *cacheContentText) write(w io.Writer, protection *PDFProtection) error {
//c.AppendStreamSetGrayFill(grayFill)
}

io.WriteString(w, "[<"+c.content.String()+">] TJ\n")
io.WriteString(w, "[<")

unitsPerEm := int(c.fontSubset.ttfp.UnitsPerEm())
var leftRune rune
var leftRuneIndex uint
for i, r := range c.text {

glyphindex, err := c.fontSubset.CharIndex(r)
if err != nil {
return err
}

pairvalPdfUnit := 0
if i > 0 && c.fontSubset.ttfFontOption.UseKerning { //kerning
pairval := kern(c.fontSubset, leftRune, r, leftRuneIndex, glyphindex)
pairvalPdfUnit = convertTTFUnit2PDFUnit(int(pairval), unitsPerEm)
if pairvalPdfUnit != 0 {
fmt.Fprintf(w, ">%d<", (-1)*pairvalPdfUnit)
}
}

fmt.Fprintf(w, "%04X", glyphindex)
leftRune = r
leftRuneIndex = glyphindex
}

io.WriteString(w, ">] TJ\n")
io.WriteString(w, "ET\n")

if strings.ToUpper(c.fontStyle) == "U" {
Expand Down Expand Up @@ -225,8 +249,7 @@ func (c *cacheContentText) underline(w io.Writer, startX float64, startY float64

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

c.content.Truncate(0) //clear
cellWidthPdfUnit, cellHeightPdfUnit, textWidthPdfUnit, err := createContent(c.fontSubset, c.text.String(), c.fontSize, c.rectangle, &c.content)
cellWidthPdfUnit, cellHeightPdfUnit, textWidthPdfUnit, err := createContent(c.fontSubset, c.text, c.fontSize, c.rectangle)
if err != nil {
return 0, 0, err
}
Expand All @@ -236,7 +259,7 @@ func (c *cacheContentText) createContent() (float64, float64, error) {
return cellWidthPdfUnit, cellHeightPdfUnit, nil
}

func createContent(f *SubsetFontObj, text string, fontSize int, rectangle *Rect, out *bytes.Buffer) (float64, float64, float64, error) {
func createContent(f *SubsetFontObj, text string, fontSize int, rectangle *Rect) (float64, float64, float64, error) {

unitsPerEm := int(f.ttfp.UnitsPerEm())
var leftRune rune
Expand All @@ -254,14 +277,8 @@ func createContent(f *SubsetFontObj, text string, fontSize int, rectangle *Rect,
if i > 0 && f.ttfFontOption.UseKerning { //kerning
pairval := kern(f, leftRune, r, leftRuneIndex, glyphindex)
pairvalPdfUnit = convertTTFUnit2PDFUnit(int(pairval), unitsPerEm)
if pairvalPdfUnit != 0 && out != nil {
out.WriteString(fmt.Sprintf(">%d<", (-1)*pairvalPdfUnit))
}
}

if out != nil {
out.WriteString(fmt.Sprintf("%04X", glyphindex))
}
width, err := f.CharWidth(r)
if err != nil {
return 0, 0, 0, err
Expand Down Expand Up @@ -348,5 +365,5 @@ func (c *CacheContent) Setup(rectangle *Rect,

//WriteTextToContent write text to content
func (c *CacheContent) WriteTextToContent(text string) {
c.cacheContentText.text.WriteString(text)
c.cacheContentText.text += text
}
2 changes: 1 addition & 1 deletion gopdf.go
Original file line number Diff line number Diff line change
Expand Up @@ -636,7 +636,7 @@ func (gp *GoPdf) MeasureTextWidth(text string) (float64, error) {
return 0, err
}

_, _, textWidthPdfUnit, err := createContent(gp.curr.Font_ISubset, text, gp.curr.Font_Size, nil, nil)
_, _, textWidthPdfUnit, err := createContent(gp.curr.Font_ISubset, text, gp.curr.Font_Size, nil)
if err != nil {
return 0, err
}
Expand Down
5 changes: 1 addition & 4 deletions list_cache_content.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,7 @@ func (l *listCacheContent) appendContentText(cache cacheContentText, text string
}

//start add text
_, err := cacheFont.text.WriteString(text)
if err != nil {
return x, y, err
}
cacheFont.text += text

//re-create contnet
textWidthPdfUnit, textHeightPdfUnit, err := cacheFont.createContent()
Expand Down

0 comments on commit e32af7e

Please sign in to comment.