class_page_info.cpp 9.16 KB
Newer Older
1 2 3
/*
 * This program source code file is part of KiCad, a free EDA CAD application.
 *
4
 * Copyright (C) 2012 SoftPLC Corporation, Dick Hollenbeck <dick@softplc.com>
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
 * Copyright (C) 2012 KiCad Developers, see CHANGELOG.TXT for contributors.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, you may find one here:
 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
 * or you may search the http://www.gnu.org website for the version 2 license,
 * or you may write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
 */


#include <common.h>
27 28
#include <macros.h>

29

30 31 32 33 34 35 36 37
// late arriving wxPAPER_A0, wxPAPER_A1
#if wxABI_VERSION >= 20999
 #define PAPER_A0   wxPAPER_A0
 #define PAPER_A1   wxPAPER_A1
#else
 #define PAPER_A0   wxPAPER_A2
 #define PAPER_A1   wxPAPER_A2
#endif
38

39

40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
// Standard paper sizes nicknames.
const wxString PAGE_INFO::A4( wxT( "A4" ) );
const wxString PAGE_INFO::A3( wxT( "A3" ) );
const wxString PAGE_INFO::A2( wxT( "A2" ) );
const wxString PAGE_INFO::A1( wxT( "A1" ) );
const wxString PAGE_INFO::A0( wxT( "A0" ) );
const wxString PAGE_INFO::A( wxT( "A" ) );
const wxString PAGE_INFO::B( wxT( "B" ) );
const wxString PAGE_INFO::C( wxT( "C" ) );
const wxString PAGE_INFO::D( wxT( "D" ) );
const wxString PAGE_INFO::E( wxT( "E" ) );
const wxString PAGE_INFO::GERBER( wxT( "GERBER" ) );
const wxString PAGE_INFO::USLetter( wxT( "USLetter" ) );
const wxString PAGE_INFO::USLegal( wxT( "USLegal" ) );
const wxString PAGE_INFO::USLedger( wxT( "USLedger" ) );
55 56
const wxString PAGE_INFO::Custom( wxT( "User" ) );

57

58
// Standard page sizes in mils, all constants
59 60
// see:  https://lists.launchpad.net/kicad-developers/msg07389.html
// also see: wx/defs.h
61

62 63
// local readability macro for millimeter wxSize
#define MMsize( x, y )  wxSize( Mm2mils( x ), Mm2mils( y ) )
64

65
// All MUST be defined as landscape.
66 67 68 69 70 71 72 73 74 75 76 77 78 79
const PAGE_INFO  PAGE_INFO::pageA4(     MMsize( 297,   210 ),   wxT( "A4" ),    wxPAPER_A4 );
const PAGE_INFO  PAGE_INFO::pageA3(     MMsize( 420,   297 ),   wxT( "A3" ),    wxPAPER_A3 );
const PAGE_INFO  PAGE_INFO::pageA2(     MMsize( 594,   420 ),   wxT( "A2" ),    wxPAPER_A2 );
const PAGE_INFO  PAGE_INFO::pageA1(     MMsize( 841,   594 ),   wxT( "A1" ),    PAPER_A1 );
const PAGE_INFO  PAGE_INFO::pageA0(     MMsize( 1189,  841 ),   wxT( "A0" ),    PAPER_A0 );

const PAGE_INFO  PAGE_INFO::pageA(      wxSize( 11000,  8500 ), wxT( "A" ), wxPAPER_LETTER );
const PAGE_INFO  PAGE_INFO::pageB(      wxSize( 17000, 11000 ), wxT( "B" ), wxPAPER_TABLOID );
const PAGE_INFO  PAGE_INFO::pageC(      wxSize( 22000, 17000 ), wxT( "C" ), wxPAPER_CSHEET );
const PAGE_INFO  PAGE_INFO::pageD(      wxSize( 34000, 22000 ), wxT( "D" ), wxPAPER_DSHEET );
const PAGE_INFO  PAGE_INFO::pageE(      wxSize( 44000, 34000 ), wxT( "E" ), wxPAPER_ESHEET );

const PAGE_INFO  PAGE_INFO::pageGERBER( wxSize( 32000, 32000 ), wxT( "GERBER" ), wxPAPER_NONE  );
const PAGE_INFO  PAGE_INFO::pageUser(   wxSize( 17000, 11000 ), Custom,         wxPAPER_NONE );
80 81

// US paper sizes
82 83 84
const PAGE_INFO  PAGE_INFO::pageUSLetter( wxSize( 11000, 8500  ),  wxT( "USLetter" ), wxPAPER_LETTER );
const PAGE_INFO  PAGE_INFO::pageUSLegal(  wxSize( 14000, 8500  ),  wxT( "USLegal" ),  wxPAPER_LEGAL );
const PAGE_INFO  PAGE_INFO::pageUSLedger( wxSize( 17000, 11000 ),  wxT( "USLedger" ), wxPAPER_TABLOID );
85 86

// Custom paper size for next instantiation of type "User"
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
int PAGE_INFO::s_user_width  = 17000;
int PAGE_INFO::s_user_height = 11000;

/*
wxArrayString PAGE_INFO::GetStandardSizes()
{
    wxArrayString ret;

    static const PAGE_INFO* stdPageSizes[] = {
        &pageA4,
        &pageA3,
        &pageA2,
        &pageA1,
        &pageA0,
        &pageA,
        &pageB,
        &pageC,
        &pageD,
        &pageE,
        // &pageGERBER,     // standard?
107 108 109
        &pageUSLetter,
        &pageUSLegal,
        &pageUSLedger,
110 111 112 113 114 115 116 117 118 119
        &pageUser,
    };

    for( unsigned i=0;  i < DIM( stdPageSizes );  ++i )
        ret.Add( stdPageSizes[i]->GetType() );

    return ret;
}
*/

120 121 122 123 124 125 126 127

inline void PAGE_INFO::updatePortrait()
{
    // update m_portrait based on orientation of m_size.x and m_size.y
    m_portrait = ( m_size.y > m_size.x );
}


128
PAGE_INFO::PAGE_INFO( const wxSize& aSizeMils, const wxString& aType, wxPaperSize aPaperId ) :
129
    m_type( aType ), m_size( aSizeMils ), m_paper_id( aPaperId )
130
{
131
    updatePortrait();
132

133 134
    // This constructor is protected, and only used by const PAGE_INFO's known
    // only to class implementation, so no further changes to "this" object are
135
    // expected.
136 137 138
}


139
PAGE_INFO::PAGE_INFO( const wxString& aType, bool IsPortrait )
140
{
141
    SetType( aType, IsPortrait );
142 143
}

144

145
bool PAGE_INFO::SetType( const wxString& aType, bool IsPortrait )
146 147 148
{
    bool rc = true;

149
    // all are landscape initially
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
    if( aType == pageA4.GetType() )
        *this = pageA4;
    else if( aType == pageA3.GetType() )
        *this = pageA3;
    else if( aType == pageA2.GetType() )
        *this = pageA2;
    else if( aType == pageA1.GetType() )
        *this = pageA1;
    else if( aType == pageA0.GetType() )
        *this = pageA0;
    else if( aType == pageA.GetType() )
        *this = pageA;
    else if( aType == pageB.GetType() )
        *this = pageB;
    else if( aType == pageC.GetType() )
        *this = pageC;
    else if( aType == pageD.GetType() )
        *this = pageD;
    else if( aType == pageE.GetType() )
        *this = pageE;
    else if( aType == pageGERBER.GetType() )
        *this = pageGERBER;
172 173 174 175 176 177
    else if( aType == pageUSLetter.GetType() )
        *this = pageUSLetter;
    else if( aType == pageUSLegal.GetType() )
        *this = pageUSLegal;
    else if( aType == pageUSLedger.GetType() )
        *this = pageUSLedger;
178 179 180 181 182 183 184 185 186
    else if( aType == pageUser.GetType() )
    {
        // pageUser is const, and may not and does not hold the custom size,
        // so customize *this later
        *this  = pageUser;

        // customize:
        m_size.x = s_user_width;
        m_size.y = s_user_height;
187 188

        updatePortrait();
189 190 191 192
    }
    else
        rc = false;

193 194 195 196 197 198 199
    if( IsPortrait )
    {
        // all private PAGE_INFOs are landscape, must swap x and y
        m_size = wxSize( m_size.y, m_size.x );
        updatePortrait();
    }

200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
    return rc;
}


bool PAGE_INFO::IsCustom() const
{
    return m_type == Custom;
}


void PAGE_INFO::SetPortrait( bool isPortrait )
{
    if( m_portrait != isPortrait )
    {
        // swap x and y in m_size
        m_size = wxSize( m_size.y, m_size.x );

        m_portrait = isPortrait;

219
        // margins are not touched, do that if you want
220 221 222 223 224 225
    }
}


static int clampWidth( int aWidthInMils )
{
226
/*  was giving EESCHEMA single component SVG plotter grief
227
    However a minimal test is made to avoid values that crashes Kicad
228 229 230 231
    if( aWidthInMils < 4000 )       // 4" is about a baseball card
        aWidthInMils = 4000;
    else if( aWidthInMils > 44000 ) //44" is plotter size
        aWidthInMils = 44000;
232
*/
233 234
    if( aWidthInMils < 10 )
        aWidthInMils = 10;
235 236 237 238 239 240
    return aWidthInMils;
}


static int clampHeight( int aHeightInMils )
{
241
/*  was giving EESCHEMA single component SVG plotter grief
242
    clamping is best done at the UI, i.e. dialog, levels
243
    However a minimal test is made to avoid values that crashes Kicad
244 245 246 247
    if( aHeightInMils < 4000 )
        aHeightInMils = 4000;
    else if( aHeightInMils > 44000 )
        aHeightInMils = 44000;
248
*/
249 250
    if( aHeightInMils < 10 )
        aHeightInMils = 10;
251 252 253 254
    return aHeightInMils;
}


255
void PAGE_INFO::SetCustomWidthMils( int aWidthInMils )
256 257 258 259 260
{
    s_user_width = clampWidth( aWidthInMils );
}


261
void PAGE_INFO::SetCustomHeightMils( int aHeightInMils )
262 263 264 265 266 267 268
{
    s_user_height = clampHeight( aHeightInMils );
}


void PAGE_INFO::SetWidthMils(  int aWidthInMils )
{
269 270 271 272 273 274 275 276 277
    if( m_size.x != aWidthInMils )
    {
        m_size.x = clampWidth( aWidthInMils );

        m_type = Custom;
        m_paper_id = wxPAPER_NONE;

        updatePortrait();
    }
278 279 280 281 282
}


void PAGE_INFO::SetHeightMils( int aHeightInMils )
{
283 284 285 286 287 288 289 290 291
    if( m_size.y != aHeightInMils )
    {
        m_size.y = clampHeight( aHeightInMils );

        m_type = Custom;
        m_paper_id = wxPAPER_NONE;

        updatePortrait();
    }
292
}
293

294 295 296 297

void PAGE_INFO::Format( OUTPUTFORMATTER* aFormatter, int aNestLevel, int aControlBits ) const
    throw( IO_ERROR )
{
298
    aFormatter->Print( aNestLevel, "(page %s", aFormatter->Quotew( GetType() ).c_str() );
299

300 301 302 303
    // The page dimensions are only required for user defined page sizes.
    // Internally, the page size is in mils
    if( GetType() == PAGE_INFO::Custom )
        aFormatter->Print( 0, " %g %g",
304 305
                           GetWidthMils() * 25.4 / 1000.0,
                           GetHeightMils() * 25.4 / 1000.0 );
306

307 308
    if( !IsCustom() && IsPortrait() )
        aFormatter->Print( 0, " portrait" );
309

310
    aFormatter->Print( 0, ")\n" );
311
}