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.

580 lines
19 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
// 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
// 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
// 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
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
// 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
// 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
// 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 drawtxt.cpp
  4. */
  5. /*
  6. * This program source code file is part of KiCad, a free EDA CAD application.
  7. *
  8. * Copyright (C) 2012 Jean-Pierre Charras, jean-pierre.charras@ujf-grenoble.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-2012 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. #ifndef DEFAULT_SIZE_TEXT
  40. # define DEFAULT_SIZE_TEXT 50
  41. #endif
  42. #define EDA_DRAWBASE
  43. #include <newstroke_font.h>
  44. #include <plot_common.h>
  45. /* factor used to calculate actual size of shapes from hershey fonts (could be adjusted depending on the font name)
  46. * Its value is choosen in order to have letters like M, P .. vertical size equal to the vertical char size parameter
  47. * Of course some shapes can be bigger or smaller than the vertical char size parameter
  48. */
  49. #define HERSHEY_SCALE_FACTOR 1 / 21.0
  50. double s_HerscheyScaleFactor = HERSHEY_SCALE_FACTOR;
  51. /* Helper function for texts with over bar
  52. */
  53. int OverbarPositionY( int size_v, int thickness )
  54. {
  55. return KiROUND( ( (double) size_v * 1.1 ) + ( (double) thickness * 1.5 ) );
  56. }
  57. /**
  58. * Function GetPensizeForBold
  59. * @return the "best" value for a pen size to draw/plot a bold text
  60. * @param aTextSize = the char size (height or width)
  61. */
  62. int GetPenSizeForBold( int aTextSize )
  63. {
  64. return KiROUND( aTextSize / 5.0 );
  65. }
  66. /**
  67. * Function Clamp_Text_PenSize
  68. * As a rule, pen width should not be >1/4em, otherwise the character
  69. * will be cluttered up in its own fatness
  70. * so pen width max is aSize/4 for bold text, and aSize/6 for normal text
  71. * The "best" pen width is aSize/5 for bold texts,
  72. * so the clamp is consistant with bold option.
  73. * @param aPenSize = the pen size to clamp
  74. * @param aSize the char size (height or width)
  75. * @param aBold = true if text accept bold pen size
  76. * @return the max pen size allowed
  77. */
  78. int Clamp_Text_PenSize( int aPenSize, int aSize, bool aBold )
  79. {
  80. int penSize = aPenSize;
  81. double scale = aBold ? 4.0 : 6.0;
  82. int maxWidth = KiROUND( ABS( aSize ) / scale );
  83. if( penSize > maxWidth )
  84. penSize = maxWidth;
  85. return penSize;
  86. }
  87. int Clamp_Text_PenSize( int aPenSize, wxSize aSize, bool aBold )
  88. {
  89. int size = MIN( ABS( aSize.x ), ABS( aSize.y ) );
  90. return Clamp_Text_PenSize( aPenSize, size, aBold );
  91. }
  92. /* Functions to draw / plot a string.
  93. * texts have only one line.
  94. * They can be in italic.
  95. * Horizontal and Vertical justification are handled.
  96. * Texts can be rotated
  97. * substrings between ~ markers can be "negated" (i.e. with an over bar
  98. */
  99. /**
  100. * Function NegableTextLength
  101. * Return the text length of a negable string, excluding the ~ markers */
  102. int NegableTextLength( const wxString& aText )
  103. {
  104. int char_count = aText.length();
  105. /* Fix the character count, removing the ~ found */
  106. for( int i = char_count - 1; i >= 0; i-- )
  107. {
  108. if( aText[i] == '~' )
  109. {
  110. char_count--;
  111. }
  112. }
  113. return char_count;
  114. }
  115. /* Function GetHersheyShapeDescription
  116. * return a pointer to the shape corresponding to unicode value AsciiCode
  117. * Note we use the same font for Bold and Normal texts
  118. * because KiCad handles a variable pen size to do that
  119. * that gives better results in XOR draw mode.
  120. */
  121. static const char* GetHersheyShapeDescription( int AsciiCode )
  122. {
  123. /* calculate font length */
  124. int font_length_max = newstroke_font_bufsize;
  125. if( AsciiCode >= (32 + font_length_max) )
  126. AsciiCode = '?';
  127. if( AsciiCode < 32 )
  128. AsciiCode = 32; /* Clamp control chars */
  129. AsciiCode -= 32;
  130. return newstroke_font[AsciiCode];
  131. }
  132. int ReturnGraphicTextWidth( const wxString& aText, int aXSize, bool aItalic, bool aWidth )
  133. {
  134. int tally = 0;
  135. int char_count = aText.length();
  136. for( int i = 0; i < char_count; i++ )
  137. {
  138. int AsciiCode = aText[i];
  139. if( AsciiCode == '~' ) /* Skip the negation marks */
  140. {
  141. continue;
  142. }
  143. const char* ptcar = GetHersheyShapeDescription( AsciiCode );
  144. /* Get metrics */
  145. int xsta = *ptcar++ - 'R';
  146. int xsto = *ptcar++ - 'R';
  147. tally += KiROUND( aXSize * (xsto - xsta) * s_HerscheyScaleFactor );
  148. }
  149. /* Italic correction, 1/8em */
  150. if( aItalic )
  151. {
  152. tally += KiROUND( aXSize * 0.125 );
  153. }
  154. return tally;
  155. }
  156. /* Helper function for drawing character polygons */
  157. static void DrawGraphicTextPline(
  158. EDA_RECT* aClipBox,
  159. wxDC* aDC,
  160. EDA_COLOR_T aColor,
  161. int aWidth,
  162. bool aSketchMode,
  163. int point_count,
  164. wxPoint* coord,
  165. void (* aCallback)(int x0, int y0, int xf, int yf ),
  166. PLOTTER* aPlotter )
  167. {
  168. if( aPlotter )
  169. {
  170. aPlotter->MoveTo( coord[0] );
  171. for( int ik = 1; ik < point_count; ik++ )
  172. {
  173. aPlotter->LineTo( coord[ik] );
  174. }
  175. aPlotter->PenFinish();
  176. }
  177. else if( aCallback )
  178. {
  179. for( int ik = 0; ik < (point_count - 1); ik++ )
  180. {
  181. aCallback( coord[ik].x, coord[ik].y,
  182. coord[ik + 1].x, coord[ik + 1].y );
  183. }
  184. }
  185. else if( aSketchMode )
  186. {
  187. for( int ik = 0; ik < (point_count - 1); ik++ )
  188. GRCSegm( aClipBox, aDC, coord[ik].x, coord[ik].y,
  189. coord[ik + 1].x, coord[ik + 1].y, aWidth, aColor );
  190. }
  191. else
  192. GRPoly( aClipBox, aDC, point_count, coord, 0,
  193. aWidth, aColor, aColor );
  194. }
  195. /**
  196. * Function DrawGraphicText
  197. * Draw a graphic text (like module texts)
  198. * @param aPanel = the current m_canvas. NULL if draw within a 3D GL Canvas
  199. * @param aDC = the current Device Context. NULL if draw within a 3D GL Canvas
  200. * @param aPos = text position (according to h_justify, v_justify)
  201. * @param aColor (enum EDA_COLOR_T) = text color
  202. * @param aText = text to draw
  203. * @param aOrient = angle in 0.1 degree
  204. * @param aSize = text size (size.x or size.y can be < 0 for mirrored texts)
  205. * @param aH_justify = horizontal justification (Left, center, right)
  206. * @param aV_justify = vertical justification (bottom, center, top)
  207. * @param aWidth = line width (pen width) (use default width if aWidth = 0)
  208. * if width < 0 : draw segments in sketch mode, width = abs(width)
  209. * Use a value min(aSize.x, aSize.y) / 5 for a bold text
  210. * @param aItalic = true to simulate an italic font
  211. * @param aBold = true to use a bold font. Useful only with default width value (aWidth = 0)
  212. * @param aCallback() = function called (if non null) to draw each segment.
  213. * used to draw 3D texts or for plotting, NULL for normal drawings
  214. * @param aPlotter = a pointer to a PLOTTER instance, when this function is used to plot
  215. * the text. NULL to draw this text.
  216. */
  217. void DrawGraphicText( EDA_DRAW_PANEL* aPanel,
  218. wxDC* aDC,
  219. const wxPoint& aPos,
  220. EDA_COLOR_T aColor,
  221. const wxString& aText,
  222. int aOrient,
  223. const wxSize& aSize,
  224. enum EDA_TEXT_HJUSTIFY_T aH_justify,
  225. enum EDA_TEXT_VJUSTIFY_T aV_justify,
  226. int aWidth,
  227. bool aItalic,
  228. bool aBold,
  229. void (* aCallback)( int x0, int y0, int xf, int yf ),
  230. PLOTTER* aPlotter )
  231. {
  232. int AsciiCode;
  233. int x0, y0;
  234. int size_h, size_v;
  235. unsigned ptr;
  236. int dx, dy; // Draw coordinate for segments to draw. also used in some other calculation
  237. wxPoint current_char_pos; // Draw coordinates for the current char
  238. wxPoint overbar_pos; // Start point for the current overbar
  239. int overbar_italic_comp; // Italic compensation for overbar
  240. EDA_RECT* clipBox; // Clip box used in basic draw functions
  241. clipBox = aPanel ? aPanel->GetClipBox() : NULL;
  242. #define BUF_SIZE 100
  243. wxPoint coord[BUF_SIZE + 1]; // Buffer coordinate used to draw polylines (one char shape)
  244. bool sketch_mode = false;
  245. bool italic_reverse = false; // true for mirrored texts with m_Size.x < 0
  246. size_h = aSize.x; /* PLEASE NOTE: H is for HORIZONTAL not for HEIGHT */
  247. size_v = aSize.y;
  248. if( aWidth == 0 && aBold ) // Use default values if aWidth == 0
  249. aWidth = GetPenSizeForBold( MIN( aSize.x, aSize.y ) );
  250. if( aWidth < 0 )
  251. {
  252. aWidth = -aWidth;
  253. sketch_mode = true;
  254. }
  255. #ifdef CLIP_PEN // made by draw and plot functions
  256. aWidth = Clamp_Text_PenSize( aWidth, aSize, aBold );
  257. #endif
  258. if( size_h < 0 ) // text is mirrored using size.x < 0 (mirror / Y axis)
  259. italic_reverse = true;
  260. unsigned char_count = NegableTextLength( aText );
  261. if( char_count == 0 )
  262. return;
  263. current_char_pos = aPos;
  264. dx = ReturnGraphicTextWidth( aText, size_h, aItalic, aWidth );
  265. dy = size_v;
  266. /* Do not draw the text if out of draw area! */
  267. if( aPanel )
  268. {
  269. int xm, ym, ll, xc, yc;
  270. ll = ABS( dx );
  271. xc = current_char_pos.x;
  272. yc = current_char_pos.y;
  273. x0 = aPanel->GetClipBox()->GetX() - ll;
  274. y0 = aPanel->GetClipBox()->GetY() - ll;
  275. xm = aPanel->GetClipBox()->GetRight() + ll;
  276. ym = aPanel->GetClipBox()->GetBottom() + ll;
  277. if( xc < x0 )
  278. return;
  279. if( yc < y0 )
  280. return;
  281. if( xc > xm )
  282. return;
  283. if( yc > ym )
  284. return;
  285. }
  286. /* Compute the position of the first letter of the text
  287. * this position is the position of the left bottom point of the letter
  288. * this is the same as the text position only for a left and bottom justified text
  289. * In others cases, this position must be calculated from the text position ans size
  290. */
  291. switch( aH_justify )
  292. {
  293. case GR_TEXT_HJUSTIFY_CENTER:
  294. current_char_pos.x -= dx / 2;
  295. break;
  296. case GR_TEXT_HJUSTIFY_RIGHT:
  297. current_char_pos.x -= dx;
  298. break;
  299. case GR_TEXT_HJUSTIFY_LEFT:
  300. break;
  301. }
  302. switch( aV_justify )
  303. {
  304. case GR_TEXT_VJUSTIFY_CENTER:
  305. current_char_pos.y += dy / 2;
  306. break;
  307. case GR_TEXT_VJUSTIFY_TOP:
  308. current_char_pos.y += dy;
  309. break;
  310. case GR_TEXT_VJUSTIFY_BOTTOM:
  311. break;
  312. }
  313. // Note: if aPanel == NULL, we are using a GL Canvas that handle scaling
  314. if( aSize.x == 0 )
  315. return;
  316. /* if a text size is too small, the text cannot be drawn, and it is drawn as a single
  317. * graphic line */
  318. if( ABS( aSize.x ) < 3 )
  319. {
  320. /* draw the text as a line always vertically centered */
  321. wxPoint end( current_char_pos.x + dx, current_char_pos.y );
  322. RotatePoint( &current_char_pos, aPos, aOrient );
  323. RotatePoint( &end, aPos, aOrient );
  324. if( aPlotter )
  325. {
  326. aPlotter->MoveTo( current_char_pos );
  327. aPlotter->FinishTo( end );
  328. }
  329. else if( aCallback )
  330. {
  331. aCallback( current_char_pos.x, current_char_pos.y, end.x, end.y );
  332. }
  333. else
  334. GRLine( clipBox, aDC,
  335. current_char_pos.x, current_char_pos.y, end.x, end.y, aWidth, aColor );
  336. return;
  337. }
  338. if( aItalic )
  339. {
  340. overbar_italic_comp = OverbarPositionY( size_v, aWidth ) / 8;
  341. if( italic_reverse )
  342. {
  343. overbar_italic_comp = -overbar_italic_comp;
  344. }
  345. }
  346. else
  347. {
  348. overbar_italic_comp = 0;
  349. };
  350. int overbars = 0; // Number of ~ seen
  351. ptr = 0; /* ptr = text index */
  352. while( ptr < char_count )
  353. {
  354. if( aText[ptr + overbars] == '~' )
  355. {
  356. /* Found an overbar, adjust the pointers */
  357. overbars++;
  358. if( overbars & 1 ) // odd overbars count
  359. {
  360. /* Starting the overbar */
  361. overbar_pos = current_char_pos;
  362. overbar_pos.x += overbar_italic_comp;
  363. overbar_pos.y -= OverbarPositionY( size_v, aWidth );
  364. RotatePoint( &overbar_pos, aPos, aOrient );
  365. }
  366. else
  367. {
  368. /* Ending the overbar */
  369. coord[0] = overbar_pos;
  370. overbar_pos = current_char_pos;
  371. overbar_pos.x += overbar_italic_comp;
  372. overbar_pos.y -= OverbarPositionY( size_v, aWidth );
  373. RotatePoint( &overbar_pos, aPos, aOrient );
  374. coord[1] = overbar_pos;
  375. /* Plot the overbar segment */
  376. DrawGraphicTextPline( clipBox, aDC, aColor, aWidth,
  377. sketch_mode, 2, coord, aCallback, aPlotter );
  378. }
  379. continue; /* Skip ~ processing */
  380. }
  381. AsciiCode = aText.GetChar( ptr + overbars );
  382. const char* ptcar = GetHersheyShapeDescription( AsciiCode );
  383. /* Get metrics */
  384. int xsta = *ptcar++ - 'R';
  385. int xsto = *ptcar++ - 'R';
  386. int point_count = 0;
  387. bool endcar = false;
  388. while( !endcar )
  389. {
  390. int hc1, hc2;
  391. hc1 = *ptcar++;
  392. if( hc1 )
  393. {
  394. hc2 = *ptcar++;
  395. }
  396. else
  397. {
  398. // End of character, insert a synthetic pen up:
  399. hc1 = ' ';
  400. hc2 = 'R';
  401. endcar = true;
  402. }
  403. // Do the Hershey decode thing: coordinates values are coded as <value> + 'R'
  404. hc1 -= 'R'; hc2 -= 'R';
  405. /* Pen up request */
  406. if( hc1 == -50 && hc2 == 0 )
  407. {
  408. if( point_count )
  409. {
  410. if( aWidth <= 1 )
  411. aWidth = 0;
  412. DrawGraphicTextPline( clipBox, aDC, aColor, aWidth,
  413. sketch_mode, point_count, coord,
  414. aCallback, aPlotter );
  415. }
  416. point_count = 0;
  417. }
  418. else
  419. {
  420. wxPoint currpoint;
  421. hc1 -= xsta; hc2 -= 11; /* Align the midpoint */
  422. hc1 = KiROUND( hc1 * size_h * s_HerscheyScaleFactor );
  423. hc2 = KiROUND( hc2 * size_v * s_HerscheyScaleFactor );
  424. // To simulate an italic font, add a x offset depending on the y offset
  425. if( aItalic )
  426. hc1 -= KiROUND( italic_reverse ? -hc2 / 8.0 : hc2 / 8.0 );
  427. currpoint.x = hc1 + current_char_pos.x;
  428. currpoint.y = hc2 + current_char_pos.y;
  429. RotatePoint( &currpoint, aPos, aOrient );
  430. coord[point_count] = currpoint;
  431. if( point_count < BUF_SIZE - 1 )
  432. point_count++;
  433. }
  434. }
  435. /* end draw 1 char */
  436. ptr++;
  437. // Apply the advance width
  438. current_char_pos.x += KiROUND( size_h * (xsto - xsta) * s_HerscheyScaleFactor );
  439. }
  440. if( overbars % 2 )
  441. {
  442. /* Close the last overbar */
  443. coord[0] = overbar_pos;
  444. overbar_pos = current_char_pos;
  445. overbar_pos.y -= OverbarPositionY( size_v, aWidth );
  446. RotatePoint( &overbar_pos, aPos, aOrient );
  447. coord[1] = overbar_pos;
  448. /* Plot the overbar segment */
  449. DrawGraphicTextPline( clipBox, aDC, aColor, aWidth,
  450. sketch_mode, 2, coord, aCallback, aPlotter );
  451. }
  452. }
  453. /**
  454. * Function PlotGraphicText
  455. * same as DrawGraphicText, but plot graphic text insteed of draw it
  456. * @param aPos = text position (according to aH_justify, aV_justify)
  457. * @param aColor (enum EDA_COLOR_T) = text color
  458. * @param aText = text to draw
  459. * @param aOrient = angle in 0.1 degree
  460. * @param aSize = text size (size.x or size.y can be < 0 for mirrored texts)
  461. * @param aH_justify = horizontal justification (Left, center, right)
  462. * @param aV_justify = vertical justification (bottom, center, top)
  463. * @param aWidth = line width (pen width) (default = 0)
  464. * if width < 0 : draw segments in sketch mode, width = abs(width)
  465. * Use a value min(aSize.x, aSize.y) / 5 for a bold text
  466. * @param aItalic = true to simulate an italic font
  467. * @param aBold = true to use a bold font Useful only with default width value (aWidth = 0)
  468. */
  469. void PLOTTER::Text( const wxPoint& aPos,
  470. enum EDA_COLOR_T aColor,
  471. const wxString& aText,
  472. int aOrient,
  473. const wxSize& aSize,
  474. enum EDA_TEXT_HJUSTIFY_T aH_justify,
  475. enum EDA_TEXT_VJUSTIFY_T aV_justify,
  476. int aWidth,
  477. bool aItalic,
  478. bool aBold )
  479. {
  480. int textPensize = aWidth;
  481. if( textPensize == 0 && aBold ) // Use default values if aWidth == 0
  482. textPensize = GetPenSizeForBold( MIN( aSize.x, aSize.y ) );
  483. if( textPensize >= 0 )
  484. textPensize = Clamp_Text_PenSize( aWidth, aSize, aBold );
  485. else
  486. textPensize = -Clamp_Text_PenSize( -aWidth, aSize, aBold );
  487. SetCurrentLineWidth( textPensize );
  488. if( aColor >= 0 )
  489. SetColor( aColor );
  490. DrawGraphicText( NULL, NULL, aPos, aColor, aText,
  491. aOrient, aSize,
  492. aH_justify, aV_justify,
  493. textPensize, aItalic,
  494. aBold,
  495. NULL,
  496. this );
  497. if( aWidth != textPensize )
  498. SetCurrentLineWidth( aWidth );
  499. }