KiCad PCB EDA Suite
ws_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 <fctsys.h>
27 #include <pgm_base.h>
28 #include <gr_basic.h>
29 #include <common.h>
30 #include <base_screen.h>
31 #include <eda_draw_frame.h>
32 #include <title_block.h>
33 #include <build_version.h>
35 #include <ws_draw_item.h>
37 
38 #include <ws_painter.h>
39 #include <ws_data_item.h>
40 
41 using namespace KIGFX;
42 
43 static 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 
67 }
68 
69 
70 const COLOR4D& WS_RENDER_SETTINGS::GetColor( const VIEW_ITEM* aItem, int aLayer ) const
71 {
72  const EDA_ITEM* item = dynamic_cast<const EDA_ITEM*>( aItem );
73 
74  if( item )
75  {
76  // Selection disambiguation
77  if( item->IsBrightened() )
78  return m_brightenedColor;
79 
80  if( item->IsSelected() )
81  return m_selectedColor;
82  }
83 
84  return m_normalColor;
85 }
86 
87 
88 void WS_DRAW_ITEM_LIST::GetTextVars( wxArrayString* aVars )
89 {
90  aVars->push_back( wxT( "KICAD_VERSION" ) );
91  aVars->push_back( wxT( "#" ) );
92  aVars->push_back( wxT( "##" ) );
93  aVars->push_back( wxT( "SHEETNAME" ) );
94  aVars->push_back( wxT( "FILENAME" ) );
95  aVars->push_back( wxT( "PAPER" ) );
96  aVars->push_back( wxT( "LAYER" ) );
97  aVars->push_back( wxT( "ISSUE_DATE" ) );
98  aVars->push_back( wxT( "REVISION" ) );
99  aVars->push_back( wxT( "TITLE" ) );
100  aVars->push_back( wxT( "COMPANY" ) );
101  aVars->push_back( wxT( "COMMENT0" ) );
102  aVars->push_back( wxT( "COMMENT1" ) );
103  aVars->push_back( wxT( "COMMENT2" ) );
104  aVars->push_back( wxT( "COMMENT3" ) );
105  aVars->push_back( wxT( "COMMENT4" ) );
106  aVars->push_back( wxT( "COMMENT5" ) );
107  aVars->push_back( wxT( "COMMENT6" ) );
108  aVars->push_back( wxT( "COMMENT7" ) );
109  aVars->push_back( wxT( "COMMENT8" ) );
110  aVars->push_back( wxT( "COMMENT9" ) );
111 }
112 
113 
114 // returns the full text corresponding to the aTextbase,
115 // after replacing format symbols by the corresponding value
116 wxString WS_DRAW_ITEM_LIST::BuildFullText( const wxString& aTextbase )
117 {
118  std::function<bool( wxString* )> wsResolver =
119  [ this ]( wxString* token ) -> bool
120  {
121  bool tokenUpdated = false;
122 
123  if( token->IsSameAs( wxT( "KICAD_VERSION" ) ) )
124  {
125  *token = wxString::Format( wxT( "%s%s %s" ),
126  productName,
127  Pgm().App().GetAppName(),
128  GetBuildVersion() );
129  tokenUpdated = true;
130  }
131  else if( token->IsSameAs( wxT( "#" ) ) )
132  {
133  *token = wxString::Format( wxT( "%d" ), m_sheetNumber );
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_sheetFullName;
144  tokenUpdated = true;
145  }
146  else if( token->IsSameAs( wxT( "FILENAME" ) ) )
147  {
148  wxFileName fn( m_fileName );
149  *token = fn.GetFullName();
150  tokenUpdated = true;
151  }
152  else if( token->IsSameAs( wxT( "PAPER" ) ) )
153  {
154  *token = m_paperFormat ? *m_paperFormat : wxString( "" );
155  tokenUpdated = true;
156  }
157  else if( token->IsSameAs( wxT( "LAYER" ) ) )
158  {
159  *token = m_sheetLayer ? *m_sheetLayer : wxString( "" );
160  tokenUpdated = true;
161  }
162  else if( token->IsSameAs( wxT( "ISSUE_DATE" ) ) )
163  {
164  *token = m_titleBlock ? m_titleBlock->GetDate() : wxString( "" );
165  tokenUpdated = true;
166  }
167  else if( token->IsSameAs( wxT( "REVISION" ) ) )
168  {
169  *token = m_titleBlock ? m_titleBlock->GetRevision() : wxString( "" );
170  tokenUpdated = true;
171  }
172  else if( token->IsSameAs( wxT( "TITLE" ) ) )
173  {
174  *token = m_titleBlock ? m_titleBlock->GetTitle() : wxString( "" );
175  tokenUpdated = true;
176  }
177  else if( token->IsSameAs( wxT( "COMPANY" ) ) )
178  {
179  *token = m_titleBlock ? m_titleBlock->GetCompany() : wxString( "" );
180  tokenUpdated = true;
181  }
182  else if( token->Left( token->Len() - 1 ).IsSameAs( wxT( "COMMENT" ) ) )
183  {
184  wxChar c = token->Last();
185 
186  switch( c )
187  {
188  case '0':
189  case '1':
190  case '2':
191  case '3':
192  case '4':
193  case '5':
194  case '6':
195  case '7':
196  case '8':
197  case '9':
198  *token = m_titleBlock ? m_titleBlock->GetComment( c - '0' )
199  : wxString( "" );
200  tokenUpdated = true;
201  }
202  }
203 
204  if( tokenUpdated )
205  {
206  *token = ExpandTextVars( *token, nullptr, m_project );
207  return true;
208  }
209 
210  return false;
211  };
212 
213  return ExpandTextVars( aTextbase, &wsResolver, m_project );
214 }
215 
216 
217 void TITLE_BLOCK::Format( OUTPUTFORMATTER* aFormatter, int aNestLevel, int aControlBits ) const
218 
219 {
220  // Don't write the title block information if there is nothing to write.
221  bool isempty = true;
222  for( unsigned idx = 0; idx < m_tbTexts.GetCount(); idx++ )
223  {
224  if( ! m_tbTexts[idx].IsEmpty() )
225  {
226  isempty = false;
227  break;
228  }
229  }
230 
231  if( !isempty )
232  {
233  aFormatter->Print( aNestLevel, "(title_block\n" );
234 
235  if( !GetTitle().IsEmpty() )
236  aFormatter->Print( aNestLevel+1, "(title %s)\n",
237  aFormatter->Quotew( GetTitle() ).c_str() );
238 
239  if( !GetDate().IsEmpty() )
240  aFormatter->Print( aNestLevel+1, "(date %s)\n",
241  aFormatter->Quotew( GetDate() ).c_str() );
242 
243  if( !GetRevision().IsEmpty() )
244  aFormatter->Print( aNestLevel+1, "(rev %s)\n",
245  aFormatter->Quotew( GetRevision() ).c_str() );
246 
247  if( !GetCompany().IsEmpty() )
248  aFormatter->Print( aNestLevel+1, "(company %s)\n",
249  aFormatter->Quotew( GetCompany() ).c_str() );
250 
251  for( int ii = 0; ii < 9; ii++ )
252  {
253  if( !GetComment(ii).IsEmpty() )
254  aFormatter->Print( aNestLevel+1, "(comment %d %s)\n", ii+1,
255  aFormatter->Quotew( GetComment(ii) ).c_str() );
256  }
257 
258  aFormatter->Print( aNestLevel, ")\n\n" );
259  }
260 }
261 
262 
263 bool KIGFX::WS_PAINTER::Draw( const VIEW_ITEM* aItem, int aLayer )
264 {
265  auto item = static_cast<const EDA_ITEM*>( aItem );
266 
267  switch( item->Type() )
268  {
269  case WSG_LINE_T: draw( (WS_DRAW_ITEM_LINE*) item, aLayer ); break;
270  case WSG_POLY_T: draw( (WS_DRAW_ITEM_POLYPOLYGONS*) item, aLayer ); break;
271  case WSG_RECT_T: draw( (WS_DRAW_ITEM_RECT*) item, aLayer ); break;
272  case WSG_TEXT_T: draw( (WS_DRAW_ITEM_TEXT*) item, aLayer ); break;
273  case WSG_BITMAP_T: draw( (WS_DRAW_ITEM_BITMAP*) item, aLayer ); break;
274  case WSG_PAGE_T: draw( (WS_DRAW_ITEM_PAGE*) item, aLayer ); break;
275  default: return false;
276  }
277 
278  return true;
279 }
280 
281 
282 void KIGFX::WS_PAINTER::draw( const WS_DRAW_ITEM_LINE* aItem, int aLayer ) const
283 {
284  m_gal->SetIsStroke( true );
285  m_gal->SetIsFill( false );
286  m_gal->SetStrokeColor( m_renderSettings.GetColor( aItem, aLayer ) );
287  m_gal->SetLineWidth( std::max( aItem->GetPenWidth(), m_renderSettings.GetDefaultPenWidth() ) );
288  m_gal->DrawLine( VECTOR2D( aItem->GetStart() ), VECTOR2D( aItem->GetEnd() ) );
289 }
290 
291 
292 void KIGFX::WS_PAINTER::draw( const WS_DRAW_ITEM_RECT* aItem, int aLayer ) const
293 {
294  m_gal->SetIsStroke( true );
295  m_gal->SetIsFill( false );
296  m_gal->SetStrokeColor( m_renderSettings.GetColor( aItem, aLayer ) );
297  m_gal->SetLineWidth( std::max( aItem->GetPenWidth(), m_renderSettings.GetDefaultPenWidth() ) );
298  m_gal->DrawRectangle( VECTOR2D( aItem->GetStart() ), VECTOR2D( aItem->GetEnd() ) );
299 }
300 
301 
302 void KIGFX::WS_PAINTER::draw( const WS_DRAW_ITEM_POLYPOLYGONS* aItem, int aLayer ) const
303 {
304  m_gal->SetFillColor( m_renderSettings.GetColor( aItem, aLayer ) );
305  m_gal->SetIsFill( true );
306  m_gal->SetIsStroke( false );
307 
309 
310  for( int idx = 0; idx < item->GetPolygons().OutlineCount(); ++idx )
311  {
312  SHAPE_LINE_CHAIN& outline = item->GetPolygons().Outline( idx );
313  m_gal->DrawPolygon( outline );
314  }
315 }
316 
317 
318 void KIGFX::WS_PAINTER::draw( const WS_DRAW_ITEM_TEXT* aItem, int aLayer ) const
319 {
320  VECTOR2D position( aItem->GetTextPos().x, aItem->GetTextPos().y );
321  int penWidth = std::max( aItem->GetEffectiveTextPenWidth(),
322  m_renderSettings.GetDefaultPenWidth() );
323 
324  m_gal->Save();
325  m_gal->Translate( position );
326  m_gal->Rotate( -aItem->GetTextAngle() * M_PI / 1800.0 );
327  m_gal->SetStrokeColor( m_renderSettings.GetColor( aItem, aLayer ) );
328  m_gal->SetLineWidth( penWidth );
329  m_gal->SetTextAttributes( aItem );
330  m_gal->SetIsFill( false );
331  m_gal->SetIsStroke( true );
332  m_gal->StrokeText( aItem->GetShownText(), VECTOR2D( 0, 0 ), 0.0 );
333  m_gal->Restore();
334 }
335 
336 
337 void KIGFX::WS_PAINTER::draw( const WS_DRAW_ITEM_BITMAP* aItem, int aLayer ) const
338 {
339  m_gal->Save();
340  auto* bitmap = static_cast<WS_DATA_ITEM_BITMAP*>( aItem->GetPeer() );
341 
342  VECTOR2D position = aItem->GetPosition();
343  m_gal->Translate( position );
344 
345  // When the image scale factor is not 1.0, we need to modify the actual scale
346  // as the image scale factor is similar to a local zoom
347  double img_scale = bitmap->m_ImageBitmap->GetScale();
348 
349  if( img_scale != 1.0 )
350  m_gal->Scale( VECTOR2D( img_scale, img_scale ) );
351 
352  m_gal->DrawBitmap( *bitmap->m_ImageBitmap );
353 
354 #if 0 // For bounding box debug purpose only
355  EDA_RECT bbox = aItem->GetBoundingBox();
356  m_gal->SetIsFill( true );
357  m_gal->SetIsStroke( true );
358  m_gal->SetFillColor( COLOR4D( 1, 1, 1, 0.4 ) );
359  m_gal->SetStrokeColor( COLOR4D( 0, 0, 0, 1 ) );
360 
361  if( img_scale != 1.0 )
362  m_gal->Scale( VECTOR2D( 1.0, 1.0 ) );
363 
364  m_gal->DrawRectangle( VECTOR2D( bbox.GetOrigin() ) - position,
365  VECTOR2D( bbox.GetEnd() ) - position );
366 #endif
367 
368  m_gal->Restore();
369 }
370 
371 
372 void KIGFX::WS_PAINTER::draw( const WS_DRAW_ITEM_PAGE* aItem, int aLayer ) const
373 {
374  VECTOR2D origin = VECTOR2D( 0.0, 0.0 );
375  VECTOR2D end = VECTOR2D( aItem->GetPageSize().x,
376  aItem->GetPageSize().y );
377 
378  m_gal->SetIsStroke( true );
379 
380  // Use a gray color for the border color
381  m_gal->SetStrokeColor( m_renderSettings.m_pageBorderColor );
382  m_gal->SetIsFill( false );
383  m_gal->DrawRectangle( origin, end );
384 
385  // Draw the corner marker
386  double marker_size = aItem->GetMarkerSize();
387 
388  m_gal->SetStrokeColor( m_renderSettings.m_pageBorderColor );
389  VECTOR2D pos = VECTOR2D( aItem->GetMarkerPos().x, aItem->GetMarkerPos().y );
390 
391  // Draw a cirle and a X
392  m_gal->DrawCircle( pos, marker_size );
393  m_gal->DrawLine( VECTOR2D( pos.x - marker_size, pos.y - marker_size),
394  VECTOR2D( pos.x + marker_size, pos.y + marker_size ) );
395  m_gal->DrawLine( VECTOR2D( pos.x + marker_size, pos.y - marker_size),
396  VECTOR2D( pos.x - marker_size, pos.y + marker_size ) );
397 }
398 
399 
400 void KIGFX::WS_PAINTER::DrawBorder( const PAGE_INFO* aPageInfo, int aScaleFactor ) const
401 {
402  VECTOR2D origin = VECTOR2D( 0.0, 0.0 );
403  VECTOR2D end = VECTOR2D( aPageInfo->GetWidthMils() * aScaleFactor,
404  aPageInfo->GetHeightMils() * aScaleFactor );
405 
406  m_gal->SetIsStroke( true );
407  // Use a gray color for the border color
408  m_gal->SetStrokeColor( m_renderSettings.m_pageBorderColor );
409  m_gal->SetIsFill( false );
410  m_gal->DrawRectangle( origin, end );
411 }
const wxPoint & GetStart() const
Definition: ws_draw_item.h:211
WS_DATA_ITEM * GetPeer() const
Definition: ws_draw_item.h:70
const wxPoint & GetEnd() const
Definition: ws_draw_item.h:213
KIWAY Kiway & Pgm(), KFCTL_STANDALONE
The global Program "get" accessor.
Definition: single_top.cpp:104
const EDA_RECT GetBoundingBox() const override
Function GetBoundingBox returns the orthogonal, bounding box of this object for display purposes.
int OutlineCount() const
Returns the number of outlines in the set
Class CAIRO_GAL is the cairo implementation of the graphics abstraction layer.
Definition: color4d.cpp:175
bool IsSelected() const
Definition: base_struct.h:203
const wxPoint & GetEnd() const
Definition: ws_draw_item.h:133
COLOR4D m_layerColors[LAYER_ID_COUNT]
int GetHeightMils() const
Definition: page_info.h:140
SHAPE_POLY_SET & GetPolygons()
Definition: ws_draw_item.h:175
double GetTextAngle() const
Definition: eda_text.h:173
OUTPUTFORMATTER is an important interface (abstract class) used to output 8 bit text in a convenient ...
Definition: richio.h:327
bool IsBrightened() const
Definition: base_struct.h:205
void LoadColors(const COLOR_SETTINGS *aSettings) override
Definition: ws_painter.cpp:57
Add new GAL layers here.
VIEW_ITEM - is an abstract base class for deriving all objects that can be added to a VIEW.
Definition: view_item.h:85
wxPoint GetPosition() const override
Definition: ws_draw_item.h:331
int GetEffectiveTextPenWidth(int aDefaultWidth=0) const
The EffectiveTextPenWidth uses the text thickness if > 1 or aDefaultWidth.
Definition: eda_text.cpp:152
COLOR4D Brightened(double aFactor) const
Function Brightened Returns a color that is brighter by a given factor, without modifying object.
Definition: color4d.h:259
const wxPoint GetEnd() const
Definition: eda_rect.h:116
VECTOR2< double > VECTOR2D
Definition: vector2d.h:593
void DrawBorder(const PAGE_INFO *aPageInfo, int aScaleFactor) const
Definition: ws_painter.cpp:400
wxString GetBuildVersion()
Get the full KiCad version string.
virtual void update()
Function update Precalculates extra colors for layers (e.g.
SHAPE_LINE_CHAIN & Outline(int aIndex)
Returns the reference to aIndex-th outline in the set
const wxPoint GetOrigin() const
Definition: eda_rect.h:114
PAGE_INFO describes the page size and margins of a paper page on which to eventually print or plot.
Definition: page_info.h:54
const wxPoint & GetStart() const
Definition: ws_draw_item.h:131
void draw(const WS_DRAW_ITEM_LINE *aItem, int aLayer) const
Definition: ws_painter.cpp:282
virtual void Format(OUTPUTFORMATTER *aFormatter, int aNestLevel, int aControlBits) const
Function Format outputs the object to aFormatter in s-expression form.
Definition: ws_painter.cpp:217
std::string Quotew(const wxString &aWrapee)
Definition: richio.cpp:472
Definition: color4d.h:59
virtual int GetPenWidth() const
Definition: ws_draw_item.h:78
COLOR4D GetColor(int aLayer) const
wxSize GetPageSize() const
Definition: ws_draw_item.h:253
VECTOR2< T > Rotate(double aAngle) const
Function Rotate rotates the vector by a given angle.
Definition: vector2d.h:377
see class PGM_BASE
void Format(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, CPTREE &aTree)
Function Format outputs a PTREE into s-expression format via an OUTPUTFORMATTER derivative.
Definition: ptree.cpp:205
wxString BuildFullText(const wxString &aTextbase)
Function BuildFullText returns the full text corresponding to the aTextbase, after replacing format s...
Definition: ws_painter.cpp:116
SHAPE_LINE_CHAIN.
virtual const COLOR4D & GetColor(const VIEW_ITEM *aItem, int aLayer) const override
Function GetColor Returns the color that should be used to draw the specific VIEW_ITEM on the specifi...
Definition: ws_painter.cpp:70
EDA_RECT handles the component boundary box.
Definition: eda_rect.h:44
double GetMarkerSize() const
Definition: ws_draw_item.h:256
EDA_ITEM is a base class for most all the KiCad significant classes, used in schematics and boards.
Definition: base_struct.h:159
The common library.
wxString ExpandTextVars(const wxString &aSource, const std::function< bool(wxString *)> *aLocalResolver, const PROJECT *aProject)
Expand '${var-name}' templates in text.
Definition: common.cpp:382
Color settings are a bit different than most of the settings objects in that there can be more than o...
const wxPoint & GetTextPos() const
Definition: eda_text.h:247
int PRINTF_FUNC Print(int nestLevel, const char *fmt,...)
Function Print formats and writes text to the output stream.
Definition: richio.cpp:404
BASE_SCREEN class implementation.
virtual bool Draw(const VIEW_ITEM *, int) override
Function Draw Takes an instance of VIEW_ITEM and passes it to a function that know how to draw the it...
Definition: ws_painter.cpp:263
virtual wxString GetShownText(int aDepth=0) const
Return the string actually shown after processing of the base text.
Definition: eda_text.h:133
static const wxString productName
Definition: ws_painter.cpp:43
const wxPoint & GetMarkerPos() const
Definition: ws_draw_item.h:254
int GetWidthMils() const
Definition: page_info.h:137
static void GetTextVars(wxArrayString *aVars)
Definition: ws_painter.cpp:88
COLOR4D is the color representation with 4 components: red, green, blue, alpha.
Definition: color4d.h:99