001: /*
002: * $Id: RtfPageSetting.java 2796 2007-05-27 09:42:20Z psoares33 $
003: * $Name: $
004: *
005: * Copyright 2003, 2004 by Mark Hall
006: *
007: * The contents of this file are subject to the Mozilla Public License Version 1.1
008: * (the "License"); you may not use this file except in compliance with the License.
009: * You may obtain a copy of the License at http://www.mozilla.org/MPL/
010: *
011: * Software distributed under the License is distributed on an "AS IS" basis,
012: * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
013: * for the specific language governing rights and limitations under the License.
014: *
015: * The Original Code is 'iText, a free JAVA-PDF library'.
016: *
017: * The Initial Developer of the Original Code is Bruno Lowagie. Portions created by
018: * the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie.
019: * All Rights Reserved.
020: * Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer
021: * are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved.
022: *
023: * Contributor(s): all the names of the contributors are added in the source code
024: * where applicable.
025: *
026: * Alternatively, the contents of this file may be used under the terms of the
027: * LGPL license (the ?GNU LIBRARY GENERAL PUBLIC LICENSE?), in which case the
028: * provisions of LGPL are applicable instead of those above. If you wish to
029: * allow use of your version of this file only under the terms of the LGPL
030: * License and not to allow others to use your version of this file under
031: * the MPL, indicate your decision by deleting the provisions above and
032: * replace them with the notice and other provisions required by the LGPL.
033: * If you do not delete the provisions above, a recipient may use your version
034: * of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE.
035: *
036: * This library is free software; you can redistribute it and/or modify it
037: * under the terms of the MPL as stated above or under the terms of the GNU
038: * Library General Public License as published by the Free Software Foundation;
039: * either version 2 of the License, or any later version.
040: *
041: * This library is distributed in the hope that it will be useful, but WITHOUT
042: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
043: * FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more
044: * details.
045: *
046: * If you didn't download this code from the following link, you should check if
047: * you aren't using an obsolete version:
048: * http://www.lowagie.com/iText/
049: */
050:
051: package com.lowagie.text.rtf.document;
052:
053: import java.io.ByteArrayOutputStream;
054: import java.io.IOException;
055: import java.io.OutputStream;
056:
057: import com.lowagie.text.PageSize;
058: import com.lowagie.text.Rectangle;
059: import com.lowagie.text.rtf.RtfElement;
060: import com.lowagie.text.rtf.RtfExtendedElement;
061:
062: /**
063: * The RtfPageSetting stores the page size / page margins for a RtfDocument.
064: * INTERNAL CLASS - NOT TO BE USED DIRECTLY
065: *
066: * @version $Id: RtfPageSetting.java 2796 2007-05-27 09:42:20Z psoares33 $
067: * @author Mark Hall (mhall@edu.uni-klu.ac.at)
068: * @author Thomas Bickel (tmb99@inode.at)
069: */
070: public class RtfPageSetting extends RtfElement implements
071: RtfExtendedElement {
072:
073: /**
074: * Constant for the page height
075: */
076: private static final byte[] PAGE_WIDTH = "\\paperw".getBytes();
077: /**
078: * Constant for the page width
079: */
080: private static final byte[] PAGE_HEIGHT = "\\paperh".getBytes();
081: /**
082: * Constant for the left margin
083: */
084: private static final byte[] MARGIN_LEFT = "\\margl".getBytes();
085: /**
086: * Constant for the right margin
087: */
088: private static final byte[] MARGIN_RIGHT = "\\margr".getBytes();
089: /**
090: * Constant for the top margin
091: */
092: private static final byte[] MARGIN_TOP = "\\margt".getBytes();
093: /**
094: * Constant for the bottom margin
095: */
096: private static final byte[] MARGIN_BOTTOM = "\\margb".getBytes();
097: /**
098: * Constant for landscape
099: */
100: private static final byte[] LANDSCAPE = "\\lndscpsxn".getBytes();
101: /**
102: * Constant for the section page width
103: */
104: private static final byte[] SECTION_PAGE_WIDTH = "\\pgwsxn"
105: .getBytes();
106: /**
107: * Constant for the section page height
108: */
109: private static final byte[] SECTION_PAGE_HEIGHT = "\\pghsxn"
110: .getBytes();
111: /**
112: * Constant for the section left margin
113: */
114: private static final byte[] SECTION_MARGIN_LEFT = "\\marglsxn"
115: .getBytes();
116: /**
117: * Constant for the section right margin
118: */
119: private static final byte[] SECTION_MARGIN_RIGHT = "\\margrsxn"
120: .getBytes();
121: /**
122: * Constant for the section top margin
123: */
124: private static final byte[] SECTION_MARGIN_TOP = "\\margtsxn"
125: .getBytes();
126: /**
127: * Constant for the section bottom margin
128: */
129: private static final byte[] SECTION_MARGIN_BOTTOM = "\\margbsxn"
130: .getBytes();
131:
132: /**
133: * The page width to use
134: */
135: private int pageWidth = 11906;
136: /**
137: * The page height to use
138: */
139: private int pageHeight = 16840;
140: /**
141: * The left margin to use
142: */
143: private int marginLeft = 1800;
144: /**
145: * The right margin to use
146: */
147: private int marginRight = 1800;
148: /**
149: * The top margin to use
150: */
151: private int marginTop = 1440;
152: /**
153: * The bottom margin to use
154: */
155: private int marginBottom = 1440;
156: /**
157: * Whether the page is portrait or landscape
158: */
159: private boolean landscape = false;
160:
161: /**
162: * Constructs a new RtfPageSetting object belonging to a RtfDocument.
163: *
164: * @param doc The RtfDocument this RtfPageSetting belongs to
165: */
166: public RtfPageSetting(RtfDocument doc) {
167: super (doc);
168: }
169:
170: /**
171: * unused
172: * @deprecated replaced by {@link #writeContent(OutputStream)}
173: */
174: public byte[] write() {
175: return (new byte[0]);
176: }
177:
178: /**
179: * unused
180: */
181: public void writeContent(final OutputStream out) throws IOException {
182: }
183:
184: /**
185: * Writes the page size / page margin definition
186: *
187: * @return A byte array with the page size / page margin definition
188: * @deprecated replaced by {@link #writeDefinition(OutputStream)}
189: */
190: public byte[] writeDefinition() {
191: ByteArrayOutputStream result = new ByteArrayOutputStream();
192: try {
193: writeDefinition(result);
194: } catch (IOException ioe) {
195: ioe.printStackTrace();
196: }
197: return result.toByteArray();
198: }
199:
200: /**
201: * Writes the page size / page margin definition
202: */
203: public void writeDefinition(final OutputStream result)
204: throws IOException {
205: result.write(PAGE_WIDTH);
206: result.write(intToByteArray(pageWidth));
207: result.write(PAGE_HEIGHT);
208: result.write(intToByteArray(pageHeight));
209: result.write(MARGIN_LEFT);
210: result.write(intToByteArray(marginLeft));
211: result.write(MARGIN_RIGHT);
212: result.write(intToByteArray(marginRight));
213: result.write(MARGIN_TOP);
214: result.write(intToByteArray(marginTop));
215: result.write(MARGIN_BOTTOM);
216: result.write(intToByteArray(marginBottom));
217: result.write((byte) '\n');
218: }
219:
220: /**
221: * Writes the definition part for a new section
222: *
223: * @return A byte array containing the definition for a new section
224: * @deprecated replaced by {@link #writeSectionDefinition(OutputStream)}
225: */
226: public byte[] writeSectionDefinition() {
227: ByteArrayOutputStream result = new ByteArrayOutputStream();
228: try {
229: writeSectionDefinition(result);
230: } catch (IOException ioe) {
231: ioe.printStackTrace();
232: }
233: return result.toByteArray();
234: }
235:
236: /**
237: * Writes the definition part for a new section
238: */
239: public void writeSectionDefinition(final OutputStream result)
240: throws IOException {
241: if (landscape) {
242: result.write(LANDSCAPE);
243: result.write(SECTION_PAGE_WIDTH);
244: result.write(intToByteArray(pageWidth));
245: result.write(SECTION_PAGE_HEIGHT);
246: result.write(intToByteArray(pageHeight));
247: result.write((byte) '\n');
248: } else {
249: result.write(SECTION_PAGE_WIDTH);
250: result.write(intToByteArray(pageWidth));
251: result.write(SECTION_PAGE_HEIGHT);
252: result.write(intToByteArray(pageHeight));
253: result.write((byte) '\n');
254: }
255: result.write(SECTION_MARGIN_LEFT);
256: result.write(intToByteArray(marginLeft));
257: result.write(SECTION_MARGIN_RIGHT);
258: result.write(intToByteArray(marginRight));
259: result.write(SECTION_MARGIN_TOP);
260: result.write(intToByteArray(marginTop));
261: result.write(SECTION_MARGIN_BOTTOM);
262: result.write(intToByteArray(marginBottom));
263: }
264:
265: /**
266: * Gets the bottom margin
267: *
268: * @return Returns the bottom margin
269: */
270: public int getMarginBottom() {
271: return marginBottom;
272: }
273:
274: /**
275: * Sets the bottom margin
276: *
277: * @param marginBottom The bottom margin to use
278: */
279: public void setMarginBottom(int marginBottom) {
280: this .marginBottom = marginBottom;
281: }
282:
283: /**
284: * Gets the left margin
285: *
286: * @return Returns the left margin
287: */
288: public int getMarginLeft() {
289: return marginLeft;
290: }
291:
292: /**
293: * Sets the left margin to use
294: *
295: * @param marginLeft The left margin to use
296: */
297: public void setMarginLeft(int marginLeft) {
298: this .marginLeft = marginLeft;
299: }
300:
301: /**
302: * Gets the right margin
303: *
304: * @return Returns the right margin
305: */
306: public int getMarginRight() {
307: return marginRight;
308: }
309:
310: /**
311: * Sets the right margin to use
312: *
313: * @param marginRight The right margin to use
314: */
315: public void setMarginRight(int marginRight) {
316: this .marginRight = marginRight;
317: }
318:
319: /**
320: * Gets the top margin
321: *
322: * @return Returns the top margin
323: */
324: public int getMarginTop() {
325: return marginTop;
326: }
327:
328: /**
329: * Sets the top margin to use
330: *
331: * @param marginTop The top margin to use
332: */
333: public void setMarginTop(int marginTop) {
334: this .marginTop = marginTop;
335: }
336:
337: /**
338: * Gets the page height
339: *
340: * @return Returns the page height
341: */
342: public int getPageHeight() {
343: return pageHeight;
344: }
345:
346: /**
347: * Sets the page height to use
348: *
349: * @param pageHeight The page height to use
350: */
351: public void setPageHeight(int pageHeight) {
352: this .pageHeight = pageHeight;
353: }
354:
355: /**
356: * Gets the page width
357: *
358: * @return Returns the page width
359: */
360: public int getPageWidth() {
361: return pageWidth;
362: }
363:
364: /**
365: * Sets the page width to use
366: *
367: * @param pageWidth The page width to use
368: */
369: public void setPageWidth(int pageWidth) {
370: this .pageWidth = pageWidth;
371: }
372:
373: /**
374: * Set the page size to use. This method will use guessFormat to try to guess the correct
375: * page format. If no format could be guessed, the sizes from the pageSize are used and
376: * the landscape setting is determined by comparing width and height;
377: *
378: * @param pageSize The pageSize to use
379: */
380: public void setPageSize(Rectangle pageSize) {
381: if (!guessFormat(pageSize, false)) {
382: this .pageWidth = (int) (pageSize.getWidth() * RtfElement.TWIPS_FACTOR);
383: this .pageHeight = (int) (pageSize.getHeight() * RtfElement.TWIPS_FACTOR);
384: this .landscape = pageWidth > pageHeight;
385: }
386: }
387:
388: /**
389: * This method tries to fit the <code>Rectangle pageSize</code> to one of the predefined PageSize rectangles.
390: * If a match is found the pageWidth and pageHeight will be set according to values determined from files
391: * generated by MS Word2000 and OpenOffice 641. If no match is found the method will try to match the rotated
392: * Rectangle by calling itself with the parameter rotate set to true.
393: *
394: * @param pageSize the page size for which to guess the correct format
395: * @param rotate Whether we should try to rotate the size befor guessing the format
396: * @return <code>True</code> if the format was guessed, <code>false/<code> otherwise
397: */
398: private boolean guessFormat(Rectangle pageSize, boolean rotate) {
399: if (rotate) {
400: pageSize = pageSize.rotate();
401: }
402: if (rectEquals(pageSize, PageSize.A3)) {
403: pageWidth = 16837;
404: pageHeight = 23811;
405: landscape = rotate;
406: return true;
407: }
408: if (rectEquals(pageSize, PageSize.A4)) {
409: pageWidth = 11907;
410: pageHeight = 16840;
411: landscape = rotate;
412: return true;
413: }
414: if (rectEquals(pageSize, PageSize.A5)) {
415: pageWidth = 8391;
416: pageHeight = 11907;
417: landscape = rotate;
418: return true;
419: }
420: if (rectEquals(pageSize, PageSize.A6)) {
421: pageWidth = 5959;
422: pageHeight = 8420;
423: landscape = rotate;
424: return true;
425: }
426: if (rectEquals(pageSize, PageSize.B4)) {
427: pageWidth = 14570;
428: pageHeight = 20636;
429: landscape = rotate;
430: return true;
431: }
432: if (rectEquals(pageSize, PageSize.B5)) {
433: pageWidth = 10319;
434: pageHeight = 14572;
435: landscape = rotate;
436: return true;
437: }
438: if (rectEquals(pageSize, PageSize.HALFLETTER)) {
439: pageWidth = 7927;
440: pageHeight = 12247;
441: landscape = rotate;
442: return true;
443: }
444: if (rectEquals(pageSize, PageSize.LETTER)) {
445: pageWidth = 12242;
446: pageHeight = 15842;
447: landscape = rotate;
448: return true;
449: }
450: if (rectEquals(pageSize, PageSize.LEGAL)) {
451: pageWidth = 12252;
452: pageHeight = 20163;
453: landscape = rotate;
454: return true;
455: }
456: if (!rotate && guessFormat(pageSize, true)) {
457: int x = pageWidth;
458: pageWidth = pageHeight;
459: pageHeight = x;
460: return true;
461: }
462: return false;
463: }
464:
465: /**
466: * This method compares to Rectangles. They are considered equal if width and height are the same
467: *
468: * @param rect1 The first Rectangle to compare
469: * @param rect2 The second Rectangle to compare
470: * @return <code>True</code> if the Rectangles equal, <code>false</code> otherwise
471: */
472: private boolean rectEquals(Rectangle rect1, Rectangle rect2) {
473: return (rect1.getWidth() == rect2.getWidth())
474: && (rect1.getHeight() == rect2.getHeight());
475: }
476: }
|