2009-03-02 13:43:52 +00:00
|
|
|
/**
|
|
|
|
* Functions to draw and plot text on screen
|
2018-04-06 13:30:52 +00:00
|
|
|
* @file draw_graphic_text.cpp
|
2009-03-02 13:43:52 +00:00
|
|
|
*/
|
2012-06-09 09:38:58 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* This program source code file is part of KiCad, a free EDA CAD application.
|
|
|
|
*
|
2018-04-06 13:30:52 +00:00
|
|
|
* Copyright (C) 2018 Jean-Pierre Charras, jp.charras at wanadoo.fr
|
2012-06-09 09:38:58 +00:00
|
|
|
* Copyright (C) 2012 SoftPLC Corporation, Dick Hollenbeck <dick@softplc.com>
|
|
|
|
* Copyright (C) 2012 Wayne Stambaugh <stambaughw@verizon.net>
|
2018-04-06 13:30:52 +00:00
|
|
|
* Copyright (C) 1992-2018 KiCad Developers, see AUTHORS.txt for contributors.
|
2012-06-09 09:38:58 +00:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, you may find one here:
|
|
|
|
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
|
|
|
|
* or you may search the http://www.gnu.org website for the version 2 license,
|
|
|
|
* or you may write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
|
|
|
|
*/
|
|
|
|
|
2012-01-23 04:33:36 +00:00
|
|
|
#include <fctsys.h>
|
|
|
|
#include <gr_basic.h>
|
|
|
|
#include <common.h>
|
2018-01-28 18:12:26 +00:00
|
|
|
#include <plotter.h>
|
2012-04-12 21:31:31 +00:00
|
|
|
#include <eda_text.h> // EDA_TEXT_HJUSTIFY_T and EDA_TEXT_VJUSTIFY_T
|
2012-01-23 04:33:36 +00:00
|
|
|
#include <trigo.h>
|
|
|
|
#include <macros.h>
|
2018-01-29 10:37:29 +00:00
|
|
|
#include <base_screen.h>
|
2019-05-31 12:15:25 +00:00
|
|
|
#include <gr_text.h>
|
2007-06-05 12:10:51 +00:00
|
|
|
|
2016-03-25 08:26:11 +00:00
|
|
|
#include <basic_gal.h>
|
2016-03-23 07:41:47 +00:00
|
|
|
|
2010-11-12 16:36:43 +00:00
|
|
|
/**
|
|
|
|
* Function GetPensizeForBold
|
2009-05-30 16:06:01 +00:00
|
|
|
* @return the "best" value for a pen size to draw/plot a bold text
|
|
|
|
* @param aTextSize = the char size (height or width)
|
|
|
|
*/
|
|
|
|
int GetPenSizeForBold( int aTextSize )
|
|
|
|
{
|
// 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;
}
2012-04-19 06:55:45 +00:00
|
|
|
return KiROUND( aTextSize / 5.0 );
|
2009-05-30 16:06:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-11-12 16:36:43 +00:00
|
|
|
/**
|
|
|
|
* Function Clamp_Text_PenSize
|
2012-09-07 19:29:44 +00:00
|
|
|
* As a rule, pen width should not be >1/4em, otherwise the character
|
2009-05-30 16:06:01 +00:00
|
|
|
* will be cluttered up in its own fatness
|
|
|
|
* so pen width max is aSize/4 for bold text, and aSize/6 for normal text
|
|
|
|
* The "best" pen width is aSize/5 for bold texts,
|
|
|
|
* so the clamp is consistant with bold option.
|
|
|
|
* @param aPenSize = the pen size to clamp
|
|
|
|
* @param aSize the char size (height or width)
|
|
|
|
* @param aBold = true if text accept bold pen size
|
|
|
|
* @return the max pen size allowed
|
|
|
|
*/
|
|
|
|
int Clamp_Text_PenSize( int aPenSize, int aSize, bool aBold )
|
|
|
|
{
|
2013-04-03 16:16:26 +00:00
|
|
|
int penSize = aPenSize;
|
|
|
|
double scale = aBold ? 4.0 : 6.0;
|
|
|
|
int maxWidth = KiROUND( std::abs( aSize ) / scale );
|
2009-05-30 16:06:01 +00:00
|
|
|
|
|
|
|
if( penSize > maxWidth )
|
|
|
|
penSize = maxWidth;
|
2013-04-03 16:16:26 +00:00
|
|
|
|
2009-05-30 16:06:01 +00:00
|
|
|
return penSize;
|
|
|
|
}
|
|
|
|
|
2010-09-11 16:33:43 +00:00
|
|
|
|
2009-05-30 16:06:01 +00:00
|
|
|
int Clamp_Text_PenSize( int aPenSize, wxSize aSize, bool aBold )
|
|
|
|
{
|
2012-09-22 11:19:37 +00:00
|
|
|
int size = std::min( std::abs( aSize.x ), std::abs( aSize.y ) );
|
2009-05-30 16:06:01 +00:00
|
|
|
|
2011-03-03 19:08:13 +00:00
|
|
|
return Clamp_Text_PenSize( aPenSize, size, aBold );
|
2010-09-11 16:33:43 +00:00
|
|
|
}
|
2009-05-30 16:06:01 +00:00
|
|
|
|
|
|
|
|
2016-03-23 07:41:47 +00:00
|
|
|
int GraphicTextWidth( const wxString& aText, const wxSize& aSize, bool aItalic, bool aBold )
|
2009-05-30 16:06:01 +00:00
|
|
|
{
|
2016-05-02 13:56:12 +00:00
|
|
|
basic_gal.SetFontItalic( aItalic );
|
|
|
|
basic_gal.SetFontBold( aBold );
|
2016-03-23 07:41:47 +00:00
|
|
|
basic_gal.SetGlyphSize( VECTOR2D( aSize ) );
|
2013-04-03 16:16:26 +00:00
|
|
|
|
2016-03-23 07:41:47 +00:00
|
|
|
VECTOR2D tsize = basic_gal.GetTextLineSize( aText );
|
2009-05-28 17:39:40 +00:00
|
|
|
|
2016-03-23 07:41:47 +00:00
|
|
|
return KiROUND( tsize.x );
|
2016-03-20 18:49:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-11-12 16:36:43 +00:00
|
|
|
/**
|
2019-05-31 12:15:25 +00:00
|
|
|
* Function GRText
|
2008-12-14 19:45:05 +00:00
|
|
|
* Draw a graphic text (like module texts)
|
2009-01-30 14:11:36 +00:00
|
|
|
* @param aDC = the current Device Context. NULL if draw within a 3D GL Canvas
|
2008-12-16 19:44:57 +00:00
|
|
|
* @param aPos = text position (according to h_justify, v_justify)
|
2017-02-20 16:57:41 +00:00
|
|
|
* @param aColor (COLOR4D) = text color
|
2008-12-14 19:45:05 +00:00
|
|
|
* @param aText = text to draw
|
|
|
|
* @param aOrient = angle in 0.1 degree
|
|
|
|
* @param aSize = text size (size.x or size.y can be < 0 for mirrored texts)
|
|
|
|
* @param aH_justify = horizontal justification (Left, center, right)
|
|
|
|
* @param aV_justify = vertical justification (bottom, center, top)
|
2009-05-30 16:06:01 +00:00
|
|
|
* @param aWidth = line width (pen width) (use default width if aWidth = 0)
|
2008-12-16 19:44:57 +00:00
|
|
|
* if width < 0 : draw segments in sketch mode, width = abs(width)
|
2009-05-30 16:06:01 +00:00
|
|
|
* Use a value min(aSize.x, aSize.y) / 5 for a bold text
|
2008-12-16 19:44:57 +00:00
|
|
|
* @param aItalic = true to simulate an italic font
|
2009-05-30 16:06:01 +00:00
|
|
|
* @param aBold = true to use a bold font. Useful only with default width value (aWidth = 0)
|
2018-04-06 13:30:52 +00:00
|
|
|
* @param aCallback( int x0, int y0, int xf, int yf, void* aData ) is a function called
|
|
|
|
* (if non null) to draw each segment. used to draw 3D texts or for plotting.
|
|
|
|
* NULL for normal drawings
|
|
|
|
* @param aCallbackData = is the auxiliary parameter aData for the callback function.
|
|
|
|
* can be nullptr if no auxiliary parameter is needed
|
2010-12-28 11:24:42 +00:00
|
|
|
* @param aPlotter = a pointer to a PLOTTER instance, when this function is used to plot
|
|
|
|
* the text. NULL to draw this text.
|
2008-01-19 20:34:10 +00:00
|
|
|
*/
|
2019-05-31 12:15:25 +00:00
|
|
|
void GRText( wxDC* aDC, const wxPoint& aPos, COLOR4D aColor, const wxString& aText,
|
|
|
|
double aOrient, const wxSize& aSize, enum EDA_TEXT_HJUSTIFY_T aH_justify,
|
|
|
|
enum EDA_TEXT_VJUSTIFY_T aV_justify, int aWidth, bool aItalic, bool aBold,
|
|
|
|
void (* aCallback)( int x0, int y0, int xf, int yf, void* aData ),
|
|
|
|
void* aCallbackData, PLOTTER* aPlotter )
|
2007-06-05 12:10:51 +00:00
|
|
|
{
|
2019-05-31 12:15:25 +00:00
|
|
|
bool fill_mode = true;
|
2008-01-19 20:34:10 +00:00
|
|
|
|
2013-04-03 16:16:26 +00:00
|
|
|
if( aWidth == 0 && aBold ) // Use default values if aWidth == 0
|
2012-09-22 11:19:37 +00:00
|
|
|
aWidth = GetPenSizeForBold( std::min( aSize.x, aSize.y ) );
|
2009-05-30 16:06:01 +00:00
|
|
|
|
2008-12-14 19:45:05 +00:00
|
|
|
if( aWidth < 0 )
|
2008-01-19 20:34:10 +00:00
|
|
|
{
|
2008-12-14 19:45:05 +00:00
|
|
|
aWidth = -aWidth;
|
2016-03-23 07:41:47 +00:00
|
|
|
fill_mode = false;
|
2016-03-20 18:49:04 +00:00
|
|
|
}
|
|
|
|
|
2016-03-23 07:41:47 +00:00
|
|
|
basic_gal.SetIsFill( fill_mode );
|
|
|
|
basic_gal.SetLineWidth( aWidth );
|
2016-03-20 18:49:04 +00:00
|
|
|
|
2016-03-23 07:41:47 +00:00
|
|
|
EDA_TEXT dummy;
|
|
|
|
dummy.SetItalic( aItalic );
|
|
|
|
dummy.SetBold( aBold );
|
|
|
|
dummy.SetHorizJustify( aH_justify );
|
|
|
|
dummy.SetVertJustify( aV_justify );
|
2016-03-20 18:49:04 +00:00
|
|
|
|
2016-03-23 07:41:47 +00:00
|
|
|
wxSize size = aSize;
|
|
|
|
dummy.SetMirrored( size.x < 0 );
|
2016-03-20 18:49:04 +00:00
|
|
|
|
2016-03-23 07:41:47 +00:00
|
|
|
if( size.x < 0 )
|
|
|
|
size.x = - size.x;
|
2016-03-20 18:49:04 +00:00
|
|
|
|
2017-01-23 20:30:11 +00:00
|
|
|
dummy.SetTextSize( size );
|
2016-03-20 18:49:04 +00:00
|
|
|
|
2016-03-23 07:41:47 +00:00
|
|
|
basic_gal.SetTextAttributes( &dummy );
|
|
|
|
basic_gal.SetPlotter( aPlotter );
|
2018-04-06 13:30:52 +00:00
|
|
|
basic_gal.SetCallback( aCallback, aCallbackData );
|
2016-03-23 07:41:47 +00:00
|
|
|
basic_gal.m_DC = aDC;
|
|
|
|
basic_gal.m_Color = aColor;
|
2019-05-31 12:15:25 +00:00
|
|
|
basic_gal.SetClipBox( nullptr );
|
2016-04-29 09:37:33 +00:00
|
|
|
|
|
|
|
basic_gal.StrokeText( aText, VECTOR2D( aPos ), aOrient * M_PI/1800 );
|
2007-06-05 12:10:51 +00:00
|
|
|
}
|
|
|
|
|
2017-01-23 20:30:11 +00:00
|
|
|
|
2019-05-31 12:15:25 +00:00
|
|
|
void GRHaloText( wxDC * aDC, const wxPoint &aPos, const COLOR4D aBgColor, COLOR4D aColor1,
|
|
|
|
COLOR4D aColor2, const wxString &aText, double aOrient, const wxSize &aSize,
|
|
|
|
enum EDA_TEXT_HJUSTIFY_T aH_justify, enum EDA_TEXT_VJUSTIFY_T aV_justify,
|
|
|
|
int aWidth, bool aItalic, bool aBold,
|
|
|
|
void (*aCallback)( int x0, int y0, int xf, int yf, void* aData ),
|
|
|
|
void* aCallbackData, PLOTTER * aPlotter )
|
2013-04-09 17:16:53 +00:00
|
|
|
{
|
|
|
|
// Swap color if contrast would be better
|
2017-02-20 16:57:41 +00:00
|
|
|
// TODO: Maybe calculate contrast some way other than brightness
|
|
|
|
if( aBgColor.GetBrightness() > 0.5 )
|
2013-06-29 09:52:22 +00:00
|
|
|
{
|
2017-02-20 16:57:41 +00:00
|
|
|
COLOR4D c = aColor1;
|
2013-04-09 17:16:53 +00:00
|
|
|
aColor1 = aColor2;
|
|
|
|
aColor2 = c;
|
|
|
|
}
|
|
|
|
|
2016-04-19 18:35:47 +00:00
|
|
|
// Draw the background
|
2019-05-31 12:15:25 +00:00
|
|
|
GRText( aDC, aPos, aColor1, aText, aOrient, aSize, aH_justify, aV_justify, aWidth, aItalic,
|
|
|
|
aBold, aCallback, aCallbackData, aPlotter );
|
2013-04-11 17:40:20 +00:00
|
|
|
|
2016-04-19 18:35:47 +00:00
|
|
|
// Draw the text
|
2019-05-31 12:15:25 +00:00
|
|
|
GRText( aDC, aPos, aColor2, aText, aOrient, aSize, aH_justify, aV_justify, aWidth/4, aItalic,
|
|
|
|
aBold, aCallback, aCallbackData, aPlotter );
|
2013-04-09 17:16:53 +00:00
|
|
|
}
|
2007-06-05 12:10:51 +00:00
|
|
|
|
2010-11-12 16:36:43 +00:00
|
|
|
/**
|
2016-09-19 11:01:36 +00:00
|
|
|
* Function PLOTTER::Text
|
2019-05-31 12:15:25 +00:00
|
|
|
* same as GRText, but plot graphic text insteed of draw it
|
2008-12-14 19:45:05 +00:00
|
|
|
* @param aPos = text position (according to aH_justify, aV_justify)
|
2017-02-20 16:57:41 +00:00
|
|
|
* @param aColor (COLOR4D) = text color
|
2008-12-14 19:45:05 +00:00
|
|
|
* @param aText = text to draw
|
|
|
|
* @param aOrient = angle in 0.1 degree
|
|
|
|
* @param aSize = text size (size.x or size.y can be < 0 for mirrored texts)
|
|
|
|
* @param aH_justify = horizontal justification (Left, center, right)
|
|
|
|
* @param aV_justify = vertical justification (bottom, center, top)
|
2008-12-19 20:40:08 +00:00
|
|
|
* @param aWidth = line width (pen width) (default = 0)
|
|
|
|
* if width < 0 : draw segments in sketch mode, width = abs(width)
|
2009-05-30 16:06:01 +00:00
|
|
|
* Use a value min(aSize.x, aSize.y) / 5 for a bold text
|
2008-12-19 20:40:08 +00:00
|
|
|
* @param aItalic = true to simulate an italic font
|
2009-05-30 16:06:01 +00:00
|
|
|
* @param aBold = true to use a bold font Useful only with default width value (aWidth = 0)
|
2013-12-23 16:25:13 +00:00
|
|
|
* @param aMultilineAllowed = true to plot text as multiline, otherwise single line
|
2017-06-15 09:28:36 +00:00
|
|
|
* @param aData = a parameter used by some plotters in SetCurrentLineWidth(),
|
|
|
|
* not directly used here.
|
2008-01-19 20:34:10 +00:00
|
|
|
*/
|
2012-05-03 18:37:56 +00:00
|
|
|
void PLOTTER::Text( const wxPoint& aPos,
|
2017-02-20 16:57:41 +00:00
|
|
|
const COLOR4D aColor,
|
2010-09-11 16:33:43 +00:00
|
|
|
const wxString& aText,
|
2013-05-05 07:17:48 +00:00
|
|
|
double aOrient,
|
2010-09-11 16:33:43 +00:00
|
|
|
const wxSize& aSize,
|
2012-01-03 17:14:17 +00:00
|
|
|
enum EDA_TEXT_HJUSTIFY_T aH_justify,
|
|
|
|
enum EDA_TEXT_VJUSTIFY_T aV_justify,
|
2010-09-11 16:33:43 +00:00
|
|
|
int aWidth,
|
|
|
|
bool aItalic,
|
2013-12-23 16:25:13 +00:00
|
|
|
bool aBold,
|
2016-09-19 11:01:36 +00:00
|
|
|
bool aMultilineAllowed,
|
|
|
|
void* aData )
|
2007-06-05 12:10:51 +00:00
|
|
|
{
|
2012-06-09 09:38:58 +00:00
|
|
|
int textPensize = aWidth;
|
|
|
|
|
2013-04-03 16:16:26 +00:00
|
|
|
if( textPensize == 0 && aBold ) // Use default values if aWidth == 0
|
2012-09-22 11:19:37 +00:00
|
|
|
textPensize = GetPenSizeForBold( std::min( aSize.x, aSize.y ) );
|
2009-05-28 17:39:40 +00:00
|
|
|
|
2012-06-09 09:38:58 +00:00
|
|
|
if( textPensize >= 0 )
|
|
|
|
textPensize = Clamp_Text_PenSize( aWidth, aSize, aBold );
|
2009-06-02 07:26:49 +00:00
|
|
|
else
|
2012-06-09 09:38:58 +00:00
|
|
|
textPensize = -Clamp_Text_PenSize( -aWidth, aSize, aBold );
|
2008-01-19 20:34:10 +00:00
|
|
|
|
2016-09-19 11:01:36 +00:00
|
|
|
SetCurrentLineWidth( textPensize, aData );
|
2008-12-22 21:06:44 +00:00
|
|
|
|
2017-02-20 16:57:41 +00:00
|
|
|
SetColor( aColor );
|
2008-01-19 20:34:10 +00:00
|
|
|
|
2019-05-31 12:15:25 +00:00
|
|
|
GRText( NULL, aPos, aColor, aText, aOrient, aSize, aH_justify, aV_justify, textPensize,
|
|
|
|
aItalic, aBold, nullptr, nullptr, this );
|
2012-06-09 09:38:58 +00:00
|
|
|
|
|
|
|
if( aWidth != textPensize )
|
2016-09-19 11:01:36 +00:00
|
|
|
SetCurrentLineWidth( aWidth, aData );
|
2007-06-05 12:10:51 +00:00
|
|
|
}
|