Search in sources :

Example 1 with ParseException

use of org.scilab.forge.jlatexmath.ParseException in project Bean by Xirado.

the class LaTeXCommand method executeCommand.

@Override
public void executeCommand(@NotNull SlashCommandInteractionEvent event, @NotNull SlashCommandContext ctx) {
    event.deferReply().queue();
    InteractionHook hook = event.getHook();
    String formula = event.getOption("formula").getAsString();
    TeXFormula tf;
    try {
        tf = new TeXFormula(formula);
    } catch (ParseException exception) {
        hook.sendMessageEmbeds(EmbedUtil.errorEmbed("An error occurred while parsing LaTeX formula!\n```\n" + exception.getMessage() + "\n```")).queue();
        return;
    }
    TeXIcon ti = tf.createTeXIcon(TeXConstants.STYLE_DISPLAY, 70);
    BufferedImage bimg = new BufferedImage(ti.getIconWidth(), ti.getIconHeight(), BufferedImage.TYPE_4BYTE_ABGR);
    Graphics2D g2d = bimg.createGraphics();
    g2d.setColor(Color.white);
    g2d.fillRect(0, 0, ti.getIconWidth(), ti.getIconHeight());
    JLabel jl = new JLabel();
    jl.setForeground(new Color(0, 0, 0));
    ti.paintIcon(jl, g2d, 0, 0);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    try {
        ImageIO.write(bimg, "png", baos);
    } catch (IOException e) {
        hook.sendMessageEmbeds(EmbedUtil.errorEmbed("An error occurred!")).queue();
        return;
    }
    hook.sendMessage("").addFile(baos.toByteArray(), "card.png").queue();
}
Also used : InteractionHook(net.dv8tion.jda.api.interactions.InteractionHook) TeXFormula(org.scilab.forge.jlatexmath.TeXFormula) ParseException(org.scilab.forge.jlatexmath.ParseException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) BufferedImage(java.awt.image.BufferedImage) TeXIcon(org.scilab.forge.jlatexmath.TeXIcon)

Example 2 with ParseException

use of org.scilab.forge.jlatexmath.ParseException in project TJ-Bot by Together-Java.

the class TeXCommand method onSlashCommand.

@Override
public void onSlashCommand(@NotNull final SlashCommandEvent event) {
    String latex = Objects.requireNonNull(event.getOption(LATEX_OPTION)).getAsString();
    String userID = (Objects.requireNonNull(event.getMember()).getId());
    TeXFormula formula;
    try {
        formula = new TeXFormula(latex);
    } catch (ParseException e) {
        event.reply("That is an invalid latex: " + e.getMessage()).setEphemeral(true).queue();
        return;
    }
    event.deferReply().queue();
    Image image = formula.createBufferedImage(TeXConstants.STYLE_DISPLAY, DEFAULT_IMAGE_SIZE, FOREGROUND_COLOR, BACKGROUND_COLOR);
    if (image.getWidth(null) == -1 || image.getHeight(null) == -1) {
        event.getHook().setEphemeral(true).editOriginal(RENDERING_ERROR).queue();
        logger.warn("Unable to render latex, image does not have an accessible width or height. Formula was {}", latex);
        return;
    }
    BufferedImage renderedTextImage = new BufferedImage(image.getWidth(null), image.getHeight(null), BufferedImage.TYPE_4BYTE_ABGR);
    renderedTextImage.getGraphics().drawImage(image, 0, 0, null);
    ByteArrayOutputStream renderedTextImageStream = new ByteArrayOutputStream();
    try {
        ImageIO.write(renderedTextImage, "png", renderedTextImageStream);
    } catch (IOException e) {
        event.getHook().setEphemeral(true).editOriginal(RENDERING_ERROR).queue();
        logger.warn("Unable to render latex, could not convert the image into an attachable form. Formula was {}", latex, e);
        return;
    }
    event.getHook().editOriginal(renderedTextImageStream.toByteArray(), "tex.png").setActionRow(Button.of(ButtonStyle.DANGER, generateComponentId(userID), "Delete")).queue();
}
Also used : TeXFormula(org.scilab.forge.jlatexmath.TeXFormula) ParseException(org.scilab.forge.jlatexmath.ParseException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) BufferedImage(java.awt.image.BufferedImage) BufferedImage(java.awt.image.BufferedImage)

Aggregations

BufferedImage (java.awt.image.BufferedImage)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 IOException (java.io.IOException)2 ParseException (org.scilab.forge.jlatexmath.ParseException)2 TeXFormula (org.scilab.forge.jlatexmath.TeXFormula)2 InteractionHook (net.dv8tion.jda.api.interactions.InteractionHook)1 TeXIcon (org.scilab.forge.jlatexmath.TeXIcon)1