📄 rtflist.java
字号:
/*
* $Id: RtfList.java 2825 2007-06-04 09:15:21Z blowagie $
* $Name$
*
* Copyright 2001, 2002, 2003, 2004, 2005 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.list;
import java.awt.Color;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import com.lowagie.text.Chunk;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Element;
import com.lowagie.text.Font;
import com.lowagie.text.List;
import com.lowagie.text.ListItem;
import com.lowagie.text.RomanList;
import com.lowagie.text.factories.RomanAlphabetFactory;
import com.lowagie.text.factories.RomanNumberFactory;
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;
import com.lowagie.text.rtf.style.RtfFont;
import com.lowagie.text.rtf.style.RtfFontList;
import com.lowagie.text.rtf.style.RtfParagraphStyle;
import com.lowagie.text.rtf.text.RtfParagraph;
/**
* The RtfList stores one List. It also provides the methods to write the
* list declaration and the list data.
*
* @version $Id: RtfList.java 2825 2007-06-04 09:15:21Z blowagie $
* @author Mark Hall (mhall@edu.uni-klu.ac.at)
* @author Thomas Bickel (tmb99@inode.at)
* @author Felix Satyaputra (f_satyaputra@yahoo.co.uk)
*/
public class RtfList extends RtfElement implements RtfExtendedElement {
/**
* Constant for list level
*/
private static final byte[] LIST_LEVEL = "\\listlevel".getBytes();
/**
* Constant for list level style old
*/
private static final byte[] LIST_LEVEL_TYPE = "\\levelnfc".getBytes();
/**
* Constant for list level style new
*/
private static final byte[] LIST_LEVEL_TYPE_NEW = "\\levelnfcn".getBytes();
/**
* Constant for list level alignment old
*/
private static final byte[] LIST_LEVEL_ALIGNMENT = "\\leveljc".getBytes();
/**
* Constant for list level alignment new
*/
private static final byte[] LIST_LEVEL_ALIGNMENT_NEW = "\\leveljcn".getBytes();
/**
* Constant for list level start at
*/
private static final byte[] LIST_LEVEL_START_AT = "\\levelstartat".getBytes();
/**
* Constant for list level text
*/
private static final byte[] LIST_LEVEL_TEXT = "\\leveltext".getBytes();
/**
* Constant for the beginning of the list level numbered style
*/
private static final byte[] LIST_LEVEL_STYLE_NUMBERED_BEGIN = "\\\'02\\\'".getBytes();
/**
* Constant for the end of the list level numbered style
*/
private static final byte[] LIST_LEVEL_STYLE_NUMBERED_END = ".;".getBytes();
/**
* Constant for the beginning of the list level bulleted style
*/
private static final byte[] LIST_LEVEL_STYLE_BULLETED_BEGIN = "\\\'01".getBytes();
/**
* Constant for the end of the list level bulleted style
*/
private static final byte[] LIST_LEVEL_STYLE_BULLETED_END = ";".getBytes();
/**
* Constant for the beginning of the list level numbers
*/
private static final byte[] LIST_LEVEL_NUMBERS_BEGIN = "\\levelnumbers".getBytes();
/**
* Constant for the list level numbers
*/
private static final byte[] LIST_LEVEL_NUMBERS_NUMBERED = "\\\'01".getBytes();
/**
* Constant for the end of the list level numbers
*/
private static final byte[] LIST_LEVEL_NUMBERS_END = ";".getBytes();
/**
* Constant for the first indentation
*/
private static final byte[] LIST_LEVEL_FIRST_INDENT = "\\fi".getBytes();
/**
* Constant for the symbol indentation
*/
private static final byte[] LIST_LEVEL_SYMBOL_INDENT = "\\tx".getBytes();
/**
* Constant for the list level value
*/
private static final byte[] LIST_LEVEL_NUMBER = "\\ilvl".getBytes();
/**
* Constant for a tab character
*/
private static final byte[] TAB = "\\tab".getBytes();
/**
* Constant for the old list text
*/
private static final byte[] LIST_TEXT = "\\listtext".getBytes();
/**
* Constant for the old list number end
*/
private static final byte[] LIST_NUMBER_END = ".".getBytes();
/**
* Constant for the old bulleted list
*/
private static final byte[] LIST_BULLET = "\\\'b7".getBytes();
private static final int LIST_TYPE_BULLET = 0;
private static final int LIST_TYPE_NUMBERED = 1;
private static final int LIST_TYPE_UPPER_LETTERS = 2;
private static final int LIST_TYPE_LOWER_LETTERS = 3;
private static final int LIST_TYPE_UPPER_ROMAN = 4;
private static final int LIST_TYPE_LOWER_ROMAN = 5;
/**
* The subitems of this RtfList
*/
private ArrayList items;
/**
* The level of this RtfList
*/
private int listLevel = 0;
/**
* The first indentation of this RtfList
*/
private int firstIndent = 0;
/**
* The left indentation of this RtfList
*/
private int leftIndent = 0;
/**
* The right indentation of this RtfList
*/
private int rightIndent = 0;
/**
* The symbol indentation of this RtfList
*/
private int symbolIndent = 0;
/**
* The list number of this RtfList
*/
private int listNumber = 1;
/**
* Whether this RtfList is numbered
*/
private int listType = LIST_TYPE_BULLET;
/**
* The RtfFont for numbered lists
*/
private RtfFont fontNumber;
/**
* The RtfFont for bulleted lists
*/
private RtfFont fontBullet;
/**
* The alignment of this RtfList
*/
private int alignment = Element.ALIGN_LEFT;
/**
* The parent List in multi-level lists.
*/
private RtfList parentList = null;
/**
* The text to use as the bullet character
*/
private String bulletCharacter = "\u00b7";
/**
* Constructs a new RtfList for the specified List.
*
* @param doc The RtfDocument this RtfList belongs to
* @param list The List this RtfList is based on
*/
public RtfList(RtfDocument doc, List list) {
super(doc);
this.listNumber = document.getDocumentHeader().getListNumber(this);
this.items = new ArrayList();
if(list.getSymbolIndent() > 0 && list.getIndentationLeft() > 0) {
this.firstIndent = (int) (list.getSymbolIndent() * RtfElement.TWIPS_FACTOR * -1);
this.leftIndent = (int) ((list.getIndentationLeft() + list.getSymbolIndent()) * RtfElement.TWIPS_FACTOR);
} else if(list.getSymbolIndent() > 0) {
this.firstIndent = (int) (list.getSymbolIndent() * RtfElement.TWIPS_FACTOR * -1);
this.leftIndent = (int) (list.getSymbolIndent() * RtfElement.TWIPS_FACTOR);
} else if(list.getIndentationLeft() > 0) {
this.firstIndent = 0;
this.leftIndent = (int) (list.getIndentationLeft() * RtfElement.TWIPS_FACTOR);
} else {
this.firstIndent = 0;
this.leftIndent = 0;
}
this.rightIndent = (int) (list.getIndentationRight() * RtfElement.TWIPS_FACTOR);
this.symbolIndent = (int) ((list.getSymbolIndent() + list.getIndentationLeft()) * RtfElement.TWIPS_FACTOR);
if(list instanceof RomanList) {
if(list.isLowercase()) {
this.listType = LIST_TYPE_LOWER_ROMAN;
} else {
this.listType = LIST_TYPE_UPPER_ROMAN;
}
} else if(list.isNumbered()) {
this.listType = LIST_TYPE_NUMBERED;
} else if(list.isLettered()) {
if(list.isLowercase()) {
this.listType = LIST_TYPE_LOWER_LETTERS;
} else {
this.listType = LIST_TYPE_UPPER_LETTERS;
}
}
for(int i = 0; i < list.getItems().size(); i++) {
try {
Element element = (Element) list.getItems().get(i);
if(element.type() == Element.CHUNK) {
element = new ListItem((Chunk) element);
}
if(element instanceof ListItem) {
this.alignment = ((ListItem) element).getAlignment();
}
RtfBasicElement rtfElement = doc.getMapper().mapElement(element);
if(rtfElement instanceof RtfList) {
((RtfList) rtfElement).setListNumber(listNumber);
((RtfList) rtfElement).setListLevel(listLevel + 1);
((RtfList) rtfElement).setParent(this);
} else if(rtfElement instanceof RtfListItem) {
((RtfListItem) rtfElement).setParent(this);
((RtfListItem) rtfElement).inheritListSettings(listNumber, listLevel + 1);
}
items.add(rtfElement);
} catch(DocumentException de) {
de.printStackTrace();
}
}
if(this.listLevel == 0) {
correctIndentation();
}
fontNumber = new RtfFont(document, new Font(Font.TIMES_ROMAN, 10, Font.NORMAL, new Color(0, 0, 0)));
if (list.getSymbol() != null && list.getSymbol().getFont() != null && !list.getSymbol().getContent().startsWith("-") && list.getSymbol().getContent().length() > 0) {
// only set this to bullet symbol is not default
this.fontBullet = new RtfFont(document, list.getSymbol().getFont());
this.bulletCharacter = list.getSymbol().getContent().substring(0, 1);
} else {
this.fontBullet = new RtfFont(document, new Font(Font.SYMBOL, 10, Font.NORMAL, new Color(0, 0, 0)));
}
}
/**
* @deprecated
* @return a byte array
*/
private byte[] writeIndentations() {
ByteArrayOutputStream result = new ByteArrayOutputStream();
try {
writeIndentations(result);
} catch(IOException ioe) {
ioe.printStackTrace();
}
return result.toByteArray();
}
private void writeIndentations(final OutputStream result) throws IOException
{
result.write(LIST_LEVEL_FIRST_INDENT);
result.write(intToByteArray(firstIndent));
result.write(RtfParagraphStyle.INDENT_LEFT);
result.write(intToByteArray(leftIndent));
result.write(RtfParagraphStyle.INDENT_RIGHT);
result.write(intToByteArray(rightIndent));
}
/**
* Writes the definition part of this list level
*
* @return A byte array containing the definition of this list level
* @deprecated replaced by {@link #writeDefinition(OutputStream)}
*/
public byte[] writeDefinition()
{
ByteArrayOutputStream result = new ByteArrayOutputStream();
try {
writeDefinition(result);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -