Search in sources :

Example 1 with FormulaRenderer

use of org.xwiki.formula.FormulaRenderer in project xwiki-platform by xwiki.

the class FormulaMacro method render.

/**
 * Renders the formula using the specified renderer.
 *
 * @param formula the formula text
 * @param inline is the formula supposed to be used inline or as a block-level element
 * @param fontSize the specified font size
 * @param imageType the specified resulting image type
 * @param rendererHint the hint for the renderer to use
 * @return the resulting block holding the generated image, or {@code null} in case of an error.
 * @throws MacroExecutionException if no renderer exists for the passed hint or if that rendered failed to render
 *         the formula
 * @throws IllegalArgumentException if the formula is not valid, according to the LaTeX syntax
 */
private Block render(String formula, boolean inline, FontSize fontSize, Type imageType, String rendererHint) throws MacroExecutionException, IllegalArgumentException {
    try {
        FormulaRenderer renderer = this.manager.getInstance(FormulaRenderer.class, rendererHint);
        String imageName = renderer.process(formula, inline, fontSize, imageType);
        // TODO: HACK!!
        // We're going through the getAttachmentURL() API so that when the PdfURLFactory is used, the generated
        // image is saved and then embedded in the exported PDF thanks to PDFURIResolver. In the future we need
        // to remove this hack by introduce a proper Resource for generated image (say TemporaryResource),
        // implement a TemporaryResourceSerializer<URL> and introduce a ResourceLoader interface and have it
        // implemented for TemporaryResource...
        AttachmentReference attachmentReference = new AttachmentReference(imageName, this.dab.getCurrentDocumentReference());
        String url = this.dab.getAttachmentURL(attachmentReference, false);
        // Note that we have to replace the download action by the tex action since the getAttachmentURL() API
        // will use the "download" action but when the generated URL is called by the browser it needs to point to
        // the TexAction...
        url = url.replace("/download/", "/tex/");
        // TODO: end HACK!!
        ResourceReference imageReference = new ResourceReference(url, ResourceType.URL);
        ImageBlock result = new ImageBlock(imageReference, false);
        // Set the alternative text for the image to be the original formula
        result.setParameter("alt", formula);
        return result;
    } catch (Exception e) {
        throw new MacroExecutionException(String.format("Failed to render formula using the [%s] renderer", rendererHint), e);
    }
}
Also used : AttachmentReference(org.xwiki.model.reference.AttachmentReference) ImageBlock(org.xwiki.rendering.block.ImageBlock) MacroExecutionException(org.xwiki.rendering.macro.MacroExecutionException) FormulaRenderer(org.xwiki.formula.FormulaRenderer) ResourceReference(org.xwiki.rendering.listener.reference.ResourceReference) MacroExecutionException(org.xwiki.rendering.macro.MacroExecutionException)

Aggregations

FormulaRenderer (org.xwiki.formula.FormulaRenderer)1 AttachmentReference (org.xwiki.model.reference.AttachmentReference)1 ImageBlock (org.xwiki.rendering.block.ImageBlock)1 ResourceReference (org.xwiki.rendering.listener.reference.ResourceReference)1 MacroExecutionException (org.xwiki.rendering.macro.MacroExecutionException)1