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 (C) 1992-2024 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
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( "FILEPATH" ) );
106 aVars->push_back( wxT( "PROJECTNAME" ) );
107 aVars->push_back( wxT( "PAPER" ) );
108 aVars->push_back( wxT( "LAYER" ) );
110}
111
112
113// Returns the full text corresponding to the aTextbase, after replacing any text variable
114// references.
115wxString DS_DRAW_ITEM_LIST::BuildFullText( const wxString& aTextbase )
116{
117 std::function<bool( wxString* )> wsResolver =
118 [&]( wxString* token ) -> bool
119 {
120 bool tokenUpdated = false;
121
122 if( token->IsSameAs( wxT( "KICAD_VERSION" ) ) && PgmOrNull() )
123 {
124 *token = wxString::Format( wxT( "%s %s" ), productName, GetBaseVersion() );
125 tokenUpdated = true;
126 }
127 else if( token->IsSameAs( wxT( "#" ) ) )
128 {
129 *token = wxString::Format( wxT( "%s" ), m_pageNumber );
130 tokenUpdated = true;
131 }
132 else if( token->IsSameAs( wxT( "##" ) ) )
133 {
134 *token = wxString::Format( wxT( "%d" ), m_sheetCount );
135 tokenUpdated = true;
136 }
137 else if( token->IsSameAs( wxT( "SHEETNAME" ) ) )
138 {
139 *token = m_sheetName;
140 tokenUpdated = true;
141 }
142 else if( token->IsSameAs( wxT( "SHEETPATH" ) ) )
143 {
144 *token = m_sheetPath;
145 tokenUpdated = true;
146 }
147 else if( token->IsSameAs( wxT( "FILENAME" ) ) )
148 {
149 wxFileName fn( m_fileName );
150 *token = fn.GetFullName();
151 tokenUpdated = true;
152 }
153 else if( token->IsSameAs( wxT( "FILEPATH" ) ) )
154 {
155 wxFileName fn( m_fileName );
156 *token = fn.GetFullPath();
157 return true;
158 }
159 else if( token->IsSameAs( wxT( "PROJECTNAME" ) ) && m_project )
160 {
161 *token = m_project->GetProjectName();
162 return true;
163 }
164 else if( token->IsSameAs( wxT( "PAPER" ) ) )
165 {
166 *token = m_paperFormat;
167 tokenUpdated = true;
168 }
169 else if( token->IsSameAs( wxT( "LAYER" ) ) )
170 {
171 *token = m_sheetLayer;
172 tokenUpdated = true;
173 }
174 else if( m_titleBlock )
175 {
176 if( m_titleBlock->TextVarResolver( token, m_project, m_flags ) )
177 {
178 // No need for tokenUpdated; TITLE_BLOCK::TextVarResolver() already goes
179 // up to the project.
180 //
181 // However, the title block may have variables in it itself, so we need
182 // to run the worksheet resolver again.
183 //
184 const TITLE_BLOCK* savedTitleBlock = m_titleBlock;
185
186 m_titleBlock = nullptr;
187 {
188 *token = ExpandTextVars( *token, &wsResolver, m_flags );
189 }
190 m_titleBlock = savedTitleBlock;
191
192 return true;
193 }
194 }
195 else if( m_properties && m_properties->count( *token ) )
196 {
197 *token = m_properties->at( *token );
198 tokenUpdated = true;
199 }
200
201 if( tokenUpdated )
202 {
203 *token = ExpandTextVars( *token, m_project, m_flags );
204 return true;
205 }
206
207 if( m_project && m_project->TextVarResolver( token ) )
208 return true;
209
210 return false;
211 };
212
213 return ExpandTextVars( aTextbase, &wsResolver, m_flags );
214}
215
216
217bool KIGFX::DS_PAINTER::Draw( const VIEW_ITEM* aItem, int aLayer )
218{
219 auto item = dynamic_cast<const EDA_ITEM*>( aItem );
220
221 if( !item )
222 return false;
223
224 switch( item->Type() )
225 {
226 case WSG_LINE_T: draw( (DS_DRAW_ITEM_LINE*) item, aLayer ); break;
227 case WSG_POLY_T: draw( (DS_DRAW_ITEM_POLYPOLYGONS*) item, aLayer ); break;
228 case WSG_RECT_T: draw( (DS_DRAW_ITEM_RECT*) item, aLayer ); break;
229 case WSG_TEXT_T: draw( (DS_DRAW_ITEM_TEXT*) item, aLayer ); break;
230 case WSG_BITMAP_T: draw( (DS_DRAW_ITEM_BITMAP*) item, aLayer ); break;
231 case WSG_PAGE_T: draw( (DS_DRAW_ITEM_PAGE*) item, aLayer ); break;
232 default: return false;
233 }
234
235 return true;
236}
237
238
239void KIGFX::DS_PAINTER::draw( const DS_DRAW_ITEM_LINE* aItem, int aLayer ) const
240{
241 m_gal->SetIsStroke( true );
242 m_gal->SetIsFill( false );
243 m_gal->SetStrokeColor( m_renderSettings.GetColor( aItem, aLayer ) );
244 m_gal->SetLineWidth( std::max( aItem->GetPenWidth(), m_renderSettings.GetDefaultPenWidth() ) );
245 m_gal->DrawLine( VECTOR2D( aItem->GetStart() ), VECTOR2D( aItem->GetEnd() ) );
246}
247
248
249void KIGFX::DS_PAINTER::draw( const DS_DRAW_ITEM_RECT* aItem, int aLayer ) const
250{
251 m_gal->SetIsStroke( true );
252 m_gal->SetIsFill( false );
253 m_gal->SetStrokeColor( m_renderSettings.GetColor( aItem, aLayer ) );
254 m_gal->SetLineWidth( std::max( aItem->GetPenWidth(), m_renderSettings.GetDefaultPenWidth() ) );
255 m_gal->DrawRectangle( VECTOR2D( aItem->GetStart() ), VECTOR2D( aItem->GetEnd() ) );
256}
257
258
259void KIGFX::DS_PAINTER::draw( const DS_DRAW_ITEM_POLYPOLYGONS* aItem, int aLayer ) const
260{
261 m_gal->SetFillColor( m_renderSettings.GetColor( aItem, aLayer ) );
262 m_gal->SetIsFill( true );
263 m_gal->SetIsStroke( false );
264
266
267 for( int idx = 0; idx < item->GetPolygons().OutlineCount(); ++idx )
268 {
269 SHAPE_LINE_CHAIN& outline = item->GetPolygons().Outline( idx );
270 m_gal->DrawPolygon( outline );
271 }
272}
273
274
275void KIGFX::DS_PAINTER::draw( const DS_DRAW_ITEM_TEXT* aItem, int aLayer ) const
276{
277 KIFONT::FONT* font = aItem->GetFont();
278
279 if( !font )
280 {
281 font = KIFONT::FONT::GetFont( m_renderSettings.GetDefaultFont(), aItem->IsBold(),
282 aItem->IsItalic(), nullptr, true );
283 }
284
285 const COLOR4D& color = m_renderSettings.GetColor( aItem, aLayer );
286
287 m_gal->SetStrokeColor( color );
288 m_gal->SetFillColor( color );
289
290 TEXT_ATTRIBUTES attrs = aItem->GetAttributes();
291 attrs.m_StrokeWidth = std::max( aItem->GetEffectiveTextPenWidth(),
292 m_renderSettings.GetDefaultPenWidth() );
293
294 font->Draw( m_gal, aItem->GetShownText( true ), aItem->GetTextPos(), attrs,
295 aItem->GetFontMetrics() );
296}
297
298
299void KIGFX::DS_PAINTER::draw( const DS_DRAW_ITEM_BITMAP* aItem, int aLayer ) const
300{
301 m_gal->Save();
302 auto* bitmap = static_cast<DS_DATA_ITEM_BITMAP*>( aItem->GetPeer() );
303
304 VECTOR2D position = aItem->GetPosition();
305 m_gal->Translate( position );
306
307 // If we've failed to read the bitmap data, don't try to draw it
308 if( !( bitmap && bitmap->m_ImageBitmap
309 && bitmap->m_ImageBitmap->GetImageData() ) )
310 {
311 return;
312 }
313
314 // When the image scale factor is not 1.0, we need to modify the actual scale
315 // as the image scale factor is similar to a local zoom
316 double img_scale = bitmap->m_ImageBitmap->GetScale();
317
318 if( img_scale != 1.0 )
319 m_gal->Scale( VECTOR2D( img_scale, img_scale ) );
320
321 m_gal->DrawBitmap( *bitmap->m_ImageBitmap );
322
323#if 0 // For bounding box debug purpose only
324 BOX2I bbox = aItem->GetBoundingBox();
325 m_gal->SetIsFill( true );
326 m_gal->SetIsStroke( true );
327 m_gal->SetFillColor( COLOR4D( 1, 1, 1, 0.4 ) );
328 m_gal->SetStrokeColor( COLOR4D( 0, 0, 0, 1 ) );
329
330 if( img_scale != 1.0 )
331 m_gal->Scale( VECTOR2D( 1.0, 1.0 ) );
332
333 m_gal->DrawRectangle( VECTOR2D( bbox.GetOrigin() ) - position,
334 VECTOR2D( bbox.GetEnd() ) - position );
335#endif
336
337 m_gal->Restore();
338}
339
340
341void KIGFX::DS_PAINTER::draw( const DS_DRAW_ITEM_PAGE* aItem, int aLayer ) const
342{
343 VECTOR2D origin = VECTOR2D( 0.0, 0.0 );
344 VECTOR2D end = VECTOR2D( aItem->GetPageSize().x,
345 aItem->GetPageSize().y );
346
347 m_gal->SetIsStroke( true );
348
349 // Use a gray color for the border color
350 m_gal->SetStrokeColor( m_renderSettings.m_pageBorderColor );
351 m_gal->SetIsFill( false );
352 m_gal->SetLineWidth( m_renderSettings.GetDefaultPenWidth() );
353
354 m_gal->DrawRectangle( origin, end );
355
356 // Draw the corner marker
357 double marker_size = aItem->GetMarkerSize();
358
359 m_gal->SetStrokeColor( m_renderSettings.m_pageBorderColor );
360 VECTOR2D pos = VECTOR2D( aItem->GetMarkerPos().x, aItem->GetMarkerPos().y );
361
362 // Draw a circle and a X
363 m_gal->DrawCircle( pos, marker_size );
364 m_gal->DrawLine( VECTOR2D( pos.x - marker_size, pos.y - marker_size),
365 VECTOR2D( pos.x + marker_size, pos.y + marker_size ) );
366 m_gal->DrawLine( VECTOR2D( pos.x + marker_size, pos.y - marker_size),
367 VECTOR2D( pos.x - marker_size, pos.y + marker_size ) );
368}
369
370
371void KIGFX::DS_PAINTER::DrawBorder( const PAGE_INFO* aPageInfo, int aScaleFactor ) const
372{
373 VECTOR2D origin = VECTOR2D( 0.0, 0.0 );
374 VECTOR2D end = VECTOR2D( aPageInfo->GetWidthMils() * aScaleFactor,
375 aPageInfo->GetHeightMils() * aScaleFactor );
376
377 m_gal->SetIsStroke( true );
378 // Use a gray color for the border color
379 m_gal->SetStrokeColor( m_renderSettings.m_pageBorderColor );
380 m_gal->SetIsFill( false );
381 m_gal->SetLineWidth( m_renderSettings.GetDefaultPenWidth() );
382 m_gal->DrawRectangle( origin, end );
383}
int color
Definition: DXF_plotter.cpp:58
BASE_SCREEN class implementation.
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
Definition: ds_draw_item.h:70
DS_DATA_ITEM * GetPeer() const
Definition: ds_draw_item.h:63
const KIFONT::METRICS & GetFontMetrics() const
VECTOR2I GetPosition() const override
Definition: ds_draw_item.h:373
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:144
const VECTOR2I & GetEnd() const
Definition: ds_draw_item.h:146
static void GetTextVars(wxArrayString *aVars)
Definition: ds_painter.cpp:97
wxString BuildFullText(const wxString &aTextbase)
Definition: ds_painter.cpp:115
A rectangle with thick segment showing the page limits and a marker showing the coordinate origin.
Definition: ds_draw_item.h:266
const VECTOR2I & GetMarkerPos() const
Definition: ds_draw_item.h:280
double GetMarkerSize() const
Definition: ds_draw_item.h:283
VECTOR2I GetPageSize() const
Definition: ds_draw_item.h:278
SHAPE_POLY_SET & GetPolygons()
Definition: ds_draw_item.h:182
Non filled rectangle with thick segment.
Definition: ds_draw_item.h:218
const VECTOR2I & GetEnd() const
Definition: ds_draw_item.h:233
const VECTOR2I & GetStart() const
Definition: ds_draw_item.h:231
A graphic text.
Definition: ds_draw_item.h:313
A base class for most all the KiCad significant classes used in schematics and boards.
Definition: eda_item.h:89
KICAD_T Type() const
Returns the type of object.
Definition: eda_item.h:101
bool IsSelected() const
Definition: eda_item.h:110
bool IsBrightened() const
Definition: eda_item.h:112
const VECTOR2I & GetTextPos() const
Definition: eda_text.h:257
bool IsItalic() const
Definition: eda_text.h:156
KIFONT::FONT * GetFont() const
Definition: eda_text.h:234
const TEXT_ATTRIBUTES & GetAttributes() const
Definition: eda_text.h:218
int GetEffectiveTextPenWidth(int aDefaultPenWidth=0) const
The EffectiveTextPenWidth uses the text thickness if > 1 or aDefaultPenWidth.
Definition: eda_text.cpp:456
bool IsBold() const
Definition: eda_text.h:171
virtual wxString GetShownText(bool aAllowExtraText, int aDepth=0) const
Return the string actually shown after processing of the base text.
Definition: eda_text.h:109
FONT is an abstract base class for both outline and stroke fonts.
Definition: font.h:131
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:146
void Draw(KIGFX::GAL *aGal, const wxString &aText, const VECTOR2I &aPosition, const VECTOR2I &aCursor, const TEXT_ATTRIBUTES &aAttributes, const METRICS &aFontMetrics) const
Draw a string.
Definition: font.cpp:258
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:268
static const COLOR4D UNSPECIFIED
For legacy support; used as a value to indicate color hasn't been set yet.
Definition: color4d.h:398
void DrawBorder(const PAGE_INFO *aPageInfo, int aScaleFactor) const
Definition: ds_painter.cpp:371
void draw(const DS_DRAW_ITEM_LINE *aItem, int aLayer) const
Definition: ds_painter.cpp:239
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:217
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.
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:84
Describe the page size and margins of a paper page on which to eventually print or plot.
Definition: page_info.h:59
double GetHeightMils() const
Definition: page_info.h:141
double GetWidthMils() const
Definition: page_info.h:136
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.
int OutlineCount() const
Return the number of outlines 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)
Definition: title_block.cpp:73
@ RED
Definition: color4d.h:59
wxString ExpandTextVars(const wxString &aSource, const PROJECT *aProject, int aFlags)
Definition: common.cpp:59
The common library.
static const wxString productName
Definition: ds_painter.cpp:43
@ GAL_LAYER_ID_START
Definition: layer_ids.h:192
@ GAL_LAYER_ID_END
Definition: layer_ids.h:268
@ SCH_LAYER_ID_END
Definition: layer_ids.h:406
@ SCH_LAYER_ID_START
Definition: layer_ids.h:354
@ LAYER_SCHEMATIC_DRAWINGSHEET
Definition: layer_ids.h:398
@ LAYER_SCHEMATIC_BACKGROUND
Definition: layer_ids.h:391
@ LAYER_SCHEMATIC_GRID
Definition: layer_ids.h:389
The Cairo implementation of the graphics abstraction layer.
Definition: color4d.cpp:247
PGM_BASE * PgmOrNull()
similar to PGM_BASE& Pgm(), but return a reference that can be nullptr when running a shared lib from...
Definition: pgm_base.cpp:1067
see class PGM_BASE
@ WSG_POLY_T
Definition: typeinfo.h:217
@ WSG_LINE_T
Definition: typeinfo.h:215
@ WSG_TEXT_T
Definition: typeinfo.h:218
@ WSG_PAGE_T
Definition: typeinfo.h:220
@ WSG_RECT_T
Definition: typeinfo.h:216
@ WSG_BITMAP_T
Definition: typeinfo.h:219
VECTOR2< double > VECTOR2D
Definition: vector2d.h:690