Skip to content

Commit

Permalink
BREAKING: Start to address tommyettinger#12 .
Browse files Browse the repository at this point in the history
Now the tag `[ ]` is used to do a full reset, and `[]` should only undo the last square-bracket markup change. Things are a little tricky with curly-brace effect markup, and I don't know how this could undo those.


Former-commit-id: 6d74976
  • Loading branch information
tommyettinger committed Aug 16, 2023
1 parent 20f1c95 commit 1ed572d
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 17 deletions.
50 changes: 40 additions & 10 deletions src/main/java/com/github/tommyettinger/textra/Font.java
Original file line number Diff line number Diff line change
Expand Up @@ -864,6 +864,7 @@ public enum DistanceFieldType {
private final float[] vertices = new float[20];
private final Layout tempLayout = new Layout();
private final LongArray glyphBuffer = new LongArray(128);
private final LongArray historyBuffer = new LongArray(64);
/**
* Must be in lexicographic order because we use {@link Arrays#binarySearch(char[], int, int, char)} to
* verify if a char is present.
Expand Down Expand Up @@ -4757,32 +4758,37 @@ public Layout markup(String text, Layout appendTo) {
}
char after = eq + 1 >= end ? '\u0000' : text.charAt(eq + 1);
if (start + 1 == end || "RESET".equalsIgnoreCase(safeSubstring(text, start + 1, end))) {
historyBuffer.add(current);
scale = 3;
font = this;
fontIndex = 0;
current &= ~SUPERSCRIPT;
} else if (after == '^' || after == '=' || after == '.') {
switch (after) {
case '^':
historyBuffer.add(current);
if ((current & SUPERSCRIPT) == SUPERSCRIPT)
current &= ~SUPERSCRIPT;
else
current |= SUPERSCRIPT;
break;
case '.':
historyBuffer.add(current);
if ((current & SUPERSCRIPT) == SUBSCRIPT)
current &= ~SUBSCRIPT;
else
current = (current & ~SUPERSCRIPT) | SUBSCRIPT;
break;
case '=':
historyBuffer.add(current);
if ((current & SUPERSCRIPT) == MIDSCRIPT)
current &= ~MIDSCRIPT;
else
current = (current & ~SUPERSCRIPT) | MIDSCRIPT;
break;
}
} else if (fontChange >= 0 && family != null) {
historyBuffer.add(current);
fontIndex = family.fontAliases.get(safeSubstring(text, fontChange + 1, end), -1);
if (fontIndex == -1) {
font = this;
Expand All @@ -4795,6 +4801,7 @@ public Layout markup(String text, Layout appendTo) {
}
}
} else if (sizeChange >= 0) {
historyBuffer.add(current);
if (sizeChange + 1 == end) {
if (eq + 1 == sizeChange) {
scale = 3;
Expand All @@ -4813,15 +4820,30 @@ public Layout markup(String text, Layout appendTo) {
c = '[';
if (++i < n && (c = text.charAt(i)) != '[' && c != '+') {
if (c == ']') {
color = baseColor;
current = color & ~SUPERSCRIPT;
scale = 3;
font = this;
capitalize = false;
capsLock = false;
lowerCase = false;
if(historyBuffer.isEmpty()) {
color = baseColor;
current = color & ~SUPERSCRIPT;
scale = 3;
font = this;
capitalize = false;
capsLock = false;
lowerCase = false;
} else {
current = historyBuffer.pop();
scale = (int)((current & 0x1f00000L) >>> 20);
if (family == null) {
font = this;
fontIndex = 0;
}
else {
fontIndex = (int) ((current & 0xFFFFFFFFFFF0FFFFL) >>> 16);
font = family.connected[fontIndex & 15];
if (font == null) font = this;
}
}
continue;
}
historyBuffer.add(current);
int len = text.indexOf(']', i) - i;
if (len < 0) break;
switch (c) {
Expand Down Expand Up @@ -4947,6 +4969,15 @@ else if (len >= 9)
else color = (long) lookupColor << 32;
current = (current & ~COLOR_MASK) | color;
break;
case ' ':
color = baseColor;
current = color & ~SUPERSCRIPT;
scale = 3;
font = this;
capitalize = false;
capsLock = false;
lowerCase = false;
break;
default:
// attempt to look up a known Color name with a ColorLookup
int gdxColor = colorLookup.getRgba(safeSubstring(text, i, i + len)) & 0xFFFFFFFE;
Expand All @@ -4967,7 +4998,6 @@ else if (len >= 9)
c = font.nameLookup.get(safeSubstring(text, i + 1, i + len), '+');
i += len;
scaleX = (scale + 1) * 0.25f * font.cellHeight / (font.mapping.get(c, font.defaultValue).xAdvance);
// scaleX = (scale + 1) * 0.25f * font.cellHeight / (font.mapping.get(c, font.defaultValue).xAdvance*1.25f);
}
}
if (font.kerning == null) {
Expand All @@ -4986,8 +5016,8 @@ else if (len >= 9)
float ox = font.mapping.get(c, font.defaultValue).offsetX;
if(ox != ox) ox = 0;
else ox *= scaleX;
ox *= (1f + 0.5f * (-(current & SUPERSCRIPT) >> 63));
if (ox < 0) w = (appendTo.peekLine().width -= ox);
ox *= (1f + 0.5f * (-(current & SUPERSCRIPT) >> 63));
if (ox < 0) w = (appendTo.peekLine().width -= ox);
}
initial = false;
}
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/com/github/tommyettinger/textra/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
*/
public class Parser {
private static final Pattern PATTERN_MARKUP_STRIP = Pattern.compile("((?<!\\[)\\[[^\\[\\]]*(\\]|$))");
private static final Replacer MARKUP_TO_TAG = new Replacer(Pattern.compile("(?<!\\[)\\[([^\\[\\]\\+][^\\[\\]]*)(\\]|$)"), "{STYLE=$1}");
private static final Replacer MARKUP_TO_TAG = new Replacer(Pattern.compile("(?<!\\[)\\[([^ \\[\\]\\+][^\\[\\]]*)(\\]|$)"), "{STYLE=$1}");
private static final Pattern PATTERN_COLOR_HEX_NO_HASH = Pattern.compile("[A-Fa-f0-9]{6,8}");

private static final CaseInsensitiveIntMap BOOLEAN_TRUE = new CaseInsensitiveIntMap(new String[]{"true", "yes", "t", "y", "on", "1"}, new int[6]);
Expand All @@ -51,7 +51,7 @@ public class Parser {
* @return {@code text} with square bracket style markup changed to curly-brace style markup
*/
public static String preprocess(CharSequence text) {
return MARKUP_TO_TAG.replace(text).replace("[]", "{RESET}");
return MARKUP_TO_TAG.replace(text).replace("[ ]", "{RESET}");
}

/**
Expand Down Expand Up @@ -556,7 +556,7 @@ private static String getResetReplacement() {
TypingConfig.EFFECT_END_TOKENS.keys().toArray(tokens);
tokens.add("NORMAL");

StringBuilder sb = new StringBuilder("[]");
StringBuilder sb = new StringBuilder("[ ]");
for (String token : tokens) {
sb.append('{').append(token).append('}');
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -205,16 +205,16 @@ public TypingLabel createTypingLabel() {

final StringBuilder text = new StringBuilder();
text.append("{SLOWER}{GRADIENT=FF70F1;light exciting pink orange with ignored words;-0.5;5}{EASE=-8;2;1}{SHRINK=2;5}[@Medieval]Welcome,{ENDSHRINK}[%] [@]{WAIT}");
text.append("{SPIRAL=2;0.5;-2.5}{STYLE=/}{STYLE=;}[%^SHADOW]{VAR=title}[%]{STYLE=;}{STYLE=/}{ENDSPIRAL}![] {TRIGGER=lightest violet}[lightest violet][+🤔][]{WAIT=0.8}");
text.append("{SPIRAL=2;0.5;-2.5}{STYLE=/}{STYLE=;}[%^SHADOW]{VAR=title}[%]{STYLE=;}{STYLE=/}{ENDSPIRAL}![ ] {TRIGGER=lightest violet}[lightest violet][+🤔][ ]{WAIT=0.8}");
text.append("{FAST}\n\n");
text.append("{RESET}[@Sans]{ATTENTION}This is a [*][MAROON][%?SHINY]simple[WHITE][*] [%?blacken]test[%][@]{ENDATTENTION} to {SPIN}show you{ENDSPIN}");
text.append("{GRADIENT=27C1F5;2776E7;-0.5;5} {CROWD=20;1;forever}how to make dialogues{ENDCROWD} {JUMP}{SLOW}[*][/]fun[/][*] again! {ENDGRADIENT}[+🥳]{ENDJUMP}{WAIT}\n");
text.append("{NORMAL}{CLEARCOLOR}{JOLT=1;0.8;inf;0.25;dddddd;fff0cc}With this library{ENDJOLT} [LIGHTER RICH gold]you[WHITE] can {SQUASH}{SIZE=150%}[_]control[_]{ENDSQUASH} {SIZE=%75}the{SIZE=150%} flow[^][SKY] [[citation needed][] of the text with");
text.append("{NORMAL}{CLEARCOLOR}{JOLT=1;0.8;inf;0.25;dddddd;fff0cc}With this library{ENDJOLT} [LIGHTER RICH gold]you[WHITE] can {SQUASH}{SIZE=150%}[_]control[_]{ENDSQUASH} {SIZE=%75}the{SIZE=150%} flow[^][SKY] [[citation needed][ ] of the text with");
text.append(" {BLINK=FF6BF3;FF0582;3}tokens{ENDBLINK},{WAIT=0.7}");
text.append("{SPEED=2.50}{COLOR=lighter dull GREEN} making the text go {SHAKE=1.1;0.6;inf}[@Future]really fast[@]{ENDSHAKE}{WAIT=0.5} ");
text.append("{SPEED=0.25}{COLOR=jade fern}{WAVE=0.66;1;0.5;∞}[@Mono] or extremely slow.[@]{ENDWAVE}");
text.append("{RESET} You {HEARTBEAT}[darker red]can also wait[#FFFFFF]{ENDHEARTBEAT} for a {EASE=-15;2;1}[black][%?whiten]second[]{ENDEASE}{WAIT=1} {EASE=15;8;1}{COLOR=#E6DB74}or two{CLEARCOLOR}{ENDEASE}{WAIT=2}, ");
text.append("[%?Error]jussst[%][.][red][@Canada] spelling[] to [%?WARN]catching[%][.][#FFD510FF][@Canada] grammar[] an {RAINBOW=1;1;0.7}[@Console][;]{STYLE=%?jostle}event[%][;][@]{ENDRAINBOW} in [%?note]code[%][.][#3088B8FF][@Canada] cool[]{EVENT=example}!{WAIT} ");
text.append("{RESET} You {HEARTBEAT}[darker red]can also wait[#FFFFFF]{ENDHEARTBEAT} for a {EASE=-15;2;1}[black][%?whiten]second[ ]{ENDEASE}{WAIT=1} {EASE=15;8;1}{COLOR=#E6DB74}or two{CLEARCOLOR}{ENDEASE}{WAIT=2}, ");
text.append("[%?Error]jussst[%][.][red][@Canada] spelling[ ] to [%?WARN]catching[%][.][#FFD510FF][@Canada] grammar[ ] an {RAINBOW=1;1;0.7}[@Console][;]{STYLE=%?jostle}event[%][;][@]{ENDRAINBOW} in [%?note]code[%][.][#3088B8FF][@Canada] cool[ ]{EVENT=example}!{WAIT} ");
text.append("{NORMAL}\n\n");
text.append("{VAR=FIRE_WIND}Imagine the [~]bugs[~]! I mean, possibilities! {ENDGRADIENT}{SPEED=0.1}{CANNON}[+🔥][+😁][+👏] {RESET}");

Expand Down

0 comments on commit 1ed572d

Please sign in to comment.