You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

199 lines
5.5 KiB

  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version 2
  9. * of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, you may find one here:
  18. * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  19. * or you may search the http://www.gnu.org website for the version 2 license,
  20. * or you may write to the Free Software Foundation, Inc.,
  21. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  22. */
  23. #pragma once
  24. #include <optional>
  25. #include <geometry/circle.h>
  26. #include <geometry/seg.h>
  27. #include <math/box2.h>
  28. #include <sch_pin.h>
  29. class SCHEMATIC_SETTINGS;
  30. /**
  31. * A pin layout helper is a class that manages the layout of the parts of
  32. * a pin on a schematic symbol:
  33. *
  34. * including, extents of:
  35. * - the pin itself
  36. * - the pin number, number, type
  37. * - decorations
  38. * - alternate mode icons
  39. *
  40. * This is useful, because this information is used in multiple places,
  41. * and regenerating it in multiple places is error-prone. It can also
  42. * be cached if it's encapsulated in one place.
  43. */
  44. class PIN_LAYOUT_CACHE
  45. {
  46. public:
  47. PIN_LAYOUT_CACHE( const SCH_PIN& aPin );
  48. enum DIRTY_FLAGS
  49. {
  50. NAME = 1,
  51. NUMBER = 2,
  52. ELEC_TYPE = 4,
  53. ALL = NAME | NUMBER | ELEC_TYPE,
  54. };
  55. /**
  56. * Recompute all the layout information.
  57. */
  58. void MarkDirty( int aFlags );
  59. void SetRenderParameters( int aNameThickness, int aNumberThickness, bool aShowElectricalType,
  60. bool aShowAltIcons );
  61. /**
  62. * Get the bounding box of the pin itself.
  63. */
  64. BOX2I GetPinBoundingBox( bool aIncludeLabelsOnInvisiblePins, bool aIncludeNameAndNumber,
  65. bool aIncludeElectricalType );
  66. /**
  67. * Get the bounding box of the pin name, if there is one.
  68. */
  69. OPT_BOX2I GetPinNameBBox();
  70. /**
  71. * Get the bounding box of the pin number, if there is one.
  72. */
  73. OPT_BOX2I GetPinNumberBBox();
  74. /**
  75. * Get the box of the alt mode icon, if there is one.
  76. */
  77. OPT_BOX2I GetAltIconBBox();
  78. /**
  79. * Gets the dangling indicator geometry for this pin, if the
  80. * pin were to be dangling.
  81. */
  82. CIRCLE GetDanglingIndicator() const;
  83. struct TEXT_INFO
  84. {
  85. wxString m_Text;
  86. int m_TextSize;
  87. int m_Thickness;
  88. VECTOR2I m_TextPosition;
  89. GR_TEXT_H_ALIGN_T m_HAlign;
  90. GR_TEXT_V_ALIGN_T m_VAlign;
  91. EDA_ANGLE m_Angle;
  92. };
  93. /**
  94. * Get the text info for the pin name.
  95. *
  96. * If the pin name is not visible, this will return an empty optional.
  97. */
  98. std::optional<TEXT_INFO> GetPinNameInfo( int aShadowWidth );
  99. std::optional<TEXT_INFO> GetPinNumberInfo( int aShadowWidth );
  100. std::optional<TEXT_INFO> GetPinElectricalTypeInfo( int aShadowWidth );
  101. private:
  102. bool isDirty( int aMask ) const
  103. {
  104. return m_dirtyFlags & aMask;
  105. }
  106. void setClean( int aMask )
  107. {
  108. m_dirtyFlags &= ~aMask;
  109. }
  110. /**
  111. * Cached extent of a text item.
  112. */
  113. struct TEXT_EXTENTS_CACHE
  114. {
  115. KIFONT::FONT* m_Font = nullptr;
  116. int m_FontSize = 0;
  117. VECTOR2I m_Extents;
  118. };
  119. static void recomputeExtentsCache( bool aDefinitelyDirty, KIFONT::FONT* aFont, int aSize,
  120. const wxString& aText, const KIFONT::METRICS& aFontMetrics,
  121. TEXT_EXTENTS_CACHE& aCache );
  122. /**
  123. * Recompute all the caches that have become dirty.
  124. */
  125. void recomputeCaches();
  126. /**
  127. * Transform a box (in-place) to the pin's orientation.
  128. */
  129. void transformBoxForPin( BOX2I& aBox ) const;
  130. /**
  131. * Transform text info to suit a pin's
  132. *
  133. * @param the 'nominal' text info for a PIN_RIGHT pin, which will be adjusted
  134. */
  135. void transformTextForPin( TEXT_INFO& aTextInfo ) const;
  136. /**
  137. * Get the current pin text offset
  138. */
  139. int getPinTextOffset() const;
  140. /**
  141. * Get the untransformd text box in the default orientation
  142. *
  143. * This will have to be offset and rotated.
  144. */
  145. OPT_BOX2I getUntransformedPinNameBox() const;
  146. OPT_BOX2I getUntransformedPinNumberBox() const;
  147. OPT_BOX2I getUntransformedPinTypeBox() const;
  148. OPT_BOX2I getUntransformedAltIconBox() const;
  149. /// Pin type decoration if any
  150. OPT_BOX2I getUntransformedDecorationBox() const;
  151. /// The pin in question
  152. const SCH_PIN& m_pin;
  153. // The schematic settings if there are any
  154. const SCHEMATIC_SETTINGS* m_schSettings;
  155. int m_dirtyFlags;
  156. // Cached render parameters
  157. float m_shadowOffsetAdjust = 1.0f;
  158. int m_nameThickness = 0;
  159. int m_numberThickness = 0;
  160. bool m_showElectricalType = false;
  161. bool m_showAltIcons = false;
  162. // Various cache members
  163. TEXT_EXTENTS_CACHE m_numExtentsCache;
  164. TEXT_EXTENTS_CACHE m_nameExtentsCache;
  165. TEXT_EXTENTS_CACHE m_typeExtentsCache;
  166. };