Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pdf_test.go TestPDFText #93

Closed
rickb777 opened this issue Jun 22, 2021 · 2 comments
Closed

pdf_test.go TestPDFText #93

rickb777 opened this issue Jun 22, 2021 · 2 comments

Comments

@rickb777
Copy link
Contributor

I have been rendering text to PDF using canvas and I tried filling in the incomplete TestPDFText like this, in order to gain more understanding of it.

func TestPDFText(t *testing.T) {
	const fontDir = "../../resources/"
	dejaVuSerif := canvas.NewFontFamily("dejavu-serif")
	err := dejaVuSerif.LoadFontFile(fontDir+"DejaVuSerif.ttf", canvas.FontRegular)
	test.Error(t, err)

	ebGaramond := canvas.NewFontFamily("eb-garamond")
	err = ebGaramond.LoadFontFile(fontDir+"EBGaramond12-Regular.otf", canvas.FontRegular)
	test.Error(t, err)

	dejaVu8 := dejaVuSerif.Face(8, canvas.Black, canvas.FontRegular, canvas.FontNormal)
	dejaVu12 := dejaVuSerif.Face(12, canvas.Red, canvas.FontRegular, canvas.FontNormal, canvas.FontUnderline)
	dejaVu12sub := dejaVuSerif.Face(12, canvas.Black, canvas.FontRegular, canvas.FontSubscript)
	garamond10 := ebGaramond.Face(10, canvas.Black, canvas.FontBold, canvas.FontNormal)

	rt := canvas.NewRichText(dejaVu12)
	rt.Add(dejaVu8, "dejaVu8")
	rt.Add(dejaVu12, " glyphspacing")
	rt.Add(dejaVu12sub, " dejaVu12sub")
	rt.Add(garamond10, " garamond10")
	text := rt.ToText(dejaVu12.TextWidth("glyphspacing")+float64(len("glyphspacing")-1), 100.0, canvas.Justify, canvas.Top, 0.0, 0.0)

	buf := &bytes.Buffer{}
	pdf := New(buf, 210, 297, &Options{Compress: false, SubsetFonts: false})
	pdf.NewPage(210.0, 297.0)

	if testing.Verbose() {
		defer func() {
			f, _ := os.Create("TestPDFText.pdf")
			pdf.w.WriteTo(f)
			pdf.Close()
			f.Close()
		}()
	}

	pdf.RenderText(text, canvas.Identity)
	//textLayer{text, Identity}.WritePDF(pdf) // this actually gives coverage to PDF font embedding, which we don't test...
	//TODO test.String(t, pdf.w.String(), " BT /F0 8 Tf 0 -7.421875 Td[(\x00G\x00H\x00M\x00D\x009) 63 (\x00X\x00\x1B)]TJ 1 0 0 rg 1 0 .3 1 0 -20.453125 Tm 1 Tc[(\x00J\x00O\x00\\\x00S\x00K\x00V\x00S\x00D\x00F\x00L\x00Q\x00J)]TJ 0 g 1 0 0 1 0 -29.765625 Tm 0 Tc 2 Tr .27984 w[(\x00G\x00H\x00M\x00D\x009) 63 (\x00X\x00\x14\x00\x15\x00V\x00X\x00E)]TJ /F1 10 Tf 0 -8.734375 Td .4 w[(\x00H\x00B\x00S\x00B\x00N\x00P\x00O\x00E\x00\x12\x00\x11)]TJ ET 1 0 0 rg 0 -22.703125 m 91.71875 -22.703125 l 91.71875 -21.803125 l 0 -21.803125 l f")
}

However, I'm completely unsure about the last line marked TODO - as it stands the test would fail.

@tdewolff
Copy link
Owner

Thanks for the heads up! Test coverage is fairly incomplete for canvas, and this is one part of it. The PDF text rendering remains untested as of now, but I've tried to add coverage in the past, which is what is commented. The glyph IDs as referenced by the TJ command have been changed by various iterations of the PDF renderer, which makes it necessary to rewrite the test that is commented. It's fairly low priority for me now though, will attend to it some time soon!

@tdewolff
Copy link
Owner

I will merge this with #74 under "Increase test coverage"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants