KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > fop > render > ps > AbstractPSTranscoder


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 /* $Id: AbstractPSTranscoder.java 426576 2006-07-28 15:44:37Z jeremias $ */
19  
20 package org.apache.fop.render.ps;
21
22
23 import java.awt.Color JavaDoc;
24
25 import java.io.IOException JavaDoc;
26
27 import org.apache.avalon.framework.configuration.Configuration;
28 import org.apache.batik.bridge.BridgeContext;
29 import org.apache.batik.bridge.UnitProcessor;
30 import org.apache.batik.transcoder.TranscoderException;
31 import org.apache.batik.transcoder.TranscoderOutput;
32
33 import org.apache.batik.transcoder.image.ImageTranscoder;
34
35 import org.apache.fop.fonts.FontInfo;
36 import org.apache.fop.fonts.FontSetup;
37 import org.apache.fop.svg.AbstractFOPTranscoder;
38 import org.apache.xmlgraphics.java2d.ps.AbstractPSDocumentGraphics2D;
39 import org.apache.xmlgraphics.java2d.ps.TextHandler;
40
41 import org.w3c.dom.Document JavaDoc;
42 import org.w3c.dom.svg.SVGLength;
43
44 /**
45  * This class enables to transcode an input to a PostScript document.
46  *
47  * <p>Two transcoding hints (<tt>KEY_WIDTH</tt> and
48  * <tt>KEY_HEIGHT</tt>) can be used to respectively specify the image
49  * width and the image height. If only one of these keys is specified,
50  * the transcoder preserves the aspect ratio of the original image.
51  *
52  * <p>The <tt>KEY_BACKGROUND_COLOR</tt> defines the background color
53  * to use for opaque image formats, or the background color that may
54  * be used for image formats that support alpha channel.
55  *
56  * <p>The <tt>KEY_AOI</tt> represents the area of interest to paint
57  * in device space.
58  *
59  * <p>Three additional transcoding hints that act on the SVG
60  * processor can be specified:
61  *
62  * <p><tt>KEY_LANGUAGE</tt> to set the default language to use (may be
63  * used by a &lt;switch> SVG element for example),
64  * <tt>KEY_USER_STYLESHEET_URI</tt> to fix the URI of a user
65  * stylesheet, and <tt>KEY_PIXEL_TO_MM</tt> to specify the pixel to
66  * millimeter conversion factor.
67  *
68  * @author <a HREF="mailto:keiron@aftexsw.com">Keiron Liddle</a>
69  * @version $Id: AbstractPSTranscoder.java 426576 2006-07-28 15:44:37Z jeremias $
70  */

71 public abstract class AbstractPSTranscoder extends AbstractFOPTranscoder {
72
73     private Configuration cfg = null;
74     protected AbstractPSDocumentGraphics2D graphics = null;
75
76     /**
77      * Constructs a new <tt>AbstractPSTranscoder</tt>.
78      */

79     public AbstractPSTranscoder() {
80         super();
81     }
82
83     protected abstract AbstractPSDocumentGraphics2D createDocumentGraphics2D();
84
85     /**
86      * Transcodes the specified Document as an image in the specified output.
87      *
88      * @param document the document to transcode
89      * @param uri the uri of the document or null if any
90      * @param output the ouput where to transcode
91      * @exception TranscoderException if an error occured while transcoding
92      */

93     protected void transcode(Document JavaDoc document, String JavaDoc uri,
94                              TranscoderOutput output)
95         throws TranscoderException {
96
97         graphics = createDocumentGraphics2D();
98         if (!isTextStroked()) {
99             FontInfo fontInfo = new FontInfo();
100             //TODO Do custom font configuration here somewhere/somehow
101
FontSetup.setup(fontInfo, null, null);
102             graphics.setCustomTextHandler(new NativeTextHandler(graphics, fontInfo));
103         }
104
105         super.transcode(document, uri, output);
106
107         getLogger().trace("document size: " + width + " x " + height);
108         
109         // prepare the image to be painted
110
UnitProcessor.Context uctx = UnitProcessor.createContext(ctx,
111                     document.getDocumentElement());
112         float widthInPt = UnitProcessor.userSpaceToSVG(width, SVGLength.SVG_LENGTHTYPE_PT,
113                     UnitProcessor.HORIZONTAL_LENGTH, uctx);
114         int w = (int)(widthInPt + 0.5);
115         float heightInPt = UnitProcessor.userSpaceToSVG(height, SVGLength.SVG_LENGTHTYPE_PT,
116                 UnitProcessor.HORIZONTAL_LENGTH, uctx);
117         int h = (int)(heightInPt + 0.5);
118         getLogger().trace("document size: " + w + "pt x " + h + "pt");
119
120         try {
121             graphics.setupDocument(output.getOutputStream(), w, h);
122             graphics.setViewportDimension(width, height);
123
124             if (hints.containsKey(ImageTranscoder.KEY_BACKGROUND_COLOR)) {
125                 graphics.setBackgroundColor
126                     ((Color JavaDoc)hints.get(ImageTranscoder.KEY_BACKGROUND_COLOR));
127         }
128             graphics.setGraphicContext
129                 (new org.apache.xmlgraphics.java2d.GraphicContext());
130             graphics.setTransform(curTxf);
131
132             this.root.paint(graphics);
133
134             graphics.finish();
135         } catch (IOException JavaDoc ex) {
136             throw new TranscoderException(ex);
137         }
138     }
139     
140     /** @return true if text should be stroked rather than painted using text operators */
141     protected boolean isTextStroked() {
142         boolean stroke = false;
143         if (hints.containsKey(KEY_STROKE_TEXT)) {
144             stroke = ((Boolean JavaDoc)hints.get(KEY_STROKE_TEXT)).booleanValue();
145         }
146         return stroke;
147     }
148
149     /** @see org.apache.batik.transcoder.SVGAbstractTranscoder#createBridgeContext() */
150     protected BridgeContext createBridgeContext() {
151
152         BridgeContext ctx = new BridgeContext(userAgent);
153         if (!isTextStroked()) {
154             TextHandler handler = graphics.getCustomTextHandler();
155             if (handler instanceof NativeTextHandler) {
156                 NativeTextHandler nativeTextHandler = (NativeTextHandler)handler;
157                 PSTextPainter textPainter = new PSTextPainter(nativeTextHandler);
158                 ctx.setTextPainter(textPainter);
159                 ctx.putBridge(new PSTextElementBridge(textPainter));
160             }
161         }
162
163         //ctx.putBridge(new PSImageElementBridge());
164
return ctx;
165     }
166
167
168 }
169
Popular Tags