use of org.antlr.v4.runtime.misc.Triple in project bookish by parrt.
the class Tex2SVG method tex2svg.
public Triple<String, Float, Float> tex2svg(String latex, LatexType type, int fontsize) {
try {
latex = latex.trim();
String outputDir = new File(tool.outputDir).getAbsolutePath();
String buildDir = tool.getBuildDir();
if (!Files.exists(Paths.get(outputDir))) {
Files.createDirectories(Paths.get(outputDir));
}
if (!Files.exists(Paths.get(buildDir))) {
Files.createDirectories(Paths.get(buildDir));
}
if (!Files.exists(Paths.get(outputDir + "/images"))) {
Files.createSymbolicLink(Paths.get(outputDir + "/images"), Paths.get(tool.outputDir + "/images"));
}
String texfilename = buildDir + "/temp.tex";
ST template = null;
switch(type) {
case EQN:
template = templates.getInstanceOf("eqntex");
break;
case BLOCKEQN:
template = templates.getInstanceOf("blockeqntex");
break;
case LATEX:
template = templates.getInstanceOf("blocktex");
break;
}
template.add("text", latex);
template.add("fontsize", fontsize);
Files.write(Paths.get(texfilename), template.render().getBytes());
// System.out.println("wrote "+texfilename);
String[] results = execInDir(buildDir, "xelatex", "-shell-escape", "-interaction=nonstopmode", "temp.tex");
// println(results.a)
float height = 0, depth = 0;
for (String line : results[0].split("\n")) {
String prefix = "// bookish metrics: ";
if (line.startsWith(prefix)) {
int first = prefix.length();
int comma = line.indexOf(',');
String heightS = line.substring(first, comma - "pt".length());
String depthS = line.substring(comma + 1, line.indexOf('p', comma));
height = Float.parseFloat(heightS);
depth = Float.parseFloat(depthS);
}
if (line.startsWith("!") || line.startsWith("l.")) {
System.err.println(line);
System.err.println(latex);
}
}
if (results[1].length() > 0) {
System.err.println(results[1]);
}
results = execInDir(buildDir, "pdfcrop", "temp.pdf");
if (results[1].length() > 0) {
System.err.println(results[1]);
}
results = execInDir(buildDir, "pdf2svg", "temp-crop.pdf", "temp.svg");
if (results[1].length() > 0) {
System.err.println(results[1]);
}
String svgfilename = buildDir + "/temp.svg";
return new Triple<>(new String(Files.readAllBytes(Paths.get(svgfilename))), height, depth);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
use of org.antlr.v4.runtime.misc.Triple in project bookish by parrt.
the class Translator method visitEqn.
@Override
public OutputModelObject visitEqn(BookishParser.EqnContext ctx) {
String eqn = stripQuotes(ctx.getText());
if (target == Tool.Target.LATEX || target == Tool.Target.LATEX_BOOK) {
return new InlineEquation(null, eqn, -1, -1);
}
// check for special cases like $w$ and $\mathbf{w}_i$.
List<String> elements = extract(eqnVarPattern, eqn);
if (elements.size() > 0) {
return new EqnVar(elements.get(0));
}
elements = extract(eqnVecVarPattern, eqn);
if (elements.size() > 0) {
return new EqnVecVar(elements.get(0));
}
elements = extract(eqnVecVarPattern2, eqn);
if (elements.size() > 0) {
return new EqnVecVar(elements.get(0));
}
elements = extract(eqnIndexedVarPattern, eqn);
if (elements.size() > 0) {
return new EqnIndexedVar(elements.get(0), elements.get(1));
}
elements = extract(eqnIndexedVecVarPattern, eqn);
if (elements.size() > 0) {
return new EqnIndexedVecVar(elements.get(0), elements.get(1));
}
elements = extract(eqnIndexedVecVarPattern2, eqn);
if (elements.size() > 0) {
return new EqnIndexedVecVar(elements.get(0), elements.get(1));
}
float height = 0, depth = 0;
String prefix = String.format("eqn-%s", md5hash(eqn));
// String prefix = String.format("images/eqn-%s",hash(eqn));
File[] files = new File(outputDir + "/images").listFiles((dir, name) -> name.startsWith(prefix));
String existing = null;
if (files != null && files.length > 0) {
existing = files[0].getName();
int i = existing.indexOf("-depth");
int j = existing.indexOf(".svg", i);
String depthS = existing.substring(i + "-depth".length(), j);
depth = Float.parseFloat(depthS);
return new InlineEquation("images/" + existing, eqn, -1, depth);
}
Triple<String, Float, Float> results = texConverter.tex2svg(eqn, Tex2SVG.LatexType.EQN, INLINE_EQN_FONT_SIZE);
String svg = results.a;
height = results.b;
depth = results.c;
try {
String src = String.format("%s/images/%s-depth%06.2f.svg", outputDir, prefix, depth);
Path outpath = Paths.get(src);
System.out.println(outpath);
Files.write(outpath, svg.getBytes());
String relativePath = String.format("images/%s-depth%06.2f.svg", prefix, depth);
return new InlineEquation(relativePath, eqn, height, depth);
} catch (IOException ioe) {
ioe.printStackTrace();
}
return null;
}
use of org.antlr.v4.runtime.misc.Triple in project bookish by parrt.
the class Tex2SVG method tex2svg.
public Triple<String, Float, Float> tex2svg(String latex, LatexType type, int fontsize) {
try {
latex = latex.trim();
String tmpdir = new File(outputDir).getAbsolutePath();
if (!Files.exists(Paths.get(tmpdir))) {
Files.createDirectories(Paths.get(tmpdir));
}
if (!Files.exists(Paths.get(tmpdir + "/images"))) {
Files.createSymbolicLink(Paths.get(tmpdir + "/images"), Paths.get(outputDir + "/images"));
}
String texfilename = tmpdir + "/temp.tex";
ST template = null;
switch(type) {
case EQN:
template = templates.getInstanceOf("eqntex");
break;
case BLOCKEQN:
template = templates.getInstanceOf("blockeqntex");
break;
case LATEX:
template = templates.getInstanceOf("blocktex");
break;
}
template.add("text", latex);
template.add("fontsize", fontsize);
Files.write(Paths.get(texfilename), template.render().getBytes());
// System.out.println("wrote "+texfilename);
String[] results = execInDir(tmpdir, "xelatex", "-shell-escape", "-interaction=nonstopmode", "temp.tex");
// println(results.a)
String metricsRegex = "// bookish metrics: ([0-9]*[.][0-9]+)pt, ([0-9]*[.][0-9]+)pt";
Pattern metricsPattern = Pattern.compile("metricsRegex");
String log = ParrtIO.load(tmpdir + "/temp.log");
float height = 0, depth = 0;
for (String line : results[0].split("\n")) {
String prefix = "// bookish metrics: ";
if (line.startsWith(prefix)) {
int first = prefix.length();
int comma = line.indexOf(',');
String heightS = line.substring(first, comma - "pt".length());
String depthS = line.substring(comma + 1, line.indexOf('p', comma));
height = Float.parseFloat(heightS);
depth = Float.parseFloat(depthS);
}
if (line.startsWith("!") || line.startsWith("l.")) {
System.err.println(line);
System.err.println(latex);
}
}
if (results[1].length() > 0) {
System.err.println(results[1]);
}
results = execInDir(tmpdir, "pdfcrop", "temp.pdf");
if (results[1].length() > 0) {
System.err.println(results[1]);
}
results = execInDir(tmpdir, "pdf2svg", "temp-crop.pdf", "temp.svg");
if (results[1].length() > 0) {
System.err.println(results[1]);
}
String svgfilename = tmpdir + "/temp.svg";
return new Triple<>(new String(Files.readAllBytes(Paths.get(svgfilename))), height, depth);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
use of org.antlr.v4.runtime.misc.Triple in project kripton by xcesco.
the class ContentUriChecker method replaceInternalFromUri.
private String replaceInternalFromUri(String input, final List<Triple<Token, Token, String>> replace, UriBaseListener rewriterListener) {
Pair<ParserRuleContext, CommonTokenStream> parser = prepareUri(input);
pathSegmentIndex = -1;
walker.walk(rewriterListener, parser.value0);
TokenStreamRewriter rewriter = new TokenStreamRewriter(parser.value1);
for (Triple<Token, Token, String> item : replace) {
rewriter.replace(item.value0, item.value1, item.value2);
}
return rewriter.getText();
}
use of org.antlr.v4.runtime.misc.Triple in project antlr4 by antlr.
the class ParserATNFactory method plus.
/**
* From {@code (blk)+} build
*
* <pre>
* |---------|
* v |
* [o-blk-o]->o->o
* </pre>
*
* We add a decision for loop back node to the existing one at {@code blk}
* start.
*/
@Override
public Handle plus(GrammarAST plusAST, Handle blk) {
PlusBlockStartState blkStart = (PlusBlockStartState) blk.left;
BlockEndState blkEnd = (BlockEndState) blk.right;
preventEpsilonClosureBlocks.add(new Triple<Rule, ATNState, ATNState>(currentRule, blkStart, blkEnd));
PlusLoopbackState loop = newState(PlusLoopbackState.class, plusAST);
loop.nonGreedy = !((QuantifierAST) plusAST).isGreedy();
atn.defineDecisionState(loop);
LoopEndState end = newState(LoopEndState.class, plusAST);
blkStart.loopBackState = loop;
end.loopBackState = loop;
plusAST.atnState = loop;
// blk can see loop back
epsilon(blkEnd, loop);
BlockAST blkAST = (BlockAST) plusAST.getChild(0);
if (((QuantifierAST) plusAST).isGreedy()) {
if (expectNonGreedy(blkAST)) {
g.tool.errMgr.grammarError(ErrorType.EXPECTED_NON_GREEDY_WILDCARD_BLOCK, g.fileName, plusAST.getToken(), plusAST.getToken().getText());
}
// loop back to start
epsilon(loop, blkStart);
// or exit
epsilon(loop, end);
} else {
// if not greedy, priority to exit branch; make it first
// exit
epsilon(loop, end);
// loop back to start
epsilon(loop, blkStart);
}
return new Handle(blkStart, end);
}
Aggregations