Skip to content

Commit

Permalink
Merge branch 'jacobalberty-Issue316CopyChanges'
Browse files Browse the repository at this point in the history
  • Loading branch information
jung-kurt committed Nov 7, 2019
2 parents f4594b6 + f9f99ed commit 47a2c24
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 11 deletions.
19 changes: 19 additions & 0 deletions fpdf_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2813,3 +2813,22 @@ func ExampleFpdf_SetTextRenderingMode() {
// Output:
// Successfully generated pdf/Fpdf_TextRenderingMode.pdf
}

// TestIssue0316 addresses issue 316 in which AddUTF8FromBytes modifies its argument
// utf8bytes resulting in a panic if you generate two PDFs with the "same" font bytes.
func TestIssue0316(t *testing.T) {
pdf := gofpdf.New(gofpdf.OrientationPortrait, "mm", "A4", "")
pdf.AddPage()
fontBytes, _ := ioutil.ReadFile(example.FontFile("DejaVuSansCondensed.ttf"))
ofontBytes := append([]byte{}, fontBytes...)
pdf.AddUTF8FontFromBytes("dejavu", "", fontBytes)
pdf.SetFont("dejavu", "", 16)
pdf.Cell(40, 10, "Hello World!")
fileStr := example.Filename("TestIssue0316")
err := pdf.OutputFileAndClose(fileStr)
example.Summary(err, fileStr)
pdf.AddPage()
if !bytes.Equal(fontBytes, ofontBytes) {
t.Fatal("Font data changed during pdf generation")
}
}
16 changes: 5 additions & 11 deletions utf8fontfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,6 @@ func (fr *fileReader) Read(s int) []byte {
return b
}

func (fr *fileReader) ReadLikeCopy(s int) []byte {
b := make([]byte, 0, s)
b = append(b, fr.array[fr.readerPosition:fr.readerPosition+int64(s)]...)
fr.readerPosition += int64(s)
return b
}

func (fr *fileReader) seek(shift int64, flag int) (int64, error) {
if flag == 0 {
fr.readerPosition = shift
Expand Down Expand Up @@ -222,6 +215,7 @@ func (utf *utf8FontFile) getUint16(pos int) int {
}

func (utf *utf8FontFile) splice(stream []byte, offset int, value []byte) []byte {
stream = append([]byte{}, stream...)
return append(append(stream[:offset], value...), stream[offset+len(value):]...)
}

Expand Down Expand Up @@ -249,7 +243,7 @@ func (utf *utf8FontFile) getTableData(name string) []byte {
return nil
}
_, _ = utf.fileReader.seek(int64(desckrip.position), 0)
s := utf.fileReader.ReadLikeCopy(desckrip.size)
s := utf.fileReader.Read(desckrip.size)
return s
}

Expand Down Expand Up @@ -896,10 +890,10 @@ func (utf *utf8FontFile) getMetrics(metricCount, gid int) []byte {
var metrics []byte
if gid < metricCount {
utf.seek(start + (gid * 4))
metrics = utf.fileReader.ReadLikeCopy(4)
metrics = utf.fileReader.Read(4)
} else {
utf.seek(start + ((metricCount - 1) * 4))
metrics = utf.fileReader.ReadLikeCopy(2)
metrics = utf.fileReader.Read(2)
utf.seek(start + (metricCount * 2) + (gid * 2))
metrics = append(metrics, utf.fileReader.Read(2)...)
}
Expand Down Expand Up @@ -1021,7 +1015,7 @@ func (utf *utf8FontFile) assembleTables() []byte {
}

for _, key := range tablesNames {
data := tables[key]
data := append([]byte{}, tables[key]...)
data = append(data, []byte{0, 0, 0}...)
answer = append(answer, data[:(len(data)&^3)]...)
}
Expand Down

0 comments on commit 47a2c24

Please sign in to comment.