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>
|
2021-06-07 18:31:53 +00:00
|
|
|
* Copyright (C) 2012 Wayne Stambaugh <stambaughw@gmail.com>
|
2022-01-10 01:53:01 +00:00
|
|
|
* Copyright (C) 1992-2022 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 <gr_basic.h>
|
2021-08-18 20:38:14 +00:00
|
|
|
#include <plotters/plotter.h>
|
2012-01-23 04:33:36 +00:00
|
|
|
#include <trigo.h>
|
2020-10-02 12:29:17 +00:00
|
|
|
#include <math/util.h> // for KiROUND
|
2021-12-29 17:31:40 +00:00
|
|
|
#include <font/font.h>
|
2007-06-05 12:10:51 +00:00
|
|
|
|
2022-01-10 01:53:01 +00:00
|
|
|
#include <callback_gal.h>
|
2016-03-23 07:41:47 +00:00
|
|
|
|
2020-01-09 00:13:02 +00:00
|
|
|
|
2010-11-12 16:36:43 +00:00
|
|
|
/**
|
2021-06-07 18:31:53 +00:00
|
|
|
* @param aTextSize is the char size (height or width).
|
|
|
|
* @return the "best" value for a pen size to draw/plot a bold text.
|
2009-05-30 16:06:01 +00:00
|
|
|
*/
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-12-31 14:07:24 +00:00
|
|
|
int GetPenSizeForBold( const wxSize& aTextSize )
|
|
|
|
{
|
|
|
|
return GetPenSizeForBold( std::min( aTextSize.x, aTextSize.y ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-05-12 14:02:24 +00:00
|
|
|
int GetPenSizeForNormal( int aTextSize )
|
|
|
|
{
|
|
|
|
return KiROUND( aTextSize / 8.0 );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-12-31 14:07:24 +00:00
|
|
|
int GetPenSizeForNormal( const wxSize& aTextSize )
|
|
|
|
{
|
|
|
|
return GetPenSizeForNormal( std::min( aTextSize.x, aTextSize.y ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-11-12 16:36:43 +00:00
|
|
|
/**
|
2019-07-30 01:57:41 +00:00
|
|
|
* Don't allow text to become cluttered up in its own fatness. Bold fonts are generally around
|
|
|
|
* aSize/5 in width, so we limit them to aSize/4, and normal text to aSize/6.
|
2021-06-07 18:31:53 +00:00
|
|
|
*
|
|
|
|
* @param aPenSize is the pen size to clamp.
|
|
|
|
* @param aSize is the character size (height or width).
|
|
|
|
* @param aBold use true if text accept bold pen size.
|
|
|
|
* @return the max pen size allowed.
|
2009-05-30 16:06:01 +00:00
|
|
|
*/
|
|
|
|
int Clamp_Text_PenSize( int aPenSize, int aSize, bool aBold )
|
|
|
|
{
|
2019-07-30 01:57:41 +00:00
|
|
|
double scale = aBold ? 4.0 : 6.0;
|
|
|
|
int maxWidth = KiROUND( (double) aSize / scale );
|
2009-05-30 16:06:01 +00:00
|
|
|
|
2019-07-30 01:57:41 +00:00
|
|
|
return std::min( aPenSize, maxWidth );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
float Clamp_Text_PenSize( float aPenSize, int aSize, bool aBold )
|
|
|
|
{
|
|
|
|
float scale = aBold ? 4.0 : 6.0;
|
|
|
|
float maxWidth = (float) aSize / scale;
|
2013-04-03 16:16:26 +00:00
|
|
|
|
2019-07-30 01:57:41 +00:00
|
|
|
return std::min( aPenSize, maxWidth );
|
2009-05-30 16:06:01 +00:00
|
|
|
}
|
|
|
|
|
2010-09-11 16:33:43 +00:00
|
|
|
|
2021-12-29 19:02:50 +00:00
|
|
|
int Clamp_Text_PenSize( int aPenSize, const VECTOR2I& aSize, bool aBold )
|
2009-05-30 16:06:01 +00:00
|
|
|
{
|
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
|
|
|
|
|
|
|
|
2021-12-31 14:07:24 +00:00
|
|
|
int GraphicTextWidth( const wxString& aText, KIFONT::FONT* aFont, const VECTOR2I& aSize,
|
2022-01-07 14:49:17 +00:00
|
|
|
int aThickness, bool aBold, bool aItalic )
|
2009-05-30 16:06:01 +00:00
|
|
|
{
|
2022-01-06 01:26:18 +00:00
|
|
|
if( !aFont )
|
|
|
|
aFont = KIFONT::FONT::GetFont();
|
|
|
|
|
2022-01-07 14:49:17 +00:00
|
|
|
return KiROUND( aFont->StringBoundaryLimits( aText, aSize, aThickness, aBold, aItalic ).x );
|
2016-03-20 18:49:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-11-12 16:36:43 +00:00
|
|
|
/**
|
2022-01-10 01:53:01 +00:00
|
|
|
* Print a graphic text through wxDC.
|
2021-06-07 18:31:53 +00:00
|
|
|
*
|
2022-01-10 01:53:01 +00:00
|
|
|
* @param aDC is the current Device Context.
|
2021-06-07 18:31:53 +00:00
|
|
|
* @param aPos is the text position (according to h_justify, v_justify).
|
|
|
|
* @param aColor is the text color.
|
|
|
|
* @param aText is the text to draw.
|
2021-12-28 22:13:54 +00:00
|
|
|
* @param aOrient is the angle.
|
2021-06-07 18:31:53 +00:00
|
|
|
* @param aSize is the text size (size.x or size.y can be < 0 for mirrored texts).
|
|
|
|
* @param aH_justify is the horizontal justification (Left, center, right).
|
|
|
|
* @param aV_justify is the vertical justification (bottom, center, top).
|
|
|
|
* @param aWidth is the 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)
|
2021-06-07 18:31:53 +00:00
|
|
|
* Use a value min(aSize.x, aSize.y) / 5 for a bold text.
|
|
|
|
* @param aItalic is the true to simulate an italic font.
|
|
|
|
* @param aBold use true to use a bold font. Useful only with default width value (aWidth = 0).
|
2021-12-29 17:31:40 +00:00
|
|
|
* @param aFont is the font to use, or nullptr for the KiCad stroke font
|
2008-01-19 20:34:10 +00:00
|
|
|
*/
|
2022-01-10 01:53:01 +00:00
|
|
|
void GRPrintText( wxDC* aDC, const VECTOR2I& aPos, const COLOR4D& aColor, const wxString& aText,
|
|
|
|
const EDA_ANGLE& aOrient, const VECTOR2I& aSize,
|
|
|
|
enum GR_TEXT_H_ALIGN_T aH_justify, enum GR_TEXT_V_ALIGN_T aV_justify,
|
|
|
|
int aWidth, bool aItalic, bool aBold, KIFONT::FONT* aFont )
|
2007-06-05 12:10:51 +00:00
|
|
|
{
|
2022-01-10 14:03:53 +00:00
|
|
|
KIGFX::GAL_DISPLAY_OPTIONS empty_opts;
|
|
|
|
bool fill_mode = true;
|
2008-01-19 20:34:10 +00:00
|
|
|
|
2021-12-31 14:07:24 +00:00
|
|
|
if( !aFont )
|
|
|
|
aFont = KIFONT::FONT::GetFont();
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2022-01-10 14:03:53 +00:00
|
|
|
CALLBACK_GAL callback_gal( empty_opts,
|
2022-01-10 01:53:01 +00:00
|
|
|
// Stroke callback
|
|
|
|
[&]( const VECTOR2I& aPt1, const VECTOR2I& aPt2 )
|
|
|
|
{
|
|
|
|
if( fill_mode )
|
2022-01-28 22:51:34 +00:00
|
|
|
GRLine( aDC, aPt1, aPt2, aWidth, aColor );
|
2022-01-10 01:53:01 +00:00
|
|
|
else
|
2022-01-28 22:51:34 +00:00
|
|
|
GRCSegm( aDC, aPt1, aPt2, aWidth, aColor );
|
2022-01-10 01:53:01 +00:00
|
|
|
},
|
2022-01-14 17:17:57 +00:00
|
|
|
// Polygon callback
|
|
|
|
[&]( const SHAPE_LINE_CHAIN& aPoly )
|
2022-01-10 01:53:01 +00:00
|
|
|
{
|
2022-01-28 22:51:34 +00:00
|
|
|
GRClosedPoly( aDC, aPoly.PointCount(), aPoly.CPoints().data(), true, aColor );
|
2022-01-10 01:53:01 +00:00
|
|
|
} );
|
2016-03-20 18:49:04 +00:00
|
|
|
|
2021-12-31 14:07:24 +00:00
|
|
|
TEXT_ATTRIBUTES attributes;
|
|
|
|
attributes.m_Angle = aOrient;
|
|
|
|
attributes.m_StrokeWidth = aWidth;
|
|
|
|
attributes.m_Italic = aItalic;
|
|
|
|
attributes.m_Bold = aBold;
|
|
|
|
attributes.m_Halign = aH_justify;
|
|
|
|
attributes.m_Valign = aV_justify;
|
2022-01-11 17:16:10 +00:00
|
|
|
attributes.m_Size = aSize;
|
2016-03-20 18:49:04 +00:00
|
|
|
|
2022-01-10 14:03:53 +00:00
|
|
|
aFont->Draw( &callback_gal, aText, aPos, attributes );
|
2007-06-05 12:10:51 +00:00
|
|
|
}
|
|
|
|
|
2017-01-23 20:30:11 +00:00
|
|
|
|