svgwrite.go 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /*
  2. * Copyright (c) 2014 Kurt Jung (Gmail: kurt.w.jung)
  3. *
  4. * Permission to use, copy, modify, and distribute this software for any
  5. * purpose with or without fee is hereby granted, provided that the above
  6. * copyright notice and this permission notice appear in all copies.
  7. *
  8. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  9. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  10. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  11. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  12. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  13. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  14. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. */
  16. package gofpdf
  17. // SVGBasicWrite renders the paths encoded in the basic SVG image specified by
  18. // sb. The scale value is used to convert the coordinates in the path to the
  19. // unit of measure specified in New(). The current position (as set with a call
  20. // to SetXY()) is used as the origin of the image. The current line cap style
  21. // (as set with SetLineCapStyle()), line width (as set with SetLineWidth()),
  22. // and draw color (as set with SetDrawColor()) are used in drawing the image
  23. // paths.
  24. func (f *Fpdf) SVGBasicWrite(sb *SVGBasicType, scale float64) {
  25. originX, originY := f.GetXY()
  26. var x, y, newX, newY float64
  27. var cx0, cy0, cx1, cy1 float64
  28. var path []SVGBasicSegmentType
  29. var seg SVGBasicSegmentType
  30. var startX, startY float64
  31. sval := func(origin float64, arg int) float64 {
  32. return origin + scale*seg.Arg[arg]
  33. }
  34. xval := func(arg int) float64 {
  35. return sval(originX, arg)
  36. }
  37. yval := func(arg int) float64 {
  38. return sval(originY, arg)
  39. }
  40. val := func(arg int) (float64, float64) {
  41. return xval(arg), yval(arg + 1)
  42. }
  43. for j := 0; j < len(sb.Segments) && f.Ok(); j++ {
  44. path = sb.Segments[j]
  45. for k := 0; k < len(path) && f.Ok(); k++ {
  46. seg = path[k]
  47. switch seg.Cmd {
  48. case 'M':
  49. x, y = val(0)
  50. startX, startY = x, y
  51. f.SetXY(x, y)
  52. case 'L':
  53. newX, newY = val(0)
  54. f.Line(x, y, newX, newY)
  55. x, y = newX, newY
  56. case 'C':
  57. cx0, cy0 = val(0)
  58. cx1, cy1 = val(2)
  59. newX, newY = val(4)
  60. f.CurveCubic(x, y, cx0, cy0, newX, newY, cx1, cy1, "D")
  61. x, y = newX, newY
  62. case 'Q':
  63. cx0, cy0 = val(0)
  64. newX, newY = val(2)
  65. f.Curve(x, y, cx0, cy0, newX, newY, "D")
  66. x, y = newX, newY
  67. case 'H':
  68. newX = xval(0)
  69. f.Line(x, y, newX, y)
  70. x = newX
  71. case 'V':
  72. newY = yval(0)
  73. f.Line(x, y, x, newY)
  74. y = newY
  75. case 'Z':
  76. f.Line(x, y, startX, startY)
  77. x, y = startX, startY
  78. default:
  79. f.SetErrorf("Unexpected path command '%c'", seg.Cmd)
  80. }
  81. }
  82. }
  83. }