Search in sources :

Example 1 with AwtCanvas

use of com.xenoage.zong.renderer.awt.canvas.AwtCanvas in project Zong by Xenoage.

the class PdfPrinter method print.

/**
 * Prints the given {@link Layout} into the given PDF output stream.
 */
public static void print(Layout layout, OutputStream out) {
    Document document = new Document();
    PdfWriter writer = null;
    try {
        writer = PdfWriter.getInstance(document, out);
    } catch (Exception e) {
        handle(warning(Voc.ErrorWhilePrinting));
    }
    document.open();
    PdfContentByte cb = writer.getDirectContent();
    It<Page> pages = it(layout.getPages());
    for (Page page : pages) {
        // create PDF page
        Size2f pageSize = page.getFormat().getSize();
        float width = Units.mmToPx(pageSize.width, 1);
        float height = Units.mmToPx(pageSize.height, 1);
        document.newPage();
        PdfTemplate tp = cb.createTemplate(width, height);
        // fill PDF page
        Graphics2D g2d = new PdfGraphics2D(cb, width, height);
        INSTANCE.log(Companion.remark("Printing page " + pages.getIndex() + "..."));
        LayoutRenderer.paintToCanvas(layout, pages.getIndex(), new AwtCanvas(g2d, pageSize, CanvasFormat.Vector, CanvasDecoration.None, CanvasIntegrity.Perfect));
        // finish page
        g2d.dispose();
        cb.addTemplate(tp, 0, 0);
    }
    document.close();
}
Also used : PdfWriter(com.itextpdf.text.pdf.PdfWriter) Size2f(com.xenoage.utils.math.geom.Size2f) PdfGraphics2D(com.itextpdf.awt.PdfGraphics2D) AwtCanvas(com.xenoage.zong.renderer.awt.canvas.AwtCanvas) PdfContentByte(com.itextpdf.text.pdf.PdfContentByte) Page(com.xenoage.zong.layout.Page) Document(com.itextpdf.text.Document) PdfTemplate(com.itextpdf.text.pdf.PdfTemplate) Graphics2D(java.awt.Graphics2D) PdfGraphics2D(com.itextpdf.awt.PdfGraphics2D)

Example 2 with AwtCanvas

use of com.xenoage.zong.renderer.awt.canvas.AwtCanvas in project Zong by Xenoage.

the class PrintProcess method print.

@Override
public int print(Graphics g, PageFormat pageFormat, int pageIndex) throws PrinterException {
    // valid page number?
    if (pageIndex < 0 || pageIndex >= layout.getPages().size()) {
        return Printable.NO_SUCH_PAGE;
    }
    // print page
    INSTANCE.log(Companion.remark("Printing page " + pageIndex + "..."));
    Graphics2D g2d = (Graphics2D) g;
    Size2f pageSize = layout.getPages().get(pageIndex).getFormat().getSize();
    LayoutRenderer.paintToCanvas(layout, pageIndex, new AwtCanvas(g2d, pageSize, CanvasFormat.Vector, CanvasDecoration.None, CanvasIntegrity.Perfect));
    return Printable.PAGE_EXISTS;
}
Also used : Size2f(com.xenoage.utils.math.geom.Size2f) AwtCanvas(com.xenoage.zong.renderer.awt.canvas.AwtCanvas) Graphics2D(java.awt.Graphics2D)

Example 3 with AwtCanvas

use of com.xenoage.zong.renderer.awt.canvas.AwtCanvas in project Zong by Xenoage.

the class AwtLayoutRenderer method paintToImage.

/**
 * Returns a {@link BufferedImage} with the given page of the given {@link Layout}
 * which is rendered at the given zoom level.
 */
public static BufferedImage paintToImage(Layout layout, int pageIndex, float zoom) {
    Page page = layout.getPages().get(pageIndex);
    Size2f pageSize = page.getFormat().getSize();
    int width = Units.mmToPxInt(pageSize.width, zoom);
    int height = Units.mmToPxInt(pageSize.height, zoom);
    BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
    Graphics2D g2d = img.createGraphics();
    g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
    g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
    g2d.setColor(Color.WHITE);
    g2d.fillRect(0, 0, width, height);
    LayoutRenderer.paintToCanvas(layout, pageIndex, zoom, origin, new AwtCanvas(g2d, pageSize, CanvasFormat.Raster, CanvasDecoration.Interactive, CanvasIntegrity.Perfect));
    g2d.dispose();
    return img;
}
Also used : Size2f(com.xenoage.utils.math.geom.Size2f) AwtCanvas(com.xenoage.zong.renderer.awt.canvas.AwtCanvas) Page(com.xenoage.zong.layout.Page) BufferedImage(java.awt.image.BufferedImage) Graphics2D(java.awt.Graphics2D)

Aggregations

Size2f (com.xenoage.utils.math.geom.Size2f)3 AwtCanvas (com.xenoage.zong.renderer.awt.canvas.AwtCanvas)3 Graphics2D (java.awt.Graphics2D)3 Page (com.xenoage.zong.layout.Page)2 PdfGraphics2D (com.itextpdf.awt.PdfGraphics2D)1 Document (com.itextpdf.text.Document)1 PdfContentByte (com.itextpdf.text.pdf.PdfContentByte)1 PdfTemplate (com.itextpdf.text.pdf.PdfTemplate)1 PdfWriter (com.itextpdf.text.pdf.PdfWriter)1 BufferedImage (java.awt.image.BufferedImage)1