line.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. from __future__ import absolute_import
  2. # Copyright (c) 2010-2019 openpyxl
  3. from openpyxl.descriptors.serialisable import Serialisable
  4. from openpyxl.descriptors import (
  5. Typed,
  6. Float,
  7. Integer,
  8. Bool,
  9. MinMax,
  10. Set,
  11. NoneSet,
  12. String,
  13. Alias,
  14. Sequence
  15. )
  16. from openpyxl.descriptors.excel import Coordinate, Percentage
  17. from openpyxl.descriptors.nested import (
  18. NestedInteger,
  19. NestedSet,
  20. NestedNoneSet,
  21. EmptyTag,
  22. )
  23. from openpyxl.compat import safe_string
  24. from openpyxl.xml.constants import DRAWING_NS
  25. from openpyxl.xml.functions import Element
  26. from .colors import ColorChoiceDescriptor
  27. from .fill import GradientFillProperties, PatternFillProperties
  28. from openpyxl.descriptors.excel import ExtensionList as OfficeArtExtensionList
  29. """
  30. Line elements from drawing main schema
  31. """
  32. class LineEndProperties(Serialisable):
  33. tagname = "end"
  34. namespace = DRAWING_NS
  35. type = NoneSet(values=(['none', 'triangle', 'stealth', 'diamond', 'oval', 'arrow']))
  36. w = NoneSet(values=(['sm', 'med', 'lg']))
  37. len = NoneSet(values=(['sm', 'med', 'lg']))
  38. def __init__(self,
  39. type=None,
  40. w=None,
  41. len=None,
  42. ):
  43. self.type = type
  44. self.w = w
  45. self.len = len
  46. class DashStop(Serialisable):
  47. tagname = "ds"
  48. namespace = DRAWING_NS
  49. d = Integer()
  50. length = Alias('d')
  51. sp = Integer()
  52. space = Alias('sp')
  53. def __init__(self,
  54. d=0,
  55. sp=0,
  56. ):
  57. self.d = d
  58. self.sp = sp
  59. class DashStopList(Serialisable):
  60. ds = Sequence(expected_type=DashStop, allow_none=True)
  61. def __init__(self,
  62. ds=None,
  63. ):
  64. self.ds = ds
  65. class LineProperties(Serialisable):
  66. tagname = "ln"
  67. namespace = DRAWING_NS
  68. w = MinMax(min=0, max=20116800, allow_none=True) # EMU
  69. width = Alias('w')
  70. cap = NoneSet(values=(['rnd', 'sq', 'flat']))
  71. cmpd = NoneSet(values=(['sng', 'dbl', 'thickThin', 'thinThick', 'tri']))
  72. algn = NoneSet(values=(['ctr', 'in']))
  73. noFill = EmptyTag()
  74. solidFill = ColorChoiceDescriptor()
  75. gradFill = Typed(expected_type=GradientFillProperties, allow_none=True)
  76. pattFill = Typed(expected_type=PatternFillProperties, allow_none=True)
  77. prstDash = NestedNoneSet(values=(['solid', 'dot', 'dash', 'lgDash', 'dashDot',
  78. 'lgDashDot', 'lgDashDotDot', 'sysDash', 'sysDot', 'sysDashDot',
  79. 'sysDashDotDot']), namespace=namespace)
  80. dashStyle = Alias('prstDash')
  81. custDash = Typed(expected_type=DashStop, allow_none=True)
  82. round = EmptyTag()
  83. bevel = EmptyTag()
  84. miter = NestedInteger(allow_none=True, attribute="lim")
  85. headEnd = Typed(expected_type=LineEndProperties, allow_none=True)
  86. tailEnd = Typed(expected_type=LineEndProperties, allow_none=True)
  87. extLst = Typed(expected_type=OfficeArtExtensionList, allow_none=True)
  88. __elements__ = ('noFill', 'solidFill', 'gradFill', 'pattFill',
  89. 'prstDash', 'custDash', 'round', 'bevel', 'miter', 'headEnd', 'tailEnd')
  90. def __init__(self,
  91. w=None,
  92. cap=None,
  93. cmpd=None,
  94. algn=None,
  95. noFill=None,
  96. solidFill=None,
  97. gradFill=None,
  98. pattFill=None,
  99. prstDash=None,
  100. custDash=None,
  101. round=None,
  102. bevel=None,
  103. miter=None,
  104. headEnd=None,
  105. tailEnd=None,
  106. extLst=None,
  107. ):
  108. self.w = w
  109. self.cap = cap
  110. self.cmpd = cmpd
  111. self.algn = algn
  112. self.noFill = noFill
  113. self.solidFill = solidFill
  114. self.gradFill = gradFill
  115. self.pattFill = pattFill
  116. if prstDash is None:
  117. prstDash = "solid"
  118. self.prstDash = prstDash
  119. self.custDash = custDash
  120. self.round = round
  121. self.bevel = bevel
  122. self.miter = miter
  123. self.headEnd = headEnd
  124. self.tailEnd = tailEnd