Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue 39 LaTeX math support #54

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ No separation (with the `-E` option)
chosen. If the overwrite option is
selected, the image file is instead
overwriten.
-L,--latex-math Enable LaTeX math mode.
-r,--round-corners Causes all corners to be rendered as round
corners.
-S,--no-shadows Turns off the drop-shadow effect.
Expand Down Expand Up @@ -358,6 +359,46 @@ must be a space before the 'o' as well as after it. See below:

![](https://rawgit.com/stathissideris/ditaa/master/doc/images/bullet.png)

#### LaTeX mode.
If you place LaTeX formulae inside 2 ```$```s, it will be rendered using [```jlatexmath```](https://github.com/opencollab/jlatexmath). That is, if you have a following input files.
```ditaa
$Box_1$ $Box^2$
+---------------------+ +------+ /---------\
|$\sum_{i=0}^{n}x^i$ | |$cBLU$| | |
| +--->|cRED +-=-+cGRE$C_k$|
|{io} | |cXYZ | |{o} |
+----------+----------+ +---+--+ \---------/
| |
| :
| V
| +-------------------+
+---------->*$A_i$ hello $B^i$ |
| +----+
| |c8FA|
+--------------+----+
$|Set| = o-*-Freunde-*-nicht=*=diese-=-*- * töne$
o Quick brown fox jumps over
* a lazy dog.
$Q_u^i$, $C_k$, $B_r^{own}$, $F_{ox}$ jumps
over a lazy $d\cdot\frac{o}{g}$.
$\forall x \in X, \quad \exists y \leq \epsilon$
$\sin A \cos B =$
$ \frac{1}{2}\left[ \sin(A-B)+\sin(A+B) \right]$
$\frac{d}{dx}\left( \int_{0}^{x} f(u)\,du\right)=f(x).$
$v \sim \mathcal{N} (m,\sigma^2)$
```

This will be rendered as follows.

![art-latexmath-1](https://user-images.githubusercontent.com/529265/47648438-5d92be00-dbbd-11e8-90c0-e2aa1b4ee858.png)

To enable this feature, you need to give ```ditaa``` an option ```-L``` or ```--latex```.

##### Limitations

* This feature is only available when you are generating ```.png``` files. [Issue-44](https://github.com/stathissideris/ditaa/issues/44)
* Generally, LaTeX formulae are rendered narrower than the widths they occupy in ascii arts. You will sometimes see blanks after your formulae, especially when they are complicated ones, and there is no workaround to adjust this as of now. To mitigate this, you need to wait for [Issue-34](https://github.com/stathissideris/ditaa/issues/34)

#### HTML mode

When `ditaa` is run using the `--html` option, the input is an HTML
Expand Down
10 changes: 8 additions & 2 deletions project.clj
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,14 @@
[net.htmlparser.jericho/jericho-html "3.4"]
[org.apache.xmlgraphics/batik-gvt "1.9"]
[org.apache.xmlgraphics/batik-codec "1.9"]
[org.apache.xmlgraphics/batik-bridge "1.9"]]
[org.apache.xmlgraphics/batik-bridge "1.9"]
[org.apache.xmlgraphics/batik-bridge "1.9"]
[org.scilab.forge/jlatexmath "1.0.7"]]
:main org.stathissideris.ascii2image.core.CommandLineConverter
:java-source-paths ["src/java"]
:profiles {:dev {:dependencies [[junit/junit "4.12"]]
:plugins [[lein-junit "1.1.9"]]
:junit ["test/java"]
:junit-formatter :plain
:junit-results-dir "target/test-results"
:profiles {:dev {:dependencies [[junit/junit "4.12"] [com.github.dakusui/thincrest "3.6.0"]]
:java-source-paths ["test/java"]}})
Copy link
Author

@dakusui dakusui Aug 24, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change was intended to run JUnit based tests with lein test.

Original file line number Diff line number Diff line change
Expand Up @@ -19,23 +19,16 @@
*/
package org.stathissideris.ascii2image.core;

import java.awt.image.RenderedImage;
import java.io.*;

import javax.imageio.ImageIO;

import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.HelpFormatter;
import org.apache.commons.cli.Option;
import org.apache.commons.cli.OptionBuilder;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.PosixParser;
import org.apache.commons.cli.*;
import org.stathissideris.ascii2image.graphics.BitmapRenderer;
import org.stathissideris.ascii2image.graphics.Diagram;
import org.stathissideris.ascii2image.graphics.SVGRenderer;
import org.stathissideris.ascii2image.text.TextGrid;

import javax.imageio.ImageIO;
import java.awt.image.RenderedImage;
import java.io.*;

/**
*
* @author Efstathios Sideris
Expand Down Expand Up @@ -63,6 +56,7 @@ public static void main(String[] args){
cmdLnOptions.addOption("d", "debug", false, "Renders the debug grid over the resulting image.");
cmdLnOptions.addOption("r", "round-corners", false, "Causes all corners to be rendered as round corners.");
cmdLnOptions.addOption("E", "no-separation", false, "Prevents the separation of common edges of shapes.");
cmdLnOptions.addOption("L", "latex-math", false, "Enable LaTeX math mode.");
cmdLnOptions.addOption("h", "html", false, "In this case the input is an HTML file. The contents of the <pre class=\"textdiagram\"> tags are rendered as diagrams and saved in the images directory and a new HTML file is produced with the appropriate <img> tags.");
cmdLnOptions.addOption("T", "transparent", false, "Causes the diagram to be rendered on a transparent background. Overrides --background.");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,12 @@
import org.xml.sax.SAXException;

import javax.xml.parsers.ParserConfigurationException;
import java.awt.*;
import java.awt.Color;
import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Objects;

/**
*
Expand Down Expand Up @@ -80,6 +81,7 @@ public ConversionOptions(CommandLine cmdLine) throws UnsupportedEncodingExceptio

processingOptions.setAllCornersAreRound(cmdLine.hasOption("round-corners"));
processingOptions.setPerformSeparationOfCommonEdges(!cmdLine.hasOption("no-separation"));
processingOptions.enableLaTeXmath(cmdLine.hasOption("latex-math"));
renderingOptions.setAntialias(!cmdLine.hasOption("no-antialias"));
renderingOptions.setFixedSlope(cmdLine.hasOption("fixed-slope"));

Expand All @@ -101,11 +103,16 @@ public ConversionOptions(CommandLine cmdLine) throws UnsupportedEncodingExceptio
}

String encoding = (String) cmdLine.getOptionValue("encoding");
if(encoding != null){
if(encoding != null) {
new String(new byte[2], encoding);
processingOptions.setCharacterEncoding(encoding);
}


if (cmdLine.hasOption("latex")) {
processingOptions.enableLaTeXmath(
Objects.equals(cmdLine.getOptionValue("latex", "no"), "yes"));
}

if (cmdLine.hasOption("svg")){
renderingOptions.setImageType(RenderingOptions.ImageType.SVG);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@
*/
package org.stathissideris.ascii2image.core;

import java.util.HashMap;

import org.stathissideris.ascii2image.graphics.CustomShapeDefinition;

import java.util.HashMap;

/**
* @author Efstathios Sideris
*
Expand All @@ -36,6 +36,7 @@ public class ProcessingOptions {
private boolean overwriteFiles = false;
private boolean performSeparationOfCommonEdges = true;
private boolean allCornersAreRound = false;
private boolean latexMathEnabled = false;

public static final int USE_TAGS = 0;
public static final int RENDER_TAGS = 1;
Expand Down Expand Up @@ -237,7 +238,12 @@ public void putAllInCustomShapes(HashMap<String, CustomShapeDefinition> customSh
public CustomShapeDefinition getFromCustomShapes(String tagName){
return customShapes.get(tagName);
}



public void enableLaTeXmath(boolean b) {
this.latexMathEnabled = b;
}

public boolean isLaTeXmathEnabled() {
return this.latexMathEnabled;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -342,16 +342,7 @@ public RenderedImage render(Diagram diagram, BufferedImage image, RenderingOpti
Iterator<DiagramText> textIt = diagram.getTextObjects().iterator();
while(textIt.hasNext()){
DiagramText text = textIt.next();
g2.setFont(text.getFont());
if(text.hasOutline()){
g2.setColor(text.getOutlineColor());
g2.drawString(text.getText(), text.getXPos() + 1, text.getYPos());
g2.drawString(text.getText(), text.getXPos() - 1, text.getYPos());
g2.drawString(text.getText(), text.getXPos(), text.getYPos() + 1);
g2.drawString(text.getText(), text.getXPos(), text.getYPos() - 1);
}
g2.setColor(text.getColor());
g2.drawString(text.getText(), text.getXPos(), text.getYPos());
text.drawOn(g2);
}

if(options.renderDebugLines() || DEBUG_LINES){
Expand Down Expand Up @@ -392,20 +383,10 @@ public TextCanvas(ArrayList<DiagramText> textObjects){
}

public void paint(Graphics g){
Graphics g2 = (Graphics2D) g;
Graphics2D g2 = (Graphics2D) g;
Iterator<DiagramText> textIt = textObjects.iterator();
while(textIt.hasNext()){
DiagramText text = (DiagramText) textIt.next();
g2.setFont(text.getFont());
if(text.hasOutline()){
g2.setColor(text.getOutlineColor());
g2.drawString(text.getText(), text.getXPos() + 1, text.getYPos());
g2.drawString(text.getText(), text.getXPos() - 1, text.getYPos());
g2.drawString(text.getText(), text.getXPos(), text.getYPos() + 1);
g2.drawString(text.getText(), text.getXPos(), text.getYPos() - 1);
}
g2.setColor(text.getColor());
g2.drawString(text.getText(), text.getXPos(), text.getYPos());
textIt.next().drawOn(g2);
}
}
}
Expand Down
19 changes: 10 additions & 9 deletions src/java/org/stathissideris/ascii2image/graphics/Diagram.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,6 @@
*/
package org.stathissideris.ascii2image.graphics;

import java.awt.Color;
import java.awt.Font;
import java.awt.geom.Rectangle2D;
import java.util.ArrayList;
import java.util.Iterator;

import org.stathissideris.ascii2image.core.ConversionOptions;
import org.stathissideris.ascii2image.core.Pair;
import org.stathissideris.ascii2image.text.AbstractionGrid;
Expand All @@ -35,6 +29,12 @@
import org.stathissideris.ascii2image.text.TextGrid.CellStringPair;
import org.stathissideris.ascii2image.text.TextGrid.CellTagPair;

import java.awt.Color;
import java.awt.Font;
import java.awt.geom.Rectangle2D;
import java.util.ArrayList;
import java.util.Iterator;

/**
*
* @author Efstathios Sideris
Expand Down Expand Up @@ -550,11 +550,12 @@ public Diagram(TextGrid grid, ConversionOptions options) {
int maxX = getCellMaxX(lastCell);

DiagramText textObject;
boolean laTeXmathEnabled = options.processingOptions.isLaTeXmathEnabled();
if(FontMeasurer.instance().getWidthFor(string, font) > maxX - minX){ //does not fit horizontally
Font lessWideFont = FontMeasurer.instance().getFontFor(maxX - minX, string);
textObject = new DiagramText(minX, y, string, lessWideFont);
} else textObject = new DiagramText(minX, y, string, font);
textObject = new DiagramText(minX, y, string, lessWideFont, laTeXmathEnabled);
} else textObject = new DiagramText(minX, y, string, font, laTeXmathEnabled);

textObject.centerVerticallyBetween(getCellMinY(cell), getCellMaxY(cell));

//TODO: if the strings start with bullets they should be aligned to the left
Expand Down
Loading