lines.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. package pptx
  2. import (
  3. "imuslab.com/arozos/mod/office/internal/xmltree"
  4. "imuslab.com/arozos/mod/office/model"
  5. )
  6. /*
  7. lines.go - how a line looks at its ends and along its length.
  8. The Slides editor offers what Google Slides does: six dash styles and ten
  9. line ends (none, a line arrow, a filled arrow, circle, square and
  10. diamond, and open versions of the last four), per end. PresentationML
  11. has a richer dash vocabulary but fewer ends - no square and nothing
  12. open - so a square end is written as the diamond it is a turn of, and an
  13. open end as its filled shape. The editor's own copy of the document
  14. (embedded in the .pptx) keeps the exact choice.
  15. A document written before these existed says only dash / arrowEnd /
  16. arrowStart; those still mean a plain dash and a filled arrow.
  17. */
  18. // prstDashFor is the <a:prstDash> preset for an object's dash, or "" for a
  19. // solid line
  20. func prstDashFor(p model.Props) string {
  21. switch p.DashStyle {
  22. case "dot":
  23. return "sysDot"
  24. case "dash":
  25. return "dash"
  26. case "dashDot":
  27. return "dashDot"
  28. case "longDash":
  29. return "lgDash"
  30. case "longDashDot":
  31. return "lgDashDot"
  32. case "solid":
  33. return ""
  34. }
  35. if p.Dash {
  36. return "dash"
  37. }
  38. return ""
  39. }
  40. // dashStyleForPrst reads a <a:prstDash> preset as the editor's dash style
  41. // ("" for solid)
  42. func dashStyleForPrst(v string) string {
  43. switch v {
  44. case "sysDot", "dot":
  45. return "dot"
  46. case "sysDash", "dash":
  47. return "dash"
  48. case "dashDot", "sysDashDot":
  49. return "dashDot"
  50. case "lgDash":
  51. return "longDash"
  52. case "lgDashDot", "lgDashDotDot", "sysDashDotDot":
  53. return "longDashDot"
  54. }
  55. return ""
  56. }
  57. // lineHeadOf is the end an object has at its start or its end ("" for
  58. // none), falling back to the old arrowStart / arrowEnd flags
  59. func lineHeadOf(p model.Props, end bool) string {
  60. head, legacy := p.StartHead, p.ArrowStart
  61. if end {
  62. head, legacy = p.EndHead, p.ArrowEnd
  63. }
  64. if head == "none" {
  65. return ""
  66. }
  67. if head != "" {
  68. return head
  69. }
  70. if legacy {
  71. return "triangle"
  72. }
  73. return ""
  74. }
  75. // lineEndType is the DrawingML <a:headEnd>/<a:tailEnd> type for an end
  76. func lineEndType(head string) string {
  77. switch head {
  78. case "arrow":
  79. return "arrow"
  80. case "triangle", "openTriangle":
  81. return "triangle"
  82. case "circle", "openCircle":
  83. return "oval"
  84. case "square", "openSquare", "diamond", "openDiamond":
  85. return "diamond"
  86. }
  87. return ""
  88. }
  89. // headForLineEnd reads a DrawingML end type as the editor's line end
  90. func headForLineEnd(t string) string {
  91. switch t {
  92. case "arrow":
  93. return "arrow"
  94. case "triangle", "stealth":
  95. return "triangle"
  96. case "oval":
  97. return "circle"
  98. case "diamond":
  99. return "diamond"
  100. }
  101. return ""
  102. }
  103. // lnDashAndEnds renders the dash and the two ends inside an <a:ln>, in the
  104. // order the schema wants them
  105. func lnDashAndEnds(p model.Props, withEnds bool) string {
  106. out := ""
  107. if d := prstDashFor(p); d != "" {
  108. out += `<a:prstDash val="` + d + `"/>`
  109. }
  110. if !withEnds {
  111. return out
  112. }
  113. if t := lineEndType(lineHeadOf(p, false)); t != "" {
  114. out += `<a:headEnd type="` + t + `"/>`
  115. }
  116. if t := lineEndType(lineHeadOf(p, true)); t != "" {
  117. out += `<a:tailEnd type="` + t + `"/>`
  118. }
  119. return out
  120. }
  121. // readLineEnds reads the dash and the two ends of an <a:ln> into props
  122. func readLineEnds(ln *xmltree.Node, props *model.Props) {
  123. if ln == nil {
  124. return
  125. }
  126. if d := ln.First("prstDash"); d != nil {
  127. props.DashStyle = dashStyleForPrst(d.Attr("val"))
  128. props.Dash = props.DashStyle != ""
  129. }
  130. if he := ln.First("headEnd"); he != nil {
  131. if h := headForLineEnd(he.Attr("type")); h != "" {
  132. props.StartHead, props.ArrowStart = h, true
  133. }
  134. }
  135. if te := ln.First("tailEnd"); te != nil {
  136. if h := headForLineEnd(te.Attr("type")); h != "" {
  137. props.EndHead, props.ArrowEnd = h, true
  138. }
  139. }
  140. }