Search in sources :

Example 6 with Triple

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;
}
Also used : Triple(org.antlr.v4.runtime.misc.Triple) ST(org.stringtemplate.v4.ST) STGroupFile(org.stringtemplate.v4.STGroupFile) File(java.io.File)

Example 7 with Triple

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;
}
Also used : Path(java.nio.file.Path) XPath(org.antlr.v4.runtime.tree.xpath.XPath) IOException(java.io.IOException) EqnVar(us.parr.bookish.model.EqnVar) EqnIndexedVecVar(us.parr.bookish.model.EqnIndexedVecVar) EqnVecVar(us.parr.bookish.model.EqnVecVar) InlineEquation(us.parr.bookish.model.InlineEquation) STGroupFile(org.stringtemplate.v4.STGroupFile) File(java.io.File) EqnIndexedVar(us.parr.bookish.model.EqnIndexedVar)

Example 8 with Triple

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;
}
Also used : Triple(org.antlr.v4.runtime.misc.Triple) ST(org.stringtemplate.v4.ST) Pattern(java.util.regex.Pattern) STGroupFile(org.stringtemplate.v4.STGroupFile) File(java.io.File)

Example 9 with Triple

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();
}
Also used : ParserRuleContext(org.antlr.v4.runtime.ParserRuleContext) CommonTokenStream(org.antlr.v4.runtime.CommonTokenStream) Token(org.antlr.v4.runtime.Token) TokenStreamRewriter(org.antlr.v4.runtime.TokenStreamRewriter)

Example 10 with Triple

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]-&gt;o-&gt;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);
}
Also used : LoopEndState(org.antlr.v4.runtime.atn.LoopEndState) PlusBlockStartState(org.antlr.v4.runtime.atn.PlusBlockStartState) PlusLoopbackState(org.antlr.v4.runtime.atn.PlusLoopbackState) BlockAST(org.antlr.v4.tool.ast.BlockAST) QuantifierAST(org.antlr.v4.tool.ast.QuantifierAST) Rule(org.antlr.v4.tool.Rule) LeftRecursiveRule(org.antlr.v4.tool.LeftRecursiveRule) BlockEndState(org.antlr.v4.runtime.atn.BlockEndState) ATNState(org.antlr.v4.runtime.atn.ATNState)

Aggregations

Token (org.antlr.v4.runtime.Token)4 ATNState (org.antlr.v4.runtime.atn.ATNState)4 LeftRecursiveRule (org.antlr.v4.tool.LeftRecursiveRule)4 Rule (org.antlr.v4.tool.Rule)4 File (java.io.File)3 IOException (java.io.IOException)3 Path (java.nio.file.Path)3 CommonTokenStream (org.antlr.v4.runtime.CommonTokenStream)3 ParserRuleContext (org.antlr.v4.runtime.ParserRuleContext)3 TokenStreamRewriter (org.antlr.v4.runtime.TokenStreamRewriter)3 Triple (org.antlr.v4.runtime.misc.Triple)3 XPath (org.antlr.v4.runtime.tree.xpath.XPath)3 STGroupFile (org.stringtemplate.v4.STGroupFile)3 BlockEndState (org.antlr.v4.runtime.atn.BlockEndState)2 LoopEndState (org.antlr.v4.runtime.atn.LoopEndState)2 PlusBlockStartState (org.antlr.v4.runtime.atn.PlusBlockStartState)2 StarBlockStartState (org.antlr.v4.runtime.atn.StarBlockStartState)2 BlockAST (org.antlr.v4.tool.ast.BlockAST)2 QuantifierAST (org.antlr.v4.tool.ast.QuantifierAST)2 ST (org.stringtemplate.v4.ST)2