Search in sources :

Example 1 with TeXFormula

use of org.scilab.forge.jlatexmath.TeXFormula in project freeplane by freeplane.

the class TeXText method createTeXIcon.

public TeXIcon createTeXIcon(int style, int size, int align, int maxWidth) {
    final String predefinedMacros = ResourceController.getResourceController().getProperty(LATEX_MACROS);
    StringBuffer sb = new StringBuffer();
    if (predefinedMacros != null) {
        sb.append(predefinedMacros + "\n\n");
    }
    sb.append("\n\n").append("\\text{").append(rawText).append("}");
    // LogUtils.severe(String.format("TeX='%s'", sb.toString()));
    TeXFormula tf = new TeXFormula(sb.toString());
    // tf.createTeXIcon(style, size, TeXConstants.UNIT_PIXEL, maxWidth, align, TeXConstants.UNIT_PIXEL, 40f);
    return tf.new TeXIconBuilder().setStyle(style).setSize(size).setWidth(TeXConstants.UNIT_PIXEL, maxWidth, align).setIsMaxWidth(true).setInterLineSpacing(TeXConstants.UNIT_PIXEL, /*40f*/
    size * 1.2F).build();
}
Also used : TeXFormula(org.scilab.forge.jlatexmath.TeXFormula)

Example 2 with TeXFormula

use of org.scilab.forge.jlatexmath.TeXFormula 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 3 with TeXFormula

use of org.scilab.forge.jlatexmath.TeXFormula in project jmathanim by davidgutierrezrubio.

the class LaTeXMathObject method generateDOMTreeFromLaTeX.

private Element generateDOMTreeFromLaTeX(String text) {
    Writer out = null;
    DOMImplementation domImpl = GenericDOMImplementation.getDOMImplementation();
    String latexText;
    if (mode == CompileMode.JLaTexMath) {
        latexText = "\\mbox{" + text + "}";
    } else {
        latexText = text;
    }
    TeXFormula formula = new TeXFormula(latexText);
    TeXIcon icon = formula.createTeXIcon(TeXConstants.ALIGN_LEFT, 40);
    String svgNS = "http://www.w3.org/2000/svg";
    Document document = domImpl.createDocument(svgNS, "svg", null);
    // Create an instance of the SVG Generator.
    SVGGeneratorContext ctx = SVGGeneratorContext.createDefault(document);
    SVGGraphics2D svgGenerator = new SVGGraphics2D(ctx, true);
    ctx.setEmbeddedFontsOn(true);
    icon.paintIcon(null, svgGenerator, 0, 0);
    DOMTreeManager domTreeManager = svgGenerator.getDOMTreeManager();
    Element domFactory = svgGenerator.getRoot();
    return domFactory;
}
Also used : DOMTreeManager(org.apache.batik.svggen.DOMTreeManager) TeXFormula(org.scilab.forge.jlatexmath.TeXFormula) Element(org.w3c.dom.Element) DOMImplementation(org.w3c.dom.DOMImplementation) GenericDOMImplementation(org.apache.batik.dom.GenericDOMImplementation) SVGGraphics2D(org.apache.batik.svggen.SVGGraphics2D) Document(org.w3c.dom.Document) SVGGeneratorContext(org.apache.batik.svggen.SVGGeneratorContext) PrintWriter(java.io.PrintWriter) FileWriter(java.io.FileWriter) Writer(java.io.Writer) TeXIcon(org.scilab.forge.jlatexmath.TeXIcon)

Example 4 with TeXFormula

use of org.scilab.forge.jlatexmath.TeXFormula in project MeteoInfo by meteoinfo.

the class Draw method getStringDimension.

/**
 * Get string dimension
 *
 * @param str String
 * @param g Graphics2D
 * @param isLaTeX Is LaTeX or not
 * @return String dimension
 */
public static Dimension getStringDimension(String str, Graphics2D g, boolean isLaTeX) {
    if (isLaTeX) {
        float size = g.getFont().getSize2D();
        // create a formula
        TeXFormula formula = new TeXFormula(str);
        // render the formla to an icon of the same size as the formula.
        TeXIcon icon = formula.createTeXIcon(TeXConstants.STYLE_TEXT, size);
        // insert a border
        // icon.setInsets(new Insets(5, 5, 5, 5));
        // return new Dimension(icon.getIconWidth(), icon.getIconHeight());
        int width = (int) icon.getTrueIconWidth() + 10;
        int height = (int) icon.getTrueIconHeight();
        // int height = icon.getIconHeight();
        return new Dimension(width, height);
    } else {
        FontMetrics metrics = g.getFontMetrics();
        // int height = (int) (metrics.getAscent() * 5.f / 6.f);
        int height = metrics.getAscent();
        // return new Dimension(metrics.stringWidth(str), metrics.getHeight());
        return new Dimension(metrics.stringWidth(str), height);
    }
}
Also used : FontMetrics(java.awt.FontMetrics) TeXFormula(org.scilab.forge.jlatexmath.TeXFormula) Dimension(java.awt.Dimension) TexturePaint(java.awt.TexturePaint) TeXIcon(org.scilab.forge.jlatexmath.TeXIcon)

Example 5 with TeXFormula

use of org.scilab.forge.jlatexmath.TeXFormula in project saturnGL by chompa111.

the class Latex method generateExp.

public static List<Gobject> generateExp(String s, double x, double y, Color color) {
    TeXFormula a = new TeXFormula(s);
    var icon2 = a.createTeXIcon(TeXConstants.STYLE_TEXT, 50);
    var img = new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB);
    var fakeGraphics = new GraphicsdrawCharProxy((Graphics2D) img.getGraphics(), color);
    icon2.paintIcon(null, fakeGraphics, (int) x, (int) y);
    return fakeGraphics.getCharList();
}
Also used : GraphicsdrawCharProxy(graphical.basics.gobject.latex.GraphicsdrawCharProxy) TeXFormula(org.scilab.forge.jlatexmath.TeXFormula) BufferedImage(java.awt.image.BufferedImage)

Aggregations

TeXFormula (org.scilab.forge.jlatexmath.TeXFormula)13 BufferedImage (java.awt.image.BufferedImage)5 TeXIcon (org.scilab.forge.jlatexmath.TeXIcon)5 GraphicsdrawCharProxy (graphical.basics.gobject.latex.GraphicsdrawCharProxy)3 Dimension (java.awt.Dimension)2 FontMetrics (java.awt.FontMetrics)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 IOException (java.io.IOException)2 ParseException (org.scilab.forge.jlatexmath.ParseException)2 Font (java.awt.Font)1 Insets (java.awt.Insets)1 TexturePaint (java.awt.TexturePaint)1 AffineTransform (java.awt.geom.AffineTransform)1 FileWriter (java.io.FileWriter)1 PrintWriter (java.io.PrintWriter)1 Writer (java.io.Writer)1 InteractionHook (net.dv8tion.jda.api.interactions.InteractionHook)1 GenericDOMImplementation (org.apache.batik.dom.GenericDOMImplementation)1 DOMTreeManager (org.apache.batik.svggen.DOMTreeManager)1 SVGGeneratorContext (org.apache.batik.svggen.SVGGeneratorContext)1