Fix 9.0 formatting of fields

Fixes https://gitlab.com/kicad/code/kicad/-/issues/20166
This commit is contained in:
Jon Evans 2025-03-01 08:52:45 -05:00
parent d7ba90c573
commit 41c3607512
5 changed files with 99 additions and 14 deletions

View File

@ -28,8 +28,11 @@
#include <advanced_config.h>
#include <base_units.h>
#include <common.h> // for ExpandTextVars
#include <ctl_flags.h>
#include <sch_edit_frame.h>
#include <plotters/plotter.h>
#include <richio.h>
#include <io/kicad/kicad_io_utils.h>
#include <bitmaps.h>
#include <kiway.h>
#include <symbol_library.h>
@ -507,6 +510,88 @@ void SCH_FIELD::Print( const SCH_RENDER_SETTINGS* aSettings, int aUnit, int aBod
}
// The following is taken from EDA_TEXT::Format because after 9.0.0, the `(hide yes)` property
// was removed from EDA_TEXT but not EDA_FIELD, but to preserve compatibility with 9.0.0, it
// needs to be written inside the effects block.
void SCH_FIELD::Format( OUTPUTFORMATTER* aFormatter, int aControlBits ) const
{
aFormatter->Print( "(effects" );
aFormatter->Print( "(font" );
if( GetFont() && !GetFont()->GetName().IsEmpty() )
aFormatter->Print( "(face %s)", aFormatter->Quotew( GetFont()->NameAsToken() ).c_str() );
// Text size
aFormatter->Print( "(size %s %s)",
EDA_UNIT_UTILS::FormatInternalUnits( m_IuScale, GetTextHeight() ).c_str(),
EDA_UNIT_UTILS::FormatInternalUnits( m_IuScale, GetTextWidth() ).c_str() );
if( GetLineSpacing() != 1.0 )
{
aFormatter->Print( "(line_spacing %s)",
FormatDouble2Str( GetLineSpacing() ).c_str() );
}
if( GetTextThickness() )
{
aFormatter->Print( "(thickness %s)",
EDA_UNIT_UTILS::FormatInternalUnits( m_IuScale, GetTextThickness() ).c_str() );
}
if( IsBold() )
KICAD_FORMAT::FormatBool( aFormatter, "bold", true );
if( IsItalic() )
KICAD_FORMAT::FormatBool( aFormatter, "italic", true );
if( !( aControlBits & CTL_OMIT_COLOR ) && GetTextColor() != COLOR4D::UNSPECIFIED )
{
aFormatter->Print( "(color %d %d %d %s)",
KiROUND( GetTextColor().r * 255.0 ),
KiROUND( GetTextColor().g * 255.0 ),
KiROUND( GetTextColor().b * 255.0 ),
FormatDouble2Str( GetTextColor().a ).c_str() );
}
aFormatter->Print( ")"); // (font
if( IsMirrored() || GetHorizJustify() != GR_TEXT_H_ALIGN_CENTER
|| GetVertJustify() != GR_TEXT_V_ALIGN_CENTER )
{
aFormatter->Print( "(justify");
if( GetHorizJustify() != GR_TEXT_H_ALIGN_CENTER )
aFormatter->Print( GetHorizJustify() == GR_TEXT_H_ALIGN_LEFT ? " left" : " right" );
if( GetVertJustify() != GR_TEXT_V_ALIGN_CENTER )
aFormatter->Print( GetVertJustify() == GR_TEXT_V_ALIGN_TOP ? " top" : " bottom" );
if( IsMirrored() )
aFormatter->Print( " mirror" );
aFormatter->Print( ")" ); // (justify
}
// The relevant difference from EDA_TEXT::Format
if( !IsVisible() )
KICAD_FORMAT::FormatBool( aFormatter, "hide", true );
if( !( aControlBits & CTL_OMIT_HYPERLINK ) && HasHyperlink() )
aFormatter->Print( "(href %s)", aFormatter->Quotew( GetHyperlink() ).c_str() );
aFormatter->Print( ")" ); // (effects
}
bool SCH_FIELD::IsDefaultFormatting() const
{
return ( IsVisible()
&& EDA_TEXT::IsDefaultFormatting()
);
}
void SCH_FIELD::ImportValues( const SCH_FIELD& aSource )
{
SetAttributes( aSource );

View File

@ -316,6 +316,17 @@ public:
double Similarity( const SCH_ITEM& aItem ) const override;
/**
* Output the object to \a aFormatter in s-expression form.
*
* @param aFormatter The #OUTPUTFORMATTER object to write to.
* @param aControlBits The control bit definition for object specific formatting.
* @throw IO_ERROR on write error.
*/
void Format( OUTPUTFORMATTER* aFormatter, int aControlBits ) const override;
bool IsDefaultFormatting() const override;
bool operator==( const SCH_ITEM& aItem ) const override;
bool operator==( const SCH_FIELD& aItem ) const;

View File

@ -911,14 +911,6 @@ void SCH_IO_KICAD_SEXPR::saveField( SCH_FIELD* aField )
aField->GetPosition().y ).c_str(),
EDA_UNIT_UTILS::FormatAngle( aField->GetTextAngle() ).c_str() );
// For 9.0.x versions keep writing the "hide" flag inside "effects".
if( !aField->IsVisible() )
{
m_out->Print( "(effects" );
KICAD_FORMAT::FormatBool( m_out, "hide", true );
m_out->Print( ")" );
}
if( aField->IsNameShown() )
KICAD_FORMAT::FormatBool( m_out, "show_name", true );

View File

@ -427,9 +427,6 @@ void SCH_IO_KICAD_SEXPR_LIB_CACHE::saveField( SCH_FIELD* aField, OUTPUTFORMATTER
if( !aField->CanAutoplace() )
aFormatter.Print( "(do_not_autoplace)" );
if( !aField->IsVisible() )
KICAD_FORMAT::FormatBool( &aFormatter, "hide", true );
aField->Format( &aFormatter, 0 );
aFormatter.Print( ")" );
}

View File

@ -228,7 +228,7 @@ public:
*/
bool Replace( const EDA_SEARCH_DATA& aSearchData );
bool IsDefaultFormatting() const;
virtual bool IsDefaultFormatting() const;
void SetFont( KIFONT::FONT* aFont );
KIFONT::FONT* GetFont() const { return m_attributes.m_Font; }
@ -440,13 +440,13 @@ protected:
*/
wxString m_hyperlink;
std::reference_wrapper<const EDA_IU_SCALE> m_IuScale;
private:
wxString m_text;
wxString m_shown_text; // Cache of unescaped text for efficient access
bool m_shown_text_has_text_var_refs;
std::reference_wrapper<const EDA_IU_SCALE> m_IuScale;
mutable wxString m_render_cache_text;
mutable const KIFONT::FONT* m_render_cache_font;
mutable EDA_ANGLE m_render_cache_angle;