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