KiCad PCB EDA Suite
class_page_info.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) 2012 SoftPLC Corporation, Dick Hollenbeck <dick@softplc.com>
5  * Copyright (C) 2012 KiCad Developers, see CHANGELOG.TXT for contributors.
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 <class_page_info.h>
28 #include <macros.h>
29 
30 
31 // late arriving wxPAPER_A0, wxPAPER_A1
32 #if wxABI_VERSION >= 20999
33  #define PAPER_A0 wxPAPER_A0
34  #define PAPER_A1 wxPAPER_A1
35 #else
36  #define PAPER_A0 wxPAPER_A2
37  #define PAPER_A1 wxPAPER_A2
38 #endif
39 
40 
41 // Standard paper sizes nicknames.
42 const wxChar PAGE_INFO::A4[] = wxT( "A4" );
43 const wxChar PAGE_INFO::A3[] = wxT( "A3" );
44 const wxChar PAGE_INFO::A2[] = wxT( "A2" );
45 const wxChar PAGE_INFO::A1[] = wxT( "A1" );
46 const wxChar PAGE_INFO::A0[] = wxT( "A0" );
47 const wxChar PAGE_INFO::A[] = wxT( "A" );
48 const wxChar PAGE_INFO::B[] = wxT( "B" );
49 const wxChar PAGE_INFO::C[] = wxT( "C" );
50 const wxChar PAGE_INFO::D[] = wxT( "D" );
51 const wxChar PAGE_INFO::E[] = wxT( "E" );
52 
53 const wxChar PAGE_INFO::GERBER[] = wxT( "GERBER" );
54 const wxChar PAGE_INFO::USLetter[] = wxT( "USLetter" );
55 const wxChar PAGE_INFO::USLegal[] = wxT( "USLegal" );
56 const wxChar PAGE_INFO::USLedger[] = wxT( "USLedger" );
57 const wxChar PAGE_INFO::Custom[] = wxT( "User" );
58 
59 
60 // Standard page sizes in mils, all constants
61 // see: https://lists.launchpad.net/kicad-developers/msg07389.html
62 // also see: wx/defs.h
63 
64 // local readability macro for millimeter wxSize
65 #define MMsize( x, y ) wxSize( Mm2mils( x ), Mm2mils( y ) )
66 
67 // All MUST be defined as landscape.
68 const PAGE_INFO PAGE_INFO::pageA4( MMsize( 297, 210 ), wxT( "A4" ), wxPAPER_A4 );
69 const PAGE_INFO PAGE_INFO::pageA3( MMsize( 420, 297 ), wxT( "A3" ), wxPAPER_A3 );
70 const PAGE_INFO PAGE_INFO::pageA2( MMsize( 594, 420 ), wxT( "A2" ), wxPAPER_A2 );
71 const PAGE_INFO PAGE_INFO::pageA1( MMsize( 841, 594 ), wxT( "A1" ), PAPER_A1 );
72 const PAGE_INFO PAGE_INFO::pageA0( MMsize( 1189, 841 ), wxT( "A0" ), PAPER_A0 );
73 
74 const PAGE_INFO PAGE_INFO::pageA( wxSize( 11000, 8500 ), wxT( "A" ), wxPAPER_LETTER );
75 const PAGE_INFO PAGE_INFO::pageB( wxSize( 17000, 11000 ), wxT( "B" ), wxPAPER_TABLOID );
76 const PAGE_INFO PAGE_INFO::pageC( wxSize( 22000, 17000 ), wxT( "C" ), wxPAPER_CSHEET );
77 const PAGE_INFO PAGE_INFO::pageD( wxSize( 34000, 22000 ), wxT( "D" ), wxPAPER_DSHEET );
78 const PAGE_INFO PAGE_INFO::pageE( wxSize( 44000, 34000 ), wxT( "E" ), wxPAPER_ESHEET );
79 
80 const PAGE_INFO PAGE_INFO::pageGERBER( wxSize( 32000, 32000 ), wxT( "GERBER" ), wxPAPER_NONE );
81 const PAGE_INFO PAGE_INFO::pageUser( wxSize( 17000, 11000 ), Custom, wxPAPER_NONE );
82 
83 // US paper sizes
84 const PAGE_INFO PAGE_INFO::pageUSLetter( wxSize( 11000, 8500 ), wxT( "USLetter" ), wxPAPER_LETTER );
85 const PAGE_INFO PAGE_INFO::pageUSLegal( wxSize( 14000, 8500 ), wxT( "USLegal" ), wxPAPER_LEGAL );
86 const PAGE_INFO PAGE_INFO::pageUSLedger( wxSize( 17000, 11000 ), wxT( "USLedger" ), wxPAPER_TABLOID );
87 
88 // Custom paper size for next instantiation of type "User"
89 int PAGE_INFO::s_user_width = 17000;
90 int PAGE_INFO::s_user_height = 11000;
91 
92 
94 {
95  // update m_portrait based on orientation of m_size.x and m_size.y
96  m_portrait = ( m_size.y > m_size.x );
97 }
98 
99 
100 PAGE_INFO::PAGE_INFO( const wxSize& aSizeMils, const wxString& aType, wxPaperSize aPaperId ) :
101  m_type( aType ), m_size( aSizeMils ), m_paper_id( aPaperId )
102 {
103  updatePortrait();
104 
105  // This constructor is protected, and only used by const PAGE_INFO's known
106  // only to class implementation, so no further changes to "this" object are
107  // expected.
108 }
109 
110 
111 PAGE_INFO::PAGE_INFO( const wxString& aType, bool IsPortrait )
112 {
113  SetType( aType, IsPortrait );
114 }
115 
116 
117 bool PAGE_INFO::SetType( const wxString& aType, bool IsPortrait )
118 {
119  bool rc = true;
120 
121  // all are landscape initially
122  if( aType == pageA4.GetType() )
123  *this = pageA4;
124  else if( aType == pageA3.GetType() )
125  *this = pageA3;
126  else if( aType == pageA2.GetType() )
127  *this = pageA2;
128  else if( aType == pageA1.GetType() )
129  *this = pageA1;
130  else if( aType == pageA0.GetType() )
131  *this = pageA0;
132  else if( aType == pageA.GetType() )
133  *this = pageA;
134  else if( aType == pageB.GetType() )
135  *this = pageB;
136  else if( aType == pageC.GetType() )
137  *this = pageC;
138  else if( aType == pageD.GetType() )
139  *this = pageD;
140  else if( aType == pageE.GetType() )
141  *this = pageE;
142  else if( aType == pageGERBER.GetType() )
143  *this = pageGERBER;
144  else if( aType == pageUSLetter.GetType() )
145  *this = pageUSLetter;
146  else if( aType == pageUSLegal.GetType() )
147  *this = pageUSLegal;
148  else if( aType == pageUSLedger.GetType() )
149  *this = pageUSLedger;
150  else if( aType == pageUser.GetType() )
151  {
152  // pageUser is const, and may not and does not hold the custom size,
153  // so customize *this later
154  *this = pageUser;
155 
156  // customize:
157  m_size.x = s_user_width;
158  m_size.y = s_user_height;
159 
160  updatePortrait();
161  }
162  else
163  rc = false;
164 
165  if( IsPortrait )
166  {
167  // all private PAGE_INFOs are landscape, must swap x and y
168  m_size = wxSize( m_size.y, m_size.x );
169  updatePortrait();
170  }
171 
172  return rc;
173 }
174 
175 
177 {
178  return m_type == Custom;
179 }
180 
181 
182 void PAGE_INFO::SetPortrait( bool isPortrait )
183 {
184  if( m_portrait != isPortrait )
185  {
186  // swap x and y in m_size
187  m_size = wxSize( m_size.y, m_size.x );
188 
189  m_portrait = isPortrait;
190 
191  // margins are not touched, do that if you want
192  }
193 }
194 
195 
196 static int clampWidth( int aWidthInMils )
197 {
198 /* was giving EESCHEMA single component SVG plotter grief
199  However a minimal test is made to avoid values that crashes Kicad
200  if( aWidthInMils < 4000 ) // 4" is about a baseball card
201  aWidthInMils = 4000;
202  else if( aWidthInMils > 44000 ) //44" is plotter size
203  aWidthInMils = 44000;
204 */
205  if( aWidthInMils < 10 )
206  aWidthInMils = 10;
207  return aWidthInMils;
208 }
209 
210 
211 static int clampHeight( int aHeightInMils )
212 {
213 /* was giving EESCHEMA single component SVG plotter grief
214  clamping is best done at the UI, i.e. dialog, levels
215  However a minimal test is made to avoid values that crashes Kicad
216  if( aHeightInMils < 4000 )
217  aHeightInMils = 4000;
218  else if( aHeightInMils > 44000 )
219  aHeightInMils = 44000;
220 */
221  if( aHeightInMils < 10 )
222  aHeightInMils = 10;
223  return aHeightInMils;
224 }
225 
226 
227 void PAGE_INFO::SetCustomWidthMils( int aWidthInMils )
228 {
229  s_user_width = clampWidth( aWidthInMils );
230 }
231 
232 
233 void PAGE_INFO::SetCustomHeightMils( int aHeightInMils )
234 {
235  s_user_height = clampHeight( aHeightInMils );
236 }
237 
238 
239 void PAGE_INFO::SetWidthMils( int aWidthInMils )
240 {
241  if( m_size.x != aWidthInMils )
242  {
243  m_size.x = clampWidth( aWidthInMils );
244 
245  m_type = Custom;
246  m_paper_id = wxPAPER_NONE;
247 
248  updatePortrait();
249  }
250 }
251 
252 
253 void PAGE_INFO::SetHeightMils( int aHeightInMils )
254 {
255  if( m_size.y != aHeightInMils )
256  {
257  m_size.y = clampHeight( aHeightInMils );
258 
259  m_type = Custom;
260  m_paper_id = wxPAPER_NONE;
261 
262  updatePortrait();
263  }
264 }
265 
266 
267 void PAGE_INFO::Format( OUTPUTFORMATTER* aFormatter, int aNestLevel, int aControlBits ) const
268 {
269  aFormatter->Print( aNestLevel, "(page %s", aFormatter->Quotew( GetType() ).c_str() );
270 
271  // The page dimensions are only required for user defined page sizes.
272  // Internally, the page size is in mils
273  if( GetType() == PAGE_INFO::Custom )
274  aFormatter->Print( 0, " %g %g",
275  GetWidthMils() * 25.4 / 1000.0,
276  GetHeightMils() * 25.4 / 1000.0 );
277 
278  if( !IsCustom() && IsPortrait() )
279  aFormatter->Print( 0, " portrait" );
280 
281  aFormatter->Print( 0, ")\n" );
282 }
static const PAGE_INFO pageA4
static const PAGE_INFO pageGERBER
static const wxChar A[]
static const wxChar E[]
void updatePortrait()
#define MMsize(x, y)
static const wxChar GERBER[]
static const wxChar A3[]
wxSize m_size
mils
static const PAGE_INFO pageA1
static void SetCustomWidthMils(int aWidthInMils)
Function SetCustomWidthMils sets the width of Custom page in mils, for any custom page constructed or...
static const PAGE_INFO pageA2
static const PAGE_INFO pageUSLetter
Class OUTPUTFORMATTER is an important interface (abstract class) used to output 8 bit text in a conve...
Definition: richio.h:327
static const wxChar Custom[]
"User" defined page type
static const wxChar D[]
static const PAGE_INFO pageC
wxString m_type
paper type: A4, A3, etc.
static const PAGE_INFO pageUser
This file contains miscellaneous commonly used macros and functions.
static const PAGE_INFO pageA
static const PAGE_INFO pageB
static const PAGE_INFO pageE
bool IsPortrait() const
static int clampHeight(int aHeightInMils)
static const wxChar USLegal[]
static const wxChar B[]
static const wxChar C[]
Class PAGE_INFO describes the page size and margins of a paper page on which to eventually print or p...
static const PAGE_INFO pageUSLedger
bool m_portrait
true if portrait, false if landscape
static const wxChar A4[]
bool SetType(const wxString &aStandardPageDescriptionName, bool IsPortrait=false)
Function SetType sets the name of the page type and also the sizes and margins commonly associated wi...
std::string Quotew(const wxString &aWrapee)
Definition: richio.cpp:486
int GetWidthMils() const
void SetPortrait(bool isPortrait)
Function SetPortrait will rotate the paper page 90 degrees.
static int clampWidth(int aWidthInMils)
#define PAPER_A1
void SetHeightMils(int aHeightInMils)
static const wxChar USLedger[]
static const wxChar A1[]
void Format(OUTPUTFORMATTER *aFormatter, int aNestLevel, int aControlBits) const
Function GetStandardSizes returns the standard page types, such as "A4", "A3", etc.
bool IsCustom() const
Function IsCustom returns true if the type is Custom.
static const wxChar USLetter[]
void SetWidthMils(int aWidthInMils)
static const PAGE_INFO pageUSLegal
The common library.
static int s_user_height
static const wxChar A2[]
const wxString & GetType() const
int PRINTF_FUNC Print(int nestLevel, const char *fmt,...)
Function Print formats and writes text to the output stream.
Definition: richio.cpp:408
#define PAPER_A0
static int s_user_width
PAGE_INFO(const wxString &aType=PAGE_INFO::A3, bool IsPortrait=false)
static const PAGE_INFO pageA3
static const PAGE_INFO pageA0
int GetHeightMils() const
wxPaperSize m_paper_id
wx' style paper id.
static void SetCustomHeightMils(int aHeightInMils)
Function SetCustomHeightMils sets the height of Custom page in mils, for any custom page constructed ...
static const PAGE_INFO pageD
static const wxChar A0[]