text.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. # Created: 25.03.2011
  2. # Copyright (c) 2011-2018, Manfred Moitzi
  3. # License: MIT License
  4. from typing import TYPE_CHECKING, Tuple, Union
  5. from ezdxf.lldxf.const import DXFValueError
  6. from ezdxf.lldxf import const
  7. from .graphics import GraphicEntity, ExtendedTags, make_attribs, DXFAttr, XType
  8. if TYPE_CHECKING:
  9. from ezdxf.eztypes import Vertex
  10. _TEXT_TPL = """0
  11. TEXT
  12. 5
  13. 0
  14. 8
  15. 0
  16. 10
  17. 0.0
  18. 20
  19. 0.0
  20. 30
  21. 0.0
  22. 40
  23. 1.0
  24. 1
  25. TEXTCONTENT
  26. 50
  27. 0.0
  28. 51
  29. 0.0
  30. 7
  31. STANDARD
  32. 41
  33. 1.0
  34. 71
  35. 0
  36. 72
  37. 0
  38. 73
  39. 0
  40. 11
  41. 0.0
  42. 21
  43. 0.0
  44. 31
  45. 0.0
  46. """
  47. class Text(GraphicEntity):
  48. __slots__ = ()
  49. TEMPLATE = ExtendedTags.from_text(_TEXT_TPL)
  50. DXFATTRIBS = make_attribs({
  51. 'insert': DXFAttr(10, xtype=XType.any_point),
  52. 'height': DXFAttr(40),
  53. 'text': DXFAttr(1),
  54. 'rotation': DXFAttr(50, default=0.0), # in degrees (circle = 360deg)
  55. 'oblique': DXFAttr(51, default=0.0), # in degrees, vertical = 0deg
  56. 'style': DXFAttr(7, default='STANDARD'), # text style
  57. 'width': DXFAttr(41, default=1.0), # width FACTOR!
  58. 'text_generation_flag': DXFAttr(71, default=0), # 2 = backward (mirr-x), 4 = upside down (mirr-y)
  59. 'halign': DXFAttr(72, default=0), # horizontal justification
  60. 'valign': DXFAttr(73, default=0), # vertical justification
  61. 'align_point': DXFAttr(11, xtype=XType.any_point),
  62. })
  63. # horizontal align values
  64. LEFT = 0
  65. CENTER = 1
  66. RIGHT = 2
  67. # vertical align values
  68. BASELINE = 0
  69. BOTTOM = 1
  70. MIDDLE = 2
  71. TOP = 3
  72. # text generation flags
  73. MIRROR_X = 2
  74. MIRROR_Y = 4
  75. BACKWARD = MIRROR_X
  76. UPSIDE_DOWN = MIRROR_Y
  77. def set_pos(self, p1: 'Vertex', p2: 'Vertex' = None, align: str = None) -> 'Text':
  78. if align is None:
  79. align = self.get_align()
  80. align = align.upper()
  81. self.set_align(align)
  82. self.set_dxf_attrib('insert', p1)
  83. if align in ('ALIGNED', 'FIT'):
  84. if p2 is None:
  85. raise DXFValueError("Alignment '{}' requires a second alignment point.".format(align))
  86. else:
  87. p2 = p1
  88. self.set_dxf_attrib('align_point', p2)
  89. return self
  90. def get_pos(self) -> Tuple[str, 'Vertex', Union['Vertex', None]]:
  91. p1 = self.dxf.insert
  92. p2 = self.get_dxf_attrib('align_point', (0., 0., 0.))
  93. align = self.get_align()
  94. if align == 'LEFT':
  95. return align, p1, None
  96. if align in ('FIT', 'ALIGN'):
  97. return align, p1, p2
  98. return align, p2, None
  99. def set_align(self, align: str='LEFT') -> 'Text':
  100. align = align.upper()
  101. halign, valign = const.TEXT_ALIGN_FLAGS[align]
  102. self.set_dxf_attrib('halign', halign)
  103. self.set_dxf_attrib('valign', valign)
  104. return self
  105. def get_align(self) -> str:
  106. halign = self.get_dxf_attrib('halign', 0)
  107. valign = self.get_dxf_attrib('valign', 0)
  108. if halign > 2:
  109. valign = 0
  110. return const.TEXT_ALIGNMENT_BY_FLAGS.get((halign, valign), 'LEFT')