1 17 18 19 20 package org.apache.fop.render.ps; 21 22 import java.awt.Shape ; 23 import java.awt.geom.AffineTransform ; 24 import java.io.IOException ; 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 39 public class NativeTextHandler implements TextHandler { 40 41 private PSGraphics2D g2d; 42 43 44 protected FontInfo fontInfo; 45 46 47 protected Font font; 48 49 50 protected Font overrideFont = null; 51 52 53 protected String currentFontName; 54 55 56 protected int currentFontSize; 57 58 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 fontInfo = new FontInfo(); 75 FontSetup.setup(fontInfo, null, null); 76 } 77 78 82 public FontInfo getFontInfo() { 83 return fontInfo; 84 } 85 86 private PSGenerator getPSGenerator() { 87 return this.g2d.getPSGenerator(); 88 } 89 90 91 public void writeSetup() throws IOException { 92 if (fontInfo != null) { 93 PSFontUtils.writeFontDict(getPSGenerator(), fontInfo); 94 } 95 } 96 97 98 public void writePageSetup() throws IOException { 99 if (fontInfo != null) { 100 getPSGenerator().writeln("FOPFonts begin"); 101 } 102 } 103 104 109 public void drawString(String s, float x, float y) throws IOException { 110 g2d.preparePainting(); 111 if (this.overrideFont == null) { 112 java.awt.Font awtFont = g2d.getFont(); 113 this.font = createFont(awtFont); 114 } else { 115 this.font = this.overrideFont; 116 this.overrideFont = null; 117 } 118 119 g2d.establishColor(g2d.getColor()); 121 establishCurrentFont(); 122 123 PSGenerator gen = getPSGenerator(); 124 gen.saveGraphicsState(); 125 126 Shape imclip = g2d.getClip(); 128 g2d.writeClip(imclip); 129 130 AffineTransform 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 sb = new StringBuffer ("("); 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 text, StringBuffer 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 f) { 156 String fontFamily = f.getFamily(); 157 if (fontFamily.equals("sanserif")) { 158 fontFamily = "sans-serif"; 159 } 160 int fontSize = 1000 * f.getSize(); 161 String 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 { 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 186 public void setOverrideFont(Font override) { 187 this.overrideFont = override; 188 } 189 190 191 } 192 | Popular Tags |