Cleanup text alignment between version 6 and 7

Version 7 text alignment changed subtly for stroke fonts from version 6.
Additionally, the output has been different between screen and plotting,
leading to offset text in plotted output relative to the text shown on
screen.

This introduces a fudge factor in FONT::getLinePositions to correct the
offset in the plotting output relative to v6.

This also changes the SCH_PAINTER and PCB_PAINTER to correct the
relative offsets between GAL and PLOTTER classes.  The source of these
offsets is atm unclear.

Fixes https://gitlab.com/kicad/code/kicad/issues/14755

(cherry picked from commit 0de24bfd59)
This commit is contained in:
Seth Hillbrand 2023-05-17 13:30:31 -07:00
parent 17c27d1176
commit d5bd1f5aea
6 changed files with 58 additions and 24 deletions

View File

@ -121,6 +121,13 @@ void FONT::getLinePositions( const wxString& aText, const VECTOR2I& aPosition,
VECTOR2I offset( 0, 0 );
offset.y += aAttrs.m_Size.y;
if( IsStroke() )
{
// Fudge factors to match 6.0 positioning
offset.x += aAttrs.m_StrokeWidth / 1.52;
offset.y -= aAttrs.m_StrokeWidth * 0.052;
}
switch( aAttrs.m_Valign )
{
case GR_TEXT_V_ALIGN_TOP: break;
@ -139,7 +146,7 @@ void FONT::getLinePositions( const wxString& aText, const VECTOR2I& aPosition,
{
case GR_TEXT_H_ALIGN_LEFT: break;
case GR_TEXT_H_ALIGN_CENTER: lineOffset.x = -lineSize.x / 2; break;
case GR_TEXT_H_ALIGN_RIGHT: lineOffset.x = -lineSize.x; break;
case GR_TEXT_H_ALIGN_RIGHT: lineOffset.x = -( lineSize.x + offset.x ); break;
}
aPositions.push_back( aPosition + lineOffset );
@ -188,11 +195,10 @@ void FONT::Draw( KIGFX::GAL* aGal, const wxString& aText, const VECTOR2I& aPosit
*/
VECTOR2I drawMarkup( BOX2I* aBoundingBox, std::vector<std::unique_ptr<GLYPH>>* aGlyphs,
const std::unique_ptr<MARKUP::NODE>& aNode, const VECTOR2I& aPosition,
const KIFONT::FONT* aFont, const VECTOR2I& aSize, const VECTOR2I& aOffset,
const EDA_ANGLE& aAngle, bool aMirror, const VECTOR2I& aOrigin,
TEXT_STYLE_FLAGS aTextStyle )
const KIFONT::FONT* aFont, const VECTOR2I& aSize, const EDA_ANGLE& aAngle,
bool aMirror, const VECTOR2I& aOrigin, TEXT_STYLE_FLAGS aTextStyle )
{
VECTOR2I nextPosition = aPosition + aOffset;
VECTOR2I nextPosition = aPosition;
bool drawUnderline = false;
bool drawOverbar = false;
@ -230,7 +236,8 @@ VECTOR2I drawMarkup( BOX2I* aBoundingBox, std::vector<std::unique_ptr<GLYPH>>* a
for( const std::unique_ptr<MARKUP::NODE>& child : aNode->children )
{
nextPosition = drawMarkup( aBoundingBox, aGlyphs, child, nextPosition, aFont, aSize,
VECTOR2I(), aAngle, aMirror, aOrigin, textStyle );
aAngle, aMirror, aOrigin, textStyle );
}
}
@ -284,13 +291,13 @@ VECTOR2I drawMarkup( BOX2I* aBoundingBox, std::vector<std::unique_ptr<GLYPH>>* a
VECTOR2I FONT::drawMarkup( BOX2I* aBoundingBox, std::vector<std::unique_ptr<GLYPH>>* aGlyphs,
const wxString& aText, const VECTOR2I& aPosition, const VECTOR2I& aSize,
const VECTOR2I& aOffset, const EDA_ANGLE& aAngle, bool aMirror,
const VECTOR2I& aOrigin, TEXT_STYLE_FLAGS aTextStyle ) const
const EDA_ANGLE& aAngle, bool aMirror, const VECTOR2I& aOrigin,
TEXT_STYLE_FLAGS aTextStyle ) const
{
MARKUP::MARKUP_PARSER markupParser( TO_UTF8( aText ) );
std::unique_ptr<MARKUP::NODE> root = markupParser.Parse();
return ::drawMarkup( aBoundingBox, aGlyphs, root, aPosition, this, aSize, aOffset, aAngle, aMirror,
return ::drawMarkup( aBoundingBox, aGlyphs, root, aPosition, this, aSize, aAngle, aMirror,
aOrigin, aTextStyle );
}
@ -304,13 +311,8 @@ void FONT::drawSingleLineText( KIGFX::GAL* aGal, BOX2I* aBoundingBox, const wxSt
return;
TEXT_STYLE_FLAGS textStyle = 0;
VECTOR2I offset;
if( !IsOutline() )
{
offset.x = aGal->GetLineWidth() / 1.52;
offset.y = -aGal->GetLineWidth() * 0.052;
}
if( aItalic )
textStyle |= TEXT_STYLE::ITALIC;
@ -319,7 +321,7 @@ void FONT::drawSingleLineText( KIGFX::GAL* aGal, BOX2I* aBoundingBox, const wxSt
std::vector<std::unique_ptr<GLYPH>> glyphs;
(void) drawMarkup( aBoundingBox, &glyphs, aText, aPosition, aSize, offset, aAngle, aMirror, aOrigin,
(void) drawMarkup( aBoundingBox, &glyphs, aText, aPosition, aSize, aAngle, aMirror, aOrigin,
textStyle );
aGal->DrawGlyphs( glyphs );
@ -339,8 +341,8 @@ VECTOR2I FONT::StringBoundaryLimits( const wxString& aText, const VECTOR2I& aSiz
if( aItalic )
textStyle |= TEXT_STYLE::ITALIC;
(void) drawMarkup( &boundingBox, nullptr, aText, VECTOR2I(), aSize, VECTOR2I(), ANGLE_0, false,
VECTOR2I(), textStyle );
(void) drawMarkup( &boundingBox, nullptr, aText, VECTOR2I(), aSize, ANGLE_0, false, VECTOR2I(),
textStyle );
if( IsStroke() )
{
@ -365,8 +367,8 @@ VECTOR2I FONT::boundingBoxSingleLine( BOX2I* aBBox, const wxString& aText,
if( aItalic )
textStyle |= TEXT_STYLE::ITALIC;
VECTOR2I extents = drawMarkup( aBBox, nullptr, aText, aPosition, aSize, VECTOR2I(), ANGLE_0,
false, VECTOR2I(), textStyle );
VECTOR2I extents = drawMarkup( aBBox, nullptr, aText, aPosition, aSize, ANGLE_0, false,
VECTOR2I(), textStyle );
return extents;
}

View File

@ -255,7 +255,7 @@ void OUTLINE_FONT::GetLinesAsGlyphs( std::vector<std::unique_ptr<GLYPH>>* aGlyph
for( size_t i = 0; i < strings.GetCount(); i++ )
{
(void) drawMarkup( nullptr, aGlyphs, strings.Item( i ), positions[i], aAttrs.m_Size, VECTOR2I(),
(void) drawMarkup( nullptr, aGlyphs, strings.Item( i ), positions[i], aAttrs.m_Size,
aAttrs.m_Angle, aAttrs.m_Mirrored, aPosition, textStyle );
}
}

View File

@ -1976,6 +1976,21 @@ void SCH_PAINTER::draw( const SCH_TEXT *aText, int aLayer )
m_gal->SetIsStroke( true );
attrs.m_StrokeWidth += getShadowWidth( !aText->IsSelected() );
attrs.m_Underlined = false;
// Fudge factors to match 6.0 positioning
// New text stroking has width dependent offset but we need to
// center the shadow on the stroke. NB this offset is in font.cpp also
double fudge = getShadowWidth( !aText->IsSelected() ) / 1.52;
if( attrs.m_Halign == GR_TEXT_H_ALIGN_LEFT && attrs.m_Angle == ANGLE_0 )
text_offset.x -= fudge;
else if( attrs.m_Halign == GR_TEXT_H_ALIGN_RIGHT && attrs.m_Angle == ANGLE_90 )
text_offset.y -= fudge;
else if( attrs.m_Halign == GR_TEXT_H_ALIGN_RIGHT && attrs.m_Angle == ANGLE_0 )
text_offset.x += fudge;
else if( attrs.m_Halign == GR_TEXT_H_ALIGN_LEFT && attrs.m_Angle == ANGLE_90 )
text_offset.y += fudge;
strokeText( shownText, aText->GetDrawPos() + text_offset, attrs );
}

View File

@ -133,7 +133,8 @@ SCH_TEXT::SCH_TEXT( const SCH_TEXT& aText ) :
VECTOR2I SCH_TEXT::GetSchematicTextOffset( const RENDER_SETTINGS* aSettings ) const
{
return VECTOR2I( 0, 0 );
// Fudge factor to match KiCad 6
return VECTOR2I( 0, -2500 );
}

View File

@ -245,7 +245,7 @@ protected:
std::vector<VECTOR2I>& aExtents, const TEXT_ATTRIBUTES& aAttrs ) const;
VECTOR2I drawMarkup( BOX2I* aBoundingBox, std::vector<std::unique_ptr<GLYPH>>* aGlyphs,
const wxString& aText, const VECTOR2I& aPosition, const VECTOR2I& aOffset,
const wxString& aText, const VECTOR2I& aPosition,
const VECTOR2I& aSize, const EDA_ANGLE& aAngle, bool aMirror,
const VECTOR2I& aOrigin, TEXT_STYLE_FLAGS aTextStyle ) const;

View File

@ -1884,7 +1884,23 @@ void PCB_PAINTER::strokeText( const wxString& aText, const VECTOR2I& aPosition,
m_gal->SetIsFill( font->IsOutline() );
m_gal->SetIsStroke( font->IsStroke() );
font->Draw( m_gal, aText, aPosition, aAttrs );
VECTOR2I pos( aPosition );
VECTOR2I fudge
{ KiROUND( 0.16 * aAttrs.m_StrokeWidth ), 0 };
RotatePoint( fudge, aAttrs.m_Angle );
if( ( aAttrs.m_Halign == GR_TEXT_H_ALIGN_LEFT && !aAttrs.m_Mirrored )
|| ( aAttrs.m_Halign == GR_TEXT_H_ALIGN_RIGHT && aAttrs.m_Mirrored ) )
{
pos -= fudge;
}
else if( ( aAttrs.m_Halign == GR_TEXT_H_ALIGN_RIGHT && !aAttrs.m_Mirrored )
|| ( aAttrs.m_Halign == GR_TEXT_H_ALIGN_LEFT && aAttrs.m_Mirrored ) )
{
pos += fudge;
}
font->Draw( m_gal, aText, pos, aAttrs );
}