📄 columntext.java
字号:
/* * $Id: ColumnText.java 3373 2008-05-12 16:21:24Z xlv $ * * Copyright 2001, 2002 by Paulo Soares. * * 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.pdf;import java.util.ArrayList;import java.util.Iterator;import java.util.LinkedList;import java.util.Stack;import com.lowagie.text.Chunk;import com.lowagie.text.DocumentException;import com.lowagie.text.Element;import com.lowagie.text.ExceptionConverter;import com.lowagie.text.Image;import com.lowagie.text.ListItem;import com.lowagie.text.Paragraph;import com.lowagie.text.Phrase;import com.lowagie.text.SimpleTable;import com.lowagie.text.pdf.draw.DrawInterface;/** * Formats text in a columnwise form. The text is bound * on the left and on the right by a sequence of lines. This allows the column * to have any shape, not only rectangular. * <P> * Several parameters can be set like the first paragraph line indent and * extra space between paragraphs. * <P> * A call to the method <CODE>go</CODE> will return one of the following * situations: the column ended or the text ended. * <P> * I the column ended, a new column definition can be loaded with the method * <CODE>setColumns</CODE> and the method <CODE>go</CODE> can be called again. * <P> * If the text ended, more text can be loaded with <CODE>addText</CODE> * and the method <CODE>go</CODE> can be called again.<BR> * The only limitation is that one or more complete paragraphs must be loaded * each time. * <P> * Full bidirectional reordering is supported. If the run direction is * <CODE>PdfWriter.RUN_DIRECTION_RTL</CODE> the meaning of the horizontal * alignments and margins is mirrored. * @author Paulo Soares (psoares@consiste.pt) */public class ColumnText { /** Eliminate the arabic vowels */ public static final int AR_NOVOWEL = ArabicLigaturizer.ar_novowel; /** Compose the tashkeel in the ligatures. */ public static final int AR_COMPOSEDTASHKEEL = ArabicLigaturizer.ar_composedtashkeel; /** Do some extra double ligatures. */ public static final int AR_LIG = ArabicLigaturizer.ar_lig; /** * Digit shaping option: Replace European digits (U+0030...U+0039) by Arabic-Indic digits. */ public static final int DIGITS_EN2AN = ArabicLigaturizer.DIGITS_EN2AN; /** * Digit shaping option: Replace Arabic-Indic digits by European digits (U+0030...U+0039). */ public static final int DIGITS_AN2EN = ArabicLigaturizer.DIGITS_AN2EN; /** * Digit shaping option: * Replace European digits (U+0030...U+0039) by Arabic-Indic digits * if the most recent strongly directional character * is an Arabic letter (its Bidi direction value is RIGHT_TO_LEFT_ARABIC). * The initial state at the start of the text is assumed to be not an Arabic, * letter, so European digits at the start of the text will not change. * Compare to DIGITS_ALEN2AN_INIT_AL. */ public static final int DIGITS_EN2AN_INIT_LR = ArabicLigaturizer.DIGITS_EN2AN_INIT_LR; /** * Digit shaping option: * Replace European digits (U+0030...U+0039) by Arabic-Indic digits * if the most recent strongly directional character * is an Arabic letter (its Bidi direction value is RIGHT_TO_LEFT_ARABIC). * The initial state at the start of the text is assumed to be an Arabic, * letter, so European digits at the start of the text will change. * Compare to DIGITS_ALEN2AN_INT_LR. */ public static final int DIGITS_EN2AN_INIT_AL = ArabicLigaturizer.DIGITS_EN2AN_INIT_AL; /** * Digit type option: Use Arabic-Indic digits (U+0660...U+0669). */ public static final int DIGIT_TYPE_AN = ArabicLigaturizer.DIGIT_TYPE_AN; /** * Digit type option: Use Eastern (Extended) Arabic-Indic digits (U+06f0...U+06f9). */ public static final int DIGIT_TYPE_AN_EXTENDED = ArabicLigaturizer.DIGIT_TYPE_AN_EXTENDED; protected int runDirection = PdfWriter.RUN_DIRECTION_DEFAULT; /** the space char ratio */ public static final float GLOBAL_SPACE_CHAR_RATIO = 0; /** Initial value of the status. */ public static final int START_COLUMN = 0; /** Signals that there is no more text available. */ public static final int NO_MORE_TEXT = 1; /** Signals that there is no more column. */ public static final int NO_MORE_COLUMN = 2; /** The column is valid. */ protected static final int LINE_STATUS_OK = 0; /** The line is out the column limits. */ protected static final int LINE_STATUS_OFFLIMITS = 1; /** The line cannot fit this column position. */ protected static final int LINE_STATUS_NOLINE = 2; /** Upper bound of the column. */ protected float maxY; /** Lower bound of the column. */ protected float minY; protected float leftX; protected float rightX; /** The column alignment. Default is left alignment. */ protected int alignment = Element.ALIGN_LEFT; /** The left column bound. */ protected ArrayList leftWall; /** The right column bound. */ protected ArrayList rightWall; /** The chunks that form the text. */// protected ArrayList chunks = new ArrayList(); protected BidiLine bidiLine; /** The current y line location. Text will be written at this line minus the leading. */ protected float yLine; /** The leading for the current line. */ protected float currentLeading = 16; /** The fixed text leading. */ protected float fixedLeading = 16; /** The text leading that is multiplied by the biggest font size in the line. */ protected float multipliedLeading = 0; /** The <CODE>PdfContent</CODE> where the text will be written to. */ protected PdfContentByte canvas; protected PdfContentByte[] canvases; /** The line status when trying to fit a line to a column. */ protected int lineStatus; /** The first paragraph line indent. */ protected float indent = 0; /** The following paragraph lines indent. */ protected float followingIndent = 0; /** The right paragraph lines indent. */ protected float rightIndent = 0; /** The extra space between paragraphs. */ protected float extraParagraphSpace = 0; /** The width of the line when the column is defined as a simple rectangle. */ protected float rectangularWidth = -1; protected boolean rectangularMode = false; /** Holds value of property spaceCharRatio. */ private float spaceCharRatio = GLOBAL_SPACE_CHAR_RATIO; private boolean lastWasNewline = true; /** Holds value of property linesWritten. */ private int linesWritten; private float firstLineY; private boolean firstLineYDone = false; /** Holds value of property arabicOptions. */ private int arabicOptions = 0; protected float descender; protected boolean composite = false; protected ColumnText compositeColumn; protected LinkedList compositeElements; protected int listIdx = 0; private boolean splittedRow; protected Phrase waitPhrase; /** if true, first line height is adjusted so that the max ascender touches the top */ private boolean useAscender = false; /** * Creates a <CODE>ColumnText</CODE>. * @param canvas the place where the text will be written to. Can * be a template. */ public ColumnText(PdfContentByte canvas) { this.canvas = canvas; } /** Creates an independent duplicated of the instance <CODE>org</CODE>. * @param org the original <CODE>ColumnText</CODE> * @return the duplicated */ public static ColumnText duplicate(ColumnText org) { ColumnText ct = new ColumnText(null); ct.setACopy(org); return ct; } /** Makes this instance an independent copy of <CODE>org</CODE>. * @param org the original <CODE>ColumnText</CODE> * @return itself */ public ColumnText setACopy(ColumnText org) { setSimpleVars(org); if (org.bidiLine != null) bidiLine = new BidiLine(org.bidiLine); return this; } protected void setSimpleVars(ColumnText org) { maxY = org.maxY; minY = org.minY; alignment = org.alignment; leftWall = null; if (org.leftWall != null) leftWall = new ArrayList(org.leftWall); rightWall = null; if (org.rightWall != null) rightWall = new ArrayList(org.rightWall); yLine = org.yLine; currentLeading = org.currentLeading; fixedLeading = org.fixedLeading; multipliedLeading = org.multipliedLeading; canvas = org.canvas; canvases = org.canvases; lineStatus = org.lineStatus; indent = org.indent; followingIndent = org.followingIndent; rightIndent = org.rightIndent; extraParagraphSpace = org.extraParagraphSpace; rectangularWidth = org.rectangularWidth; rectangularMode = org.rectangularMode; spaceCharRatio = org.spaceCharRatio; lastWasNewline = org.lastWasNewline; linesWritten = org.linesWritten; arabicOptions = org.arabicOptions; runDirection = org.runDirection; descender = org.descender; composite = org.composite; splittedRow = org.splittedRow; if (org.composite) { compositeElements = new LinkedList(org.compositeElements); if (splittedRow) { PdfPTable table = (PdfPTable)compositeElements.getFirst(); compositeElements.set(0, new PdfPTable(table)); } if (org.compositeColumn != null) compositeColumn = duplicate(org.compositeColumn); } listIdx = org.listIdx; firstLineY = org.firstLineY; leftX = org.leftX; rightX = org.rightX; firstLineYDone = org.firstLineYDone; waitPhrase = org.waitPhrase; useAscender = org.useAscender; filledWidth = org.filledWidth; adjustFirstLine = org.adjustFirstLine; } private void addWaitingPhrase() { if (bidiLine == null && waitPhrase != null) { bidiLine = new BidiLine(); for (Iterator j = waitPhrase.getChunks().iterator(); j.hasNext();) { bidiLine.addChunk(new PdfChunk((Chunk)j.next(), null)); } waitPhrase = null; } } /** * Adds a <CODE>Phrase</CODE> to the current text array. * Will not have any effect if addElement() was called before. * @param phrase the text */ public void addText(Phrase phrase) { if (phrase == null || composite) return; addWaitingPhrase(); if (bidiLine == null) { waitPhrase = phrase; return; } for (Iterator j = phrase.getChunks().iterator(); j.hasNext();) { bidiLine.addChunk(new PdfChunk((Chunk)j.next(), null)); } } /** * Replaces the current text array with this <CODE>Phrase</CODE>. * Anything added previously with addElement() is lost. * @param phrase the text */ public void setText(Phrase phrase) { bidiLine = null; composite = false; compositeColumn = null; compositeElements = null; listIdx = 0; splittedRow = false; waitPhrase = phrase; } /** * Adds a <CODE>Chunk</CODE> to the current text array. * Will not have any effect if addElement() was called before. * @param chunk the text */ public void addText(Chunk chunk) { if (chunk == null || composite) return; addText(new Phrase(chunk)); } /** * Adds an element. Elements supported are <CODE>Paragraph</CODE>, * <CODE>List</CODE>, <CODE>PdfPTable</CODE>, <CODE>Image</CODE> and * <CODE>Graphic</CODE>. * <p> * It removes all the text placed with <CODE>addText()</CODE>. * @param element the <CODE>Element</CODE> */ public void addElement(Element element) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -