*/
public class FontTriplet implements Comparable<FontTriplet>, Serializable {
+ public static final FontTriplet DEFAULT_FONT_TRIPLET = new FontTriplet("any", Font.STYLE_NORMAL, Font.WEIGHT_NORMAL);
+
/** serial version UID */
private static final long serialVersionUID = 1168991106658033508L;
*/
boolean matches(FontTriplet triplet);
}
-
}
String key = fontInfo.getInternalFontKey(triplet);
if (key == null) {
//Find a default fallback font as last resort
- triplet = new FontTriplet("any", Font.STYLE_NORMAL, Font.WEIGHT_NORMAL);
+ triplet = FontTriplet.DEFAULT_FONT_TRIPLET;
key = fontInfo.getInternalFontKey(triplet);
}
return key;
import org.apache.fop.render.intermediate.IFPainter;
/**
- * {@link org.apache.fop.render.intermediate.IFDocumentHandler} implementation that
- * produces AFP (MO:DCA).
+ * {@link org.apache.fop.render.intermediate.IFDocumentHandler} implementation that produces AFP
+ * (MO:DCA).
*/
public class AFPDocumentHandler extends AbstractBinaryWritingIFDocumentHandler
implements AFPCustomizable {
import org.apache.xmlgraphics.image.loader.ImageSessionContext;
import org.apache.fop.afp.AFPBorderPainter;
+import org.apache.fop.afp.AFPEventProducer;
import org.apache.fop.afp.AFPPaintingState;
import org.apache.fop.afp.AFPUnitConverter;
import org.apache.fop.afp.AbstractAFPPainter;
import org.apache.fop.afp.util.DefaultFOPResourceAccessor;
import org.apache.fop.afp.util.ResourceAccessor;
import org.apache.fop.fonts.Font;
-import org.apache.fop.fonts.FontInfo;
import org.apache.fop.fonts.FontTriplet;
import org.apache.fop.fonts.Typeface;
import org.apache.fop.render.RenderingContext;
import org.apache.fop.render.intermediate.AbstractIFPainter;
import org.apache.fop.render.intermediate.BorderPainter;
-import org.apache.fop.render.intermediate.IFContext;
import org.apache.fop.render.intermediate.IFException;
import org.apache.fop.render.intermediate.IFState;
import org.apache.fop.render.intermediate.IFUtil;
/**
* IFPainter implementation that produces AFP (MO:DCA).
*/
-public class AFPPainter extends AbstractIFPainter {
+public class AFPPainter extends AbstractIFPainter<AFPDocumentHandler> {
//** logging instance */
//private static Log log = LogFactory.getLog(AFPPainter.class);
private static final int X = 0;
private static final int Y = 1;
- private final AFPDocumentHandler documentHandler;
-
/** the border painter */
private final AFPBorderPainterAdapter borderPainter;
/** the rectangle painter */
/** unit converter */
private final AFPUnitConverter unitConv;
+ private final AFPEventProducer eventProducer;
+
/**
* Default constructor.
* @param documentHandler the parent document handler
*/
public AFPPainter(AFPDocumentHandler documentHandler) {
- super();
- this.documentHandler = documentHandler;
+ super(documentHandler);
this.state = IFState.create();
this.borderPainter = new AFPBorderPainterAdapter(
new AFPBorderPainter(getPaintingState(), getDataStream()));
this.rectanglePainter = documentHandler.createRectanglePainter();
this.unitConv = getPaintingState().getUnitConverter();
+ this.eventProducer = AFPEventProducer.Provider.get(getUserAgent().getEventBroadcaster());
}
- /** {@inheritDoc} */
- @Override
- protected IFContext getContext() {
- return this.documentHandler.getContext();
- }
-
- FontInfo getFontInfo() {
- return this.documentHandler.getFontInfo();
+ private AFPPaintingState getPaintingState() {
+ return getDocumentHandler().getPaintingState();
}
- AFPPaintingState getPaintingState() {
- return this.documentHandler.getPaintingState();
+ private DataStream getDataStream() {
+ return getDocumentHandler().getDataStream();
}
- DataStream getDataStream() {
- return this.documentHandler.getDataStream();
+ @Override
+ public String getFontKey(FontTriplet triplet) throws IFException {
+ try {
+ return super.getFontKey(triplet);
+ } catch (IFException e) {
+ eventProducer.invalidConfiguration(null, e);
+ return super.getFontKey(FontTriplet.DEFAULT_FONT_TRIPLET);
+ }
}
/** {@inheritDoc} */
protected RenderingContext createRenderingContext() {
AFPRenderingContext psContext = new AFPRenderingContext(
getUserAgent(),
- documentHandler.getResourceManager(),
+ getDocumentHandler().getResourceManager(),
getPaintingState(),
getFontInfo(),
getContext().getForeignAttributes());
/** {@inheritDoc} */
public void drawImage(String uri, Rectangle rect) throws IFException {
- PageSegmentDescriptor pageSegment = documentHandler.getPageSegmentNameFor(uri);
+ PageSegmentDescriptor pageSegment = getDocumentHandler().getPageSegmentNameFor(uri);
if (pageSegment != null) {
float[] srcPts = {rect.x, rect.y};
//Do we need to embed an external page segment?
if (pageSegment.getURI() != null) {
ResourceAccessor accessor = new DefaultFOPResourceAccessor (
- documentHandler.getUserAgent(), null, null);
+ getUserAgent(), null, null);
try {
URI resourceUri = new URI(pageSegment.getURI());
- documentHandler.getResourceManager().createIncludedResourceFromExternal(
+ getDocumentHandler().getResourceManager().createIncludedResourceFromExternal(
pageSegment.getName(), resourceUri, accessor);
} catch (URISyntaxException urie) {
FontTriplet triplet = new FontTriplet(
state.getFontFamily(), state.getFontStyle(), state.getFontWeight());
//TODO Ignored: state.getFontVariant()
- String fontKey = getFontInfo().getInternalFontKey(triplet);
- if (fontKey == null) {
- triplet = new FontTriplet("any", Font.STYLE_NORMAL, Font.WEIGHT_NORMAL);
- fontKey = getFontInfo().getInternalFontKey(triplet);
- }
+ String fontKey = getFontKey(triplet);
// register font as necessary
- Map<String, Typeface> fontMetricMap = documentHandler.getFontInfo().getFonts();
+ Map<String, Typeface> fontMetricMap = getFontInfo().getFonts();
final AFPFont afpFont = (AFPFont)fontMetricMap.get(fontKey);
final Font font = getFontInfo().getFontInstance(triplet, fontSize);
AFPPageFonts pageFonts = getPaintingState().getPageFonts();
if (afpFont.isEmbeddable()) {
try {
- documentHandler.getResourceManager().embedFont(afpFont, charSet);
+ getDocumentHandler().getResourceManager().embedFont(afpFont, charSet);
} catch (IOException ioe) {
throw new IFException("Error while embedding font resources", ioe);
}
import org.apache.fop.apps.FOUserAgent;
import org.apache.fop.apps.FopFactory;
import org.apache.fop.fo.Constants;
+import org.apache.fop.fonts.FontInfo;
+import org.apache.fop.fonts.FontTriplet;
import org.apache.fop.render.ImageHandler;
import org.apache.fop.render.ImageHandlerRegistry;
import org.apache.fop.render.ImageHandlerUtil;
/**
* Abstract base class for IFPainter implementations.
*/
-public abstract class AbstractIFPainter implements IFPainter {
+public abstract class AbstractIFPainter<T extends IFDocumentHandler> implements IFPainter {
/** logging instance */
private static Log log = LogFactory.getLog(AbstractIFPainter.class);
/** Holds the intermediate format state */
protected IFState state;
+ private final T documentHandler;
/**
* Default constructor.
*/
- public AbstractIFPainter() {
+ public AbstractIFPainter(T documentHandler) {
+ this.documentHandler = documentHandler;
+ }
+
+ protected String getFontKey(FontTriplet triplet) throws IFException {
+ String key = getFontInfo().getInternalFontKey(triplet);
+ if (key == null) {
+ throw new IFException("The font triplet is not available: \"" + triplet + "\" "
+ + "for the MIME type: \"" + documentHandler.getMimeType() + "\"");
+ }
+ return key;
}
/**
* Returns the intermediate format context object.
* @return the context object
*/
- protected abstract IFContext getContext();
+ protected IFContext getContext() {
+ return documentHandler.getContext();
+ }
+
+ protected FontInfo getFontInfo() {
+ return documentHandler.getFontInfo();
+ }
+
+ protected T getDocumentHandler() {
+ return documentHandler;
+ }
/**
* Returns the user agent.
private static final long serialVersionUID = 0L;
/**
- * Constructs a new exception with the specified detail message and
- * cause. <p>Note that the detail message associated with
- * <code>cause</code> is <i>not</i> automatically incorporated in
+ * Constructs a new exception with the specified detail message and cause. <p>Note that the
+ * detail message associated with <code>cause</code> is <i>not</i> automatically incorporated in
* this exception's detail message.
*
- * @param message the detail message (which is saved for later retrieval
- * by the {@link #getMessage()} method).
- * @param cause the cause (which is saved for later retrieval by the
- * {@link #getCause()} method). (A <code>null</code> value is
- * permitted, and indicates that the cause is nonexistent or
- * unknown.)
+ * @param message the detail message (which is saved for later retrieval by the
+ * {@link #getMessage()} method).
+ * @param cause the cause (which is saved for later retrieval by the {@link #getCause()}
+ * method). (A <code>null</code> value is permitted, and indicates that the cause is
+ * nonexistent or unknown.)
*/
public IFException(String message, Exception cause) {
super(message, cause);
}
+ /**
+ * Constructs a new exception with the specified detail message.
+ *
+ * @param message the detail message (which is saved for later retrieval by the
+ * {@link #getMessage()} method).
+ */
+ public IFException(String message) {
+ super(message);
+ }
}
--- /dev/null
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.
+ */
+
+package org.apache.fop.render.java2d;
+
+import java.awt.Dimension;
+import java.util.Locale;
+
+import javax.xml.transform.Result;
+
+import org.apache.fop.accessibility.StructureTreeEventHandler;
+import org.apache.fop.fonts.FontInfo;
+import org.apache.fop.render.intermediate.IFContext;
+import org.apache.fop.render.intermediate.IFDocumentHandler;
+import org.apache.fop.render.intermediate.IFDocumentHandlerConfigurator;
+import org.apache.fop.render.intermediate.IFDocumentNavigationHandler;
+import org.apache.fop.render.intermediate.IFException;
+import org.apache.fop.render.intermediate.IFPainter;
+
+/**
+ * {@link org.apache.fop.render.intermediate.IFDocumentHandler} implementation that produces
+ * Graphics2D instance.
+ */
+public class Java2DDocumentHandler implements IFDocumentHandler {
+
+ public void setContext(IFContext context) {
+ throw new UnsupportedOperationException();
+ }
+
+ public IFContext getContext() {
+ throw new UnsupportedOperationException();
+ }
+
+ public void setResult(Result result) throws IFException {
+ throw new UnsupportedOperationException();
+ }
+
+ public void setFontInfo(FontInfo fontInfo) {
+ throw new UnsupportedOperationException();
+ }
+
+ public FontInfo getFontInfo() {
+ throw new UnsupportedOperationException();
+ }
+
+ public void setDefaultFontInfo(FontInfo fontInfo) {
+ throw new UnsupportedOperationException();
+ }
+
+ public IFDocumentHandlerConfigurator getConfigurator() {
+ throw new UnsupportedOperationException();
+ }
+
+ public StructureTreeEventHandler getStructureTreeEventHandler() {
+ throw new UnsupportedOperationException();
+ }
+
+ public IFDocumentNavigationHandler getDocumentNavigationHandler() {
+ throw new UnsupportedOperationException();
+ }
+
+ public boolean supportsPagesOutOfOrder() {
+ throw new UnsupportedOperationException();
+ }
+
+ public String getMimeType() {
+ throw new UnsupportedOperationException();
+ }
+
+ public void startDocument() throws IFException {
+ throw new UnsupportedOperationException();
+ }
+
+ public void endDocument() throws IFException {
+ throw new UnsupportedOperationException();
+ }
+
+ public void setDocumentLocale(Locale locale) {
+ throw new UnsupportedOperationException();
+ }
+
+ public void startDocumentHeader() throws IFException {
+ throw new UnsupportedOperationException();
+ }
+
+ public void endDocumentHeader() throws IFException {
+ throw new UnsupportedOperationException();
+ }
+
+ public void startDocumentTrailer() throws IFException {
+ throw new UnsupportedOperationException();
+ }
+
+ public void endDocumentTrailer() throws IFException {
+ throw new UnsupportedOperationException();
+ }
+
+ public void startPageSequence(String id) throws IFException {
+ throw new UnsupportedOperationException();
+ }
+
+ public void endPageSequence() throws IFException {
+ throw new UnsupportedOperationException();
+ }
+
+ public void startPage(int index, String name, String pageMasterName, Dimension size)
+ throws IFException {
+ throw new UnsupportedOperationException();
+ }
+
+ public void endPage() throws IFException {
+ throw new UnsupportedOperationException();
+ }
+
+ public void startPageHeader() throws IFException {
+ throw new UnsupportedOperationException();
+ }
+
+ public void endPageHeader() throws IFException {
+ throw new UnsupportedOperationException();
+ }
+
+ public IFPainter startPageContent() throws IFException {
+ throw new UnsupportedOperationException();
+ }
+
+ public void endPageContent() throws IFException {
+ throw new UnsupportedOperationException();
+ }
+
+ public void startPageTrailer() throws IFException {
+ throw new UnsupportedOperationException();
+ }
+
+ public void endPageTrailer() throws IFException {
+ throw new UnsupportedOperationException();
+ }
+
+ public void handleExtensionObject(Object extension) throws IFException {
+ throw new UnsupportedOperationException();
+ }
+}
import org.apache.fop.util.CharUtilities;
/**
- * {@link org.apache.fop.render.intermediate.IFPainter} implementation
- * that paints on a Graphics2D instance.
+ * {@link org.apache.fop.render.intermediate.IFPainter} implementation that paints on a Graphics2D
+ * instance.
*/
-public class Java2DPainter extends AbstractIFPainter {
+public class Java2DPainter extends AbstractIFPainter<Java2DDocumentHandler> {
/** the IF context */
protected IFContext ifContext;
/** The current state, holds a Graphics2D and its context */
protected Java2DGraphicsState g2dState;
- private Stack g2dStateStack = new Stack();
+ private Stack<Java2DGraphicsState> g2dStateStack = new Stack<Java2DGraphicsState>();
/**
* Main constructor.
* @param state the IF state object
*/
public Java2DPainter(Graphics2D g2d, IFContext context, FontInfo fontInfo, IFState state) {
- super();
+ super(new Java2DDocumentHandler());
this.ifContext = context;
if (state != null) {
this.state = state.push();
/** Restores the last graphics state from the stack. */
protected void restoreGraphicsState() {
g2dState.dispose();
- g2dState = (Java2DGraphicsState)g2dStateStack.pop();
+ g2dState = g2dStateStack.pop();
}
private void concatenateTransformationMatrix(AffineTransform transform) throws IOException {
import org.apache.fop.render.ImageHandlerUtil;
import org.apache.fop.render.RenderingContext;
import org.apache.fop.render.intermediate.AbstractIFPainter;
-import org.apache.fop.render.intermediate.IFContext;
import org.apache.fop.render.intermediate.IFException;
import org.apache.fop.render.intermediate.IFState;
import org.apache.fop.render.intermediate.IFUtil;
import org.apache.fop.util.CharUtilities;
/**
- * {@link org.apache.fop.render.intermediate.IFPainter} implementation
- * that produces PCL 5.
+ * {@link org.apache.fop.render.intermediate.IFPainter} implementation that produces PCL 5.
*/
-public class PCLPainter extends AbstractIFPainter implements PCLConstants {
+public class PCLPainter extends AbstractIFPainter<PCLDocumentHandler> implements PCLConstants {
private static final boolean DEBUG = false;
- private PCLDocumentHandler parent;
-
/** The PCL generator */
private PCLGenerator gen;
private int currentPrintDirection = 0;
//private GeneralPath currentPath = null;
- private Stack graphicContextStack = new Stack();
+ private Stack<GraphicContext> graphicContextStack = new Stack<GraphicContext>();
private GraphicContext graphicContext = new GraphicContext();
/**
* @param pageDefinition the page definition describing the page to be rendered
*/
public PCLPainter(PCLDocumentHandler parent, PCLPageDefinition pageDefinition) {
- this.parent = parent;
+ super(parent);
this.gen = parent.getPCLGenerator();
this.state = IFState.create();
this.currentPageDefinition = pageDefinition;
}
- /** {@inheritDoc} */
- public IFContext getContext() {
- return this.parent.getContext();
- }
-
PCLRenderingUtil getPCLUtil() {
- return this.parent.getPCLUtil();
+ return getDocumentHandler().getPCLUtil();
}
/** @return the target resolution */
protected int getResolution() {
- int resolution = (int)Math.round(getUserAgent().getTargetResolution());
+ int resolution = Math.round(getUserAgent().getTargetResolution());
if (resolution <= 300) {
return 300;
} else {
g2d.translate(-rect.x, -rect.y);
Java2DPainter painter = new Java2DPainter(g2d,
- getContext(), parent.getFontInfo(), state);
+ getContext(), getFontInfo(), state);
try {
painter.drawBorderRect(rect, top, bottom, left, right);
} catch (IFException e) {
g2d.translate(-boundingBox.x, -boundingBox.y);
Java2DPainter painter = new Java2DPainter(g2d,
- getContext(), parent.getFontInfo(), state);
+ getContext(), getFontInfo(), state);
try {
painter.drawLine(start, end, width, color, style);
} catch (IFException e) {
state.getFontFamily(), state.getFontStyle(), state.getFontWeight());
//TODO Ignored: state.getFontVariant()
//TODO Opportunity for font caching if font state is more heavily used
- String fontKey = parent.getFontInfo().getInternalFontKey(triplet);
- boolean pclFont = getPCLUtil().isAllTextAsBitmaps()
- ? false
+ String fontKey = getFontKey(triplet);
+ boolean pclFont = getPCLUtil().isAllTextAsBitmaps() ? false
: HardcodedFonts.setFont(gen, fontKey, state.getFontSize(), text);
if (pclFont) {
drawTextNative(x, y, letterSpacing, wordSpacing, dp, text, triplet);
setCursorPos(x, y);
float fontSize = state.getFontSize() / 1000f;
- Font font = parent.getFontInfo().getFontInstance(triplet, state.getFontSize());
+ Font font = getFontInfo().getFontInstance(triplet, state.getFontSize());
int l = text.length();
int[] dx = IFUtil.convertDPToDX ( dp );
int dxl = (dx != null ? dx.length : 0);
final int letterSpacing, final int wordSpacing, final int[][] dp,
final String text, FontTriplet triplet) throws IFException {
//Use Java2D to paint different fonts via bitmap
- final Font font = parent.getFontInfo().getFontInstance(triplet, state.getFontSize());
+ final Font font = getFontInfo().getFontInstance(triplet, state.getFontSize());
//for cursive fonts, so the text isn't clipped
- final FontMetricsMapper mapper = (FontMetricsMapper)parent.getFontInfo().getMetricsFor(
+ final FontMetricsMapper mapper = (FontMetricsMapper) getFontInfo().getMetricsFor(
font.getFontName());
final int maxAscent = mapper.getMaxAscent(font.getFontSize()) / 1000;
final int ascent = mapper.getAscender(font.getFontSize()) / 1000;
rect = new Rectangle(x, y, 1000, -descent);
g2d.draw(rect);
}
- Java2DPainter painter = new Java2DPainter(g2d,
- getContext(), parent.getFontInfo(), state);
+ Java2DPainter painter = new Java2DPainter(g2d, getContext(), getFontInfo(), state);
try {
painter.drawText(x, y, letterSpacing, wordSpacing, dp, text);
} catch (IFException e) {
/** Restores the last graphics state from the stack. */
private void restoreGraphicsState() {
- graphicContext = (GraphicContext)graphicContextStack.pop();
+ graphicContext = graphicContextStack.pop();
}
private void concatenateTransformationMatrix(AffineTransform transform) throws IOException {
import org.apache.fop.pdf.PDFAnnotList;
import org.apache.fop.pdf.PDFDocument;
import org.apache.fop.pdf.PDFPage;
-import org.apache.fop.pdf.PDFResourceContext;
import org.apache.fop.pdf.PDFResources;
import org.apache.fop.render.extensions.prepress.PageBoundaries;
import org.apache.fop.render.extensions.prepress.PageScale;
private PDFStructureTreeBuilder structureTreeBuilder;
/** the PDF Document being created */
- protected PDFDocument pdfDoc;
+ private PDFDocument pdfDoc;
/**
* Utility class which enables all sorts of features that are not directly connected to the
* normal rendering process.
*/
- protected PDFRenderingUtil pdfUtil;
+ private PDFRenderingUtil pdfUtil;
/** the /Resources object of the PDF document being created */
- protected PDFResources pdfResources;
+ private PDFResources pdfResources;
/** The current content generator */
- protected PDFContentGenerator generator;
-
- /** the current annotation list to add annotations to */
- protected PDFResourceContext currentContext;
+ private PDFContentGenerator generator;
/** the current page to add annotations to */
- protected PDFPage currentPage;
+ private PDFPage currentPage;
/** the current page's PDF reference */
- protected PageReference currentPageRef;
+ private PageReference currentPageRef;
/** Used for bookmarks/outlines. */
- protected Map<Integer, PageReference> pageReferences = new HashMap<Integer, PageReference>();
+ private Map<Integer, PageReference> pageReferences = new HashMap<Integer, PageReference>();
private final PDFDocumentNavigationHandler documentNavigationHandler
= new PDFDocumentNavigationHandler(this);
return logicalStructureHandler;
}
+ PDFDocument getPDFDocument() {
+ return pdfDoc;
+ }
+
+ PDFPage getCurrentPage() {
+ return currentPage;
+ }
+
+ PageReference getCurrentPageRef() {
+ return currentPageRef;
+ }
+
+ PDFContentGenerator getGenerator() {
+ return generator;
+ }
+
/** {@inheritDoc} */
public void startDocument() throws IFException {
super.startDocument();
pdfResources = null;
this.generator = null;
- currentContext = null;
currentPage = null;
} catch (IOException ioe) {
throw new IFException("I/O error in endDocument()", ioe);
}
PDFDocument getPDFDoc() {
- return this.documentHandler.pdfDoc;
+ return this.documentHandler.getPDFDocument();
}
/** {@inheritDoc} */
/** {@inheritDoc} */
public void renderLink(Link link) throws IFException {
Rectangle targetRect = link.getTargetRect();
- int pageHeight = documentHandler.currentPageRef.getPageDimension().height;
+ int pageHeight = documentHandler.getCurrentPageRef().getPageDimension().height;
Rectangle2D targetRect2D = new Rectangle2D.Double(
targetRect.getMinX() / 1000.0,
(pageHeight - targetRect.getMinY() - targetRect.getHeight()) / 1000.0,
if (documentHandler.getUserAgent().isAccessibilityEnabled() && structure != null) {
documentHandler.getLogicalStructureHandler().addLinkContentItem(pdfLink, structure);
}
- documentHandler.currentPage.addAnnotation(pdfLink);
+ documentHandler.getCurrentPage().addAnnotation(pdfLink);
}
}
import org.w3c.dom.Document;
import org.apache.fop.fonts.Font;
-import org.apache.fop.fonts.FontInfo;
import org.apache.fop.fonts.FontTriplet;
import org.apache.fop.fonts.LazyFont;
import org.apache.fop.fonts.SingleByteFont;
import org.apache.fop.fonts.Typeface;
-import org.apache.fop.pdf.PDFDocument;
import org.apache.fop.pdf.PDFNumber;
import org.apache.fop.pdf.PDFStructElem;
import org.apache.fop.pdf.PDFTextUtil;
import org.apache.fop.pdf.PDFXObject;
import org.apache.fop.render.RenderingContext;
import org.apache.fop.render.intermediate.AbstractIFPainter;
-import org.apache.fop.render.intermediate.IFContext;
import org.apache.fop.render.intermediate.IFException;
import org.apache.fop.render.intermediate.IFState;
import org.apache.fop.render.intermediate.IFUtil;
/**
* IFPainter implementation that produces PDF.
*/
-public class PDFPainter extends AbstractIFPainter {
-
- private final PDFDocumentHandler documentHandler;
+public class PDFPainter extends AbstractIFPainter<PDFDocumentHandler> {
/** The current content generator */
protected PDFContentGenerator generator;
*/
public PDFPainter(PDFDocumentHandler documentHandler,
PDFLogicalStructureHandler logicalStructureHandler) {
- super();
- this.documentHandler = documentHandler;
+ super(documentHandler);
this.logicalStructureHandler = logicalStructureHandler;
- this.generator = documentHandler.generator;
+ this.generator = documentHandler.getGenerator();
this.borderPainter = new PDFBorderPainter(this.generator);
this.state = IFState.create();
accessEnabled = this.getUserAgent().isAccessibilityEnabled();
}
- /** {@inheritDoc} */
- @Override
- protected IFContext getContext() {
- return this.documentHandler.getContext();
- }
-
- PDFRenderingUtil getPDFUtil() {
- return this.documentHandler.pdfUtil;
- }
-
- PDFDocument getPDFDoc() {
- return this.documentHandler.pdfDoc;
- }
-
- FontInfo getFontInfo() {
- return this.documentHandler.getFontInfo();
- }
-
/** {@inheritDoc} */
public void startViewport(AffineTransform transform, Dimension size, Rectangle clipRect)
throws IFException {
/** {@inheritDoc} */
public void drawImage(String uri, Rectangle rect)
throws IFException {
- PDFXObject xobject = getPDFDoc().getXObject(uri);
+ PDFXObject xobject = getDocumentHandler().getPDFDocument().getXObject(uri);
if (xobject != null) {
if (accessEnabled) {
PDFStructElem structElem = (PDFStructElem) getContext().getStructureTreeElement();
@Override
protected RenderingContext createRenderingContext() {
PDFRenderingContext pdfContext = new PDFRenderingContext(
- getUserAgent(), generator, this.documentHandler.currentPage, getFontInfo());
+ getUserAgent(), generator, getDocumentHandler().getCurrentPage(), getFontInfo());
pdfContext.setMarkedContentInfo(imageMCI);
return pdfContext;
}
}
}
- private void drawTextWithDX ( int x, int y, String text, FontTriplet triplet,
- int letterSpacing, int wordSpacing, int[] dx ) {
-
+ private void drawTextWithDX(int x, int y, String text, FontTriplet triplet,
+ int letterSpacing, int wordSpacing, int[] dx) throws IFException {
//TODO Ignored: state.getFontVariant()
//TODO Opportunity for font caching if font state is more heavily used
- String fontKey = getFontInfo().getInternalFontKey(triplet);
+ String fontKey = getFontKey(triplet);
int sizeMillipoints = state.getFontSize();
float fontSize = sizeMillipoints / 1000f;
import org.apache.fop.apps.MimeConstants;
import org.apache.fop.render.intermediate.AbstractBinaryWritingIFDocumentHandler;
import org.apache.fop.render.intermediate.IFContext;
+import org.apache.fop.render.intermediate.IFDocumentHandler;
import org.apache.fop.render.intermediate.IFDocumentHandlerConfigurator;
import org.apache.fop.render.intermediate.IFException;
import org.apache.fop.render.intermediate.IFPainter;
* Utility class which enables all sorts of features that are not directly connected to the
* normal rendering process.
*/
- protected PSRenderingUtil psUtil;
+ private PSRenderingUtil psUtil;
/** The PostScript generator used to output the PostScript */
- protected PSGenerator gen;
+ PSGenerator gen;
/** the temporary file in case of two-pass processing */
private File tempFile;
return MimeConstants.MIME_POSTSCRIPT;
}
+ PSGenerator getGenerator() {
+ return gen;
+ }
+
/** {@inheritDoc} */
public void setContext(IFContext context) {
super.setContext(context);
import org.apache.xmlgraphics.ps.PSResource;
import org.apache.fop.fonts.Font;
-import org.apache.fop.fonts.FontInfo;
import org.apache.fop.fonts.FontTriplet;
import org.apache.fop.fonts.LazyFont;
import org.apache.fop.fonts.MultiByteFont;
import org.apache.fop.fonts.Typeface;
import org.apache.fop.render.RenderingContext;
import org.apache.fop.render.intermediate.AbstractIFPainter;
-import org.apache.fop.render.intermediate.IFContext;
import org.apache.fop.render.intermediate.IFException;
import org.apache.fop.render.intermediate.IFState;
import org.apache.fop.render.intermediate.IFUtil;
/**
* IFPainter implementation that produces PostScript.
*/
-public class PSPainter extends AbstractIFPainter {
+public class PSPainter extends AbstractIFPainter<PSDocumentHandler> {
/** logging instance */
private static Log log = LogFactory.getLog(PSPainter.class);
- private PSDocumentHandler documentHandler;
private PSBorderPainter borderPainter;
private boolean inTextMode = false;
}
protected PSPainter(PSDocumentHandler documentHandler, IFState state) {
- super();
- this.documentHandler = documentHandler;
- this.borderPainter = new PSBorderPainter(documentHandler.gen);
+ super(documentHandler);
+ this.borderPainter = new PSBorderPainter(getGenerator());
this.state = state;
}
- /** {@inheritDoc} */
- protected IFContext getContext() {
- return this.documentHandler.getContext();
- }
-
- PSRenderingUtil getPSUtil() {
- return this.documentHandler.psUtil;
- }
-
- FontInfo getFontInfo() {
- return this.documentHandler.getFontInfo();
- }
-
private PSGenerator getGenerator() {
- return this.documentHandler.gen;
+ return getDocumentHandler().getGenerator();
}
/** {@inheritDoc} */
/** {@inheritDoc} */
protected void drawImageUsingImageHandler(ImageInfo info, Rectangle rect)
throws ImageException, IOException {
- if (!getPSUtil().isOptimizeResources()
+ if (!getDocumentHandler().getPSUtil().isOptimizeResources()
|| PSImageUtils.isImageInlined(info,
(PSRenderingContext)createRenderingContext())) {
super.drawImageUsingImageHandler(info, rect);
log.debug("Image " + info + " is embedded as a form later");
}
//Don't load image at this time, just put a form placeholder in the stream
- PSResource form = documentHandler.getFormForImage(info.getOriginalURI());
+ PSResource form = getDocumentHandler().getFormForImage(info.getOriginalURI());
PSImageUtils.drawForm(form, info, rect, getGenerator());
}
}
if (top != null || bottom != null || left != null || right != null) {
try {
endTextObject();
- if (getPSUtil().getRenderingMode() == PSRenderingMode.SIZE
+ if (getDocumentHandler().getPSUtil().getRenderingMode() == PSRenderingMode.SIZE
&& hasOnlySolidBorders(top, bottom, left, right)) {
super.drawBorderRect(rect, top, bottom, left, right);
} else {
if (fontName == null) {
throw new NullPointerException("fontName must not be null");
}
- Typeface tf = (Typeface)getFontInfo().getFonts().get(fontName);
+ Typeface tf = getFontInfo().getFonts().get(fontName);
if (tf instanceof LazyFont) {
tf = ((LazyFont)tf).getRealFont();
}
state.getFontFamily(), state.getFontStyle(), state.getFontWeight());
//TODO Ignored: state.getFontVariant()
//TODO Opportunity for font caching if font state is more heavily used
- String fontKey = getFontInfo().getInternalFontKey(triplet);
- if (fontKey == null) {
- throw new IFException("Font not available: " + triplet, null);
- }
+ String fontKey = getFontKey(triplet);
int sizeMillipoints = state.getFontSize();
// This assumes that *all* CIDFonts use a /ToUnicode mapping
}
private void useFont(String key, int size) throws IOException {
- PSFontResource res = this.documentHandler.getPSResourceForFontKey(key);
+ PSFontResource res = getDocumentHandler().getPSResourceForFontKey(key);
PSGenerator generator = getGenerator();
generator.useFont("/" + res.getName(), size / 1000f);
res.notifyResourceUsageOnPage(generator.getResourceTracker());
}
-
}
import java.util.Map;
import org.w3c.dom.Document;
-
import org.xml.sax.SAXException;
import org.xml.sax.helpers.AttributesImpl;
import org.apache.fop.render.RenderingContext;
import org.apache.fop.render.intermediate.AbstractIFPainter;
import org.apache.fop.render.intermediate.IFConstants;
-import org.apache.fop.render.intermediate.IFContext;
import org.apache.fop.render.intermediate.IFException;
import org.apache.fop.render.intermediate.IFState;
import org.apache.fop.render.intermediate.IFUtil;
/**
* IFPainter implementation that writes SVG.
*/
-public class SVGPainter extends AbstractIFPainter implements SVGConstants {
-
- private AbstractSVGDocumentHandler parent;
+public class SVGPainter extends AbstractIFPainter<AbstractSVGDocumentHandler>
+ implements SVGConstants {
/** The SAX content handler that receives the generated XML events. */
- protected GenerationHelperContentHandler handler;
+ private GenerationHelperContentHandler handler;
private static final int MODE_NORMAL = 0;
private static final int MODE_TEXT = 1;
*/
public SVGPainter(AbstractSVGDocumentHandler parent,
GenerationHelperContentHandler contentHandler) {
- super();
- this.parent = parent;
+ super(parent);
this.handler = contentHandler;
this.state = IFState.create();
}
- /** {@inheritDoc} */
- protected IFContext getContext() {
- return parent.getContext();
- }
-
/** {@inheritDoc} */
public void startViewport(AffineTransform transform, Dimension size, Rectangle clipRect)
throws IFException {
--- /dev/null
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.
+ */
+
+package org.apache.fop.render.intermediate;
+
+import java.awt.Dimension;
+import java.awt.Paint;
+import java.awt.Rectangle;
+import java.awt.geom.AffineTransform;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.w3c.dom.Document;
+
+import org.apache.fop.fonts.FontInfo;
+import org.apache.fop.fonts.FontTriplet;
+import org.apache.fop.render.RenderingContext;
+
+import static org.junit.Assert.assertEquals;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+public class AbstractIFPainterTestCase {
+
+ private AbstractIFPainter<?> sut;
+ private IFDocumentHandler handler;
+
+ @Before
+ public void setUp() {
+ handler = mock(IFDocumentHandler.class);
+ sut = new AbstractIFPainter<IFDocumentHandler>(handler) {
+ public void startViewport(AffineTransform transform, Dimension size, Rectangle clipRect)
+ throws IFException {
+ }
+
+ public void endViewport() throws IFException {
+ }
+
+ public void startGroup(AffineTransform transform) throws IFException {
+ }
+
+ public void endGroup() throws IFException {
+ }
+
+ public void clipRect(Rectangle rect) throws IFException {
+ }
+
+ public void fillRect(Rectangle rect, Paint fill) throws IFException {
+ }
+
+ public void drawImage(String uri, Rectangle rect) throws IFException {
+ }
+
+ public void drawImage(Document doc, Rectangle rect) throws IFException {
+ }
+
+ @Override
+ protected RenderingContext createRenderingContext() {
+ return null;
+ }
+
+ public void drawText(int x, int y, int letterSpacing, int wordSpacing, int[][] dp,
+ String text) throws IFException {
+ }
+ };
+ FontInfo fontInfo = mock(FontInfo.class);
+ when(handler.getFontInfo()).thenReturn(fontInfo);
+ }
+
+ @Test
+ public void testGetFontKey() throws IFException {
+ String expected = "the expected string";
+ FontTriplet triplet = mock(FontTriplet.class);
+ FontInfo fontInfo = handler.getFontInfo();
+ when(fontInfo.getInternalFontKey(triplet)).thenReturn(expected);
+ assertEquals(expected, sut.getFontKey(triplet));
+ }
+
+ @Test(expected = IFException.class)
+ public void testGetFontKeyMissingFont() throws IFException {
+ FontTriplet triplet = mock(FontTriplet.class);
+ when(handler.getFontInfo().getInternalFontKey(triplet)).thenReturn(null);
+ sut.getFontKey(triplet);
+ }
+}