1    
2    /* ====================================================================
3     * The Apache Software License, Version 1.1
4     *
5     * Copyright (c) 2002 The Apache Software Foundation.  All rights
6     * reserved.
7     *
8     * Redistribution and use in source and binary forms, with or without
9     * modification, are permitted provided that the following conditions
10    * are met:
11    *
12    * 1. Redistributions of source code must retain the above copyright
13    *    notice, this list of conditions and the following disclaimer.
14    *
15    * 2. Redistributions in binary form must reproduce the above copyright
16    *    notice, this list of conditions and the following disclaimer in
17    *    the documentation and/or other materials provided with the
18    *    distribution.
19    *
20    * 3. The end-user documentation included with the redistribution,
21    *    if any, must include the following acknowledgment:
22    *       "This product includes software developed by the
23    *        Apache Software Foundation (http://www.apache.org/)."
24    *    Alternately, this acknowledgment may appear in the software itself,
25    *    if and wherever such third-party acknowledgments normally appear.
26    *
27    * 4. The names "Apache" and "Apache Software Foundation" and
28    *    "Apache POI" must not be used to endorse or promote products
29    *    derived from this software without prior written permission. For
30    *    written permission, please contact apache@apache.org.
31    *
32    * 5. Products derived from this software may not be called "Apache",
33    *    "Apache POI", nor may "Apache" appear in their name, without
34    *    prior written permission of the Apache Software Foundation.
35    *
36    * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
37    * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
38    * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
39    * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
40    * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41    * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
42    * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
43    * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
44    * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
45    * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
46    * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47    * SUCH DAMAGE.
48    * ====================================================================
49    *
50    * This software consists of voluntary contributions made by many
51    * individuals on behalf of the Apache Software Foundation.  For more
52    * information on the Apache Software Foundation, please see
53    * <http://www.apache.org/>.
54    */
55   
56   package org.apache.poi.hssf.record;
57   
58   import org.apache.poi.util.LittleEndian;
59   import org.apache.poi.util.BitField;
60   
61   /**
62    * Title:        Print Setup Record<P>
63    * Description:  Stores print setup options -- bogus for HSSF (and marked as such)<P>
64    * REFERENCE:  PG 385 Microsoft Excel 97 Developer's Kit (ISBN: 1-57231-498-2)<P>
65    * @author Andrew C. Oliver (acoliver at apache dot org)
66    * @author Jason Height (jheight at chariot dot net dot au)
67    * @version 2.0-pre
68    */
69   
70   public class PrintSetupRecord
71       extends Record
72   {
73       public final static short     sid = 0xa1;
74       private short                 field_1_paper_size;
75       private short                 field_2_scale;
76       private short                 field_3_page_start;
77       private short                 field_4_fit_width;
78       private short                 field_5_fit_height;
79       private short                 field_6_options;
80       static final private BitField lefttoright   =
81           new BitField(0x01);   // print over then down
82       static final private BitField landscape     =
83           new BitField(0x02);   // landscape mode
84       static final private BitField validsettings = new BitField(
85           0x04);                // if papersize, scale, resolution, copies, landscape
86   
87       // weren't obtained from the print consider them
88       // mere bunk
89       static final private BitField nocolor       =
90           new BitField(0x08);   // print mono/b&w, colorless
91       static final private BitField draft         =
92           new BitField(0x10);   // print draft quality
93       static final private BitField notes         =
94           new BitField(0x20);   // print the notes
95       static final private BitField noOrientation =
96           new BitField(0x40);   // the orientation is not set
97       static final private BitField usepage       =
98           new BitField(0x80);   // use a user set page no, instead of auto
99       private short                 field_7_hresolution;
100      private short                 field_8_vresolution;
101      private double                field_9_headermargin;
102      private double                field_10_footermargin;
103      private short                 field_11_copies;
104  
105      public PrintSetupRecord()
106      {
107      }
108  
109      /**
110       * Constructs a PrintSetup (SETUP) record and sets its fields appropriately.
111       *
112       * @param id     id must be 0xa1 or an exception will be throw upon validation
113       * @param size  the size of the data area of the record
114       * @param data  data of the record (should not contain sid/len)
115       */
116  
117      public PrintSetupRecord(short id, short size, byte [] data)
118      {
119          super(id, size, data);
120      }
121  
122      /**
123       * Constructs a PrintSetup (SETUP) record and sets its fields appropriately.
124       *
125       * @param id     id must be 0xa1 or an exception will be throw upon validation
126       * @param size  the size of the data area of the record
127       * @param data  data of the record (should not contain sid/len)
128       */
129  
130      public PrintSetupRecord(short id, short size, byte [] data, int offset)
131      {
132          super(id, size, data, offset);
133      }
134  
135      protected void validateSid(short id)
136      {
137          if (id != sid)
138          {
139              throw new RecordFormatException(
140                  "NOT A valid PrintSetup record RECORD");
141          }
142      }
143  
144      protected void fillFields(byte [] data, short size, int offset)
145      {
146          field_1_paper_size    = LittleEndian.getShort(data, 0 + offset);
147          field_2_scale         = LittleEndian.getShort(data, 2 + offset);
148          field_3_page_start    = LittleEndian.getShort(data, 4 + offset);
149          field_4_fit_width     = LittleEndian.getShort(data, 6 + offset);
150          field_5_fit_height    = LittleEndian.getShort(data, 8 + offset);
151          field_6_options       = LittleEndian.getShort(data, 10 + offset);
152          field_7_hresolution   = LittleEndian.getShort(data, 12 + offset);
153          field_8_vresolution   = LittleEndian.getShort(data, 14 + offset);
154          field_9_headermargin  = LittleEndian.getDouble(data, 16 + offset);
155          field_10_footermargin = LittleEndian.getDouble(data, 24 + offset);
156          field_11_copies       = LittleEndian.getShort(data, 32 + offset);
157      }
158  
159      public void setPaperSize(short size)
160      {
161          field_1_paper_size = size;
162      }
163  
164      public void setScale(short scale)
165      {
166          field_2_scale = scale;
167      }
168  
169      public void setPageStart(short start)
170      {
171          field_3_page_start = start;
172      }
173  
174      public void setFitWidth(short width)
175      {
176          field_4_fit_width = width;
177      }
178  
179      public void setFitHeight(short height)
180      {
181          field_5_fit_height = height;
182      }
183  
184      public void setOptions(short options)
185      {
186          field_6_options = options;
187      }
188  
189      // option bitfields
190      public void setLeftToRight(boolean ltor)
191      {
192          field_6_options = lefttoright.setShortBoolean(field_6_options, ltor);
193      }
194  
195      public void setLandscape(boolean ls)
196      {
197          field_6_options = landscape.setShortBoolean(field_6_options, ls);
198      }
199  
200      public void setValidSettings(boolean valid)
201      {
202          field_6_options = validsettings.setShortBoolean(field_6_options, valid);
203      }
204  
205      public void setNoColor(boolean mono)
206      {
207          field_6_options = nocolor.setShortBoolean(field_6_options, mono);
208      }
209  
210      public void setDraft(boolean d)
211      {
212          field_6_options = draft.setShortBoolean(field_6_options, d);
213      }
214  
215      public void setNotes(boolean printnotes)
216      {
217          field_6_options = notes.setShortBoolean(field_6_options, printnotes);
218      }
219  
220      public void setNoOrientation(boolean orientation)
221      {
222          field_6_options = noOrientation.setShortBoolean(field_6_options, orientation);
223      }
224  
225      public void setUsePage(boolean page)
226      {
227          field_6_options = usepage.setShortBoolean(field_6_options, page);
228      }
229  
230      // end option bitfields
231      public void setHResolution(short resolution)
232      {
233          field_7_hresolution = resolution;
234      }
235  
236      public void setVResolution(short resolution)
237      {
238          field_8_vresolution = resolution;
239      }
240  
241      public void setHeaderMargin(double headermargin)
242      {
243          field_9_headermargin = headermargin;
244      }
245  
246      public void setFooterMargin(double footermargin)
247      {
248          field_10_footermargin = footermargin;
249      }
250  
251      public void setCopies(short copies)
252      {
253          field_11_copies = copies;
254      }
255  
256      public short getPaperSize()
257      {
258          return field_1_paper_size;
259      }
260  
261      public short getScale()
262      {
263          return field_2_scale;
264      }
265  
266      public short getPageStart()
267      {
268          return field_3_page_start;
269      }
270  
271      public short getFitWidth()
272      {
273          return field_4_fit_width;
274      }
275  
276      public short getFitHeight()
277      {
278          return field_5_fit_height;
279      }
280  
281      public short getOptions()
282      {
283          return field_6_options;
284      }
285  
286      // option bitfields
287      public boolean getLeftToRight()
288      {
289          return lefttoright.isSet(field_6_options);
290      }
291  
292      public boolean getLandscape()
293      {
294          return landscape.isSet(field_6_options);
295      }
296  
297      public boolean getValidSettings()
298      {
299          return validsettings.isSet(field_6_options);
300      }
301  
302      public boolean getNoColor()
303      {
304          return nocolor.isSet(field_6_options);
305      }
306  
307      public boolean getDraft()
308      {
309          return draft.isSet(field_6_options);
310      }
311  
312      public boolean getNotes()
313      {
314          return notes.isSet(field_6_options);
315      }
316  
317      public boolean getNoOrientation()
318      {
319          return noOrientation.isSet(field_6_options);
320      }
321  
322      public boolean getUsePage()
323      {
324          return usepage.isSet(field_6_options);
325      }
326  
327      // end option bitfields
328      public short getHResolution()
329      {
330          return field_7_hresolution;
331      }
332  
333      public short getVResolution()
334      {
335          return field_8_vresolution;
336      }
337  
338      public double getHeaderMargin()
339      {
340          return field_9_headermargin;
341      }
342  
343      public double getFooterMargin()
344      {
345          return field_10_footermargin;
346      }
347  
348      public short getCopies()
349      {
350          return field_11_copies;
351      }
352  
353      public String toString()
354      {
355          StringBuffer buffer = new StringBuffer();
356  
357          buffer.append("[PRINTSETUP]\n");
358          buffer.append("    .papersize      = ").append(getPaperSize())
359              .append("\n");
360          buffer.append("    .scale          = ").append(getScale())
361              .append("\n");
362          buffer.append("    .pagestart      = ").append(getPageStart())
363              .append("\n");
364          buffer.append("    .fitwidth       = ").append(getFitWidth())
365              .append("\n");
366          buffer.append("    .fitheight      = ").append(getFitHeight())
367              .append("\n");
368          buffer.append("    .options        = ").append(getOptions())
369              .append("\n");
370          buffer.append("        .ltor       = ").append(getLeftToRight())
371              .append("\n");
372          buffer.append("        .landscape  = ").append(getLandscape())
373              .append("\n");
374          buffer.append("        .valid      = ").append(getValidSettings())
375              .append("\n");
376          buffer.append("        .mono       = ").append(getNoColor())
377              .append("\n");
378          buffer.append("        .draft      = ").append(getDraft())
379              .append("\n");
380          buffer.append("        .notes      = ").append(getNotes())
381              .append("\n");
382          buffer.append("        .noOrientat = ").append(getNoOrientation())
383              .append("\n");
384          buffer.append("        .usepage    = ").append(getUsePage())
385              .append("\n");
386          buffer.append("    .hresolution    = ").append(getHResolution())
387              .append("\n");
388          buffer.append("    .vresolution    = ").append(getVResolution())
389              .append("\n");
390          buffer.append("    .headermargin   = ").append(getHeaderMargin())
391              .append("\n");
392          buffer.append("    .footermargin   = ").append(getFooterMargin())
393              .append("\n");
394          buffer.append("    .copies         = ").append(getCopies())
395              .append("\n");
396          buffer.append("[/PRINTSETUP]\n");
397          return buffer.toString();
398      }
399  
400      public int serialize(int offset, byte [] data)
401      {
402          LittleEndian.putShort(data, 0 + offset, sid);
403          LittleEndian.putShort(data, 2 + offset, ( short ) 34);
404          LittleEndian.putShort(data, 4 + offset, getPaperSize());
405          LittleEndian.putShort(data, 6 + offset, getScale());
406          LittleEndian.putShort(data, 8 + offset, getPageStart());
407          LittleEndian.putShort(data, 10 + offset, getFitWidth());
408          LittleEndian.putShort(data, 12 + offset, getFitHeight());
409          LittleEndian.putShort(data, 14 + offset, getOptions());
410          LittleEndian.putShort(data, 16 + offset, getHResolution());
411          LittleEndian.putShort(data, 18 + offset, getVResolution());
412          LittleEndian.putDouble(data, 20 + offset, getHeaderMargin());
413          LittleEndian.putDouble(data, 28 + offset, getFooterMargin());
414          LittleEndian.putShort(data, 36 + offset, getCopies());
415          return getRecordSize();
416      }
417  
418      public int getRecordSize()
419      {
420          return 38;
421      }
422  
423      public short getSid()
424      {
425          return this.sid;
426      }
427  
428      public Object clone() {
429        PrintSetupRecord rec = new PrintSetupRecord();
430        rec.field_1_paper_size = field_1_paper_size;
431        rec.field_2_scale = field_2_scale;
432        rec.field_3_page_start = field_3_page_start;
433        rec.field_4_fit_width = field_4_fit_width;
434        rec.field_5_fit_height = field_5_fit_height;
435        rec.field_6_options = field_6_options;
436        rec.field_7_hresolution = field_7_hresolution;
437        rec.field_8_vresolution = field_8_vresolution;
438        rec.field_9_headermargin = field_9_headermargin;
439        rec.field_10_footermargin = field_10_footermargin;
440        rec.field_11_copies = field_11_copies;
441        return rec;
442      }
443  }
444