layer.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. // Routines in this file are translated from
  18. // http://www.fpdf.org/en/script/script97.php
  19. type layerType struct {
  20. name string
  21. visible bool
  22. objNum int // object number
  23. }
  24. type layerRecType struct {
  25. list []layerType
  26. currentLayer int
  27. openLayerPane bool
  28. }
  29. func (f *Fpdf) layerInit() {
  30. f.layer.list = make([]layerType, 0)
  31. f.layer.currentLayer = -1
  32. f.layer.openLayerPane = false
  33. }
  34. // AddLayer defines a layer that can be shown or hidden when the document is
  35. // displayed. name specifies the layer name that the document reader will
  36. // display in the layer list. visible specifies whether the layer will be
  37. // initially visible. The return value is an integer ID that is used in a call
  38. // to BeginLayer().
  39. func (f *Fpdf) AddLayer(name string, visible bool) (layerID int) {
  40. layerID = len(f.layer.list)
  41. f.layer.list = append(f.layer.list, layerType{name: name, visible: visible})
  42. return
  43. }
  44. // BeginLayer is called to begin adding content to the specified layer. All
  45. // content added to the page between a call to BeginLayer and a call to
  46. // EndLayer is added to the layer specified by id. See AddLayer for more
  47. // details.
  48. func (f *Fpdf) BeginLayer(id int) {
  49. f.EndLayer()
  50. if id >= 0 && id < len(f.layer.list) {
  51. f.outf("/OC /OC%d BDC", id)
  52. f.layer.currentLayer = id
  53. }
  54. }
  55. // EndLayer is called to stop adding content to the currently active layer. See
  56. // BeginLayer for more details.
  57. func (f *Fpdf) EndLayer() {
  58. if f.layer.currentLayer >= 0 {
  59. f.out("EMC")
  60. f.layer.currentLayer = -1
  61. }
  62. }
  63. // OpenLayerPane advises the document reader to open the layer pane when the
  64. // document is initially displayed.
  65. func (f *Fpdf) OpenLayerPane() {
  66. f.layer.openLayerPane = true
  67. }
  68. func (f *Fpdf) layerEndDoc() {
  69. if len(f.layer.list) > 0 {
  70. if f.pdfVersion < "1.5" {
  71. f.pdfVersion = "1.5"
  72. }
  73. }
  74. }
  75. func (f *Fpdf) layerPutLayers() {
  76. for j, l := range f.layer.list {
  77. f.newobj()
  78. f.layer.list[j].objNum = f.n
  79. f.outf("<</Type /OCG /Name %s>>", f.textstring(utf8toutf16(l.name)))
  80. f.out("endobj")
  81. }
  82. }
  83. func (f *Fpdf) layerPutResourceDict() {
  84. if len(f.layer.list) > 0 {
  85. f.out("/Properties <<")
  86. for j, layer := range f.layer.list {
  87. f.outf("/OC%d %d 0 R", j, layer.objNum)
  88. }
  89. f.out(">>")
  90. }
  91. }
  92. func (f *Fpdf) layerPutCatalog() {
  93. if len(f.layer.list) > 0 {
  94. onStr := ""
  95. offStr := ""
  96. for _, layer := range f.layer.list {
  97. onStr += sprintf("%d 0 R ", layer.objNum)
  98. if !layer.visible {
  99. offStr += sprintf("%d 0 R ", layer.objNum)
  100. }
  101. }
  102. f.outf("/OCProperties <</OCGs [%s] /D <</OFF [%s] /Order [%s]>>>>", onStr, offStr, onStr)
  103. if f.layer.openLayerPane {
  104. f.out("/PageMode /UseOC")
  105. }
  106. }
  107. }