| •1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
package com.github.weisj.jsvg.geometry.path; |
| 23 |
|
| 24 |
import java.util.ArrayList; |
| 25 |
import java.util.List; |
| 26 |
|
| 27 |
import org.jetbrains.annotations.NotNull; |
| 28 |
|
| 29 |
import com.github.weisj.jsvg.logging.Logger; |
| 30 |
import com.github.weisj.jsvg.logging.Logger.Level; |
| 31 |
import com.github.weisj.jsvg.logging.impl.LogFactory; |
| 32 |
import com.github.weisj.jsvg.util.ParserBase; |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
public final class PathParser extends ParserBase { |
| 40 |
private static final Logger LOGGER = LogFactory.createLogger(PathParser.class); |
| 41 |
private char currentCommand; |
| 42 |
|
| 43 |
public PathParser(@NotNull String input) { |
| 44 |
super(input, 0); |
| 45 |
} |
| 46 |
|
| 47 |
private boolean isCommandChar(char c) { |
| 48 |
return (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z'); |
| 49 |
} |
| 50 |
|
| 51 |
@Override |
| 52 |
public float nextFloat() throws NumberFormatException { |
| 53 |
float f = super.nextFloat(); |
| 54 |
consumeWhiteSpaceOrSeparator(); |
| 55 |
return f; |
| 56 |
} |
| 57 |
|
| 58 |
private boolean nextFlag() { |
| 59 |
char c = peek(); |
| 60 |
consume(); |
| 61 |
consumeWhiteSpaceOrSeparator(); |
| 62 |
if (c == '1') { |
| 63 |
return true; |
| 64 |
} else if (c == '0') { |
| 65 |
return false; |
| 66 |
} else { |
| 67 |
throw new IllegalStateException("Invalid flag value '" + c + "' " + currentLocation()); |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
public @NotNull BezierPathCommand parseMeshCommand() { |
| 72 |
char peekChar = peek(); |
| 73 |
currentCommand = 'z'; |
| 74 |
if (isCommandChar(peekChar)) { |
| 75 |
consume(); |
| 76 |
currentCommand = peekChar; |
| 77 |
} |
| 78 |
consumeWhiteSpaceOrSeparator(); |
| 79 |
switch (currentCommand) { |
| 80 |
case 'l': |
| 81 |
return new LineToBezier(true, nextFloatOrUnspecified(), nextFloatOrUnspecified()); |
| 82 |
case 'L': |
| 83 |
return new LineToBezier(false, nextFloatOrUnspecified(), nextFloatOrUnspecified()); |
| 84 |
case 'c': |
| 85 |
return new CubicBezierCommand(true, nextFloat(), nextFloat(), nextFloat(), nextFloat(), |
| 86 |
nextFloatOrUnspecified(), nextFloatOrUnspecified()); |
| 87 |
case 'C': |
| 88 |
return new CubicBezierCommand(false, nextFloat(), nextFloat(), nextFloat(), nextFloat(), |
| 89 |
nextFloatOrUnspecified(), nextFloatOrUnspecified()); |
| 90 |
default: |
| 91 |
throw new IllegalStateException("Only commands c C l L allowed"); |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
public PathCommand[] parsePathCommand() { |
| 96 |
if ("none".equals(input)) return new PathCommand[0]; |
| 97 |
List<PathCommand> commands = new ArrayList<>(); |
| 98 |
try { |
| 99 |
parsePathCommandInto(commands); |
| 100 |
} catch (Exception e) { |
| 101 |
LOGGER.log(Level.INFO, "Error parsing path command: " + e.getMessage()); |
| 102 |
} |
| 103 |
return commands.toArray(new PathCommand[0]); |
| 104 |
} |
| 105 |
|
| 106 |
private void parsePathCommandInto(List<PathCommand> commands) { |
| 107 |
currentCommand = 'Z'; |
| 108 |
while (hasNext()) { |
| 109 |
char peekChar = peek(); |
| 110 |
if (isCommandChar(peekChar)) { |
| 111 |
consume(); |
| 112 |
currentCommand = peekChar; |
| 113 |
} |
| 114 |
consumeWhiteSpaceOrSeparator(); |
| 115 |
|
| 116 |
if (currentCommand != 'M' && currentCommand != 'm' && commands.isEmpty()) { |
| 117 |
throw new IllegalArgumentException("Path must start with a move command " + currentLocation()); |
| 118 |
} |
| 119 |
|
| 120 |
PathCommand cmd; |
| 121 |
switch (currentCommand) { |
| 122 |
case 'M': |
| 123 |
cmd = new MoveTo(false, nextFloat(), nextFloat()); |
| 124 |
currentCommand = 'L'; |
| 125 |
break; |
| 126 |
case 'm': |
| 127 |
cmd = new MoveTo(true, nextFloat(), nextFloat()); |
| 128 |
currentCommand = 'l'; |
| 129 |
break; |
| 130 |
case 'L': |
| 131 |
cmd = new LineTo(false, nextFloat(), nextFloat()); |
| 132 |
break; |
| 133 |
case 'l': |
| 134 |
cmd = new LineTo(true, nextFloat(), nextFloat()); |
| 135 |
break; |
| 136 |
case 'H': |
| 137 |
cmd = new Horizontal(false, nextFloat()); |
| 138 |
break; |
| 139 |
case 'h': |
| 140 |
cmd = new Horizontal(true, nextFloat()); |
| 141 |
break; |
| 142 |
case 'V': |
| 143 |
cmd = new Vertical(false, nextFloat()); |
| 144 |
break; |
| 145 |
case 'v': |
| 146 |
cmd = new Vertical(true, nextFloat()); |
| 147 |
break; |
| 148 |
case 'A': |
| 149 |
cmd = new Arc(false, nextFloat(), nextFloat(), nextFloat(), |
| 150 |
nextFlag(), nextFlag(), nextFloat(), nextFloat()); |
| 151 |
break; |
| 152 |
case 'a': |
| 153 |
cmd = new Arc(true, nextFloat(), nextFloat(), nextFloat(), |
| 154 |
nextFlag(), nextFlag(), nextFloat(), nextFloat()); |
| 155 |
break; |
| 156 |
case 'Q': |
| 157 |
cmd = new Quadratic(false, nextFloat(), nextFloat(), |
| 158 |
nextFloat(), nextFloat()); |
| 159 |
break; |
| 160 |
case 'q': |
| 161 |
cmd = new Quadratic(true, nextFloat(), nextFloat(), |
| 162 |
nextFloat(), nextFloat()); |
| 163 |
break; |
| 164 |
case 'T': |
| 165 |
cmd = new QuadraticSmooth(false, nextFloat(), nextFloat()); |
| 166 |
break; |
| 167 |
case 't': |
| 168 |
cmd = new QuadraticSmooth(true, nextFloat(), nextFloat()); |
| 169 |
break; |
| 170 |
case 'C': |
| 171 |
cmd = new Cubic(false, nextFloat(), nextFloat(), |
| 172 |
nextFloat(), nextFloat(), |
| 173 |
nextFloat(), nextFloat()); |
| 174 |
break; |
| 175 |
case 'c': |
| 176 |
cmd = new Cubic(true, nextFloat(), nextFloat(), |
| 177 |
nextFloat(), nextFloat(), |
| 178 |
nextFloat(), nextFloat()); |
| 179 |
break; |
| 180 |
case 'S': |
| 181 |
cmd = new CubicSmooth(false, nextFloat(), nextFloat(), |
| 182 |
nextFloat(), nextFloat()); |
| 183 |
break; |
| 184 |
case 's': |
| 185 |
cmd = new CubicSmooth(true, nextFloat(), nextFloat(), |
| 186 |
nextFloat(), nextFloat()); |
| 187 |
break; |
| 188 |
case 'Z': |
| 189 |
case 'z': |
| 190 |
cmd = new Terminal(); |
| 191 |
break; |
| 192 |
default: |
| 193 |
throw new IllegalArgumentException("Invalid path element " + currentCommand + currentLocation()); |
| 194 |
} |
| 195 |
commands.add(cmd); |
| 196 |
} |
| 197 |
} |
| 198 |
} |