KiCad PCB EDA Suite
ds_painter.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 (C) 1992-2020 KiCad Developers, see AUTHORS.txt for contributors.
5 *
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, you may find one here:
19 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
20 * or you may search the http://www.gnu.org website for the version 2 license,
21 * or you may write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23 */
24
25
26#include <common.h>
27#include <pgm_base.h>
28#include <base_screen.h>
29#include <eda_draw_frame.h>
30#include <title_block.h>
31#include <build_version.h>
35
38
39#include <wx/app.h>
40
41using namespace KIGFX;
42
43static const wxString productName = wxT( "KiCad E.D.A. " );
44
45DS_RENDER_SETTINGS::DS_RENDER_SETTINGS()
46{
47 m_backgroundColor = COLOR4D( 1.0, 1.0, 1.0, 1.0 );
50 m_brightenedColor = COLOR4D( 0.0, 1.0, 0.0, 0.9 );
51 m_pageBorderColor = COLOR4D( 0.4, 0.4, 0.4, 1.0 );
52
53 update();
54}
55
56
58{
59 for( int layer = SCH_LAYER_ID_START; layer < SCH_LAYER_ID_END; layer ++)
60 m_layerColors[ layer ] = aSettings->GetColor( layer );
61
62 for( int layer = GAL_LAYER_ID_START; layer < GAL_LAYER_ID_END; layer ++)
63 m_layerColors[ layer ] = aSettings->GetColor( layer );
64
68}
69
70
71COLOR4D DS_RENDER_SETTINGS::GetColor( const VIEW_ITEM* aItem, int aLayer ) const
72{
73 const EDA_ITEM* item = dynamic_cast<const EDA_ITEM*>( aItem );
74
75 if( item )
76 {
77 // Selection disambiguation
78 if( item->IsBrightened() )
79 return m_brightenedColor;
80
81 if( item->IsSelected() )
82 return m_selectedColor;
83
84 if( item->Type() == WSG_TEXT_T )
85 {
86 COLOR4D color = static_cast<const DS_DRAW_ITEM_TEXT*>( item )->GetTextColor();
87
89 return color;
90 }
91 }
92
93 return m_normalColor;
94}
95
96
97void DS_DRAW_ITEM_LIST::GetTextVars( wxArrayString* aVars )
98{
99 aVars->push_back( wxT( "KICAD_VERSION" ) );
100 aVars->push_back( wxT( "#" ) );
101 aVars->push_back( wxT( "##" ) );
102 aVars->push_back( wxT( "SHEETNAME" ) );
103 aVars->push_back( wxT( "SHEETPATH" ) );
104 aVars->push_back( wxT( "FILENAME" ) );
105 aVars->push_back( wxT( "PAPER" ) );
106 aVars->push_back( wxT( "LAYER" ) );
108}
109
110
111// Returns the full text corresponding to the aTextbase, after replacing any text variable
112// references.
113wxString DS_DRAW_ITEM_LIST::BuildFullText( const wxString& aTextbase )
114{
115 std::function<bool( wxString* )> wsResolver =
116 [ this ]( wxString* token ) -> bool
117 {
118 bool tokenUpdated = false;
119
120 if( token->IsSameAs( wxT( "KICAD_VERSION" ) ) && PgmOrNull() )
121 {
122 // TODO: it'd be nice to get the Python script name/version here for when
123 // PgmOrNull() is null...
124
125 *token = wxString::Format( wxT( "%s%s %s" ),
127 Pgm().App().GetAppName(),
128 GetBuildVersion() );
129 tokenUpdated = true;
130 }
131 else if( token->IsSameAs( wxT( "#" ) ) )
132 {
133 *token = wxString::Format( wxT( "%s" ), m_pageNumber );
134 tokenUpdated = true;
135 }
136 else if( token->IsSameAs( wxT( "##" ) ) )
137 {
138 *token = wxString::Format( wxT( "%d" ), m_sheetCount );
139 tokenUpdated = true;
140 }
141 else if( token->IsSameAs( wxT( "SHEETNAME" ) ) )
142 {
143 *token = m_sheetName;
144 tokenUpdated = true;
145 }
146 else if( token->IsSameAs( wxT( "SHEETPATH" ) ) )
147 {
148 *token = m_sheetPath;
149 tokenUpdated = true;
150 }
151 else if( token->IsSameAs( wxT( "FILENAME" ) ) )
152 {
153 wxFileName fn( m_fileName );
154 *token = fn.GetFullName();
155 tokenUpdated = true;
156 }
157 else if( token->IsSameAs( wxT( "PAPER" ) ) )
158 {
159 *token = m_paperFormat ? *m_paperFormat : wxString( "" );
160 tokenUpdated = true;
161 }
162 else if( token->IsSameAs( wxT( "LAYER" ) ) )
163 {
164 *token = m_sheetLayer ? *m_sheetLayer : wxString( "" );
165 tokenUpdated = true;
166 }
167 else if( m_titleBlock )
168 {
169 // no need for tokenUpdated; TITLE_BLOCK::TextVarResolver() does a full
170 // resolve
171 return m_titleBlock->TextVarResolver( token, m_project );
172 }
173 else if( m_properties && m_properties->count( *token ) )
174 {
175 *token = m_properties->at( *token );
176 tokenUpdated = true;
177 }
178
179 if( tokenUpdated )
180 {
181 *token = ExpandTextVars( *token, m_project );
182 return true;
183 }
184
185 return false;
186 };
187
188 return ExpandTextVars( aTextbase, &wsResolver, nullptr, m_project );
189}
190
191
192bool KIGFX::DS_PAINTER::Draw( const VIEW_ITEM* aItem, int aLayer )
193{
194 auto item = dynamic_cast<const EDA_ITEM*>( aItem );
195
196 if( !item )
197 return false;
198
199 switch( item->Type() )
200 {
201 case WSG_LINE_T: draw( (DS_DRAW_ITEM_LINE*) item, aLayer ); break;
202 case WSG_POLY_T: draw( (DS_DRAW_ITEM_POLYPOLYGONS*) item, aLayer ); break;
203 case WSG_RECT_T: draw( (DS_DRAW_ITEM_RECT*) item, aLayer ); break;
204 case WSG_TEXT_T: draw( (DS_DRAW_ITEM_TEXT*) item, aLayer ); break;
205 case WSG_BITMAP_T: draw( (DS_DRAW_ITEM_BITMAP*) item, aLayer ); break;
206 case WSG_PAGE_T: draw( (DS_DRAW_ITEM_PAGE*) item, aLayer ); break;
207 default: return false;
208 }
209
210 return true;
211}
212
213
214void KIGFX::DS_PAINTER::draw( const DS_DRAW_ITEM_LINE* aItem, int aLayer ) const
215{
216 m_gal->SetIsStroke( true );
217 m_gal->SetIsFill( false );
218 m_gal->SetStrokeColor( m_renderSettings.GetColor( aItem, aLayer ) );
219 m_gal->SetLineWidth( std::max( aItem->GetPenWidth(), m_renderSettings.GetDefaultPenWidth() ) );
220 m_gal->DrawLine( VECTOR2D( aItem->GetStart() ), VECTOR2D( aItem->GetEnd() ) );
221}
222
223
224void KIGFX::DS_PAINTER::draw( const DS_DRAW_ITEM_RECT* aItem, int aLayer ) const
225{
226 m_gal->SetIsStroke( true );
227 m_gal->SetIsFill( false );
228 m_gal->SetStrokeColor( m_renderSettings.GetColor( aItem, aLayer ) );
229 m_gal->SetLineWidth( std::max( aItem->GetPenWidth(), m_renderSettings.GetDefaultPenWidth() ) );
230 m_gal->DrawRectangle( VECTOR2D( aItem->GetStart() ), VECTOR2D( aItem->GetEnd() ) );
231}
232
233
234void KIGFX::DS_PAINTER::draw( const DS_DRAW_ITEM_POLYPOLYGONS* aItem, int aLayer ) const
235{
236 m_gal->SetFillColor( m_renderSettings.GetColor( aItem, aLayer ) );
237 m_gal->SetIsFill( true );
238 m_gal->SetIsStroke( false );
239
241
242 for( int idx = 0; idx < item->GetPolygons().OutlineCount(); ++idx )
243 {
244 SHAPE_LINE_CHAIN& outline = item->GetPolygons().Outline( idx );
245 m_gal->DrawPolygon( outline );
246 }
247}
248
249
250void KIGFX::DS_PAINTER::draw( const DS_DRAW_ITEM_TEXT* aItem, int aLayer ) const
251{
252 KIFONT::FONT* font = aItem->GetFont();
253
254 if( !font )
255 {
256 font = KIFONT::FONT::GetFont( m_renderSettings.GetDefaultFont(), aItem->IsBold(),
257 aItem->IsItalic() );
258 }
259
260 const COLOR4D& color = m_renderSettings.GetColor( aItem, aLayer );
261
262 m_gal->SetStrokeColor( color );
263 m_gal->SetFillColor( color );
264
265 TEXT_ATTRIBUTES attrs = aItem->GetAttributes();
266 attrs.m_StrokeWidth = std::max( aItem->GetEffectiveTextPenWidth(),
267 m_renderSettings.GetDefaultPenWidth() );
268
269 font->Draw( m_gal, aItem->GetShownText(), aItem->GetTextPos(), attrs );
270}
271
272
273void KIGFX::DS_PAINTER::draw( const DS_DRAW_ITEM_BITMAP* aItem, int aLayer ) const
274{
275 m_gal->Save();
276 auto* bitmap = static_cast<DS_DATA_ITEM_BITMAP*>( aItem->GetPeer() );
277
278 VECTOR2D position = aItem->GetPosition();
279 m_gal->Translate( position );
280
281 // When the image scale factor is not 1.0, we need to modify the actual scale
282 // as the image scale factor is similar to a local zoom
283 double img_scale = bitmap->m_ImageBitmap->GetScale();
284
285 if( img_scale != 1.0 )
286 m_gal->Scale( VECTOR2D( img_scale, img_scale ) );
287
288 m_gal->DrawBitmap( *bitmap->m_ImageBitmap );
289
290#if 0 // For bounding box debug purpose only
291 BOX2I bbox = aItem->GetBoundingBox();
292 m_gal->SetIsFill( true );
293 m_gal->SetIsStroke( true );
294 m_gal->SetFillColor( COLOR4D( 1, 1, 1, 0.4 ) );
295 m_gal->SetStrokeColor( COLOR4D( 0, 0, 0, 1 ) );
296
297 if( img_scale != 1.0 )
298 m_gal->Scale( VECTOR2D( 1.0, 1.0 ) );
299
300 m_gal->DrawRectangle( VECTOR2D( bbox.GetOrigin() ) - position,
301 VECTOR2D( bbox.GetEnd() ) - position );
302#endif
303
304 m_gal->Restore();
305}
306
307
308void KIGFX::DS_PAINTER::draw( const DS_DRAW_ITEM_PAGE* aItem, int aLayer ) const
309{
310 VECTOR2D origin = VECTOR2D( 0.0, 0.0 );
311 VECTOR2D end = VECTOR2D( aItem->GetPageSize().x,
312 aItem->GetPageSize().y );
313
314 m_gal->SetIsStroke( true );
315
316 // Use a gray color for the border color
317 m_gal->SetStrokeColor( m_renderSettings.m_pageBorderColor );
318 m_gal->SetIsFill( false );
319 m_gal->SetLineWidth( m_renderSettings.GetDefaultPenWidth() );
320
321 m_gal->DrawRectangle( origin, end );
322
323 // Draw the corner marker
324 double marker_size = aItem->GetMarkerSize();
325
326 m_gal->SetStrokeColor( m_renderSettings.m_pageBorderColor );
327 VECTOR2D pos = VECTOR2D( aItem->GetMarkerPos().x, aItem->GetMarkerPos().y );
328
329 // Draw a circle and a X
330 m_gal->DrawCircle( pos, marker_size );
331 m_gal->DrawLine( VECTOR2D( pos.x - marker_size, pos.y - marker_size),
332 VECTOR2D( pos.x + marker_size, pos.y + marker_size ) );
333 m_gal->DrawLine( VECTOR2D( pos.x + marker_size, pos.y - marker_size),
334 VECTOR2D( pos.x - marker_size, pos.y + marker_size ) );
335}
336
337
338void KIGFX::DS_PAINTER::DrawBorder( const PAGE_INFO* aPageInfo, int aScaleFactor ) const
339{
340 VECTOR2D origin = VECTOR2D( 0.0, 0.0 );
341 VECTOR2D end = VECTOR2D( aPageInfo->GetWidthMils() * aScaleFactor,
342 aPageInfo->GetHeightMils() * aScaleFactor );
343
344 m_gal->SetIsStroke( true );
345 // Use a gray color for the border color
346 m_gal->SetStrokeColor( m_renderSettings.m_pageBorderColor );
347 m_gal->SetIsFill( false );
348 m_gal->DrawRectangle( origin, end );
349}
int color
Definition: DXF_plotter.cpp:57
BASE_SCREEN class implementation.
wxString GetBuildVersion()
Get the full KiCad version string.
const Vec & GetOrigin() const
Definition: box2.h:183
const Vec GetEnd() const
Definition: box2.h:185
Color settings are a bit different than most of the settings objects in that there can be more than o...
COLOR4D GetColor(int aLayer) const
virtual int GetPenWidth() const
Definition: ds_draw_item.h:70
DS_DATA_ITEM * GetPeer() const
Definition: ds_draw_item.h:63
VECTOR2I GetPosition() const override
Definition: ds_draw_item.h:357
const BOX2I GetBoundingBox() const override
Return the orthogonal bounding box of this object for display purposes.
const VECTOR2I & GetStart() const
Definition: ds_draw_item.h:133
const VECTOR2I & GetEnd() const
Definition: ds_draw_item.h:135
static void GetTextVars(wxArrayString *aVars)
Definition: ds_painter.cpp:97
wxString BuildFullText(const wxString &aTextbase)
Definition: ds_painter.cpp:113
A rectangle with thick segment showing the page limits and a marker showing the coordinate origin.
Definition: ds_draw_item.h:256
const VECTOR2I & GetMarkerPos() const
Definition: ds_draw_item.h:270
double GetMarkerSize() const
Definition: ds_draw_item.h:273
VECTOR2I GetPageSize() const
Definition: ds_draw_item.h:268
SHAPE_POLY_SET & GetPolygons()
Definition: ds_draw_item.h:171
Non filled rectangle with thick segment.
Definition: ds_draw_item.h:208
const VECTOR2I & GetEnd() const
Definition: ds_draw_item.h:223
const VECTOR2I & GetStart() const
Definition: ds_draw_item.h:221
A graphic text.
Definition: ds_draw_item.h:303
A base class for most all the KiCad significant classes used in schematics and boards.
Definition: eda_item.h:85
KICAD_T Type() const
Returns the type of object.
Definition: eda_item.h:97
bool IsSelected() const
Definition: eda_item.h:107
bool IsBrightened() const
Definition: eda_item.h:110
const VECTOR2I & GetTextPos() const
Definition: eda_text.h:208
bool IsItalic() const
Definition: eda_text.h:130
KIFONT::FONT * GetFont() const
Definition: eda_text.h:188
const TEXT_ATTRIBUTES & GetAttributes() const
Definition: eda_text.h:172
int GetEffectiveTextPenWidth(int aDefaultPenWidth=0) const
The EffectiveTextPenWidth uses the text thickness if > 1 or aDefaultPenWidth.
Definition: eda_text.cpp:297
bool IsBold() const
Definition: eda_text.h:133
virtual wxString GetShownText(int aDepth=0, bool aAllowExtraText=true) const
Return the string actually shown after processing of the base text.
Definition: eda_text.h:98
FONT is an abstract base class for both outline and stroke fonts.
Definition: font.h:105
static FONT * GetFont(const wxString &aFontName=wxEmptyString, bool aBold=false, bool aItalic=false)
Definition: font.cpp:65
void Draw(KIGFX::GAL *aGal, const wxString &aText, const VECTOR2I &aPosition, const VECTOR2I &aCursor, const TEXT_ATTRIBUTES &aAttrs) const
Draw a string.
Definition: font.cpp:159
A color representation with 4 components: red, green, blue, alpha.
Definition: color4d.h:104
COLOR4D Brightened(double aFactor) const
Return a color that is brighter by a given factor, without modifying object.
Definition: color4d.h:278
static const COLOR4D UNSPECIFIED
For legacy support; used as a value to indicate color hasn't been set yet.
Definition: color4d.h:390
void DrawBorder(const PAGE_INFO *aPageInfo, int aScaleFactor) const
Definition: ds_painter.cpp:338
void draw(const DS_DRAW_ITEM_LINE *aItem, int aLayer) const
Definition: ds_painter.cpp:214
virtual bool Draw(const VIEW_ITEM *, int) override
Takes an instance of VIEW_ITEM and passes it to a function that knows how to draw the item.
Definition: ds_painter.cpp:192
void LoadColors(const COLOR_SETTINGS *aSettings) override
Definition: ds_painter.cpp:57
virtual COLOR4D GetColor(const VIEW_ITEM *aItem, int aLayer) const override
Returns the color that should be used to draw the specific VIEW_ITEM on the specific layer using curr...
Definition: ds_painter.cpp:71
virtual void update()
Precalculates extra colors for layers (e.g.
COLOR4D m_layerColors[LAYER_ID_COUNT]
An abstract base class for deriving all objects that can be added to a VIEW.
Definition: view_item.h:77
Describe the page size and margins of a paper page on which to eventually print or plot.
Definition: page_info.h:54
int GetHeightMils() const
Definition: page_info.h:133
int GetWidthMils() const
Definition: page_info.h:130
Represent a polyline containing arcs as well as line segments: A chain of connected line and/or arc s...
SHAPE_LINE_CHAIN & Outline(int aIndex)
int OutlineCount() const
Return the number of vertices in a given outline/hole.
static void GetContextualTextVars(wxArrayString *aVars)
Definition: title_block.cpp:74
@ RED
Definition: color4d.h:59
wxString ExpandTextVars(const wxString &aSource, const PROJECT *aProject)
Definition: common.cpp:57
The common library.
PGM_BASE * PgmOrNull()
similar to PGM_BASE& Pgm(), but return a reference that can be nullptr when running a shared lib from...
Definition: cvpcb.cpp:125
static const wxString productName
Definition: ds_painter.cpp:43
@ GAL_LAYER_ID_START
Definition: layer_ids.h:191
@ GAL_LAYER_ID_END
Definition: layer_ids.h:260
@ SCH_LAYER_ID_END
Definition: layer_ids.h:388
@ SCH_LAYER_ID_START
Definition: layer_ids.h:342
@ LAYER_SCHEMATIC_DRAWINGSHEET
Definition: layer_ids.h:382
@ LAYER_SCHEMATIC_BACKGROUND
Definition: layer_ids.h:376
@ LAYER_SCHEMATIC_GRID
Definition: layer_ids.h:374
The Cairo implementation of the graphics abstraction layer.
Definition: color4d.cpp:266
see class PGM_BASE
void Format(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, const CPTREE &aTree)
Output a PTREE into s-expression format via an OUTPUTFORMATTER derivative.
Definition: ptree.cpp:200
KIWAY Kiway & Pgm(), KFCTL_STANDALONE
The global Program "get" accessor.
Definition: single_top.cpp:111
@ WSG_POLY_T
Definition: typeinfo.h:222
@ WSG_LINE_T
Definition: typeinfo.h:220
@ WSG_TEXT_T
Definition: typeinfo.h:223
@ WSG_PAGE_T
Definition: typeinfo.h:225
@ WSG_RECT_T
Definition: typeinfo.h:221
@ WSG_BITMAP_T
Definition: typeinfo.h:224
VECTOR2< double > VECTOR2D
Definition: vector2d.h:617