📄 pdfstyle.java
字号:
// $Id: PDFStyle.java,v 1.6 2003/11/04 17:16:01 mike Exp $package org.faceless.pdf;import java.util.*;import java.awt.Color;import java.awt.Paint;import java.awt.GradientPaint;/** * <p> * A PDFStyle controls the colors, font and many other aspects of the actual * display of elements on a PDF page. It's conceptually similar to a CSS * style used with HTML markup. * </p> * <p> * The idea behind the PDFStyle class is that you create a style and then * apply it to the page. This means you can switch from one style to another * with a single command, and switch back just as easily. It makes defining * a consistant "feel" to your document much easier than it would if you had * to set the font and color separately. * </p> * <b>Example</b> * <pre> * import java.awt.Color; * * // Create a new style "normal": 12pt black Times-Roman, with * // line-spacing of 1.5 * PDFStyle normal = new PDFStyle(); * normal.setFont(new StandardFont(StandardFont.TIMES), 12); * normal.setFillColor(Color.black); * normal.setTextLineSpacing(1.5); * * // Create a new varient of "normal": 12pt red Times-Italic with * // the same line-spacing * PDFStyle italic = (PDFStyle)normal.clone(); * normal.setFont(new StandardFont(StandardFont.TIMESITALIC), 12); * normal.setFillColor(Color.red); * * // Create a style to draw a box around the text in green, with a * // line width of 2 points * PDFStyle boxstyle = new PDFStyle(); * boxstyle.setLineColor(Color.green); * boxstyle.setLineWeighting(2); * * // Now use these styles in a document * PDF p = new PDF(); * PDFPage page = p.newPage(PDF.PAGESIZE_A4); * page.setStyle(normal); * page.drawText("This is in 12pt black Times Roman", 100, 100); * page.setStyle(italic); * page.drawText("This is in 12pt red Times Italic", 100, 120); * * // Draw a box around them in green * page.setStyle(boxstyle); * page.drawRectangle(90,90, 200, 40); * </pre> * * <p> * This shows several useful aspects of styles: * <ul> * <li>Styles can easily be extended by using the <code>clone()</code> method.</li> * <li>Not all the aspects of a style need to be set.</li> * <li>Styles can be given meaningful names.</li> * </ul> * </p> * @version $Revision: 1.6 $ */public class PDFStyle extends PeeredObject implements Cloneable{ /** * Set the text alignment for this style to left-aligned */ public static final int TEXTALIGN_LEFT = 1; /** * Set the text alignment for this style to right-aligned */ public static final int TEXTALIGN_RIGHT = 2; /** * Set the text alignment for this style to centered */ public static final int TEXTALIGN_CENTER = 4; /** * Set the text alignment for this style to justified (the default). * Justification is only useful when text is written within a paragraph. * When a single element of text is drawn, the effect is the same as * left-alignment. */ public static final int TEXTALIGN_JUSTIFY = 8; /** * Set the vertical text alignment for this style to baseline (the default). */ public static final int TEXTALIGN_BASELINE = 0; /** * Set the vertical text alignment for this style to top */ public static final int TEXTALIGN_TOP = 16; /** * Set the vertical text alignment for this style to middle */ public static final int TEXTALIGN_MIDDLE = 32; /** * Set the vertical text alignment for this style to bottom */ public static final int TEXTALIGN_BOTTOM = 64; /** * Set the end of a line to be squared off at the end. There is no * projection beyond the end of the path. This is the default */ public static final int LINECAP_BUTT = 0; /** * Set the end of a line to be rounded at the end. Effectively draws a circle with * a diameter of the line width at the end of each line. */ public static final int LINECAP_ROUND = 1; /** * Set the end of a line to be squared at the end. Effectively extends each line by * half the linewidth. */ public static final int LINECAP_SQUARE = 2; /** * Sets the join style of two lines so that the lines are extended so they meet at * a point (like a picture frame). For extremely sharp angles, this will automatically * be converted to a LINEJOIN_BEVEL. This is the default. */ public static final int LINEJOIN_MITER = 0; /** * Sets the join style of two lines so that the lines are rounded, equivalent to * drawing a circle with a diameter of the linewidth where the lines meet. */ public static final int LINEJOIN_ROUND = 1; /** * Sets the join style of two lines so that the lines are beveled. The two lines * are drawn with LINECAP_BUTT ends, and the notch between the two segments is * filled in with a triangle. */ public static final int LINEJOIN_BEVEL = 2; /** * Set any text rendered in this style to be filled with the styles * FillColor (the default) */ public static final int FONTSTYLE_FILLED = 0; /** * Set any text rendered in this style to be drawn as a hollow outline * with the styles LineColor (the default) */ public static final int FONTSTYLE_OUTLINE = 1; /** * Set any text rendered in this style to be filled with the styles * FillColor, then to be outlined with the styles' LineColor */ public static final int FONTSTYLE_FILLEDOUTLINE = 2; /** * Set any text rendered in this style to be invisible. This becomes * useful in applications like OCR, where the original scanned image * is displayed on the screen and invisible text written above it, * allowing the text to be cut and pasted. */ public static final int FONTSTYLE_INVISIBLE = 3; /** * A parameter to {@link #setPaintMethod} to set the paint method to * use the non-zero winding number method to determine which areas are * inside or outside a shape. This is the default. */ public static final int PAINTMETHOD_NONZEROWINDING = 0; /** * A parameter to {@link #setPaintMethod} to set the paint method to * use the even-odd method to determine which areas are inside or * outside a shape. */ public static final int PAINTMETHOD_EVENODD = 1; final org.faceless.pdf2.PDFStyle style; Object getPeer() { return style; } PDFStyle(org.faceless.pdf2.PDFStyle style) { this.style=style; } /** * A PDFStyle which can be passed in to {@link PDFPage#beginTextLink} * to underline the text links. */ public static final PDFStyle LINKSTYLE; static { LINKSTYLE = new PDFStyle(); LINKSTYLE.setTextUnderline(true); } /** * Create a new PDFStyle. The default aspects of this style are: * <ul> * <li>No Font specified, no line color, no fill color. These must be set by the user</li> * <li>Line Weighting of 1 point</li> * <li>Line Dashing set to a solid line</li> * <li>Line Spacing set to a 1</li> * <li>Text Justification ratio of 0.5</li> * <li>Line ends set to {@link #LINECAP_BUTT}</li> * <li>Line joints set to {@link #LINEJOIN_MITER}</li> * <li>Font style set to {@link #FONTSTYLE_FILLED}</li> * <li>Text alignment is locale dependent - for hebrew, arabic, urdu and yiddish, it's {@link #TEXTALIGN_RIGHT}. For japanese, chinese and korean, it's {@link #TEXTALIGN_LEFT} and for everything else it's {@link #TEXTALIGN_JUSTIFY}.</li> * </ul> */ public PDFStyle() { style = new org.faceless.pdf2.PDFStyle(); } /** * Set the text justification ratio for a style. When a line of text * is padded to justify it against two margins, there is always the * the option of extending the space between each character, extending * the space between each word, or a combination of the two. The * "justification ratio" determines how much to apply to each. A ratio * of 0 means "only extend the spaces between words", a ratio of 1 * means "only extend the spaces between letters". The default is 0.5. */ public void setTextJustificationRatio(float in) { style.setTextJustificationRatio(in); } /** * Set whether text rendered with this style is underlined or not. * The exact position and width of the underlining is font specific. * The default is <i>false</i>. * @since 1.1 */ public void setTextUnderline(boolean on) { style.setTextUnderline(on); } /** * Set whether text rendered with this style is struck-out or not. * The default is <i>false</i>. * @since 1.1 */ public void setTextStrikeOut(boolean on) { style.setTextStrikeOut(on); } /** * Set the line cap style. Value can be {@link #LINECAP_BUTT}, {@link #LINECAP_ROUND} or * {@link #LINECAP_SQUARE} * <p> * Changes to this setting midway through a <code>path</code> don't * take effect until it is closed. * </p> */ public void setLineCap(int type) { style.setLineCap(type); } /** * Set the line join style. Value can be {@link #LINEJOIN_MITER}, {@link #LINEJOIN_ROUND} or * {@link #LINEJOIN_BEVEL} * <p> * Changes to this setting midway through a <code>path</code> don't * take effect until it is closed. * </p> */ public void setLineJoin(int type) { style.setLineJoin(type); } /** * <p> * Set the line dashing pattern. The first <code>on</code> points of * the line will be drawn, then the next <code>off</code> points of * the line will be skipped. The <code>phase</code> argument defines * how far into this pattern to start. * </p> * <p> * An example would be <code>setLineDash(2,2,0)</code> to draw lines that * have two points on, two points off and so on, starting with two "on" * points. For solid lines, the call should * be <code>setLineDash(0,0,0)</code> * </p> * <p> * Changes to this setting midway through a <code>path</code> don't * take effect until it is closed. * </p> * Since 1.1.5, the parameters are floats rather than ints * @param on how many points of the line to draw * @param off how many points of the line to skip * @param phase how far into the pattern to start */ public void setLineDash(float on, float off, float phase) { style.setLineDash(on,off,phase); } /** * Set the line weighting, for fonts and geometric shapes drawn as * outlines. The minimum possible value is zero, which indicates to * the PDF renderer to use the thinnest line possible. On high-resolution * devices, this will become nearly invisible. Because of its * device-dependent nature, it's not recommended. * <p> * Changes to this setting midway through a <code>path</code> don't * take effect until it is closed. * </p> * * @param thickness The thickness of the line, in points */ public void setLineWeighting(float thickness) { style.setLineWeighting(thickness); } /** * <p> * Set the spacing between lines of text. In versions prior to 1.1, * this was a multiplied by the the fonts point size to give the difference * between the baselines of two successive lines of text, and the default * value was 1.2. * </p><p> * Since 1.1, this has been changed to a ratio of the <i>default spacing * between lines for this font</i> (as determined by the * {@link PDFFont#getDefaultLeading} method). This allows individual fonts to set * their preferred line spacing more accurately. * Although the default setting should result in nearly identical results * for most fonts, developers calling this method who are upgrading from * versions 1.0.4 or earlier should divide the value passed in by 1.2 to * meet the new method contract. * </p><p> * The default value is now 1. A value of 1.5 sets line-and-a-half spacing, * a value of 2 gives double spacing, and so on. * </p> * @param factor the spacing between lines */ public void setTextLineSpacing(float factor) { style.setTextLineSpacing(factor); } /** * Set the text-alignment. Can be one of {@link #TEXTALIGN_LEFT}, * {@link #TEXTALIGN_RIGHT}, {@link #TEXTALIGN_CENTER} or {@link #TEXTALIGN_JUSTIFY}. */ public void setTextAlign(int align) { style.setTextAlign(align); } /** * <p> * Set the text vertical offset - the distance between the standard * baseline and the basline for this style, as a proportion of the * font size. This is mainly used for superscripting or subscripting. * text. * </p><p> * As an example, if you wanted to create a line of text that was 6 points * high and 6 points above the standard baseline, set the font size to * 6 and the TextRise() to 1. (1x6 = 6 points above the baseline). * To place the same text 3 points below the baseline, set the offset to -0.5. * </p> * @since 1.1 */ public void setTextRise(float offset) { style.setTextRise(offset); } /** * <p> * Set the fill color. For text, this is the color of the text. * For geometric shapes, this is paint to fill those shapes with. * To draw only outlines, set it to <code>null</code> (the default). * </p><p> * Prior to release 1.2, this method took a * {@link java.awt.Color} as an argument, but this has been changed to * its superclass, {@link java.awt.Paint}, instead. * Although most of the time the parameter will still be a plain color, * this change allows a {@link java.awt.GradientPaint} to be used * as well. * </p> * @param paint the paint to use, or <code>null</code> if no fill * is required. */ public void setFillColor(Paint paint) { if (paint!=null && paint instanceof ColorPattern) { paint = ((ColorPattern)paint).pattern; } style.setFillColor(paint); } /** * Set the line color. For text, this is the color of the outline of * the text. For geometric shapes, this is color to draw the outlines * of those shapes. If no outline is required, set it to * <code>null</code> (the default). * @param color The Color to use, or <code>null</code> if no outline * is required. */ public void setLineColor(Color color) { style.setLineColor(color); } /** * <p> * Set the paint method to either {@link #PAINTMETHOD_EVENODD} or * {@link #PAINTMETHOD_NONZEROWINDING} (the default). The paint method * determines which area of a self-intersecting polygon is filled. If * your polygons aren't self-intersecting, it has no effect. * </p><p> * This setting is very obscure and is really only here for completeness. * For a full discussion of the difference see the <a href="http://partners.adobe.com/asn/developer/acrosdk/docs/PDFRef.pdf">PDF Reference manual version 1.3</a>, page 156. * </p> * @since 1.1.5 */ public void setPaintMethod(int method) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -