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
7 changes: 7 additions & 0 deletions src/main/java/com/arcao/fontcreator/ArgumentParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public class ArgumentParser {
private final OptionSpec<File> outputFile;
private final OptionSpec<String> font;
private final OptionSpec<Integer> yoffset;
private final OptionSpec<String> characters;

public ArgumentParser() {
help = parser.acceptsAll(asList("h", "help"), "display this help and exit").forHelp();
Expand All @@ -31,6 +32,7 @@ public ArgumentParser() {
charset = parser.acceptsAll(asList("c", "charset"), "font table index charset").withOptionalArg().defaultsTo("iso-8859-1").ofType(String.class);
outputFile = parser.acceptsAll(asList("o", "output"), "write .h font table to file instead to StdOut").withOptionalArg().ofType(File.class);
font = parser.nonOptions("system font name or path to font file to be processed").describedAs("FONT").ofType(String.class);
characters = parser.acceptsAll(asList("a", "characters"), "characters to include").withRequiredArg().defaultsTo("").ofType(String.class);
}

public Arguments parse(String args[]) throws OptionException {
Expand Down Expand Up @@ -76,6 +78,11 @@ public File outputFile() {
public String font() {
return options.valueOf(font);
}

@Override
public String characters() {
return options.valueOf(characters);
}
};
}

Expand Down
3 changes: 2 additions & 1 deletion src/main/java/com/arcao/fontcreator/Arguments.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,6 @@ interface Arguments {
String font();

int yoffset();


String characters();
}
65 changes: 50 additions & 15 deletions src/main/java/com/arcao/fontcreator/FontConverterV3.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ Based on squix78 implementation (MIT License) from:

public class FontConverterV3 {

private static final int END_CHAR = 256;
private static final int START_CHAR = 32;
private int endChar = 256;
private int startChar = 32;

private Graphics2D g;
private FontMetrics fontMetrics;
Expand All @@ -44,20 +44,26 @@ public FontConverterV3(Font font, Charset charset) {
this.charset = charset;
}

public void printFontData(StringBuilder builder, int yoffset) {
public void printFontData(StringBuilder builder, int yoffset, String characters) {
calculateStartEnd(characters);
this.yoffset=yoffset;
List<LetterData> letterList = produceLetterDataList();
List<LetterData> letterList = produceLetterDataList(characters);

String fontName = g.getFont().getFontName().replaceAll("[\\s\\-\\.]", "_") + "_"
+ getFontStyle() + "_" + g.getFont().getSize();
builder.append("// Font table version: 3\n");
builder.append("// Created by FontCreator (https://github.com/arcao/esp8266-oled-ssd1306-font-creator.\n");
builder.append("// In case of problems make sure that you are using the font file with the correct version!\n");
builder.append(String.format("const char %s[] PROGMEM = {\n", fontName));
if (characters != null && !characters.isEmpty()) {
builder.append("// contains only \"");
builder.append(characters);
builder.append("\"\n");
}
builder.append(String.format("const uint8_t %s[] PROGMEM = {\n", fontName));
writeHexValue(builder, "Width", getMaxCharWidth());
writeHexValue(builder, "Height", getMaxCharHeight());
writeHexValue(builder, "First Char", START_CHAR);
writeHexValue(builder, "Numbers of Chars", END_CHAR - START_CHAR);
writeHexValue(builder, "First Char", startChar);
writeHexValue(builder, "Numbers of Chars", endChar - startChar);
builder.append("\n");
builder.append("\t// Jump Table:\n");

Expand All @@ -80,7 +86,7 @@ public void printFontData(StringBuilder builder, int yoffset) {
letterList.stream().filter(LetterData::isVisible).forEach(letter -> {
builder.append("\t");
builder.append(letter.toString());
if ((int) letter.getCode() != END_CHAR - 1) {
if ((int) letter.getCode() != endChar - 1) {
builder.append(",");
}
builder.append(String.format("\t// %d\n", (int) letter.getCode()));
Expand All @@ -89,17 +95,20 @@ public void printFontData(StringBuilder builder, int yoffset) {
builder.append("};\n");
}

private List<LetterData> produceLetterDataList() {
List<LetterData> letterDataList = new ArrayList<>(END_CHAR - START_CHAR);
for (int i = START_CHAR; i < END_CHAR; i++) {
private List<LetterData> produceLetterDataList(String characters) {
List<LetterData> letterDataList = new ArrayList<>(endChar - startChar);
for (int i = startChar; i < endChar; i++) {
char ch;
if (charset == null) {
ch = (char) i;
} else {
ch = new String(new byte[]{(byte) (i & 0xFF)}, charset).charAt(0);
}

letterDataList.add(createLetterData(ch));
LetterData letter = createLetterData(ch);
letterDataList.add(letter);
if (characters != null && !characters.isEmpty() && characters.indexOf(ch) < 0) {
letter.visible = false;
}
}
return letterDataList;
}
Expand Down Expand Up @@ -187,7 +196,7 @@ private void writeHexValue(StringBuilder builder, String label, int value) {

private int getMaxCharWidth() {
int maxWidth = 0;
for (int i = START_CHAR; i < END_CHAR; i++) {
for (int i = startChar; i < endChar; i++) {
maxWidth = Math.max(maxWidth, fontMetrics.charWidth((char) i));
}
return maxWidth;
Expand All @@ -197,6 +206,32 @@ private int getMaxCharHeight() {
return fontMetrics.getMaxAscent() + fontMetrics.getMaxDescent() + fontMetrics.getLeading();
}

private void calculateStartEnd(String characters) {
if (characters != null && !characters.isEmpty()) {
startChar = 255;
endChar = 0;
for (int i = 0; i < characters.length(); i++) {
int idx = characterTargetIndex(characters.charAt(i));
if (idx < startChar) {
startChar = idx;
}
if (idx >= endChar) {
endChar = idx + 1;
}
}
}
}

private int characterTargetIndex(char c) {
int ch;
if (charset == null) {
ch = c;
} else {
ch = Character.toString(c).getBytes(charset)[0] & 0xFF;
}
return ch;
}

private class LetterData {

private char code;
Expand Down Expand Up @@ -243,4 +278,4 @@ public String toString() {
}
}

}
}
2 changes: 1 addition & 1 deletion src/main/java/com/arcao/fontcreator/FontCreator.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public static void main(String args[]) {
FontConverterV3 fontConverter = new FontConverterV3(font, charset);

StringBuilder builder = new StringBuilder();
fontConverter.printFontData(builder, arguments.yoffset());
fontConverter.printFontData(builder, arguments.yoffset(), arguments.characters());

PrintStream out = System.out;
if (arguments.outputFile() != null) {
Expand Down