summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKurt Jung <kurt.w.jung@code.google.com>2014-02-13 16:10:53 -0500
committerKurt Jung <kurt.w.jung@code.google.com>2014-02-13 16:10:53 -0500
commit54926dff7aced06e420e30acacf942650e7a6e59 (patch)
treef3a88ba82cda42bfc6e962d05d78ff6ee69d79aa
parent5c3d717c55e3a9cadd924d6b20eab99f8f9615ba (diff)
Patch provided by Stefan Schroeder to control vertical alignment of text within cell.
-rw-r--r--doc.go3
-rw-r--r--fpdf.go31
-rw-r--r--fpdf_test.go37
3 files changed, 61 insertions, 10 deletions
diff --git a/doc.go b/doc.go
index 1dad3f2..e93a4b5 100644
--- a/doc.go
+++ b/doc.go
@@ -61,7 +61,8 @@ adapted from FPDF scripts by Andreas Würmser. Support for outline bookmarks is
adapted from Olivier Plathey by Manuel Cornes. Support for transformations is
adapted from the FPDF transformation script by Moritz Wagner and Andreas
Würmser. Lawrence Kesteloot provided code to allow an image's extent to be
-determined prior to placement. Bruno Michel has provided valuable assistance
+determined prior to placement. Support for vertical alignment within a cell was
+provided by Stefan Schroeder. Bruno Michel has provided valuable assistance
with the code.
The FPDF website is http://www.fpdf.org/.
diff --git a/fpdf.go b/fpdf.go
index 7f197ed..7b381a5 100644
--- a/fpdf.go
+++ b/fpdf.go
@@ -1489,8 +1489,11 @@ func (f *Fpdf) SetAcceptPageBreakFunc(fnc func() bool) {
// values are 0 (to the right), 1 (to the beginning of the next line), and 2
// (below). Putting 1 is equivalent to putting 0 and calling Ln() just after.
//
-// alignStr allows the text to be aligned to the right ("R"), center ("C") or
-// left ("L" or "").
+// alignStr specifies how the text is to be positionined within the cell.
+// Horizontal alignment is controlled by including "L", "C" or "R" (left,
+// center, right) in alignStr. Vertical alignment is controlled by including
+// "T", "M" or "B" (top, middle, bottom) in alignStr. The default alignment is
+// left middle.
//
// fill is true to paint the cell background or false to leave it transparent.
//
@@ -1498,6 +1501,8 @@ func (f *Fpdf) SetAcceptPageBreakFunc(fnc func() bool) {
//
// linkStr is a target URL or empty for no external link. A non--zero value for
// link takes precedence over linkStr.
+//
+// See tutorial 21 for a demonstration of text alignment within a cell.
func (f *Fpdf) CellFormat(w, h float64, txtStr string, borderStr string, ln int, alignStr string, fill bool, link int, linkStr string) {
// dbg("CellFormat. h = %.2f, borderStr = %s", h, borderStr)
if f.err != nil {
@@ -1567,15 +1572,23 @@ func (f *Fpdf) CellFormat(w, h float64, txtStr string, borderStr string, ln int,
}
}
if len(txtStr) > 0 {
- var dx float64
- if alignStr == "R" {
+ var dx, dy float64
+ // Horizontal alignment
+ if strings.Index(alignStr, "R") != -1 {
dx = w - f.cMargin - f.GetStringWidth(txtStr)
- } else if alignStr == "C" {
+ } else if strings.Index(alignStr, "C") != -1 {
dx = (w - f.GetStringWidth(txtStr)) / 2
- // dbg("center cell, dx %.2f\n", dx)
} else {
dx = f.cMargin
}
+ // Vertical alignment
+ if strings.Index(alignStr, "T") != -1 {
+ dy = (f.fontSize - h) / 2.0
+ } else if strings.Index(alignStr, "B") != -1 {
+ dy = (h - f.fontSize) / 2.0
+ } else {
+ dy = 0
+ }
if f.colorFlag {
s.printf("q %s ", f.color.text.str)
}
@@ -1585,16 +1598,16 @@ func (f *Fpdf) CellFormat(w, h float64, txtStr string, borderStr string, ln int,
// if strings.Contains(txt2, "end of excerpt") {
// dbg("f.h %.2f, f.y %.2f, h %.2f, f.fontSize %.2f, k %.2f", f.h, f.y, h, f.fontSize, k)
// }
- s.printf("BT %.2f %.2f Td (%s) Tj ET", (f.x+dx)*k, (f.h-(f.y+.5*h+.3*f.fontSize))*k, txt2)
+ s.printf("BT %.2f %.2f Td (%s) Tj ET", (f.x+dx)*k, (f.h-(f.y+dy+.5*h+.3*f.fontSize))*k, txt2)
//BT %.2F %.2F Td (%s) Tj ET',($this->x+$dx)*$k,($this->h-($this->y+.5*$h+.3*$this->FontSize))*$k,$txt2);
if f.underline {
- s.printf(" %s", f.dounderline(f.x+dx, f.y+.5*h+.3*f.fontSize, txtStr))
+ s.printf(" %s", f.dounderline(f.x+dx, f.y+dy+.5*h+.3*f.fontSize, txtStr))
}
if f.colorFlag {
s.printf(" Q")
}
if link > 0 || len(linkStr) > 0 {
- f.newLink(f.x+dx, f.y+.5*h-.5*f.fontSize, f.GetStringWidth(txtStr), f.fontSize, link, linkStr)
+ f.newLink(f.x+dx, f.y+dy+.5*h-.5*f.fontSize, f.GetStringWidth(txtStr), f.fontSize, link, linkStr)
}
}
str := s.String()
diff --git a/fpdf_test.go b/fpdf_test.go
index e771ac5..aaf5807 100644
--- a/fpdf_test.go
+++ b/fpdf_test.go
@@ -1086,3 +1086,40 @@ func ExampleFpdf_tutorial20() {
// Output:
// Successfully generated pdf/tutorial20.pdf
}
+
+// This example demonstrates Stefan Schroeder's code to control vertical
+// alignment.
+func ExampleFpdf_tutorial21() {
+ type recType struct {
+ align, txt string
+ }
+ recList := []recType{
+ recType{"TL", "top left"},
+ recType{"TC", "top center"},
+ recType{"TR", "top right"},
+ recType{"LM", "middle left"},
+ recType{"CM", "middle center"},
+ recType{"RM", "middle right"},
+ recType{"BL", "bottom left"},
+ recType{"BC", "bottom center"},
+ recType{"BR", "bottom right"},
+ }
+ pdf := gofpdf.New("P", "mm", "A4", cnFontDir) // A4 210.0 x 297.0
+ pdf.SetFont("Helvetica", "", 16)
+ linkStr := ""
+ for pageJ := 0; pageJ < 2; pageJ++ {
+ pdf.AddPage()
+ pdf.SetMargins(10, 10, 10)
+ pdf.SetAutoPageBreak(false, 0)
+ borderStr := "1"
+ for _, rec := range recList {
+ pdf.SetXY(20, 20)
+ pdf.CellFormat(170, 257, rec.txt, borderStr, 0, rec.align, false, 0, linkStr)
+ borderStr = ""
+ }
+ linkStr = "https://code.google.com/p/gofpdf/"
+ }
+ pdf.OutputAndClose(docWriter(pdf, 21))
+ // Output:
+ // Successfully generated pdf/tutorial21.pdf
+}