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