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    * @version 2.0-pre
67    */
68   
69   public class PrintSetupRecord
70       extends Record
71   {
72       public final static short     sid = 0xa1;
73       private short                 field_1_paper_size;
74       private short                 field_2_scale;
75       private short                 field_3_page_start;
76       private short                 field_4_fit_width;
77       private short                 field_5_fit_height;
78       private short                 field_6_options;
79       static final private BitField lefttoright   =
80           new BitField(0x01);   // print over then down
81       static final private BitField landscape     =
82           new BitField(0x02);   // landscape mode
83       static final private BitField validsettings = new BitField(
84           0x04);                // if papersize, scale, resolution, copies, landscape
85   
86       // weren't obtained from the print consider them
87       // mere bunk
88       static final private BitField nocolor       =
89           new BitField(0x08);   // print mono/b&w, colorless
90       static final private BitField draft         =
91           new BitField(0x10);   // print draft quality
92       static final private BitField notes         =
93           new BitField(0x20);   // print the notes
94       static final private BitField noOrientation =
95           new BitField(0x40);   // the orientation is not set
96       static final private BitField usepage       =
97           new BitField(0x80);   // use a user set page no, instead of auto
98       private short                 field_7_hresolution;
99       private short                 field_8_vresolution;
100      private double                field_9_headermargin;
101      private double                field_10_footermargin;
102      private short                 field_11_copies;
103  
104      public PrintSetupRecord()
105      {
106      }
107  
108      /**
109       * Constructs a PrintSetup (SETUP) record and sets its fields appropriately.
110       *
111       * @param id     id must be 0xa1 or an exception will be throw upon validation
112       * @param size  the size of the data area of the record
113       * @param data  data of the record (should not contain sid/len)
114       */
115  
116      public PrintSetupRecord(short id, short size, byte [] data)
117      {
118          super(id, size, data);
119      }
120  
121      /**
122       * Constructs a PrintSetup (SETUP) record and sets its fields appropriately.
123       *
124       * @param id     id must be 0xa1 or an exception will be throw upon validation
125       * @param size  the size of the data area of the record
126       * @param data  data of the record (should not contain sid/len)
127       */
128  
129      public PrintSetupRecord(short id, short size, byte [] data, int offset)
130      {
131          super(id, size, data, offset);
132      }
133  
134      protected void validateSid(short id)
135      {
136          if (id != sid)
137          {
138              throw new RecordFormatException(
139                  "NOT A valid PrintSetup record RECORD");
140          }
141      }
142  
143      protected void fillFields(byte [] data, short size, int offset)
144      {
145          field_1_paper_size    = LittleEndian.getShort(data, 0 + offset);
146          field_2_scale         = LittleEndian.getShort(data, 2 + offset);
147          field_3_page_start    = LittleEndian.getShort(data, 4 + offset);
148          field_4_fit_width     = LittleEndian.getShort(data, 6 + offset);
149          field_5_fit_height    = LittleEndian.getShort(data, 8 + offset);
150          field_6_options       = LittleEndian.getShort(data, 10 + offset);
151          field_7_hresolution   = LittleEndian.getShort(data, 12 + offset);
152          field_8_vresolution   = LittleEndian.getShort(data, 14 + offset);
153          field_9_headermargin  = LittleEndian.getDouble(data, 16 + offset);
154          field_10_footermargin = LittleEndian.getDouble(data, 24 + offset);
155          field_11_copies       = LittleEndian.getShort(data, 32 + offset);
156      }
157  
158      public void setPaperSize(short size)
159      {
160          field_1_paper_size = size;
161      }
162  
163      public void setScale(short scale)
164      {
165          field_2_scale = scale;
166      }
167  
168      public void setPageStart(short start)
169      {
170          field_3_page_start = start;
171      }
172  
173      public void setFitWidth(short width)
174      {
175          field_4_fit_width = width;
176      }
177  
178      public void setFitHeight(short height)
179      {
180          field_5_fit_height = height;
181      }
182  
183      public void setOptions(short options)
184      {
185          field_6_options = options;
186      }
187  
188      // option bitfields
189      public void setLeftToRight(boolean ltor)
190      {
191          field_6_options = lefttoright.setShortBoolean(field_6_options, ltor);
192      }
193  
194      public void setLandscape(boolean ls)
195      {
196          field_6_options = landscape.setShortBoolean(field_6_options, ls);
197      }
198  
199      public void setValidSettings(boolean valid)
200      {
201          field_6_options = validsettings.setShortBoolean(field_6_options, valid);
202      }
203  
204      public void setNoColor(boolean mono)
205      {
206          field_6_options = nocolor.setShortBoolean(field_6_options, mono);
207      }
208  
209      public void setDraft(boolean d)
210      {
211          field_6_options = draft.setShortBoolean(field_6_options, d);
212      }
213  
214      public void setNotes(boolean printnotes)
215      {
216          field_6_options = notes.setShortBoolean(field_6_options, printnotes);
217      }
218  
219      public void setNoOrientation(boolean orientation)
220      {
221          field_6_options = noOrientation.setShortBoolean(field_6_options, orientation);
222      }
223  
224      public void setUsePage(boolean page)
225      {
226          field_6_options = usepage.setShortBoolean(field_6_options, page);
227      }
228  
229      // end option bitfields
230      public void setHResolution(short resolution)
231      {
232          field_7_hresolution = resolution;
233      }
234  
235      public void setVResolution(short resolution)
236      {
237          field_8_vresolution = resolution;
238      }
239  
240      public void setHeaderMargin(double headermargin)
241      {
242          field_9_headermargin = headermargin;
243      }
244  
245      public void setFooterMargin(double footermargin)
246      {
247          field_10_footermargin = footermargin;
248      }
249  
250      public void setCopies(short copies)
251      {
252          field_11_copies = copies;
253      }
254  
255      public short getPaperSize()
256      {
257          return field_1_paper_size;
258      }
259  
260      public short getScale()
261      {
262          return field_2_scale;
263      }
264  
265      public short getPageStart()
266      {
267          return field_3_page_start;
268      }
269  
270      public short getFitWidth()
271      {
272          return field_4_fit_width;
273      }
274  
275      public short getFitHeight()
276      {
277          return field_5_fit_height;
278      }
279  
280      public short getOptions()
281      {
282          return field_6_options;
283      }
284  
285      // option bitfields
286      public boolean getLeftToRight()
287      {
288          return lefttoright.isSet(field_6_options);
289      }
290  
291      public boolean getLandscape()
292      {
293          return landscape.isSet(field_6_options);
294      }
295  
296      public boolean getValidSettings()
297      {
298          return validsettings.isSet(field_6_options);
299      }
300  
301      public boolean getNoColor()
302      {
303          return nocolor.isSet(field_6_options);
304      }
305  
306      public boolean getDraft()
307      {
308          return draft.isSet(field_6_options);
309      }
310  
311      public boolean getNotes()
312      {
313          return notes.isSet(field_6_options);
314      }
315  
316      public boolean getNoOrientation()
317      {
318          return noOrientation.isSet(field_6_options);
319      }
320  
321      public boolean getUsePage()
322      {
323          return usepage.isSet(field_6_options);
324      }
325  
326      // end option bitfields
327      public short getHResolution()
328      {
329          return field_7_hresolution;
330      }
331  
332      public short getVResolution()
333      {
334          return field_8_vresolution;
335      }
336  
337      public double getHeaderMargin()
338      {
339          return field_9_headermargin;
340      }
341  
342      public double getFooterMargin()
343      {
344          return field_10_footermargin;
345      }
346  
347      public short getCopies()
348      {
349          return field_11_copies;
350      }
351  
352      public String toString()
353      {
354          StringBuffer buffer = new StringBuffer();
355  
356          buffer.append("[PRINTSETUP]\n");
357          buffer.append("    .papersize      = ").append(getPaperSize())
358              .append("\n");
359          buffer.append("    .scale          = ").append(getScale())
360              .append("\n");
361          buffer.append("    .pagestart      = ").append(getPageStart())
362              .append("\n");
363          buffer.append("    .fitwidth       = ").append(getFitWidth())
364              .append("\n");
365          buffer.append("    .fitheight      = ").append(getFitHeight())
366              .append("\n");
367          buffer.append("    .options        = ").append(getOptions())
368              .append("\n");
369          buffer.append("        .ltor       = ").append(getLeftToRight())
370              .append("\n");
371          buffer.append("        .landscape  = ").append(getLandscape())
372              .append("\n");
373          buffer.append("        .valid      = ").append(getValidSettings())
374              .append("\n");
375          buffer.append("        .mono       = ").append(getNoColor())
376              .append("\n");
377          buffer.append("        .draft      = ").append(getDraft())
378              .append("\n");
379          buffer.append("        .notes      = ").append(getNotes())
380              .append("\n");
381          buffer.append("        .noOrientat = ").append(getNoOrientation())
382              .append("\n");
383          buffer.append("        .usepage    = ").append(getUsePage())
384              .append("\n");
385          buffer.append("    .hresolution    = ").append(getHResolution())
386              .append("\n");
387          buffer.append("    .vresolution    = ").append(getVResolution())
388              .append("\n");
389          buffer.append("    .headermargin   = ").append(getHeaderMargin())
390              .append("\n");
391          buffer.append("    .footermargin   = ").append(getFooterMargin())
392              .append("\n");
393          buffer.append("    .copies         = ").append(getCopies())
394              .append("\n");
395          buffer.append("[/PRINTSETUP]\n");
396          return buffer.toString();
397      }
398  
399      public int serialize(int offset, byte [] data)
400      {
401          LittleEndian.putShort(data, 0 + offset, sid);
402          LittleEndian.putShort(data, 2 + offset, ( short ) 34);
403          LittleEndian.putShort(data, 4 + offset, getPaperSize());
404          LittleEndian.putShort(data, 6 + offset, getScale());
405          LittleEndian.putShort(data, 8 + offset, getPageStart());
406          LittleEndian.putShort(data, 10 + offset, getFitWidth());
407          LittleEndian.putShort(data, 12 + offset, getFitHeight());
408          LittleEndian.putShort(data, 14 + offset, getOptions());
409          LittleEndian.putShort(data, 16 + offset, getHResolution());
410          LittleEndian.putShort(data, 18 + offset, getVResolution());
411          LittleEndian.putDouble(data, 20 + offset, getHeaderMargin());
412          LittleEndian.putDouble(data, 28 + offset, getFooterMargin());
413          LittleEndian.putShort(data, 36 + offset, getCopies());
414          return getRecordSize();
415      }
416  
417      public int getRecordSize()
418      {
419          return 38;
420      }
421  
422      public short getSid()
423      {
424          return this.sid;
425      }
426  }
427