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.

263 lines
9.6 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
18 years ago
  1. /**
  2. * Functions to draw and plot text on screen
  3. * @file drawtxt.cpp
  4. */
  5. /*
  6. * This program source code file is part of KiCad, a free EDA CAD application.
  7. *
  8. * Copyright (C) 2016 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@verizon.net>
  11. * Copyright (C) 1992-2016 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 <fctsys.h>
  31. #include <gr_basic.h>
  32. #include <common.h>
  33. #include <plot_common.h>
  34. #include <eda_text.h> // EDA_TEXT_HJUSTIFY_T and EDA_TEXT_VJUSTIFY_T
  35. #include <trigo.h>
  36. #include <macros.h>
  37. #include <class_drawpanel.h>
  38. #include <class_base_screen.h>
  39. #include <basic_gal.h>
  40. /**
  41. * Function GetPensizeForBold
  42. * @return the "best" value for a pen size to draw/plot a bold text
  43. * @param aTextSize = the char size (height or width)
  44. */
  45. int GetPenSizeForBold( int aTextSize )
  46. {
  47. return KiROUND( aTextSize / 5.0 );
  48. }
  49. /**
  50. * Function Clamp_Text_PenSize
  51. * As a rule, pen width should not be >1/4em, otherwise the character
  52. * will be cluttered up in its own fatness
  53. * so pen width max is aSize/4 for bold text, and aSize/6 for normal text
  54. * The "best" pen width is aSize/5 for bold texts,
  55. * so the clamp is consistant with bold option.
  56. * @param aPenSize = the pen size to clamp
  57. * @param aSize the char size (height or width)
  58. * @param aBold = true if text accept bold pen size
  59. * @return the max pen size allowed
  60. */
  61. int Clamp_Text_PenSize( int aPenSize, int aSize, bool aBold )
  62. {
  63. int penSize = aPenSize;
  64. double scale = aBold ? 4.0 : 6.0;
  65. int maxWidth = KiROUND( std::abs( aSize ) / scale );
  66. if( penSize > maxWidth )
  67. penSize = maxWidth;
  68. return penSize;
  69. }
  70. int Clamp_Text_PenSize( int aPenSize, 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. * Function DrawGraphicText
  85. * Draw a graphic text (like module texts)
  86. * @param aClipBox = the clipping rect, or NULL if no clipping
  87. * @param aDC = the current Device Context. NULL if draw within a 3D GL Canvas
  88. * @param aPos = text position (according to h_justify, v_justify)
  89. * @param aColor (COLOR4D) = text color
  90. * @param aText = text to draw
  91. * @param aOrient = angle in 0.1 degree
  92. * @param aSize = text size (size.x or size.y can be < 0 for mirrored texts)
  93. * @param aH_justify = horizontal justification (Left, center, right)
  94. * @param aV_justify = vertical justification (bottom, center, top)
  95. * @param aWidth = line width (pen width) (use default width if aWidth = 0)
  96. * if width < 0 : draw segments in sketch mode, width = abs(width)
  97. * Use a value min(aSize.x, aSize.y) / 5 for a bold text
  98. * @param aItalic = true to simulate an italic font
  99. * @param aBold = true to use a bold font. Useful only with default width value (aWidth = 0)
  100. * @param aCallback() = function called (if non null) to draw each segment.
  101. * used to draw 3D texts or for plotting, NULL for normal drawings
  102. * @param aPlotter = a pointer to a PLOTTER instance, when this function is used to plot
  103. * the text. NULL to draw this text.
  104. */
  105. void DrawGraphicText( EDA_RECT* aClipBox,
  106. wxDC* aDC,
  107. const wxPoint& aPos,
  108. COLOR4D aColor,
  109. const wxString& aText,
  110. double aOrient,
  111. const wxSize& aSize,
  112. enum EDA_TEXT_HJUSTIFY_T aH_justify,
  113. enum EDA_TEXT_VJUSTIFY_T aV_justify,
  114. int aWidth,
  115. bool aItalic,
  116. bool aBold,
  117. void (* aCallback)( int x0, int y0, int xf, int yf ),
  118. PLOTTER* aPlotter )
  119. {
  120. bool fill_mode = true;
  121. if( aWidth == 0 && aBold ) // Use default values if aWidth == 0
  122. aWidth = GetPenSizeForBold( std::min( aSize.x, aSize.y ) );
  123. if( aWidth < 0 )
  124. {
  125. aWidth = -aWidth;
  126. fill_mode = false;
  127. }
  128. basic_gal.SetIsFill( fill_mode );
  129. basic_gal.SetLineWidth( aWidth );
  130. EDA_TEXT dummy;
  131. dummy.SetItalic( aItalic );
  132. dummy.SetBold( aBold );
  133. dummy.SetHorizJustify( aH_justify );
  134. dummy.SetVertJustify( aV_justify );
  135. wxSize size = aSize;
  136. dummy.SetMirrored( size.x < 0 );
  137. if( size.x < 0 )
  138. size.x = - size.x;
  139. dummy.SetTextSize( size );
  140. basic_gal.SetTextAttributes( &dummy );
  141. basic_gal.SetPlotter( aPlotter );
  142. basic_gal.SetCallback( aCallback );
  143. basic_gal.m_DC = aDC;
  144. basic_gal.m_Color = aColor;
  145. basic_gal.SetClipBox( aClipBox );
  146. basic_gal.StrokeText( aText, VECTOR2D( aPos ), aOrient * M_PI/1800 );
  147. }
  148. void DrawGraphicHaloText( EDA_RECT* aClipBox, wxDC * aDC,
  149. const wxPoint &aPos,
  150. const COLOR4D aBgColor,
  151. COLOR4D aColor1,
  152. COLOR4D aColor2,
  153. const wxString &aText,
  154. double aOrient,
  155. const wxSize &aSize,
  156. enum EDA_TEXT_HJUSTIFY_T aH_justify,
  157. enum EDA_TEXT_VJUSTIFY_T aV_justify,
  158. int aWidth, bool aItalic, bool aBold,
  159. void (*aCallback)( int x0, int y0, int xf, int yf ),
  160. PLOTTER * aPlotter )
  161. {
  162. // Swap color if contrast would be better
  163. // TODO: Maybe calculate contrast some way other than brightness
  164. if( aBgColor.GetBrightness() > 0.5 )
  165. {
  166. COLOR4D c = aColor1;
  167. aColor1 = aColor2;
  168. aColor2 = c;
  169. }
  170. // Draw the background
  171. DrawGraphicText( aClipBox, aDC, aPos, aColor1, aText, aOrient, aSize,
  172. aH_justify, aV_justify, aWidth, aItalic, aBold,
  173. aCallback, aPlotter );
  174. // Draw the text
  175. DrawGraphicText( aClipBox, aDC, aPos, aColor2, aText, aOrient, aSize,
  176. aH_justify, aV_justify, aWidth/4, aItalic, aBold,
  177. aCallback, aPlotter );
  178. }
  179. /**
  180. * Function PLOTTER::Text
  181. * same as DrawGraphicText, but plot graphic text insteed of draw it
  182. * @param aPos = text position (according to aH_justify, aV_justify)
  183. * @param aColor (COLOR4D) = text color
  184. * @param aText = text to draw
  185. * @param aOrient = angle in 0.1 degree
  186. * @param aSize = text size (size.x or size.y can be < 0 for mirrored texts)
  187. * @param aH_justify = horizontal justification (Left, center, right)
  188. * @param aV_justify = vertical justification (bottom, center, top)
  189. * @param aWidth = line width (pen width) (default = 0)
  190. * if width < 0 : draw segments in sketch mode, width = abs(width)
  191. * Use a value min(aSize.x, aSize.y) / 5 for a bold text
  192. * @param aItalic = true to simulate an italic font
  193. * @param aBold = true to use a bold font Useful only with default width value (aWidth = 0)
  194. * @param aMultilineAllowed = true to plot text as multiline, otherwise single line
  195. * @param aData = a parameter used by some plotters in SetCurrentLineWidth(),
  196. * not directly used here.
  197. */
  198. void PLOTTER::Text( const wxPoint& aPos,
  199. const COLOR4D aColor,
  200. const wxString& aText,
  201. double aOrient,
  202. const wxSize& aSize,
  203. enum EDA_TEXT_HJUSTIFY_T aH_justify,
  204. enum EDA_TEXT_VJUSTIFY_T aV_justify,
  205. int aWidth,
  206. bool aItalic,
  207. bool aBold,
  208. bool aMultilineAllowed,
  209. void* aData )
  210. {
  211. int textPensize = aWidth;
  212. if( textPensize == 0 && aBold ) // Use default values if aWidth == 0
  213. textPensize = GetPenSizeForBold( std::min( aSize.x, aSize.y ) );
  214. if( textPensize >= 0 )
  215. textPensize = Clamp_Text_PenSize( aWidth, aSize, aBold );
  216. else
  217. textPensize = -Clamp_Text_PenSize( -aWidth, aSize, aBold );
  218. SetCurrentLineWidth( textPensize, aData );
  219. SetColor( aColor );
  220. DrawGraphicText( NULL, NULL, aPos, aColor, aText,
  221. aOrient, aSize,
  222. aH_justify, aV_justify,
  223. textPensize, aItalic, aBold, NULL, this );
  224. if( aWidth != textPensize )
  225. SetCurrentLineWidth( aWidth, aData );
  226. }