KiCad PCB EDA Suite
Loading...
Searching...
No Matches
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 <[email protected]>
5 * Copyright The KiCad Developers, see AUTHORS.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#include <page_info.h>
26#include <macros.h>
27#include <eda_units.h>
28#include <richio.h> // for OUTPUTFORMATTER and IO_ERROR
29#include <string_utils.h>
30#include <magic_enum.hpp>
31
32
33// Standard page sizes in mils, all constants
34// see: https://lists.launchpad.net/kicad-developers/msg07389.html
35// also see: wx/defs.h
36
37// local readability macro for millimeter wxSize
38#define MMsize( x, y ) VECTOR2D( EDA_UNIT_UTILS::Mm2mils( x ), EDA_UNIT_UTILS::Mm2mils( y ) )
39
40// List of page formats.
41// they are prefixed by "_HKI" (already in use for hotkeys) instead of "_",
42// because we need both the translated and the not translated version.
43// when displayed in dialog we should explicitly call wxGetTranslation()
44#define _HKI( x ) wxT( x )
45
46std::vector<PAGE_INFO> PAGE_INFO::standardPageSizes = {
47 // All MUST be defined as landscape.
48 PAGE_INFO( MMsize( 210, 148 ), PAGE_SIZE_TYPE::A5, wxPAPER_A5, _HKI( "A5 148 x 210mm" ) ),
49 PAGE_INFO( MMsize( 297, 210 ), PAGE_SIZE_TYPE::A4, wxPAPER_A4, _HKI( "A4 210 x 297mm" ) ),
50 PAGE_INFO( MMsize( 420, 297 ), PAGE_SIZE_TYPE::A3, wxPAPER_A3, _HKI( "A3 297 x 420mm" ) ),
51 PAGE_INFO( MMsize( 594, 420 ), PAGE_SIZE_TYPE::A2, wxPAPER_A2, _HKI( "A2 420 x 594mm" ) ),
52 PAGE_INFO( MMsize( 841, 594 ), PAGE_SIZE_TYPE::A1, wxPAPER_A1, _HKI( "A1 594 x 841mm" ) ),
53 PAGE_INFO( MMsize( 1189, 841 ), PAGE_SIZE_TYPE::A0, wxPAPER_A0, _HKI( "A0 841 x 1189mm" ) ),
54 PAGE_INFO( VECTOR2D( 11000, 8500 ), PAGE_SIZE_TYPE::A, wxPAPER_LETTER, _HKI( "A 8.5 x 11in" ) ),
55 PAGE_INFO( VECTOR2D( 17000, 11000 ), PAGE_SIZE_TYPE::B, wxPAPER_TABLOID, _HKI( "B 11 x 17in" ) ),
56 PAGE_INFO( VECTOR2D( 22000, 17000 ), PAGE_SIZE_TYPE::C, wxPAPER_CSHEET, _HKI( "C 17 x 22in" ) ),
57 PAGE_INFO( VECTOR2D( 34000, 22000 ), PAGE_SIZE_TYPE::D, wxPAPER_DSHEET, _HKI( "D 22 x 34in" ) ),
58 PAGE_INFO( VECTOR2D( 44000, 34000 ), PAGE_SIZE_TYPE::E, wxPAPER_ESHEET, _HKI( "E 34 x 44in" ) ),
59
60 // US paper sizes
61 PAGE_INFO( VECTOR2D( 32000, 32000 ), PAGE_SIZE_TYPE::GERBER, wxPAPER_NONE ),
62 PAGE_INFO( VECTOR2D( 17000, 11000 ), PAGE_SIZE_TYPE::User, wxPAPER_NONE, _HKI( "User (Custom)" ) ),
63
64 PAGE_INFO( VECTOR2D( 11000, 8500 ), PAGE_SIZE_TYPE::USLetter, wxPAPER_LETTER, _HKI("US Letter 8.5 x 11in") ),
65 PAGE_INFO( VECTOR2D( 14000, 8500 ), PAGE_SIZE_TYPE::USLegal, wxPAPER_LEGAL, _HKI("US Legal 8.5 x 14in") ),
66 PAGE_INFO( VECTOR2D( 17000, 11000 ), PAGE_SIZE_TYPE::USLedger, wxPAPER_TABLOID, _HKI("US Ledger 11 x 17in") )
67};
68
69// Custom paper size for next instantiation of type "User"
70double PAGE_INFO::s_user_width = 17000;
71double PAGE_INFO::s_user_height = 11000;
72
73
75{
76 // update m_portrait based on orientation of m_size.x and m_size.y
77 m_portrait = ( m_size.y > m_size.x );
78}
79
80
81PAGE_INFO::PAGE_INFO( const VECTOR2D& aSizeMils, const PAGE_SIZE_TYPE& aType, wxPaperSize aPaperId,
82 const wxString& aDescription ) :
83 m_type( aType ),
84 m_size( aSizeMils ),
85 m_paper_id( aPaperId ),
86 m_description( aDescription )
87{
89
90 // This constructor is protected, and only used by const PAGE_INFO's known
91 // only to class implementation, so no further changes to "this" object are
92 // expected.
93}
94
95
96PAGE_INFO::PAGE_INFO( PAGE_SIZE_TYPE aType, bool aIsPortrait ) :
98 m_paper_id( wxPAPER_NONE )
99{
100 SetType( aType, aIsPortrait );
101}
102
103
104bool PAGE_INFO::SetType( const wxString& aPageSize, bool aIsPortrait )
105{
106 auto type = magic_enum::enum_cast<PAGE_SIZE_TYPE>( aPageSize.ToStdString(), magic_enum::case_insensitive );
107
108 if( !type.has_value() )
109 return false;
110
111 return SetType( type.value(), aIsPortrait );
112}
113
114
115bool PAGE_INFO::SetType( PAGE_SIZE_TYPE aType, bool aIsPortrait )
116{
117 bool rc = true;
118
119 auto result = std::find_if( standardPageSizes.begin(), standardPageSizes.end(),
120 [aType]( const PAGE_INFO& p )
121 {
122 return p.m_type == aType;
123 } );
124
125 if( result != standardPageSizes.end() )
126 {
127 *this = *result;
128 }
129 else
130 {
131 rc = false;
132 }
133
134 if( aType == PAGE_SIZE_TYPE::User )
135 {
137 m_paper_id = wxPAPER_NONE;
140
142 }
143
144 if( aIsPortrait )
145 {
146 // all private PAGE_INFOs are landscape, must swap x and y
147 std::swap( m_size.y, m_size.x );
149 }
150
151 return rc;
152}
153
154
156{
157 std::string typeStr( magic_enum::enum_name( m_type ) );
158 return wxString( typeStr );
159}
160
161
163{
165}
166
167
168void PAGE_INFO::SetPortrait( bool aIsPortrait )
169{
170 if( m_portrait != aIsPortrait )
171 {
172 // swap x and y in m_size
173 std::swap( m_size.y, m_size.x );
174
175 m_portrait = aIsPortrait;
176
177 // margins are not touched, do that if you want
178 }
179}
180
181
182static double clampWidth( double aWidthInMils )
183{
184/* was giving EESCHEMA single component SVG plotter grief
185 However a minimal test is made to avoid values that crashes KiCad
186 if( aWidthInMils < 4000 ) // 4" is about a baseball card
187 aWidthInMils = 4000;
188 else if( aWidthInMils > 44000 ) //44" is plotter size
189 aWidthInMils = 44000;
190*/
191 if( aWidthInMils < 10 )
192 aWidthInMils = 10;
193
194 return aWidthInMils;
195}
196
197
198static double clampHeight( double aHeightInMils )
199{
200/* was giving EESCHEMA single component SVG plotter grief
201 clamping is best done at the UI, i.e. dialog, levels
202 However a minimal test is made to avoid values that crashes KiCad
203 if( aHeightInMils < 4000 )
204 aHeightInMils = 4000;
205 else if( aHeightInMils > 44000 )
206 aHeightInMils = 44000;
207*/
208 if( aHeightInMils < 10.0 )
209 aHeightInMils = 10.0;
210
211 return aHeightInMils;
212}
213
214
215void PAGE_INFO::SetCustomWidthMils( double aWidthInMils )
216{
217 s_user_width = clampWidth( aWidthInMils );
218}
219
220
221void PAGE_INFO::SetCustomHeightMils( double aHeightInMils )
222{
223 s_user_height = clampHeight( aHeightInMils );
224}
225
226
227void PAGE_INFO::SetWidthMils( double aWidthInMils )
228{
229 if( m_size.x != aWidthInMils )
230 {
231 m_size.x = clampWidth( aWidthInMils );
232
234 m_paper_id = wxPAPER_NONE;
235
237 }
238}
239
240
241void PAGE_INFO::SetHeightMils( double aHeightInMils )
242{
243 if( m_size.y != aHeightInMils )
244 {
245 m_size.y = clampHeight( aHeightInMils );
246
248 m_paper_id = wxPAPER_NONE;
249
251 }
252}
253
254
255void PAGE_INFO::Format( OUTPUTFORMATTER* aFormatter ) const
256{
257 std::string typeStr( magic_enum::enum_name( GetType() ) );
258 aFormatter->Print( "(paper %s", aFormatter->Quotew( typeStr ).c_str() );
259
260 // The page dimensions are only required for user defined page sizes.
261 // Internally, the page size is in mils
263 {
264 aFormatter->Print( " %s %s",
265 FormatDouble2Str( GetWidthMils() * 25.4 / 1000.0 ).c_str(),
266 FormatDouble2Str( GetHeightMils() * 25.4 / 1000.0 ).c_str() );
267 }
268
269 if( !IsCustom() && IsPortrait() )
270 aFormatter->Print( " portrait" );
271
272 aFormatter->Print( ")" );
273}
274
275
276const std::vector<PAGE_INFO>& PAGE_INFO::GetPageFormatsList()
277{
279}
An interface used to output 8 bit text in a convenient way.
Definition richio.h:322
std::string Quotew(const wxString &aWrapee) const
Definition richio.cpp:548
int PRINTF_FUNC_N Print(int nestLevel, const char *fmt,...)
Format and write text to the output stream.
Definition richio.cpp:463
Describe the page size and margins of a paper page on which to eventually print or plot.
Definition page_info.h:79
VECTOR2D m_size
mils
Definition page_info.h:227
wxString GetTypeAsString() const
bool m_portrait
true if portrait, false if landscape
Definition page_info.h:229
void SetPortrait(bool aIsPortrait)
Rotate the paper page 90 degrees.
bool SetType(PAGE_SIZE_TYPE aPageSize, bool aIsPortrait=false)
Set the name of the page type and also the sizes and margins commonly associated with that type name.
static void SetCustomWidthMils(double aWidthInMils)
Set the width of Custom page in mils for any custom page constructed or made via SetType() after maki...
static double s_user_width
Definition page_info.h:235
void SetHeightMils(double aHeightInMils)
static std::vector< PAGE_INFO > standardPageSizes
Definition page_info.h:46
static double s_user_height
Definition page_info.h:234
static const std::vector< PAGE_INFO > & GetPageFormatsList()
void updatePortrait()
Definition page_info.cpp:74
double GetHeightMils() const
Definition page_info.h:147
PAGE_INFO(PAGE_SIZE_TYPE aType=PAGE_SIZE_TYPE::A3, bool IsPortrait=false)
Definition page_info.cpp:96
void Format(OUTPUTFORMATTER *aFormatter) const
Output the page class to aFormatter in s-expression form.
wxString m_description
more human friendly description of page size
Definition page_info.h:232
double GetWidthMils() const
Definition page_info.h:142
bool IsCustom() const
bool IsPortrait() const
Definition page_info.h:128
void SetWidthMils(double aWidthInMils)
static void SetCustomHeightMils(double aHeightInMils)
Set the height of Custom page in mils for any custom page constructed or made via SetType() after mak...
const PAGE_SIZE_TYPE & GetType() const
Definition page_info.h:102
PAGE_SIZE_TYPE m_type
paper type: A4, A3, etc.
Definition page_info.h:226
wxPaperSize m_paper_id
wx' style paper id.
Definition page_info.h:231
This file contains miscellaneous commonly used macros and functions.
#define MMsize(x, y)
Definition page_info.cpp:38
#define _HKI(x)
Definition page_info.cpp:44
static double clampHeight(double aHeightInMils)
static double clampWidth(double aWidthInMils)
PAGE_SIZE_TYPE
Definition page_info.h:50
std::string FormatDouble2Str(double aValue)
Print a float number without using scientific notation and no trailing 0 This function is intended in...
wxString result
Test unit parsing edge cases and error handling.
VECTOR2< double > VECTOR2D
Definition vector2d.h:694