diff options
author | Kurt <kurt.w.jung@gmail.com> | 2018-12-27 08:34:59 -0500 |
---|---|---|
committer | Kurt <kurt.w.jung@gmail.com> | 2018-12-27 08:34:59 -0500 |
commit | 6bd017132c2142e181eb6f945c06a90f45027d7d (patch) | |
tree | 3729284049053f335191879b7e6c446252633bb0 | |
parent | a3cf901b6d1246d3e00875a3e88c69bee068156d (diff) |
Support subscripted and superscripted text with new SubWrite method
-rw-r--r-- | fpdf_test.go | 50 | ||||
-rw-r--r-- | subwrite.go | 33 |
2 files changed, 83 insertions, 0 deletions
diff --git a/fpdf_test.go b/fpdf_test.go index 2fbca9c..86fae86 100644 --- a/fpdf_test.go +++ b/fpdf_test.go @@ -2325,3 +2325,53 @@ func ExampleFpdf_SetPageBox() { // Output: // Successfully generated pdf/Fpdf_PageBox.pdf } + +// ExampleFpdf_SubWrite demonstrates subscripted and superscripted text +// Adapted from http://www.fpdf.org/en/script/script61.php +func ExampleFpdf_SubWrite() { + + const ( + fontSize = 12 + halfX = 105 + ) + + pdf := gofpdf.New("P", "mm", "A4", "") // 210mm x 297mm + pdf.AddPage() + pdf.SetFont("Arial", "", fontSize) + _, lineHt := pdf.GetFontSize() + + pdf.Write(lineHt, "Hello World!") + pdf.SetX(halfX) + pdf.Write(lineHt, "This is standard text.\n") + pdf.Ln(lineHt * 2) + + pdf.SubWrite(10, "H", 33, 0, 0, "") + pdf.Write(10, "ello World!") + pdf.SetX(halfX) + pdf.Write(10, "This is text with a capital first letter.\n") + pdf.Ln(lineHt * 2) + + pdf.SubWrite(lineHt, "Y", 6, 0, 0, "") + pdf.Write(lineHt, "ou can also begin the sentence with a small letter. And word wrap also works if the line is too long, like this one is.") + pdf.SetX(halfX) + pdf.Write(lineHt, "This is text with a small first letter.\n") + pdf.Ln(lineHt * 2) + + pdf.Write(lineHt, "The world has a lot of km") + pdf.SubWrite(lineHt, "2", 6, 4, 0, "") + pdf.SetX(halfX) + pdf.Write(lineHt, "This is text with a superscripted letter.\n") + pdf.Ln(lineHt * 2) + + pdf.Write(lineHt, "The world has a lot of H") + pdf.SubWrite(lineHt, "2", 6, -3, 0, "") + pdf.Write(lineHt, "O") + pdf.SetX(halfX) + pdf.Write(lineHt, "This is text with a subscripted letter.\n") + + fileStr := example.Filename("Fpdf_SubWrite") + err := pdf.OutputFileAndClose(fileStr) + example.Summary(err, fileStr) + // Output: + // Successfully generated pdf/Fpdf_SubWrite.pdf +} diff --git a/subwrite.go b/subwrite.go new file mode 100644 index 0000000..dbeee5b --- /dev/null +++ b/subwrite.go @@ -0,0 +1,33 @@ +package gofpdf + +// Adapted from http://www.fpdf.org/en/script/script61.php by Wirus and released with the FPDF license. + +// SubWrite prints text from the current position in the same way as Write(). +// ht is the line height in the unit of measure specified in New(). str +// specifies the text to write. subFontSize is the size of the font in points. +// subOffset is the vertical offset of the text in points; a positive value +// indicates a superscript, a negative value indicates a subscript. link is the +// identifier returned by AddLink() or 0 for no internal link. linkStr is a +// target URL or empty for no external link. A non--zero value for link takes +// precedence over linkStr. +func (f *Fpdf) SubWrite(ht float64, str string, subFontSize, subOffset float64, link int, linkStr string) { + if f.err != nil { + return + } + // resize font + subFontSizeOld := f.fontSizePt + f.SetFontSize(subFontSize) + // reposition y + subOffset = (((subFontSize - subFontSizeOld) / f.k) * 0.3) + (subOffset / f.k) + subX := f.x + subY := f.y + f.SetXY(subX, subY-subOffset) + //Output text + f.write(ht, str, link, linkStr) + // restore y position + subX = f.x + subY = f.y + f.SetXY(subX, subY+subOffset) + // restore font size + f.SetFontSize(subFontSizeOld) +} |