Search in sources :

Example 1 with ImageData

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

the class MathTranFormulaRenderer method renderImage.

@Override
protected ImageData renderImage(String formula, boolean inline, FormulaRenderer.FontSize size, FormulaRenderer.Type type) throws IllegalArgumentException, IOException {
    String encodedFormula = URLEncoder.encode((inline ? "" : "\\displaystyle") + formula, "UTF-8");
    GetMethod method = new GetMethod(MATHTRAN_BASE_URL + "D=" + Math.max(size.ordinal() - 3, 0) + "&tex=" + encodedFormula);
    method.setRequestHeader("accept", type.getMimetype());
    method.setFollowRedirects(true);
    // Execute the GET method
    int statusCode = this.client.executeMethod(method);
    if (statusCode >= HttpStatus.SC_OK && statusCode < HttpStatus.SC_BAD_REQUEST) {
        byte[] b = method.getResponseBody();
        method.releaseConnection();
        return new ImageData(b, type);
    }
    throw new IOException("Can't load image from MathTran server");
}
Also used : ImageData(org.xwiki.formula.ImageData) GetMethod(org.apache.commons.httpclient.methods.GetMethod) IOException(java.io.IOException)

Example 2 with ImageData

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

the class TexAction method render.

@Override
public String render(XWikiContext context) throws XWikiException {
    XWikiRequest request = context.getRequest();
    XWikiResponse response = context.getResponse();
    String path = request.getRequestURI();
    // Expected /xwiki/bin/tex/Current/Document/image_identifier
    String filename = Util.decodeURI(path.substring(path.lastIndexOf("/") + 1), context);
    ImageStorage storage = Utils.getComponent(ImageStorage.class);
    ImageData image = storage.get(filename);
    if (image == null) {
        return "docdoesnotexist";
    }
    response.setContentLength(image.getData().length);
    response.setContentType(image.getMimeType());
    try {
        response.getOutputStream().write(image.getData());
    } catch (IOException e) {
        LOGGER.info("Failed to send image to the client");
    }
    return null;
}
Also used : ImageStorage(org.xwiki.formula.ImageStorage) ImageData(org.xwiki.formula.ImageData) IOException(java.io.IOException)

Example 3 with ImageData

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

the class PDFResourceResolver method getResource.

@Override
public Resource getResource(URI uri) throws IOException {
    XWikiContext xcontext = xcontextProvider.get();
    Map<String, AttachmentReference> attachmentMap = (Map<String, AttachmentReference>) xcontext.get(PdfURLFactory.PDF_EXPORT_CONTEXT_KEY);
    if (attachmentMap != null) {
        String uriString = uri.toString();
        // implemented for TemporaryResource...
        if (uriString.contains(TEX_ACTION)) {
            // Note: See the comments in FormulaMacro to understand why we do a replace...
            AttachmentReference reference = attachmentMap.get(uriString.replace(TEX_ACTION, "/download/"));
            if (reference != null) {
                // Get the generated image's input stream
                ImageStorage storage = Utils.getComponent(ImageStorage.class);
                ImageData image = storage.get(reference.getName());
                return new Resource(new ByteArrayInputStream(image.getData()));
            }
        }
        // TODO: end HACK
        AttachmentReference reference = attachmentMap.get(uriString);
        if (reference != null) {
            try {
                XWikiDocument xdoc = xcontext.getWiki().getDocument(reference.extractReference(EntityType.DOCUMENT), xcontext);
                // TODO: handle revisions
                XWikiAttachment attachment = xdoc.getAttachment(reference.extractReference(EntityType.ATTACHMENT).getName());
                return new Resource(attachment.getContentInputStream(xcontext));
            } catch (Exception e) {
                throw new IOException(String.format("Failed to resolve export URI [%s]", uriString), e);
            }
        }
    }
    return this.standardResolver.getResource(uri);
}
Also used : AttachmentReference(org.xwiki.model.reference.AttachmentReference) ImageStorage(org.xwiki.formula.ImageStorage) Resource(org.apache.xmlgraphics.io.Resource) XWikiContext(com.xpn.xwiki.XWikiContext) XWikiAttachment(com.xpn.xwiki.doc.XWikiAttachment) IOException(java.io.IOException) IOException(java.io.IOException) XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) ByteArrayInputStream(java.io.ByteArrayInputStream) ImageData(org.xwiki.formula.ImageData) Map(java.util.Map)

Example 4 with ImageData

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

the class GoogleChartsFormulaRenderer method renderImage.

@Override
protected ImageData renderImage(String formula, boolean inline, FormulaRenderer.FontSize size, FormulaRenderer.Type type) throws IllegalArgumentException, IOException {
    String encodedFormula = URLEncoder.encode(formula, "UTF-8");
    GetMethod method = new GetMethod(SERVICE_BASE_URL + encodedFormula);
    method.setRequestHeader("accept", type.getMimetype());
    method.setFollowRedirects(true);
    // Execute the GET method
    int statusCode = this.client.executeMethod(method);
    if (statusCode >= HttpStatus.SC_OK && statusCode < HttpStatus.SC_BAD_REQUEST) {
        byte[] b = method.getResponseBody();
        method.releaseConnection();
        return new ImageData(b, type);
    }
    throw new IOException("Can't load image from the Google server");
}
Also used : ImageData(org.xwiki.formula.ImageData) GetMethod(org.apache.commons.httpclient.methods.GetMethod) IOException(java.io.IOException)

Example 5 with ImageData

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

the class NativeFormulaRenderer method renderImage.

@Override
protected ImageData renderImage(String formula, boolean inline, FormulaRenderer.FontSize size, FormulaRenderer.Type type) throws IllegalArgumentException, IOException {
    File tmpDirectory = null;
    try {
        String texContent = "\\documentclass[10pt]{article}\n" + "\\usepackage[paperheight=1000in]{geometry}\n" + "\\usepackage{amsmath}\n" + "\\usepackage{amsfonts}\n" + "\\usepackage{amssymb}\n" + "\\usepackage{pst-plot}\n" + "\\usepackage{color}\n" + "\\pagestyle{empty}\n" + "\\begin{document}\n" + size.getCommand() + "\n" + wrapFormula(formula, inline) + "\\end{document}\n";
        do {
            tmpDirectory = new File(this.tempDirectory, RandomStringUtils.randomAlphanumeric(8));
        } while (tmpDirectory.exists());
        tmpDirectory.mkdir();
        final String baseName = tmpDirectory.getAbsolutePath() + "/file";
        final String texFileName = baseName + ".tex";
        final String dviFileName = baseName + ".dvi";
        final String psFileName = baseName + ".ps";
        final String imageFileName = baseName + type.getExtension();
        // Write the formula in a tex file
        FileWriter fw = new FileWriter(texFileName);
        fw.write(texContent);
        fw.close();
        // TeX to DVI
        String[] commandLine = new String[] { "latex", "--interaction=nonstopmode", texFileName };
        executeCommand(commandLine, tmpDirectory);
        // DVI to PS
        commandLine = new String[] { "dvips", "-E", dviFileName, "-o", psFileName };
        executeCommand(commandLine, tmpDirectory);
        // PS to image
        commandLine = new String[] { "convert", "-density", "120", psFileName, imageFileName };
        executeCommand(commandLine, tmpDirectory);
        return new ImageData(FileUtils.readFileToByteArray(new File(imageFileName)), type);
    } finally {
        FileUtils.deleteQuietly(tmpDirectory);
    }
}
Also used : ImageData(org.xwiki.formula.ImageData) FileWriter(java.io.FileWriter) File(java.io.File)

Aggregations

ImageData (org.xwiki.formula.ImageData)7 IOException (java.io.IOException)4 ImageStorage (org.xwiki.formula.ImageStorage)3 XWikiAttachment (com.xpn.xwiki.doc.XWikiAttachment)2 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 GetMethod (org.apache.commons.httpclient.methods.GetMethod)2 AttachmentReference (org.xwiki.model.reference.AttachmentReference)2 XWikiContext (com.xpn.xwiki.XWikiContext)1 File (java.io.File)1 FileWriter (java.io.FileWriter)1 Map (java.util.Map)1 TransformerException (javax.xml.transform.TransformerException)1 StreamSource (javax.xml.transform.stream.StreamSource)1 ByteArrayOutputStream (org.apache.commons.io.output.ByteArrayOutputStream)1 NullOutputStream (org.apache.commons.io.output.NullOutputStream)1 Resource (org.apache.xmlgraphics.io.Resource)1 SnuggleInput (uk.ac.ed.ph.snuggletex.SnuggleInput)1 SnuggleSession (uk.ac.ed.ph.snuggletex.SnuggleSession)1 WebPageOutputOptions (uk.ac.ed.ph.snuggletex.WebPageOutputOptions)1