1 /*
2  * This program source code file is part of KiCad, a free EDA CAD application.
3  *
4  * Copyright (C) 2007-2013 SoftPLC Corporation, Dick Hollenbeck <dick@softplc.com>
5  * Copyright (C) 2008-2013 Wayne Stambaugh <stambaughw@gmail.com>
6  * Copyright (C) 2007-2020 KiCad Developers, see AUTHORS.txt for contributors.
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version 2
11  * of the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, you may find one here:
20  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
21  * or you may search the http://www.gnu.org website for the version 2 license,
22  * or you may write to the Free Software Foundation, Inc.,
23  * 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
24  */
25 
26 /**
27  * @file page_info.h
28  */
29 
30 #ifndef PAGE_INFO_H
31 #define PAGE_INFO_H
32 
33 #include <wx/string.h>
34 
35 #include <richio.h>         // for OUTPUTFORMATTER and IO_ERROR
36 #include <base_units.h>     // for IU_PER_MILS
37 
38 /// Min and max page sizes for clamping, in mils.
39 #define MIN_PAGE_SIZE_MILS           100
40 #define MAX_PAGE_SIZE_PCBNEW_MILS  48000
41 #define MAX_PAGE_SIZE_MILS        120000
42 
43 
44 /**
45  * Describe the page size and margins of a paper page on which to eventually print or plot.
46  *
47  * Paper sizes are often described in inches.  Here paper is described in 1/1000th of an
48  * inch (mils).  For convenience there are some read only accessors for internal units
49  * which is a compile time calculation, not runtime.
50  *
51  * @author Dick Hollenbeck
52  */
53 class PAGE_INFO
54 {
55 public:
56     PAGE_INFO( const wxString& aType = PAGE_INFO::A3, bool IsPortrait = false );
57 
58     // paper size names which are part of the public API, pass to SetType() or
59     // above constructor.
60 
61     // these were once wxStrings, but it caused static construction sequence problems:
62     static const wxChar A5[];
63     static const wxChar A4[];
64     static const wxChar A3[];
65     static const wxChar A2[];
66     static const wxChar A1[];
67     static const wxChar A0[];
68     static const wxChar A[];
69     static const wxChar B[];
70     static const wxChar C[];
71     static const wxChar D[];
72     static const wxChar E[];
73     static const wxChar GERBER[];
74     static const wxChar USLetter[];
75     static const wxChar USLegal[];
76     static const wxChar USLedger[];
77     static const wxChar Custom[];     ///< "User" defined page type
78 
79 
80     /**
81      * Set the name of the page type and also the sizes and margins commonly associated with
82      * that type name.
83      *
84      * @param aStandardPageDescriptionName is a wxString constant giving one of:
85      * "A5" "A4" "A3" "A2" "A1" "A0" "A" "B" "C" "D" "E" "GERBER", "USLetter", "USLegal",
86      * "USLedger", or "User".  If "User" then the width and height are custom,
87      * and will be set according to <b>previous</b> calls to
88      * static PAGE_INFO::SetUserWidthMils() and
89      * static PAGE_INFO::SetUserHeightMils();
90      * @param aIsPortrait Set to true to set page orientation to portrait mode.
91      * @return true if @a aStandarePageDescription was a recognized type.
92      */
93     bool SetType( const wxString& aStandardPageDescriptionName, bool aIsPortrait = false );
GetType()94     const wxString& GetType() const { return m_type; }
95 
96     /**
97      * @return True if the object has the default page settings which are A3, landscape.
98      */
IsDefault()99     bool IsDefault() const { return m_type == PAGE_INFO::A3 && !m_portrait; }
100 
101     /**
102      * @return true if the type is Custom.
103      */
104     bool IsCustom() const;
105 
106     /**
107      * Rotate the paper page 90 degrees.
108      *
109      * This PAGE_INFO may either be in portrait or landscape mode.  Use this function to
110      * change from one mode to the other mode.
111      *
112      * @param aIsPortrait if true and not already in portrait mode, will change this
113      *                    PAGE_INFO to portrait mode.  Or if false and not already in
114      *                    landscape mode, will change this PAGE_INFO to landscape mode.
115      */
116     void SetPortrait( bool aIsPortrait );
IsPortrait()117     bool IsPortrait() const { return m_portrait; }
118 
119     /**
120      * @return ws' style printing orientation (wxPORTRAIT or wxLANDSCAPE).
121      */
GetWxOrientation()122     wxPrintOrientation  GetWxOrientation() const { return IsPortrait() ? wxPORTRAIT : wxLANDSCAPE; }
123 
124     /**
125      * @return wxPrintData's style paper id associated with page type name.
126      */
GetPaperId()127     wxPaperSize GetPaperId() const { return m_paper_id; }
128 
129     void SetWidthMils(  int aWidthInMils );
GetWidthMils()130     int GetWidthMils() const { return m_size.x; }
131 
132     void SetHeightMils( int aHeightInMils );
GetHeightMils()133     int GetHeightMils() const { return m_size.y; }
134 
GetSizeMils()135     const wxSize& GetSizeMils() const { return m_size; }
136 
137     // Accessors returning "Internal Units (IU)".  IUs are mils in EESCHEMA,
138     // and either deci-mils or nanometers in PCBNew.
139 #if defined(PCBNEW) || defined(EESCHEMA) || defined(GERBVIEW) || defined(PL_EDITOR)
GetWidthIU()140     int GetWidthIU() const  { return IU_PER_MILS * GetWidthMils();  }
GetHeightIU()141     int GetHeightIU() const { return IU_PER_MILS * GetHeightMils(); }
GetSizeIU()142     const wxSize GetSizeIU() const  { return wxSize( GetWidthIU(), GetHeightIU() ); }
143 #endif
144 
145     /**
146      * Set the width of Custom page in mils for any custom page constructed or made via
147      * SetType() after making this call.
148      */
149     static void SetCustomWidthMils( int aWidthInMils );
150 
151     /**
152      * Set the height of Custom page in mils for any custom page constructed or made via
153      * SetType() after making this call.
154      */
155     static void SetCustomHeightMils( int aHeightInMils );
156 
157     /**
158      * @return custom paper width in mils.
159      */
GetCustomWidthMils()160     static int GetCustomWidthMils() { return s_user_width; }
161 
162     /**
163      * @return custom paper height in mils.
164      */
GetCustomHeightMils()165     static int GetCustomHeightMils() { return s_user_height; }
166 
167     /**
168      * Output the page class to \a aFormatter in s-expression form.
169      *
170      * @param aFormatter The #OUTPUTFORMATTER object to write to.
171      * @param aNestLevel The indentation next level.
172      * @param aControlBits The control bit definition for object specific formatting.
173      * @throw IO_ERROR on write error.
174      */
175     void Format( OUTPUTFORMATTER* aFormatter, int aNestLevel, int aControlBits ) const;
176 
177 protected:
178     // only the class implementation(s) may use this constructor
179     PAGE_INFO( const wxSize& aSizeMils, const wxString& aName, wxPaperSize aPaperId );
180 
181 private:
182     // standard pre-defined sizes
183     static const PAGE_INFO pageA5;
184     static const PAGE_INFO pageA4;
185     static const PAGE_INFO pageA3;
186     static const PAGE_INFO pageA2;
187     static const PAGE_INFO pageA1;
188     static const PAGE_INFO pageA0;
189     static const PAGE_INFO pageA;
190     static const PAGE_INFO pageB;
191     static const PAGE_INFO pageC;
192     static const PAGE_INFO pageD;
193     static const PAGE_INFO pageE;
194     static const PAGE_INFO pageGERBER;
195 
196     static const PAGE_INFO pageUSLetter;
197     static const PAGE_INFO pageUSLegal;
198     static const PAGE_INFO pageUSLedger;
199 
200     static const PAGE_INFO pageUser;
201 
202     // all dimensions here are in mils
203 
204     wxString    m_type;             ///< paper type: A4, A3, etc.
205     wxSize      m_size;             ///< mils
206 
207     bool        m_portrait;         ///< true if portrait, false if landscape
208 
209     wxPaperSize m_paper_id;         ///< wx' style paper id.
210 
211     static int s_user_height;
212     static int s_user_width;
213 
214     void    updatePortrait();
215 
216     void    setMargins();
217 };
218 
219 #endif  // PAGE_INFO_H
220