From 4a228ee23cb1cdb372e5464807c320eae3b3da2d Mon Sep 17 00:00:00 2001 From: Nick White Date: Fri, 1 Nov 2019 16:50:19 +0000 Subject: [PATCH 1/2] Add SetTextRenderingMode function This follows section 9.3.6 of the PDF32000_2008 specification. --- fpdf.go | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/fpdf.go b/fpdf.go index cc47d162..26b436f1 100644 --- a/fpdf.go +++ b/fpdf.go @@ -2226,6 +2226,20 @@ func (f *Fpdf) SetWordSpacing(space float64) { f.out(sprintf("%.5f Tw", space*f.k)) } +// SetTextRenderingMode sets the rendering mode of following text. +// The mode can be as follows: +// 0: Fill text +// 1: Stroke text +// 2: Fill, then stroke text +// 3: Neither fill nor stroke text (invisible) +// 4: Fill text and add to path for clipping +// 5: Stroke text and add to path for clipping +// 6: Fills then stroke text and add to path for clipping +// 7: Add text to path for clipping +func (f *Fpdf) SetTextRenderingMode(mode int) { + f.out(sprintf("%d Tr", mode)) +} + // SetAcceptPageBreakFunc allows the application to control where page breaks // occur. // From 465a102180bbde0d03cacd1bb19a743a5a08499a Mon Sep 17 00:00:00 2001 From: Kurt Date: Sat, 2 Nov 2019 11:43:46 -0400 Subject: [PATCH 2/2] Add parameter guard and example for SetTextRenderingMode method --- fpdf.go | 5 ++++- fpdf_test.go | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+), 1 deletion(-) diff --git a/fpdf.go b/fpdf.go index 26b436f1..cd9ee7e3 100644 --- a/fpdf.go +++ b/fpdf.go @@ -2236,8 +2236,11 @@ func (f *Fpdf) SetWordSpacing(space float64) { // 5: Stroke text and add to path for clipping // 6: Fills then stroke text and add to path for clipping // 7: Add text to path for clipping +// This method is demonstrated in the SetTextRenderingMode example. func (f *Fpdf) SetTextRenderingMode(mode int) { - f.out(sprintf("%d Tr", mode)) + if mode >= 0 && mode <= 7 { + f.out(sprintf("%d Tr", mode)) + } } // SetAcceptPageBreakFunc allows the application to control where page breaks diff --git a/fpdf_test.go b/fpdf_test.go index 0bcd2f6e..761cb0e3 100644 --- a/fpdf_test.go +++ b/fpdf_test.go @@ -2765,3 +2765,38 @@ func ExampleFpdf_Cell_strikeout() { // Output: // Successfully generated pdf/Fpdf_Cell_strikeout.pdf } + +// ExampleFpdf_SetTextRenderingMode demonstrates rendering modes in PDFs. +func ExampleFpdf_SetTextRenderingMode() { + + pdf := gofpdf.New("P", "mm", "A4", "") // 210mm x 297mm + pdf.AddPage() + fontSz := float64(16) + lineSz := pdf.PointToUnitConvert(fontSz) + pdf.SetFont("Times", "", fontSz) + pdf.Write(lineSz, "This document demonstrates various modes of text rendering. Search for \"Mode 3\" "+ + "to locate text that has been rendered invisibly. This selection can be copied "+ + "into the clipboard as usual and is useful for overlaying onto non-textual elements such "+ + "as images to make them searchable.\n\n") + fontSz = float64(125) + lineSz = pdf.PointToUnitConvert(fontSz) + pdf.SetFontSize(fontSz) + pdf.SetTextColor(170, 170, 190) + pdf.SetDrawColor(50, 60, 90) + + write := func(mode int) { + pdf.SetTextRenderingMode(mode) + pdf.CellFormat(210, lineSz, fmt.Sprintf("Mode %d", mode), "", 1, "", false, 0, "") + } + + for mode := 0; mode < 4; mode++ { + write(mode) + } + write(0) + + fileStr := example.Filename("Fpdf_TextRenderingMode") + err := pdf.OutputFileAndClose(fileStr) + example.Summary(err, fileStr) + // Output: + // Successfully generated pdf/Fpdf_TextRenderingMode.pdf +}