KiCad PCB EDA Suite
Loading...
Searching...
No Matches
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 The 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>
36
39
40#include <wx/app.h>
41
42using namespace KIGFX;
43
44static const wxString productName = wxT( "KiCad E.D.A." );
45
47{
48 m_backgroundColor = COLOR4D( 1.0, 1.0, 1.0, 1.0 );
50 m_selectedColor = m_normalColor.Brightened( 0.5 );
51 m_brightenedColor = COLOR4D( 0.0, 1.0, 0.0, 0.9 );
52 m_pageBorderColor = COLOR4D( 0.4, 0.4, 0.4, 1.0 );
53
54 update();
55}
56
57
59{
60 for( int layer = SCH_LAYER_ID_START; layer < SCH_LAYER_ID_END; layer++ )
61 m_layerColors[ layer ] = aSettings->GetColor( layer );
62
63 for( int layer = GAL_LAYER_ID_START; layer < GAL_LAYER_ID_END; layer++ )
64 m_layerColors[ layer ] = aSettings->GetColor( layer );
65
69}
70
71
72COLOR4D DS_RENDER_SETTINGS::GetColor( const VIEW_ITEM* aItem, int aLayer ) const
73{
74 const EDA_ITEM* item = dynamic_cast<const EDA_ITEM*>( aItem );
75
76 if( item )
77 {
78 // Selection disambiguation
79 if( item->IsBrightened() )
80 return m_brightenedColor;
81
82 if( item->IsSelected() )
83 return m_selectedColor;
84
85 if( item->Type() == WSG_TEXT_T )
86 {
87 COLOR4D color = static_cast<const DS_DRAW_ITEM_TEXT*>( item )->GetTextColor();
88
89 if( color != COLOR4D::UNSPECIFIED )
90 return color;
91 }
92 }
93
94 return m_normalColor;
95}
96
97
98void DS_DRAW_ITEM_LIST::GetTextVars( wxArrayString* aVars )
99{
100 aVars->push_back( wxT( "KICAD_VERSION" ) );
101 aVars->push_back( wxT( "#" ) );
102 aVars->push_back( wxT( "##" ) );
103 aVars->push_back( wxT( "SHEETNAME" ) );
104 aVars->push_back( wxT( "SHEETPATH" ) );
105 aVars->push_back( wxT( "FILENAME" ) );
106 aVars->push_back( wxT( "FILEPATH" ) );
107 aVars->push_back( wxT( "PROJECTNAME" ) );
108 aVars->push_back( wxT( "PAPER" ) );
109 aVars->push_back( wxT( "LAYER" ) );
110 aVars->push_back( wxT( "VARIANT" ) );
111 aVars->push_back( wxT( "VARIANT_DESC" ) );
113}
114
115
116wxString DS_DRAW_ITEM_LIST::BuildFullText( const wxString& aTextbase )
117{
118 std::function<bool( wxString* )> wsResolver =
119 [&]( wxString* token ) -> bool
120 {
121 bool tokenUpdated = false;
122
123 if( token->IsSameAs( wxT( "KICAD_VERSION" ) ) && PgmOrNull() )
124 {
125 *token = wxString::Format( wxT( "%s %s" ), productName, GetBaseVersion() );
126 tokenUpdated = true;
127 }
128 else if( token->IsSameAs( wxT( "#" ) ) )
129 {
130 *token = wxString::Format( wxT( "%s" ), m_pageNumber );
131 tokenUpdated = true;
132 }
133 else if( token->IsSameAs( wxT( "##" ) ) )
134 {
135 *token = wxString::Format( wxT( "%d" ), m_sheetCount );
136 tokenUpdated = true;
137 }
138 else if( token->IsSameAs( wxT( "SHEETNAME" ) ) )
139 {
140 *token = m_sheetName;
141 tokenUpdated = true;
142 }
143 else if( token->IsSameAs( wxT( "SHEETPATH" ) ) )
144 {
145 *token = m_sheetPath;
146 tokenUpdated = true;
147 }
148 else if( token->IsSameAs( wxT( "FILENAME" ) ) )
149 {
150 wxFileName fn( m_fileName );
151 *token = fn.GetFullName();
152 tokenUpdated = true;
153 }
154 else if( token->IsSameAs( wxT( "FILEPATH" ) ) )
155 {
156 wxFileName fn( m_fileName );
157 *token = fn.GetFullPath();
158 return true;
159 }
160 else if( token->IsSameAs( wxT( "PAPER" ) ) )
161 {
162 *token = m_paperFormat;
163 tokenUpdated = true;
164 }
165 else if( token->IsSameAs( wxT( "LAYER" ) ) )
166 {
167 *token = m_sheetLayer;
168 tokenUpdated = true;
169 }
170 else if( token->IsSameAs( wxT( "VARIANT" ) ) )
171 {
172 *token = m_variantName;
173 tokenUpdated = true;
174 }
175 else if( token->IsSameAs( wxT( "VARIANT_DESC" ) ) )
176 {
177 *token = m_variantDesc;
178 tokenUpdated = true;
179 }
180 else if( m_titleBlock )
181 {
182 if( m_titleBlock->TextVarResolver( token, m_project, m_flags ) )
183 {
184 // No need for tokenUpdated; TITLE_BLOCK::TextVarResolver() already goes
185 // up to the project.
186 //
187 // However, the title block may have variables in it itself, so we need
188 // to run the worksheet resolver again.
189 //
190 const TITLE_BLOCK* savedTitleBlock = m_titleBlock;
191
192 m_titleBlock = nullptr;
193 {
194 *token = ExpandTextVars( *token, &wsResolver, m_flags );
195 }
196 m_titleBlock = savedTitleBlock;
197
198 return true;
199 }
200 }
201 else if( m_properties && m_properties->count( *token ) )
202 {
203 *token = m_properties->at( *token );
204 tokenUpdated = true;
205 }
206
207 if( tokenUpdated )
208 {
209 *token = ExpandTextVars( *token, m_project, m_flags );
210 return true;
211 }
212
213 if( m_project && m_project->TextVarResolver( token ) )
214 return true;
215
216 return false;
217 };
218
219 wxString retv = ExpandTextVars( aTextbase, &wsResolver, m_flags );
220
221 static EXPRESSION_EVALUATOR evaluator;
222
223 if( retv.Contains( wxS( "@{" ) ) )
224 retv = evaluator.Evaluate( retv );
225
226 return retv;
227}
228
229
230bool KIGFX::DS_PAINTER::Draw( const VIEW_ITEM* aItem, int aLayer )
231{
232 auto item = dynamic_cast<const EDA_ITEM*>( aItem );
233
234 if( !item )
235 return false;
236
237 switch( item->Type() )
238 {
239 case WSG_LINE_T: draw( (DS_DRAW_ITEM_LINE*) item, aLayer ); break;
240 case WSG_POLY_T: draw( (DS_DRAW_ITEM_POLYPOLYGONS*) item, aLayer ); break;
241 case WSG_RECT_T: draw( (DS_DRAW_ITEM_RECT*) item, aLayer ); break;
242 case WSG_TEXT_T: draw( (DS_DRAW_ITEM_TEXT*) item, aLayer ); break;
243 case WSG_BITMAP_T: draw( (DS_DRAW_ITEM_BITMAP*) item, aLayer ); break;
244 case WSG_PAGE_T: draw( (DS_DRAW_ITEM_PAGE*) item, aLayer ); break;
245 default: return false;
246 }
247
248 return true;
249}
250
251
252void KIGFX::DS_PAINTER::draw( const DS_DRAW_ITEM_LINE* aItem, int aLayer ) const
253{
254 m_gal->SetIsStroke( true );
255 m_gal->SetIsFill( false );
256 m_gal->SetStrokeColor( m_renderSettings.GetColor( aItem, aLayer ) );
257 m_gal->SetLineWidth( std::max( aItem->GetPenWidth(), m_renderSettings.GetDefaultPenWidth() ) );
258 m_gal->DrawLine( VECTOR2D( aItem->GetStart() ), VECTOR2D( aItem->GetEnd() ) );
259}
260
261
262void KIGFX::DS_PAINTER::draw( const DS_DRAW_ITEM_RECT* aItem, int aLayer ) const
263{
264 m_gal->SetIsStroke( true );
265 m_gal->SetIsFill( false );
266 m_gal->SetStrokeColor( m_renderSettings.GetColor( aItem, aLayer ) );
267 m_gal->SetLineWidth( std::max( aItem->GetPenWidth(), m_renderSettings.GetDefaultPenWidth() ) );
268 m_gal->DrawRectangle( VECTOR2D( aItem->GetStart() ), VECTOR2D( aItem->GetEnd() ) );
269}
270
271
272void KIGFX::DS_PAINTER::draw( const DS_DRAW_ITEM_POLYPOLYGONS* aItem, int aLayer ) const
273{
274 m_gal->SetFillColor( m_renderSettings.GetColor( aItem, aLayer ) );
275 m_gal->SetIsFill( true );
276 m_gal->SetIsStroke( false );
277
279
280 for( int idx = 0; idx < item->GetPolygons().OutlineCount(); ++idx )
281 {
282 SHAPE_LINE_CHAIN& outline = item->GetPolygons().Outline( idx );
283 m_gal->DrawPolygon( outline );
284 }
285}
286
287
288void KIGFX::DS_PAINTER::draw( const DS_DRAW_ITEM_TEXT* aItem, int aLayer ) const
289{
290 KIFONT::FONT* font = aItem->GetFont();
291
292 if( !font )
293 {
294 font = KIFONT::FONT::GetFont( m_renderSettings.GetDefaultFont(), aItem->IsBold(),
295 aItem->IsItalic(), nullptr, true );
296 }
297
298 const COLOR4D& color = m_renderSettings.GetColor( aItem, aLayer );
299
300 m_gal->SetStrokeColor( color );
301 m_gal->SetFillColor( color );
302
303 TEXT_ATTRIBUTES attrs = aItem->GetAttributes();
304 attrs.m_StrokeWidth = std::max( aItem->GetEffectiveTextPenWidth(),
305 m_renderSettings.GetDefaultPenWidth() );
306
307 font->Draw( m_gal, aItem->GetShownText( true ), aItem->GetTextPos(), attrs,
308 aItem->GetFontMetrics() );
309}
310
311
312void KIGFX::DS_PAINTER::draw( const DS_DRAW_ITEM_BITMAP* aItem, int aLayer ) const
313{
314 m_gal->Save();
315 auto* bitmap = static_cast<DS_DATA_ITEM_BITMAP*>( aItem->GetPeer() );
316
317 VECTOR2D position = aItem->GetPosition();
318 m_gal->Translate( position );
319
320 // If we've failed to read the bitmap data, don't try to draw it
321 if( !( bitmap && bitmap->m_ImageBitmap
322 && bitmap->m_ImageBitmap->GetImageData() ) )
323 {
324 return;
325 }
326
327 // When the image scale factor is not 1.0, we need to modify the actual scale
328 // as the image scale factor is similar to a local zoom
329 double img_scale = bitmap->m_ImageBitmap->GetScale();
330
331 if( img_scale != 1.0 )
332 m_gal->Scale( VECTOR2D( img_scale, img_scale ) );
333
334 m_gal->DrawBitmap( *bitmap->m_ImageBitmap );
335
336#if 0 // For bounding box debug purpose only
337 BOX2I bbox = aItem->GetBoundingBox();
338 m_canvas->SetIsFill( true );
339 m_canvas->SetIsStroke( true );
340 m_canvas->SetFillColor( COLOR4D( 1, 1, 1, 0.4 ) );
341 m_canvas->SetStrokeColor( COLOR4D( 0, 0, 0, 1 ) );
342
343 if( img_scale != 1.0 )
344 m_canvas->Scale( VECTOR2D( 1.0, 1.0 ) );
345
346 m_canvas->DrawRectangle( VECTOR2D( bbox.GetOrigin() ) - position,
347 VECTOR2D( bbox.GetEnd() ) - position );
348#endif
349
350 m_gal->Restore();
351}
352
353
354void KIGFX::DS_PAINTER::draw( const DS_DRAW_ITEM_PAGE* aItem, int aLayer ) const
355{
356 VECTOR2D origin = VECTOR2D( 0.0, 0.0 );
357 VECTOR2D end = VECTOR2D( aItem->GetPageSize().x,
358 aItem->GetPageSize().y );
359
360 m_gal->SetIsStroke( true );
361
362 // Use a gray color for the border color
363 m_gal->SetStrokeColor( m_renderSettings.m_pageBorderColor );
364 m_gal->SetIsFill( false );
365 m_gal->SetLineWidth( m_renderSettings.GetDefaultPenWidth() );
366
367 m_gal->DrawRectangle( origin, end );
368
369 // Draw the corner marker
370 double marker_size = aItem->GetMarkerSize();
371
372 m_gal->SetStrokeColor( m_renderSettings.m_pageBorderColor );
373 VECTOR2D pos = VECTOR2D( aItem->GetMarkerPos().x, aItem->GetMarkerPos().y );
374
375 // Draw a circle and a X
376 m_gal->DrawCircle( pos, marker_size );
377 m_gal->DrawLine( VECTOR2D( pos.x - marker_size, pos.y - marker_size),
378 VECTOR2D( pos.x + marker_size, pos.y + marker_size ) );
379 m_gal->DrawLine( VECTOR2D( pos.x + marker_size, pos.y - marker_size),
380 VECTOR2D( pos.x - marker_size, pos.y + marker_size ) );
381}
382
383
384void KIGFX::DS_PAINTER::DrawBorder( const PAGE_INFO* aPageInfo, int aScaleFactor ) const
385{
386 VECTOR2D origin = VECTOR2D( 0.0, 0.0 );
387 VECTOR2D end = VECTOR2D( aPageInfo->GetWidthMils() * aScaleFactor,
388 aPageInfo->GetHeightMils() * aScaleFactor );
389
390 m_gal->SetIsStroke( true );
391
392 // Use a gray color for the border color
393 m_gal->SetStrokeColor( m_renderSettings.m_pageBorderColor );
394 m_gal->SetIsFill( false );
395 m_gal->SetLineWidth( m_renderSettings.GetDefaultPenWidth() );
396 m_gal->DrawRectangle( origin, end );
397}
BASE_SCREEN class implementation.
BOX2< VECTOR2I > BOX2I
Definition box2.h:922
wxString GetBaseVersion()
Get the KiCad version string without the information added by the packagers.
constexpr const Vec GetEnd() const
Definition box2.h:212
constexpr const Vec & GetOrigin() const
Definition box2.h:210
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
DS_DATA_ITEM * GetPeer() const
const KIFONT::METRICS & GetFontMetrics() const
VECTOR2I GetPosition() const override
const BOX2I GetBoundingBox() const override
Return the orthogonal bounding box of this object for display purposes.
const VECTOR2I & GetStart() const
const VECTOR2I & GetEnd() const
int m_sheetCount
The number of sheets.
static void GetTextVars(wxArrayString *aVars)
const std::map< wxString, wxString > * m_properties
wxString BuildFullText(const wxString &aTextbase)
const TITLE_BLOCK * m_titleBlock
const PROJECT * m_project
wxString m_pageNumber
The actual page number displayed in the title block.
A rectangle with thick segment showing the page limits and a marker showing the coordinate origin.
const VECTOR2I & GetMarkerPos() const
double GetMarkerSize() const
VECTOR2I GetPageSize() const
SHAPE_POLY_SET & GetPolygons()
Non filled rectangle with thick segment.
const VECTOR2I & GetEnd() const
const VECTOR2I & GetStart() const
A graphic text.
A base class for most all the KiCad significant classes used in schematics and boards.
Definition eda_item.h:100
KICAD_T Type() const
Returns the type of object.
Definition eda_item.h:112
bool IsSelected() const
Definition eda_item.h:129
bool IsBrightened() const
Definition eda_item.h:131
const VECTOR2I & GetTextPos() const
Definition eda_text.h:284
bool IsItalic() const
Definition eda_text.h:180
KIFONT::FONT * GetFont() const
Definition eda_text.h:258
const TEXT_ATTRIBUTES & GetAttributes() const
Definition eda_text.h:242
int GetEffectiveTextPenWidth(int aDefaultPenWidth=0) const
The EffectiveTextPenWidth uses the text thickness if > 1 or aDefaultPenWidth.
Definition eda_text.cpp:449
bool IsBold() const
Definition eda_text.h:195
virtual wxString GetShownText(bool aAllowExtraText, int aDepth=0) const
Return the string actually shown after processing of the base text.
Definition eda_text.h:120
High-level wrapper for evaluating mathematical and string expressions in wxString format.
wxString Evaluate(const wxString &aInput)
Main evaluation function - processes input string and evaluates all} expressions.
FONT is an abstract base class for both outline and stroke fonts.
Definition font.h:98
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:147
void Draw(KIGFX::GAL *aGal, const wxString &aText, const VECTOR2I &aPosition, const VECTOR2I &aCursor, const TEXT_ATTRIBUTES &aAttributes, const METRICS &aFontMetrics, std::optional< VECTOR2I > aMousePos=std::nullopt, wxString *aActiveUrl=nullptr) const
Draw a string.
Definition font.cpp:250
A color representation with 4 components: red, green, blue, alpha.
Definition color4d.h:105
static const COLOR4D UNSPECIFIED
For legacy support; used as a value to indicate color hasn't been set yet.
Definition color4d.h:402
void DrawBorder(const PAGE_INFO *aPageInfo, int aScaleFactor) const
DS_RENDER_SETTINGS m_renderSettings
Definition ds_painter.h:121
void draw(const DS_DRAW_ITEM_LINE *aItem, int aLayer) const
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.
void LoadColors(const COLOR_SETTINGS *aSettings) override
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...
GAL * m_gal
Instance of graphic abstraction layer that gives an interface to call commands used to draw (eg.
Definition painter.h:102
virtual void update()
Precalculates extra colors for layers (e.g.
std::map< int, COLOR4D > m_layerColors
An abstract base class for deriving all objects that can be added to a VIEW.
Definition view_item.h:86
Describe the page size and margins of a paper page on which to eventually print or plot.
Definition page_info.h:79
double GetHeightMils() const
Definition page_info.h:147
double GetWidthMils() const
Definition page_info.h:142
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)
Return the reference to aIndex-th outline in the set.
Hold the information shown in the lower right corner of a plot, printout, or editing view.
Definition title_block.h:41
static void GetContextualTextVars(wxArrayString *aVars)
@ RED
Definition color4d.h:59
wxString ExpandTextVars(const wxString &aSource, const PROJECT *aProject, int aFlags)
Definition common.cpp:62
The common library.
static const wxString productName
@ GAL_LAYER_ID_START
Definition layer_ids.h:229
@ GAL_LAYER_ID_END
Definition layer_ids.h:362
@ SCH_LAYER_ID_END
Definition layer_ids.h:507
@ SCH_LAYER_ID_START
Definition layer_ids.h:452
@ LAYER_SCHEMATIC_DRAWINGSHEET
Definition layer_ids.h:498
@ LAYER_SCHEMATIC_BACKGROUND
Definition layer_ids.h:490
@ LAYER_SCHEMATIC_GRID
Definition layer_ids.h:488
The Cairo implementation of the graphics abstraction layer.
Definition eda_group.h:33
PGM_BASE * PgmOrNull()
Return a reference that can be nullptr when running a shared lib from a script, not from a kicad app.
see class PGM_BASE
VECTOR2I end
@ WSG_POLY_T
Definition typeinfo.h:219
@ WSG_LINE_T
Definition typeinfo.h:217
@ WSG_TEXT_T
Definition typeinfo.h:220
@ WSG_PAGE_T
Definition typeinfo.h:222
@ WSG_RECT_T
Definition typeinfo.h:218
@ WSG_BITMAP_T
Definition typeinfo.h:221
VECTOR2< double > VECTOR2D
Definition vector2d.h:686