Gerbview: refinements in D_Code display.

Remove duplicate code between legacy and Gal canvas to calculate D_Code id draw parameters.
This commit is contained in:
jean-pierre charras 2017-11-13 11:30:23 +01:00
parent e14a1656db
commit f7c0562c8b
5 changed files with 94 additions and 64 deletions

View File

@ -370,8 +370,6 @@ void GBR_LAYOUT::Draw( EDA_DRAW_PANEL* aPanel, wxDC* aDC, GR_DRAWMODE aDrawMode,
void GBR_LAYOUT::DrawItemsDCodeID( EDA_DRAW_PANEL* aPanel, wxDC* aDC,
GR_DRAWMODE aDrawMode, COLOR4D aDrawColor )
{
wxPoint pos;
int width;
wxString Line;
GRSetDrawMode( aDC, aDrawMode );
@ -388,57 +386,24 @@ void GBR_LAYOUT::DrawItemsDCodeID( EDA_DRAW_PANEL* aPanel, wxDC* aDC,
for( GERBER_DRAW_ITEM* item = gerber->GetItemsList(); item != NULL; item = item->Next() )
{
wxPoint pos;
int size;
double orient;
if( item->m_DCode <= 0 )
if( ! item->GetTextD_CodePrms( size, pos, orient ) )
continue;
if( item->m_Flashed || item->m_Shape == GBR_ARC )
{
pos = item->m_Start;
}
else
{
pos.x = (item->m_Start.x + item->m_End.x) / 2;
pos.y = (item->m_Start.y + item->m_End.y) / 2;
}
pos = item->GetABPosition( pos );
Line.Printf( wxT( "D%d" ), item->m_DCode );
if( item->GetDcodeDescr() )
width = item->GetDcodeDescr()->GetShapeDim( item );
else
width = std::min( item->m_Size.x, item->m_Size.y );
double orient = TEXT_ANGLE_HORIZ;
if( item->m_Flashed )
{
// A reasonable size for text is width/3 because most of time this text has 3 chars.
width /= 3;
}
else // this item is a line
{
wxPoint delta = item->m_Start - item->m_End;
if( abs( delta.x ) < abs( delta.y ) )
orient = TEXT_ANGLE_VERT;
// A reasonable size for text is width/2 because text needs margin below and above it.
// a margin = width/4 seems good
width /= 2;
}
// Avoid to draw text, if it is too small (size in pixel < 5 pixels)
// to be readable:
int size_pixel = aDC->LogicalToDeviceXRel( width );
int size_pixel = aDC->LogicalToDeviceXRel( size );
const int threshold = 5;
if( size_pixel >= threshold )
{
DrawGraphicText( aPanel->GetClipBox(), aDC, pos, aDrawColor, Line,
orient, wxSize( width, width ),
orient, wxSize( size, size ),
GR_TEXT_HJUSTIFY_CENTER, GR_TEXT_VJUSTIFY_CENTER,
0, false, false );
}

View File

@ -85,6 +85,72 @@ int GERBER_DRAW_ITEM::GetLayer() const
}
bool GERBER_DRAW_ITEM::GetTextD_CodePrms( int& aSize, wxPoint& aPos, double& aOrientation )
{
// calculate the best size and orientation of the D_Code text
if( m_DCode <= 0 )
return false; // No D_Code for this item
if( m_Flashed || m_Shape == GBR_ARC )
{
aPos = m_Start;
}
else // it is a line:
{
aPos = ( m_Start + m_End) / 2;
}
aPos = GetABPosition( aPos );
int size; // the best size for the text
if( GetDcodeDescr() )
size = GetDcodeDescr()->GetShapeDim( this );
else
size = std::min( m_Size.x, m_Size.y );
aOrientation = TEXT_ANGLE_HORIZ;
if( m_Flashed )
{
// A reasonable size for text is min_dim/3 because most of time this text has 3 chars.
aSize = size / 3;
}
else // this item is a line
{
wxPoint delta = m_Start - m_End;
aOrientation = RAD2DECIDEG( atan2( (double)delta.y, (double)delta.x ) );
NORMALIZE_ANGLE_90( aOrientation );
// A reasonable size for text is size/2 because text needs margin below and above it.
// a margin = size/4 seems good, expecting the line len is large enough to show 3 chars,
// that is the case most of time.
aSize = size / 2;
}
return true;
}
bool GERBER_DRAW_ITEM::GetTextD_CodePrms( double& aSize, VECTOR2D& aPos, double& aOrientation )
{
// aOrientation is returned in radians
int size;
wxPoint pos;
if( ! GetTextD_CodePrms( size, pos, aOrientation ) )
return false;
aPos = pos;
aSize = (double) size;
aOrientation = DECIDEG2RAD( aOrientation );
return true;
}
wxPoint GERBER_DRAW_ITEM::GetABPosition( const wxPoint& aXYPosition ) const
{
/* Note: RS274Xrevd_e is obscure about the order of transforms:
@ -807,7 +873,7 @@ unsigned int GERBER_DRAW_ITEM::ViewGetLOD( int aLayer, KIGFX::VIEW* aView ) cons
// the level of details is chosen experimentally, to show
// only a readable text:
const int level = Millimeter2iu( 1000 );
const int level = Millimeter2iu( 500 );
return ( level / ( size + 1 ) );
}

View File

@ -135,6 +135,21 @@ public:
return m_LayerNegative;
}
/**
* Returns the best size and orientation to display the D_Code on screen
* @param aSize is a reference to return the text size
* @param aPos is a reference to return the text position
* @param aOrientation is a reference to return the text orientation
* @return true if the parameters can be calculated, false for unknown D_Code
*/
bool GetTextD_CodePrms( int& aSize, wxPoint& aPos, double& aOrientation );
/**
* Returns the best size and orientation to display the D_Code in GAL
* aOrientation is returned in radians
*/
bool GetTextD_CodePrms( double& aSize, VECTOR2D& aPos, double& aOrientation );
/**
* Function HasNegativeItems
* @return true if this item or at least one shape (when using aperture macros

View File

@ -189,29 +189,13 @@ void GERBVIEW_PAINTER::draw( /*const*/ GERBER_DRAW_ITEM* aItem, int aLayer )
wxString codeText;
VECTOR2D textPosition;
double textSize;
double orient;
if( aItem->GetDcodeDescr() )
textSize = aItem->GetDcodeDescr()->GetShapeDim( aItem ) / 3.0;
else
textSize = std::min( aItem->m_Size.x, aItem->m_Size.y ) / 2.0;
if( aItem->m_Shape == GBR_ARC )
{
textPosition = start;
}
else if( aItem->m_Flashed )
{
BOX2I bb = aItem->ViewBBox();
textPosition = bb.Centre();
}
else
{
textPosition.x = ( start.x + end.x ) / 2;
textPosition.y = ( start.y + end.y ) / 2;
}
if( !aItem->GetTextD_CodePrms( textSize, textPosition, orient ) )
return;
color = m_gerbviewSettings.GetColor( aItem, aLayer );
codeText.Printf( wxT( "D%d" ), aItem->m_DCode );
codeText.Printf( "D%d", aItem->m_DCode );
m_gal->SetIsStroke( true );
m_gal->SetIsFill( false );
@ -224,7 +208,7 @@ void GERBVIEW_PAINTER::draw( /*const*/ GERBER_DRAW_ITEM* aItem, int aLayer )
m_gal->SetGlyphSize( VECTOR2D( textSize, textSize) );
m_gal->SetHorizontalJustify( GR_TEXT_HJUSTIFY_CENTER );
m_gal->SetVerticalJustify( GR_TEXT_VJUSTIFY_CENTER );
m_gal->BitmapText( codeText, textPosition, 0 );
m_gal->BitmapText( codeText, textPosition, orient );
return;
}

View File

@ -72,7 +72,7 @@ public:
*
* @param aText is the text to be drawn.
* @param aPosition is the text position in world coordinates.
* @param aRotationAngle is the text rotation angle.
* @param aRotationAngle is the text rotation angle in radians.
*/
void Draw( const UTF8& aText, const VECTOR2D& aPosition, double aRotationAngle );