Search in sources :

Example 1 with OutputException

use of net.parostroj.timetable.output2.OutputException in project grafikon by jub77.

the class DrawOutput method processPdf.

private void processPdf(Collection<Image> images, OutputStream stream, DrawLayout layout) throws OutputException {
    PDFDocumentGraphics2D g2d;
    try {
        g2d = new PDFDocumentGraphics2D();
        g2d.setGraphicContext(new GraphicContext());
        FopFactory fopFactory = PdfTransformer.createFopFactory();
        FOUserAgent userAgent = fopFactory.newFOUserAgent();
        FontConfig fc = userAgent.getRendererConfig(MimeConstants.MIME_PDF, new PDFRendererConfigParser()).getFontInfoConfig();
        FontManager fontManager = fopFactory.getFontManager();
        DefaultFontConfigurator fontInfoConfigurator = new DefaultFontConfigurator(fontManager, null, false);
        List<EmbedFontInfo> fontInfoList = fontInfoConfigurator.configure(fc);
        FontInfo fontInfo = new FontInfo();
        FontSetup.setup(fontInfo, fontInfoList, userAgent.getResourceResolver(), fontManager.isBase14KerningEnabled());
        g2d.setFontInfo(fontInfo);
        g2d.setFont(new Font("SansCondensed", Font.PLAIN, g2d.getFont().getSize()));
        List<Dimension> sizes = this.getSizes(images, g2d);
        Dimension size = this.getTotalSize(sizes, layout);
        g2d.setupDocument(stream, size.width, size.height);
        this.drawImages(sizes, images, g2d, layout);
        g2d.finish();
    } catch (IOException | FOPException e) {
        throw new OutputException(e.getMessage(), e);
    }
}
Also used : EmbedFontInfo(org.apache.fop.fonts.EmbedFontInfo) GraphicContext(org.apache.xmlgraphics.java2d.GraphicContext) PDFRendererConfigParser(org.apache.fop.render.pdf.PDFRendererConfig.PDFRendererConfigParser) FOUserAgent(org.apache.fop.apps.FOUserAgent) FontConfig(org.apache.fop.fonts.FontConfig) PDFDocumentGraphics2D(org.apache.fop.svg.PDFDocumentGraphics2D) FopFactory(org.apache.fop.apps.FopFactory) Dimension(java.awt.Dimension) IOException(java.io.IOException) FontManager(org.apache.fop.fonts.FontManager) Font(java.awt.Font) FOPException(org.apache.fop.apps.FOPException) OutputException(net.parostroj.timetable.output2.OutputException) EmbedFontInfo(org.apache.fop.fonts.EmbedFontInfo) FontInfo(org.apache.fop.fonts.FontInfo) DefaultFontConfigurator(org.apache.fop.fonts.DefaultFontConfigurator)

Example 2 with OutputException

use of net.parostroj.timetable.output2.OutputException in project grafikon by jub77.

the class DrawOutput method processPng.

private void processPng(Collection<Image> images, OutputStream stream, DrawLayout layout) throws OutputException {
    BufferedImage testImg = new BufferedImage(10, 10, BufferedImage.TYPE_INT_RGB);
    List<Dimension> sizes = this.getSizes(images, testImg.createGraphics());
    Dimension size = this.getTotalSize(sizes, layout);
    BufferedImage img = new BufferedImage(size.width, size.height, BufferedImage.TYPE_INT_RGB);
    Graphics2D g2d = img.createGraphics();
    g2d.setColor(Color.white);
    g2d.fillRect(0, 0, size.width, size.height);
    this.drawImages(sizes, images, g2d, layout);
    try {
        ImageIO.write(img, "png", stream);
    } catch (IOException e) {
        throw new OutputException(e.getMessage(), e);
    }
}
Also used : OutputException(net.parostroj.timetable.output2.OutputException) Dimension(java.awt.Dimension) IOException(java.io.IOException) BufferedImage(java.awt.image.BufferedImage) PDFDocumentGraphics2D(org.apache.fop.svg.PDFDocumentGraphics2D) Graphics2D(java.awt.Graphics2D) SVGGraphics2D(org.apache.batik.svggen.SVGGraphics2D)

Example 3 with OutputException

use of net.parostroj.timetable.output2.OutputException in project grafikon by jub77.

the class PdfTransformer method write.

public FormattingResults write(OutputStream os, InputStream is, URIResolver resolver) throws OutputException {
    FopFactory fopFactory = getFopFactory(resolver);
    try {
        Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, fopFactory.newFOUserAgent(), os);
        Transformer transformer = factory.newTransformer();
        Source src = new StreamSource(is);
        Result res = new SAXResult(fop.getDefaultHandler());
        transformer.transform(src, res);
        return fop.getResults();
    } catch (FOPException | TransformerException e) {
        throw new OutputException("Error transformation exception", e);
    }
}
Also used : SAXResult(javax.xml.transform.sax.SAXResult) StreamSource(javax.xml.transform.stream.StreamSource) OutputException(net.parostroj.timetable.output2.OutputException) StreamSource(javax.xml.transform.stream.StreamSource) SAXResult(javax.xml.transform.sax.SAXResult)

Example 4 with OutputException

use of net.parostroj.timetable.output2.OutputException in project grafikon by jub77.

the class CirculationView method draw.

@Override
public void draw(Dimension size, File outputFile, Type type) throws OutputException {
    // ignore dimension - it is fixed for the circulation output
    OutputFactory factory = OutputFactory.newInstance("draw");
    Output output = factory.createOutput("circulations");
    Tuple<Integer> limits = this.getLimits();
    List<TrainsCycle> circulations = this.getCirculations();
    CirculationDrawParams cdParams = new CirculationDrawParams(circulations).setFrom(limits.first).setTo(limits.second).setWidthInChars(stepWidth).setZoom(zoom).setColors(drawColors);
    output.write(output.getAvailableParams().setParam(Output.PARAM_OUTPUT_FILE, outputFile).setParam(Output.PARAM_TRAIN_DIAGRAM, diagram).setParam(DrawParams.CD_PARAMS, Arrays.asList(cdParams)).setParam(DrawParams.OUTPUT_TYPE, type == Type.SVG ? FileOutputType.SVG : FileOutputType.PNG));
}
Also used : Output(net.parostroj.timetable.output2.Output)

Example 5 with OutputException

use of net.parostroj.timetable.output2.OutputException in project grafikon by jub77.

the class XmlEndPositionsOutput method writeTo.

@Override
protected void writeTo(OutputParams params, OutputStream stream, TrainDiagram diagram) throws OutputException {
    try {
        // extract positions
        PositionsExtractor pe = new PositionsExtractor(diagram);
        List<Position> engines = pe.getEndPositions(diagram.getEngineCycleType().getCycles(), null);
        List<Position> trainUnits = pe.getEndPositions(diagram.getTrainUnitCycleType().getCycles(), null);
        EndPositions ep = new EndPositions();
        ep.setEnginesPositions(engines);
        ep.setTrainUnitsPositions(trainUnits);
        JAXBContext context = JAXBContext.newInstance(EndPositions.class);
        Marshaller m = context.createMarshaller();
        m.setProperty(Marshaller.JAXB_ENCODING, this.getCharset().name());
        m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
        Writer writer = new OutputStreamWriter(stream, this.getCharset());
        m.marshal(ep, writer);
    } catch (Exception e) {
        throw new OutputException(e);
    }
}
Also used : Marshaller(javax.xml.bind.Marshaller) PositionsExtractor(net.parostroj.timetable.output2.impl.PositionsExtractor) Position(net.parostroj.timetable.output2.impl.Position) EndPositions(net.parostroj.timetable.output2.impl.EndPositions) OutputException(net.parostroj.timetable.output2.OutputException) JAXBContext(javax.xml.bind.JAXBContext) OutputStreamWriter(java.io.OutputStreamWriter) Writer(java.io.Writer) OutputStreamWriter(java.io.OutputStreamWriter) OutputException(net.parostroj.timetable.output2.OutputException)

Aggregations

OutputException (net.parostroj.timetable.output2.OutputException)13 OutputStreamWriter (java.io.OutputStreamWriter)9 Writer (java.io.Writer)9 JAXBContext (javax.xml.bind.JAXBContext)8 Marshaller (javax.xml.bind.Marshaller)8 Dimension (java.awt.Dimension)4 IOException (java.io.IOException)4 Font (java.awt.Font)2 InputStream (java.io.InputStream)2 StreamSource (javax.xml.transform.stream.StreamSource)2 Position (net.parostroj.timetable.output2.impl.Position)2 PositionsExtractor (net.parostroj.timetable.output2.impl.PositionsExtractor)2 SVGGraphics2D (org.apache.batik.svggen.SVGGraphics2D)2 PDFDocumentGraphics2D (org.apache.fop.svg.PDFDocumentGraphics2D)2 Frame (java.awt.Frame)1 Graphics2D (java.awt.Graphics2D)1 RenderingHints (java.awt.RenderingHints)1 Key (java.awt.RenderingHints.Key)1 BufferedImage (java.awt.image.BufferedImage)1 URI (java.net.URI)1