Skip to content

Commit 03903d6

Browse files
committed
minor changes, plus svg transformations fix
1 parent 09d26df commit 03903d6

File tree

6 files changed

+128
-115
lines changed

6 files changed

+128
-115
lines changed

android/core/src/processing/core/PShapeSVG.java

Lines changed: 90 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,18 @@
2323

2424
package processing.core;
2525

26+
import java.awt.Paint;
27+
import java.awt.PaintContext;
28+
import java.awt.Rectangle;
29+
import java.awt.RenderingHints;
30+
import java.awt.geom.AffineTransform;
31+
import java.awt.geom.Point2D;
32+
import java.awt.geom.Rectangle2D;
33+
import java.awt.image.ColorModel;
34+
import java.awt.image.Raster;
35+
import java.awt.image.WritableRaster;
2636
import java.util.HashMap;
2737

28-
import android.graphics.*;
29-
3038
import processing.xml.XMLElement;
3139

3240

@@ -143,11 +151,11 @@ public class PShapeSVG extends PShape {
143151

144152

145153
Gradient strokeGradient;
146-
Shader strokeGradientPaint;
154+
Paint strokeGradientPaint;
147155
String strokeName; // id of another object, gradients only?
148156

149157
Gradient fillGradient;
150-
Shader fillGradientPaint;
158+
Paint fillGradientPaint;
151159
String fillName; // id of another object
152160

153161

@@ -274,7 +282,7 @@ public PShapeSVG(PShapeSVG parent, XMLElement properties) {
274282

275283
String transformStr = properties.getString("transform");
276284
if (transformStr != null) {
277-
matrix = parseMatrix(transformStr);
285+
matrix = parseTransform(transformStr);
278286
}
279287

280288
parseColors(properties);
@@ -817,8 +825,27 @@ private void parsePathQuadto(float x1, float y1,
817825
* @param matrixStr text of the matrix param.
818826
* @return a good old-fashioned PMatrix2D
819827
*/
820-
static protected PMatrix2D parseMatrix(String matrixStr) {
821-
String[] pieces = PApplet.match(matrixStr, "\\s*(\\w+)\\((.*)\\)");
828+
static protected PMatrix2D parseTransform(String matrixStr) {
829+
matrixStr = matrixStr.trim();
830+
PMatrix2D outgoing = null;
831+
int start = 0;
832+
int stop = -1;
833+
while ((stop = matrixStr.indexOf(')', start)) != -1) {
834+
PMatrix2D m = parseSingleTransform(matrixStr.substring(start, stop+1));
835+
if (outgoing == null) {
836+
outgoing = m;
837+
} else {
838+
outgoing.apply(m);
839+
}
840+
start = stop + 1;
841+
}
842+
return outgoing;
843+
}
844+
845+
846+
static protected PMatrix2D parseSingleTransform(String matrixStr) {
847+
//String[] pieces = PApplet.match(matrixStr, "^\\s*(\\w+)\\((.*)\\)\\s*$");
848+
String[] pieces = PApplet.match(matrixStr, "[,\\s]*(\\w+)\\((.*)\\)");
822849
if (pieces == null) {
823850
System.err.println("Could not parse transform " + matrixStr);
824851
return null;
@@ -1014,7 +1041,7 @@ void setColor(String colorText, boolean isFill) {
10141041
int color = 0;
10151042
String name = "";
10161043
Gradient gradient = null;
1017-
Shader paint = null;
1044+
Paint paint = null;
10181045
if (colorText.equals("none")) {
10191046
visible = false;
10201047
} else if (colorText.equals("black")) {
@@ -1131,7 +1158,7 @@ static protected float parseUnitSize(String text) {
11311158

11321159

11331160
static class Gradient extends PShapeSVG {
1134-
Matrix transform;
1161+
AffineTransform transform;
11351162

11361163
float[] offset;
11371164
int[] color;
@@ -1190,30 +1217,16 @@ public LinearGradient(PShapeSVG parent, XMLElement properties) {
11901217
properties.getString("gradientTransform");
11911218

11921219
if (transformStr != null) {
1193-
float t[] = parseMatrix(transformStr).get(null);
1194-
//this.transform = new AffineTransform(t[0], t[3], t[1], t[4], t[2], t[5]);
1195-
transform = new Matrix();
1196-
transform.setValues(new float[] { // TODO don't create temp floats
1197-
t[0], t[1], t[2],
1198-
t[3], t[4], t[5],
1199-
0, 0, 1
1200-
});
1201-
1202-
// Point2D t1 = transform.transform(new Point2D.Float(x1, y1), null);
1203-
// Point2D t2 = transform.transform(new Point2D.Float(x2, y2), null);
1204-
float[] t1 = new float[] { x1, y1 };
1205-
float[] t2 = new float[] { x2, y2 };
1206-
transform.mapPoints(t1);
1207-
transform.mapPoints(t2);
1208-
1209-
// this.x1 = (float) t1.getX();
1210-
// this.y1 = (float) t1.getY();
1211-
// this.x2 = (float) t2.getX();
1212-
// this.y2 = (float) t2.getY();
1213-
x1 = t1[0];
1214-
y1 = t1[1];
1215-
x2 = t2[0];
1216-
y2 = t2[1];
1220+
float t[] = parseTransform(transformStr).get(null);
1221+
this.transform = new AffineTransform(t[0], t[3], t[1], t[4], t[2], t[5]);
1222+
1223+
Point2D t1 = transform.transform(new Point2D.Float(x1, y1), null);
1224+
Point2D t2 = transform.transform(new Point2D.Float(x2, y2), null);
1225+
1226+
this.x1 = (float) t1.getX();
1227+
this.y1 = (float) t1.getY();
1228+
this.x2 = (float) t2.getX();
1229+
this.y2 = (float) t2.getY();
12171230
}
12181231
}
12191232
}
@@ -1233,34 +1246,21 @@ public RadialGradient(PShapeSVG parent, XMLElement properties) {
12331246
properties.getString("gradientTransform");
12341247

12351248
if (transformStr != null) {
1236-
float t[] = parseMatrix(transformStr).get(null);
1237-
// this.transform = new AffineTransform(t[0], t[3], t[1], t[4], t[2], t[5]);
1238-
transform = new Matrix();
1239-
transform.setValues(new float[] { // TODO don't create temp floats
1240-
t[0], t[1], t[2],
1241-
t[3], t[4], t[5],
1242-
0, 0, 1
1243-
});
1244-
1245-
// Point2D t1 = transform.transform(new Point2D.Float(cx, cy), null);
1246-
// Point2D t2 = transform.transform(new Point2D.Float(cx + r, cy), null);
1247-
float[] t1 = new float[] { cx, cy };
1248-
float[] t2 = new float[] { cx + r, cy };
1249-
transform.mapPoints(t1);
1250-
transform.mapPoints(t2);
1251-
1252-
// this.cx = (float) t1.getX();
1253-
// this.cy = (float) t1.getY();
1254-
// this.r = (float) (t2.getX() - t1.getX());
1255-
cx = t1[0];
1256-
cy = t1[1];
1257-
r = t2[0] - t1[0];
1249+
float t[] = parseTransform(transformStr).get(null);
1250+
this.transform = new AffineTransform(t[0], t[3], t[1], t[4], t[2], t[5]);
1251+
1252+
Point2D t1 = transform.transform(new Point2D.Float(cx, cy), null);
1253+
Point2D t2 = transform.transform(new Point2D.Float(cx + r, cy), null);
1254+
1255+
this.cx = (float) t1.getX();
1256+
this.cy = (float) t1.getY();
1257+
this.r = (float) (t2.getX() - t1.getX());
12581258
}
12591259
}
12601260
}
12611261

12621262

1263-
/*
1263+
12641264
class LinearGradientPaint implements Paint {
12651265
float x1, y1, x2, y2;
12661266
float[] offset;
@@ -1324,7 +1324,7 @@ public Raster getRaster(int x, int y, int w, int h) {
13241324
ny /= len;
13251325
}
13261326

1327-
int span = (int) PActivity.dist(tx1, ty1, tx2, ty2) * ACCURACY;
1327+
int span = (int) PApplet.dist(tx1, ty1, tx2, ty2) * ACCURACY;
13281328
if (span <= 0) {
13291329
//System.err.println("span is too small");
13301330
// annoying edge case where the gradient isn't legit
@@ -1347,11 +1347,11 @@ public Raster getRaster(int x, int y, int w, int h) {
13471347
int last = (int) (offset[i] * (span-1));
13481348
//System.out.println("last is " + last);
13491349
for (int j = prev; j <= last; j++) {
1350-
float btwn = PActivity.norm(j, prev, last);
1351-
interp[j][0] = (int) PActivity.lerp((c0 >> 16) & 0xff, (c1 >> 16) & 0xff, btwn);
1352-
interp[j][1] = (int) PActivity.lerp((c0 >> 8) & 0xff, (c1 >> 8) & 0xff, btwn);
1353-
interp[j][2] = (int) PActivity.lerp(c0 & 0xff, c1 & 0xff, btwn);
1354-
interp[j][3] = (int) (PActivity.lerp((c0 >> 24) & 0xff, (c1 >> 24) & 0xff, btwn) * opacity);
1350+
float btwn = PApplet.norm(j, prev, last);
1351+
interp[j][0] = (int) PApplet.lerp((c0 >> 16) & 0xff, (c1 >> 16) & 0xff, btwn);
1352+
interp[j][1] = (int) PApplet.lerp((c0 >> 8) & 0xff, (c1 >> 8) & 0xff, btwn);
1353+
interp[j][2] = (int) PApplet.lerp(c0 & 0xff, c1 & 0xff, btwn);
1354+
interp[j][3] = (int) (PApplet.lerp((c0 >> 24) & 0xff, (c1 >> 24) & 0xff, btwn) * opacity);
13551355
//System.out.println(j + " " + interp[j][0] + " " + interp[j][1] + " " + interp[j][2]);
13561356
}
13571357
prev = last;
@@ -1384,10 +1384,8 @@ public Raster getRaster(int x, int y, int w, int h) {
13841384
}
13851385
}
13861386
}
1387-
*/
13881387

13891388

1390-
/*
13911389
class RadialGradientPaint implements Paint {
13921390
float cx, cy, radius;
13931391
float[] offset;
@@ -1436,11 +1434,11 @@ public Raster getRaster(int x, int y, int w, int h) {
14361434
int c1 = color[i];
14371435
int last = (int) (offset[i] * (span - 1));
14381436
for (int j = prev; j <= last; j++) {
1439-
float btwn = PActivity.norm(j, prev, last);
1440-
interp[j][0] = (int) PActivity.lerp((c0 >> 16) & 0xff, (c1 >> 16) & 0xff, btwn);
1441-
interp[j][1] = (int) PActivity.lerp((c0 >> 8) & 0xff, (c1 >> 8) & 0xff, btwn);
1442-
interp[j][2] = (int) PActivity.lerp(c0 & 0xff, c1 & 0xff, btwn);
1443-
interp[j][3] = (int) (PActivity.lerp((c0 >> 24) & 0xff, (c1 >> 24) & 0xff, btwn) * opacity);
1437+
float btwn = PApplet.norm(j, prev, last);
1438+
interp[j][0] = (int) PApplet.lerp((c0 >> 16) & 0xff, (c1 >> 16) & 0xff, btwn);
1439+
interp[j][1] = (int) PApplet.lerp((c0 >> 8) & 0xff, (c1 >> 8) & 0xff, btwn);
1440+
interp[j][2] = (int) PApplet.lerp(c0 & 0xff, c1 & 0xff, btwn);
1441+
interp[j][3] = (int) (PApplet.lerp((c0 >> 24) & 0xff, (c1 >> 24) & 0xff, btwn) * opacity);
14441442
}
14451443
prev = last;
14461444
}
@@ -1449,8 +1447,8 @@ public Raster getRaster(int x, int y, int w, int h) {
14491447
int index = 0;
14501448
for (int j = 0; j < h; j++) {
14511449
for (int i = 0; i < w; i++) {
1452-
float distance = PActivity.dist(cx, cy, x + i, y + j);
1453-
int which = PActivity.min((int) (distance * ACCURACY), interp.length-1);
1450+
float distance = PApplet.dist(cx, cy, x + i, y + j);
1451+
int which = PApplet.min((int) (distance * ACCURACY), interp.length-1);
14541452

14551453
data[index++] = interp[which][0];
14561454
data[index++] = interp[which][1];
@@ -1464,41 +1462,26 @@ public Raster getRaster(int x, int y, int w, int h) {
14641462
}
14651463
}
14661464
}
1467-
*/
14681465

14691466

1470-
protected Shader calcGradientPaint(Gradient gradient) {
1471-
// TODO just do this with the other parsing
1472-
int[] colors = new int[gradient.count];
1473-
int opacityMask = ((int) (opacity * 255)) << 24;
1474-
for (int i = 0; i < gradient.count; i++) {
1475-
colors[i] = opacityMask | (gradient.color[i] & 0xFFFFFF);
1476-
}
1477-
1467+
protected Paint calcGradientPaint(Gradient gradient) {
14781468
if (gradient instanceof LinearGradient) {
14791469
LinearGradient grad = (LinearGradient) gradient;
1480-
// return new LinearGradientPaint(grad.x1, grad.y1, grad.x2, grad.y2,
1481-
// grad.offset, grad.color, grad.count,
1482-
// opacity);
1483-
return new android.graphics.LinearGradient(grad.x1, grad.y1,
1484-
grad.x2, grad.y2,
1485-
colors, grad.offset,
1486-
Shader.TileMode.CLAMP );
1470+
return new LinearGradientPaint(grad.x1, grad.y1, grad.x2, grad.y2,
1471+
grad.offset, grad.color, grad.count,
1472+
opacity);
14871473

14881474
} else if (gradient instanceof RadialGradient) {
14891475
RadialGradient grad = (RadialGradient) gradient;
1490-
// return new RadialGradientPaint(grad.cx, grad.cy, grad.r,
1491-
// grad.offset, grad.color, grad.count,
1492-
// opacity);
1493-
return new android.graphics.RadialGradient(grad.cx, grad.cy, grad.r,
1494-
colors, grad.offset,
1495-
Shader.TileMode.CLAMP);
1476+
return new RadialGradientPaint(grad.cx, grad.cy, grad.r,
1477+
grad.offset, grad.color, grad.count,
1478+
opacity);
14961479
}
14971480
return null;
14981481
}
14991482

15001483

1501-
// protected Shader calcGradientPaint(Gradient gradient,
1484+
// protected Paint calcGradientPaint(Gradient gradient,
15021485
// float x1, float y1, float x2, float y2) {
15031486
// if (gradient instanceof LinearGradient) {
15041487
// LinearGradient grad = (LinearGradient) gradient;
@@ -1510,7 +1493,7 @@ protected Shader calcGradientPaint(Gradient gradient) {
15101493
// }
15111494

15121495

1513-
// protected Shader calcGradientPaint(Gradient gradient,
1496+
// protected Paint calcGradientPaint(Gradient gradient,
15141497
// float cx, float cy, float r) {
15151498
// if (gradient instanceof RadialGradient) {
15161499
// RadialGradient grad = (RadialGradient) gradient;
@@ -1528,18 +1511,22 @@ protected Shader calcGradientPaint(Gradient gradient) {
15281511
protected void styles(PGraphics g) {
15291512
super.styles(g);
15301513

1531-
if (g instanceof PGraphicsAndroid2D) {
1532-
PGraphicsAndroid2D gg = (PGraphicsAndroid2D) g;
1514+
if (g instanceof PGraphicsJava2D) {
1515+
PGraphicsJava2D p2d = (PGraphicsJava2D) g;
15331516

15341517
if (strokeGradient != null) {
1535-
// gg.strokeGradient = true;
1536-
// gg.strokeGradientObject = strokeGradientPaint;
1537-
gg.strokePaint.setShader(strokeGradientPaint);
1518+
p2d.strokeGradient = true;
1519+
p2d.strokeGradientObject = strokeGradientPaint;
1520+
} else {
1521+
// need to shut off, in case parent object has a gradient applied
1522+
//p2d.strokeGradient = false;
15381523
}
15391524
if (fillGradient != null) {
1540-
// p2d.fillGradient = true;
1541-
// p2d.fillGradientObject = fillGradientPaint;
1542-
gg.fillPaint.setShader(fillGradientPaint);
1525+
p2d.fillGradient = true;
1526+
p2d.fillGradientObject = fillGradientPaint;
1527+
} else {
1528+
// need to shut off, in case parent object has a gradient applied
1529+
//p2d.fillGradient = false;
15431530
}
15441531
}
15451532
}

core/src/processing/core/PFont.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -863,13 +863,13 @@ public class Glyph {
863863
public int leftExtent;
864864

865865

866-
protected Glyph() {
866+
public Glyph() {
867867
index = -1;
868868
// used when reading from a stream or for subclasses
869869
}
870870

871871

872-
protected Glyph(DataInputStream is) throws IOException {
872+
public Glyph(DataInputStream is) throws IOException {
873873
index = -1;
874874
readHeader(is);
875875
}

core/src/processing/core/PImage.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2766,7 +2766,7 @@ protected boolean saveTGA(OutputStream output) {
27662766
* To get a list of the supported formats for writing, use: <BR>
27672767
* <TT>println(javax.imageio.ImageIO.getReaderFormatNames())</TT>
27682768
*/
2769-
protected void saveImageIO(String path) throws IOException {
2769+
protected boolean saveImageIO(String path) throws IOException {
27702770
try {
27712771
int outputFormat = (format == ARGB) ?
27722772
BufferedImage.TYPE_INT_ARGB : BufferedImage.TYPE_INT_RGB;
@@ -2785,7 +2785,7 @@ protected void saveImageIO(String path) throws IOException {
27852785
File file = new File(path);
27862786
String extension = path.substring(path.lastIndexOf('.') + 1);
27872787

2788-
ImageIO.write(bimage, extension, file);
2788+
return ImageIO.write(bimage, extension, file);
27892789

27902790
} catch (Exception e) {
27912791
e.printStackTrace();
@@ -2863,7 +2863,9 @@ public void save(String filename) { // ignore
28632863
if (saveImageFormats != null) {
28642864
for (int i = 0; i < saveImageFormats.length; i++) {
28652865
if (filename.endsWith("." + saveImageFormats[i])) {
2866-
saveImageIO(filename);
2866+
if (!saveImageIO(filename)) {
2867+
throw new RuntimeException("Error while saving image.");
2868+
}
28672869
return;
28682870
}
28692871
}

0 commit comments

Comments
 (0)