001package conexp.fx.gui.util; 002 003/* 004 * #%L 005 * Concept Explorer FX 006 * %% 007 * Copyright (C) 2010 - 2023 Francesco Kriegel 008 * %% 009 * This program is free software: you can redistribute it and/or modify 010 * it under the terms of the GNU General Public License as 011 * published by the Free Software Foundation, either version 3 of the 012 * License, or (at your option) any later version. 013 * 014 * This program is distributed in the hope that it will be useful, 015 * but WITHOUT ANY WARRANTY; without even the implied warranty of 016 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 017 * GNU General Public License for more details. 018 * 019 * You should have received a copy of the GNU General Public 020 * License along with this program. If not, see 021 * <http://www.gnu.org/licenses/gpl-3.0.html>. 022 * #L% 023 */ 024 025import java.awt.Graphics2D; 026import java.awt.Toolkit; 027import java.awt.image.BufferedImage; 028import java.awt.image.FilteredImageSource; 029import java.awt.image.RGBImageFilter; 030 031import org.scilab.forge.jlatexmath.ParseException; 032import org.scilab.forge.jlatexmath.TeXConstants; 033import org.scilab.forge.jlatexmath.TeXFormula; 034 035import conexp.fx.gui.ConExpFX; 036import javafx.beans.binding.ObjectBinding; 037import javafx.beans.value.ObservableValue; 038import javafx.embed.swing.SwingFXUtils; 039import javafx.scene.image.Image; 040 041public class LaTeX { 042 043 public final static Image toFXImage(final String string, final float height) { 044 if (string == null) 045 return new Image(ConExpFX.class.getResourceAsStream("image/16x16/warning.png")); 046 try { 047 final BufferedImage texImage = (BufferedImage) new TeXFormula( 048 "\\sf\\mbox{" + string 049 .replaceAll("\\\\kern-?\\d*(\\.\\d+)?[a-zA-Z]{2}", "") 050 .replace("%", "\\%") 051 .replace("@", "\\@") 052 .replace("&", "\\&") + "}") 053 .createBufferedImage(TeXConstants.STYLE_DISPLAY, height, java.awt.Color.BLACK, java.awt.Color.WHITE); 054 final BufferedImage TeXImage = 055 new BufferedImage(texImage.getWidth(), texImage.getHeight(), BufferedImage.TYPE_INT_ARGB); 056 final Graphics2D graphics = TeXImage.createGraphics(); 057 graphics.drawImage( 058 Toolkit.getDefaultToolkit().createImage(new FilteredImageSource(texImage.getSource(), new RGBImageFilter() { 059 060 public final int filterRGB(final int x, final int y, final int rgb) { 061 if (0xFFEEEEEE < rgb) 062 return 0x00FFFFFF; 063 return rgb; 064 } 065 })), 066 0, 067 0, 068 null); 069 graphics.dispose(); 070 return SwingFXUtils.toFXImage(TeXImage, null); 071 } catch (ParseException e) { 072 System.err.println(string); 073 e.printStackTrace(); 074 return null; 075 } 076 } 077 078 public final static ObjectBinding<Image> toFXImageBinding(final ObservableValue<String> string, final float height) { 079 return new ObjectBinding<Image>() { 080 081 { 082 bind(string); 083 } 084 085 @Override 086 protected final Image computeValue() { 087 return LaTeX.toFXImage(string.getValue(), height); 088 } 089 }; 090 } 091 092 public final static ObjectBinding<Image> 093 toFXImageBinding(final ObservableValue<String> string, final ObservableValue<Number> height) { 094 return new ObjectBinding<Image>() { 095 096 { 097 bind(string, height); 098 } 099 100 @Override 101 protected final Image computeValue() { 102 return LaTeX.toFXImage(string.getValue(), height.getValue().floatValue()); 103 } 104 }; 105 } 106 107}