summaryrefslogtreecommitdiff
path: root/fpdf_test.go
diff options
context:
space:
mode:
authorKurt Jung <kurt.w.jung@code.google.com>2013-09-10 15:30:35 -0400
committerKurt Jung <kurt.w.jung@code.google.com>2013-09-10 15:30:35 -0400
commitf9941ced55be6fca307fec19bad2d81bd4fb7831 (patch)
treec6ac5232b8ff4d9d404abb526d964ecb616a390c /fpdf_test.go
parenta360bfb8ef7cbaacda300889a067b36ff94b944a (diff)
Transformation functionality adapted from Moritz Wagner and Andreas Würmser.
Diffstat (limited to 'fpdf_test.go')
-rw-r--r--fpdf_test.go119
1 files changed, 119 insertions, 0 deletions
diff --git a/fpdf_test.go b/fpdf_test.go
index fad67bc..ccdf1bf 100644
--- a/fpdf_test.go
+++ b/fpdf_test.go
@@ -964,3 +964,122 @@ func ExampleFpdf_tutorial16() {
// Output:
// Successfully generated pdf/tutorial16.pdf
}
+
+// Transformation test adapted from an example script by Moritz Wagner and
+// Andreas Würmser.
+func ExampleFpdf_tutorial17() {
+ const (
+ light = 200
+ dark = 0
+ )
+ var refX, refY float64
+ var refStr string
+ pdf := gofpdf.New("P", "mm", "A4", FONT_DIR)
+ pdf.AddPage()
+ color := func(val int) {
+ pdf.SetDrawColor(val, val, val)
+ pdf.SetTextColor(val, val, val)
+ }
+ reference := func(str string, x, y float64, val int) {
+ color(val)
+ pdf.Rect(x, y, 40, 10, "D")
+ pdf.Text(x, y-1, str)
+ }
+ refDraw := func(str string, x, y float64) {
+ refStr = str
+ refX = x
+ refY = y
+ reference(str, x, y, light)
+ }
+ refDupe := func() {
+ reference(refStr, refX, refY, dark)
+ }
+
+ titleStr := "Transformations"
+ titlePt := 36.0
+ titleHt := titlePt * 25.4 / 72.0
+ pdf.SetFont("Helvetica", "", titlePt)
+ titleWd := pdf.GetStringWidth(titleStr)
+ titleX := (210 - titleWd) / 2
+ pdf.Text(titleX, 10+titleHt, titleStr)
+ pdf.TransformBegin()
+ pdf.TransformMirrorVertical(10 + titleHt + 0.5)
+ pdf.ClipText(titleX, 10+titleHt, titleStr, false)
+ // Remember that the transform will mirror the gradient box too
+ pdf.LinearGradient(titleX, 10, titleWd, titleHt+4, 120, 120, 120, 255, 255, 255, 0, 0, 0, 0.6)
+ pdf.ClipEnd()
+ pdf.TransformEnd()
+
+ pdf.SetFont("Helvetica", "", 12)
+
+ // Scale by 150% centered by lower left corner of the rectangle
+ refDraw("Scale", 50, 60)
+ pdf.TransformBegin()
+ pdf.TransformScaleXY(150, 50, 70)
+ refDupe()
+ pdf.TransformEnd()
+
+ // Translate 7 to the right, 5 to the bottom
+ refDraw("Translate", 125, 60)
+ pdf.TransformBegin()
+ pdf.TransformTranslate(7, 5)
+ refDupe()
+ pdf.TransformEnd()
+
+ // Rotate 20 degrees counter-clockwise centered by the lower left corner of
+ // the rectangle
+ refDraw("Rotate", 50, 110)
+ pdf.TransformBegin()
+ pdf.TransformRotate(20, 50, 120)
+ refDupe()
+ pdf.TransformEnd()
+
+ // Skew 30 degrees along the x-axis centered by the lower left corner of the
+ // rectangle
+ refDraw("Skew", 125, 110)
+ pdf.TransformBegin()
+ pdf.TransformSkewX(30, 125, 110)
+ refDupe()
+ pdf.TransformEnd()
+
+ // Mirror horizontally with axis of reflection at left side of the rectangle
+ refDraw("Mirror horizontal", 50, 160)
+ pdf.TransformBegin()
+ pdf.TransformMirrorHorizontal(50)
+ refDupe()
+ pdf.TransformEnd()
+
+ // Mirror vertically with axis of reflection at bottom side of the rectangle
+ refDraw("Mirror vertical", 125, 160)
+ pdf.TransformBegin()
+ pdf.TransformMirrorVertical(170)
+ refDupe()
+ pdf.TransformEnd()
+
+ // Reflect against a point at the lower left point of rectangle
+ refDraw("Mirror point", 50, 210)
+ pdf.TransformBegin()
+ pdf.TransformMirrorPoint(50, 220)
+ refDupe()
+ pdf.TransformEnd()
+
+ // Mirror against a straight line described by a point and an angle
+ angle := -20.0
+ px := 120.0
+ py := 220.0
+ refDraw("Mirror line", 125, 210)
+ pdf.TransformBegin()
+ pdf.TransformRotate(angle, px, py)
+ pdf.Line(px-1, py-1, px+1, py+1)
+ pdf.Line(px-1, py+1, px+1, py-1)
+ pdf.Line(px-5, py, px+60, py)
+ pdf.TransformEnd()
+ pdf.TransformBegin()
+ pdf.TransformMirrorLine(angle, px, py)
+ refDupe()
+ pdf.TransformEnd()
+
+ pdf.OutputAndClose(docWriter(pdf, 17))
+ // Output:
+ // Successfully generated pdf/tutorial17.pdf
+}