Skip to content
Open
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
82 changes: 53 additions & 29 deletions TPSVG/src/com/trevorpage/tpsvg/SVGParserRenderer.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Stack;
import java.util.regex.Matcher;
Expand All @@ -25,6 +27,7 @@
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Matrix;
import android.graphics.Paint;
Expand All @@ -34,6 +37,7 @@
import android.graphics.RectF;
import android.graphics.Shader;
import android.graphics.Typeface;
import android.os.Build;
import android.util.Log;
import android.view.View;

Expand Down Expand Up @@ -1669,7 +1673,11 @@ private static float parseCoOrdinate(String value) {
}
return result;
}


static Pattern shortHexPattern = Pattern.compile("#([0-9a-f])([0-9a-f])([0-9a-f])([0-9a-f]?)");
static Pattern rgbPattern = Pattern.compile("rgb\\(([0-9]{1,3}),([0-9]{1,3}),([0-9]{1,3})\\)");
static Pattern argbPattern = Pattern.compile("rgba\\(([0-9]{1,3}),([0-9]{1,3}),([0-9]{1,3}),([0-9]{1,3})\\)");
private static List<String> alphaMissingColors = Arrays.asList(new String[] {"aqua", "fuchsia", "lime", "maroon", "navy", "olive", "purple", "silver", "teal"});
/**
* Parse a basic data type of type &lt;color&gt;. See SVG specification
* section: http://www.w3.org/TR/SVG/types.html#BasicDataTypes.
Expand All @@ -1678,38 +1686,54 @@ private static float parseCoOrdinate(String value) {
*/
private int parseColour(String value) {

int result = 0xffffff;

// Handle colour values that are in the format "rgb(r,g,b)"
if (value.startsWith("rgb")) {
int r, g, b;
ValueTokenizer t = new ValueTokenizer();
t.getToken(value.substring(3));
if (t.currentTok == ValueTokenizer.LTOK_NUMBER) {
r = (int) t.tokenF;
t.getToken(null);
if (t.currentTok == ValueTokenizer.LTOK_NUMBER) {
g = (int) t.tokenF;
t.getToken(null);
if (t.currentTok == ValueTokenizer.LTOK_NUMBER) {
b = (int) t.tokenF;
result = (r << 16) + (g << 8) + b;
int color = Color.TRANSPARENT;
if (value != null) {
String lowval = value.trim().toLowerCase();

Matcher m = null;
if ((m = shortHexPattern.matcher(lowval)).matches()) {
StringBuilder sb = new StringBuilder();
sb.append("#");
for(int i = 1; i <= m.groupCount(); i++) {
String s = m.group(i);
sb.append(s).append(s);
}
String newColor = sb.toString();
color = Color.parseColor(newColor);
} else if ((m = rgbPattern.matcher(lowval)).matches()) {
color = Color.rgb(
Integer.valueOf(m.group(1)),
Integer.valueOf(m.group(2)),
Integer.valueOf(m.group(3))
);
} else if ((m = argbPattern.matcher(lowval)).matches()) {
color = Color.argb(
Integer.valueOf(m.group(4)),
Integer.valueOf(m.group(1)),
Integer.valueOf(m.group(2)),
Integer.valueOf(m.group(3))
);
} else {
// Try the parser, will throw illegalArgument if it can't parse it.
try {

color = Color.parseColor(lowval);
// In 4.3, Google introduced some new string color constants and they forgot to
// add the alpha bits to them! This is a temporary workaround
// until they fix it. I've created a Google ticket for this:
// https://code.google.com/p/android/issues/detail?id=58352&thanks=58352
if (Build.VERSION.SDK_INT > 17 && alphaMissingColors.contains(lowval)) {
color = Color.parseColor(lowval) | 0xFF000000;
} else {
color = Color.parseColor(lowval);
}
} catch (IllegalArgumentException e) {
Log.w(LOGTAG, "Unknown color: " + value);
return 0xff0000;
}
}
}

// Handle colour values that are in the format #123abc. (Assume that's
// what it is,
// if the length is seven characters).
else if (value.length() == 7) {
try {
result = Integer.parseInt(value.substring(1, 7), 16);
} catch (NumberFormatException e) {
result = 0xff0000;
}
}
return result;
return color;
}

private float parseAttrValueFloat(String value) {
Expand Down