KiCad PCB EDA Suite
Loading...
Searching...
No Matches
pin_layout_cache.cpp
Go to the documentation of this file.
1/*
2 * This program source code file is part of KiCad, a free EDA CAD application.
3 *
4 * Copyright The KiCad Developers
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 3
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <https://www.gnu.org/licenses/>.
18 */
19
20#include "pin_layout_cache.h"
21#include <font/font.h>
22#include <gr_text.h>
24#include <pgm_base.h>
25#include <schematic.h>
27#include <sch_symbol.h>
28#include <eeschema_settings.h>
29#include <schematic_settings.h>
30#include <string_utils.h>
32
33// Small margin in internal units between the pin text and the pin line
34static const int PIN_TEXT_MARGIN = 4;
35
36std::optional<PIN_LAYOUT_CACHE::TEXT_INFO> PIN_LAYOUT_CACHE::GetPinNumberInfo( int aShadowWidth )
37{
39
40 if( m_pin.GetShownNumber().IsEmpty() || !m_pin.GetParentSymbol()->GetShowPinNumbers() )
41 return std::nullopt;
42
43 // Draw the string recomputeCaches() measured, so the box always matches the text.
44 const wxString& formatted = m_numExtentsCache.m_Text;
45 const int num_size = m_pin.GetNumberTextSize();
46
47 std::optional<TEXT_INFO> info = TEXT_INFO();
48 info->m_Text = formatted;
49 info->m_TextSize = num_size;
50 info->m_Thickness = m_numberThickness;
51 info->m_HAlign = GR_TEXT_H_ALIGN_CENTER;
52 info->m_VAlign = GR_TEXT_V_ALIGN_CENTER;
53
54 PIN_ORIENTATION orient = m_pin.PinDrawOrient( DefaultTransform );
55
56 const SYMBOL* parentSym = m_pin.GetParentSymbol();
57
58 VECTOR2I pinPos = m_pin.GetPosition();
59 const int halfLength = m_pin.GetLength() / 2;
60 bool verticalOrient = ( orient == PIN_ORIENTATION::PIN_UP || orient == PIN_ORIENTATION::PIN_DOWN );
61
62 const int perpendicularOffset = getNumberBlockOffset();
63
64 // Keep a multi-line stacked block on the authored side of a mirrored symbol (issue 24894)
65 const bool flipStacked = formatted.Contains( '\n' )
66 && ( m_pin.GetFlipStackedTextSide() != m_pin.StackedTextSideFlipped( DefaultTransform ) );
67
68 if( verticalOrient )
69 {
70 // Vertical pins: text is rotated vertical so that it reads bottom->top.
71
72 // Check if both name and number are displayed
73 bool showBothNameAndNumber = isNameShown()
74 && parentSym->GetPinNameOffset() == 0; // name is outside
75
76 int centerX;
77
78 if( showBothNameAndNumber )
79 {
80 // When both are shown: name goes to the left, number goes to the right
81 centerX = pinPos.x + perpendicularOffset;
82 }
83 else
84 {
85 // When only number is shown: place it to the left of the pin (right when mirrored)
86 centerX = flipStacked ? pinPos.x + perpendicularOffset : pinPos.x - perpendicularOffset;
87 }
88
89 info->m_TextPosition.x = centerX;
90
91 if( orient == PIN_ORIENTATION::PIN_DOWN )
92 info->m_TextPosition.y = pinPos.y + halfLength;
93 else
94 info->m_TextPosition.y = pinPos.y - halfLength;
95
96 info->m_Angle = ANGLE_VERTICAL;
97 }
98 else
99 {
100 // Horizontal pins: "above" means negative Y direction.
101
102 // Check if both name and number are displayed
103 bool showBothNameAndNumber = isNameShown()
104 && parentSym->GetPinNameOffset() == 0; // name is outside
105
106 int centerY;
107
108 if( showBothNameAndNumber )
109 {
110 // When both are shown: name goes above, number goes below (top-aligned)
111 centerY = pinPos.y + perpendicularOffset;
112 }
113 else
114 {
115 // When only number is shown: place it above the pin (below when mirrored)
116 centerY = flipStacked ? pinPos.y + perpendicularOffset : pinPos.y - perpendicularOffset;
117 }
118
119 if( orient == PIN_ORIENTATION::PIN_LEFT )
120 info->m_TextPosition.x = pinPos.x - halfLength; // centered horizontally along pin
121 else
122 info->m_TextPosition.x = pinPos.x + halfLength; // centered horizontally along pin
123
124 info->m_TextPosition.y = centerY;
125 info->m_Angle = ANGLE_HORIZONTAL;
126 }
127
128 return info;
129}
130
131
132static int externalPinDecoSize( const SCHEMATIC_SETTINGS* aSettings, const SCH_PIN& aPin )
133{
134 if( aSettings && aSettings->m_PinSymbolSize )
135 return aSettings->m_PinSymbolSize;
136 return aPin.GetNumberTextSize() / 2;
137}
138
139
140static int internalPinDecoSize( const SCHEMATIC_SETTINGS* aSettings, const SCH_PIN& aPin )
141{
142 if( aSettings && aSettings->m_PinSymbolSize > 0 )
143 return aSettings->m_PinSymbolSize;
144 return aPin.GetNameTextSize() != 0 ? aPin.GetNameTextSize() / 2 : aPin.GetNumberTextSize() / 2;
145}
146
147
149 m_pin( aPin ), m_schSettings( nullptr ), m_dirtyFlags( DIRTY_FLAGS::ALL )
150{
151 // Resolve the schematic (can be null, e.g. in previews)
152 const SCHEMATIC* schematic = aPin.Schematic();
153
154 if( schematic )
155 m_schSettings = &schematic->Settings();
156}
157
158
159void PIN_LAYOUT_CACHE::MarkDirty( int aDirtyFlags )
160{
161 m_dirtyFlags |= aDirtyFlags;
162}
163
164
165void PIN_LAYOUT_CACHE::SetRenderParameters( int aNameThickness, int aNumberThickness,
166 bool aShowElectricalType, bool aShowAltIcons )
167{
168 if( aNameThickness != m_nameThickness )
169 {
171 m_nameThickness = aNameThickness;
172 }
173
174 if( aNumberThickness != m_numberThickness )
175 {
177 m_numberThickness = aNumberThickness;
178 }
179
180 if( aShowElectricalType != m_showElectricalType )
181 {
183 m_showElectricalType = aShowElectricalType;
184 }
185
186 // Not (yet?) cached
187 m_showAltIcons = aShowAltIcons;
188}
189
190
191void PIN_LAYOUT_CACHE::recomputeExtentsCache( bool aDefinitelyDirty, KIFONT::FONT* aFont, int aSize,
192 const wxString& aText,
193 const KIFONT::METRICS& aFontMetrics,
194 TEXT_EXTENTS_CACHE& aCache )
195{
196 // Even if not definitely dirty, verify no font changes. The text is part of the key because
197 // the stacked-pin layout also depends on the pin length, which has no dirty flag of its own.
198 if( !aDefinitelyDirty && aCache.m_Font == aFont && aCache.m_FontSize == aSize
199 && aCache.m_Text == aText )
200 {
201 return;
202 }
203
204 aCache.m_Font = aFont;
205 aCache.m_FontSize = aSize;
206 aCache.m_Text = aText;
207
208 VECTOR2D fontSize( aSize, aSize );
209 int penWidth = GetPenSizeForNormal( aSize );
210
211 // Handle multi-line text bounds properly
212 if( aText.StartsWith( "[" ) && aText.EndsWith( "]" ) && aText.Contains( "\n" ) )
213 {
214 // Extract content between braces and split into lines
215 wxString content = aText.Mid( 1, aText.Length() - 2 );
216 wxArrayString lines;
217 wxStringSplit( content, lines, '\n' );
218
219 if( lines.size() > 1 )
220 {
221 int lineSpacing = KiROUND( aSize * 1.3 ); // Same as drawMultiLineText
222 int maxWidth = 0;
223
224 // Find the widest line
225 for( const wxString& line : lines )
226 {
227 wxString trimmedLine = line;
228 trimmedLine.Trim( true ).Trim( false );
229 VECTOR2I lineExtents = aFont->StringBoundaryLimits( trimmedLine, fontSize, penWidth, false, false, aFontMetrics );
230 maxWidth = std::max( maxWidth, lineExtents.x );
231 }
232
233 // Calculate total dimensions - width is max line width, height accounts for all lines
234 int totalHeight = aSize + ( lines.size() - 1 ) * lineSpacing;
235
236 // Add space for braces. drawBracesAroundText() anchors each brace a braceWidth
237 // clear of the text and drawBrace() curls a further braceWidth outwards.
238 int braceWidth = aSize / 3;
239 maxWidth += braceWidth * 4;
240 totalHeight += aSize / 3; // Extra height for brace extensions
241
242 aCache.m_Extents = VECTOR2I( maxWidth, totalHeight );
243 return;
244 }
245 }
246
247 // Single line text (normal case)
248 aCache.m_Extents = aFont->StringBoundaryLimits( aText, fontSize, penWidth, false, false, aFontMetrics );
249}
250
251
253{
255 KIFONT::FONT* font = KIFONT::FONT::GetFont( cfg ? cfg->m_Appearance.default_font : wxString( "" ) );
256 const KIFONT::METRICS& metrics = m_pin.GetFontMetrics();
257
258 // Due to the fact a shadow text in position INSIDE or OUTSIDE is drawn left or right aligned,
259 // it needs an offset = shadowWidth/2 to be drawn at the same place as normal text
260 // texts drawn as GR_TEXT_H_ALIGN_CENTER do not need a specific offset.
261 // this offset is shadowWidth/2 but for some reason we need to slightly modify this offset
262 // for a better look (better alignment of shadow shape), for KiCad font only
263 if( !font->IsOutline() )
264 m_shadowOffsetAdjust = 1.2f; // Value chosen after tests
265 else
267
268 {
269 const int numSize = m_pin.GetNumberTextSize();
270 const int length = m_pin.GetLength();
271 const wxString& number = m_pin.GetShownNumber();
272
273 // Formatting a stacked number measures it, so only redo it when one of its inputs moves.
274 // The pin length is one of them and has no dirty flag of its own.
275 if( isDirty( DIRTY_FLAGS::NUMBER ) || m_numSource != number || m_numSourceLength != length
276 || m_numExtentsCache.m_Font != font || m_numExtentsCache.m_FontSize != numSize )
277 {
278 m_numSource = number;
279 m_numSourceLength = length;
280
281 recomputeExtentsCache( true, font, numSize,
282 FormatStackedPinForDisplay( number, length, numSize, font, metrics ),
283 metrics, m_numExtentsCache );
284 }
285 }
286
287 {
288 const bool dirty = isDirty( DIRTY_FLAGS::NAME );
289 const wxString name = m_pin.GetShownName();
290 recomputeExtentsCache( dirty, font, m_pin.GetNameTextSize(), name, metrics, m_nameExtentsCache );
291 }
292
293 {
294 double fontSize = std::max( m_pin.GetNameTextSize() * 3 / 4, schIUScale.mmToIU( 0.7 ) );
296 m_pin.GetElectricalTypeName(), metrics, m_typeExtentsCache );
297 }
298
300}
301
302
304{
305 // Now, calculate boundary box corners position for the actual pin orientation
306 switch( m_pin.PinDrawOrient( DefaultTransform ) )
307 {
309 {
310 // Pin is rotated and texts positions are mirrored
311 VECTOR2I c1{ aBox.GetLeft(), aBox.GetTop() };
312 VECTOR2I c2{ aBox.GetRight(), aBox.GetBottom() };
313
314 RotatePoint( c1, VECTOR2I( 0, 0 ), ANGLE_90 );
315 RotatePoint( c2, VECTOR2I( 0, 0 ), ANGLE_90 );
316
317 aBox = BOX2I::ByCorners( c1, c2 );
318 break;
319 }
321 {
322 VECTOR2I c1{ aBox.GetLeft(), aBox.GetTop() };
323 VECTOR2I c2{ aBox.GetRight(), aBox.GetBottom() };
324
325 RotatePoint( c1, VECTOR2I( 0, 0 ), -ANGLE_90 );
326 RotatePoint( c2, VECTOR2I( 0, 0 ), -ANGLE_90 );
327
328 c1.x = -c1.x;
329 c2.x = -c2.x;
330
331 aBox = BOX2I::ByCorners( c1, c2 );
332 break;
333 }
335 // Flip it around
336 aBox.Move( { -aBox.GetCenter().x * 2, 0 } );
337 break;
338
339 default:
341 // Already in this form
342 break;
343 }
344
345 aBox.Move( m_pin.GetPosition() );
346}
347
348
350{
351 // Local nominal position for a PIN_RIGHT orientation.
352 const VECTOR2I baseLocal = aInfo.m_TextPosition;
353
354 // We apply a rotation/mirroring depending on the pin orientation so that the text anchor
355 // maintains a constant perpendicular offset from the pin origin regardless of rotation.
356 VECTOR2I rotated = baseLocal;
357 EDA_ANGLE finalAngle = aInfo.m_Angle;
358
359 switch( m_pin.PinDrawOrient( DefaultTransform ) )
360 {
361 case PIN_ORIENTATION::PIN_RIGHT: // identity
362 break;
364 rotated.x = -rotated.x;
365 rotated.y = -rotated.y;
366 aInfo.m_HAlign = GetFlippedAlignment( aInfo.m_HAlign );
367 break;
368 case PIN_ORIENTATION::PIN_UP: // rotate +90 (x,y)->(y,-x) and vertical text
369 rotated = { baseLocal.y, -baseLocal.x };
370 finalAngle = ANGLE_VERTICAL;
371 break;
372 case PIN_ORIENTATION::PIN_DOWN: // rotate -90 (x,y)->(-y,x) and vertical text, flip h-align
373 rotated = { -baseLocal.y, baseLocal.x };
374 finalAngle = ANGLE_VERTICAL;
375 aInfo.m_HAlign = GetFlippedAlignment( aInfo.m_HAlign );
376 break;
377 default:
378 break;
379 }
380
381 aInfo.m_TextPosition = rotated + m_pin.GetPosition();
382 aInfo.m_Angle = finalAngle;
383}
384
385
386BOX2I PIN_LAYOUT_CACHE::GetPinBoundingBox( bool aIncludeLabelsOnInvisiblePins,
387 bool aIncludeNameAndNumber, bool aIncludeElectricalType )
388{
389 if( const SCH_SYMBOL* symbol = dynamic_cast<const SCH_SYMBOL*>( m_pin.GetParentSymbol() ) )
390 {
391 SCH_PIN* const libPin = m_pin.GetLibPin();
392 wxCHECK( libPin, BOX2I() );
393
394 BOX2I r = libPin->GetBoundingBox( aIncludeLabelsOnInvisiblePins, aIncludeNameAndNumber,
395 aIncludeElectricalType );
396
397 r = symbol->GetTransform().TransformCoordinate( r );
398 r.Offset( symbol->GetPosition() );
399 r.Normalize();
400
401 return r;
402 }
403
404 bool includeName = aIncludeNameAndNumber && !m_pin.GetShownName().IsEmpty();
405 bool includeNumber = aIncludeNameAndNumber && !m_pin.GetShownNumber().IsEmpty();
406 bool includeType = aIncludeElectricalType;
407
408 if( !aIncludeLabelsOnInvisiblePins && !m_pin.IsVisible() )
409 {
410 includeName = false;
411 includeNumber = false;
412 includeType = false;
413 }
414
415 if( const SYMBOL* parentSymbol = m_pin.GetParentSymbol() )
416 {
417 if( !parentSymbol->GetShowPinNames() )
418 includeName = false;
419
420 if( !parentSymbol->GetShowPinNumbers() )
421 includeNumber = false;
422 }
423
425
426 const int pinLength = m_pin.GetLength();
427
428 // Creating and merging all the boxes is pretty quick, if cached we'd have
429 // to track many variables here, which is possible, but unlikely to be worth it.
430 BOX2I bbox;
431
432 // Untransformed pin box
433 {
434 BOX2I pinBox = BOX2I::ByCorners( { 0, 0 }, { pinLength, 0 } );
435 pinBox.Inflate( m_pin.GetPenWidth() / 2 );
436 bbox.Merge( pinBox );
437 }
438
440 {
441 bbox.Merge( *decoBox );
442 }
443
444 if( includeName )
445 {
446 if( OPT_BOX2I nameBox = getUntransformedPinNameBox() )
447 {
448 bbox.Merge( *nameBox );
449 }
450
451 if( OPT_BOX2I altIconBox = getUntransformedAltIconBox() )
452 {
453 bbox.Merge( *altIconBox );
454 }
455 }
456
457 if( includeNumber )
458 {
460 {
461 bbox.Merge( *numBox );
462 }
463 }
464
465 if( includeType )
466 {
467 if( OPT_BOX2I typeBox = getUntransformedPinTypeBox() )
468 {
469 bbox.Merge( *typeBox );
470 }
471 }
472
473 transformBoxForPin( bbox );
474
475 if( m_pin.IsDangling() )
476 {
477 // Not much point caching this, but we could
478 const CIRCLE c = GetDanglingIndicator();
479
480 BOX2I cBox = BOX2I::ByCenter( c.Center, { c.Radius * 2, c.Radius * 2 } );
481 // TODO: need some way to find the thickness...?
482 // cBox.Inflate( ??? );
483
484 bbox.Merge( cBox );
485 }
486
487 bbox.Normalize();
488 bbox.Inflate( ( m_pin.GetPenWidth() / 2 ) + 1 );
489
490 return bbox;
491}
492
493
495{
496 return CIRCLE{
497 m_pin.GetPosition(),
499 };
500}
501
502
504{
505 const int numSize = m_pin.GetNumberTextSize();
506 int perpHeight = numSize;
507
508 if( m_numExtentsCache.m_Text.Contains( '\n' ) )
509 {
510 wxArrayString lines;
511 wxStringSplit( m_numExtentsCache.m_Text, lines, '\n' );
512 perpHeight = (int) lines.size() * KiROUND( numSize * 1.3 );
513 }
514
515 return perpHeight / 2 + getPinTextOffset() + schIUScale.MilsToIU( PIN_TEXT_MARGIN ) + m_numberThickness;
516}
517
518
520{
521 const SYMBOL* parentSym = m_pin.GetParentSymbol();
522
523 return parentSym && parentSym->GetShowPinNames() && m_pin.GetNameTextSize() > 0
524 && !m_pin.GetShownName().IsEmpty();
525}
526
527
529{
530 const float offsetRatio =
532 return schIUScale.MilsToIU( KiROUND( 24 * offsetRatio ) );
533}
534
535
537{
538 int pinNameOffset = 0;
539 if( const SYMBOL* parentSymbol = m_pin.GetParentSymbol() )
540 {
541 if( parentSymbol->GetShowPinNames() )
542 pinNameOffset = parentSymbol->GetPinNameOffset();
543 }
544
545 // We're considering the PIN_RIGHT scenario
546 // TEXT
547 // X-------| TEXT
548 // TEXT
549 //
550 // We'll rotate it later.
551
552 OPT_BOX2I box;
553 const int pinLength = m_pin.GetLength();
554
555 if( pinNameOffset > 0 )
556 {
557 // This means name inside the pin
558 box = BOX2I::ByCenter( { pinLength, 0 }, m_nameExtentsCache.m_Extents );
559
560 // Bump over to be left aligned just inside the pin
561 box->Move( { m_nameExtentsCache.m_Extents.x / 2 + pinNameOffset, 0 } );
562 }
563 else
564 {
565 // The pin name is always over the pin
566 box = BOX2I::ByCenter( { pinLength / 2, 0 }, m_nameExtentsCache.m_Extents );
567
568 // Bump it up
569 box->Move( { 0, -m_nameExtentsCache.m_Extents.y / 2 - getPinTextOffset() } );
570 }
571
572 return box;
573}
574
575
577{
578 int pinNameOffset = 0;
579
580 if( const SYMBOL* parentSymbol = m_pin.GetParentSymbol() )
581 {
582 if( parentSymbol->GetShowPinNames() )
583 pinNameOffset = parentSymbol->GetPinNameOffset();
584 }
585
586 const int pinLength = m_pin.GetLength();
587
588 // The pin number is always over the pin (centered along its length)
589 OPT_BOX2I box = BOX2I::ByCenter( { pinLength / 2, 0 }, m_numExtentsCache.m_Extents );
590
591 // Check if both name and number are displayed (name outside the pin)
592 bool showBothNameAndNumber = pinNameOffset == 0 && isNameShown();
593
594 // Same side decision as GetPinNumberInfo(), so the box follows a mirrored block (issue 24894)
595 const bool flipStacked = m_numExtentsCache.m_Text.Contains( '\n' )
596 && ( m_pin.GetFlipStackedTextSide() != m_pin.StackedTextSideFlipped( DefaultTransform ) );
597
598 const int offset = m_numExtentsCache.m_Extents.y / 2 + getPinTextOffset();
599
600 int textPos;
601
602 if( showBothNameAndNumber )
603 {
604 // When both are shown: name goes above, number goes below (top-aligned to bottom)
605 // Position the number below the pin, with its top edge at the clearance distance
606 textPos = offset;
607 }
608 else
609 {
610 // When only number is shown: place it above the pin (below when mirrored)
611 textPos = flipStacked ? offset : -offset;
612 }
613
614 // Bump it up (or down)
615 box->Move( { 0, textPos } );
616
617 return box;
618}
619
620
622{
624 return std::nullopt;
625
626 BOX2I box{
627 { -m_typeExtentsCache.m_Extents.x, -m_typeExtentsCache.m_Extents.y / 2 },
628 m_typeExtentsCache.m_Extents,
629 };
630
631 // Jog left
632 box.Move( { -schIUScale.MilsToIU( PIN_TEXT_MARGIN ) - TARGET_PIN_RADIUS, 0 } );
633
634 return box;
635}
636
637
639{
640 const OPT_BOX2I nameBox = getUntransformedPinNameBox();
641
642 if( !nameBox || m_pin.GetAlternates().empty() || !m_showAltIcons )
643 return std::nullopt;
644
645 const int iconSize = std::min( m_pin.GetNameTextSize(), schIUScale.mmToIU( 1.5 ) );
646
647 VECTOR2I c{ 0, ( nameBox->GetTop() + nameBox->GetBottom() ) / 2 };
648 if( m_pin.GetParentSymbol()->GetPinNameOffset() > 0 )
649 {
650 // name inside, so icon more inside
651 c.x = nameBox->GetRight() + iconSize * 0.75;
652 }
653 else
654 {
655 c.x = nameBox->GetLeft() - iconSize * 0.75;
656 }
657
658 return BOX2I::ByCenter( c, { iconSize, iconSize } );
659}
660
661
663{
664 const GRAPHIC_PINSHAPE shape = m_pin.GetShape();
665 const int decoSize = externalPinDecoSize( m_schSettings, m_pin );
666 const int intDecoSize = internalPinDecoSize( m_schSettings, m_pin );
667
668 const auto makeInvertBox =
669 [&]()
670 {
671 return BOX2I::ByCenter( { -decoSize, 0 }, { decoSize * 2, decoSize * 2 } );
672 };
673
674 const auto makeLowBox =
675 [&]()
676 {
677 return BOX2I::ByCorners( { -decoSize * 2, -decoSize * 2 }, { 0, 0 } );
678 };
679
680 const auto makeClockBox =
681 [&]()
682 {
683 return BOX2I::ByCorners( { 0, -intDecoSize }, { intDecoSize, intDecoSize } );
684 };
685
686 OPT_BOX2I box;
687
688 switch( shape )
689 {
691 box = makeInvertBox();
692 break;
693
695 box = makeClockBox();
696 break;
697
699 box = makeInvertBox();
700 box->Merge( makeClockBox() );
701 break;
702
704 box = makeLowBox();
705 break;
706
709 box = makeLowBox();
710 box->Merge( makeClockBox() );
711 break;
712
714 box = BOX2I::ByCenter( { 0, 0 }, { decoSize * 2, decoSize * 2 } );
715 break;
716
718 default:
719 // No decoration
720 break;
721 }
722
723 if( box )
724 {
725 // Put the box at the root of the pin
726 box->Move( { m_pin.GetLength(), 0 } );
727 box->Inflate( m_pin.GetPenWidth() / 2 );
728 }
729
730 return box;
731}
732
733
735{
738
739 if( box )
740 transformBoxForPin( *box );
741
742 return box;
743}
744
745
747{
750
751 if( box )
752 transformBoxForPin( *box );
753
754 return box;
755}
756
757
759{
761
762 if( box )
763 transformBoxForPin( *box );
764
765 return box;
766}
767
768
769std::optional<PIN_LAYOUT_CACHE::TEXT_INFO> PIN_LAYOUT_CACHE::GetPinNameInfo( int aShadowWidth )
770{
772 wxString name = m_pin.GetShownName();
773
774 // TODO - work out exactly what we need to do to cache this
775 // (or if it's worth the memory/complexity)
776 // But it's not hugely expensive to recompute, and that's what's always been
777 // done to now
778 //
779 // Because pins are very likely to share a lot of characteristics, a global
780 // cache might make more sense than a per-pin cache.
781
782 if( !isNameShown() )
783 return std::nullopt;
784
785 std::optional<TEXT_INFO> info = TEXT_INFO();
786 info->m_Text = std::move( name );
787 info->m_TextSize = m_pin.GetNameTextSize();
788 info->m_Thickness = m_nameThickness;
789 info->m_Angle = ANGLE_HORIZONTAL;
790
791 bool nameInside = m_pin.GetParentSymbol()->GetPinNameOffset() > 0;
792
793 if( nameInside )
794 {
795 // This means name inside the pin
796 VECTOR2I pos = { m_pin.GetLength() + m_pin.GetParentSymbol()->GetPinNameOffset(), 0 };
797 const int shadowOffset = KiROUND( aShadowWidth * m_shadowOffsetAdjust ) / 2;
798
799 info->m_TextPosition = pos + VECTOR2I{ -shadowOffset, 0 };
800 info->m_HAlign = GR_TEXT_H_ALIGN_LEFT;
801 info->m_VAlign = GR_TEXT_V_ALIGN_CENTER;
803 }
804 else
805 {
806 // The pin name is always over the pin
807 VECTOR2I pos = { m_pin.GetLength() / 2, -getPinTextOffset() - info->m_Thickness / 2 };
808
809 info->m_TextPosition = pos;
810 info->m_HAlign = GR_TEXT_H_ALIGN_CENTER;
811 info->m_VAlign = GR_TEXT_V_ALIGN_CENTER;
812
813 // New policy: names follow same positioning semantics as numbers except when
814 // specified as inside. When names are inside, they should not overlap with the
815 // number position.
816 const SYMBOL* parentSym = m_pin.GetParentSymbol();
817
818 if( parentSym )
819 {
820 int maxHalfHeight = 0;
821
822 for( const SCH_PIN* p : parentSym->GetGraphicalPins( ALL_UNITS, ALL_BODY_STYLES ) )
823 {
824 wxString n = p->GetShownName();
825
826 if( n.IsEmpty() )
827 continue;
828
829 maxHalfHeight = std::max( maxHalfHeight, p->GetNameTextSize() / 2 );
830 }
831
833 VECTOR2I pinPos = m_pin.GetPosition();
834 const int halfLength = m_pin.GetLength() / 2;
835 PIN_ORIENTATION orient = m_pin.PinDrawOrient( DefaultTransform );
836 bool verticalOrient = ( orient == PIN_ORIENTATION::PIN_UP || orient == PIN_ORIENTATION::PIN_DOWN );
837
838 if( verticalOrient )
839 {
840 // Vertical pins: name mirrors number placement (left + rotated) for visual consistency.
841 // Use text HEIGHT (not width) for perpendicular offset - same as horizontal pins.
842 // This ensures consistent spacing between horizontal and vertical pin orientations.
843 int perpendicularOffset = clearance + info->m_TextSize / 2 + info->m_Thickness;
844 int centerX = pinPos.x - perpendicularOffset;
845 info->m_TextPosition = { centerX, pinPos.y };
846
847 if( orient == PIN_ORIENTATION::PIN_DOWN )
848 info->m_TextPosition.y += halfLength;
849 else
850 info->m_TextPosition.y -= halfLength;
851
852 info->m_Angle = ANGLE_VERTICAL;
853 info->m_HAlign = GR_TEXT_H_ALIGN_CENTER;
854 info->m_VAlign = GR_TEXT_V_ALIGN_CENTER;
855 }
856 else
857 {
858 // Horizontal pins: name above (negative Y) aligned to same Y offset logic as numbers.
859 info->m_TextPosition = { pinPos.x, pinPos.y - ( maxHalfHeight + clearance + info->m_Thickness ) };
860
861 if( orient == PIN_ORIENTATION::PIN_LEFT )
862 info->m_TextPosition.x -= halfLength;
863 else
864 info->m_TextPosition.x += halfLength;
865
866 info->m_Angle = ANGLE_HORIZONTAL;
867 info->m_HAlign = GR_TEXT_H_ALIGN_CENTER;
868 info->m_VAlign = GR_TEXT_V_ALIGN_CENTER;
869 }
870 }
871 }
872 return info;
873}
874
875
876// (Removed duplicate later GetPinNumberInfo – earlier definition retained at top of file.)
877
878
879std::optional<PIN_LAYOUT_CACHE::TEXT_INFO>
881{
883
885 return std::nullopt;
886
887 std::optional<TEXT_INFO> info = TEXT_INFO();
888 info->m_Text = m_pin.GetElectricalTypeName();
889 info->m_TextSize = std::max( m_pin.GetNameTextSize() * 3 / 4, schIUScale.mmToIU( 0.7 ) );
890 info->m_Angle = ANGLE_HORIZONTAL;
891 info->m_Thickness = info->m_TextSize / 8;
892 info->m_TextPosition = { -getPinTextOffset() - info->m_Thickness / 2
893 + KiROUND( aShadowWidth * m_shadowOffsetAdjust ) / 2,
894 0 };
895 info->m_HAlign = GR_TEXT_H_ALIGN_RIGHT;
896 info->m_VAlign = GR_TEXT_V_ALIGN_CENTER;
897
898 info->m_TextPosition.x -= TARGET_PIN_RADIUS;
899
900 if( m_pin.IsDangling() )
901 {
902 info->m_TextPosition.x -= TARGET_PIN_RADIUS / 2;
903 }
904
906 return info;
907}
const char * name
constexpr EDA_IU_SCALE schIUScale
Definition base_units.h:130
BOX2< VECTOR2I > BOX2I
Definition box2.h:917
std::optional< BOX2I > OPT_BOX2I
Definition box2.h:921
constexpr BOX2I KiROUND(const BOX2D &aBoxD)
Definition box2.h:985
constexpr BOX2< Vec > & Inflate(coord_type dx, coord_type dy)
Inflates the rectangle horizontally by dx and vertically by dy.
Definition box2.h:556
static constexpr BOX2< VECTOR2I > ByCorners(const VECTOR2I &aCorner1, const VECTOR2I &aCorner2)
Definition box2.h:67
constexpr BOX2< Vec > & Normalize()
Ensure that the height and width are positive.
Definition box2.h:149
static constexpr BOX2< VECTOR2I > ByCenter(const VECTOR2I &aCenter, const SizeVec &aSize)
Definition box2.h:72
constexpr BOX2< Vec > & Merge(const BOX2< Vec > &aRect)
Modify the position and size of the rectangle in order to contain aRect.
Definition box2.h:597
constexpr const Vec GetCenter() const
Definition box2.h:233
constexpr coord_type GetLeft() const
Definition box2.h:231
constexpr void Move(const Vec &aMoveVector)
Move the rectangle by the aMoveVector.
Definition box2.h:141
constexpr coord_type GetRight() const
Definition box2.h:220
constexpr coord_type GetTop() const
Definition box2.h:232
constexpr void Offset(coord_type dx, coord_type dy)
Definition box2.h:262
constexpr coord_type GetBottom() const
Definition box2.h:225
Represent basic circle geometry with utility geometry functions.
Definition circle.h:33
VECTOR2I Center
Public to make access simpler.
Definition circle.h:150
FONT is an abstract base class for both outline and stroke fonts.
Definition font.h:94
static FONT * GetFont(const wxString &aFontName=wxEmptyString, bool aBold=false, bool aItalic=false, const std::vector< wxString > *aEmbeddedFiles=nullptr, bool aForDrawingSheet=false)
Definition font.cpp:144
virtual bool IsOutline() const
Definition font.h:102
VECTOR2I StringBoundaryLimits(const wxString &aText, const VECTOR2I &aSize, int aThickness, bool aBold, bool aItalic, const METRICS &aFontMetrics) const
Compute the boundary limits of aText (the bounding box of all shapes).
Definition font.cpp:442
OPT_BOX2I getUntransformedPinNameBox() const
Get the untransformd text box in the default orientation.
TEXT_EXTENTS_CACHE m_nameExtentsCache
void transformTextForPin(TEXT_INFO &aTextInfo) const
Transform text info to suit a pin's.
int getNumberBlockOffset() const
Get the distance from the pin to the centre of the drawn number, measured perpendicular to the pin.
OPT_BOX2I GetAltIconBBox()
Get the box of the alt mode icon, if there is one.
void transformBoxForPin(BOX2I &aBox) const
Transform a box (in-place) to the pin's orientation.
void recomputeCaches()
Recompute all the caches that have become dirty.
const SCHEMATIC_SETTINGS * m_schSettings
OPT_BOX2I GetPinNumberBBox()
Get the bounding box of the pin number, if there is one.
PIN_LAYOUT_CACHE(const SCH_PIN &aPin)
TEXT_EXTENTS_CACHE m_typeExtentsCache
BOX2I GetPinBoundingBox(bool aIncludeLabelsOnInvisiblePins, bool aIncludeNameAndNumber, bool aIncludeElectricalType)
Get the bounding box of the pin itself.
TEXT_EXTENTS_CACHE m_numExtentsCache
void setClean(int aMask)
OPT_BOX2I getUntransformedPinTypeBox() const
std::optional< TEXT_INFO > GetPinNameInfo(int aShadowWidth)
Get the text info for the pin name.
static void recomputeExtentsCache(bool aDefinitelyDirty, KIFONT::FONT *aFont, int aSize, const wxString &aText, const KIFONT::METRICS &aFontMetrics, TEXT_EXTENTS_CACHE &aCache)
std::optional< TEXT_INFO > GetPinElectricalTypeInfo(int aShadowWidth)
bool isDirty(int aMask) const
CIRCLE GetDanglingIndicator() const
Gets the dangling indicator geometry for this pin, if the pin were to be dangling.
std::optional< TEXT_INFO > GetPinNumberInfo(int aShadowWidth)
bool isNameShown() const
Whether the pin's name is drawn.
OPT_BOX2I GetPinNameBBox()
Get the bounding box of the pin name, if there is one.
void MarkDirty(int aFlags)
Recompute all the layout information.
void SetRenderParameters(int aNameThickness, int aNumberThickness, bool aShowElectricalType, bool aShowAltIcons)
OPT_BOX2I getUntransformedAltIconBox() const
int getPinTextOffset() const
Get the current pin text offset.
OPT_BOX2I getUntransformedDecorationBox() const
Pin type decoration if any.
const SCH_PIN & m_pin
The pin in question.
OPT_BOX2I getUntransformedPinNumberBox() const
These are loaded from Eeschema settings but then overwritten by the project settings.
Holds all the data relating to one schematic.
Definition schematic.h:155
SCHEMATIC_SETTINGS & Settings() const
SCHEMATIC * Schematic() const
Search the item hierarchy to find a SCHEMATIC.
Definition sch_item.cpp:302
int GetNumberTextSize() const
Definition sch_pin.cpp:885
int GetNameTextSize() const
Definition sch_pin.cpp:859
const BOX2I GetBoundingBox() const override
Return the orthogonal bounding box of this object for display purposes.
Definition sch_pin.h:280
Schematic symbol object.
Definition sch_symbol.h:76
A base class for LIB_SYMBOL and SCH_SYMBOL.
Definition symbol.h:60
int GetPinNameOffset() const
Definition symbol.h:163
virtual std::vector< SCH_PIN * > GetGraphicalPins(int aUnit, int aBodyStyle) const =0
virtual bool GetShowPinNames() const
Definition symbol.h:169
#define DEFAULT_TEXT_OFFSET_RATIO
Ratio of the font height to the baseline of the text above the wire.
static constexpr EDA_ANGLE ANGLE_90
Definition eda_angle.h:474
static constexpr EDA_ANGLE ANGLE_VERTICAL
Definition eda_angle.h:469
static constexpr EDA_ANGLE ANGLE_HORIZONTAL
Definition eda_angle.h:468
TRANSFORM DefaultTransform
Definition transform.cpp:28
int GetPenSizeForNormal(int aTextSize)
Definition gr_text.cpp:58
see class PGM_BASE
static int externalPinDecoSize(const SCHEMATIC_SETTINGS *aSettings, const SCH_PIN &aPin)
static int internalPinDecoSize(const SCHEMATIC_SETTINGS *aSettings, const SCH_PIN &aPin)
PIN_ORIENTATION
The symbol library pin object orientations.
Definition pin_type.h:101
@ PIN_UP
The pin extends upwards from the connection point: Probably on the bottom side of the symbol.
Definition pin_type.h:123
@ PIN_RIGHT
The pin extends rightwards from the connection point.
Definition pin_type.h:107
@ PIN_LEFT
The pin extends leftwards from the connection point: Probably on the right side of the symbol.
Definition pin_type.h:114
@ PIN_DOWN
The pin extends downwards from the connection: Probably on the top side of the symbol.
Definition pin_type.h:131
GRAPHIC_PINSHAPE
Definition pin_type.h:80
wxString FormatStackedPinForDisplay(const wxString &aPinNumber, int aPinLength, int aTextSize, KIFONT::FONT *aFont, const KIFONT::METRICS &aFontMetrics)
Break a stacked pin number of the form "[A,B,C]" into one number per line when it is too wide to sit ...
Definition sch_pin.cpp:51
#define PIN_TEXT_MARGIN
Definition sch_pin.cpp:109
#define TARGET_PIN_RADIUS
Definition sch_pin.h:48
T * GetAppSettings(const char *aFilename)
Utility functions for working with shapes.
void wxStringSplit(const wxString &aText, wxArrayString &aStrings, wxChar aSplitter)
Split aString to a string list separated at aSplitter.
Cached extent of a text item.
#define ALL_UNITS
Definition symbol.h:150
#define ALL_BODY_STYLES
Definition symbol.h:151
static wxString formatted(LIB_SYMBOL &aSymbol)
Serialize one symbol the way the library writer would.
int clearance
@ GR_TEXT_H_ALIGN_CENTER
@ GR_TEXT_H_ALIGN_RIGHT
@ GR_TEXT_H_ALIGN_LEFT
@ GR_TEXT_V_ALIGN_CENTER
constexpr GR_TEXT_H_ALIGN_T GetFlippedAlignment(GR_TEXT_H_ALIGN_T aAlign)
Get the reverse alignment: left-right are swapped, others are unchanged.
void RotatePoint(int *pX, int *pY, const EDA_ANGLE &aAngle)
Calculate the new point of coord coord pX, pY, for a rotation center 0, 0.
Definition trigo.cpp:227
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:708
VECTOR2< double > VECTOR2D
Definition vector2d.h:707