summaryrefslogtreecommitdiff
path: root/fpdf.go
diff options
context:
space:
mode:
authorJelmer Snoeck <jelmer.snoeck@gmail.com>2015-09-25 13:46:33 +0100
committerJelmer Snoeck <jelmer.snoeck@gmail.com>2015-09-25 13:52:55 +0100
commit04e0bd700c8e33c22e277c2c4898eaf9962f461f (patch)
tree32b65db95f3537d2926f11004c4966e21c2aba00 /fpdf.go
parent02db05c2cda70eb7ae66c31af83b355917b9694b (diff)
Fpdf: add WriteAligned.
WriteAligned allows a user to write a single string and align it. Previously, Write() would only write with Left alignment. Sometimes, you want to align a specific text string to the center or the right. HTMLBasic: add <center> tag. This allows a user to center text within the HTML block. This way they do not have to split up their input. A new line will be created automatically before and after the centered text.
Diffstat (limited to 'fpdf.go')
-rw-r--r--fpdf.go41
1 files changed, 41 insertions, 0 deletions
diff --git a/fpdf.go b/fpdf.go
index da3d92a..f1c6b58 100644
--- a/fpdf.go
+++ b/fpdf.go
@@ -2160,6 +2160,47 @@ func (f *Fpdf) WriteLinkID(h float64, displayStr string, linkID int) {
f.write(h, displayStr, linkID, "")
}
+// WriteAligned is an implementation of Write that makes it possible to align
+// text.
+//
+// width indicates the width of the box the text will be drawn in. This is in
+// the unit of measure specified in New(). If it is set to 0, the bounding box
+//of the page will be taken (pageWidth - leftMargin - rightMargin).
+//
+// lineHeight indicates the line height in the unit of measure specified in
+// New().
+//
+// alignStr sees to horizontal alignment of the given textStr. The options are
+// "L", "C" and "R" (Left, Center, Right). The default is "L".
+func (f *Fpdf) WriteAligned(width, lineHeight float64, textStr, alignStr string) {
+ lMargin, _, rMargin, _ := f.GetMargins()
+
+ if width == 0 {
+ pageWidth, _ := f.GetPageSize()
+ width = pageWidth - (lMargin + rMargin)
+ }
+
+ lines := f.SplitLines([]byte(textStr), width)
+
+ for _, lineBt := range lines {
+ lineStr := string(lineBt[:])
+ lineWidth := f.GetStringWidth(lineStr)
+
+ switch alignStr {
+ case "C":
+ f.SetLeftMargin(lMargin + ((width - lineWidth) / 2))
+ f.Write(lineHeight, lineStr)
+ f.SetLeftMargin(lMargin)
+ case "R":
+ f.SetLeftMargin(lMargin + (width - lineWidth) - rMargin)
+ f.Write(lineHeight, lineStr)
+ f.SetLeftMargin(lMargin)
+ default:
+ f.Write(lineHeight, lineStr)
+ }
+ }
+}
+
// Ln performs a line break. The current abscissa goes back to the left margin
// and the ordinate increases by the amount passed in parameter. A negative
// value of h indicates the height of the last printed cell.