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, see <https://www.gnu.org/licenses/>.
19 */
20
21#include <page_info.h>
22#include <macros.h>
23#include <eda_units.h>
24#include <richio.h> // for OUTPUTFORMATTER and IO_ERROR
25#include <string_utils.h>
26#include <magic_enum.hpp>
27
28
29// Standard page sizes in mils, all constants
30// see: https://lists.launchpad.net/kicad-developers/msg07389.html
31// also see: wx/defs.h
32
33// local readability macro for millimeter wxSize
34#define MMsize( x, y ) VECTOR2D( EDA_UNIT_UTILS::Mm2mils( x ), EDA_UNIT_UTILS::Mm2mils( y ) )
35
36// List of page formats.
37// they are prefixed by "_HKI" (already in use for hotkeys) instead of "_",
38// because we need both the translated and the not translated version.
39// when displayed in dialog we should explicitly call wxGetTranslation()
40#define _HKI( x ) wxT( x )
41
42std::vector<PAGE_INFO> PAGE_INFO::standardPageSizes = {
43 // All MUST be defined as landscape.
44 PAGE_INFO( MMsize( 210, 148 ), PAGE_SIZE_TYPE::A5, wxPAPER_A5, _HKI( "A5 148 x 210mm" ) ),
45 PAGE_INFO( MMsize( 297, 210 ), PAGE_SIZE_TYPE::A4, wxPAPER_A4, _HKI( "A4 210 x 297mm" ) ),
46 PAGE_INFO( MMsize( 420, 297 ), PAGE_SIZE_TYPE::A3, wxPAPER_A3, _HKI( "A3 297 x 420mm" ) ),
47 PAGE_INFO( MMsize( 594, 420 ), PAGE_SIZE_TYPE::A2, wxPAPER_A2, _HKI( "A2 420 x 594mm" ) ),
48 PAGE_INFO( MMsize( 841, 594 ), PAGE_SIZE_TYPE::A1, wxPAPER_A1, _HKI( "A1 594 x 841mm" ) ),
49 PAGE_INFO( MMsize( 1189, 841 ), PAGE_SIZE_TYPE::A0, wxPAPER_A0, _HKI( "A0 841 x 1189mm" ) ),
50 PAGE_INFO( VECTOR2D( 11000, 8500 ), PAGE_SIZE_TYPE::A, wxPAPER_LETTER, _HKI( "A 8.5 x 11in" ) ),
51 PAGE_INFO( VECTOR2D( 17000, 11000 ), PAGE_SIZE_TYPE::B, wxPAPER_TABLOID, _HKI( "B 11 x 17in" ) ),
52 PAGE_INFO( VECTOR2D( 22000, 17000 ), PAGE_SIZE_TYPE::C, wxPAPER_CSHEET, _HKI( "C 17 x 22in" ) ),
53 PAGE_INFO( VECTOR2D( 34000, 22000 ), PAGE_SIZE_TYPE::D, wxPAPER_DSHEET, _HKI( "D 22 x 34in" ) ),
54 PAGE_INFO( VECTOR2D( 44000, 34000 ), PAGE_SIZE_TYPE::E, wxPAPER_ESHEET, _HKI( "E 34 x 44in" ) ),
55
56 // US paper sizes
57 PAGE_INFO( VECTOR2D( 32000, 32000 ), PAGE_SIZE_TYPE::GERBER, wxPAPER_NONE ),
58 PAGE_INFO( VECTOR2D( 17000, 11000 ), PAGE_SIZE_TYPE::User, wxPAPER_NONE, _HKI( "User (Custom)" ) ),
59
60 PAGE_INFO( VECTOR2D( 11000, 8500 ), PAGE_SIZE_TYPE::USLetter, wxPAPER_LETTER, _HKI("US Letter 8.5 x 11in") ),
61 PAGE_INFO( VECTOR2D( 14000, 8500 ), PAGE_SIZE_TYPE::USLegal, wxPAPER_LEGAL, _HKI("US Legal 8.5 x 14in") ),
62 PAGE_INFO( VECTOR2D( 17000, 11000 ), PAGE_SIZE_TYPE::USLedger, wxPAPER_TABLOID, _HKI("US Ledger 11 x 17in") )
63};
64
65// Custom paper size for next instantiation of type "User"
66double PAGE_INFO::s_user_width = 17000;
67double PAGE_INFO::s_user_height = 11000;
68
69
71{
72 // update m_portrait based on orientation of m_size.x and m_size.y
73 m_portrait = ( m_size.y > m_size.x );
74}
75
76
77PAGE_INFO::PAGE_INFO( const VECTOR2D& aSizeMils, const PAGE_SIZE_TYPE& aType, wxPaperSize aPaperId,
78 const wxString& aDescription ) :
79 m_type( aType ),
80 m_size( aSizeMils ),
81 m_paper_id( aPaperId ),
82 m_description( aDescription )
83{
85
86 // This constructor is protected, and only used by const PAGE_INFO's known
87 // only to class implementation, so no further changes to "this" object are
88 // expected.
89}
90
91
92PAGE_INFO::PAGE_INFO( PAGE_SIZE_TYPE aType, bool aIsPortrait ) :
95 m_portrait( false ),
96 m_paper_id( wxPAPER_NONE )
97{
98 SetType( aType, aIsPortrait );
99}
100
101
102bool PAGE_INFO::SetType( const wxString& aPageSize, bool aIsPortrait )
103{
104 auto type = magic_enum::enum_cast<PAGE_SIZE_TYPE>( aPageSize.ToStdString(), magic_enum::case_insensitive );
105
106 if( !type.has_value() )
107 return false;
108
109 return SetType( type.value(), aIsPortrait );
110}
111
112
113bool PAGE_INFO::SetType( PAGE_SIZE_TYPE aType, bool aIsPortrait )
114{
115 bool rc = true;
116
117 auto result = std::find_if( standardPageSizes.begin(), standardPageSizes.end(),
118 [aType]( const PAGE_INFO& p )
119 {
120 return p.m_type == aType;
121 } );
122
123 if( result != standardPageSizes.end() )
124 *this = *result;
125 else
126 rc = false;
127
128 if( aType == PAGE_SIZE_TYPE::User )
129 {
131 m_paper_id = wxPAPER_NONE;
134
136 }
137
138 if( aIsPortrait )
139 {
140 // all private PAGE_INFOs are landscape, must swap x and y
141 std::swap( m_size.y, m_size.x );
143 }
144
145 return rc;
146}
147
148
150{
151 std::string typeStr( magic_enum::enum_name( m_type ) );
152 return wxString( typeStr );
153}
154
155
157{
159}
160
161
162void PAGE_INFO::SetPortrait( bool aIsPortrait )
163{
164 if( m_portrait != aIsPortrait )
165 {
166 // swap x and y in m_size
167 std::swap( m_size.y, m_size.x );
168
169 m_portrait = aIsPortrait;
170
171 // margins are not touched, do that if you want
172 }
173}
174
175
176static double clampWidth( double aWidthInMils )
177{
178 if( aWidthInMils < 10 )
179 aWidthInMils = 10;
180
181 return aWidthInMils;
182}
183
184
185static double clampHeight( double aHeightInMils )
186{
187 if( aHeightInMils < 10.0 )
188 aHeightInMils = 10.0;
189
190 return aHeightInMils;
191}
192
193
194void PAGE_INFO::SetCustomWidthMils( double aWidthInMils )
195{
196 s_user_width = clampWidth( aWidthInMils );
197}
198
199
200void PAGE_INFO::SetCustomHeightMils( double aHeightInMils )
201{
202 s_user_height = clampHeight( aHeightInMils );
203}
204
205
206void PAGE_INFO::SetWidthMils( double aWidthInMils )
207{
208 if( m_size.x != aWidthInMils )
209 {
210 m_size.x = clampWidth( aWidthInMils );
211
213 m_paper_id = wxPAPER_NONE;
214
216 }
217}
218
219
220void PAGE_INFO::SetHeightMils( double aHeightInMils )
221{
222 if( m_size.y != aHeightInMils )
223 {
224 m_size.y = clampHeight( aHeightInMils );
225
227 m_paper_id = wxPAPER_NONE;
228
230 }
231}
232
233
234void PAGE_INFO::Format( OUTPUTFORMATTER* aFormatter ) const
235{
236 std::string typeStr( magic_enum::enum_name( GetType() ) );
237 aFormatter->Print( "(paper %s", aFormatter->Quotew( typeStr ).c_str() );
238
239 // The page dimensions are only required for user defined page sizes.
240 // Internally, the page size is in mils
242 {
243 aFormatter->Print( " %s %s",
244 FormatDouble2Str( GetWidthMils() * 25.4 / 1000.0 ).c_str(),
245 FormatDouble2Str( GetHeightMils() * 25.4 / 1000.0 ).c_str() );
246 }
247
248 if( !IsCustom() && IsPortrait() )
249 aFormatter->Print( " portrait" );
250
251 aFormatter->Print( ")" );
252}
253
254
255const std::vector<PAGE_INFO>& PAGE_INFO::GetPageFormatsList()
256{
258}
An interface used to output 8 bit text in a convenient way.
Definition richio.h:291
std::string Quotew(const wxString &aWrapee) const
Definition richio.cpp:507
int PRINTF_FUNC_N Print(int nestLevel, const char *fmt,...)
Format and write text to the output stream.
Definition richio.cpp:422
Describe the page size and margins of a paper page on which to eventually print or plot.
Definition page_info.h:75
VECTOR2D m_size
mils
Definition page_info.h:223
wxString GetTypeAsString() const
bool m_portrait
true if portrait, false if landscape
Definition page_info.h:225
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:231
void SetHeightMils(double aHeightInMils)
static std::vector< PAGE_INFO > standardPageSizes
Definition page_info.h:42
static double s_user_height
Definition page_info.h:230
static const std::vector< PAGE_INFO > & GetPageFormatsList()
void updatePortrait()
Definition page_info.cpp:70
double GetHeightMils() const
Definition page_info.h:143
PAGE_INFO(PAGE_SIZE_TYPE aType=PAGE_SIZE_TYPE::A3, bool IsPortrait=false)
Definition page_info.cpp:92
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:228
double GetWidthMils() const
Definition page_info.h:138
bool IsCustom() const
bool IsPortrait() const
Definition page_info.h:124
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:98
PAGE_SIZE_TYPE m_type
paper type: A4, A3, etc.
Definition page_info.h:222
wxPaperSize m_paper_id
wx' style paper id.
Definition page_info.h:227
This file contains miscellaneous commonly used macros and functions.
#define MMsize(x, y)
Definition page_info.cpp:34
#define _HKI(x)
Definition page_info.cpp:40
static double clampHeight(double aHeightInMils)
static double clampWidth(double aWidthInMils)
PAGE_SIZE_TYPE
Definition page_info.h:46
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:682