[FOray-commit] SF.net SVN: foray:[11831] trunk/foray
Modular XSL-FO Implementation for Java.
Status: Alpha
Brought to you by:
victormote
|
From: <vic...@us...> - 2021-01-19 13:09:07
|
Revision: 11831
http://sourceforge.net/p/foray/code/11831
Author: victormote
Date: 2021-01-19 13:09:03 +0000 (Tue, 19 Jan 2021)
Log Message:
-----------
Conform to aXSL change: Change API for rendering a vector graphic to PDF to use a Java Graphics2D implementation to do the rendering. This pushes the creation of the Graphics2D instance out of foray-graphic and into foray-pdf.
Modified Paths:
--------------
trunk/foray/foray-graphic/src/main/java/org/foray/graphic/batik/PdfaNode.java
trunk/foray/foray-graphic/src/main/java/org/foray/graphic/output/SvgPdf.java
trunk/foray/foray-pdf/build.gradle
trunk/foray/foray-pdf/src/main/java/org/foray/pdf/object/PdfContentStream4a.java
trunk/foray/foray-pdf/src/main/java/org/foray/pdf/object/PdfXformMath.java
trunk/foray/foray-pdf/src/main/java/org/foray/pdf/object/PdfXformSvg.java
trunk/foray/foray-pdf/src/main/java/org/foray/pdf/object/PdfXobject.java
Added Paths:
-----------
trunk/foray/foray-graphic/src/main/java/org/foray/graphic/batik/PdfTextPainter.java
trunk/foray/foray-pdf/src/main/java/org/foray/pdf/util/PdfGraphics2D.java
trunk/foray/foray-pdf/src/main/java/org/foray/pdf/util/PdfGraphicsConfiguration.java
trunk/foray/foray-pdf/src/main/java/org/foray/pdf/util/PdfGraphicsDevice.java
Removed Paths:
-------------
trunk/foray/foray-graphic/src/main/java/org/foray/graphic/batik/PdfGraphics2D.java
trunk/foray/foray-graphic/src/main/java/org/foray/graphic/batik/PdfGraphicsConfiguration.java
trunk/foray/foray-graphic/src/main/java/org/foray/graphic/batik/PdfGraphicsDevice.java
trunk/foray/foray-graphic/src/main/java/org/foray/graphic/batik/PdfTextPainter.java
Deleted: trunk/foray/foray-graphic/src/main/java/org/foray/graphic/batik/PdfGraphics2D.java
===================================================================
--- trunk/foray/foray-graphic/src/main/java/org/foray/graphic/batik/PdfGraphics2D.java 2021-01-19 03:10:34 UTC (rev 11830)
+++ trunk/foray/foray-graphic/src/main/java/org/foray/graphic/batik/PdfGraphics2D.java 2021-01-19 13:09:03 UTC (rev 11831)
@@ -1,950 +0,0 @@
-/*
- * Copyright 2004 The FOray Project.
- * http://www.foray.org
- *
- * Licensed under the Apache License, Version 2.0 (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.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- *
- * This work is in part derived from the following work(s), used with the
- * permission of the licensor:
- * Apache FOP, licensed by the Apache Software Foundation
- *
- */
-
-/*
- * $LastChangedRevision$
- * $LastChangedDate$
- * $LastChangedBy$
- */
-
-/*
- * Known Contributors:
- * Original Author: <a href="mailto:ke...@af...">Keiron Liddle</a>
- */
-
-package org.foray.graphic.batik;
-
-import org.foray.common.CharacterOutputStream;
-import org.foray.common.Gradient4a;
-import org.foray.common.WellKnownConstants;
-import org.foray.common.i18n.Orthography4a;
-import org.foray.common.ps.PsColor;
-import org.foray.common.ps.PsUtil;
-
-import org.axsl.font.FontConsumer;
-import org.axsl.font.FontOptions;
-import org.axsl.font.FontUse;
-import org.axsl.font.FontUtility;
-import org.axsl.graphic.output.GraphicOutputContext;
-import org.axsl.pdf.PdfPage;
-
-import org.slf4j.LoggerFactory;
-
-import java.awt.AlphaComposite;
-import java.awt.BasicStroke;
-import java.awt.Color;
-import java.awt.Dimension;
-import java.awt.Font;
-import java.awt.FontMetrics;
-import java.awt.GradientPaint;
-import java.awt.Graphics;
-import java.awt.Graphics2D;
-import java.awt.GraphicsConfiguration;
-import java.awt.Image;
-import java.awt.Paint;
-import java.awt.Rectangle;
-import java.awt.Shape;
-import java.awt.Stroke;
-import java.awt.TexturePaint;
-import java.awt.color.ColorSpace;
-import java.awt.font.TextAttribute;
-import java.awt.geom.AffineTransform;
-import java.awt.geom.PathIterator;
-import java.awt.geom.Point2D;
-import java.awt.geom.Rectangle2D;
-import java.awt.image.BufferedImage;
-import java.awt.image.DataBuffer;
-import java.awt.image.DataBufferInt;
-import java.awt.image.ImageObserver;
-import java.awt.image.Raster;
-import java.awt.image.RenderedImage;
-import java.awt.image.renderable.RenderableImage;
-import java.io.IOException;
-import java.io.OutputStream;
-import java.text.AttributedCharacterIterator;
-import java.text.CharacterIterator;
-import java.util.ArrayList;
-import java.util.List;
-import java.util.Stack;
-
-/**
- * A {@link Graphics2D} implementation used by Batik to render two-dimensional
- * objects (the SVG image) into a PDF document.
- */
-public class PdfGraphics2D
- extends org.apache.batik.ext.awt.g2d.AbstractGraphics2D {
- /* TODO: The capabilities in this class need to be merged with those of
- * {@link PDFContentStream}. This class should essentially use the content
- * stream to do most of the work. */
-
- /**
- * Private inner bean to track a subset of the graphics state.
- */
- private final class PdfGraphicsState {
-
- /** The stroke color. */
- private Color strokeColor;
-
- /** The "non-stroke" or "other" color. */
- private Color fillColor;
-
- /** The font. */
- private FontUse font;
-
- /** The font size. */
- private float fontSize;
-
- /**
- * Constructor.
- * @param existingState The state on which the new state should be based.
- * If null, the new state is set to default values.
- */
- private PdfGraphicsState(final PdfGraphicsState existingState) {
- if (existingState != null) {
- this.strokeColor = existingState.strokeColor;
- this.fillColor = existingState.fillColor;
- this.font = existingState.font;
- this.fontSize = existingState.fontSize;
- }
- }
-
- }
-
- /** Constant indicating the number of elements in a cubic element, that is,
- * 6. */
- public static final byte QTY_ELEMENTS_CUBIC = 6;
-
- /** Constant indicating the number of elements in a quad element, that is,
- * 4. */
- public static final byte QTY_ELEMENTS_QUAD = 4;
-
- /** Constant indicating the number of elements in a linear element, that is,
- * 2. */
- public static final byte QTY_ELEMENTS_LINEAR = 2;
-
- /** The current stream to add PDF commands to. */
- private CharacterOutputStream currentStream;
-
- /** The override font. */
- private FontUse overrideFont = null;
-
- /** The override font size, in points. */
- private float overrideFontSize;
-
- /** The font consumer instance for this document. */
- private FontConsumer fontConsumer;
-
- /** Used to create proper font metrics. */
- private Graphics2D fmg;
-
- /** The PDF context. */
- private GraphicOutputContext pdfContext;
-
- /** The graphics state stack. The top item on the stack is always the
- * current graphics state. The next one down is what will be current after
- * the next "restore". */
- private Stack<PdfGraphics2D.PdfGraphicsState> graphicsStateStack = new Stack<PdfGraphics2D.PdfGraphicsState>();
-
- /** Indicates whether any I/O Exceptions were found while processing. */
- private boolean ioErrorFound = false;
-
- /**
- * Create a new PdfGraphics2D with the given pdf document info.
- * This is used to create a Graphics object for use inside an already existing document.
- * @param textAsShapes Set this to true so that text will be rendered using curves and not the font.
- * @param fontConsumer The font consumer for this document.
- * @param pdfContext The PDF context in which this content is being written.
- * @param outputStream The output stream to which this processor writes its PDF output.
- */
- public PdfGraphics2D(final boolean textAsShapes, final FontConsumer fontConsumer,
- final GraphicOutputContext pdfContext, final OutputStream outputStream) {
- super(textAsShapes);
- /* TODO: Only write the comments if running in debug mode?? */
- this.currentStream = new CharacterOutputStream(outputStream, true);
- this.comment("%%%% Start FOrayGraphic SVG-to-PDF conversion. %%%%");
-
- this.fontConsumer = fontConsumer;
- this.pdfContext = pdfContext;
- this.graphicsStateStack.push(new PdfGraphicsState(null));
- }
-
- /**
- * This constructor supports the create method.
- * @param g The encapsulated image.
- */
- public PdfGraphics2D(final PdfGraphics2D g) {
- super(g);
- this.currentStream = g.currentStream;
- this.fontConsumer = g.fontConsumer;
- this.pdfContext = g.pdfContext;
- this.graphicsStateStack = g.graphicsStateStack;
- }
-
- /**
- * Converts a matrix of octal values to its PDF representation.
- * @param theMatrix The matrix to be converted.
- * @return The PDF representation of the matrix.
- */
- public static String octalMatrixToString(final double[] theMatrix) {
- final StringBuilder buffer = new StringBuilder();
- for (int i = 0; i < theMatrix.length; i++) {
- buffer.append(PsUtil.doubleOut(theMatrix[i],
- WellKnownConstants.RADIX_BASE_8));
- buffer.append(" ");
- }
- return buffer.toString();
- }
-
- /**
- * Converts a matrix of decimal values to its PDF representation.
- * @param theMatrix The matrix to be converted.
- * @param numElements The number of elements in the matrix to be included
- * in the output.
- * @return The PDF representation of the matrix.
- */
- public static String matrixToString(final double[] theMatrix,
- final int numElements) {
- final StringBuilder buffer = new StringBuilder();
- for (int i = 0; i < numElements; i++) {
- buffer.append(PsUtil.doubleOut(theMatrix[i]) + " ");
- }
- return buffer.toString();
- }
-
- /**
- * Sets the graphic context.
- * @param c The new graphic context.
- */
- public void setGraphicContext(
- final org.apache.batik.ext.awt.g2d.GraphicContext c) {
- gc = c;
- }
-
- /**
- * Sets the override font and size.
- * @param infont The new override font.
- * @param size The new override font size.
- */
- public void setOverrideFontState(final FontUse infont, final float size) {
- this.overrideFont = infont;
- this.overrideFontSize = size;
- }
-
- @Override
- public Graphics create() {
- return new PdfGraphics2D(this);
- }
-
- /**
- * This is a pdf specific method used to add a link to the pdf document.
- * @param pdfPage The page on which the link should be placed.
- * @param bounds The location of the link.
- * @param trans The affine transform.
- * @param dest The name of the destination.
- * @param externalLink Set to true if the link is to an external document.
- */
- public void addLink(final PdfPage pdfPage, final Shape bounds,
- final AffineTransform trans, final String dest,
- final boolean externalLink) {
- final AffineTransform at = getTransform();
- Shape b = at.createTransformedShape(bounds);
- b = trans.createTransformedShape(b);
- final Rectangle2D.Float linkRectangle =
- (Rectangle2D.Float) b.getBounds2D();
- linkRectangle.height *= -1;
- if (externalLink) {
- final String pdfdest = "/XYZ " + dest;
- pdfPage.createLink(linkRectangle, pdfdest, true);
- } else {
- pdfPage.createLink(linkRectangle, dest, false);
- }
- }
-
- @Override
- public boolean drawImage(final Image img, final int x, final int y,
- final ImageObserver observer) {
- final int width = img.getWidth(observer);
- final int height = img.getHeight(observer);
-
- if (width == -1 || height == -1) {
- return false;
- }
-
- final int numChannels = this.getGraphicsState().strokeColor.getColorSpace().getNumComponents();
- final Dimension size = new Dimension(width * numChannels,
- height * numChannels);
- final BufferedImage buf = new BufferedImage(size.width, size.height,
- BufferedImage.TYPE_INT_ARGB);
-
- final Graphics2D g = buf.createGraphics();
- g.setComposite(AlphaComposite.SrcOver);
- g.setBackground(new Color(1, 1, 1, 0));
- g.setPaint(new Color(1, 1, 1, 0));
- g.fillRect(0, 0, width * numChannels, height * numChannels);
- g.clip(new Rectangle(0, 0, buf.getWidth(), buf.getHeight()));
-
- if (!g.drawImage(img, 0, 0, buf.getWidth(), buf.getHeight(),
- observer)) {
- return false;
- }
- g.dispose();
-
- final byte[] result = new byte[buf.getWidth() * buf.getHeight()
- * numChannels];
-
- final Raster raster = buf.getData();
- final DataBuffer bd = raster.getDataBuffer();
-
- int count = 0;
- int[] iarray;
- int i;
- int j;
- int val;
- int alpha;
- int add;
- int mult;
- switch (bd.getDataType()) {
- case DataBuffer.TYPE_INT:
- final int[][] idata = ((DataBufferInt) bd).getBankData();
- for (i = 0; i < idata.length; i++) {
- iarray = idata[i];
- for (j = 0; j < iarray.length; j++) {
- val = iarray[j];
- alpha = val >>> WellKnownConstants.SHIFT_3_BYTES;
- // mask[maskpos++] = (byte)((idata[i][j] >> 24) & 0xFF);
- if (alpha != WellKnownConstants.MAX_8_BIT_UNSIGNED_INT) {
- // Composite with opaque white...
- add = WellKnownConstants.MAX_8_BIT_UNSIGNED_INT - alpha;
- mult = (alpha << WellKnownConstants.SHIFT_2_BYTES)
- / WellKnownConstants.MAX_8_BIT_UNSIGNED_INT;
- result[count++] =
- (byte) (add
- + ((((val >> WellKnownConstants.SHIFT_2_BYTES)
- & WellKnownConstants.MAX_8_BIT_UNSIGNED_INT)
- * mult) >> WellKnownConstants.SHIFT_2_BYTES));
- result[count++] =
- (byte) (add
- + ((((val >> WellKnownConstants.SHIFT_1_BYTE)
- & WellKnownConstants.MAX_8_BIT_UNSIGNED_INT)
- * mult) >> WellKnownConstants.SHIFT_2_BYTES));
- result[count++] = (byte) (add
- + (((val & WellKnownConstants.MAX_8_BIT_UNSIGNED_INT)
- * mult)
- >> WellKnownConstants.SHIFT_2_BYTES));
- } else {
- result[count++] = (byte) ((val >> WellKnownConstants.SHIFT_2_BYTES)
- & WellKnownConstants.MAX_8_BIT_UNSIGNED_INT);
- result[count++] = (byte) ((val >> WellKnownConstants.SHIFT_1_BYTE)
- & WellKnownConstants.MAX_8_BIT_UNSIGNED_INT);
- result[count++] = (byte) (val
- & WellKnownConstants.MAX_8_BIT_UNSIGNED_INT);
- }
- }
- }
- break;
- default:
- // error
- break;
- }
-
- // now do any transformation required and add the actual image
- // placement instance
- final AffineTransform at = getTransform();
- final double[] matrix = new double[PsUtil.MATRIX_QTY_ELEMENTS];
- at.getMatrix(matrix);
- this.saveGraphicsState();
- final Shape imclip = getClip();
- writeClip(imclip);
- final String matrixString = matrixToString(matrix,
- PdfGraphics2D.QTY_ELEMENTS_CUBIC);
- this.write(matrixString + "cm");
- this.restoreGraphicsState();
- return true;
- }
-
- @Override
- public boolean drawImage(final Image img, final int x, final int y,
- final int width, final int height, final ImageObserver observer) {
- return true;
- }
-
- @Override
- public void dispose() {
- this.comment("%%%% End FOrayGraphic SVG-to-PDF conversion. %%%%");
- this.currentStream = null;
- this.graphicsStateStack = null;
- }
-
- @Override
- public void draw(final Shape s) {
- final Color c = getColor();
- if (c.getAlpha() == 0) {
- return;
- }
-
- this.saveGraphicsState();
- final Shape imclip = getClip();
- writeClip(imclip);
- applyColor(c, false);
-
- applyPaint(getPaint(), false);
- applyStroke(getStroke());
-
- final AffineTransform trans = getTransform();
- final double[] tranvals =
- new double[PsUtil.MATRIX_QTY_ELEMENTS];
- trans.getMatrix(tranvals);
- String matrixString = octalMatrixToString(tranvals);
- this.write(matrixString + "cm");
-
- final PathIterator iter = s.getPathIterator(new AffineTransform());
- while (!iter.isDone()) {
- final double vals[] =
- new double[PsUtil.MATRIX_QTY_ELEMENTS];
- final int type = iter.currentSegment(vals);
- switch (type) {
- case PathIterator.SEG_CUBICTO:
- matrixString = matrixToString(vals,
- PdfGraphics2D.QTY_ELEMENTS_CUBIC);
- this.write(matrixString + "c");
- break;
- case PathIterator.SEG_LINETO:
- matrixString = matrixToString(vals,
- PdfGraphics2D.QTY_ELEMENTS_LINEAR);
- this.write(matrixString + "l");
- break;
- case PathIterator.SEG_MOVETO:
- matrixString = matrixToString(vals,
- PdfGraphics2D.QTY_ELEMENTS_LINEAR);
- this.write(matrixString + "m");
- break;
- case PathIterator.SEG_QUADTO:
- matrixString = matrixToString(vals,
- PdfGraphics2D.QTY_ELEMENTS_QUAD);
- this.write(matrixString + "y");
- break;
- case PathIterator.SEG_CLOSE:
- this.write("h");
- break;
- default:
- break;
- }
- iter.next();
- }
- doDrawing(false, true, false);
- this.restoreGraphicsState();
- }
-
- /**
- * Clip the document to a specific shape.
- * @param s The shape to which the document should be clipped.
- */
- protected void writeClip(final Shape s) {
- if (s == null) {
- return;
- }
- final PathIterator iter = s.getPathIterator(getTransform());
- String matrixString = null;
- while (!iter.isDone()) {
- final double vals[] =
- new double[PsUtil.MATRIX_QTY_ELEMENTS];
- final int type = iter.currentSegment(vals);
- switch (type) {
- case PathIterator.SEG_CUBICTO:
- matrixString = matrixToString(vals,
- PdfGraphics2D.QTY_ELEMENTS_CUBIC);
- this.write(matrixString + "c");
- break;
- case PathIterator.SEG_LINETO:
- matrixString = matrixToString(vals,
- PdfGraphics2D.QTY_ELEMENTS_LINEAR);
- this.write(matrixString + "l");
- break;
- case PathIterator.SEG_MOVETO:
- matrixString = matrixToString(vals,
- PdfGraphics2D.QTY_ELEMENTS_LINEAR);
- this.write(matrixString + "m");
- break;
- case PathIterator.SEG_QUADTO:
- matrixString = matrixToString(vals,
- PdfGraphics2D.QTY_ELEMENTS_QUAD);
- this.write(matrixString + "y");
- break;
- case PathIterator.SEG_CLOSE:
- this.write("h");
- break;
- default:
- break;
- }
- iter.next();
- }
- // clip area
- this.write("W");
- this.write("n");
- }
-
- /**
- * Apply a color to the document.
- * @param color The color to be applied.
- * @param fill Set to true if the color is for fill, false if for stroke.
- */
- protected void applyColor(final Color color, final boolean fill) {
- boolean anyChange = false;
- if (fill) {
- anyChange = ! color.equals(this.getGraphicsState().fillColor);
- this.getGraphicsState().fillColor = color;
- } else {
- anyChange = ! color.equals(this.getGraphicsState().strokeColor);
- this.getGraphicsState().strokeColor = color;
- }
- if (anyChange) {
- this.write(PsColor.toPS(color, fill));
- }
- }
-
- /**
- * Apply a paint to the document.
- * @param paint The paint to be applied.
- * @param fill Set to true if the paint is for fill, false if for stroke.
- */
- protected void applyPaint(final Paint paint, final boolean fill) {
- if (paint instanceof GradientPaint) {
- final GradientPaint gp = (GradientPaint) paint;
- final Color c1 = gp.getColor1();
- final Color c2 = gp.getColor2();
- final Point2D p1 = gp.getPoint1();
- final Point2D p2 = gp.getPoint2();
-
- final List<Double> theCoords = new ArrayList<Double>();
- theCoords.add(p1.getX());
- theCoords.add(p1.getY());
- theCoords.add(p2.getX());
- theCoords.add(p2.getY());
-
- final List<Boolean> theExtend = new ArrayList<Boolean>();
- theExtend.add(Boolean.TRUE);
- theExtend.add(Boolean.TRUE);
-
- final List<Double> theDomain = new ArrayList<Double>();
- theDomain.add(0D);
- theDomain.add(1D);
-
- final List<Double> theEncode = new ArrayList<Double>();
- theEncode.add(0D);
- theEncode.add(1D);
- theEncode.add(0D);
- theEncode.add(1D);
-
- final List<Double> theBounds = new ArrayList<Double>();
- theBounds.add(0D);
- theBounds.add(1D);
-
- final List<Color> someColors = new ArrayList<Color>();
-
- someColors.add(c1);
- someColors.add(c2);
-
- final ColorSpace aColorSpace = ColorSpace.getInstance(ColorSpace.CS_sRGB);
- final Gradient4a gradient = new Gradient4a(false, aColorSpace, someColors, null, theCoords);
- final String gradientName = this.pdfContext.getGradientName(gradient);
- if (fill) {
- this.write("/Pattern cs /" + gradientName + " scn" + WellKnownConstants.ASCII_LINEFEED);
- } else {
- this.write("/Pattern CS /" + gradientName + " SCN" + WellKnownConstants.ASCII_LINEFEED);
- }
- } else if (paint instanceof TexturePaint) { }
- }
-
- /**
- * Apply a stroke to the document.
- * @param stroke The stroke to be applied.
- */
- protected void applyStroke(final Stroke stroke) {
- if (stroke instanceof BasicStroke) {
- final BasicStroke bs = (BasicStroke) stroke;
-
- final float[] da = bs.getDashArray();
- if (da != null) {
- this.write("[");
- for (int count = 0; count < da.length; count++) {
- if (((int) da[count]) == 0) {
- // the dasharray units in pdf are (whole) numbers
- // in user space units, cannot be 0
- this.write("1");
- } else {
- this.write("" + ((int) da[count]));
- }
- if (count < da.length - 1) {
- this.write(" ");
- }
- }
- this.write("] ");
- final float offset = bs.getDashPhase();
- this.write(((int) offset) + " d");
- }
- final int ec = bs.getEndCap();
- switch (ec) {
- case BasicStroke.CAP_BUTT:
- this.write(0 + " J");
- break;
- case BasicStroke.CAP_ROUND:
- this.write(1 + " J");
- break;
- case BasicStroke.CAP_SQUARE:
- this.write(2 + " J");
- break;
- }
-
- final int lj = bs.getLineJoin();
- switch (lj) {
- case BasicStroke.JOIN_MITER:
- this.write(0 + " j");
- break;
- case BasicStroke.JOIN_ROUND:
- this.write(1 + " j");
- break;
- case BasicStroke.JOIN_BEVEL:
- this.write(2 + " j");
- break;
- }
- final float lw = bs.getLineWidth();
- this.write(PsUtil.doubleOut(lw) + " w");
-
- final float ml = bs.getMiterLimit();
- this.write(PsUtil.doubleOut(ml) + " M");
- }
- }
-
- @Override
- public void drawRenderedImage(final RenderedImage img, final AffineTransform xform) {
- }
-
- @Override
- public void drawRenderableImage(final RenderableImage img, final AffineTransform xform) {
- }
-
- @Override
- public void drawString(final String s, final float x, final float y) {
- final FontUse font;
- final float size;
- if (this.overrideFont == null) {
- font = getImplicitFont();
- size = getFont().getSize();
- } else {
- font = this.overrideFont;
- size = this.overrideFontSize;
- this.overrideFont = null;
- }
-
- if (font != this.getGraphicsState().font
- || (size != this.getGraphicsState().fontSize)) {
- this.getGraphicsState().font = font;
- this.getGraphicsState().fontSize = size;
- final String fontName = this.pdfContext.getFontName(font);
- this.write("/" + fontName + " " + size + " Tf");
- }
-
- this.saveGraphicsState();
-
- final Shape imclip = getClip();
- writeClip(imclip);
- Color c = getColor();
- applyColor(c, true);
- c = getBackground();
- applyColor(c, false);
-
- this.write("BT");
-
- final AffineTransform trans = getTransform();
- trans.translate(x, y);
- final double[] vals = new double[PsUtil.MATRIX_QTY_ELEMENTS];
- trans.getMatrix(vals);
-
- final String matrixString = octalMatrixToString(vals);
- this.write(matrixString + "cm");
- this.write("1 0 0 -1 0 0 Tm ");
-
- final CharSequence outputString = font.textToPdf(s, FontOptions.DEFAULT, Orthography4a.USA);
- this.write(outputString);
-
- this.write("ET");
- this.restoreGraphicsState();
- }
-
- /**
- * This method attempts to obtain an org.foray.font.Font instance from a
- * java.awt.Font that comes from the Batik environment.
- * @return The axsl Font, or null if the internal font cannot be converted.
- */
- private FontUse getImplicitFont() {
- final Font gFont = getFont();
- String fontFamily = gFont.getFamily();
- if (fontFamily.equals("sanserif")) {
- fontFamily = "sans-serif";
- }
- final float fontSize = gFont.getSize();
- org.axsl.font.Font.Style fontStyle = org.axsl.font.Font.Style.NORMAL;
- if (gFont.isItalic()) {
- fontStyle = org.axsl.font.Font.Style.ITALIC;
- }
- org.axsl.font.Font.Weight fontWeight
- = org.axsl.font.Font.Weight.NORMAL;
- if (gFont.isBold()) {
- fontWeight = org.axsl.font.Font.Weight.BOLD;
- }
- FontUse fontUse = this.fontConsumer.selectFontXsl(
- org.axsl.font.Font.SelectionStrategy.AUTO,
- FontUtility.foFontFamily(fontFamily),
- fontStyle, fontWeight,
- org.axsl.font.Font.Variant.NORMAL,
- org.axsl.font.Font.Stretch.NORMAL,
- (int) (fontSize * WellKnownConstants.MILLIPOINTS_PER_POINT),
- ' ');
- if (fontUse == null) {
- fontUse = this.fontConsumer.selectFontFallback();
- }
- return fontUse;
- }
-
- @Override
- public void drawString(final AttributedCharacterIterator iterator, final float x, final float y) {
- LoggerFactory.getLogger(this.getClass()).error("drawString(AttributedCharacterIterator)");
- /* TODO: This method may not ever be called, as we never see the above error logged. Investigate further. */
-
- final Shape imclip = getClip();
- writeClip(imclip);
- Color c = getColor();
- applyColor(c, true);
- c = getBackground();
- applyColor(c, false);
-
- this.write("BT");
-
- final AffineTransform trans = getTransform();
- trans.translate(x, y);
- final double[] vals = new double[PsUtil.MATRIX_QTY_ELEMENTS];
- trans.getMatrix(vals);
-
- for (char ch = iterator.first(); ch != CharacterIterator.DONE;
- ch = iterator.next()) {
- /* TODO: All of this should be integrated with the regular methods
- * in PDFContentStream. */
- final List<String> fontFamily = new ArrayList<String>(1);
- fontFamily.add((String) iterator.getAttribute(TextAttribute.FAMILY));
- final Float fontSize = (Float) iterator.getAttribute(
- TextAttribute.FAMILY);
- FontUse font = this.fontConsumer.selectFontCss(fontFamily,
- org.axsl.font.Font.Style.NORMAL,
- org.axsl.font.Font.Weight.NORMAL,
- org.axsl.font.Font.Variant.NORMAL,
- org.axsl.font.Font.Stretch.NORMAL, 0, ch);
- if (font == null) {
- font = this.fontConsumer.selectFontFallback();
- }
- final float size = fontSize.floatValue();
- if ((font != this.getGraphicsState().font)
- || (size != this.getGraphicsState().fontSize)) {
- this.getGraphicsState().font = font;
- this.getGraphicsState().fontSize = size;
- final String fontName = this.pdfContext.getFontName(font);
- this.write("/" + fontName + " " + size + " Tf");
- }
-
- final String matrixString = octalMatrixToString(vals);
- this.write(matrixString + " Tm (" + ch + ") Tj");
- }
-
- this.write("ET");
- }
-
- @Override
- public void fill(final Shape s) {
- Color c;
- c = getBackground();
- if (c.getAlpha() == 0) {
- c = getColor();
- if (c.getAlpha() == 0) {
- return;
- }
- }
- this.saveGraphicsState();
- final Shape imclip = getClip();
- writeClip(imclip);
- c = getColor();
- applyColor(c, true);
- c = getBackground();
- applyColor(c, false);
-
- applyPaint(getPaint(), true);
-
- final PathIterator iter = s.getPathIterator(getTransform());
- while (!iter.isDone()) {
- final double vals[] =
- new double[PsUtil.MATRIX_QTY_ELEMENTS];
- final int type = iter.currentSegment(vals);
- String matrixString = null;
- switch (type) {
- case PathIterator.SEG_CUBICTO:
- matrixString = matrixToString(vals,
- PdfGraphics2D.QTY_ELEMENTS_CUBIC);
- this.write(matrixString + "c");
- break;
- case PathIterator.SEG_LINETO:
- matrixString = matrixToString(vals,
- PdfGraphics2D.QTY_ELEMENTS_LINEAR);
- this.write(matrixString + "l");
- break;
- case PathIterator.SEG_MOVETO:
- matrixString = matrixToString(vals,
- PdfGraphics2D.QTY_ELEMENTS_LINEAR);
- this.write(matrixString + "m");
- break;
- case PathIterator.SEG_QUADTO:
- matrixString = matrixToString(vals,
- PdfGraphics2D.QTY_ELEMENTS_QUAD);
- this.write(matrixString + "y");
- break;
- case PathIterator.SEG_CLOSE:
- this.write("h");
- break;
- default:
- break;
- }
- iter.next();
- }
- doDrawing(true, false,
- iter.getWindingRule() == PathIterator.WIND_EVEN_ODD);
- this.restoreGraphicsState();
- }
-
- /**
- * Add the commands to do the actual drawing.
- * @param fill Set to true if the item should be filled.
- * @param stroke Set to true if the item should be stroked.
- * @param nonzero Set to true to use the nonzero winding number rule.
- */
- protected void doDrawing(final boolean fill, final boolean stroke,
- final boolean nonzero) {
- if (fill) {
- if (stroke) {
- if (nonzero) {
- this.write("B*");
- } else {
- this.write("B");
- }
- } else {
- if (nonzero) {
- this.write("f*");
- } else {
- this.write("f");
- }
- }
- } else {
- // if(stroke)
- this.write("S");
- }
- }
-
- @Override
- public GraphicsConfiguration getDeviceConfiguration() {
- return new PdfGraphicsConfiguration();
- }
-
- @Override
- public FontMetrics getFontMetrics(final Font f) {
- return this.fmg.getFontMetrics(f);
- }
-
- @Override
- public void setXORMode(final Color c1) {
- }
-
-
- @Override
- public void copyArea(final int x, final int y, final int width, final int height, final int dx, final int dy) {
- }
-
- /**
- * Writes some content.
- * @param text The content to be written.
- */
- private void write(final CharSequence text) {
- try {
- this.currentStream.write(text);
- } catch (final IOException e) {
- this.ioErrorFound = true;
- }
- }
-
- /**
- * Writes some content.
- * @param text The content to be written.
- */
- private void comment(final String text) {
- try {
- this.currentStream.comment(text);
- } catch (final IOException e) {
- this.ioErrorFound = true;
- }
- }
-
- /**
- * Indicates whether any I/O errors were found while writing to the output
- * stream.
- * @return True iff there were I/O errors while writing to the output
- * stream.
- */
- public boolean ioErrorFound() {
- return this.ioErrorFound;
- }
-
- /**
- * Returns the current graphics state.
- * @return The current graphics state.
- */
- private PdfGraphicsState getGraphicsState() {
- return this.graphicsStateStack.peek();
- }
-
- /**
- * Saves the current PDF graphics state by pushing it onto the stack,
- * copying it, and making the copy the current graphics state.
- */
- private void saveGraphicsState() {
- this.write("q");
- /* Create a new state with the same settings as the current state. */
- final PdfGraphicsState newState = new PdfGraphicsState(
- this.getGraphicsState());
- /* Push it onto the stack. */
- this.graphicsStateStack.push(newState);
- }
-
- /**
- * Restores the previously-saved PDF graphics state by popping the current
- * one off of the stack.
- */
- private void restoreGraphicsState() {
- this.write("Q");
- this.graphicsStateStack.pop();
- }
-
-}
Deleted: trunk/foray/foray-graphic/src/main/java/org/foray/graphic/batik/PdfGraphicsConfiguration.java
===================================================================
--- trunk/foray/foray-graphic/src/main/java/org/foray/graphic/batik/PdfGraphicsConfiguration.java 2021-01-19 03:10:34 UTC (rev 11830)
+++ trunk/foray/foray-graphic/src/main/java/org/foray/graphic/batik/PdfGraphicsConfiguration.java 2021-01-19 13:09:03 UTC (rev 11831)
@@ -1,139 +0,0 @@
-/*
- * Copyright 2004 The FOray Project.
- * http://www.foray.org
- *
- * Licensed under the Apache License, Version 2.0 (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.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- *
- * This work is in part derived from the following work(s), used with the
- * permission of the licensor:
- * Apache FOP, licensed by the Apache Software Foundation
- *
- */
-
-/*
- * $LastChangedRevision$
- * $LastChangedDate$
- * $LastChangedBy$
- */
-
-package org.foray.graphic.batik;
-
-import java.awt.GraphicsConfiguration;
-import java.awt.GraphicsDevice;
-import java.awt.Rectangle;
-import java.awt.Transparency;
-import java.awt.geom.AffineTransform;
-import java.awt.image.BufferedImage;
-import java.awt.image.ColorModel;
-import java.awt.image.VolatileImage;
-
-/**
- * PDF-specific implementation of {@link GraphicsConfiguration}, which describes
- * the characteristics of a graphics destination such as a printer or monitor,
- * or, in this case, a PDF document.
- */
-class PdfGraphicsConfiguration extends GraphicsConfiguration {
-
- /** Static item used to get an appropriate colormodel. */
- private static final BufferedImage BI_WITH_ALPHA =
- new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB);
-
- /** Static item used to get an appropriate colormodel. */
- private static final BufferedImage BI_WITHOUT_ALPHA =
- new BufferedImage(1, 1, BufferedImage.TYPE_INT_RGB);
-
- /**
- * {@inheritDoc}
- * Constructs a buffered image with an alpha channel, unless
- * transparencty is OPAQUE (no alpha at all).
- */
- @Override
- public BufferedImage createCompatibleImage(final int width,
- final int height, final int transparency) {
- if (transparency == Transparency.OPAQUE) {
- return new BufferedImage(width, height,
- BufferedImage.TYPE_INT_RGB);
- }
- return new BufferedImage(width, height,
- BufferedImage.TYPE_INT_ARGB);
- }
-
- /**
- * {@inheritDoc}
- * Constructs a buffered image with an alpha channel.
- */
- @Override
- public BufferedImage createCompatibleImage(final int width,
- final int height) {
- return new BufferedImage(width, height,
- BufferedImage.TYPE_INT_ARGB);
- }
-
- @Override
- public VolatileImage createCompatibleVolatileImage(final int width,
- final int height) {
- /* Stub implementation to get Java 5 to compile.
- * TODO: This needs to be implemented properly. */
- return null;
- }
-
- @Override
- public VolatileImage createCompatibleVolatileImage(final int width,
- final int height, final int transparency) {
- /** Stub implementation to get Java 5 to compile.
- * TODO: This needs to be implemented properly. */
- return null;
- }
-
- @Override
- public Rectangle getBounds() {
- /* FIXX ME: This should return the page bounds in Pts,
- * I couldn't figure out how to get this for the current
- * page from the PDFDocument (this still works for now,
- * but it should be fixed...). */
- return null;
- }
-
- @Override
- public ColorModel getColorModel() {
- return PdfGraphicsConfiguration.BI_WITH_ALPHA.getColorModel();
- }
-
- @Override
- public ColorModel getColorModel(final int transparency) {
- if (transparency == Transparency.OPAQUE) {
- return PdfGraphicsConfiguration.BI_WITHOUT_ALPHA.getColorModel();
- }
- return PdfGraphicsConfiguration.BI_WITH_ALPHA.getColorModel();
- }
-
- /**
- * {@inheritDoc}
- * This implementation returns the default transform (1:1).
- */
- @Override
- public AffineTransform getDefaultTransform() {
- return new AffineTransform();
- }
-
- @Override
- public AffineTransform getNormalizingTransform() {
- return new AffineTransform(2, 0, 0, 2, 0, 0);
- }
-
- @Override
- public GraphicsDevice getDevice() {
- return new PdfGraphicsDevice(this);
- }
-
-}
Deleted: trunk/foray/foray-graphic/src/main/java/org/foray/graphic/batik/PdfGraphicsDevice.java
===================================================================
--- trunk/foray/foray-graphic/src/main/java/org/foray/graphic/batik/PdfGraphicsDevice.java 2021-01-19 03:10:34 UTC (rev 11830)
+++ trunk/foray/foray-graphic/src/main/java/org/foray/graphic/batik/PdfGraphicsDevice.java 2021-01-19 13:09:03 UTC (rev 11831)
@@ -1,95 +0,0 @@
-/*
- * Copyright 2004 The FOray Project.
- * http://www.foray.org
- *
- * Licensed under the Apache License, Version 2.0 (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.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- *
- * This work is in part derived from the following work(s), used with the
- * permission of the licensor:
- * Apache FOP, licensed by the Apache Software Foundation
- *
- */
-
-/*
- * $LastChangedRevision$
- * $LastChangedDate$
- * $LastChangedBy$
- */
-
-package org.foray.graphic.batik;
-
-import java.awt.GraphicsConfigTemplate;
-import java.awt.GraphicsConfiguration;
-import java.awt.GraphicsDevice;
-
-/**
- * This implements the GraphicsDevice interface as appropriate for
- * a PdfGraphics2D. This is quite simple since we only have one
- * GraphicsConfiguration for now.
- */
-class PdfGraphicsDevice extends GraphicsDevice {
-
- /** The "parent" Graphics Config. */
- private GraphicsConfiguration gc;
-
- /** The array of possible Graphics Configs with which this device is
- * associated. */
- private GraphicsConfiguration[] configurations;
-
- /**
- * Create a new PDF graphics device.
- * @param gc The "parent" Graphics Config.
- */
- PdfGraphicsDevice(final PdfGraphicsConfiguration gc) {
- this.gc = gc;
- }
-
- /**
- * {@inheritDoc}
- * Override the superclass and return the only config we have.
- */
- @Override
- public GraphicsConfiguration getBestConfiguration(
- final GraphicsConfigTemplate gct) {
- return this.gc;
- }
-
- @Override
- public GraphicsConfiguration[] getConfigurations() {
- /* We have only one config, so return an array containing it. */
- if (this.configurations == null) {
- this.configurations = new GraphicsConfiguration[] {this.gc};
- }
- return this.configurations;
- }
-
- @Override
- public GraphicsConfiguration getDefaultConfiguration() {
- return this.gc;
- }
-
- @Override
- public String getIDstring() {
- return toString();
- }
-
- /**
- * {@inheritDoc}
- * This implementation reports itself as a "printer".
- */
- @Override
- public int getType() {
- return GraphicsDevice.TYPE_PRINTER;
- }
-
-}
Deleted: trunk/foray/foray-graphic/src/main/java/org/foray/graphic/batik/PdfTextPainter.java
===================================================================
--- trunk/foray/foray-graphic/src/main/java/org/foray/graphic/batik/PdfTextPainter.java 2021-01-19 03:10:34 UTC (rev 11830)
+++ trunk/foray/foray-graphic/src/main/java/org/foray/graphic/batik/PdfTextPainter.java 2021-01-19 13:09:03 UTC (rev 11831)
@@ -1,414 +0,0 @@
-/*
- * Copyright 2004 The FOray Project.
- * http://www.foray.org
- *
- * Licensed under the Apache License, Version 2.0 (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.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- *
- * This work is in part derived from the following work(s), used with the
- * permission of the licensor:
- * Apache FOP, licensed by the Apache Software Foundation
- *
- */
-
-/*
- * $LastChangedRevision$
- * $LastChangedDate$
- * $LastChangedBy$
- */
-
-/*
- * Known contributors:
- * Original Author: <a href="mailto:ke...@af...">Keiron Liddle</a>
- */
-
-package org.foray.graphic.batik;
-
-import org.foray.common.WellKnownConstants;
-import org.foray.common.i18n.Orthography4a;
-
-import org.axsl.font.Font;
-import org.axsl.font.FontConsumer;
-import org.axsl.font.FontException;
-import org.axsl.font.FontOptions;
-import org.axsl.font.FontUtility;
-
-import org.apache.batik.bridge.Mark;
-import org.apache.batik.bridge.StrokingTextPainter;
-import org.apache.batik.bridge.TextNode;
-import org.apache.batik.bridge.TextPainter;
-import org.apache.batik.gvt.font.GVTFontFamily;
-import org.apache.batik.gvt.text.GVTAttributedCharacterIterator;
-import org.apache.batik.gvt.text.TextPaintInfo;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-import java.awt.Color;
-import java.awt.Graphics2D;
-import java.awt.Paint;
-import java.awt.Shape;
-import java.awt.Stroke;
-import java.awt.font.TextAttribute;
-import java.awt.geom.Point2D;
-import java.awt.geom.Rectangle2D;
-import java.text.AttributedCharacterIterator;
-import java.text.CharacterIterator;
-import java.util.Arrays;
-import java.util.Iterator;
-import java.util.List;
-
-/**
- * Implementation of the Batik {@link TextPainter} that handles the drawing of
- * text for SVG.
- */
-public class PdfTextPainter implements TextPainter {
-
- /** A stroking text painter to get the bounds and shape, which can also be
- * used as a fallback to draw the string with strokes. */
- private static final TextPainter PROXY_PAINTER =
- StrokingTextPainter.getInstance();
-
- /** The FontConsumer that should be used for font resolution. */
- private FontConsumer fontConsumer;
-
- /** The font family name used by this text. */
- private String fontFamily = null;
-
- /** Lazily-loaded logger. Use {@link #getLogger()} to obtain the instance. */
- private Logger logger;
-
- /**
- * Constructor.
- * @param consumer The FontConsumer that should be used for font resolution.
- */
- public PdfTextPainter(final FontConsumer consumer) {
- this.fontConsumer = consumer;
- }
-
- @Override
- public void paint(final TextNode node, final Graphics2D g2d) {
- final Point2D loc = node.getLocation();
- /*
-
- AttributedCharacterIterator aci =
- node.getAttributedCharacterIterator();
- if (aci.getBeginIndex() == aci.getEndIndex()) {
- return;
- }
- // reset position to start of char iterator
- char ch = aci.first();
- if (ch == AttributedCharacterIterator.DONE) {
- return;
- }
- */
- final TextNode.Anchor anchor = (TextNode.Anchor) node
- .getAttributedCharacterIterator()
- .getAttribute(GVTAttributedCharacterIterator
- .TextAttribute.ANCHOR_TYPE);
- if (anchor != null) {
- }
- /*
- printAttrs(node.getAttributedCharacterIterator());
- */
- paintTextRuns(node.getTextRuns(), g2d, loc);
-
- }
-
- /**
- * Paints a set of text runs.
- * @param textRuns The text runs to paint.
- * @param g2d The graphics environment.
- * @param loc The location at which the text items should be painted.
- */
- protected void paintTextRuns(final List<?> textRuns, final Graphics2D g2d,
- final Point2D loc) {
- Point2D currentloc = loc;
- final Iterator<?> i = textRuns.iterator();
- while (i.hasNext()) {
- final StrokingTextPainter.TextRun run =
- (StrokingTextPainter.TextRun) i.next();
- currentloc = paintTextRun(run, g2d, currentloc);
- }
- }
-
- /**
- * Paints a text runs.
- * @param run The text run to paint.
- * @param g2d The graphics environment.
- * @param loc The location at which the text item should be painted.
- * @return The ending point.
- */
- protected Point2D paintTextRun(final StrokingTextPainter.TextRun run,
- final Graphics2D g2d, final Point2D loc) {
- final AttributedCharacterIterator aci = run.getACI();
- return paintACI(aci, g2d, loc);
- }
-
- /**
- * Converts the text in an AttributedCharacterIterator into a String.
- * @param aci The iterator containing the needed text.
- * @return The iterator text as a String.
- */
- protected String getText(final AttributedCharacterIterator aci) {
- final StringBuilder sb = new StringBuilder(aci.getEndIndex()
- - aci.getBeginIndex());
- for (char c = aci.first(); c != CharacterIterator.DONE;
- c = aci.next()) {
- sb.append(c);
- }
- return sb.toString();
- }
-
- /**
- * Paints an AttributedCharacterIterator.
- * @param aci The iterator whose content should be painted.
- * @param g2d The graphics environment.
- * @param loc The location at which the text should be painted.
- * @return The ending point.
- */
- protected Point2D paintACI(final AttributedCharacterIterator aci, final Graphics2D g2d, final Point2D loc) {
- aci.first();
-
- final TextNode.Anchor anchor = (TextNode.Anchor) aci.getAttribute(
- GVTAttributedCharacterIterator.TextAttribute.ANCHOR_TYPE);
-
- //Adjust position of span
- final Float xpos = (Float) aci.getAttribute(
- GVTAttributedCharacterIterator.TextAttribute.X);
- final Float ypos = (Float) aci.getAttribute(
- GVTAttributedCharacterIterator.TextAttribute.Y);
- final Float dxpos = (Float) aci.getAttribute(
- GVTAttributedCharacterIterator.TextAttribute.DX);
- final Float dypos = (Float) aci.getAttribute(
- GVTAttributedCharacterIterator.TextAttribute.DY);
- if (xpos != null) {
- loc.setLocation(xpos.doubleValue(), loc.getY());
- }
- if (ypos != null) {
- loc.setLocation(loc.getX(), ypos.doubleValue());
- }
- if (dxpos != null) {
- loc.setLocation(loc.getX() + dxpos.doubleValue(), loc.getY());
- }
- if (dypos != null) {
- loc.setLocation(loc.getX(), loc.getY() + dypos.doubleValue());
- }
-
-
- /* Get and set the font size. */
- final Float taFontSize = (Float) aci.getAttribute(TextAttribute.SIZE);
- if (taFontSize == null) {
- return loc;
- }
-
- /* Get and set the colors. */
- final TextPaintInfo tpi = (TextPaintInfo) aci.getAttribute(
- GVTAttributedCharacterIterator.TextAttribute.PAINT_INFO);
- if (tpi == null) {
- return loc;
- }
- final Paint forg = tpi.fillPaint;
- if (forg instanceof Color) {
- final Color col = (Color) forg;
- g2d.setColor(col);
- }
- g2d.setPaint(forg);
-
- /* Get and set the stroke. */
- final Stroke stroke = tpi.strokeStroke;
- g2d.setStroke(stroke);
-
- /*
- * There are at least four sets of measurements being used here for
- * font-size, font-weight, and font-style: 1) the java.awt.Font
- * nomenclature, 2) the org.foray.font.Font nomenclature, 3) the
- * FOray FOTree nomenclature, and 4) the Batik TextAttribute
- * nomenclature. The Batik may be related to the AWT.
- * To try to distinguish them, "awt", "fo", and "ta" prefixes are added
- * to local variable names.
- */
- final Float taPosture = (Float) aci.getAttribute(TextAttribute.POSTURE);
- final Float taWeight = (Float) aci.getAttribute(TextAttribute.WEIGHT);
-
- boolean isItalic = false;
- boolean isBold = false;
- int awtFontStyle = java.awt.Font.PLAIN;
- if ((taPosture != null) && (taPosture.floatValue() > 0.0)) {
- awtFontStyle |= java.awt.Font.ITALIC;
- isItalic = true;
- }
- if ((taWeight != null) && (taWeight.floatValue() > 1.0)) {
- awtFontStyle |= java.awt.Font.BOLD;
- isBold = true;
- }
-
- org.axsl.font.FontUse fontToUse = null;
-
- /*
- * TODO: Since an awt font is required here, it may be much better
- * to create the best awt font that can be created, and then enhance
- * the ForayFont system to allow a Foray Font to be created from the
- * awt font. It seems like the struggle here is to try to make sure
- * that the two are similar.
- */
- try {
- fontToUse = getFontToUse(aci, g2d, taFontSize, isItalic, isBold);
- } catch (final FontException e) {
- getLogger().error(e.getMessage());
- }
-
- final String awtFontFamily = this.fontFamily;
- final int awtFontSize = Math.round(taFontSize.floatValue());
- final java.awt.Font awtFont = new java.awt.Font(awtFontFamily,
- awtFontStyle, awtFontSize);
- g2d.setFont(awtFont);
-
- //Get text and paint
- final String txt = getText(aci);
- fontToUse.registerCharsUsed(txt);
- /* TODO: If letter-spacing is used, it needs to come from the SVG. */
- final int letterSpacing = 0;
- /* TODO: Pass the font options and orthography below instead of hard-coding them. */
- final int width = fontToUse.width(txt, 0, txt.length(),
- awtFontSize * WellKnownConstants.MILLIPOINTS_PER_POINT, letterSpacing, 0, FontOptions.DEFAULT,
- Orthography4a.USA);
- final float advance = WellKnownConstants.millipointsToPoints(width);
- float tx = 0;
- if (anchor != null) {
- switch (anchor.getType()) {
- case TextNode.Anchor.ANCHOR_MIDDLE:
- tx = -advance / 2;
- break;
- case TextNode.Anchor.ANCHOR_END:
- tx = -advance;
- }
- }
- g2d.drawString(txt, (float) (loc.getX() + tx), (float) loc.getY());
- loc.setLocation(loc.getX() + advance, loc.getY());
- return loc;
- }
-
- /**
- * Returns the font to use.
- * @param aci The attributed character iterator.
- * @param g2d The graphics 2D.
- * @param size The size, in points, of the text to be written.
- * @param italic Set to true if the font should be italic.
- * @param bold Set to true if the font should be bold.
- * @return The axsl font, or null if one cannot be matched.
- * @throws FontException For errors during font selection.
- */
- private org.axsl.font.FontUse getFontToUse(
- final AttributedCharacterIterator aci, final Graphics2D g2d,
- final Float size, final boolean italic, final boolean bold)
- throws FontException {
- Font.Style style = Font.Style.NORMAL;
- if (italic) {
- style = Font.Style.ITALIC;
- }
- org.axsl.font.Font.Weight weight
- = org.axsl.font.Font.Weight.NORMAL;
- if (bold) {
- weight = org.axsl.font.Font.Weight.BOLD;
- }
- final List<?> gvtFonts = (List<?>) aci.getAttribute(
- GVTAttributedCharacterIterator.TextAttribute.GVT_FONT_FAMILIES);
-
- final int fsize = (int) size.floatValue();
- if (gvtFonts != null) {
- for (final Iterator<?> i = gvtFonts.iterator(); i.hasNext(); ) {
- final GVTFontFamily fam = (GVTFontFamily) i.next();
- this.fontFamily = fam.getFamilyName();
- final org.axsl.font.FontUse font = this.fontConsumer.selectFontCss(
- FontUtility.foFontFamily(this.fontFamily),
- style, weight, Font.Variant.NORMAL,
- org.axsl.font.Font.Stretch.NORMAL,
- fsize * WellKnownConstants.MILLIPOINTS_PER_POINT,
- ' ');
- if (font != null) {
- if (g2d instanceof PdfGraphics2D) {
- ((PdfGraphics2D) g2d).setOverrideFontState(font, fsize);
- }
- return font;
- }
- }
- }
- this.fontFamily = "any";
- return this.fontConsumer.selectFontXsl(
- Font.SelectionStrategy.AUTO,
- Arrays.asList(new String[] {"any"}),
- style,
- weight,
- Font.Variant.NORMAL,
- org.axsl.font.Font.Stretch.NORMAL,
- fsize * WellKnownConstants.MILLIPOINTS_PER_POINT,
- ' ');
- }
-
- @Override
- public Mark getMark(final TextNode node, final int pos, final boolean all) {
- return null;
- }
-
- @Override
- public Mark selectAt(final double x, final double y, final TextNode node) {
- return null;
- }
-
- @Override
- public Mark selectTo(final double x, final double y, final Mark beginMark) {
- return null;
- }
-
- @Override
- public Mark selectFirst(final TextNode node) {
- return null;
- }
-
- @Override
- public Mark selectLast(final TextNode node) {
- return null;
- }
-
- @Override
- public int[] getSelected(final Mark start, final Mark finish) {
- return null;
- }
-
- @Override
- public Shape getHighlightShape(final Mark beginMark, final Mark endMark) {
- return null;
- }
-
- @Override
- public Rectangle2D getBounds2D(final TextNode node) {
- return PdfTextPainter.PROXY_PAINTER.getBounds2D(node);
- }
-
- @Override
- public Rectangle2D getGeometryBounds(final TextNode node) {
- return PdfTextPainter.PROXY_PAINTER.getGeometryBounds(node);
- }
-
- @Override
- public Shape getOutline(final TextNode node) {
- return PdfTextPainter.PROXY_PAINTER.getOutline(node);
- }
-
- private Logger getLogger() {
- if (this.logger == null) {
- this.logger = LoggerFactory.getLogger(PdfTextPainter.class);
- }
- return this.logger;
- }
-
-}
Copied: trunk/foray/foray-graphic/src/main/java/org/foray/graphic/batik/PdfTextPainter.java (from rev 11829, trunk/foray/foray-graphic/src/main/java/org/foray/graphic/batik/PdfTextPainter.java)
===================================================================
--- trunk/foray/foray-graphic/src/main/java/or...
[truncated message content] |