KiCad PCB EDA Suite
title_block_shapes.cpp
Go to the documentation of this file.
1 
6 /*
7  * This program source code file is part of KiCad, a free EDA CAD application.
8  *
9  * Copyright (C) 1992-2013 Jean-Pierre Charras <jp.charras at wanadoo.fr>.
10  * Copyright (C) 1992-2013 KiCad Developers, see change_log.txt for contributors.
11  *
12  *
13  * This program is free software; you can redistribute it and/or
14  * modify it under the terms of the GNU General Public License
15  * as published by the Free Software Foundation; either version 2
16  * of the License, or (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, you may find one here:
25  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
26  * or you may search the http://www.gnu.org website for the version 2 license,
27  * or you may write to the Free Software Foundation, Inc.,
28  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
29  */
30 
31 
32 /*
33  * the class WORKSHEET_DATAITEM (and WORKSHEET_DATAITEM_TEXT) defines
34  * a basic shape of a page layout ( frame references and title block )
35  * Basic shapes are line, rect and texts
36  * the WORKSHEET_DATAITEM coordinates units is the mm, and are relative to
37  * one of 4 page corners.
38  *
39  * These items cannot be drawn or plot "as this". they should be converted
40  * to a "draw list" (WS_DRAW_ITEM_BASE and derived items)
41 
42  * The list of these items is stored in a WORKSHEET_LAYOUT instance.
43  *
44  * When building the draw list:
45  * the WORKSHEET_LAYOUT is used to create a WS_DRAW_ITEM_LIST
46  * coordinates are converted to draw/plot coordinates.
47  * texts are expanded if they contain format symbols.
48  * Items with m_RepeatCount > 1 are created m_RepeatCount times
49  *
50  * the WORKSHEET_LAYOUT is created only once.
51  * the WS_DRAW_ITEM_LIST is created each time the page layout is plot/drawn
52  *
53  * the WORKSHEET_LAYOUT instance is created from a S expression which
54  * describes the page layout (can be the default page layout or a custom file).
55  */
56 
57 #include <fctsys.h>
58 #include <drawtxt.h>
59 #include <class_page_info.h>
60 #include <worksheet.h>
61 #include <class_title_block.h>
64 
65 
67  const PAGE_INFO& aPageInfo,
68  const TITLE_BLOCK& aTitleBlock,
69  COLOR4D aColor, COLOR4D aAltColor )
70 {
72 
73  #define milsTomm (25.4/1000)
74 
75  m_titleBlock = &aTitleBlock;
76  m_paperFormat = &aPageInfo.GetType();
77 
78  wxPoint LTmargin( Mm2mils( pglayout.GetLeftMargin() ),
79  Mm2mils( pglayout.GetTopMargin() ) );
80  wxPoint RBmargin( Mm2mils( pglayout.GetRightMargin() ),
81  Mm2mils( pglayout.GetBottomMargin() ) );
82 
83  SetMargins( LTmargin, RBmargin );
84  SetPageSize( aPageInfo.GetSizeMils() );
85 
86  // Build the basic layout shape, if the layout list is empty
87  if( pglayout.GetCount() == 0 && !pglayout.VoidListAllowed() )
88  pglayout.SetPageLayout();
89 
91  WORKSHEET_DATAITEM::m_Color = aColor; // the default color to draw items
92  WORKSHEET_DATAITEM::m_AltColor = aAltColor; // an alternate color to draw items
93 
94  // Left top corner position
95  DPOINT lt_corner;
96  lt_corner.x = pglayout.GetLeftMargin();
97  lt_corner.y = pglayout.GetTopMargin();
99 
100  // Right bottom corner position
101  DPOINT rb_corner;
102  rb_corner.x = (m_pageSize.x*milsTomm) - pglayout.GetRightMargin();
103  rb_corner.y = (m_pageSize.y*milsTomm) - pglayout.GetBottomMargin();
105 
106  WS_DRAW_ITEM_TEXT* gtext;
107  int pensize;
108 
109  for( unsigned ii = 0; ; ii++ )
110  {
111  WORKSHEET_DATAITEM* wsItem = pglayout.GetItem( ii );
112 
113  if( wsItem == NULL )
114  break;
115 
116  // Generate it only if the page option allows this
117  if( wsItem->GetPage1Option() < 0 // Not on page 1
118  && m_sheetNumber <= 1 )
119  continue;
120 
121  if( wsItem->GetPage1Option() > 0 // Only on page 1
122  && m_sheetNumber > 1 )
123  continue;
124 
125  COLOR4D color = wsItem->GetItemColor();
126 
127  pensize = wsItem->GetPenSizeUi();
128 
129  switch( wsItem->GetType() )
130  {
132  {
134  bool multilines = false;
135 
136  if( wsText->m_SpecialMode )
137  wsText->m_FullText = wsText->m_TextBase;
138  else
139  {
140  wsText->m_FullText = BuildFullText( wsText->m_TextBase );
141  multilines = wsText->ReplaceAntiSlashSequence();
142  }
143 
144  if( wsText->m_FullText.IsEmpty() )
145  break;
146 
147  if( pensize == 0 )
148  pensize = m_penSize;
149 
150  wsText->SetConstrainedTextSize();
151  wxSize textsize;
152 
153  textsize.x = KiROUND( wsText->m_ConstrainedTextSize.x
155  textsize.y = KiROUND( wsText->m_ConstrainedTextSize.y
157 
158  if( wsText->IsBold())
159  pensize = GetPenSizeForBold( std::min( textsize.x, textsize.y ) );
160 
161  for( int jj = 0; jj < wsText->m_RepeatCount; jj++)
162  {
163  if( jj && ! wsText->IsInsidePage( jj ) )
164  continue;
165 
166  gtext = new WS_DRAW_ITEM_TEXT( wsText, wsText->m_FullText,
167  wsText->GetStartPosUi( jj ),
168  textsize, pensize, color,
169  wsText->IsItalic(),
170  wsText->IsBold() );
171  Append( gtext );
172  gtext->SetMultilineAllowed( multilines );
173  wsText->TransfertSetupToGraphicText( gtext );
174 
175  // Increment label for the next text
176  // (has no meaning for multiline texts)
177  if( wsText->m_RepeatCount > 1 && !multilines )
178  wsText->IncrementLabel( (jj+1)*wsText->m_IncrementLabel);
179  }
180  }
181  break;
182 
184  if( pensize == 0 )
185  pensize = m_penSize;
186 
187  for( int jj = 0; jj < wsItem->m_RepeatCount; jj++ )
188  {
189  if( jj && ! wsItem->IsInsidePage( jj ) )
190  continue;
191  Append( new WS_DRAW_ITEM_LINE( wsItem, wsItem->GetStartPosUi( jj ),
192  wsItem->GetEndPosUi( jj ),
193  pensize, color ) );
194  }
195  break;
196 
198  if( pensize == 0 )
199  pensize = m_penSize;
200 
201  for( int jj = 0; jj < wsItem->m_RepeatCount; jj++ )
202  {
203  if( jj && ! wsItem->IsInsidePage( jj ) )
204  break;
205 
206  Append( new WS_DRAW_ITEM_RECT( wsItem, wsItem->GetStartPosUi( jj ),
207  wsItem->GetEndPosUi( jj ),
208  pensize, color ) );
209  }
210  break;
211 
213  {
216  for( int jj = 0; jj < wsItem->m_RepeatCount; jj++ )
217  {
218  if( jj && ! wsItem->IsInsidePage( jj ) )
219  continue;
220 
221  for( int kk = 0; kk < wspoly->GetPolyCount(); kk++ )
222  {
223  const bool fill = true;
224  WS_DRAW_ITEM_POLYGON* poly = new WS_DRAW_ITEM_POLYGON( wspoly,
225  wspoly->GetStartPosUi( jj ),
226  fill, pensize, color );
227  Append( poly );
228 
229  // Create polygon outline
230  unsigned ist = wspoly->GetPolyIndexStart( kk );
231  unsigned iend = wspoly->GetPolyIndexEnd( kk );
232  while( ist <= iend )
233  poly->m_Corners.push_back(
234  wspoly->GetCornerPositionUi( ist++, jj ) );
235 
236  }
237  }
238  }
239  break;
240 
242 
243  ((WORKSHEET_DATAITEM_BITMAP*)wsItem)->SetPixelScaleFactor();
244 
245  for( int jj = 0; jj < wsItem->m_RepeatCount; jj++ )
246  {
247  if( jj && ! wsItem->IsInsidePage( jj ) )
248  continue;
249 
250  Append( new WS_DRAW_ITEM_BITMAP( wsItem,
251  wsItem->GetStartPosUi( jj ) ) );
252  }
253  break;
254 
255  }
256  }
257 }
258 
int Mm2mils(double x)
Convert mm to mils.
Definition: base_units.h:41
void TransfertSetupToGraphicText(WS_DRAW_ITEM_TEXT *aGText)
transfert the text justification and orientation to aGText
const wxString * m_paperFormat
static int KiROUND(double v)
KiROUND rounds a floating point number to an int using "round halfway cases away from zero"...
Definition: common.h:107
const TITLE_BLOCK * m_titleBlock
description of graphic items and texts to build a title block
void BuildWorkSheetGraphicList(const PAGE_INFO &aPageInfo, const TITLE_BLOCK &aTitleBlock, COLOR4D aColor, COLOR4D aAltColor)
Function BuildWorkSheetGraphicList is a core function for drawing or plotting the page layout with th...
virtual bool IsInsidePage(int ii) const
Class TITLE_BLOCK holds the information shown in the lower right corner of a plot, printout, or editing view.
const wxPoint GetStartPosUi(int ii=0) const
void Append(WS_DRAW_ITEM_BASE *aItem)
void IncrementLabel(int aIncr)
Try to build text wihich is an increment of m_TextBase has meaning only if m_TextBase is a basic text...
static WORKSHEET_LAYOUT & GetTheInstance()
static function: returns the instance of WORKSHEET_LAYOUT used in the application ...
const wxSize & GetSizeMils() const
void SetPageSize(const wxSize &aPageSize)
Function SetPageSize Set the size of the page layout.
WORKSHEET_DATAITEM * GetItem(unsigned aIdx) const
Class PAGE_INFO describes the page size and margins of a paper page on which to eventually print or p...
unsigned GetPolyIndexStart(unsigned aContour) const
const wxPoint GetEndPosUi(int ii=0) const
std::vector< wxPoint > m_Corners
void SetMultilineAllowed(bool aAllow)
Function SetMultiLineAllowed.
Definition: eda_text.h:187
WORKSHEET_LAYOUT handles the graphic items list to draw/plot the title block and other items (page re...
#define milsTomm
unsigned GetCount() const
const wxPoint GetCornerPositionUi(unsigned aIdx, int aRepeat=0) const
int GetPenSizeForBold(int aTextSize)
Function GetPensizeForBold.
Definition: drawtxt.cpp:49
wxString BuildFullText(const wxString &aTextbase)
Function BuildFullText returns the full text corresponding to the aTextbase, after replacing format s...
Definition: worksheet.cpp:129
void SetConstrainedTextSize()
Calculates m_ConstrainedTextSize from m_TextSize to keep the X size and the full Y size of the text s...
WS_ItemType GetType() const
unsigned GetPolyIndexEnd(unsigned aContour) const
void SetMargins(const wxPoint &aLTmargin, const wxPoint &aRBmargin)
Function SetMargins Set the left top margin and the right bottom margin of the page layout...
This file is part of the common libary.
classes and function to generate graphics to plt or draw titles blocks and frame references ...
const wxString & GetType() const
bool ReplaceAntiSlashSequence()
Replace the '\''n' sequence by EOL and the sequence '\''\' by only one '\' inside m_FullText...
void SetPageLayout(const wxString &aFullFileName=wxEmptyString, bool Append=false)
Populates the list with a custom layout, or the default layout, if no custom layout available...
#define min(a, b)
Definition: auxiliary.h:85
Class COLOR4D is the color representation with 4 components: red, green, blue, alpha.
Definition: color4d.h:39