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.

132 lines
4.7 KiB

17 years ago
  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2009-2014 Jerry Jacobs
  5. * Copyright (C) 1992-2023 KiCad Developers, see AUTHORS.txt for contributors.
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; either version 2
  10. * of the License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, you may find one here:
  19. * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  20. * or you may search the http://www.gnu.org website for the version 2 license,
  21. * or you may write to the Free Software Foundation, Inc.,
  22. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  23. */
  24. #ifndef GR_TEXT_H
  25. #define GR_TEXT_H
  26. #include <font/text_attributes.h>
  27. #include <wx/gdicmn.h>
  28. class wxDC;
  29. namespace KIGFX
  30. {
  31. class COLOR4D;
  32. }
  33. namespace KIFONT
  34. {
  35. class METRICS;
  36. }
  37. class PLOTTER;
  38. /**
  39. * Pen width should not allow characters to become cluttered up in their own fatness. Normal
  40. * text is normally around 15% the fontsize, and bold text around 20%. So we set a hard limit
  41. * at 25%, and a secondary limit for non-decorative text that must be readable at small sizes
  42. * at 18%.
  43. *
  44. * @param aPenSize the pen size to clamp.
  45. * @param aSize the char size (height or width, or its wxSize).
  46. * @param aBold true if text accept bold pen size.
  47. * @return the max pen size allowed.
  48. */
  49. int Clamp_Text_PenSize( int aPenSize, int aSize, bool aStrict = false );
  50. float Clamp_Text_PenSize( float aPenSize, int aSize, bool aStrict = false );
  51. int Clamp_Text_PenSize( int aPenSize, const VECTOR2I& aSize, bool aStrict = false );
  52. /**
  53. * @param aTextSize the char size (height or width).
  54. * @return the "best" value for a pen size to draw/plot a bold text.
  55. */
  56. int GetPenSizeForBold( int aTextSize );
  57. int GetPenSizeForBold( const wxSize& aTextSize );
  58. /**
  59. * @param aTextSize the char size (height or width).
  60. * @return the "best" value for a pen size to draw/plot a demibold text.
  61. */
  62. int GetPenSizeForDemiBold( int aTextSize );
  63. int GetPenSizeForDemiBold( const wxSize& aTextSize );
  64. /**
  65. * @param aTextSize = the char size (height or width).
  66. * @return the "best" value for a pen size to draw/plot a non-bold text.
  67. */
  68. int GetPenSizeForNormal( int aTextSize );
  69. int GetPenSizeForNormal( const wxSize& aTextSize );
  70. inline void InferBold( TEXT_ATTRIBUTES* aAttrs )
  71. {
  72. int penSize( aAttrs->m_StrokeWidth );
  73. wxSize textSize( aAttrs->m_Size.x, aAttrs->m_Size.y );
  74. aAttrs->m_Bold = abs( penSize - GetPenSizeForBold( textSize ) )
  75. < abs( penSize - GetPenSizeForNormal( textSize ) );
  76. }
  77. /**
  78. * Returns the margin for knocking out text.
  79. */
  80. inline int GetKnockoutTextMargin( const VECTOR2I& aSize, int aThickness )
  81. {
  82. return std::max( KiROUND( aThickness / 2 ), KiROUND( aSize.y / 9.0 ) );
  83. }
  84. /**
  85. * @return the X size of the graphic text.
  86. */
  87. int GRTextWidth( const wxString& aText, KIFONT::FONT* aFont, const VECTOR2I& aSize,
  88. int aThickness, bool aBold, bool aItalic, const KIFONT::METRICS& aFontMetrics );
  89. /**
  90. * Print a graphic text through wxDC.
  91. *
  92. * @param aDC is the current Device Context.
  93. * @param aPos is the text position (according to h_justify, v_justify).
  94. * @param aColor is the text color.
  95. * @param aText is the text to draw.
  96. * @param aOrient is the angle.
  97. * @param aSize is the text size (size.x or size.y can be < 0 for mirrored texts).
  98. * @param aH_justify is the horizontal justification (Left, center, right).
  99. * @param aV_justify is the vertical justification (bottom, center, top).
  100. * @param aWidth is the line width (pen width) (use default width if aWidth = 0).
  101. * if width < 0 : draw segments in sketch mode, width = abs(width)
  102. * Use a value min(aSize.x, aSize.y) / 5 for a bold text.
  103. * @param aItalic is the true to simulate an italic font.
  104. * @param aBold use true to use a bold font. Useful only with default width value (aWidth = 0).
  105. * @param aFont is the font to use, or nullptr for the KiCad stroke font
  106. */
  107. void GRPrintText( wxDC* aDC, const VECTOR2I& aPos, const KIGFX::COLOR4D& aColor,
  108. const wxString& aText, const EDA_ANGLE& aOrient, const VECTOR2I& aSize,
  109. enum GR_TEXT_H_ALIGN_T aH_justify, enum GR_TEXT_V_ALIGN_T aV_justify,
  110. int aWidth, bool aItalic, bool aBold, KIFONT::FONT* aFont,
  111. const KIFONT::METRICS& aFontMetrics );
  112. #endif /* GR_TEXT_H */