LibreOffice Module sc (master)  1
xlpage.cxx
Go to the documentation of this file.
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
5  * This Source Code Form is subject to the terms of the Mozilla Public
6  * License, v. 2.0. If a copy of the MPL was not distributed with this
7  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8  *
9  * This file incorporates work covered by the following license notice:
10  *
11  * Licensed to the Apache Software Foundation (ASF) under one or more
12  * contributor license agreements. See the NOTICE file distributed
13  * with this work for additional information regarding copyright
14  * ownership. The ASF licenses this file to you under the Apache
15  * License, Version 2.0 (the "License"); you may not use this file
16  * except in compliance with the License. You may obtain a copy of
17  * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18  */
19 
20 #include <xlpage.hxx>
21 #include <xltools.hxx>
22 #include <editeng/paperinf.hxx>
23 #include <sal/macros.h>
24 #include <editeng/brushitem.hxx>
25 #include <global.hxx>
26 #include <xlconst.hxx>
27 
28 namespace{
29 
30 struct XclPaperSize
31 {
32  Paper mePaper;
35 };
36 
37 constexpr tools::Long in2twips(double n_inch)
38 {
39  return static_cast<tools::Long>( (n_inch * EXC_TWIPS_PER_INCH) + 0.5);
40 }
41 constexpr tools::Long mm2twips(double n_mm)
42 {
43  return static_cast<tools::Long>( (n_mm * EXC_TWIPS_PER_INCH / CM_PER_INCH / 10.0) + 0.5);
44 }
45 constexpr tools::Long twips2mm(tools::Long n_twips)
46 {
47  return static_cast<tools::Long>((static_cast<double>(n_twips) - 0.5) / EXC_TWIPS_PER_INCH * CM_PER_INCH * 10.0);
48 }
49 
50 // see ApiPaperSize spPaperSizeTable in filter and aDinTab in i18nutil
51 constexpr XclPaperSize pPaperSizeTable[] =
52 {
53 /* 0*/ { PAPER_USER, 0, 0 }, // undefined
54  { PAPER_LETTER, in2twips( 8.5 ), in2twips( 11 ) }, // Letter
55  { PAPER_USER, in2twips( 8.5 ), in2twips( 11 ) }, // Letter Small
56  { PAPER_TABLOID, in2twips( 11 ), in2twips( 17 ) }, // Tabloid
57  { PAPER_LEDGER, in2twips( 17 ), in2twips( 11 ) }, // Ledger
58 /* 5*/ { PAPER_LEGAL, in2twips( 8.5 ), in2twips( 14 ) }, // Legal
59  { PAPER_STATEMENT, in2twips( 5.5 ), in2twips( 8.5 ) }, // Statement
60  { PAPER_EXECUTIVE, in2twips( 7.25 ), in2twips( 10.5 ) }, // Executive
61  { PAPER_A3, mm2twips( 297 ), mm2twips( 420 ) }, // A3
62  { PAPER_A4, mm2twips( 210 ), mm2twips( 297 ) }, // A4
63 /* 10*/ { PAPER_USER, mm2twips( 210 ), mm2twips( 297 ) }, // A4 Small
64  { PAPER_A5, mm2twips( 148 ), mm2twips( 210 ) }, // A5
65  /* for JIS vs ISO B confusion see:
66  https://docs.microsoft.com/en-us/windows/win32/intl/paper-sizes
67  http://wiki.openoffice.org/wiki/DefaultPaperSize comments
68  http://partners.adobe.com/public/developer/en/ps/5003.PPD_Spec_v4.3.pdf */
69  { PAPER_B4_JIS, mm2twips( 257 ), mm2twips( 364 ) }, // B4 (JIS)
70  { PAPER_B5_JIS, mm2twips( 182 ), mm2twips( 257 ) }, // B5 (JIS)
71  { PAPER_USER, in2twips( 8.5 ), in2twips( 13 ) }, // Folio
72 /* 15*/ { PAPER_QUARTO, mm2twips( 215 ), mm2twips( 275 ) }, // Quarto
73  { PAPER_10x14, in2twips( 10 ), in2twips( 14 ) }, // 10x14
74  { PAPER_USER, in2twips( 11 ), in2twips( 17 ) }, // 11x17
75  { PAPER_USER, in2twips( 8.5 ), in2twips( 11 ) }, // Note
76  { PAPER_ENV_9, in2twips( 3.875 ), in2twips( 8.875 ) }, // Envelope #9
77 /* 20*/ { PAPER_ENV_10, in2twips( 4.125 ), in2twips( 9.5 ) }, // Envelope #10
78  { PAPER_ENV_11, in2twips( 4.5 ), in2twips( 10.375 ) }, // Envelope #11
79  { PAPER_ENV_12, in2twips( 4.75 ), in2twips( 11 ) }, // Envelope #12
80  { PAPER_ENV_14, in2twips( 5 ), in2twips( 11.5 ) }, // Envelope #14
81  { PAPER_C, in2twips( 17 ), in2twips( 22 ) }, // ANSI-C
82 /* 25*/ { PAPER_D, in2twips( 22 ), in2twips( 34 ) }, // ANSI-D
83  { PAPER_E, in2twips( 34 ), in2twips( 44 ) }, // ANSI-E
84  { PAPER_ENV_DL, mm2twips( 110 ), mm2twips( 220 ) }, // Envelope DL
85  { PAPER_ENV_C5, mm2twips( 162 ), mm2twips( 229 ) }, // Envelope C5
86  { PAPER_ENV_C3, mm2twips( 324 ), mm2twips( 458 ) }, // Envelope C3
87 /* 30*/ { PAPER_ENV_C4, mm2twips( 229 ), mm2twips( 324 ) }, // Envelope C4
88  { PAPER_ENV_C6, mm2twips( 114 ), mm2twips( 162 ) }, // Envelope C6
89  { PAPER_ENV_C65, mm2twips( 114 ), mm2twips( 229 ) }, // Envelope C65
90  { PAPER_B4_ISO, mm2twips( 250 ), mm2twips( 353 ) }, // B4 (ISO)
91  { PAPER_B5_ISO, mm2twips( 176 ), mm2twips( 250 ) }, // B5 (ISO)
92 /* 35*/ { PAPER_B6_ISO, mm2twips( 125 ), mm2twips( 176 ) }, // B6 (ISO)
93  { PAPER_ENV_ITALY, mm2twips( 110 ), mm2twips( 230 ) }, // Envelope Italy
94  { PAPER_ENV_MONARCH, in2twips( 3.875 ), in2twips( 7.5 ) }, // Envelope Monarch
95  { PAPER_ENV_PERSONAL, in2twips( 3.625 ), in2twips( 6.5 ) }, // 6 3/4 Envelope
96  { PAPER_FANFOLD_US, in2twips( 14.875 ), in2twips( 11 ) }, // US Std Fanfold
97 /* 40*/ { PAPER_FANFOLD_DE, in2twips( 8.5 ), in2twips( 12 ) }, // German Std Fanfold
98  { PAPER_FANFOLD_LEGAL_DE, in2twips( 8.5 ), in2twips( 13 ) }, // German Legal Fanfold
99  { PAPER_B4_ISO, mm2twips( 250 ), mm2twips( 353 ) }, // B4 (ISO)
100  { PAPER_POSTCARD_JP,mm2twips( 100 ), mm2twips( 148 ) }, // Japanese Postcard
101  { PAPER_9x11, in2twips( 9 ), in2twips( 11 ) }, // 9x11
102 /* 45*/ { PAPER_10x11, in2twips( 10 ), in2twips( 11 ) }, // 10x11
103  { PAPER_15x11, in2twips( 15 ), in2twips( 11 ) }, // 15x11
104  { PAPER_ENV_INVITE, mm2twips( 220 ), mm2twips( 220 ) }, // Envelope Invite
105  { PAPER_USER, 0, 0 }, // undefined
106  { PAPER_USER, 0, 0 }, // undefined
107  /* See: https://docs.microsoft.com/en-us/windows/win32/intl/paper-sizes */
108 /* 50*/ { PAPER_USER, in2twips( 9.5 ), in2twips( 12 ) }, // Letter Extra
109  { PAPER_USER, in2twips( 9.5 ), in2twips( 15 ) }, // Legal Extra
110  { PAPER_USER, in2twips( 11.69 ), in2twips( 18 ) }, // Tabloid Extra
111  { PAPER_USER, mm2twips( 235 ), mm2twips( 322 ) }, // A4 Extra
112  { PAPER_USER, in2twips( 8.5 ), in2twips( 11 ) }, // Letter Transverse
113 /* 55*/ { PAPER_USER, mm2twips( 210 ), mm2twips( 297 ) }, // A4 Transverse
114  { PAPER_USER, in2twips( 9.5 ), in2twips( 12 ) }, // Letter Extra Transverse
115  { PAPER_A_PLUS, mm2twips( 227 ), mm2twips( 356 ) }, // Super A/A4
116  { PAPER_B_PLUS, mm2twips( 305 ), mm2twips( 487 ) }, // Super B/A3
117  { PAPER_LETTER_PLUS,in2twips( 8.5 ), in2twips( 12.69 ) }, // Letter Plus
118 /* 60*/ { PAPER_A4_PLUS, mm2twips( 210 ), mm2twips( 330 ) }, // A4 Plus
119  { PAPER_USER, mm2twips( 148 ), mm2twips( 210 ) }, // A5 Transverse
120  { PAPER_USER, mm2twips( 182 ), mm2twips( 257 ) }, // B5 (JIS) Transverse
121  { PAPER_USER, mm2twips( 322 ), mm2twips( 445 ) }, // A3 Extra
122  { PAPER_USER, mm2twips( 174 ), mm2twips( 235 ) }, // A5 Extra
123 /* 65*/ { PAPER_USER, mm2twips( 201 ), mm2twips( 276 ) }, // B5 (ISO) Extra
124  { PAPER_A2, mm2twips( 420 ), mm2twips( 594 ) }, // A2
125  { PAPER_USER, mm2twips( 297 ), mm2twips( 420 ) }, // A3 Transverse
126  { PAPER_USER, mm2twips( 322 ), mm2twips( 445 ) }, // A3 Extra Transverse
127  { PAPER_DOUBLEPOSTCARD_JP, mm2twips( 200 ), mm2twips( 148 ) }, // Double Japanese Postcard
128 /* 70*/ { PAPER_A6, mm2twips( 105 ), mm2twips( 148 ) }, // A6
129  { PAPER_USER, 0, 0 }, // Japanese Envelope Kaku #2
130  { PAPER_USER, 0, 0 }, // Japanese Envelope Kaku #3
131  { PAPER_USER, 0, 0 }, // Japanese Envelope Chou #3
132  { PAPER_USER, 0, 0 }, // Japanese Envelope Chou #4
133 /* 75*/ { PAPER_USER, in2twips( 11 ), in2twips( 8.5 ) }, // Letter Rotated
134  { PAPER_USER, mm2twips( 420 ), mm2twips( 297 ) }, // A3 Rotated
135  { PAPER_USER, mm2twips( 297 ), mm2twips( 210 ) }, // A4 Rotated
136  { PAPER_USER, mm2twips( 210 ), mm2twips( 148 ) }, // A5 Rotated
137  { PAPER_USER, mm2twips( 364 ), mm2twips( 257 ) }, // B4 (JIS) Rotated
138 /* 80*/ { PAPER_USER, mm2twips( 257 ), mm2twips( 182 ) }, // B5 (JIS) Rotated
139  { PAPER_USER, mm2twips( 148 ), mm2twips( 100 ) }, // Japanese Postcard Rotated
140  { PAPER_USER, mm2twips( 148 ), mm2twips( 200 ) }, // Double Japanese Postcard Rotated
141  { PAPER_USER, mm2twips( 148 ), mm2twips( 105 ) }, // A6 Rotated
142  { PAPER_USER, 0, 0 }, // Japanese Envelope Kaku #2 Rotated
143 /* 85*/ { PAPER_USER, 0, 0 }, // Japanese Envelope Kaku #3 Rotated
144  { PAPER_USER, 0, 0 }, // Japanese Envelope Chou #3 Rotated
145  { PAPER_USER, 0, 0 }, // Japanese Envelope Chou #4 Rotated
146  { PAPER_B6_JIS, mm2twips( 128 ), mm2twips( 182 ) }, // B6 (JIS)
147  { PAPER_USER, mm2twips( 182 ), mm2twips( 128 ) }, // B6 (JIS) Rotated
148 /* 90*/ { PAPER_12x11, in2twips( 12 ), in2twips( 11 ) } // 12x11
149 };
150 
151 } //namespace
152 
153 // Page settings ==============================================================
154 
156 {
157  SetDefaults();
158 }
159 
161 {
162  // SvxBrushItem incomplete in header
163 }
164 
166 {
167  maHorPageBreaks.clear();
168  maVerPageBreaks.clear();
169  mxBrushItem.reset();
170  maHeader.clear();
171  maFooter.clear();
172  maHeaderEven.clear();
173  maFooterEven.clear();
180  mnPaperWidth = 0;
181  mnPaperHeight = 0;
182  mnCopies = 1;
183  mnStartPage = 0;
184  mnScaling = 100;
187  mbUseEvenHF = /*mbUseFirstHF =*/ false;
188  mbValid = false;
189  mbPortrait = true;
192 }
193 
195 {
196  const XclPaperSize* pEntry = pPaperSizeTable;
197  if( mnPaperSize < SAL_N_ELEMENTS( pPaperSizeTable ) )
198  pEntry += mnPaperSize;
199 
200  Size aSize;
201  if( pEntry->mePaper == PAPER_USER )
202  aSize = Size( pEntry->mnWidth, pEntry->mnHeight );
203  else
204  aSize = SvxPaperInfo::GetPaperSize( pEntry->mePaper );
205 
206  // invalid size -> back to default
207  if( !aSize.Width() || !aSize.Height() )
209 
210  if( !mbPortrait )
211  {
212  // swap width and height
213  tools::Long n = aSize.Width();
214  aSize.setWidth(aSize.Height());
215  aSize.setHeight(n);
216  }
217 
218  return aSize;
219 }
220 
221 void XclPageData::SetScPaperSize( const Size& rSize, bool bPortrait, bool bStrictSize )
222 {
223  mbPortrait = bPortrait;
224  mnPaperSize = 0;
225  tools::Long nWidth = bPortrait ? rSize.Width() : rSize.Height();
226  tools::Long nHeight = bPortrait ? rSize.Height() : rSize.Width();
227  tools::Long nMaxWDiff = 80;
228  tools::Long nMaxHDiff = 50;
229 
230  mnPaperWidth = twips2mm( nWidth );
231  mnPaperHeight = twips2mm( nHeight );
232  if( bStrictSize )
233  {
234  nMaxWDiff = 5;
235  nMaxHDiff = 5;
237  }
238  else
239  {
241  }
242 
243  for( const auto &rEntry : pPaperSizeTable)
244  {
245  tools::Long nWDiff = std::abs( rEntry.mnWidth - nWidth );
246  tools::Long nHDiff = std::abs( rEntry.mnHeight - nHeight );
247  if( ((nWDiff <= nMaxWDiff) && (nHDiff < nMaxHDiff)) ||
248  ((nWDiff < nMaxWDiff) && (nHDiff <= nMaxHDiff)) )
249  {
250  sal_uInt16 nIndex = static_cast< sal_uInt16 >( &rEntry - pPaperSizeTable );
251  mnPaperSize = nIndex;
252  if( bStrictSize )
253  mnStrictPaperSize = nIndex;
254 
255  nMaxWDiff = nWDiff;
256  nMaxHDiff = nHDiff;
257  }
258  }
259  if( !bStrictSize )
260  SetScPaperSize( rSize, bPortrait, true );
261 }
262 
263 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
double mfHdrRightMargin
Left margin to header.
Definition: xlpage.hxx:119
double mfRightMargin
Left margin in inches.
Definition: xlpage.hxx:113
PAPER_FANFOLD_US
double mnHeight
double mfFtrLeftMargin
Right margin to header.
Definition: xlpage.hxx:120
const sal_Int32 EXC_MARGIN_DEFAULT_HF
Top/bottom default margin in 1/100mm.
Definition: xlpage.hxx:53
sal_uInt16 mnVerPrintRes
Horizontal printing resolution.
Definition: xlpage.hxx:132
sal_Int32 nIndex
PAPER_EXECUTIVE
PAPER_B6_ISO
sal_uInt16 mnCopies
Paper Height in mm.
Definition: xlpage.hxx:126
PAPER_FANFOLD_DE
PAPER_ENV_C3
const sal_Int32 EXC_MARGIN_DEFAULT_FLR
Left/right header default margin in 1/100mm.
Definition: xlpage.hxx:55
bool mbManualStart
true = print notes.
Definition: xlpage.hxx:141
PAPER_A6
void setWidth(tools::Long nWidth)
PAPER_ENV_MONARCH
PAPER_ENV_C5
PAPER_B5_ISO
bool mbVerCenter
true = centered horizontally; false = left aligned.
Definition: xlpage.hxx:144
PAPER_B4_JIS
PAPER_ENV_PERSONAL
double mfHdrLeftMargin
Margin main page to footer.
Definition: xlpage.hxx:118
long Long
const sal_Int32 EXC_MARGIN_DEFAULT_HLR
Header/footer default margin in 1/100mm.
Definition: xlpage.hxx:54
PAPER_POSTCARD_JP
sal_Int64 n
PAPER_B5_JIS
PAPER_ENV_11
const sal_uInt16 EXC_PAPERSIZE_USER
Definition: xlpage.hxx:90
OUString maFooterEven
Excel header string for even pages (empty = off).
Definition: xlpage.hxx:111
PAPER_10x14
sal_uInt16 mnPaperSize
Right margin to footer.
Definition: xlpage.hxx:122
PAPER_A_PLUS
bool mbPrintGrid
true = print column and row headings.
Definition: xlpage.hxx:146
bool mbHorCenter
true = fit to pages; false = scale in percent.
Definition: xlpage.hxx:143
PAPER_ENV_14
void SetDefaults()
Sets Excel default page settings.
Definition: xlpage.cxx:165
double mfTopMargin
Right margin in inches.
Definition: xlpage.hxx:114
PAPER_ENV_9
bool mbFitToPages
true = mnStartPage valid; false = automatic.
Definition: xlpage.hxx:142
PAPER_QUARTO
PAPER_ENV_C6
PAPER_LEGAL
OUString maHeader
Background bitmap.
Definition: xlpage.hxx:108
const sal_uInt16 EXC_PAPERSIZE_DEFAULT
Definition: xlpage.hxx:89
PAPER_ENV_ITALY
PAPER_TABLOID
#define SAL_N_ELEMENTS(arr)
static Size GetDefaultPaperSize(MapUnit eUnit=MapUnit::MapTwip)
bool mbValid
True = use maHeaderEven/maFooterEven.
Definition: xlpage.hxx:135
PAPER_B_PLUS
sal_uInt16 mnPaperWidth
Same as papersize - but for ooxml (considering stricter dimensions)
Definition: xlpage.hxx:124
const sal_Int32 EXC_TWIPS_PER_INCH
Definition: xlconst.hxx:135
PAPER_FANFOLD_LEGAL_DE
const sal_Int32 EXC_MARGIN_DEFAULT_LR
Definition: xlpage.hxx:51
PAPER_ENV_DL
XclPageData()
true = print grid lines.
Definition: xlpage.cxx:155
PAPER_A4_PLUS
PAPER_ENV_10
sal_uInt16 mnStrictPaperSize
Index into paper size table.
Definition: xlpage.hxx:123
OUString maFooter
Excel header string (empty = off).
Definition: xlpage.hxx:109
PAPER_C
sal_uInt16 mnStartPage
Number of copies.
Definition: xlpage.hxx:127
PAPER_ENV_INVITE
SvxBrushItemPtr mxBrushItem
Vertical page breaks.
Definition: xlpage.hxx:107
sal_uInt16 mnScaling
Start page number.
Definition: xlpage.hxx:128
double mfFtrRightMargin
Left margin to footer.
Definition: xlpage.hxx:121
tools::Long Width() const
const sal_Int32 EXC_MARGIN_DEFAULT_TB
Left/right default margin in 1/100mm.
Definition: xlpage.hxx:52
PAPER_D
~XclPageData()
Definition: xlpage.cxx:160
void SetScPaperSize(const Size &rSize, bool bPortrait, bool bStrict=false)
Sets the Excel paper size index and paper orientation from Calc paper size (twips).
Definition: xlpage.cxx:221
bool mbPrintNotes
true = draft; false = default quality.
Definition: xlpage.hxx:140
Paper
static Size GetPaperSize(Paper ePaper, MapUnit eUnit=MapUnit::MapTwip)
ScfUInt16Vec maVerPageBreaks
Horizontal page breaks.
Definition: xlpage.hxx:106
sal_uInt16 mnFitToWidth
Scaling in percent.
Definition: xlpage.hxx:129
PAPER_12x11
PAPER_ENV_C4
PAPER_9x11
OUString maHeaderEven
Excel footer string (empty = off).
Definition: xlpage.hxx:110
bool mbDraftQuality
true = black/white; false = colors.
Definition: xlpage.hxx:139
bool mbPortrait
false = some of the values are not valid.
Definition: xlpage.hxx:136
bool mbUseEvenHF
Vertical printing resolution.
Definition: xlpage.hxx:133
ScfUInt16Vec maHorPageBreaks
Definition: xlpage.hxx:105
#define CM_PER_INCH
Definition: global.hxx:82
static double GetInchFromHmm(sal_Int32 nHmm)
Returns the length in inches calculated from a length in 1/100 mm.
Definition: xltools.cxx:290
sal_uInt16 mnPaperHeight
Paper Width in mm.
Definition: xlpage.hxx:125
double mfBottomMargin
Top margin in inches.
Definition: xlpage.hxx:115
PAPER_LEDGER
PAPER_USER
double mfHeaderMargin
Bottom margin in inches.
Definition: xlpage.hxx:116
Size GetScPaperSize() const
Returns the real paper size (twips) from the paper size index and paper orientation.
Definition: xlpage.cxx:194
double mfFooterMargin
Margin main page to header.
Definition: xlpage.hxx:117
bool mbBlackWhite
true = in rows; false = in columns.
Definition: xlpage.hxx:138
PAPER_DOUBLEPOSTCARD_JP
PAPER_ENV_12
sal_uInt16 mnHorPrintRes
Fit to number of pages in height.
Definition: xlpage.hxx:131
double mnWidth
tools::Long Height() const
PAPER_B4_ISO
PAPER_A3
PAPER_15x11
PAPER_LETTER
void setHeight(tools::Long nHeight)
bool mbPrintHeadings
true = centered vertically; false = top aligned.
Definition: xlpage.hxx:145
PAPER_ENV_C65
PAPER_LETTER_PLUS
sal_uInt16 mnFitToHeight
Fit to number of pages in width.
Definition: xlpage.hxx:130
PAPER_B6_JIS
PAPER_A5
PAPER_E
PAPER_A4
PAPER_STATEMENT
PAPER_10x11
bool mbPrintInRows
true = portrait; false = landscape.
Definition: xlpage.hxx:137
double mfLeftMargin
Excel footer string for even pages (empty = off).
Definition: xlpage.hxx:112
PAPER_A2