File: pcb_textbox.cpp

package info (click to toggle)
kicad 9.0.3%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 770,320 kB
  • sloc: cpp: 961,692; ansic: 121,001; xml: 66,428; python: 18,387; sh: 1,010; awk: 301; asm: 292; makefile: 227; javascript: 167; perl: 10
file content (850 lines) | stat: -rw-r--r-- 28,512 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
/*
 * This program source code file is part of KiCad, a free EDA CAD application.
 *
 * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, you may find one here:
 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
 * or you may search the http://www.gnu.org website for the version 2 license,
 * or you may write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
 */

#include <advanced_config.h>
#include <pcb_edit_frame.h>
#include <base_units.h>
#include <bitmaps.h>
#include <board.h>
#include <board_design_settings.h>
#include <footprint.h>
#include <pcb_textbox.h>
#include <pcb_painter.h>
#include <trigo.h>
#include <string_utils.h>
#include <geometry/shape_compound.h>
#include <callback_gal.h>
#include <convert_basic_shapes_to_polygon.h>
#include <macros.h>
#include <core/ignore.h>
#include <api/api_enums.h>
#include <api/api_utils.h>
#include <api/board/board_types.pb.h>

PCB_TEXTBOX::PCB_TEXTBOX( BOARD_ITEM* aParent, KICAD_T aType ) :
    PCB_SHAPE( aParent, aType, SHAPE_T::RECTANGLE ),
    EDA_TEXT( pcbIUScale ),
    m_borderEnabled( true )
{
    SetHorizJustify( GR_TEXT_H_ALIGN_LEFT );
    SetVertJustify( GR_TEXT_V_ALIGN_CENTER );
    SetMultilineAllowed( true );

    int defaultMargin = GetLegacyTextMargin();
    m_marginLeft = defaultMargin;
    m_marginTop = defaultMargin;
    m_marginRight = defaultMargin;
    m_marginBottom = defaultMargin;
}

PCB_TEXTBOX::~PCB_TEXTBOX()
{
}


void PCB_TEXTBOX::CopyFrom( const BOARD_ITEM* aOther )
{
    wxCHECK( aOther && aOther->Type() == PCB_TEXTBOX_T, /* void */ );
    *this = *static_cast<const PCB_TEXTBOX*>( aOther );
}


void PCB_TEXTBOX::Serialize( google::protobuf::Any &aContainer ) const
{
    using namespace kiapi::common::types;
    using namespace kiapi::board;
    types::BoardTextBox boardText;
    boardText.set_layer( ToProtoEnum<PCB_LAYER_ID, types::BoardLayer>( GetLayer() ) );
    boardText.mutable_id()->set_value( m_Uuid.AsStdString() );
    boardText.set_locked( IsLocked() ? LockedState::LS_LOCKED : LockedState::LS_UNLOCKED );

    TextBox& text = *boardText.mutable_textbox();

    kiapi::common::PackVector2( *text.mutable_top_left(), GetPosition() );
    kiapi::common::PackVector2( *text.mutable_bottom_right(), GetEnd() );
    text.set_text( GetText().ToStdString() );
    //text.set_hyperlink( GetHyperlink().ToStdString() );

    TextAttributes* attrs = text.mutable_attributes();

    if( GetFont() )
        attrs->set_font_name( GetFont()->GetName().ToStdString() );

    attrs->set_horizontal_alignment(
            ToProtoEnum<GR_TEXT_H_ALIGN_T, HorizontalAlignment>( GetHorizJustify() ) );

    attrs->set_vertical_alignment(
            ToProtoEnum<GR_TEXT_V_ALIGN_T, VerticalAlignment>( GetVertJustify() ) );

    attrs->mutable_angle()->set_value_degrees( GetTextAngleDegrees() );
    attrs->set_line_spacing( GetLineSpacing() );
    attrs->mutable_stroke_width()->set_value_nm( GetTextThickness() );
    attrs->set_italic( IsItalic() );
    attrs->set_bold( IsBold() );
    attrs->set_underlined( GetAttributes().m_Underlined );
    attrs->set_mirrored( IsMirrored() );
    attrs->set_multiline( IsMultilineAllowed() );
    attrs->set_keep_upright( IsKeepUpright() );
    kiapi::common::PackVector2( *attrs->mutable_size(), GetTextSize() );

    aContainer.PackFrom( boardText );
}


bool PCB_TEXTBOX::Deserialize( const google::protobuf::Any &aContainer )
{
    using namespace kiapi::board;
    types::BoardTextBox boardText;

    if( !aContainer.UnpackTo( &boardText ) )
        return false;

    const_cast<KIID&>( m_Uuid ) = KIID( boardText.id().value() );
    SetLayer( FromProtoEnum<PCB_LAYER_ID, types::BoardLayer>( boardText.layer() ) );
    SetLocked( boardText.locked() == kiapi::common::types::LockedState::LS_LOCKED );

    const kiapi::common::types::TextBox& text = boardText.textbox();

    SetPosition( kiapi::common::UnpackVector2( text.top_left() ) );
    SetEnd( kiapi::common::UnpackVector2( text.bottom_right() ) );
    SetText( wxString( text.text().c_str(), wxConvUTF8 ) );
    //SetHyperlink( wxString::FromUTF8( text.hyperlink() );

    if( text.has_attributes() )
    {
        TEXT_ATTRIBUTES attrs = GetAttributes();

        attrs.m_Bold = text.attributes().bold();
        attrs.m_Italic = text.attributes().italic();
        attrs.m_Underlined = text.attributes().underlined();
        attrs.m_Mirrored = text.attributes().mirrored();
        attrs.m_Multiline = text.attributes().multiline();
        attrs.m_KeepUpright = text.attributes().keep_upright();
        attrs.m_Size = kiapi::common::UnpackVector2( text.attributes().size() );

        if( !text.attributes().font_name().empty() )
        {
            attrs.m_Font = KIFONT::FONT::GetFont(
                    wxString( text.attributes().font_name().c_str(), wxConvUTF8 ), attrs.m_Bold,
                    attrs.m_Italic );
        }

        attrs.m_Angle = EDA_ANGLE( text.attributes().angle().value_degrees(), DEGREES_T );
        attrs.m_LineSpacing = text.attributes().line_spacing();
        attrs.m_StrokeWidth = text.attributes().stroke_width().value_nm();
        attrs.m_Halign =
                FromProtoEnum<GR_TEXT_H_ALIGN_T, kiapi::common::types::HorizontalAlignment>(
                        text.attributes().horizontal_alignment() );

        attrs.m_Valign = FromProtoEnum<GR_TEXT_V_ALIGN_T, kiapi::common::types::VerticalAlignment>(
                text.attributes().vertical_alignment() );

        SetAttributes( attrs );
    }

    return true;
}


void PCB_TEXTBOX::StyleFromSettings( const BOARD_DESIGN_SETTINGS& settings )
{
    PCB_SHAPE::StyleFromSettings( settings );

    SetTextSize( settings.GetTextSize( GetLayer() ) );
    SetTextThickness( settings.GetTextThickness( GetLayer() ) );
    SetItalic( settings.GetTextItalic( GetLayer() ) );
    SetKeepUpright( settings.GetTextUpright( GetLayer() ) );
    SetMirrored( IsBackLayer( GetLayer() ) );
}


int PCB_TEXTBOX::GetLegacyTextMargin() const
{
    return KiROUND( GetStroke().GetWidth() / 2.0 ) + KiROUND( GetTextSize().y * 0.75 );
}


VECTOR2I PCB_TEXTBOX::GetTopLeft() const
{
    EDA_ANGLE rotation = GetDrawRotation();

    if( rotation == ANGLE_90 )
        return VECTOR2I( GetStartX(), GetEndY() );
    else if( rotation == ANGLE_180 )
        return GetEnd();
    else if( rotation == ANGLE_270 )
        return VECTOR2I( GetEndX(), GetStartY() );
    else
        return GetStart();
}


VECTOR2I PCB_TEXTBOX::GetBotRight() const
{
    EDA_ANGLE rotation = GetDrawRotation();

    if( rotation == ANGLE_90 )
        return VECTOR2I( GetEndX(), GetStartY() );
    else if( rotation == ANGLE_180 )
        return GetStart();
    else if( rotation == ANGLE_270 )
        return VECTOR2I( GetStartX(), GetEndY() );
    else
        return GetEnd();
}


void PCB_TEXTBOX::SetTop( int aVal )
{
    EDA_ANGLE rotation = GetDrawRotation();

    if( rotation == ANGLE_90 || rotation == ANGLE_180 )
        SetEndY( aVal );
    else
        SetStartY( aVal );
}


void PCB_TEXTBOX::SetBottom( int aVal )
{
    EDA_ANGLE rotation = GetDrawRotation();

    if( rotation == ANGLE_90 || rotation == ANGLE_180 )
        SetStartY( aVal );
    else
        SetEndY( aVal );
}


void PCB_TEXTBOX::SetLeft( int aVal )
{
    EDA_ANGLE rotation = GetDrawRotation();

    if( rotation == ANGLE_180 || rotation == ANGLE_270 )
        SetEndX( aVal );
    else
        SetStartX( aVal );
}


void PCB_TEXTBOX::SetRight( int aVal )
{
    EDA_ANGLE rotation = GetDrawRotation();

    if( rotation == ANGLE_180 || rotation == ANGLE_270 )
        SetStartX( aVal );
    else
        SetEndX( aVal );
}


void PCB_TEXTBOX::SetTextAngle( const EDA_ANGLE& aAngle )
{
    EDA_ANGLE delta = aAngle.Normalized() - GetTextAngle();
    Rotate( GetPosition(), delta );
}


VECTOR2I PCB_TEXTBOX::GetDrawPos() const
{
    return GetDrawPos( false );
}


VECTOR2I PCB_TEXTBOX::GetDrawPos( bool aIsFlipped ) const
{
    EDA_ANGLE             drawAngle = GetDrawRotation();
    std::vector<VECTOR2I> corners = GetCornersInSequence( drawAngle );
    GR_TEXT_H_ALIGN_T     horizontalAlignment = GetHorizJustify();
    GR_TEXT_V_ALIGN_T     verticalAlignment = GetVertJustify();
    VECTOR2I              textAnchor;
    VECTOR2I              offset;

    // Calculate midpoints
    VECTOR2I midTop = ( corners[0] + corners[1] ) / 2;
    VECTOR2I midBottom = ( corners[3] + corners[2] ) / 2;
    VECTOR2I midLeft = ( corners[0] + corners[3] ) / 2;
    VECTOR2I midRight = ( corners[1] + corners[2] ) / 2;
    VECTOR2I center = ( corners[0] + corners[1] + corners[2] + corners[3] ) / 4;

    if( IsMirrored() != aIsFlipped )
    {
        switch( GetHorizJustify() )
        {
        case GR_TEXT_H_ALIGN_LEFT: horizontalAlignment = GR_TEXT_H_ALIGN_RIGHT; break;
        case GR_TEXT_H_ALIGN_CENTER: horizontalAlignment = GR_TEXT_H_ALIGN_CENTER; break;
        case GR_TEXT_H_ALIGN_RIGHT: horizontalAlignment = GR_TEXT_H_ALIGN_LEFT; break;
        case GR_TEXT_H_ALIGN_INDETERMINATE:
            horizontalAlignment = GR_TEXT_H_ALIGN_INDETERMINATE;
            break;
        }
    }

    wxASSERT_MSG(
            horizontalAlignment != GR_TEXT_H_ALIGN_INDETERMINATE
                    && verticalAlignment != GR_TEXT_V_ALIGN_INDETERMINATE,
            wxS( "Indeterminate state legal only in dialogs. Horizontal and vertical alignment "
                 "must be set before calling PCB_TEXTBOX::GetDrawPos." ) );

    if( horizontalAlignment == GR_TEXT_H_ALIGN_INDETERMINATE
        || verticalAlignment == GR_TEXT_V_ALIGN_INDETERMINATE )
    {
        return center;
    }

    if( horizontalAlignment == GR_TEXT_H_ALIGN_LEFT && verticalAlignment == GR_TEXT_V_ALIGN_TOP )
    {
        textAnchor = corners[0];
    }
    else if( horizontalAlignment == GR_TEXT_H_ALIGN_CENTER
             && verticalAlignment == GR_TEXT_V_ALIGN_TOP )
    {
        textAnchor = midTop;
    }
    else if( horizontalAlignment == GR_TEXT_H_ALIGN_RIGHT
             && verticalAlignment == GR_TEXT_V_ALIGN_TOP )
    {
        textAnchor = corners[1];
    }
    else if( horizontalAlignment == GR_TEXT_H_ALIGN_LEFT
             && verticalAlignment == GR_TEXT_V_ALIGN_CENTER )
    {
        textAnchor = midLeft;
    }
    else if( horizontalAlignment == GR_TEXT_H_ALIGN_CENTER
             && verticalAlignment == GR_TEXT_V_ALIGN_CENTER )
    {
        textAnchor = center;
    }
    else if( horizontalAlignment == GR_TEXT_H_ALIGN_RIGHT
             && verticalAlignment == GR_TEXT_V_ALIGN_CENTER )
    {
        textAnchor = midRight;
    }
    else if( horizontalAlignment == GR_TEXT_H_ALIGN_LEFT
             && verticalAlignment == GR_TEXT_V_ALIGN_BOTTOM )
    {
        textAnchor = corners[3];
    }
    else if( horizontalAlignment == GR_TEXT_H_ALIGN_CENTER
             && verticalAlignment == GR_TEXT_V_ALIGN_BOTTOM )
    {
        textAnchor = midBottom;
    }
    else if( horizontalAlignment == GR_TEXT_H_ALIGN_RIGHT
             && verticalAlignment == GR_TEXT_V_ALIGN_BOTTOM )
    {
        textAnchor = corners[2];
    }

    int marginLeft = GetMarginLeft();
    int marginRight = GetMarginRight();
    int marginTop = GetMarginTop();
    int marginBottom = GetMarginBottom();

    if( horizontalAlignment == GR_TEXT_H_ALIGN_LEFT )
        offset.x = marginLeft;
    else if( horizontalAlignment == GR_TEXT_H_ALIGN_RIGHT )
        offset.x = -marginRight;

    if( verticalAlignment == GR_TEXT_V_ALIGN_TOP )
        offset.y = marginTop;
    else if( verticalAlignment == GR_TEXT_V_ALIGN_BOTTOM )
        offset.y = -marginBottom;

    RotatePoint( offset, GetDrawRotation() );
    return textAnchor + offset;
}


double PCB_TEXTBOX::ViewGetLOD( int aLayer, const KIGFX::VIEW* aView ) const
{
    KIGFX::PCB_PAINTER&         painter = static_cast<KIGFX::PCB_PAINTER&>( *aView->GetPainter() );
    KIGFX::PCB_RENDER_SETTINGS& renderSettings = *painter.GetSettings();

    if( aLayer == LAYER_LOCKED_ITEM_SHADOW )
    {
        // Hide shadow if the main layer is not shown
        if( !aView->IsLayerVisible( m_layer ) )
            return LOD_HIDE;

        // Hide shadow on dimmed tracks
        if( renderSettings.GetHighContrast() )
        {
            if( m_layer != renderSettings.GetPrimaryHighContrastLayer() )
                return LOD_HIDE;
        }
    }

    return LOD_SHOW;
}


std::vector<int> PCB_TEXTBOX::ViewGetLayers() const
{
    if( IsLocked() )
        return { GetLayer(), LAYER_LOCKED_ITEM_SHADOW };

    return { GetLayer() };
}


wxString PCB_TEXTBOX::GetShownText( bool aAllowExtraText, int aDepth ) const
{
    const FOOTPRINT* parentFootprint = GetParentFootprint();
    const BOARD*     board = GetBoard();

    std::function<bool( wxString* )> resolver = [&]( wxString* token ) -> bool
    {
        if( token->IsSameAs( wxT( "LAYER" ) ) )
        {
            *token = GetLayerName();
            return true;
        }

        if( parentFootprint && parentFootprint->ResolveTextVar( token, aDepth + 1 ) )
            return true;

        if( board->ResolveTextVar( token, aDepth + 1 ) )
            return true;

        return false;
    };

    wxString text = EDA_TEXT::GetShownText( aAllowExtraText, aDepth );

    if( HasTextVars() )
    {
        if( aDepth < ADVANCED_CFG::GetCfg().m_ResolveTextRecursionDepth )
            text = ExpandTextVars( text, &resolver );
    }

    KIFONT::FONT*         font = getDrawFont();
    EDA_ANGLE             drawAngle = GetDrawRotation();
    std::vector<VECTOR2I> corners = GetCornersInSequence( drawAngle );
    int                   colWidth = ( corners[1] - corners[0] ).EuclideanNorm();

    if( GetTextAngle().IsHorizontal() )
        colWidth -= ( GetMarginLeft() + GetMarginRight() );
    else
        colWidth -= ( GetMarginTop() + GetMarginBottom() );

    font->LinebreakText( text, colWidth, GetTextSize(), GetTextThickness(), IsBold(), IsItalic() );

    return text;
}


bool PCB_TEXTBOX::Matches( const EDA_SEARCH_DATA& aSearchData, void* aAuxData ) const
{
    return BOARD_ITEM::Matches( UnescapeString( GetText() ), aSearchData );
}


void PCB_TEXTBOX::GetMsgPanelInfo( EDA_DRAW_FRAME* aFrame, std::vector<MSG_PANEL_ITEM>& aList )
{
    // Don't use GetShownText() here; we want to show the user the variable references
    aList.emplace_back( _( "Text Box" ), KIUI::EllipsizeStatusText( aFrame, GetText() ) );

    if( aFrame->GetName() == PCB_EDIT_FRAME_NAME && IsLocked() )
        aList.emplace_back( _( "Status" ), _( "Locked" ) );

    aList.emplace_back( _( "Layer" ), GetLayerName() );
    aList.emplace_back( _( "Mirror" ), IsMirrored() ? _( "Yes" ) : _( "No" ) );
    aList.emplace_back( _( "Angle" ), wxString::Format( "%g", GetTextAngle().AsDegrees() ) );

    aList.emplace_back( _( "Font" ), GetFont() ? GetFont()->GetName() : _( "Default" ) );
    aList.emplace_back( _( "Text Thickness" ), aFrame->MessageTextFromValue( GetTextThickness() ) );
    aList.emplace_back( _( "Text Width" ), aFrame->MessageTextFromValue( GetTextWidth() ) );
    aList.emplace_back( _( "Text Height" ), aFrame->MessageTextFromValue( GetTextHeight() ) );

    aList.emplace_back( _( "Box Width" ),
                        aFrame->MessageTextFromValue( std::abs( GetEnd().x - GetStart().x ) ) );

    aList.emplace_back( _( "Box Height" ),
                        aFrame->MessageTextFromValue( std::abs( GetEnd().y - GetStart().y ) ));

    m_stroke.GetMsgPanelInfo( aFrame, aList );
}


void PCB_TEXTBOX::Move( const VECTOR2I& aMoveVector )
{
    PCB_SHAPE::Move( aMoveVector );
    EDA_TEXT::Offset( aMoveVector );
}


void PCB_TEXTBOX::Rotate( const VECTOR2I& aRotCentre, const EDA_ANGLE& aAngle )
{
    PCB_SHAPE::Rotate( aRotCentre, aAngle );
    EDA_TEXT::SetTextAngle( ( GetTextAngle() + aAngle ).Normalize() );

    if( GetTextAngle().IsCardinal() && GetShape() != SHAPE_T::RECTANGLE )
    {
        // To convert the polygon to its equivalent rectangle, we use GetCornersInSequence( drawAngle )
        // but this method uses the polygon bounding box.
        // set the line thickness to 0 to get the actual rectangle corner
        int lineWidth = GetWidth();
        SetWidth( 0 );
        EDA_ANGLE             drawAngle = GetDrawRotation();
        std::vector<VECTOR2I> corners = GetCornersInSequence( drawAngle );
        SetWidth( lineWidth );
        VECTOR2I              diag = corners[2] - corners[0];
        EDA_ANGLE             angle = GetTextAngle();

        SetShape( SHAPE_T::RECTANGLE );
        SetStart( corners[0] );

        if( angle == ANGLE_90 )
            SetEnd( VECTOR2I( corners[0].x + abs( diag.x ), corners[0].y - abs( diag.y ) ) );
        else if( angle == ANGLE_180 )
            SetEnd( VECTOR2I( corners[0].x - abs( diag.x ), corners[0].y - abs( diag.y ) ) );
        else if( angle == ANGLE_270 )
            SetEnd( VECTOR2I( corners[0].x - abs( diag.x ), corners[0].y + abs( diag.y ) ) );
        else
            SetEnd( VECTOR2I( corners[0].x + abs( diag.x ), corners[0].y + abs( diag.y ) ) );
    }
}


void PCB_TEXTBOX::Mirror( const VECTOR2I& aCentre, FLIP_DIRECTION aFlipDirection )
{
    // the position and angle are mirrored, but not the text (or its justification)
    PCB_SHAPE::Mirror( aCentre, aFlipDirection );

    if( aFlipDirection == FLIP_DIRECTION::LEFT_RIGHT )
        EDA_TEXT::SetTextAngle( ANGLE_180 - GetTextAngle() );
    else
        EDA_TEXT::SetTextAngle( -GetTextAngle() );
}


void PCB_TEXTBOX::Flip( const VECTOR2I& aCentre, FLIP_DIRECTION aFlipDirection )
{
    PCB_SHAPE::Flip( aCentre, aFlipDirection );

    EDA_TEXT::SetTextAngle( -GetTextAngle() );

    if( IsSideSpecific() )
        SetMirrored( !IsMirrored() );
}


bool PCB_TEXTBOX::HitTest( const VECTOR2I& aPosition, int aAccuracy ) const
{
    BOX2I rect = GetBoundingBox();

    rect.Inflate( aAccuracy );

    return rect.Contains( aPosition );
}


bool PCB_TEXTBOX::HitTest( const BOX2I& aRect, bool aContained, int aAccuracy ) const
{
    BOX2I rect = aRect;

    rect.Inflate( aAccuracy );

    if( aContained )
        return rect.Contains( GetBoundingBox() );

    return rect.Intersects( GetBoundingBox() );
}


wxString PCB_TEXTBOX::GetItemDescription( UNITS_PROVIDER* aUnitsProvider, bool aFull ) const
{
    return wxString::Format( _( "PCB Text Box '%s' on %s" ),
                             aFull ? GetShownText( false ) : KIUI::EllipsizeMenuText( GetText() ),
                             GetLayerName() );
}


BITMAPS PCB_TEXTBOX::GetMenuImage() const
{
    return BITMAPS::add_textbox;
}


EDA_ITEM* PCB_TEXTBOX::Clone() const
{
    return new PCB_TEXTBOX( *this );
}


void PCB_TEXTBOX::swapData( BOARD_ITEM* aImage )
{
    wxASSERT( aImage->Type() == PCB_TEXTBOX_T );

    std::swap( *((PCB_TEXTBOX*) this), *((PCB_TEXTBOX*) aImage) );
}


std::shared_ptr<SHAPE> PCB_TEXTBOX::GetEffectiveShape( PCB_LAYER_ID aLayer, FLASHING aFlash ) const
{
    std::shared_ptr<SHAPE_COMPOUND> shape = GetEffectiveTextShape();

    if( PCB_SHAPE::GetStroke().GetWidth() >= 0 )
        shape->AddShape( PCB_SHAPE::GetEffectiveShape( aLayer, aFlash ) );

    return shape;
}


void PCB_TEXTBOX::TransformTextToPolySet( SHAPE_POLY_SET& aBuffer, int aClearance, int aMaxError,
                                          ERROR_LOC aErrorLoc ) const
{
    KIGFX::GAL_DISPLAY_OPTIONS empty_opts;
    KIFONT::FONT*              font = getDrawFont();
    int                        penWidth = GetEffectiveTextPenWidth();

    // Note: this function is mainly used in 3D viewer.
    // the polygonal shape of a text can have many basic shapes,
    // so combining these shapes can be very useful to create a final shape
    // swith a lot less vertices to speedup calculations using this final shape
    // Simplify shapes is not usually always efficient, but in this case it is.
    SHAPE_POLY_SET buffer;

    CALLBACK_GAL callback_gal( empty_opts,
            // Stroke callback
            [&]( const VECTOR2I& aPt1, const VECTOR2I& aPt2 )
            {
                TransformOvalToPolygon( buffer, aPt1, aPt2, penWidth, aMaxError, aErrorLoc );
            },
            // Triangulation callback
            [&]( const VECTOR2I& aPt1, const VECTOR2I& aPt2, const VECTOR2I& aPt3 )
            {
                buffer.NewOutline();

                for( const VECTOR2I& point : { aPt1, aPt2, aPt3 } )
                    buffer.Append( point.x, point.y );
            } );

    font->Draw( &callback_gal, GetShownText( true ), GetDrawPos(), GetAttributes(), GetFontMetrics() );

    if( aClearance > 0 || aErrorLoc == ERROR_OUTSIDE )
    {
        if( aErrorLoc == ERROR_OUTSIDE )
            aClearance += aMaxError;

        buffer.Inflate( aClearance, CORNER_STRATEGY::ROUND_ALL_CORNERS, aMaxError, true );
    }
    else
    {
        buffer.Simplify();
    }

    aBuffer.Append( buffer );
}


void PCB_TEXTBOX::TransformShapeToPolygon( SHAPE_POLY_SET& aBuffer, PCB_LAYER_ID aLayer,
                                           int aClearance, int aMaxError, ERROR_LOC aErrorLoc,
                                           bool aIgnoreLineWidth ) const
{
    // Don't use PCB_SHAPE::TransformShapeToPolygon.  We want to treat the textbox as filled even
    // if there's no background colour.

    int width = GetWidth() + ( 2 * aClearance );

    if( GetShape() == SHAPE_T::RECTANGLE )
    {
        std::vector<VECTOR2I> pts = GetRectCorners();

        aBuffer.NewOutline();

        for( const VECTOR2I& pt : pts )
            aBuffer.Append( pt );

        if( m_borderEnabled && width > 0 )
        {
            // Add in segments
            TransformOvalToPolygon( aBuffer, pts[0], pts[1], width, aMaxError, aErrorLoc );
            TransformOvalToPolygon( aBuffer, pts[1], pts[2], width, aMaxError, aErrorLoc );
            TransformOvalToPolygon( aBuffer, pts[2], pts[3], width, aMaxError, aErrorLoc );
            TransformOvalToPolygon( aBuffer, pts[3], pts[0], width, aMaxError, aErrorLoc );
        }
    }
    else if( GetShape() == SHAPE_T::POLY )  // Non-cardinally-rotated rect
    {
        aBuffer.NewOutline();

        const SHAPE_LINE_CHAIN& poly = m_poly.Outline( 0 );

        for( int ii = 0; ii < poly.PointCount(); ++ii )
            aBuffer.Append( poly.GetPoint( ii ) );

        if( m_borderEnabled && width > 0 )
        {
            for( int ii = 0; ii < poly.SegmentCount(); ++ii )
            {
                const SEG& seg = poly.GetSegment( ii );
                TransformOvalToPolygon( aBuffer, seg.A, seg.B, width, aMaxError, aErrorLoc );
            }
        }
    }
}


bool PCB_TEXTBOX::IsBorderEnabled() const
{
    return m_borderEnabled;
}


void PCB_TEXTBOX::SetBorderEnabled( bool enabled )
{
    m_borderEnabled = enabled;
}


void PCB_TEXTBOX::SetBorderWidth( const int aSize )
{
    m_stroke.SetWidth( aSize );
}


bool PCB_TEXTBOX::operator==( const BOARD_ITEM& aBoardItem ) const
{
    if( aBoardItem.Type() != Type() )
        return false;

    const PCB_TEXTBOX& other = static_cast<const PCB_TEXTBOX&>( aBoardItem );

    return *this == other;
}


bool PCB_TEXTBOX::operator==( const PCB_TEXTBOX& aOther ) const
{
    return m_borderEnabled == aOther.m_borderEnabled
            && EDA_TEXT::operator==( aOther );
}


double PCB_TEXTBOX::Similarity( const BOARD_ITEM& aBoardItem ) const
{
    if( aBoardItem.Type() != Type() )
        return 0.0;

    const PCB_TEXTBOX& other = static_cast<const PCB_TEXTBOX&>( aBoardItem );

    double similarity = 1.0;

    if( m_borderEnabled != other.m_borderEnabled )
        similarity *= 0.9;

    if( GetMarginLeft() != other.GetMarginLeft() )
        similarity *= 0.9;

    if( GetMarginTop() != other.GetMarginTop() )
        similarity *= 0.9;

    if( GetMarginRight() != other.GetMarginRight() )
        similarity *= 0.9;

    if( GetMarginBottom() != other.GetMarginBottom() )
        similarity *= 0.9;

    similarity *= EDA_TEXT::Similarity( other );

    return similarity;
}


static struct PCB_TEXTBOX_DESC
{
    PCB_TEXTBOX_DESC()
    {
        ENUM_MAP<LINE_STYLE>& lineStyleEnum = ENUM_MAP<LINE_STYLE>::Instance();

        if( lineStyleEnum.Choices().GetCount() == 0 )
        {
            lineStyleEnum.Map( LINE_STYLE::SOLID, _HKI( "Solid" ) )
                         .Map( LINE_STYLE::DASH, _HKI( "Dashed" ) )
                         .Map( LINE_STYLE::DOT, _HKI( "Dotted" ) )
                         .Map( LINE_STYLE::DASHDOT, _HKI( "Dash-Dot" ) )
                         .Map( LINE_STYLE::DASHDOTDOT, _HKI( "Dash-Dot-Dot" ) );
        }

        PROPERTY_MANAGER& propMgr = PROPERTY_MANAGER::Instance();
        REGISTER_TYPE( PCB_TEXTBOX );
        propMgr.AddTypeCast( new TYPE_CAST<PCB_TEXTBOX, PCB_SHAPE> );
        propMgr.AddTypeCast( new TYPE_CAST<PCB_TEXTBOX, EDA_TEXT> );
        propMgr.InheritsAfter( TYPE_HASH( PCB_TEXTBOX ), TYPE_HASH( PCB_SHAPE ) );
        propMgr.InheritsAfter( TYPE_HASH( PCB_TEXTBOX ), TYPE_HASH( EDA_TEXT ) );

        propMgr.Mask( TYPE_HASH( PCB_TEXTBOX ), TYPE_HASH( EDA_SHAPE ), _HKI( "Shape" ) );
        propMgr.Mask( TYPE_HASH( PCB_TEXTBOX ), TYPE_HASH( EDA_SHAPE ), _HKI( "Start X" ) );
        propMgr.Mask( TYPE_HASH( PCB_TEXTBOX ), TYPE_HASH( EDA_SHAPE ), _HKI( "Start Y" ) );
        propMgr.Mask( TYPE_HASH( PCB_TEXTBOX ), TYPE_HASH( EDA_SHAPE ), _HKI( "End X" ) );
        propMgr.Mask( TYPE_HASH( PCB_TEXTBOX ), TYPE_HASH( EDA_SHAPE ), _HKI( "End Y" ) );
        propMgr.Mask( TYPE_HASH( PCB_TEXTBOX ), TYPE_HASH( EDA_SHAPE ), _HKI( "Width" ) );
        propMgr.Mask( TYPE_HASH( PCB_TEXTBOX ), TYPE_HASH( EDA_SHAPE ), _HKI( "Height" ) );
        propMgr.Mask( TYPE_HASH( PCB_TEXTBOX ), TYPE_HASH( EDA_SHAPE ), _HKI( "Line Width" ) );
        propMgr.Mask( TYPE_HASH( PCB_TEXTBOX ), TYPE_HASH( EDA_SHAPE ), _HKI( "Line Style" ) );
        propMgr.Mask( TYPE_HASH( PCB_TEXTBOX ), TYPE_HASH( EDA_SHAPE ), _HKI( "Filled" ) );
        propMgr.Mask( TYPE_HASH( PCB_TEXTBOX ), TYPE_HASH( EDA_TEXT ), _HKI( "Color" ) );

        const wxString borderProps = _( "Border Properties" );

        void ( PCB_TEXTBOX::*lineStyleSetter )( LINE_STYLE ) = &PCB_TEXTBOX::SetLineStyle;
        LINE_STYLE ( PCB_TEXTBOX::*lineStyleGetter )() const = &PCB_TEXTBOX::GetLineStyle;

        propMgr.AddProperty( new PROPERTY<PCB_TEXTBOX, bool>( _HKI( "Border" ),
                    &PCB_TEXTBOX::SetBorderEnabled, &PCB_TEXTBOX::IsBorderEnabled ),
                borderProps );

        propMgr.AddProperty( new PROPERTY_ENUM<PCB_TEXTBOX, LINE_STYLE>( _HKI( "Border Style" ),
                    lineStyleSetter, lineStyleGetter ),
                borderProps );

        propMgr.AddProperty( new PROPERTY<PCB_TEXTBOX, int>( _HKI( "Border Width" ),
                    &PCB_TEXTBOX::SetBorderWidth, &PCB_TEXTBOX::GetBorderWidth,
                    PROPERTY_DISPLAY::PT_SIZE ),
                borderProps );

        const wxString marginProps = _( "Margins" );

        propMgr.AddProperty( new PROPERTY<PCB_TEXTBOX, int>( _HKI( "Margin Left" ),
                    &PCB_TEXTBOX::SetMarginLeft, &PCB_TEXTBOX::GetMarginLeft,
                    PROPERTY_DISPLAY::PT_SIZE ),
                marginProps );
        propMgr.AddProperty( new PROPERTY<PCB_TEXTBOX, int>( _HKI( "Margin Top" ),
                    &PCB_TEXTBOX::SetMarginTop, &PCB_TEXTBOX::GetMarginTop,
                    PROPERTY_DISPLAY::PT_SIZE ),
                marginProps );
        propMgr.AddProperty( new PROPERTY<PCB_TEXTBOX, int>( _HKI( "Margin Right" ),
                    &PCB_TEXTBOX::SetMarginRight, &PCB_TEXTBOX::GetMarginRight,
                    PROPERTY_DISPLAY::PT_SIZE ),
                marginProps );
        propMgr.AddProperty( new PROPERTY<PCB_TEXTBOX, int>( _HKI( "Margin Bottom" ),
                    &PCB_TEXTBOX::SetMarginBottom, &PCB_TEXTBOX::GetMarginBottom,
                    PROPERTY_DISPLAY::PT_SIZE ),
                marginProps );

        propMgr.Mask( TYPE_HASH( PCB_TEXT ), TYPE_HASH( EDA_TEXT ), _HKI( "Hyperlink" ) );
    }
} _PCB_TEXTBOX_DESC;