1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
| package demolatexfx;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.net.URLStreamHandler;
import java.util.Collections;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import javax.imageio.ImageIO;
import javax.swing.JLabel;
import org.scilab.forge.jlatexmath.TeXConstants;
import org.scilab.forge.jlatexmath.TeXFormula;
import org.scilab.forge.jlatexmath.TeXIcon;
public class LatexURLHandler extends URLStreamHandler {
private final static Map<String, Color> NAMED_COLOR = createAllowedColors();
private final static String ALLOWED_COLOR_NAMES = toString(NAMED_COLOR);
@Override
protected URLConnection openConnection(URL url) throws IOException {
return new LatexConnection(url);
}
private static class LatexConnection extends URLConnection {
private static final String SIZE_PARAM = "size";
private static final String COLOR_PARAM = "color";
private static final String TYPE_PARAM = "type";
public LatexConnection(URL url) {
super(url);
if (!"latex".equals(url.getProtocol())) {
throw new IllegalArgumentException();
}
}
@Override
public void connect() throws IOException {
}
@Override
public String getContentType() {
return "image/png";
}
@Override
public InputStream getInputStream() throws IOException {
try {
return createInputStream();
}
catch(IOException e) {
e.printStackTrace();
throw e;
}
}
private InputStream createInputStream() throws IOException {
String latex = decodePath(url.getPath());
int size = 20;
Color color = Color.BLACK;
String type = "png";
String query = url.getQuery();
if (query != null) {
String[] params = query.split("&");
Set<String> setParams = new HashSet<>();
for (String param : params) {
String[] paramPart = param.split("=");
if ( paramPart.length!=2 ) {
throw new IOException(String.format("Unknown parameter or no value: %s.",param));
}
String name = paramPart[0].trim().toLowerCase();
if ( setParams.contains(name) ) {
throw new IOException(String.format("Duplicate parameter %s.", name));
}
String value = decode(paramPart[1]);
switch (name) {
case SIZE_PARAM:
try {
size = Integer.parseInt(value);
if ( size<1 ) {
throw new IOException(String.format("Size parameter must be greater than 0: %s.",value));
}
} catch (NumberFormatException e) {
throw new IOException(String.format("Size parameter must be an integer value: %s.",value), e);
}
break;
case TYPE_PARAM:
if ( ImageIO.getImageWritersByFormatName(value).hasNext() ) {
type = value;
}
else {
throw new IOException(String.format("Unknown image format: %s.",value));
}
break;
case COLOR_PARAM:
String colorName = value.toLowerCase();
if ( NAMED_COLOR.containsKey(colorName) ) {
color = NAMED_COLOR.get(colorName);
}
else {
try {
int colorcode = Integer.parseInt(value, 16);
color = new Color(colorcode);
} catch (NumberFormatException e) {
throw new IOException(String.format("Color must be a hexadecimal value or a name (%s) : %s.",value, ALLOWED_COLOR_NAMES), e);
}
}
break;
default:
throw new IOException(String.format("Unknown parameter: %s."));
}
setParams.add(name);
}
}
TeXFormula formula = new TeXFormula(latex);
TeXIcon icon = formula.new TeXIconBuilder()
.setStyle(TeXConstants.STYLE_DISPLAY).setSize(size).build();
BufferedImage image = new BufferedImage(icon.getIconWidth(),
icon.getIconHeight(), BufferedImage.TYPE_INT_ARGB);
Graphics2D g = image.createGraphics();
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
JLabel jl = new JLabel();
jl.setForeground(color);
icon.paintIcon(jl, g, 0, 0);
g.dispose();
try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
ImageIO.write(image, type, outputStream);
return new BufferedInputStream(new ByteArrayInputStream(
outputStream.toByteArray()));
}
}
}
private static String decodePath(String value) throws UnsupportedEncodingException {
return URLDecoder.decode(value, "UTF-8");
}
private static String decode(String value) throws IOException {
return URLDecoder.decode(value, "UTF8").trim();
}
private static Map<String, Color> createAllowedColors() {
Map<String, Color> namedColors = new TreeMap<String, Color>();
for(java.lang.reflect.Field field : Color.class.getFields()) {
if ( Color.class.equals(field.getType()) && java.lang.reflect.Modifier.isStatic(field.getModifiers()) ) {
try {
namedColors.put( field.getName().toLowerCase().replaceAll("_", ""), (Color)field.get(null));
} catch (IllegalArgumentException | IllegalAccessException e) {
// ignore
}
}
}
return Collections.unmodifiableMap(namedColors);
}
private static String toString(Map<String, Color> namedColor) {
return namedColor.keySet().stream()
.map(Object::toString)
.collect(java.util.stream.Collectors.joining(", "));
}
public static String encodeLatex(String url) throws UnsupportedEncodingException {
return URLEncoder.encode(url, "UTF-8");
}
} |
Partager