Handle justification when hit-testing fields.

This steals the algo used in SCH_PAINTER to get the right coords
for the text.

Fixes https://gitlab.com/kicad/code/kicad/-/issues/15722
This commit is contained in:
Jeff Young 2023-10-03 14:35:46 +01:00
parent 89f444c38b
commit 6fbbf981f0
4 changed files with 37 additions and 28 deletions

View File

@ -906,7 +906,8 @@ void EDA_TEXT::Format( OUTPUTFORMATTER* aFormatter, int aNestLevel, int aControl
std::shared_ptr<SHAPE_COMPOUND> EDA_TEXT::GetEffectiveTextShape( bool aTriangulate, std::shared_ptr<SHAPE_COMPOUND> EDA_TEXT::GetEffectiveTextShape( bool aTriangulate,
bool aUseTextRotation ) const const BOX2I& aBBox,
const EDA_ANGLE& aAngle ) const
{ {
std::shared_ptr<SHAPE_COMPOUND> shape = std::make_shared<SHAPE_COMPOUND>(); std::shared_ptr<SHAPE_COMPOUND> shape = std::make_shared<SHAPE_COMPOUND>();
KIGFX::GAL_DISPLAY_OPTIONS empty_opts; KIGFX::GAL_DISPLAY_OPTIONS empty_opts;
@ -918,12 +919,12 @@ std::shared_ptr<SHAPE_COMPOUND> EDA_TEXT::GetEffectiveTextShape( bool aTriangula
std::vector<std::unique_ptr<KIFONT::GLYPH>>* cache = nullptr; std::vector<std::unique_ptr<KIFONT::GLYPH>>* cache = nullptr;
if( aUseTextRotation ) if( aBBox.GetWidth() )
{ {
attrs.m_Angle = GetDrawRotation(); drawPos = aBBox.GetCenter();
attrs.m_Halign = GR_TEXT_H_ALIGN_CENTER;
if( font->IsOutline() ) attrs.m_Valign = GR_TEXT_V_ALIGN_CENTER;
cache = GetRenderCache( font, shownText, VECTOR2I() ); attrs.m_Angle = aAngle;
} }
else else
{ {

View File

@ -1768,9 +1768,19 @@ BOX2I SCH_SYMBOL::doGetBoundingBox( bool aIncludePins, bool aIncludeFields ) con
BOX2I SCH_SYMBOL::GetBodyBoundingBox() const BOX2I SCH_SYMBOL::GetBodyBoundingBox() const
{
try
{ {
return doGetBoundingBox( false, false ); return doGetBoundingBox( false, false );
} }
catch( const boost::bad_pointer& exc )
{
// This may be overkill and could be an assertion but we are more likely to
// find any boost pointer container errors this way.
wxLogError( wxT( "Boost bad pointer exception '%s' occurred." ), exc.what() );
return BOX2I();
}
}
BOX2I SCH_SYMBOL::GetBodyAndPinsBoundingBox() const BOX2I SCH_SYMBOL::GetBodyAndPinsBoundingBox() const

View File

@ -1354,6 +1354,7 @@ void EE_SELECTION_TOOL::GuessSelectionCandidates( EE_COLLECTOR& collector, const
} }
SCH_LINE* line = dynamic_cast<SCH_LINE*>( item ); SCH_LINE* line = dynamic_cast<SCH_LINE*>( item );
SCH_FIELD* field = dynamic_cast<SCH_FIELD*>( item );
EDA_TEXT* text = dynamic_cast<EDA_TEXT*>( item ); EDA_TEXT* text = dynamic_cast<EDA_TEXT*>( item );
SCH_SYMBOL* symbol = dynamic_cast<SCH_SYMBOL*>( item ); SCH_SYMBOL* symbol = dynamic_cast<SCH_SYMBOL*>( item );
@ -1361,36 +1362,32 @@ void EE_SELECTION_TOOL::GuessSelectionCandidates( EE_COLLECTOR& collector, const
{ {
dist = KiROUND( DistanceLinePoint( line->GetStartPoint(), line->GetEndPoint(), pos ) ); dist = KiROUND( DistanceLinePoint( line->GetStartPoint(), line->GetEndPoint(), pos ) );
} }
else if( text ) else if( field )
{
if( SCH_FIELD* field = dynamic_cast<SCH_FIELD*>( text ) )
{ {
BOX2I box = field->GetBoundingBox();
EDA_ANGLE orient = field->GetTextAngle();
if( field->GetParent() && field->GetParent()->Type() == SCH_SYMBOL_T ) if( field->GetParent() && field->GetParent()->Type() == SCH_SYMBOL_T )
{ {
symbol = static_cast<SCH_SYMBOL*>( field->GetParent() ); if( static_cast<SCH_SYMBOL*>( field->GetParent() )->GetTransform().y1 )
{
VECTOR2I relPos = pos - symbol->GetPosition(); if( orient.IsHorizontal() )
relPos = symbol->GetTransform().InverseTransform().TransformCoordinate( relPos ); orient = ANGLE_VERTICAL;
pos = relPos + symbol->GetPosition(); else
orient = ANGLE_HORIZONTAL;
poss = SEG( pos, pos );
} }
} }
field->GetEffectiveTextShape( false, box, orient )->Collide( poss, INT_MAX / 2,
&dist );
}
else if( text )
{
text->GetEffectiveTextShape( false )->Collide( poss, INT_MAX / 2, &dist ); text->GetEffectiveTextShape( false )->Collide( poss, INT_MAX / 2, &dist );
} }
else if( symbol ) else if( symbol )
{
try
{ {
bbox = symbol->GetBodyBoundingBox(); bbox = symbol->GetBodyBoundingBox();
}
catch( const boost::bad_pointer& exc )
{
// This may be overkill and could be an assertion but we are more likely to
// find any boost pointer container errors this way.
wxLogError( wxT( "Boost bad pointer exception '%s' occurred." ), exc.what() );
}
SHAPE_RECT rect( bbox.GetPosition(), bbox.GetWidth(), bbox.GetHeight() ); SHAPE_RECT rect( bbox.GetPosition(), bbox.GetWidth(), bbox.GetHeight() );

View File

@ -246,7 +246,8 @@ public:
* false to build a list of shape for a not rotated text ("native" shapes). * false to build a list of shape for a not rotated text ("native" shapes).
*/ */
std::shared_ptr<SHAPE_COMPOUND> GetEffectiveTextShape( bool aTriangulate = true, std::shared_ptr<SHAPE_COMPOUND> GetEffectiveTextShape( bool aTriangulate = true,
bool aUseTextRotation = true ) const; const BOX2I& aBBox = BOX2I(),
const EDA_ANGLE& aAngle = ANGLE_0 ) const;
/** /**
* Test if \a aPoint is within the bounds of this object. * Test if \a aPoint is within the bounds of this object.