Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,31 @@ public static void main(String[] args){
.create()
);

cmdLnOptions.addOption(
OptionBuilder.withLongOpt("font-family")
.withDescription("Font Family.")
.hasArg()
.withArgName("FONTFAMILY")
.create()
);

cmdLnOptions.addOption(
OptionBuilder.withLongOpt("font-size")
.withDescription("Font Size.")
.hasArg()
.withArgName("FONTSIZE")
.create()
);

cmdLnOptions.addOption(
OptionBuilder.withLongOpt("short-color-codes")
.withDescription("Short Custom Color Codes.")
.hasArg()
.withArgName("SHORTCOLORCODES")
.create()
);


//TODO: uncomment this for next version:
// cmdLnOptions.addOption(
// OptionBuilder.withLongOpt("config")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.Option;
import org.stathissideris.ascii2image.graphics.CustomShapeDefinition;
import org.stathissideris.ascii2image.graphics.FontMeasurer;
import org.xml.sax.SAXException;

import javax.xml.parsers.ParserConfigurationException;
Expand Down Expand Up @@ -114,6 +115,23 @@ public ConversionOptions(CommandLine cmdLine) throws UnsupportedEncodingExceptio
renderingOptions.setFontURL(cmdLine.getOptionValue("svg-font-url"));
}

if (cmdLine.hasOption("short-color-codes")) {
processingOptions.setShortColorCodes(cmdLine.getOptionValue("short-color-codes"));
}

if (cmdLine.hasOption(("font-family"))) {
processingOptions.setFontFamily(cmdLine.getOptionValue("font-family"));
FontMeasurer.instance().setFontFamilyName(processingOptions.getFontFamily());
}

if (cmdLine.hasOption(("font-size"))) {
processingOptions.setFontSize(cmdLine.getOptionValue("font-size"));
FontMeasurer.instance().setFontSize(Integer.parseInt(cmdLine.getOptionValue("font-size")));
}
if (cmdLine.hasOption(("font-style"))) {
processingOptions.setFontSize(cmdLine.getOptionValue("font-style"));
FontMeasurer.setFontStyle(cmdLine.getOptionValue("font-style").equals("PLAIN")?Font.PLAIN:Font.BOLD);
}
ConfigurationParser configParser = new ConfigurationParser();
try {
for (Option curOption : cmdLine.getOptions()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@
*/
package org.stathissideris.ascii2image.core;

import java.awt.*;
import java.security.InvalidParameterException;
import java.util.HashMap;
import java.util.Map;

import org.stathissideris.ascii2image.graphics.CustomShapeDefinition;

Expand All @@ -37,6 +40,76 @@ public class ProcessingOptions {
private boolean performSeparationOfCommonEdges = true;
private boolean allCornersAreRound = false;

public String getFontFamily() {
return fontFamily;
}

public void setFontFamily(final String fontFamily) {
this.fontFamily = fontFamily;
}

public String getFontSize() {
return fontSize;
}

public void setFontSize(final String fontSize) {
this.fontSize = fontSize;
}

private String fontFamily;
private String fontSize;
private String fontStyle;

public String getFontStyle() {
return fontStyle;
}

public void setFontStyle(final String fontStyle) {
this.fontStyle = fontStyle;
}

public boolean isShortColorCodes() {
return shortColorCodes != null;
}
public String getShortColorCodes() {
return shortColorCodes;
}

public void setShortColorCodes(final String shortColorCodes) {
this.shortColorCodes = shortColorCodes;
shortColorCodeMap.put('0', new Color(0, 0, 0)); // Black
shortColorCodeMap.put('2', new Color(0x5*17, 0x5*17, 0xB*17)); //Blue
shortColorCodeMap.put('1', new Color(0x9*17, 0xD*17, 0x9*17)); //Green
shortColorCodeMap.put('3', new Color(0xF*17, 0xA*17, 0xA*17)); //Pink
shortColorCodeMap.put('4', new Color(0xE*17, 0x3*17, 0x2*17)); //Red (RED)
shortColorCodeMap.put('5', new Color(0xF*17, 0xF*17, 0x3*17)); //Yellow
shortColorCodeMap.put('6', new Color(0x1 * 17, 0x4*17, 0x4*17)); //BlackGreen (144)
shortColorCodeMap.put('7', new Color(0x1*17, 0x8*17, 0x8*17)); //DarkGreen (188)
shortColorCodeMap.put('8', new Color(0x8 * 17, 0x8*17, 0x8*17)); //Grey (888)
shortColorCodeMap.put('9', new Color(0xF*17, 0x7*17, 0x3*17)); //Orange (F73)
shortColorCodeMap.put('A', new Color(0xF*17, 0xD*17, 0xE*17)); //Light Pink (FDE)
shortColorCodeMap.put('B', new Color(0xB*17, 0xD*17, 0xE*17)); //Light Blue (BDE)
shortColorCodeMap.put('C', new Color(0x0*17, 0xB*17, 0xF*17)); //Bright Blue (0BF)

if (shortColorCodes.length()==0) return;
String[] rgbList= shortColorCodes.split(";");
Character currentKey= 'G';
for (int i=0; i<rgbList.length; i++) {
String[] rgb= rgbList[i].split(",");
if (rgb.length !=3 ) throw new InvalidParameterException(String.format("Invalid RBG Color format for %ith Color: %s", i+1, rgbList[i]));
shortColorCodeMap.put(currentKey, new Color(Integer.parseInt(rgb[0]), Integer.parseInt(rgb[1]), Integer.parseInt(rgb[2])));
currentKey++;
}
}

private String shortColorCodes;

public HashMap<Character, Color> getShortColorCodeMap() {
return shortColorCodeMap;
}

private HashMap<Character, Color> shortColorCodeMap= new HashMap<Character, Color>();

public static final int USE_TAGS = 0;
public static final int RENDER_TAGS = 1;
public static final int IGNORE_TAGS = 2;
Expand Down Expand Up @@ -238,6 +311,6 @@ public CustomShapeDefinition getFromCustomShapes(String tagName){
return customShapes.get(tagName);
}



}
4 changes: 2 additions & 2 deletions src/java/org/stathissideris/ascii2image/graphics/Diagram.java
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,7 @@ public Diagram(TextGrid grid, ConversionOptions options) {
//assign color codes to shapes
//TODO: text on line should not change its color

Iterator<CellColorPair> cellColorPairs = grid.findColorCodes().iterator();
Iterator<CellColorPair> cellColorPairs = grid.findColorCodes(options).iterator();
while(cellColorPairs.hasNext()){
TextGrid.CellColorPair pair =
(TextGrid.CellColorPair) cellColorPairs.next();
Expand Down Expand Up @@ -513,7 +513,7 @@ public Diagram(TextGrid grid, ConversionOptions options) {

//copy again
workGrid = new TextGrid(grid);
workGrid.removeNonText();
workGrid.removeNonText(options);


// ****** handle text *******
Expand Down
55 changes: 41 additions & 14 deletions src/java/org/stathissideris/ascii2image/graphics/FontMeasurer.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,35 @@
* @author Efstathios Sideris
*/
public class FontMeasurer {

private static final String fontFamilyName = "Dialog";

public static String getFontFamilyName() {
return fontFamilyName;
}

public static void setFontFamilyName (String fontFamily) {
fontFamilyName= fontFamily;
}

private static String fontFamilyName = "Dialog";
private static int fontSize = 12;
private static int fontStyle = Font.BOLD;

public static int getFontSize() {
return fontSize;
}

public static void setFontSize(final int fontSize) {
FontMeasurer.fontSize = fontSize;
}

public static int getFontStyle() {
return fontStyle;
}

public static void setFontStyle(final int fontStyle) {
FontMeasurer.fontStyle = fontStyle;
}

//private static final String fontFamilyName = "Helvetica";

private static final boolean DEBUG = false;
Expand Down Expand Up @@ -139,37 +166,37 @@ public Font getFontFor(int maxWidth, String string){


public Font getFontFor(int pixelHeight, FontRenderContext frc){
float size = 12;
Font currentFont = new Font(fontFamilyName, Font.BOLD, (int) size);
float fontSizeInFloat= fontSize;
Font currentFont = new Font(fontFamilyName, fontStyle, fontSize);
// Font currentFont = new Font("Times", Font.BOLD, (int) size);
if(DEBUG) System.out.println(currentFont.getFontName());
//ascent is the distance between the baseline and the tallest character
int ascent = getAscent(currentFont);

int direction; //direction of size change (towards smaller or bigger)
if(ascent > pixelHeight){
currentFont = currentFont.deriveFont(size - 1);
size--;
currentFont = currentFont.deriveFont(fontSizeInFloat - 1);
fontSizeInFloat--;
direction = -1;
} else {
currentFont = currentFont.deriveFont(size + 1);
size++;
currentFont = currentFont.deriveFont(fontSizeInFloat + 1);
fontSizeInFloat++;
direction = 1;
}
while(size > 0){
currentFont = currentFont.deriveFont(size);
while(fontSizeInFloat > 0){
currentFont = currentFont.deriveFont(fontSizeInFloat);
//rectangle = currentFont.getStringBounds(testString, frc);
ascent = getAscent(currentFont);
if(direction == 1){
if(ascent > pixelHeight){
size = size - 0.5f;
return currentFont.deriveFont(size);
fontSizeInFloat = fontSizeInFloat - 0.5f;
return currentFont.deriveFont(fontSizeInFloat);
}
else size = size + 0.5f;
else fontSizeInFloat = fontSizeInFloat + 0.5f;
} else {
if(ascent < pixelHeight)
return currentFont;
else size = size - 0.5f;
else fontSizeInFloat = fontSizeInFloat - 0.5f;
}
}
return null;
Expand Down
49 changes: 32 additions & 17 deletions src/java/org/stathissideris/ascii2image/text/TextGrid.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.stathissideris.ascii2image.core.ConversionOptions;
import org.stathissideris.ascii2image.core.FileUtils;
import org.stathissideris.ascii2image.core.ProcessingOptions;
import org.stathissideris.ascii2image.graphics.CustomShapeDefinition;
Expand Down Expand Up @@ -559,12 +560,12 @@ public boolean isBlankBetweenCharacters(Cell cell){
* Makes blank all the cells that contain non-text
* elements.
*/
public void removeNonText(){
public void removeNonText(ConversionOptions options){
//the following order is significant
//since the south-pointing arrowheads
//are determined based on the surrounding boundaries
removeArrowheads();
removeColorCodes();
removeColorCodes(options);
removeBoundaries();
removeMarkupTags();
}
Expand All @@ -580,14 +581,19 @@ public void removeArrowheads(){
}
}

public void removeColorCodes(){
Iterator cells = findColorCodes().iterator();
public void removeColorCodes(ConversionOptions options){

Iterator cells = findColorCodes(options).iterator();
while(cells.hasNext()){
Cell cell = ((CellColorPair) cells.next()).cell;
set(cell, ' ');
cell = cell.getEast(); set(cell, ' ');
cell = cell.getEast(); set(cell, ' ');
cell = cell.getEast(); set(cell, ' ');
if (options.processingOptions.getShortColorCodes() == null) {
cell = cell.getEast();
set(cell, ' ');
cell = cell.getEast();
set(cell, ' ');
}
}
}

Expand Down Expand Up @@ -628,24 +634,33 @@ public ArrayList<Cell> findArrowheads(){
}


public ArrayList<CellColorPair> findColorCodes(){
Pattern colorCodePattern = Pattern.compile("c[A-F0-9]{3}");
public ArrayList<CellColorPair> findColorCodes(ConversionOptions options){
Pattern colorCodePattern =
options.processingOptions.isShortColorCodes()?
Pattern.compile("z[A-Z0-9]"):
Pattern.compile("c[A-F0-9]{3}");
int colorCodeWidth= options.processingOptions.isShortColorCodes()?2:4;
ArrayList<CellColorPair> result = new ArrayList<CellColorPair>();
int width = getWidth();
int height = getHeight();
for(int yi = 0; yi < height; yi++){
for(int xi = 0; xi < width - 3; xi++){
for(int xi = 0; xi < width - colorCodeWidth-1; xi++){
Cell cell = new Cell(xi, yi);
String s = getStringAt(cell, 4);
String s = getStringAt(cell, colorCodeWidth);
Matcher matcher = colorCodePattern.matcher(s);
if(matcher.matches()){
char cR = s.charAt(1);
char cG = s.charAt(2);
char cB = s.charAt(3);
int r = Integer.valueOf(String.valueOf(cR), 16).intValue() * 17;
int g = Integer.valueOf(String.valueOf(cG), 16).intValue() * 17;
int b = Integer.valueOf(String.valueOf(cB), 16).intValue() * 17;
result.add(new CellColorPair(cell, new Color(r, g, b)));
if (options.processingOptions.isShortColorCodes()) {
Color c= options.processingOptions.getShortColorCodeMap().getOrDefault(s.charAt(1), Color.white);
result.add(new CellColorPair(cell, c));
} else {
char cR = s.charAt(1);
char cG = s.charAt(2);
char cB = s.charAt(3);
int r = Integer.valueOf(String.valueOf(cR), 16).intValue() * 17;
int g = Integer.valueOf(String.valueOf(cG), 16).intValue() * 17;
int b = Integer.valueOf(String.valueOf(cB), 16).intValue() * 17;
result.add(new CellColorPair(cell, new Color(r, g, b)));
}
}
}
}
Expand Down