KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > fop > svg > PDFTextElementBridge


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: PDFTextElementBridge.java 426576 2006-07-28 15:44:37Z jeremias $ */
19
20 package org.apache.fop.svg;
21
22 import org.apache.batik.gvt.TextNode;
23 import org.apache.batik.bridge.SVGTextElementBridge;
24 import org.apache.batik.bridge.BridgeContext;
25 import org.apache.batik.bridge.TextUtilities;
26 import org.apache.batik.gvt.GraphicsNode;
27
28 import org.apache.fop.fonts.FontInfo;
29
30 import org.w3c.dom.Element JavaDoc;
31 import org.w3c.dom.Node JavaDoc;
32
33 /**
34  * Bridge class for the <text> element.
35  * This bridge will use the direct text painter if the text
36  * for the element is simple.
37  *
38  * @author <a HREF="mailto:keiron@aftexsw.com">Keiron Liddle</a>
39  */

40 public class PDFTextElementBridge extends SVGTextElementBridge {
41     private PDFTextPainter pdfTextPainter;
42
43     /**
44      * Constructs a new bridge for the &lt;text> element.
45      * @param fi the font infomration
46      */

47     public PDFTextElementBridge(FontInfo fi) {
48         pdfTextPainter = new PDFTextPainter(fi);
49     }
50
51     /**
52      * Create a text element bridge.
53      * This set the text painter on the node if the text is simple.
54      * @param ctx the bridge context
55      * @param e the svg element
56      * @return the text graphics node created by the super class
57      */

58     public GraphicsNode createGraphicsNode(BridgeContext ctx, Element JavaDoc e) {
59         GraphicsNode node = super.createGraphicsNode(ctx, e);
60         if (node != null && isSimple(ctx, e, node)) {
61             ((TextNode)node).setTextPainter(getTextPainter());
62         }
63         return node;
64     }
65
66     private PDFTextPainter getTextPainter() {
67         return pdfTextPainter;
68     }
69
70     /**
71      * Check if text element contains simple text.
72      * This checks the children of the text element to determine
73      * if the text is simple. The text is simple if it can be rendered
74      * with basic text drawing algorithms. This means there are no
75      * alternate characters, the font is known and there are no effects
76      * applied to the text.
77      *
78      * @param ctx the bridge context
79      * @param element the svg text element
80      * @param node the graphics node
81      * @return true if this text is simple of false if it cannot be
82      * easily rendered using normal drawString on the PDFGraphics2D
83      */

84     private boolean isSimple(BridgeContext ctx, Element JavaDoc element, GraphicsNode node) {
85         // Font size, in user space units.
86
float fs = TextUtilities.convertFontSize(element).floatValue();
87         // PDF cannot display fonts over 36pt
88
if (fs > 36) {
89             return false;
90         }
91
92         Element JavaDoc nodeElement;
93         for (Node n = element.getFirstChild();
94              n != null;
95              n = n.getNextSibling()) {
96
97             switch (n.getNodeType()) {
98             case Node.ELEMENT_NODE:
99
100                 nodeElement = (Element JavaDoc)n;
101
102                 if (n.getLocalName().equals(SVG_TSPAN_TAG)
103                     || n.getLocalName().equals(SVG_ALT_GLYPH_TAG)) {
104                     return false;
105                 } else if (n.getLocalName().equals(SVG_TEXT_PATH_TAG)) {
106                     return false;
107                 } else if (n.getLocalName().equals(SVG_TREF_TAG)) {
108                     return false;
109                 }
110                 break;
111             case Node.TEXT_NODE:
112             case Node.CDATA_SECTION_NODE:
113             }
114         }
115
116         /*if (CSSUtilities.convertFilter(element, node, ctx) != null) {
117             return false;
118         }*/

119
120         return true;
121     }
122 }
123
124
Popular Tags