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, const wxColour& aForeground,
34 wxWindow* aWindow, bool aShapeOnRight )
35{
36 double scaleFactor = aWindow->GetDPIScaleFactor();
37 wxSize physSize( aWindow->ToPhys( aSize.GetWidth() ), aWindow->ToPhys( aSize.GetHeight() ) );
38 wxBitmap bitmap( physSize );
39
40 bitmap.UseAlpha();
41
42 wxMemoryDC dc( bitmap );
43
44 dc.SetBackground( *wxTRANSPARENT_BRUSH );
45 dc.Clear();
46
47 int w = physSize.GetWidth();
48 int h = physSize.GetHeight();
49 int midY = h / 2;
50 int marginL = KiROUND( h / 2.0 ); // Use font height to calculate left/right margins
51 int marginR = KiROUND( h / 2.0 );
52 int marginT = KiROUND( h / 6.0 ); // ... and for top/bottom as well
53 int marginB = KiROUND( h / 6.0 );
54
55 int lineStartX = marginL;
56 int lineEndX = w - marginR;
57 int lineDrawStartX = lineStartX;
58 int lineDrawEndX = lineEndX;
59
60 if( aStyle != LINE_ENDING_STYLE::NONE )
61 {
62 int shapeHeight = h - marginT - marginB;
63 int fakeLineWidth = std::max( 1, static_cast<int>( shapeHeight / LINE_ENDING::DEFAULT_RATIO_WIDTH ) );
64
65 LINE_ENDING ending( aStyle );
66
67 if( aStyle == LINE_ENDING_STYLE::ARROW_OPEN )
68 {
69 int len = static_cast<int>( fakeLineWidth * LINE_ENDING::DEFAULT_RATIO_LENGTH * 0.75 );
70 int wid = static_cast<int>( fakeLineWidth * LINE_ENDING::DEFAULT_RATIO_WIDTH * 1.2 );
71 ending = LINE_ENDING( aStyle, len, wid );
72 }
73
74 VECTOR2I tip;
75 EDA_ANGLE tangent;
76
77 if( aShapeOnRight )
78 {
79 tip = VECTOR2I( lineEndX, midY );
80 tangent = EDA_ANGLE( 0.0, DEGREES_T );
81 }
82 else
83 {
84 tip = VECTOR2I( lineStartX, midY );
85 tangent = EDA_ANGLE( 180.0, DEGREES_T );
86 }
87
88 std::vector<VECTOR2I> polygon;
89 ending.GetShapes( tip, tangent, fakeLineWidth, polygon );
90
91 if( !polygon.empty() )
92 {
93 std::vector<wxPoint> wxPts;
94 wxPts.reserve( polygon.size() );
95
96 for( const VECTOR2I& pt : polygon )
97 wxPts.emplace_back( pt.x, pt.y );
98
99 int shortenDepth = ending.GetShortenDepth( fakeLineWidth );
100
101 if( aShapeOnRight )
102 lineDrawEndX = lineEndX - shortenDepth;
103 else
104 lineDrawStartX = lineStartX + shortenDepth;
105
106 dc.SetPen( wxPen( aForeground, static_cast<int>( scaleFactor ) ) );
107
108 if( aStyle == LINE_ENDING_STYLE::ARROW_OPEN )
109 {
110 dc.SetBrush( *wxTRANSPARENT_BRUSH );
111 dc.SetPen( wxPen( aForeground, std::max( 1, static_cast<int>( 1.5 * scaleFactor ) ) ) );
112 dc.DrawLines( static_cast<int>( wxPts.size() ), wxPts.data() );
113 }
114 else
115 {
116 dc.SetBrush( wxBrush( aForeground ) );
117 dc.DrawPolygon( static_cast<int>( wxPts.size() ), wxPts.data() );
118 }
119 }
120 }
121
122 dc.SetPen( wxPen( aForeground, static_cast<int>( scaleFactor ) ) );
123 dc.DrawLine( lineDrawStartX, midY, lineDrawEndX, midY );
124
125 dc.SelectObject( wxNullBitmap );
126 bitmap.SetScaleFactor( scaleFactor );
127 return bitmap;
128}
constexpr BOX2I KiROUND(const BOX2D &aBoxD)
Definition box2.h:982
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:30
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, wxWindow *aWindow, bool aShapeOnRight)
Generate a DPI-aware bitmap icon showing a line ending style.
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:708