From 6025b6016517c6d898d8957d1d7e03ba71431912 Mon Sep 17 00:00:00 2001 From: tknall Date: Fri, 1 Dec 2006 12:20:24 +0000 Subject: Initial import of release 2.2. git-svn-id: https://joinup.ec.europa.eu/svn/pdf-as/trunk@4 7b5415b0-85f9-ee4d-85bd-d5d0c3b42d1c --- .../java/com/lowagie/text/rtf/style/RtfColor.java | 302 +++++++++ .../com/lowagie/text/rtf/style/RtfColorList.java | 137 +++++ .../java/com/lowagie/text/rtf/style/RtfFont.java | 681 +++++++++++++++++++++ .../com/lowagie/text/rtf/style/RtfFontList.java | 149 +++++ .../lowagie/text/rtf/style/RtfParagraphStyle.java | 665 ++++++++++++++++++++ .../lowagie/text/rtf/style/RtfStylesheetList.java | 121 ++++ 6 files changed, 2055 insertions(+) create mode 100644 src/main/java/com/lowagie/text/rtf/style/RtfColor.java create mode 100644 src/main/java/com/lowagie/text/rtf/style/RtfColorList.java create mode 100644 src/main/java/com/lowagie/text/rtf/style/RtfFont.java create mode 100644 src/main/java/com/lowagie/text/rtf/style/RtfFontList.java create mode 100644 src/main/java/com/lowagie/text/rtf/style/RtfParagraphStyle.java create mode 100644 src/main/java/com/lowagie/text/rtf/style/RtfStylesheetList.java (limited to 'src/main/java/com/lowagie/text/rtf/style') diff --git a/src/main/java/com/lowagie/text/rtf/style/RtfColor.java b/src/main/java/com/lowagie/text/rtf/style/RtfColor.java new file mode 100644 index 0000000..aca4ea2 --- /dev/null +++ b/src/main/java/com/lowagie/text/rtf/style/RtfColor.java @@ -0,0 +1,302 @@ +/* + * $Id: RtfColor.java,v 1.7 2004/12/14 12:51:59 blowagie Exp $ + * $Name: $ + * + * Copyright 2001, 2002, 2003, 2004 by Mark Hall + * + * The contents of this file are subject to the Mozilla Public License Version 1.1 + * (the "License"); you may not use this file except in compliance with the License. + * You may obtain a copy of the License at http://www.mozilla.org/MPL/ + * + * Software distributed under the License is distributed on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License + * for the specific language governing rights and limitations under the License. + * + * The Original Code is 'iText, a free JAVA-PDF library'. + * + * The Initial Developer of the Original Code is Bruno Lowagie. Portions created by + * the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie. + * All Rights Reserved. + * Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer + * are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved. + * + * Contributor(s): all the names of the contributors are added in the source code + * where applicable. + * + * Alternatively, the contents of this file may be used under the terms of the + * LGPL license (the ?GNU LIBRARY GENERAL PUBLIC LICENSE?), in which case the + * provisions of LGPL are applicable instead of those above. If you wish to + * allow use of your version of this file only under the terms of the LGPL + * License and not to allow others to use your version of this file under + * the MPL, indicate your decision by deleting the provisions above and + * replace them with the notice and other provisions required by the LGPL. + * If you do not delete the provisions above, a recipient may use your version + * of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE. + * + * This library is free software; you can redistribute it and/or modify it + * under the terms of the MPL as stated above or under the terms of the GNU + * Library General Public License as published by the Free Software Foundation; + * either version 2 of the License, or any later version. + * + * This library is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more + * details. + * + * If you didn't download this code from the following link, you should check if + * you aren't using an obsolete version: + * http://www.lowagie.com/iText/ + */ + +package com.lowagie.text.rtf.style; + +import java.awt.Color; +import java.io.ByteArrayOutputStream; +import java.io.IOException; + +import com.lowagie.text.rtf.RtfElement; +import com.lowagie.text.rtf.RtfExtendedElement; +import com.lowagie.text.rtf.document.RtfDocument; + + +/** + * The RtfColor stores one rtf color value for a rtf document + * + * @version $Version:$ + * @author Mark Hall (mhall@edu.uni-klu.ac.at) + */ +public class RtfColor extends RtfElement implements RtfExtendedElement { + + /** + * Constant for RED value + */ + private static final byte[] COLOR_RED = "\\red".getBytes(); + /** + * Constant for GREEN value + */ + private static final byte[] COLOR_GREEN = "\\green".getBytes(); + /** + * Constant for BLUE value + */ + private static final byte[] COLOR_BLUE = "\\blue".getBytes(); + /** + * Constant for the end of one color entry + */ + private static final byte COLON = (byte) ';'; + /** + * Constant for the number of the colour in the list of colours + */ + private static final byte[] COLOR_NUMBER = "\\cf".getBytes(); + + /** + * The number of the colour in the list of colours + */ + private int colorNumber = 0; + /** + * The red value + */ + private int red = 0; + /** + * The green value + */ + private int green = 0; + /** + * The blue value + */ + private int blue = 0; + + /** + * Constructor only for use when initializing the RtfColorList + * + * @param doc The RtfDocument this RtfColor belongs to + * @param red The red value to use + * @param green The green value to use + * @param blue The blue value to use + * @param colorNumber The number of the colour in the colour list + */ + protected RtfColor(RtfDocument doc, int red, int green, int blue, int colorNumber) { + super(doc); + this.red = red; + this.blue = blue; + this.green = green; + this.colorNumber = colorNumber; + } + + /** + * Constructs a RtfColor as a clone of an existing RtfColor + * + * @param doc The RtfDocument this RtfColor belongs to + * @param col The RtfColor to use as a base + */ + public RtfColor(RtfDocument doc, RtfColor col) { + super(doc); + if(col != null) { + this.red = col.getRed(); + this.green = col.getGreen(); + this.blue = col.getBlue(); + } + if(this.document != null) { + this.colorNumber = this.document.getDocumentHeader().getColorNumber(this); + } + } + + /** + * Constructs a RtfColor based on the Color + * + * @param doc The RtfDocument this RtfColor belongs to + * @param col The Color to base this RtfColor on + */ + public RtfColor(RtfDocument doc, Color col) { + super(doc); + if(col != null) { + this.red = col.getRed(); + this.blue = col.getBlue(); + this.green = col.getGreen(); + } + if(this.document != null) { + this.colorNumber = this.document.getDocumentHeader().getColorNumber(this); + } + } + + /** + * Constructs a RtfColor based on the red/green/blue values + * + * @param doc The RtfDocument this RtfColor belongs to + * @param red The red value to use + * @param green The green value to use + * @param blue The blue value to use + */ + public RtfColor(RtfDocument doc, int red, int green, int blue) { + super(doc); + this.red = red; + this.blue = blue; + this.green = green; + if(this.document != null) { + this.colorNumber = this.document.getDocumentHeader().getColorNumber(this); + } + } + + /** + * Write the definition part of this RtfColor. + * + * @return A byte array with the definition of this colour + */ + public byte[] writeDefinition() { + ByteArrayOutputStream result = new ByteArrayOutputStream(); + try { + result.write(COLOR_RED); + result.write(intToByteArray(red)); + result.write(COLOR_GREEN); + result.write(intToByteArray(green)); + result.write(COLOR_BLUE); + result.write(intToByteArray(blue)); + result.write(COLON); + } catch(IOException ioe) { + ioe.printStackTrace(); + } + return result.toByteArray(); + } + + /** + * Writes the beginning of this RtfColor + * + * @return A byte array with the colour start data + */ + public byte[] writeBegin() { + ByteArrayOutputStream result = new ByteArrayOutputStream(); + try { + result.write(COLOR_NUMBER); + result.write(intToByteArray(colorNumber)); + } catch(IOException ioe) { + ioe.printStackTrace(); + } + return result.toByteArray(); + } + + /** + * Unused + * + * @return An empty (byte[0]) byte array + */ + public byte[] writeEnd() { + return new byte[0]; + } + + /** + * Tests if this RtfColor is equal to another RtfColor. + * + * @param obj another RtfColor + * @return True if red, green and blue values of the two colours match, + * false otherwise. + */ + public boolean equals(Object obj) { + if(!(obj instanceof RtfColor)) { + return false; + } + RtfColor color = (RtfColor) obj; + if(this.red == color.getRed() && this.green == color.getGreen() && this.blue == color.getBlue()) { + return true; + } else { + return false; + } + } + + /** + * Returns the hash code of this RtfColor. The hash code is + * an integer with the lowest three bytes containing the values + * of red, green and blue. + * + * @return The hash code of this RtfColor + */ + public int hashCode() { + return (this.red << 16) | (this.green << 8) | this.blue; + } + + /** + * Get the blue value of this RtfColor + * + * @return The blue value + */ + public int getBlue() { + return blue; + } + + /** + * Get the green value of this RtfColor + * + * @return The green value + */ + public int getGreen() { + return green; + } + + /** + * Get the red value of this RtfColor + * + * @return The red value + */ + public int getRed() { + return red; + } + + /** + * Gets the number of this RtfColor in the list of colours + * + * @return Returns the colorNumber. + */ + public int getColorNumber() { + return colorNumber; + } + + /** + * Sets the RtfDocument this RtfColor belongs to + * + * @param doc The RtfDocument to use + */ + public void setRtfDocument(RtfDocument doc) { + super.setRtfDocument(doc); + if(document != null) { + this.colorNumber = document.getDocumentHeader().getColorNumber(this); + } + } +} diff --git a/src/main/java/com/lowagie/text/rtf/style/RtfColorList.java b/src/main/java/com/lowagie/text/rtf/style/RtfColorList.java new file mode 100644 index 0000000..ddb71d1 --- /dev/null +++ b/src/main/java/com/lowagie/text/rtf/style/RtfColorList.java @@ -0,0 +1,137 @@ +/* + * $Id: RtfColorList.java,v 1.16 2005/05/04 14:33:52 blowagie Exp $ + * $Name: $ + * + * Copyright 2001, 2002, 2003, 2004 by Mark Hall + * + * The contents of this file are subject to the Mozilla Public License Version 1.1 + * (the "License"); you may not use this file except in compliance with the License. + * You may obtain a copy of the License at http://www.mozilla.org/MPL/ + * + * Software distributed under the License is distributed on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License + * for the specific language governing rights and limitations under the License. + * + * The Original Code is 'iText, a free JAVA-PDF library'. + * + * The Initial Developer of the Original Code is Bruno Lowagie. Portions created by + * the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie. + * All Rights Reserved. + * Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer + * are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved. + * + * Contributor(s): all the names of the contributors are added in the source code + * where applicable. + * + * Alternatively, the contents of this file may be used under the terms of the + * LGPL license (the ?GNU LIBRARY GENERAL PUBLIC LICENSE?), in which case the + * provisions of LGPL are applicable instead of those above. If you wish to + * allow use of your version of this file only under the terms of the LGPL + * License and not to allow others to use your version of this file under + * the MPL, indicate your decision by deleting the provisions above and + * replace them with the notice and other provisions required by the LGPL. + * If you do not delete the provisions above, a recipient may use your version + * of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE. + * + * This library is free software; you can redistribute it and/or modify it + * under the terms of the MPL as stated above or under the terms of the GNU + * Library General Public License as published by the Free Software Foundation; + * either version 2 of the License, or any later version. + * + * This library is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more + * details. + * + * If you didn't download this code from the following link, you should check if + * you aren't using an obsolete version: + * http://www.lowagie.com/iText/ + */ + +package com.lowagie.text.rtf.style; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.util.ArrayList; + +import com.lowagie.text.rtf.RtfElement; +import com.lowagie.text.rtf.RtfExtendedElement; +import com.lowagie.text.rtf.document.RtfDocument; + + +/** + * The RtfColorList stores all colours that appear in the document. Black + * and White are always added + * + * @version $Version:$ + * @author Mark Hall (mhall@edu.uni-klu.ac.at) + */ +public class RtfColorList extends RtfElement implements RtfExtendedElement { + + /** + * Constant for the beginning of the colour table + */ + private static final byte[] COLOR_TABLE = "\\colortbl".getBytes(); + + /** + * ArrayList containing all colours of this RtfColorList + */ + ArrayList colorList = new ArrayList(); + + /** + * Constructs a new RtfColorList for the RtfDocument. Will add the default + * black and white colours. + * + * @param doc The RtfDocument this RtfColorList belongs to + */ + public RtfColorList(RtfDocument doc) { + super(doc); + colorList.add(new RtfColor(doc, 0, 0, 0, 0)); + colorList.add(new RtfColor(doc, 255, 255, 255, 1)); + } + + /** + * Returns the index of the given RtfColor in the colour list. If the RtfColor + * is not in the list of colours, then it is added. + * + * @param color The RtfColor for which to get the index + * @return The index of the RtfColor + */ + public int getColorNumber(RtfColor color) { + int colorIndex = -1; + for(int i = 0; i < colorList.size(); i++) { + if(colorList.get(i).equals(color)) { + colorIndex = i; + } + } + if(colorIndex == -1) { + colorIndex = colorList.size(); + colorList.add(color); + } + return colorIndex; + } + + /** + * Write the definition part of the colour list. Calls the writeDefinition + * methods of the RtfColors in the colour list. + * + * @return A byte array with the definition colour list + */ + public byte[] writeDefinition() { + ByteArrayOutputStream result = new ByteArrayOutputStream(); + try { + result.write(OPEN_GROUP); + result.write(COLOR_TABLE); + for(int i = 0; i < colorList.size(); i++) { + RtfColor color = (RtfColor) colorList.get(i); + result.write(color.writeDefinition()); + } + result.write(CLOSE_GROUP); + result.write((byte)'\n'); + } catch(IOException ioe) { + ioe.printStackTrace(); + } + return result.toByteArray(); + } + +} diff --git a/src/main/java/com/lowagie/text/rtf/style/RtfFont.java b/src/main/java/com/lowagie/text/rtf/style/RtfFont.java new file mode 100644 index 0000000..150b0b2 --- /dev/null +++ b/src/main/java/com/lowagie/text/rtf/style/RtfFont.java @@ -0,0 +1,681 @@ +/* + * $Id: RtfFont.java,v 1.23 2006/02/09 17:11:31 hallm Exp $ + * $Name: $ + * + * Copyright 2001, 2002, 2003, 2004 by Mark Hall + * + * The contents of this file are subject to the Mozilla Public License Version 1.1 + * (the "License"); you may not use this file except in compliance with the License. + * You may obtain a copy of the License at http://www.mozilla.org/MPL/ + * + * Software distributed under the License is distributed on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License + * for the specific language governing rights and limitations under the License. + * + * The Original Code is 'iText, a free JAVA-PDF library'. + * + * The Initial Developer of the Original Code is Bruno Lowagie. Portions created by + * the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie. + * All Rights Reserved. + * Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer + * are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved. + * + * Contributor(s): all the names of the contributors are added in the source code + * where applicable. + * + * Alternatively, the contents of this file may be used under the terms of the + * LGPL license (the ?GNU LIBRARY GENERAL PUBLIC LICENSE?), in which case the + * provisions of LGPL are applicable instead of those above. If you wish to + * allow use of your version of this file only under the terms of the LGPL + * License and not to allow others to use your version of this file under + * the MPL, indicate your decision by deleting the provisions above and + * replace them with the notice and other provisions required by the LGPL. + * If you do not delete the provisions above, a recipient may use your version + * of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE. + * + * This library is free software; you can redistribute it and/or modify it + * under the terms of the MPL as stated above or under the terms of the GNU + * Library General Public License as published by the Free Software Foundation; + * either version 2 of the License, or any later version. + * + * This library is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more + * details. + * + * If you didn't download this code from the following link, you should check if + * you aren't using an obsolete version: + * http://www.lowagie.com/iText/ + */ + +package com.lowagie.text.rtf.style; + +import com.lowagie.text.rtf.RtfExtendedElement; +import com.lowagie.text.rtf.document.RtfDocument; +import com.lowagie.text.Font; + +import java.awt.Color; +import java.io.ByteArrayOutputStream; +import java.io.IOException; + +/** + * The RtfFont class stores one font for an rtf document. It extends Font, + * so can be set as a font, to allow adding of fonts with arbitrary names. + * BaseFont fontname handling contributed by Craig Fleming. Various fixes + * Renaud Michel, Werner Daehn. + * + * Version: $Id: RtfFont.java,v 1.23 2006/02/09 17:11:31 hallm Exp $ + * @author Mark Hall (mhall@edu.uni-klu.ac.at) + * @author Craig Fleming (rythos@rhana.dhs.org) + * @author Renaud Michel (r.michel@immedia.be) + * @author Werner Daehn (Werner.Daehn@BusinessObjects.com) + */ +public class RtfFont extends Font implements RtfExtendedElement { + /** + * Constant for the font family to use ("froman") + */ + private static final byte[] FONT_FAMILY = "\\froman".getBytes(); + /** + * Constant for the charset + */ + private static final byte[] FONT_CHARSET = "\\fcharset".getBytes(); + /** + * Constant for the font size + */ + public static final byte[] FONT_SIZE = "\\fs".getBytes(); + /** + * Constant for the bold flag + */ + private static final byte[] FONT_BOLD = "\\b".getBytes(); + /** + * Constant for the italic flag + */ + private static final byte[] FONT_ITALIC = "\\i".getBytes(); + /** + * Constant for the underline flag + */ + private static final byte[] FONT_UNDERLINE = "\\ul".getBytes(); + /** + * Constant for the strikethrough flag + */ + private static final byte[] FONT_STRIKETHROUGH = "\\strike".getBytes(); + /** + * Constant for the double strikethrough flag + */ + private static final byte[] FONT_DOUBLE_STRIKETHROUGH = "\\striked".getBytes(); + /** + * Constant for the shadow flag + */ + private static final byte[] FONT_SHADOW = "\\shad".getBytes(); + /** + * Constant for the outline flag + */ + private static final byte[] FONT_OUTLINE = "\\outl".getBytes(); + /** + * Constant for the embossed flag + */ + private static final byte[] FONT_EMBOSSED = "\\embo".getBytes(); + /** + * Constant for the engraved flag + */ + private static final byte[] FONT_ENGRAVED = "\\impr".getBytes(); + /** + * Constant for hidden text flag + */ + private static final byte[] FONT_HIDDEN = "\\v".getBytes(); + + /** + * Constant for a plain font + */ + public static final int STYLE_NONE = 0; + /** + * Constant for a bold font + */ + public static final int STYLE_BOLD = 1; + /** + * Constant for an italic font + */ + public static final int STYLE_ITALIC = 2; + /** + * Constant for an underlined font + */ + public static final int STYLE_UNDERLINE = 4; + /** + * Constant for a strikethrough font + */ + public static final int STYLE_STRIKETHROUGH = 8; + /** + * Constant for a double strikethrough font + */ + public static final int STYLE_DOUBLE_STRIKETHROUGH = 16; + /** + * Constant for a shadowed font + */ + public static final int STYLE_SHADOW = 32; + /** + * Constant for an outlined font + */ + public static final int STYLE_OUTLINE = 64; + /** + * Constant for an embossed font + */ + public static final int STYLE_EMBOSSED = 128; + /** + * Constant for an engraved font + */ + public static final int STYLE_ENGRAVED = 256; + /** + * Constant for a font that hides the actual text. + */ + public static final int STYLE_HIDDEN = 512; + + /** + * The font name. Defaults to "Times New Roman" + */ + private String fontName = "Times New Roman"; + /** + * The font size. Defaults to 10 + */ + private int fontSize = 10; + /** + * The font style. Defaults to STYLE_NONE + */ + private int fontStyle = STYLE_NONE; + /** + * The number of this font + */ + private int fontNumber = 0; + /** + * The colour of this font + */ + private RtfColor color = null; + /** + * The character set to use for this font + */ + private int charset = 0; + /** + * The RtfDocument this RtfFont belongs to. + */ + protected RtfDocument document = null; + + /** + * Constructs a RtfFont with the given font name and all other properties + * at their default values. + * + * @param fontName The font name to use + */ + public RtfFont(String fontName) { + super(Font.UNDEFINED, Font.UNDEFINED, Font.UNDEFINED, null); + this.fontName = fontName; + } + + /** + * Constructs a RtfFont with the given font name and font size and all other + * properties at their default values. + * + * @param fontName The font name to use + * @param size The font size to use + */ + public RtfFont(String fontName, float size) { + super(Font.UNDEFINED, size, Font.UNDEFINED, null); + this.fontName = fontName; + } + + /** + * Constructs a RtfFont with the given font name, font size and font style and the + * default color. + * + * @param fontName The font name to use + * @param size The font size to use + * @param style The font style to use + */ + public RtfFont(String fontName, float size, int style) { + super(Font.UNDEFINED, size, style, null); + this.fontName = fontName; + } + + /** + * Constructs a RtfFont with the given font name, font size, font style and + * color. + * + * @param fontName The font name to use + * @param size the font size to use + * @param style The font style to use + * @param color The font color to use + */ + public RtfFont(String fontName, float size, int style, Color color) { + super(Font.UNDEFINED, size, style, color); + this.fontName = fontName; + } + + /** + * Special constructor for the default font + * + * @param doc The RtfDocument this font appears in + * @param fontNumber The id of this font + */ + protected RtfFont(RtfDocument doc, int fontNumber) { + this.document = doc; + this.fontNumber = fontNumber; + color = new RtfColor(doc, 0, 0, 0); + } + + /** + * Constructs a RtfFont from a com.lowagie.text.Font + * @param doc The RtfDocument this font appears in + * @param font The Font to use as a base + */ + public RtfFont(RtfDocument doc, Font font) { + this.document = doc; + if(font != null) { + if(font instanceof RtfFont) { + this.fontName = ((RtfFont) font).getFontName(); + } else { + setToDefaultFamily(font.getFamilyname()); + } + if(font.getBaseFont() != null) { + String[][] fontNames = font.getBaseFont().getFullFontName(); + for(int i = 0; i < fontNames.length; i++) { + if(fontNames[i][2].equals("0")) { + this.fontName = fontNames[i][3]; + break; + } else if(fontNames[i][2].equals("1033") || fontNames[i][2].equals("")) { + this.fontName = fontNames[i][3]; + } + } + } + + setSize(font.size()); + setStyle(font.style()); + setColor(font.color()); + } + + if(this.fontName.equalsIgnoreCase("unknown")) { + return; + } + + if(document != null) { + setRtfDocument(document); + } + } + + /** + * Writes the font definition + * + * @return A byte array with the font definition + */ + public byte[] writeDefinition() { + ByteArrayOutputStream result = new ByteArrayOutputStream(); + try { + result.write(FONT_FAMILY); + result.write(FONT_CHARSET); + result.write(intToByteArray(charset)); + result.write(DELIMITER); + result.write(document.filterSpecialChar(fontName, true, false).getBytes()); + } catch(IOException ioe) { + ioe.printStackTrace(); + } + return result.toByteArray(); + } + + /** + * Writes the font beginning + * + * @return A byte array with the font start data + */ + public byte[] writeBegin() { + ByteArrayOutputStream result = new ByteArrayOutputStream(); + try { + if(this.fontNumber != Font.UNDEFINED) { + result.write(RtfFontList.FONT_NUMBER); + result.write(intToByteArray(fontNumber)); + } + if(this.fontSize != Font.UNDEFINED) { + result.write(FONT_SIZE); + result.write(intToByteArray(fontSize * 2)); + } + if(this.fontStyle != UNDEFINED) { + if((fontStyle & STYLE_BOLD) == STYLE_BOLD) { + result.write(FONT_BOLD); + } + if((fontStyle & STYLE_ITALIC) == STYLE_ITALIC) { + result.write(FONT_ITALIC); + } + if((fontStyle & STYLE_UNDERLINE) == STYLE_UNDERLINE) { + result.write(FONT_UNDERLINE); + } + if((fontStyle & STYLE_STRIKETHROUGH) == STYLE_STRIKETHROUGH) { + result.write(FONT_STRIKETHROUGH); + } + if((fontStyle & STYLE_HIDDEN) == STYLE_HIDDEN) { + result.write(FONT_HIDDEN); + } + if((fontStyle & STYLE_DOUBLE_STRIKETHROUGH) == STYLE_DOUBLE_STRIKETHROUGH) { + result.write(FONT_DOUBLE_STRIKETHROUGH); + result.write(intToByteArray(1)); + } + if((fontStyle & STYLE_SHADOW) == STYLE_SHADOW) { + result.write(FONT_SHADOW); + } + if((fontStyle & STYLE_OUTLINE) == STYLE_OUTLINE) { + result.write(FONT_OUTLINE); + } + if((fontStyle & STYLE_EMBOSSED) == STYLE_EMBOSSED) { + result.write(FONT_EMBOSSED); + } + if((fontStyle & STYLE_ENGRAVED) == STYLE_ENGRAVED) { + result.write(FONT_ENGRAVED); + } + } + if(color != null) { + result.write(color.writeBegin()); + } + } catch(IOException ioe) { + ioe.printStackTrace(); + } + return result.toByteArray(); + } + + /** + * Write the font end + * + * @return A byte array with the end of font data + */ + public byte[] writeEnd() { + ByteArrayOutputStream result = new ByteArrayOutputStream(); + try { + if(this.fontStyle != UNDEFINED) { + if((fontStyle & STYLE_BOLD) == STYLE_BOLD) { + result.write(FONT_BOLD); + result.write(intToByteArray(0)); + } + if((fontStyle & STYLE_ITALIC) == STYLE_ITALIC) { + result.write(FONT_ITALIC); + result.write(intToByteArray(0)); + } + if((fontStyle & STYLE_UNDERLINE) == STYLE_UNDERLINE) { + result.write(FONT_UNDERLINE); + result.write(intToByteArray(0)); + } + if((fontStyle & STYLE_STRIKETHROUGH) == STYLE_STRIKETHROUGH) { + result.write(FONT_STRIKETHROUGH); + result.write(intToByteArray(0)); + } + if((fontStyle & STYLE_HIDDEN) == STYLE_HIDDEN) { + result.write(FONT_HIDDEN); + result.write(intToByteArray(0)); + } + if((fontStyle & STYLE_DOUBLE_STRIKETHROUGH) == STYLE_DOUBLE_STRIKETHROUGH) { + result.write(FONT_DOUBLE_STRIKETHROUGH); + result.write(intToByteArray(0)); + } + if((fontStyle & STYLE_SHADOW) == STYLE_SHADOW) { + result.write(FONT_SHADOW); + result.write(intToByteArray(0)); + } + if((fontStyle & STYLE_OUTLINE) == STYLE_OUTLINE) { + result.write(FONT_OUTLINE); + result.write(intToByteArray(0)); + } + if((fontStyle & STYLE_EMBOSSED) == STYLE_EMBOSSED) { + result.write(FONT_EMBOSSED); + result.write(intToByteArray(0)); + } + if((fontStyle & STYLE_ENGRAVED) == STYLE_ENGRAVED) { + result.write(FONT_ENGRAVED); + result.write(intToByteArray(0)); + } + } + } catch(IOException ioe) { + ioe.printStackTrace(); + } + return result.toByteArray(); + } + + /** + * Unused + * @return an empty byte array + */ + public byte[] write() { + return new byte[0]; + } + + /** + * Tests for equality of RtfFonts. RtfFonts are equal if their fontName, + * fontSize, fontStyle and fontSuperSubscript are equal + * + * @param obj The RtfFont to compare with this RtfFont + * @return True if the RtfFonts are equal, false otherwise + */ + public boolean equals(Object obj) { + if(!(obj instanceof RtfFont)) { + return false; + } + RtfFont font = (RtfFont) obj; + boolean result = true; + result = result & this.fontName.equals(font.getFontName()); + + return result; + } + + /** + * Returns the hash code of this RtfFont. The hash code is the hash code of the + * string containing the font name + font size + "-" + the font style + "-" + the + * font super/supscript value. + * + * @return The hash code of this RtfFont + */ + public int hashCode() { + return (this.fontName + this.fontSize + "-" + this.fontStyle).hashCode(); + } + + /** + * Gets the font name of this RtfFont + * + * @return The font name + */ + public String getFontName() { + return this.fontName; + } + + /** + * Sets the font name of this RtfFont. + * + * @param fontName The font name to use + */ + protected void setFontName(String fontName) { + this.fontName = fontName; + if(document != null) { + this.fontNumber = document.getDocumentHeader().getFontNumber(this); + } + } + + /** + * @see com.lowagie.text.Font#getFamilyname() + */ + public String getFamilyname() { + return this.fontName; + } + + /** + * @see com.lowagie.text.Font#setFamily(String) + */ + public void setFamily(String family){ + super.setFamily(family); + setToDefaultFamily(family); + } + + /** + * Sets the correct font name from the family name. + * + * @param familyname The family name to set the name to. + */ + private void setToDefaultFamily(String familyname){ + switch (Font.getFamilyIndex(familyname)) { + case Font.COURIER: + this.fontName = "Courier"; + break; + case Font.HELVETICA: + this.fontName = "Arial"; + break; + case Font.SYMBOL: + this.fontName = "Symbol"; + this.charset = 2; + break; + case Font.TIMES_ROMAN: + this.fontName = "Times New Roman"; + break; + case Font.ZAPFDINGBATS: + this.fontName = "Windings"; + break; + default: + this.fontName = familyname; + } + } + + /** + * Gets the font size of this RtfFont + * + * @return The font size + */ + public int getFontSize() { + return this.fontSize; + } + + /** + * @see com.lowagie.text.Font#setSize(float) + */ + public void setSize(float size){ + super.setSize(size); + this.fontSize = (int) size(); + } + + /** + * Gets the font style of this RtfFont + * + * @return The font style + */ + public int getFontStyle() { + return this.fontStyle; + } + + /** + * @see com.lowagie.text.Font#setStyle(int) + */ + public void setStyle(int style){ + super.setStyle(style); + this.fontStyle = style(); + } + + /** + * @see com.lowagie.text.Font#setStyle(String) + */ + public void setStyle(String style) { + super.setStyle(style); + fontStyle = style(); + } + + /** + * Gets the font number of this RtfFont + * + * @return The font number + */ + public int getFontNumber() { + return fontNumber; + } + + /** + * Sets the RtfDocument this RtfFont belongs to + * + * @param doc The RtfDocument to use + */ + public void setRtfDocument(RtfDocument doc) { + this.document = doc; + if(document != null) { + this.fontNumber = document.getDocumentHeader().getFontNumber(this); + } + if(this.color != null) { + this.color.setRtfDocument(this.document); + } + } + + /** + * Unused + * @param inTable + */ + public void setInTable(boolean inTable) { + } + + /** + * Unused + * @param inHeader + */ + public void setInHeader(boolean inHeader) { + } + + /** + * @see com.lowagie.text.Font#setColor(Color) + */ + public void setColor(Color color) { + super.setColor(color); + if(color != null) { + this.color = new RtfColor(document, color); + } else { + this.color = null; + } + } + + /** + * @see com.lowagie.text.Font#setColor(int, int, int) + */ + public void setColor(int red, int green, int blue) { + super.setColor(red,green,blue); + this.color = new RtfColor(document, red, green, blue); + } + + /** + * Transforms an integer into its String representation and then returns the bytes + * of that string. + * + * @param i The integer to convert + * @return A byte array representing the integer + */ + protected byte[] intToByteArray(int i) { + return Integer.toString(i).getBytes(); + } + + /** + * Replaces the attributes that are equal to null with + * the attributes of a given font. + * + * @param font The surrounding font + * @return A RtfFont + */ + public Font difference(Font font) { + String dFamilyname = font.getFamilyname(); + if(dFamilyname == null || dFamilyname.trim().equals("") || dFamilyname.trim().equalsIgnoreCase("unknown")) { + dFamilyname = this.fontName; + } + + float dSize = font.size(); + if(dSize == Font.UNDEFINED) { + dSize = this.size(); + } + + int dStyle = Font.UNDEFINED; + if(this.style() != Font.UNDEFINED && font.style() != Font.UNDEFINED) { + dStyle = this.style() | font.style(); + } else if(this.style() != Font.UNDEFINED) { + dStyle = this.style(); + } else if(font.style() != Font.UNDEFINED) { + dStyle = font.style(); + } + + Color dColor = font.color(); + if(dColor == null) { + dColor = this.color(); + } + + return new RtfFont(dFamilyname, dSize, dStyle, dColor); + } +} diff --git a/src/main/java/com/lowagie/text/rtf/style/RtfFontList.java b/src/main/java/com/lowagie/text/rtf/style/RtfFontList.java new file mode 100644 index 0000000..6e90371 --- /dev/null +++ b/src/main/java/com/lowagie/text/rtf/style/RtfFontList.java @@ -0,0 +1,149 @@ +/* + * $Id: RtfFontList.java,v 1.17 2005/12/24 13:10:37 hallm Exp $ + * $Name: $ + * + * Copyright 2001, 2002, 2003, 2004 by Mark Hall + * + * The contents of this file are subject to the Mozilla Public License Version 1.1 + * (the "License"); you may not use this file except in compliance with the License. + * You may obtain a copy of the License at http://www.mozilla.org/MPL/ + * + * Software distributed under the License is distributed on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License + * for the specific language governing rights and limitations under the License. + * + * The Original Code is 'iText, a free JAVA-PDF library'. + * + * The Initial Developer of the Original Code is Bruno Lowagie. Portions created by + * the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie. + * All Rights Reserved. + * Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer + * are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved. + * + * Contributor(s): all the names of the contributors are added in the source code + * where applicable. + * + * Alternatively, the contents of this file may be used under the terms of the + * LGPL license (the ?GNU LIBRARY GENERAL PUBLIC LICENSE?), in which case the + * provisions of LGPL are applicable instead of those above. If you wish to + * allow use of your version of this file only under the terms of the LGPL + * License and not to allow others to use your version of this file under + * the MPL, indicate your decision by deleting the provisions above and + * replace them with the notice and other provisions required by the LGPL. + * If you do not delete the provisions above, a recipient may use your version + * of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE. + * + * This library is free software; you can redistribute it and/or modify it + * under the terms of the MPL as stated above or under the terms of the GNU + * Library General Public License as published by the Free Software Foundation; + * either version 2 of the License, or any later version. + * + * This library is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more + * details. + * + * If you didn't download this code from the following link, you should check if + * you aren't using an obsolete version: + * http://www.lowagie.com/iText/ + */ + +package com.lowagie.text.rtf.style; + +import com.lowagie.text.rtf.RtfElement; +import com.lowagie.text.rtf.RtfExtendedElement; +import com.lowagie.text.rtf.document.RtfDocument; + +import java.util.ArrayList; +import java.io.ByteArrayOutputStream; +import java.io.IOException; + +/** + * The RtfFontList stores the list of fonts used in the rtf document. It also + * has methods for writing this list to the document + * + * Version: $Id: RtfFontList.java,v 1.17 2005/12/24 13:10:37 hallm Exp $ + * @author Mark Hall (mhall@edu.uni-klu.ac.at) + */ +public class RtfFontList extends RtfElement implements RtfExtendedElement { + + /** + * Constant for the default font + */ + private static final byte[] DEFAULT_FONT = "\\deff".getBytes(); + /** + * Constant for the font table + */ + private static final byte[] FONT_TABLE = "\\fonttbl".getBytes(); + /** + * Constant for the font number + */ + public static final byte[] FONT_NUMBER = "\\f".getBytes(); + + /** + * The list of fonts + */ + private ArrayList fontList = new ArrayList(); + + /** + * Creates a RtfFontList + * + * @param doc The RtfDocument this RtfFontList belongs to + */ + public RtfFontList(RtfDocument doc) { + super(doc); + fontList.add(new RtfFont(document, 0)); + } + + /** + * Gets the index of the font in the list of fonts. If the font does not + * exist in the list, it is added. + * + * @param font The font to get the id for + * @return The index of the font + */ + public int getFontNumber(RtfFont font) { + if(font instanceof RtfParagraphStyle) { + font = new RtfFont(this.document, (RtfParagraphStyle) font); + } + int fontIndex = -1; + for(int i = 0; i < fontList.size(); i++) { + if(fontList.get(i).equals(font)) { + fontIndex = i; + } + } + if(fontIndex == -1) { + fontIndex = fontList.size(); + fontList.add(font); + } + return fontIndex; + } + + /** + * Writes the definition of the font list + * + * @return A byte array with the definition of the font list + */ + public byte[] writeDefinition() { + ByteArrayOutputStream result = new ByteArrayOutputStream(); + try { + result.write(DEFAULT_FONT); + result.write(intToByteArray(0)); + result.write(OPEN_GROUP); + result.write(FONT_TABLE); + for(int i = 0; i < fontList.size(); i++) { + result.write(OPEN_GROUP); + result.write(FONT_NUMBER); + result.write(intToByteArray(i)); + result.write(((RtfFont) fontList.get(i)).writeDefinition()); + result.write(COMMA_DELIMITER); + result.write(CLOSE_GROUP); + } + result.write(CLOSE_GROUP); + result.write((byte)'\n'); + } catch(IOException ioe) { + ioe.printStackTrace(); + } + return result.toByteArray(); + } +} diff --git a/src/main/java/com/lowagie/text/rtf/style/RtfParagraphStyle.java b/src/main/java/com/lowagie/text/rtf/style/RtfParagraphStyle.java new file mode 100644 index 0000000..d8c582c --- /dev/null +++ b/src/main/java/com/lowagie/text/rtf/style/RtfParagraphStyle.java @@ -0,0 +1,665 @@ +package com.lowagie.text.rtf.style; + +import java.awt.Color; +import java.io.ByteArrayOutputStream; +import java.io.IOException; + +import com.lowagie.text.Element; +import com.lowagie.text.Font; +import com.lowagie.text.rtf.RtfBasicElement; +import com.lowagie.text.rtf.document.RtfDocument; +import com.lowagie.text.rtf.text.RtfParagraph; + +/** + * The RtfParagraphStyle stores all style/formatting attributes of a RtfParagraph. + * Additionally it also supports the style name system available in RTF. The RtfParagraphStyle + * is a Font and can thus be used as such. To use the stylesheet functionality + * it needs to be set as the font of a Paragraph. Otherwise it will work like a + * RtfFont. It also supports inheritance of styles. + * + * @version $Revision: 1.4 $ + * @author Mark Hall (mhall@edu.uni-klu.ac.at) + */ +public class RtfParagraphStyle extends RtfFont { + + /** + * Constant for left alignment + */ + public static final byte[] ALIGN_LEFT = "\\ql".getBytes(); + /** + * Constant for right alignment + */ + public static final byte[] ALIGN_RIGHT = "\\qr".getBytes(); + /** + * Constant for center alignment + */ + public static final byte[] ALIGN_CENTER = "\\qc".getBytes(); + /** + * Constant for justified alignment + */ + public static final byte[] ALIGN_JUSTIFY = "\\qj".getBytes(); + /** + * Constant for left indentation + */ + public static final byte[] INDENT_LEFT = "\\li".getBytes(); + /** + * Constant for right indentation + */ + public static final byte[] INDENT_RIGHT = "\\ri".getBytes(); + /** + * Constant for keeping the paragraph together on one page + */ + public static final byte[] KEEP_TOGETHER = "\\keep".getBytes(); + /** + * Constant for keeping the paragraph toghether with the next one on one page + */ + public static final byte[] KEEP_TOGETHER_WITH_NEXT = "\\keepn".getBytes(); + /** + * Constant for the space after the paragraph. + */ + public static final byte[] SPACING_AFTER = "\\sa".getBytes(); + /** + * Constant for the space before the paragraph. + */ + public static final byte[] SPACING_BEFORE = "\\sb".getBytes(); + + /** + * The NORMAL/STANDARD style. + */ + public static final RtfParagraphStyle STYLE_NORMAL = new RtfParagraphStyle("Normal", "Arial", 12, Font.NORMAL, Color.black); + /** + * The style for level 1 headings. + */ + public static final RtfParagraphStyle STYLE_HEADING_1 = new RtfParagraphStyle("heading 1", "Normal"); + /** + * The style for level 2 headings. + */ + public static final RtfParagraphStyle STYLE_HEADING_2 = new RtfParagraphStyle("heading 2", "Normal"); + /** + * The style for level 3 headings. + */ + public static final RtfParagraphStyle STYLE_HEADING_3 = new RtfParagraphStyle("heading 3", "Normal"); + + /** + * Initialises the properties of the styles. + */ + static { + STYLE_HEADING_1.setSize(16); + STYLE_HEADING_1.setStyle(Font.BOLD); + STYLE_HEADING_2.setSize(14); + STYLE_HEADING_2.setStyle(Font.BOLDITALIC); + STYLE_HEADING_3.setSize(13); + STYLE_HEADING_3.setStyle(Font.BOLD); + } + + /** + * No modification has taken place when compared to the RtfParagraphStyle this RtfParagraphStyle + * is based on. These modification markers are used to determine what needs to be + * inherited and what not from the parent RtfParagraphStyle. + */ + private static final int MODIFIED_NONE = 0; + /** + * The alignment has been modified. + */ + private static final int MODIFIED_ALIGNMENT = 1; + /** + * The left indentation has been modified. + */ + private static final int MODIFIED_INDENT_LEFT = 2; + /** + * The right indentation has been modified. + */ + private static final int MODIFIED_INDENT_RIGHT = 4; + /** + * The spacing before a paragraph has been modified. + */ + private static final int MODIFIED_SPACING_BEFORE = 8; + /** + * The spacing after a paragraph has been modified. + */ + private static final int MODIFIED_SPACING_AFTER = 16; + /** + * The font name has been modified. + */ + private static final int MODIFIED_FONT_NAME = 32; + /** + * The font style has been modified. + */ + private static final int MODIFIED_FONT_SIZE = 64; + /** + * The font size has been modified. + */ + private static final int MODIFIED_FONT_STYLE = 128; + /** + * The font colour has been modified. + */ + private static final int MODIFIED_FONT_COLOR = 256; + /** + * The line leading has been modified. + */ + private static final int MODIFIED_LINE_LEADING = 512; + /** + * The paragraph keep together setting has been modified. + */ + private static final int MODIFIED_KEEP_TOGETHER = 1024; + /** + * The paragraph keep together with next setting has been modified. + */ + private static final int MODIFIED_KEEP_TOGETHER_WITH_NEXT = 2048; + + /** + * The alignment of the paragraph. + */ + private int alignment = Element.ALIGN_LEFT; + /** + * The left indentation of the paragraph. + */ + private int indentLeft = 0; + /** + * The right indentation of the paragraph. + */ + private int indentRight = 0; + /** + * The spacing before a paragraph. + */ + private int spacingBefore = 0; + /** + * The spacing after a paragraph. + */ + private int spacingAfter = 0; + /** + * The line leading of the paragraph. + */ + private int lineLeading = 0; + /** + * Whether this RtfParagraph must stay on one page. + */ + private boolean keepTogether = false; + /** + * Whether this RtfParagraph must stay on the same page as the next paragraph. + */ + private boolean keepTogetherWithNext = false; + /** + * The name of this RtfParagraphStyle. + */ + private String styleName = ""; + /** + * The name of the RtfParagraphStyle this RtfParagraphStyle is based on. + */ + private String basedOnName = null; + /** + * The RtfParagraphStyle this RtfParagraphStyle is based on. + */ + private RtfParagraphStyle baseStyle = null; + /** + * Which properties have been modified when compared to the base style. + */ + private int modified = MODIFIED_NONE; + /** + * The number of this RtfParagraphStyle in the stylesheet list. + */ + private int styleNumber = -1; + + /** + * Constructs a new RtfParagraphStyle with the given attributes. + * + * @param styleName The name of this RtfParagraphStyle. + * @param fontName The name of the font to use for this RtfParagraphStyle. + * @param fontSize The size of the font to use for this RtfParagraphStyle. + * @param fontStyle The style of the font to use for this RtfParagraphStyle. + * @param fontColor The colour of the font to use for this RtfParagraphStyle. + */ + public RtfParagraphStyle(String styleName, String fontName, int fontSize, int fontStyle, Color fontColor) { + super(null, new RtfFont(fontName, fontSize, fontStyle, fontColor)); + this.styleName = styleName; + } + + /** + * Constructs a new RtfParagraphStyle that is based on an existing RtfParagraphStyle. + * + * @param styleName The name of this RtfParagraphStyle. + * @param basedOnName The name of the RtfParagraphStyle this RtfParagraphStyle is based on. + */ + public RtfParagraphStyle(String styleName, String basedOnName) { + super(null, new Font()); + this.styleName = styleName; + this.basedOnName = basedOnName; + } + + /** + * Constructs a RtfParagraphStyle from another RtfParagraphStyle. + * + * INTERNAL USE ONLY + * + * @param doc The RtfDocument this RtfParagraphStyle belongs to. + * @param style The RtfParagraphStyle to copy settings from. + */ + public RtfParagraphStyle(RtfDocument doc, RtfParagraphStyle style) { + super(doc, style); + this.document = doc; + this.styleName = style.getStyleName(); + this.alignment = style.getAlignment(); + this.indentLeft = (int) (style.getIndentLeft() * RtfBasicElement.TWIPS_FACTOR); + this.indentRight = (int) (style.getIndentRight() * RtfBasicElement.TWIPS_FACTOR); + this.spacingBefore = (int) (style.getSpacingBefore() * RtfBasicElement.TWIPS_FACTOR); + this.spacingAfter = (int) (style.getSpacingAfter() * RtfBasicElement.TWIPS_FACTOR); + this.lineLeading = (int) (style.getLineLeading() * RtfBasicElement.TWIPS_FACTOR); + this.keepTogether = style.getKeepTogether(); + this.keepTogetherWithNext = style.getKeepTogetherWithNext(); + this.basedOnName = style.basedOnName; + this.modified = style.modified; + this.styleNumber = style.getStyleNumber(); + + if(this.document != null) { + setRtfDocument(this.document); + } + } + + /** + * Gets the name of this RtfParagraphStyle. + * + * @return The name of this RtfParagraphStyle. + */ + public String getStyleName() { + return this.styleName; + } + + /** + * Gets the name of the RtfParagraphStyle this RtfParagraphStyle is based on. + * + * @return The name of the base RtfParagraphStyle. + */ + public String getBasedOnName() { + return this.basedOnName; + } + + /** + * Gets the alignment of this RtfParagraphStyle. + * + * @return The alignment of this RtfParagraphStyle. + */ + public int getAlignment() { + return this.alignment; + } + + /** + * Sets the alignment of this RtfParagraphStyle. + * + * @param alignment The alignment to use. + */ + public void setAlignment(int alignment) { + this.modified = this.modified | MODIFIED_ALIGNMENT; + this.alignment = alignment; + } + + /** + * Gets the left indentation of this RtfParagraphStyle. + * + * @return The left indentation of this RtfParagraphStyle. + */ + public int getIndentLeft() { + return this.indentLeft; + } + + /** + * Sets the left indentation of this RtfParagraphStyle. + * + * @param indentLeft The left indentation to use. + */ + public void setIndentLeft(int indentLeft) { + this.modified = this.modified | MODIFIED_INDENT_LEFT; + this.indentLeft = indentLeft; + } + + /** + * Gets the right indentation of this RtfParagraphStyle. + * + * @return The right indentation of this RtfParagraphStyle. + */ + public int getIndentRight() { + return this.indentRight; + } + + /** + * Sets the right indentation of this RtfParagraphStyle. + * + * @param indentRight The right indentation to use. + */ + public void setIndentRight(int indentRight) { + this.modified = this.modified | MODIFIED_INDENT_RIGHT; + this.indentRight = indentRight; + } + + /** + * Gets the space before the paragraph of this RtfParagraphStyle.. + * + * @return The space before the paragraph. + */ + public int getSpacingBefore() { + return this.spacingBefore; + } + + /** + * Sets the space before the paragraph of this RtfParagraphStyle. + * + * @param spacingBefore The space before to use. + */ + public void setSpacingBefore(int spacingBefore) { + this.modified = this.modified | MODIFIED_SPACING_BEFORE; + this.spacingBefore = spacingBefore; + } + + /** + * Gets the space after the paragraph of this RtfParagraphStyle. + * + * @return The space after the paragraph. + */ + public int getSpacingAfter() { + return this.spacingAfter; + } + + /** + * Sets the space after the paragraph of this RtfParagraphStyle. + * + * @param spacingAfter The space after to use. + */ + public void setSpacingAfter(int spacingAfter) { + this.modified = this.modified | MODIFIED_SPACING_AFTER; + this.spacingAfter = spacingAfter; + } + + /** + * Sets the font name of this RtfParagraphStyle. + * + * @param fontName The font name to use + */ + public void setFontName(String fontName) { + this.modified = this.modified | MODIFIED_FONT_NAME; + super.setFontName(fontName); + } + + /** + * Sets the font size of this RtfParagraphStyle. + * + * @param fontSize The font size to use. + */ + public void setSize(float fontSize) { + this.modified = this.modified | MODIFIED_FONT_SIZE; + super.setSize(fontSize); + } + + /** + * Sets the font style of this RtfParagraphStyle. + * + * @param fontStyle The font style to use. + */ + public void setStyle(int fontStyle) { + this.modified = this.modified | MODIFIED_FONT_STYLE; + super.setStyle(fontStyle); + } + + /** + * Sets the colour of this RtfParagraphStyle. + * + * @param color The Color to use. + */ + public void setColor(Color color) { + this.modified = this.modified | MODIFIED_FONT_COLOR; + super.setColor(color); + } + + /** + * Gets the line leading of this RtfParagraphStyle. + * + * @return The line leading of this RtfParagraphStyle. + */ + public int getLineLeading() { + return this.lineLeading; + } + + /** + * Sets the line leading of this RtfParagraphStyle. + * + * @param lineLeading The line leading to use. + */ + public void setLineLeading(int lineLeading) { + this.lineLeading = lineLeading; + this.modified = this.modified | MODIFIED_LINE_LEADING; + } + + /** + * Gets whether the lines in the paragraph should be kept together in + * this RtfParagraphStyle. + * + * @return Whether the lines in the paragraph should be kept together. + */ + public boolean getKeepTogether() { + return this.keepTogether; + } + + /** + * Sets whether the lines in the paragraph should be kept together in + * this RtfParagraphStyle. + * + * @param keepTogether Whether the lines in the paragraph should be kept together. + */ + public void setKeepTogether(boolean keepTogether) { + this.keepTogether = keepTogether; + this.modified = this.modified | MODIFIED_KEEP_TOGETHER; + } + + /** + * Gets whether the paragraph should be kept toggether with the next in + * this RtfParagraphStyle. + * + * @return Whether the paragraph should be kept together with the next. + */ + public boolean getKeepTogetherWithNext() { + return this.keepTogetherWithNext; + } + + /** + * Sets whether the paragraph should be kept together with the next in + * this RtfParagraphStyle. + * + * @param keepTogetherWithNext Whether the paragraph should be kept together with the next. + */ + public void setKeepTogetherWithNext(boolean keepTogetherWithNext) { + this.keepTogetherWithNext = keepTogetherWithNext; + this.modified = this.modified | MODIFIED_KEEP_TOGETHER_WITH_NEXT; + } + + /** + * Handles the inheritance of paragraph style settings. All settings that + * have not been modified will be inherited from the base RtfParagraphStyle. + * If this RtfParagraphStyle is not based on another one, then nothing happens. + */ + public void handleInheritance() { + if(this.basedOnName != null && this.document.getDocumentHeader().getRtfParagraphStyle(this.basedOnName) != null) { + this.baseStyle = this.document.getDocumentHeader().getRtfParagraphStyle(this.basedOnName); + this.baseStyle.handleInheritance(); + if(!((this.modified & MODIFIED_ALIGNMENT) == MODIFIED_ALIGNMENT)) { + this.alignment = this.baseStyle.getAlignment(); + } + if(!((this.modified & MODIFIED_INDENT_LEFT) == MODIFIED_INDENT_LEFT)) { + this.indentLeft = this.baseStyle.getIndentLeft(); + } + if(!((this.modified & MODIFIED_INDENT_RIGHT) == MODIFIED_INDENT_RIGHT)) { + this.indentRight = this.baseStyle.getIndentRight(); + } + if(!((this.modified & MODIFIED_SPACING_BEFORE) == MODIFIED_SPACING_BEFORE)) { + this.spacingBefore = this.baseStyle.getSpacingBefore(); + } + if(!((this.modified & MODIFIED_SPACING_AFTER) == MODIFIED_SPACING_AFTER)) { + this.spacingAfter = this.baseStyle.getSpacingAfter(); + } + if(!((this.modified & MODIFIED_FONT_NAME) == MODIFIED_FONT_NAME)) { + setFontName(this.baseStyle.getFontName()); + } + if(!((this.modified & MODIFIED_FONT_SIZE) == MODIFIED_FONT_SIZE)) { + setSize(this.baseStyle.getFontSize()); + } + if(!((this.modified & MODIFIED_FONT_STYLE) == MODIFIED_FONT_STYLE)) { + setStyle(this.baseStyle.getFontStyle()); + } + if(!((this.modified & MODIFIED_FONT_COLOR) == MODIFIED_FONT_COLOR)) { + setColor(this.baseStyle.color()); + } + if(!((this.modified & MODIFIED_LINE_LEADING) == MODIFIED_LINE_LEADING)) { + setLineLeading(this.baseStyle.getLineLeading()); + } + if(!((this.modified & MODIFIED_KEEP_TOGETHER) == MODIFIED_KEEP_TOGETHER)) { + setKeepTogether(this.baseStyle.getKeepTogether()); + } + if(!((this.modified & MODIFIED_KEEP_TOGETHER_WITH_NEXT) == MODIFIED_KEEP_TOGETHER_WITH_NEXT)) { + setKeepTogetherWithNext(this.baseStyle.getKeepTogetherWithNext()); + } + } + } + + /** + * Writes the settings of this RtfParagraphStyle. + * + * @return A byte array with the settings of this RtfParagraphStyle. + */ + private byte[] writeParagraphSettings() { + ByteArrayOutputStream result = new ByteArrayOutputStream(); + try { + if(this.keepTogether) { + result.write(RtfParagraphStyle.KEEP_TOGETHER); + } + if(this.keepTogetherWithNext) { + result.write(RtfParagraphStyle.KEEP_TOGETHER_WITH_NEXT); + } + switch (alignment) { + case Element.ALIGN_LEFT: + result.write(RtfParagraphStyle.ALIGN_LEFT); + break; + case Element.ALIGN_RIGHT: + result.write(RtfParagraphStyle.ALIGN_RIGHT); + break; + case Element.ALIGN_CENTER: + result.write(RtfParagraphStyle.ALIGN_CENTER); + break; + case Element.ALIGN_JUSTIFIED: + case Element.ALIGN_JUSTIFIED_ALL: + result.write(RtfParagraphStyle.ALIGN_JUSTIFY); + break; + } + result.write(RtfParagraphStyle.INDENT_LEFT); + result.write(intToByteArray(indentLeft)); + result.write(RtfParagraphStyle.INDENT_RIGHT); + result.write(intToByteArray(indentRight)); + if(this.spacingBefore > 0) { + result.write(RtfParagraphStyle.SPACING_BEFORE); + result.write(intToByteArray(this.spacingBefore)); + } + if(this.spacingAfter > 0) { + result.write(RtfParagraphStyle.SPACING_AFTER); + result.write(intToByteArray(this.spacingAfter)); + } + if(this.lineLeading > 0) { + result.write(RtfParagraph.LINE_SPACING); + result.write(intToByteArray(this.lineLeading)); + } + } catch(IOException ioe) { + ioe.printStackTrace(); + } + return result.toByteArray(); + } + + /** + * Writes the definition of this RtfParagraphStyle for the stylesheet list. + */ + public byte[] writeDefinition() { + ByteArrayOutputStream result = new ByteArrayOutputStream(); + try { + result.write("{".getBytes()); + result.write("\\style".getBytes()); + result.write("\\s".getBytes()); + result.write(intToByteArray(this.styleNumber)); + result.write(RtfBasicElement.DELIMITER); + result.write(writeParagraphSettings()); + result.write(super.writeBegin()); + result.write(RtfBasicElement.DELIMITER); + result.write(this.styleName.getBytes()); + result.write(";".getBytes()); + result.write("}".getBytes()); + if(this.document.getDocumentSettings().isOutputDebugLineBreaks()) { + result.write('\n'); + } + } catch(IOException ioe) { + ioe.printStackTrace(); + } + return result.toByteArray(); + } + + /** + * Writes the start information of this RtfParagraphStyle. + */ + public byte[] writeBegin() { + ByteArrayOutputStream result = new ByteArrayOutputStream(); + try { + result.write("\\s".getBytes()); + result.write(intToByteArray(this.styleNumber)); + result.write(writeParagraphSettings()); + } catch(IOException ioe) { + ioe.printStackTrace(); + } + return result.toByteArray(); + } + + /** + * Unused + * @return An empty byte array. + */ + public byte[] writeEnd() { + return new byte[0]; + } + + /** + * Unused + * @return An empty byte array. + */ + public byte[] write() { + return new byte[0]; + } + + /** + * Tests whether two RtfParagraphStyles are equal. Equality + * is determined via the name. + */ + public boolean equals(Object o) { + if(o == null || !(o instanceof RtfParagraphStyle)) { + return false; + } + RtfParagraphStyle paragraphStyle = (RtfParagraphStyle) o; + boolean result = this.getStyleName().equals(paragraphStyle.getStyleName()); + return result; + } + + /** + * Gets the hash code of this RtfParagraphStyle. + */ + public int hashCode() { + return this.styleName.hashCode(); + } + + /** + * Gets the number of this RtfParagraphStyle in the stylesheet list. + * + * @return The number of this RtfParagraphStyle in the stylesheet list. + */ + private int getStyleNumber() { + return this.styleNumber; + } + + /** + * Sets the number of this RtfParagraphStyle in the stylesheet list. + * + * @param styleNumber The number to use. + */ + protected void setStyleNumber(int styleNumber) { + this.styleNumber = styleNumber; + } +} diff --git a/src/main/java/com/lowagie/text/rtf/style/RtfStylesheetList.java b/src/main/java/com/lowagie/text/rtf/style/RtfStylesheetList.java new file mode 100644 index 0000000..edb04ea --- /dev/null +++ b/src/main/java/com/lowagie/text/rtf/style/RtfStylesheetList.java @@ -0,0 +1,121 @@ +/* + * Created on Sep 22, 2005 + * + * To change the template for this generated file go to + * Window - Preferences - Java - Code Generation - Code and Comments + */ +package com.lowagie.text.rtf.style; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.util.HashMap; +import java.util.Iterator; + +import com.lowagie.text.rtf.RtfBasicElement; +import com.lowagie.text.rtf.RtfElement; +import com.lowagie.text.rtf.RtfExtendedElement; +import com.lowagie.text.rtf.document.RtfDocument; + +/** + * The RtfStylesheetList stores the RtfParagraphStyles that are used in the document. + * + * @version $Revision: 1.1 $ + * @author Mark Hall (mhall@edu.uni-klu.ac.at) + */ +public class RtfStylesheetList extends RtfElement implements RtfExtendedElement { + + /** + * The HashMap containing the RtfParagraphStyles. + */ + private HashMap styleMap = null; + /** + * Whether the default settings have been loaded. + */ + private boolean defaultsLoaded = false; + + /** + * Constructs a new RtfStylesheetList for the RtfDocument. + * + * @param doc The RtfDocument this RtfStylesheetList belongs to. + */ + public RtfStylesheetList(RtfDocument doc) { + super(doc); + this.styleMap = new HashMap(); + } + + /** + * Register a RtfParagraphStyle with this RtfStylesheetList. + * + * @param rtfParagraphStyle The RtfParagraphStyle to add. + */ + public void registerParagraphStyle(RtfParagraphStyle rtfParagraphStyle) { + RtfParagraphStyle tempStyle = new RtfParagraphStyle(this.document, rtfParagraphStyle); + tempStyle.setStyleNumber(this.styleMap.size()); + tempStyle.handleInheritance(); + this.styleMap.put(tempStyle.getStyleName(), tempStyle); + } + + /** + * Registers all default styles. If styles with the given name have already been registered, + * then they are NOT overwritten. + */ + private void registerDefaultStyles() { + defaultsLoaded = true; + if(!this.styleMap.containsKey(RtfParagraphStyle.STYLE_NORMAL.getStyleName())) { + registerParagraphStyle(RtfParagraphStyle.STYLE_NORMAL); + } + if(!this.styleMap.containsKey(RtfParagraphStyle.STYLE_HEADING_1.getStyleName())) { + registerParagraphStyle(RtfParagraphStyle.STYLE_HEADING_1); + } + if(!this.styleMap.containsKey(RtfParagraphStyle.STYLE_HEADING_2.getStyleName())) { + registerParagraphStyle(RtfParagraphStyle.STYLE_HEADING_2); + } + if(!this.styleMap.containsKey(RtfParagraphStyle.STYLE_HEADING_3.getStyleName())) { + registerParagraphStyle(RtfParagraphStyle.STYLE_HEADING_3); + } + } + + /** + * Gets the RtfParagraphStyle with the given name. Makes sure that the defaults + * have been loaded. + * + * @param styleName The name of the RtfParagraphStyle to get. + * @return The RtfParagraphStyle with the given name or null. + */ + public RtfParagraphStyle getRtfParagraphStyle(String styleName) { + if(!defaultsLoaded) { + registerDefaultStyles(); + } + if(this.styleMap.containsKey(styleName)) { + return (RtfParagraphStyle) this.styleMap.get(styleName); + } else { + return null; + } + } + + /** + * Writes the definition of the stylesheet list. + */ + public byte[] writeDefinition() { + ByteArrayOutputStream result = new ByteArrayOutputStream(); + try { + result.write("{".getBytes()); + result.write("\\stylesheet".getBytes()); + result.write(RtfBasicElement.DELIMITER); + if(this.document.getDocumentSettings().isOutputDebugLineBreaks()) { + result.write("\n".getBytes()); + } + Iterator it = this.styleMap.values().iterator(); + while(it.hasNext()) { + result.write(((RtfParagraphStyle) it.next()).writeDefinition()); + } + result.write("}".getBytes()); + if(this.document.getDocumentSettings().isOutputDebugLineBreaks()) { + result.write('\n'); + } + } catch(IOException ioe) { + ioe.printStackTrace(); + } + return result.toByteArray(); + } +} -- cgit v1.2.3