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.

237 lines
8.8 KiB

// Dick Hollenbeck's KiROUND R&D // This provides better project control over rounding to int from double // than wxRound() did. This scheme provides better logging in Debug builds // and it provides for compile time calculation of constants. #include <stdio.h> #include <assert.h> #include <limits.h> //-----<KiROUND KIT>------------------------------------------------------------ /** * KiROUND * rounds a floating point number to an int using * "round halfway cases away from zero". * In Debug build an assert fires if will not fit into an int. */ #if defined( DEBUG ) // DEBUG: a macro to capture line and file, then calls this inline static inline int KiRound( double v, int line, const char* filename ) { v = v < 0 ? v - 0.5 : v + 0.5; if( v > INT_MAX + 0.5 ) { printf( "%s: in file %s on line %d, val: %.16g too ' > 0 ' for int\n", __FUNCTION__, filename, line, v ); } else if( v < INT_MIN - 0.5 ) { printf( "%s: in file %s on line %d, val: %.16g too ' < 0 ' for int\n", __FUNCTION__, filename, line, v ); } return int( v ); } #define KiROUND( v ) KiRound( v, __LINE__, __FILE__ ) #else // RELEASE: a macro so compile can pre-compute constants. #define KiROUND( v ) int( (v) < 0 ? (v) - 0.5 : (v) + 0.5 ) #endif //-----</KiROUND KIT>----------------------------------------------------------- // Only a macro is compile time calculated, an inline function causes a static constructor // in a situation like this. // Therefore the Release build is best done with a MACRO not an inline function. int Computed = KiROUND( 14.3 * 8 ); int main( int argc, char** argv ) { for( double d = double(INT_MAX)-1; d < double(INT_MAX)+8; d += 2.0 ) { int i = KiROUND( d ); printf( "t: %d %.16g\n", i, d ); } return 0; }
14 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
  1. /**
  2. * Functions to draw and plot text on screen
  3. * @file draw_graphic_text.cpp
  4. */
  5. /*
  6. * This program source code file is part of KiCad, a free EDA CAD application.
  7. *
  8. * Copyright (C) 2018 Jean-Pierre Charras, jp.charras at wanadoo.fr
  9. * Copyright (C) 2012 SoftPLC Corporation, Dick Hollenbeck <dick@softplc.com>
  10. * Copyright (C) 2012 Wayne Stambaugh <stambaughw@gmail.com>
  11. * Copyright (C) 1992-2021 KiCad Developers, see AUTHORS.txt for contributors.
  12. *
  13. * This program is free software; you can redistribute it and/or
  14. * modify it under the terms of the GNU General Public License
  15. * as published by the Free Software Foundation; either version 2
  16. * of the License, or (at your option) any later version.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU General Public License
  24. * along with this program; if not, you may find one here:
  25. * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  26. * or you may search the http://www.gnu.org website for the version 2 license,
  27. * or you may write to the Free Software Foundation, Inc.,
  28. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  29. */
  30. #include <gr_basic.h>
  31. #include <plotters/plotter.h>
  32. #include <eda_text.h> // EDA_TEXT_HJUSTIFY_T and EDA_TEXT_VJUSTIFY_T
  33. #include <trigo.h>
  34. #include <base_screen.h>
  35. #include <math/util.h> // for KiROUND
  36. #include <basic_gal.h>
  37. /**
  38. * @param aTextSize is the char size (height or width).
  39. * @return the "best" value for a pen size to draw/plot a bold text.
  40. */
  41. int GetPenSizeForBold( int aTextSize )
  42. {
  43. return KiROUND( aTextSize / 5.0 );
  44. }
  45. int GetPenSizeForNormal( int aTextSize )
  46. {
  47. return KiROUND( aTextSize / 8.0 );
  48. }
  49. /**
  50. * Don't allow text to become cluttered up in its own fatness. Bold fonts are generally around
  51. * aSize/5 in width, so we limit them to aSize/4, and normal text to aSize/6.
  52. *
  53. * @param aPenSize is the pen size to clamp.
  54. * @param aSize is the character size (height or width).
  55. * @param aBold use true if text accept bold pen size.
  56. * @return the max pen size allowed.
  57. */
  58. int Clamp_Text_PenSize( int aPenSize, int aSize, bool aBold )
  59. {
  60. double scale = aBold ? 4.0 : 6.0;
  61. int maxWidth = KiROUND( (double) aSize / scale );
  62. return std::min( aPenSize, maxWidth );
  63. }
  64. float Clamp_Text_PenSize( float aPenSize, int aSize, bool aBold )
  65. {
  66. float scale = aBold ? 4.0 : 6.0;
  67. float maxWidth = (float) aSize / scale;
  68. return std::min( aPenSize, maxWidth );
  69. }
  70. int Clamp_Text_PenSize( int aPenSize, const wxSize& aSize, bool aBold )
  71. {
  72. int size = std::min( std::abs( aSize.x ), std::abs( aSize.y ) );
  73. return Clamp_Text_PenSize( aPenSize, size, aBold );
  74. }
  75. int GraphicTextWidth( const wxString& aText, const wxSize& aSize, bool aItalic, bool aBold )
  76. {
  77. basic_gal.SetFontItalic( aItalic );
  78. basic_gal.SetFontBold( aBold );
  79. basic_gal.SetGlyphSize( VECTOR2D( aSize ) );
  80. VECTOR2D tsize = basic_gal.GetTextLineSize( aText );
  81. return KiROUND( tsize.x );
  82. }
  83. /**
  84. * Draw a graphic text (like footprint texts).
  85. *
  86. * @param aDC is the current Device Context. NULL if draw within a 3D GL Canvas.
  87. * @param aPos is the text position (according to h_justify, v_justify).
  88. * @param aColor is the text color.
  89. * @param aText is the text to draw.
  90. * @param aOrient is the angle in 0.1 degree.
  91. * @param aSize is the text size (size.x or size.y can be < 0 for mirrored texts).
  92. * @param aH_justify is the horizontal justification (Left, center, right).
  93. * @param aV_justify is the vertical justification (bottom, center, top).
  94. * @param aWidth is the line width (pen width) (use default width if aWidth = 0).
  95. * if width < 0 : draw segments in sketch mode, width = abs(width)
  96. * Use a value min(aSize.x, aSize.y) / 5 for a bold text.
  97. * @param aItalic is the true to simulate an italic font.
  98. * @param aBold use true to use a bold font. Useful only with default width value (aWidth = 0).
  99. * @param aCallback( int x0, int y0, int xf, int yf, void* aData ) is a function called
  100. * (if non null) to draw each segment. used to draw 3D texts or for plotting.
  101. * NULL for normal drawings
  102. * @param aCallbackData is the auxiliary parameter aData for the callback function.
  103. * can be nullptr if no auxiliary parameter is needed
  104. * @param aPlotter is a PLOTTER instance, when this function is used to plot
  105. * the text. NULL to draw this text.
  106. */
  107. void GRText( wxDC* aDC, const wxPoint& aPos, const COLOR4D& aColor, const wxString& aText,
  108. double aOrient, const wxSize& aSize, enum EDA_TEXT_HJUSTIFY_T aH_justify,
  109. enum EDA_TEXT_VJUSTIFY_T aV_justify, int aWidth, bool aItalic, bool aBold,
  110. void (* aCallback)( int x0, int y0, int xf, int yf, void* aData ),
  111. void* aCallbackData, PLOTTER* aPlotter )
  112. {
  113. bool fill_mode = true;
  114. if( aWidth == 0 && aBold ) // Use default values if aWidth == 0
  115. aWidth = GetPenSizeForBold( std::min( aSize.x, aSize.y ) );
  116. if( aWidth < 0 )
  117. {
  118. aWidth = -aWidth;
  119. fill_mode = false;
  120. }
  121. basic_gal.SetIsFill( fill_mode );
  122. basic_gal.SetLineWidth( aWidth );
  123. EDA_TEXT dummy;
  124. dummy.SetItalic( aItalic );
  125. dummy.SetBold( aBold );
  126. dummy.SetHorizJustify( aH_justify );
  127. dummy.SetVertJustify( aV_justify );
  128. wxSize size = aSize;
  129. dummy.SetMirrored( size.x < 0 );
  130. if( size.x < 0 )
  131. size.x = - size.x;
  132. dummy.SetTextSize( size );
  133. basic_gal.SetTextAttributes( &dummy );
  134. basic_gal.SetPlotter( aPlotter );
  135. basic_gal.SetCallback( aCallback, aCallbackData );
  136. basic_gal.m_DC = aDC;
  137. basic_gal.m_Color = aColor;
  138. basic_gal.SetClipBox( nullptr );
  139. basic_gal.StrokeText( aText, VECTOR2D( aPos ), aOrient * M_PI/1800 );
  140. }
  141. void GRHaloText( wxDC* aDC, const wxPoint &aPos, const COLOR4D& aBgColor, const COLOR4D& aColor1,
  142. const COLOR4D& aColor2, const wxString &aText, double aOrient, const wxSize &aSize,
  143. enum EDA_TEXT_HJUSTIFY_T aH_justify, enum EDA_TEXT_VJUSTIFY_T aV_justify,
  144. int aWidth, bool aItalic, bool aBold,
  145. void (*aCallback)( int x0, int y0, int xf, int yf, void* aData ),
  146. void* aCallbackData, PLOTTER * aPlotter )
  147. {
  148. COLOR4D color1 = aColor1;
  149. COLOR4D color2 = aColor2;
  150. // Swap color if contrast would be better
  151. // TODO: Maybe calculate contrast some way other than brightness
  152. if( aBgColor.GetBrightness() > 0.5 )
  153. {
  154. COLOR4D c = color1;
  155. color1 = color2;
  156. color2 = c;
  157. }
  158. // Draw the background
  159. GRText( aDC, aPos, color1, aText, aOrient, aSize, aH_justify, aV_justify, aWidth, aItalic,
  160. aBold, aCallback, aCallbackData, aPlotter );
  161. // Draw the text
  162. GRText( aDC, aPos, color2, aText, aOrient, aSize, aH_justify, aV_justify, aWidth / 4, aItalic,
  163. aBold, aCallback, aCallbackData, aPlotter );
  164. }
  165. /**
  166. * Same as GRText, but plot graphic text instead of draw it.
  167. *
  168. * @param aPos is the text position (according to aH_justify, aV_justify).
  169. * @param aColor is the text color.
  170. * @param aText is the text to draw.
  171. * @param aOrient is the angle in 0.1 degree.
  172. * @param aSize is the text size (size.x or size.y can be < 0 for mirrored texts).
  173. * @param aH_justify is the horizontal justification (Left, center, right).
  174. * @param aV_justify is the vertical justification (bottom, center, top).
  175. * @param aPenWidth is the line width (if = 0, use plot default line width).
  176. * @param aItalic is the true to simulate an italic font.
  177. * @param aBold use true to use a bold font Useful only with default width value (aWidth = 0).
  178. * @param aMultilineAllowed use true to plot text as multiline, otherwise single line.
  179. * @param aData is a parameter used by some plotters in SetCurrentLineWidth(),
  180. * not directly used here.
  181. */
  182. void PLOTTER::Text( const wxPoint& aPos,
  183. const COLOR4D& aColor,
  184. const wxString& aText,
  185. double aOrient,
  186. const wxSize& aSize,
  187. enum EDA_TEXT_HJUSTIFY_T aH_justify,
  188. enum EDA_TEXT_VJUSTIFY_T aV_justify,
  189. int aPenWidth,
  190. bool aItalic,
  191. bool aBold,
  192. bool aMultilineAllowed,
  193. void* aData )
  194. {
  195. SetColor( aColor );
  196. SetCurrentLineWidth( aPenWidth, aData );
  197. GRText( nullptr, aPos, aColor, aText, aOrient, aSize, aH_justify, aV_justify, aPenWidth,
  198. aItalic, aBold, nullptr, nullptr, this );
  199. }