Skip to content
Closed
Show file tree
Hide file tree
Changes from all 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
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,11 @@ build/testclasses/
releases/
tests/images/
tests/images-expected/

# pom.xml for ditaa is available at https://gist.github.com/dakusui/c6b7532cbaae0ed7317036c389d761f4
pom.xml
# Files copied by mvn's build procedure
tests/text/
tests/latex/
tests/build.xml
target/
55 changes: 55 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,60 @@ 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
4 changes: 3 additions & 1 deletion project.clj
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
[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"]
[com.github.dakusui/thincrest "3.6.0"]
[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"]]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@
import java.io.UnsupportedEncodingException;

/**
*
* @author Efstathios Sideris
*/
public class CommandLineConverter {
Expand All @@ -67,6 +66,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 @@ -29,9 +29,9 @@
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Objects;

/**
*
* @author Efstathios Sideris
*/
public class ConversionOptions {
Expand All @@ -49,8 +49,10 @@ public void setDebug(boolean value) {
public ConversionOptions() {
}

/** Parse a color from a 6- or 8-digit hex string. For example, FF0000 is red.
* If eight digits, last two digits are alpha. */
/**
* Parse a color from a 6- or 8-digit hex string. For example, FF0000 is red.
* If eight digits, last two digits are alpha.
*/
public static Color parseColor(String hexString) {
if (hexString.length() == 6) {
return new Color(Integer.parseInt(hexString, 16));
Expand Down Expand Up @@ -80,6 +82,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 Down Expand Up @@ -107,6 +110,10 @@ public ConversionOptions(CommandLine cmdLine) throws UnsupportedEncodingExceptio
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 @@ -35,6 +35,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,5 +238,11 @@ 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 @@ -338,16 +338,7 @@ public RenderedImage render(Diagram diagram, BufferedImage image, RenderingOptio
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 @@ -387,20 +378,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
80 changes: 39 additions & 41 deletions src/java/org/stathissideris/ascii2image/graphics/Diagram.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
import java.util.Iterator;

/**
*
* @author Efstathios Sideris
*/
public class Diagram {
Expand All @@ -53,54 +52,53 @@ public class Diagram {


/**
*
* <p>An outline of the inner workings of this very important (and monstrous)
* constructor is presented here. Boundary processing is the first step
* of the process:</p>
*
* <ol>
* <li>Copy the grid into a work grid and remove all type-on-line
* and point markers from the work grid</li>
* <li>Split grid into distinct shapes by plotting the grid
* onto an AbstractionGrid and its getDistinctShapes() method.</li>
* <li>Find all the possible boundary sets of each of the
* distinct shapes. This can produce duplicate shapes (if the boundaries
* are the same when filling from the inside and the outside).</li>
* <li>Remove duplicate boundaries.</li>
* <li>Remove obsolete boundaries. Obsolete boundaries are the ones that are
* the sum of their parts when plotted as filled shapes. (see method
* removeObsoleteShapes())</li>
* <li>Separate the found boundary sets to open, closed or mixed
* (See CellSet class on how its done).</li>
* <li>Are there any closed boundaries?
* <ul>
* <li>YES. Subtract all the closed boundaries from each of the
* open ones. That should convert the mixed shapes into open.</li>
* <li>NO. In this (harder) case, we use the method
* breakTrulyMixedBoundaries() of CellSet to break boundaries
* into open and closed shapes (would work in any case, but it's
* probably slower than the other method). This method is based
* on tracing from the lines' ends and splitting when we get to
* an intersection.</li>
* </ul>
* </li>
* <li>If we had to eliminate any mixed shapes, we separate the found
* boundary sets again to open, closed or mixed.</li>
* <li>Copy the grid into a work grid and remove all type-on-line
* and point markers from the work grid</li>
* <li>Split grid into distinct shapes by plotting the grid
* onto an AbstractionGrid and its getDistinctShapes() method.</li>
* <li>Find all the possible boundary sets of each of the
* distinct shapes. This can produce duplicate shapes (if the boundaries
* are the same when filling from the inside and the outside).</li>
* <li>Remove duplicate boundaries.</li>
* <li>Remove obsolete boundaries. Obsolete boundaries are the ones that are
* the sum of their parts when plotted as filled shapes. (see method
* removeObsoleteShapes())</li>
* <li>Separate the found boundary sets to open, closed or mixed
* (See CellSet class on how its done).</li>
* <li>Are there any closed boundaries?
* <ul>
* <li>YES. Subtract all the closed boundaries from each of the
* open ones. That should convert the mixed shapes into open.</li>
* <li>NO. In this (harder) case, we use the method
* breakTrulyMixedBoundaries() of CellSet to break boundaries
* into open and closed shapes (would work in any case, but it's
* probably slower than the other method). This method is based
* on tracing from the lines' ends and splitting when we get to
* an intersection.</li>
* </ul>
* </li>
* <li>If we had to eliminate any mixed shapes, we separate the found
* boundary sets again to open, closed or mixed.</li>
* </ol>
*
* <p>At this stage, the boundary processing is all complete and we
* proceed with using those boundaries to create the shapes:</p>
*
* <ol>
* <li>Create closed shapes.</li>
* <li>Create open shapes. That's when the line end corrections are
* also applied, concerning the positioning of the ends of lines
* see methods connectEndsToAnchors() and moveEndsToCellEdges() of
* DiagramShape.</li>
* <li>Assign color codes to closed shapes.</li>
* <li>Assign extended markup tags to closed shapes.</p>
* <li>Create arrowheads.</p>
* <li>Create point markers.</p>
* <li>Create closed shapes.</li>
* <li>Create open shapes. That's when the line end corrections are
* also applied, concerning the positioning of the ends of lines
* see methods connectEndsToAnchors() and moveEndsToCellEdges() of
* DiagramShape.</li>
* <li>Assign color codes to closed shapes.</li>
* <li>Assign extended markup tags to closed shapes.</p>
* <li>Create arrowheads.</p>
* <li>Create point markers.</p>
* </ol>
*
* <p>Finally, the text processing occurs: [pending]</p>
Expand Down Expand Up @@ -565,11 +563,12 @@ else if (type == CellSet.TYPE_MIXED)
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);
textObject = new DiagramText(minX, y, string, lessWideFont, laTeXmathEnabled);
} else
textObject = new DiagramText(minX, y, string, font);
textObject = new DiagramText(minX, y, string, font, laTeXmathEnabled);

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

Expand Down Expand Up @@ -648,7 +647,6 @@ public ArrayList<DiagramShape> getAllDiagramShapes() {
* when plotted as filled shapes.
*
* @return true if it removed any obsolete.
*
*/
private boolean removeObsoleteShapes(TextGrid grid, ArrayList<CellSet> sets) {
if (DEBUG)
Expand Down
Loading