我如何优化 batik svg 以便它可以生成小尺寸文件
How can i optimize batik svg so it can generate small size file
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Insets;
import java.io.IOException;
import java.io.StringWriter;
import java.nio.file.Files;
import java.nio.file.Paths;
import org.apache.batik.dom.GenericDOMImplementation;
import org.apache.batik.svggen.SVGGeneratorContext;
import org.apache.batik.svggen.SVGGraphics2D;
import org.scilab.forge.jlatexmath.TeXConstants;
import org.scilab.forge.jlatexmath.TeXFormula;
import org.scilab.forge.jlatexmath.TeXFormula.TeXIconBuilder;
import org.scilab.forge.jlatexmath.TeXIcon;
import org.w3c.dom.DOMImplementation;
import org.w3c.dom.Document;
public class jlatexmath {
private final static String SVG_NS = "http://www.w3.org/2000/svg";
private final static String SVG_ROOT = "svg";
private final static float FONT_SIZE = 20;
private String renderLatex(String source) {
DOMImplementation DOMImpl = GenericDOMImplementation.getDOMImplementation();
Document document = DOMImpl.createDocument(SVG_NS, SVG_ROOT, null);
SVGGeneratorContext ctx = SVGGeneratorContext.createDefault(document);
SVGGraphics2D g = new SVGGraphics2D(ctx, true);
TeXFormula formula = new TeXFormula(source);
TeXFormula.TeXIconBuilder builder = formula.new TeXIconBuilder();
builder.setStyle(TeXConstants.STYLE_DISPLAY);
builder.setSize(FONT_SIZE);
TeXIcon icon = builder.build();
icon.setInsets(new Insets(0, 0, 0, 0));
g.setSVGCanvasSize(new Dimension(icon.getIconWidth(), icon.getIconHeight()));
g.setColor(new Color(0, 0, 0, 0));
g.fillRect(0, 0, icon.getIconWidth(), icon.getIconHeight());
icon.paintIcon(null, g, 0, 0);
StringWriter out = new StringWriter();
try {
g.stream(out, true);
out.flush();
out.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
return out.toString();
}
public static void main(String[] args) throws IOException {
String latex = "(a+b)^{2}=a^{2}+2ab+b^{2}";
jlatexmath j = new jlatexmath();
String svgString = j.renderLatex(latex);
Files.write(Paths.get("D:/latex.svg"), svgString.getBytes());
System.out.println(svgString);
}
}
我正在使用 jlatexmath-1.0.7 生成工作正常的乳胶图像。但我需要 svg 输出,所以我使用 apache batik 来生成 ~17KB 文件。一些在线工具,如 codecogs.com 为相同的乳胶输入生成 ~7KB 的 svg。我如何从 java 中的 svg 中删除不必要的信息,以便它可以生成较小尺寸的图像。
您是否尝试过在构造函数 SVGGraphics2D(ctx, true) 中传递 false
?
它使用文本而不是图形,因此生成的文件大小也更小。
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Insets;
import java.io.IOException;
import java.io.StringWriter;
import java.nio.file.Files;
import java.nio.file.Paths;
import org.apache.batik.dom.GenericDOMImplementation;
import org.apache.batik.svggen.SVGGeneratorContext;
import org.apache.batik.svggen.SVGGraphics2D;
import org.scilab.forge.jlatexmath.TeXConstants;
import org.scilab.forge.jlatexmath.TeXFormula;
import org.scilab.forge.jlatexmath.TeXFormula.TeXIconBuilder;
import org.scilab.forge.jlatexmath.TeXIcon;
import org.w3c.dom.DOMImplementation;
import org.w3c.dom.Document;
public class jlatexmath {
private final static String SVG_NS = "http://www.w3.org/2000/svg";
private final static String SVG_ROOT = "svg";
private final static float FONT_SIZE = 20;
private String renderLatex(String source) {
DOMImplementation DOMImpl = GenericDOMImplementation.getDOMImplementation();
Document document = DOMImpl.createDocument(SVG_NS, SVG_ROOT, null);
SVGGeneratorContext ctx = SVGGeneratorContext.createDefault(document);
SVGGraphics2D g = new SVGGraphics2D(ctx, true);
TeXFormula formula = new TeXFormula(source);
TeXFormula.TeXIconBuilder builder = formula.new TeXIconBuilder();
builder.setStyle(TeXConstants.STYLE_DISPLAY);
builder.setSize(FONT_SIZE);
TeXIcon icon = builder.build();
icon.setInsets(new Insets(0, 0, 0, 0));
g.setSVGCanvasSize(new Dimension(icon.getIconWidth(), icon.getIconHeight()));
g.setColor(new Color(0, 0, 0, 0));
g.fillRect(0, 0, icon.getIconWidth(), icon.getIconHeight());
icon.paintIcon(null, g, 0, 0);
StringWriter out = new StringWriter();
try {
g.stream(out, true);
out.flush();
out.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
return out.toString();
}
public static void main(String[] args) throws IOException {
String latex = "(a+b)^{2}=a^{2}+2ab+b^{2}";
jlatexmath j = new jlatexmath();
String svgString = j.renderLatex(latex);
Files.write(Paths.get("D:/latex.svg"), svgString.getBytes());
System.out.println(svgString);
}
}
我正在使用 jlatexmath-1.0.7 生成工作正常的乳胶图像。但我需要 svg 输出,所以我使用 apache batik 来生成 ~17KB 文件。一些在线工具,如 codecogs.com 为相同的乳胶输入生成 ~7KB 的 svg。我如何从 java 中的 svg 中删除不必要的信息,以便它可以生成较小尺寸的图像。
您是否尝试过在构造函数 SVGGraphics2D(ctx, true) 中传递 false
?
它使用文本而不是图形,因此生成的文件大小也更小。