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

(cherry picked from commit 6fbbf981f0)
This commit is contained in:
Jeff Young 2023-10-03 14:35:46 +01:00
parent d5cc334e20
commit 1f870bf76c
4 changed files with 37 additions and 28 deletions

View File

@ -881,7 +881,8 @@ void EDA_TEXT::Format( OUTPUTFORMATTER* aFormatter, int aNestLevel, int aControl
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>();
KIGFX::GAL_DISPLAY_OPTIONS empty_opts;
@ -893,12 +894,12 @@ std::shared_ptr<SHAPE_COMPOUND> EDA_TEXT::GetEffectiveTextShape( bool aTriangula
std::vector<std::unique_ptr<KIFONT::GLYPH>>* cache = nullptr;
if( aUseTextRotation )
if( aBBox.GetWidth() )
{
attrs.m_Angle = GetDrawRotation();
if( font->IsOutline() )
cache = GetRenderCache( font, shownText, VECTOR2I() );
drawPos = aBBox.GetCenter();
attrs.m_Halign = GR_TEXT_H_ALIGN_CENTER;
attrs.m_Valign = GR_TEXT_V_ALIGN_CENTER;
attrs.m_Angle = aAngle;
}
else
{

View File

@ -1735,7 +1735,17 @@ BOX2I SCH_SYMBOL::doGetBoundingBox( bool aIncludePins, bool aIncludeFields ) con
BOX2I SCH_SYMBOL::GetBodyBoundingBox() const
{
return doGetBoundingBox( false, false );
try
{
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();
}
}

View File

@ -1127,6 +1127,7 @@ void EE_SELECTION_TOOL::GuessSelectionCandidates( EE_COLLECTOR& collector, const
}
SCH_LINE* line = dynamic_cast<SCH_LINE*>( item );
SCH_FIELD* field = dynamic_cast<SCH_FIELD*>( item );
EDA_TEXT* text = dynamic_cast<EDA_TEXT*>( item );
SCH_SYMBOL* symbol = dynamic_cast<SCH_SYMBOL*>( item );
@ -1134,36 +1135,32 @@ void EE_SELECTION_TOOL::GuessSelectionCandidates( EE_COLLECTOR& collector, const
{
dist = 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 )
if( static_cast<SCH_SYMBOL*>( field->GetParent() )->GetTransform().y1 )
{
symbol = static_cast<SCH_SYMBOL*>( field->GetParent() );
VECTOR2I relPos = pos - symbol->GetPosition();
relPos = symbol->GetTransform().InverseTransform().TransformCoordinate( relPos );
pos = relPos + symbol->GetPosition();
poss = SEG( pos, pos );
if( orient.IsHorizontal() )
orient = ANGLE_VERTICAL;
else
orient = ANGLE_HORIZONTAL;
}
}
field->GetEffectiveTextShape( false, box, orient )->Collide( poss, INT_MAX / 2,
&dist );
}
else if( text )
{
text->GetEffectiveTextShape( false )->Collide( poss, INT_MAX / 2, &dist );
}
else if( symbol )
{
try
{
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() );
}
bbox = symbol->GetBodyBoundingBox();
SHAPE_RECT rect( bbox.GetPosition(), bbox.GetWidth(), bbox.GetHeight() );

View File

@ -238,7 +238,8 @@ public:
* false to build a list of shape for a not rotated text ("native" shapes).
*/
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.