KiCad PCB EDA Suite
Loading...
Searching...
No Matches
line_ending_bitmap.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, see AUTHORS.txt for contributors.
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 2
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, you may find one here:
18 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
19 * or you may search the http://www.gnu.org website for the version 2 license,
20 * or you may write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22 */
23
25#include <line_ending.h>
26#include <geometry/eda_angle.h>
27#include <math/vector2d.h>
28#include <wx/dcmemory.h>
29#include <wx/window.h>
30#include <algorithm>
31
32
33wxBitmap MakeLineEndingBitmap( LINE_ENDING_STYLE aStyle, const wxSize& aSize,
34 const wxColour& aForeground, const wxColour& aBackground,
35 wxWindow* aWindow, bool aShapeOnRight )
36{
37 double scaleFactor = aWindow->GetDPIScaleFactor();
38
39 wxSize physSize( aWindow->ToPhys( aSize.GetWidth() ),
40 aWindow->ToPhys( aSize.GetHeight() ) );
41
42 wxBitmap bitmap( physSize );
43 wxMemoryDC dc( bitmap );
44
45 dc.SetBackground( wxBrush( aBackground ) );
46 dc.Clear();
47
48 int w = physSize.GetWidth();
49 int h = physSize.GetHeight();
50 int midY = h / 2;
51 int marginL = static_cast<int>( 4 * scaleFactor );
52 int marginR = static_cast<int>( 4 * scaleFactor );
53
54 int lineStartX = marginL;
55 int lineEndX = w - marginR;
56 int lineDrawStartX = lineStartX;
57 int lineDrawEndX = lineEndX;
58
59 if( aStyle != LINE_ENDING_STYLE::NONE )
60 {
61 int shapeHeight = h - static_cast<int>( 4 * scaleFactor );
62 int fakeLineWidth = std::max( 1, static_cast<int>( shapeHeight / LINE_ENDING::DEFAULT_RATIO_WIDTH ) );
63
64 LINE_ENDING ending( aStyle );
65
66 if( aStyle == LINE_ENDING_STYLE::ARROW_OPEN )
67 {
68 int len = static_cast<int>( fakeLineWidth * LINE_ENDING::DEFAULT_RATIO_LENGTH * 0.75 );
69 int wid = static_cast<int>( fakeLineWidth * LINE_ENDING::DEFAULT_RATIO_WIDTH * 1.2 );
70 ending = LINE_ENDING( aStyle, len, wid );
71 }
72
73 VECTOR2I tip;
74 EDA_ANGLE tangent;
75
76 if( aShapeOnRight )
77 {
78 tip = VECTOR2I( lineEndX, midY );
79 tangent = EDA_ANGLE( 0.0, DEGREES_T );
80 }
81 else
82 {
83 tip = VECTOR2I( lineStartX, midY );
84 tangent = EDA_ANGLE( 180.0, DEGREES_T );
85 }
86
87 std::vector<VECTOR2I> polygon;
88 ending.GetShapes( tip, tangent, fakeLineWidth, polygon );
89
90 if( !polygon.empty() )
91 {
92 std::vector<wxPoint> wxPts;
93 wxPts.reserve( polygon.size() );
94
95 for( const VECTOR2I& pt : polygon )
96 wxPts.emplace_back( pt.x, pt.y );
97
98 int shortenDepth = ending.GetShortenDepth( fakeLineWidth );
99
100 if( aShapeOnRight )
101 lineDrawEndX = lineEndX - shortenDepth;
102 else
103 lineDrawStartX = lineStartX + shortenDepth;
104
105 dc.SetPen( wxPen( aForeground, static_cast<int>( scaleFactor ) ) );
106
107 if( aStyle == LINE_ENDING_STYLE::ARROW_OPEN )
108 {
109 dc.SetBrush( *wxTRANSPARENT_BRUSH );
110 dc.SetPen( wxPen( aForeground, std::max( 1, static_cast<int>( 1.5 * scaleFactor ) ) ) );
111 dc.DrawLines( static_cast<int>( wxPts.size() ), wxPts.data() );
112 }
113 else
114 {
115 dc.SetBrush( wxBrush( aForeground ) );
116 dc.DrawPolygon( static_cast<int>( wxPts.size() ), wxPts.data() );
117 }
118 }
119 }
120
121 dc.SetPen( wxPen( aForeground, static_cast<int>( scaleFactor ) ) );
122 dc.DrawLine( lineDrawStartX, midY, lineDrawEndX, midY );
123
124 dc.SelectObject( wxNullBitmap );
125 bitmap.SetScaleFactor( scaleFactor );
126 return bitmap;
127}
Decorative shape (arrowhead, circle, square) at the start or end of a graphic line,...
Definition line_ending.h:62
static constexpr double DEFAULT_RATIO_LENGTH
int GetShortenDepth(int aLineWidth) const
Return how far the line should be shortened at this ending.
static constexpr double DEFAULT_RATIO_WIDTH
void GetShapes(const VECTOR2I &aPoint, const EDA_ANGLE &aTangent, int aLineWidth, std::vector< VECTOR2I > &aPolygon) const
Generate ending geometry as polygon vertices at the given point and direction.
@ DEGREES_T
Definition eda_angle.h:31
LINE_ENDING_STYLE
Line ending styles for graphic lines, arcs, and beziers.
Definition line_ending.h:44
wxBitmap MakeLineEndingBitmap(LINE_ENDING_STYLE aStyle, const wxSize &aSize, const wxColour &aForeground, const wxColour &aBackground, wxWindow *aWindow, bool aShapeOnRight)
Generate a DPI-aware bitmap icon showing a line ending style.
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:683