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 ) :
99 m_portrait( false ),
100 m_paper_id( wxPAPER_NONE )
101{
102 SetType( aType, aIsPortrait );
103}
104
105
106bool PAGE_INFO::SetType( const wxString& aPageSize, bool aIsPortrait )
107{
108 auto type = magic_enum::enum_cast<PAGE_SIZE_TYPE>( aPageSize.ToStdString(), magic_enum::case_insensitive );
109
110 if( !type.has_value() )
111 return false;
112
113 return SetType( type.value(), aIsPortrait );
114}
115
116
117bool PAGE_INFO::SetType( PAGE_SIZE_TYPE aType, bool aIsPortrait )
118{
119 bool rc = true;
120
121 auto result = std::find_if( standardPageSizes.begin(), standardPageSizes.end(),
122 [aType]( const PAGE_INFO& p )
123 {
124 return p.m_type == aType;
125 } );
126
127 if( result != standardPageSizes.end() )
128 *this = *result;
129 else
130 rc = false;
131
132 if( aType == PAGE_SIZE_TYPE::User )
133 {
135 m_paper_id = wxPAPER_NONE;
138
140 }
141
142 if( aIsPortrait )
143 {
144 // all private PAGE_INFOs are landscape, must swap x and y
145 std::swap( m_size.y, m_size.x );
147 }
148
149 return rc;
150}
151
152
154{
155 std::string typeStr( magic_enum::enum_name( m_type ) );
156 return wxString( typeStr );
157}
158
159
161{
163}
164
165
166void PAGE_INFO::SetPortrait( bool aIsPortrait )
167{
168 if( m_portrait != aIsPortrait )
169 {
170 // swap x and y in m_size
171 std::swap( m_size.y, m_size.x );
172
173 m_portrait = aIsPortrait;
174
175 // margins are not touched, do that if you want
176 }
177}
178
179
180static double clampWidth( double aWidthInMils )
181{
182 if( aWidthInMils < 10 )
183 aWidthInMils = 10;
184
185 return aWidthInMils;
186}
187
188
189static double clampHeight( double aHeightInMils )
190{
191 if( aHeightInMils < 10.0 )
192 aHeightInMils = 10.0;
193
194 return aHeightInMils;
195}
196
197
198void PAGE_INFO::SetCustomWidthMils( double aWidthInMils )
199{
200 s_user_width = clampWidth( aWidthInMils );
201}
202
203
204void PAGE_INFO::SetCustomHeightMils( double aHeightInMils )
205{
206 s_user_height = clampHeight( aHeightInMils );
207}
208
209
210void PAGE_INFO::SetWidthMils( double aWidthInMils )
211{
212 if( m_size.x != aWidthInMils )
213 {
214 m_size.x = clampWidth( aWidthInMils );
215
217 m_paper_id = wxPAPER_NONE;
218
220 }
221}
222
223
224void PAGE_INFO::SetHeightMils( double aHeightInMils )
225{
226 if( m_size.y != aHeightInMils )
227 {
228 m_size.y = clampHeight( aHeightInMils );
229
231 m_paper_id = wxPAPER_NONE;
232
234 }
235}
236
237
238void PAGE_INFO::Format( OUTPUTFORMATTER* aFormatter ) const
239{
240 std::string typeStr( magic_enum::enum_name( GetType() ) );
241 aFormatter->Print( "(paper %s", aFormatter->Quotew( typeStr ).c_str() );
242
243 // The page dimensions are only required for user defined page sizes.
244 // Internally, the page size is in mils
246 {
247 aFormatter->Print( " %s %s",
248 FormatDouble2Str( GetWidthMils() * 25.4 / 1000.0 ).c_str(),
249 FormatDouble2Str( GetHeightMils() * 25.4 / 1000.0 ).c_str() );
250 }
251
252 if( !IsCustom() && IsPortrait() )
253 aFormatter->Print( " portrait" );
254
255 aFormatter->Print( ")" );
256}
257
258
259const std::vector<PAGE_INFO>& PAGE_INFO::GetPageFormatsList()
260{
262}
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:550
int PRINTF_FUNC_N Print(int nestLevel, const char *fmt,...)
Format and write text to the output stream.
Definition richio.cpp:465
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