private List<Element> renderGral(String raw) throws BadElementException, IOException { ChartDescriptor descriptor = new ChartDescriptorParser().parse(raw); PdfContentByte cb = writer.get().getDirectContent(); float width = (float) descriptor.getWidth(); float height = (float) descriptor.getHeight(); PdfTemplate template = cb.createTemplate(width, height); Graphics2D g2 = new PdfGraphics2D(template, width, height); GralRenderer renderer = new GralRenderer(); renderer.render(g2, descriptor); ArrayList<Element> elements = new ArrayList<Element>(1); elements.add(new ImgTemplate(template)); return elements; }
@Override public void emit(SourceCode sourceCode, ITextContext context) { String lang = sourceCode.lang(); String code = sourceCode.content(); try { String trimmed = Strings.unindentBlock(code); log.debug("Initializing text grid"); TeXFormula formula = new TeXFormula(trimmed); TeXIcon teXIcon = formula.createTeXIcon(TeXConstants.STYLE_DISPLAY, 14f); teXIcon.setInsets(new Insets(1, 1, 1, 1)); teXIcon.setForeground(foreground); PdfWriter pdfWriter = context.getPdfWriter(); PdfContentByte cb = pdfWriter.getDirectContent(); float width = (float) teXIcon.getIconWidth(); float height = (float) teXIcon.getIconHeight(); PdfTemplate template = cb.createTemplate(width, height); Graphics2D g2 = new PdfGraphics2D(template, width, height, new JLaTeXmathFontMapper()); log.debug("Rendering formula"); teXIcon.paintIcon(null, g2, 0, 0); g2.dispose(); log.debug("Rendering diagram done"); ImgTemplate imgTemplate = new ImgTemplate(template); scaleToFit(imgTemplate, context.getDocumentArtBox()); context.append(imgTemplate); } catch (Exception e) { throw new WrappedRuntimeException(e); } }