1 51 package org.apache.fop.svg; 52 53 import org.apache.fop.pdf.*; 54 import org.apache.fop.render.pdf.FontSetup; 55 import org.apache.fop.layout.*; 56 import org.apache.fop.apps.FOPException; 57 58 import java.awt.Graphics ; 59 import java.awt.Font ; 60 import java.awt.Color ; 61 import java.awt.Shape ; 62 import java.awt.font.FontRenderContext ; 63 import java.awt.font.GlyphVector ; 64 import java.io.OutputStream ; 65 import java.io.IOException ; 66 67 import org.apache.batik.ext.awt.g2d.GraphicContext; 68 69 78 public class PDFDocumentGraphics2D extends PDFGraphics2D { 79 OutputStream stream; 80 81 PDFStream pdfStream; 82 int width; 83 int height; 84 85 FontInfo fontInfo = null; 86 87 99 public PDFDocumentGraphics2D(boolean textAsShapes, OutputStream stream, 100 int width, int height) throws FOPException { 101 super(textAsShapes); 102 103 if (!textAsShapes) { 104 fontInfo = new FontInfo(); 105 FontSetup.setup(fontInfo); 106 try { 107 fontState = new FontState(fontInfo, "Helvetica", "normal", 108 "normal", 12, 0); 109 } catch (FOPException e) {} 110 } 111 standalone = true; 112 this.stream = stream; 113 this.pdfDoc = new PDFDocument(); 114 this.pdfDoc.setProducer("FOP SVG Renderer"); 115 pdfStream = this.pdfDoc.makeStream(); 116 this.width = width; 117 this.height = height; 118 119 currentFontName = ""; 120 currentFontSize = 0; 121 currentYPosition = 0; 122 currentXPosition = 0; 123 124 currentStream.write("1 0 0 -1 0 " + height + " cm\n"); 125 126 } 127 128 public FontState getFontState() { 129 return fontState; 130 } 131 132 public PDFDocument getPDFDocument() { 133 return this.pdfDoc; 134 } 135 136 142 public void setSVGDimension(float w, float h) { 143 currentStream.write("" + PDFNumber.doubleOut(width / w) + " 0 0 " 144 + PDFNumber.doubleOut(height / h) + " 0 0 cm\n"); 145 } 146 147 152 public void setBackgroundColor(Color col) { 153 Color c = col; 154 currentColour = new PDFColor(c.getRed(), c.getGreen(), c.getBlue()); 155 currentStream.write("q\n"); 156 currentStream.write(currentColour.getColorSpaceOut(true)); 157 158 currentStream.write("0 0 " + width + " " + height + " re\n"); 159 160 currentStream.write("f\n"); 161 currentStream.write("Q\n"); 162 } 163 164 170 public void finish() throws IOException { 171 pdfStream.add(getString()); 172 PDFResources pdfResources = this.pdfDoc.getResources(); 173 PDFPage currentPage = this.pdfDoc.makePage(pdfResources, pdfStream, 174 width, height, null); 175 if(currentAnnotList != null) { 176 currentPage.setAnnotList(currentAnnotList); 177 } 178 if (fontInfo != null) { 179 FontSetup.addToResources(this.pdfDoc, fontInfo); 180 } 181 pdfDoc.outputHeader(stream); 182 this.pdfDoc.output(stream); 183 pdfDoc.outputTrailer(stream); 184 } 185 186 public void setGraphicContext(GraphicContext c) { 187 gc = c; 188 } 189 190 193 public PDFDocumentGraphics2D(PDFDocumentGraphics2D g) { 194 super(g); 195 } 196 197 203 public Graphics create() { 204 return new PDFDocumentGraphics2D(this); 205 } 206 207 public void drawString(String s, float x, float y) { 208 if (super.textAsShapes) { 209 Font font = super.getFont(); 210 FontRenderContext frc = super.getFontRenderContext(); 211 GlyphVector gv = font.createGlyphVector(frc, s); 212 Shape glyphOutline = gv.getOutline(x, y); 213 super.fill(glyphOutline); 214 } else { 215 super.drawString(s, x, y); 216 } 217 } 218 219 } 220 221 | Popular Tags |