KickJava   Java API By Example, From Geeks To Geeks.

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


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$ */
19
20 package org.apache.fop.render.ps;
21
22 import java.awt.Shape JavaDoc;
23 import java.awt.geom.AffineTransform JavaDoc;
24 import java.io.IOException JavaDoc;
25
26 import org.apache.fop.fonts.Font;
27 import org.apache.fop.fonts.FontInfo;
28 import org.apache.fop.fonts.FontSetup;
29 import org.apache.fop.fonts.FontTriplet;
30
31 import org.apache.xmlgraphics.java2d.ps.PSGraphics2D;
32 import org.apache.xmlgraphics.java2d.ps.TextHandler;
33 import org.apache.xmlgraphics.ps.PSGenerator;
34
35 /**
36  * Specialized TextHandler implementation that the PSGraphics2D class delegates to to paint text
37  * using PostScript text operations.
38  */

39 public class NativeTextHandler implements TextHandler {
40
41     private PSGraphics2D g2d;
42     
43     /** FontInfo containing all available fonts */
44     protected FontInfo fontInfo;
45
46     /** Currently valid Font */
47     protected Font font;
48     
49     /** Overriding FontState */
50     protected Font overrideFont = null;
51     
52     /** the current (internal) font name */
53     protected String JavaDoc currentFontName;
54
55     /** the current font size in millipoints */
56     protected int currentFontSize;
57
58     /**
59      * Main constructor.
60      * @param g2d the PSGraphics2D instance this instances is used by
61      * @param fontInfo the FontInfo object with all available fonts
62      */

63     public NativeTextHandler(PSGraphics2D g2d, FontInfo fontInfo) {
64         this.g2d = g2d;
65         if (fontInfo != null) {
66             this.fontInfo = fontInfo;
67         } else {
68             setupFontInfo();
69         }
70     }
71     
72     private void setupFontInfo() {
73         //Sets up a FontInfo with default fonts
74
fontInfo = new FontInfo();
75         FontSetup.setup(fontInfo, null, null);
76     }
77     
78     /**
79      * Return the font information associated with this object
80      * @return the FontInfo object
81      */

82     public FontInfo getFontInfo() {
83         return fontInfo;
84     }
85
86     private PSGenerator getPSGenerator() {
87         return this.g2d.getPSGenerator();
88     }
89     
90     /** @see org.apache.xmlgraphics.java2d.ps.TextHandler#writeSetup() */
91     public void writeSetup() throws IOException JavaDoc {
92         if (fontInfo != null) {
93             PSFontUtils.writeFontDict(getPSGenerator(), fontInfo);
94         }
95     }
96
97     /** @see org.apache.xmlgraphics.java2d.ps.TextHandler#writePageSetup() */
98     public void writePageSetup() throws IOException JavaDoc {
99         if (fontInfo != null) {
100             getPSGenerator().writeln("FOPFonts begin");
101         }
102     }
103
104     /**
105      * Draw a string to the PostScript document. The text is painted using
106      * text operations.
107      * @see org.apache.xmlgraphics.java2d.ps.TextHandler#drawString(java.lang.String, float, float)
108      */

109     public void drawString(String JavaDoc s, float x, float y) throws IOException JavaDoc {
110         g2d.preparePainting();
111         if (this.overrideFont == null) {
112             java.awt.Font JavaDoc awtFont = g2d.getFont();
113             this.font = createFont(awtFont);
114         } else {
115             this.font = this.overrideFont;
116             this.overrideFont = null;
117         }
118         
119         //Color and Font state
120
g2d.establishColor(g2d.getColor());
121         establishCurrentFont();
122
123         PSGenerator gen = getPSGenerator();
124         gen.saveGraphicsState();
125
126         //Clip
127
Shape JavaDoc imclip = g2d.getClip();
128         g2d.writeClip(imclip);
129
130         //Prepare correct transformation
131
AffineTransform JavaDoc trans = g2d.getTransform();
132         gen.concatMatrix(trans);
133         gen.writeln(gen.formatDouble(x) + " "
134                   + gen.formatDouble(y) + " moveto ");
135         gen.writeln("1 -1 scale");
136   
137         StringBuffer JavaDoc sb = new StringBuffer JavaDoc("(");
138         escapeText(s, sb);
139         sb.append(") t ");
140
141         gen.writeln(sb.toString());
142         
143         gen.restoreGraphicsState();
144     }
145
146     private void escapeText(final String JavaDoc text, StringBuffer JavaDoc target) {
147         final int l = text.length();
148         for (int i = 0; i < l; i++) {
149             final char ch = text.charAt(i);
150             final char mch = this.font.mapChar(ch);
151             PSGenerator.escapeChar(mch, target);
152         }
153     }
154
155     private Font createFont(java.awt.Font JavaDoc f) {
156         String JavaDoc fontFamily = f.getFamily();
157         if (fontFamily.equals("sanserif")) {
158             fontFamily = "sans-serif";
159         }
160         int fontSize = 1000 * f.getSize();
161         String JavaDoc style = f.isItalic() ? "italic" : "normal";
162         int weight = f.isBold() ? Font.BOLD : Font.NORMAL;
163                 
164         FontTriplet triplet = fontInfo.findAdjustWeight(fontFamily, style, weight);
165         if (triplet == null) {
166             triplet = fontInfo.findAdjustWeight("sans-serif", style, weight);
167         }
168         return fontInfo.getFontInstance(triplet, fontSize);
169     }
170
171     private void establishCurrentFont() throws IOException JavaDoc {
172         if ((currentFontName != this.font.getFontName())
173                 || (currentFontSize != this.font.getFontSize())) {
174             PSGenerator gen = getPSGenerator();
175             gen.writeln(this.font.getFontName() + " "
176                     + gen.formatDouble(font.getFontSize() / 1000f) + " F");
177             currentFontName = this.font.getFontName();
178             currentFontSize = this.font.getFontSize();
179         }
180     }
181
182     /**
183      * Sets the overriding font.
184      * @param override Overriding Font to set
185      */

186     public void setOverrideFont(Font override) {
187         this.overrideFont = override;
188     }
189     
190
191 }
192
Popular Tags