fpdftrans.go 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. package gofpdf
  2. import (
  3. "fmt"
  4. "math"
  5. )
  6. // Routines in this file are translated from the work of Moritz Wagner and
  7. // Andreas Würmser.
  8. // TransformMatrix is used for generalized transformations of text, drawings
  9. // and images.
  10. type TransformMatrix struct {
  11. A, B, C, D, E, F float64
  12. }
  13. // TransformBegin sets up a transformation context for subsequent text,
  14. // drawings and images. The typical usage is to immediately follow a call to
  15. // this method with a call to one or more of the transformation methods such as
  16. // TransformScale(), TransformSkew(), etc. This is followed by text, drawing or
  17. // image output and finally a call to TransformEnd(). All transformation
  18. // contexts must be properly ended prior to outputting the document.
  19. func (f *Fpdf) TransformBegin() {
  20. f.transformNest++
  21. f.out("q")
  22. }
  23. // TransformScaleX scales the width of the following text, drawings and images.
  24. // scaleWd is the percentage scaling factor. (x, y) is center of scaling.
  25. //
  26. // The TransformBegin() example demonstrates this method.
  27. func (f *Fpdf) TransformScaleX(scaleWd, x, y float64) {
  28. f.TransformScale(scaleWd, 100, x, y)
  29. }
  30. // TransformScaleY scales the height of the following text, drawings and
  31. // images. scaleHt is the percentage scaling factor. (x, y) is center of
  32. // scaling.
  33. //
  34. // The TransformBegin() example demonstrates this method.
  35. func (f *Fpdf) TransformScaleY(scaleHt, x, y float64) {
  36. f.TransformScale(100, scaleHt, x, y)
  37. }
  38. // TransformScaleXY uniformly scales the width and height of the following
  39. // text, drawings and images. s is the percentage scaling factor for both width
  40. // and height. (x, y) is center of scaling.
  41. //
  42. // The TransformBegin() example demonstrates this method.
  43. func (f *Fpdf) TransformScaleXY(s, x, y float64) {
  44. f.TransformScale(s, s, x, y)
  45. }
  46. // TransformScale generally scales the following text, drawings and images.
  47. // scaleWd and scaleHt are the percentage scaling factors for width and height.
  48. // (x, y) is center of scaling.
  49. //
  50. // The TransformBegin() example demonstrates this method.
  51. func (f *Fpdf) TransformScale(scaleWd, scaleHt, x, y float64) {
  52. if scaleWd == 0 || scaleHt == 0 {
  53. f.err = fmt.Errorf("scale factor cannot be zero")
  54. return
  55. }
  56. y = (f.h - y) * f.k
  57. x *= f.k
  58. scaleWd /= 100
  59. scaleHt /= 100
  60. f.Transform(TransformMatrix{scaleWd, 0, 0,
  61. scaleHt, x * (1 - scaleWd), y * (1 - scaleHt)})
  62. }
  63. // TransformMirrorHorizontal horizontally mirrors the following text, drawings
  64. // and images. x is the axis of reflection.
  65. //
  66. // The TransformBegin() example demonstrates this method.
  67. func (f *Fpdf) TransformMirrorHorizontal(x float64) {
  68. f.TransformScale(-100, 100, x, f.y)
  69. }
  70. // TransformMirrorVertical vertically mirrors the following text, drawings and
  71. // images. y is the axis of reflection.
  72. //
  73. // The TransformBegin() example demonstrates this method.
  74. func (f *Fpdf) TransformMirrorVertical(y float64) {
  75. f.TransformScale(100, -100, f.x, y)
  76. }
  77. // TransformMirrorPoint symmetrically mirrors the following text, drawings and
  78. // images on the point specified by (x, y).
  79. //
  80. // The TransformBegin() example demonstrates this method.
  81. func (f *Fpdf) TransformMirrorPoint(x, y float64) {
  82. f.TransformScale(-100, -100, x, y)
  83. }
  84. // TransformMirrorLine symmetrically mirrors the following text, drawings and
  85. // images on the line defined by angle and the point (x, y). angles is
  86. // specified in degrees and measured counter-clockwise from the 3 o'clock
  87. // position.
  88. //
  89. // The TransformBegin() example demonstrates this method.
  90. func (f *Fpdf) TransformMirrorLine(angle, x, y float64) {
  91. f.TransformScale(-100, 100, x, y)
  92. f.TransformRotate(-2*(angle-90), x, y)
  93. }
  94. // TransformTranslateX moves the following text, drawings and images
  95. // horizontally by the amount specified by tx.
  96. //
  97. // The TransformBegin() example demonstrates this method.
  98. func (f *Fpdf) TransformTranslateX(tx float64) {
  99. f.TransformTranslate(tx, 0)
  100. }
  101. // TransformTranslateY moves the following text, drawings and images vertically
  102. // by the amount specified by ty.
  103. //
  104. // The TransformBegin() example demonstrates this method.
  105. func (f *Fpdf) TransformTranslateY(ty float64) {
  106. f.TransformTranslate(0, ty)
  107. }
  108. // TransformTranslate moves the following text, drawings and images
  109. // horizontally and vertically by the amounts specified by tx and ty.
  110. //
  111. // The TransformBegin() example demonstrates this method.
  112. func (f *Fpdf) TransformTranslate(tx, ty float64) {
  113. f.Transform(TransformMatrix{1, 0, 0, 1, tx * f.k, -ty * f.k})
  114. }
  115. // TransformRotate rotates the following text, drawings and images around the
  116. // center point (x, y). angle is specified in degrees and measured
  117. // counter-clockwise from the 3 o'clock position.
  118. //
  119. // The TransformBegin() example demonstrates this method.
  120. func (f *Fpdf) TransformRotate(angle, x, y float64) {
  121. y = (f.h - y) * f.k
  122. x *= f.k
  123. angle = angle * math.Pi / 180
  124. var tm TransformMatrix
  125. tm.A = math.Cos(angle)
  126. tm.B = math.Sin(angle)
  127. tm.C = -tm.B
  128. tm.D = tm.A
  129. tm.E = x + tm.B*y - tm.A*x
  130. tm.F = y - tm.A*y - tm.B*x
  131. f.Transform(tm)
  132. }
  133. // TransformSkewX horizontally skews the following text, drawings and images
  134. // keeping the point (x, y) stationary. angleX ranges from -90 degrees (skew to
  135. // the left) to 90 degrees (skew to the right).
  136. //
  137. // The TransformBegin() example demonstrates this method.
  138. func (f *Fpdf) TransformSkewX(angleX, x, y float64) {
  139. f.TransformSkew(angleX, 0, x, y)
  140. }
  141. // TransformSkewY vertically skews the following text, drawings and images
  142. // keeping the point (x, y) stationary. angleY ranges from -90 degrees (skew to
  143. // the bottom) to 90 degrees (skew to the top).
  144. //
  145. // The TransformBegin() example demonstrates this method.
  146. func (f *Fpdf) TransformSkewY(angleY, x, y float64) {
  147. f.TransformSkew(0, angleY, x, y)
  148. }
  149. // TransformSkew generally skews the following text, drawings and images
  150. // keeping the point (x, y) stationary. angleX ranges from -90 degrees (skew to
  151. // the left) to 90 degrees (skew to the right). angleY ranges from -90 degrees
  152. // (skew to the bottom) to 90 degrees (skew to the top).
  153. //
  154. // The TransformBegin() example demonstrates this method.
  155. func (f *Fpdf) TransformSkew(angleX, angleY, x, y float64) {
  156. if angleX <= -90 || angleX >= 90 || angleY <= -90 || angleY >= 90 {
  157. f.err = fmt.Errorf("skew values must be between -90° and 90°")
  158. return
  159. }
  160. x *= f.k
  161. y = (f.h - y) * f.k
  162. var tm TransformMatrix
  163. tm.A = 1
  164. tm.B = math.Tan(angleY * math.Pi / 180)
  165. tm.C = math.Tan(angleX * math.Pi / 180)
  166. tm.D = 1
  167. tm.E = -tm.C * y
  168. tm.F = -tm.B * x
  169. f.Transform(tm)
  170. }
  171. // Transform generally transforms the following text, drawings and images
  172. // according to the specified matrix. It is typically easier to use the various
  173. // methods such as TransformRotate() and TransformMirrorVertical() instead.
  174. func (f *Fpdf) Transform(tm TransformMatrix) {
  175. if f.transformNest > 0 {
  176. f.outf("%.5f %.5f %.5f %.5f %.5f %.5f cm",
  177. tm.A, tm.B, tm.C, tm.D, tm.E, tm.F)
  178. } else if f.err == nil {
  179. f.err = fmt.Errorf("transformation context is not active")
  180. }
  181. }
  182. // TransformEnd applies a transformation that was begun with a call to TransformBegin().
  183. //
  184. // The TransformBegin() example demonstrates this method.
  185. func (f *Fpdf) TransformEnd() {
  186. if f.transformNest > 0 {
  187. f.transformNest--
  188. f.out("Q")
  189. } else {
  190. f.err = fmt.Errorf("error attempting to end transformation operation out of sequence")
  191. }
  192. }