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");
}
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;
}
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);
}
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");
}
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);
}
}
Aggregations