Skip to content

Commit 5e1161c

Browse files
authored
Merge pull request #905 from Junology/rxattrib
2 parents 5f32f67 + 1f1c81a commit 5e1161c

File tree

1 file changed

+95
-8
lines changed

1 file changed

+95
-8
lines changed

core/src/processing/core/PShapeSVG.java

Lines changed: 95 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -440,15 +440,102 @@ protected void parseEllipse(boolean circle) {
440440
}
441441

442442

443+
/**
444+
* Parse <rect> element.
445+
* Syntax defined at https://www.w3.org/TR/SVG11/shapes.html#RectElement
446+
*/
443447
protected void parseRect() {
444-
kind = RECT;
445-
family = PRIMITIVE;
446-
params = new float[] {
447-
getFloatWithUnit(element, "x", svgWidth),
448-
getFloatWithUnit(element, "y", svgHeight),
449-
getFloatWithUnit(element, "width", svgWidth),
450-
getFloatWithUnit(element, "height", svgHeight)
451-
};
448+
// Load rectangle parameters
449+
float x = getFloatWithUnit(element, "x", svgWidth);
450+
float y = getFloatWithUnit(element, "y", svgHeight);
451+
float w = getFloatWithUnit(element, "width", svgWidth);
452+
float h = getFloatWithUnit(element, "height", svgHeight);
453+
454+
// The specification above says zero size should disable rendering.
455+
// The resulting shape is an empty GROUP shape since it is the most light one in drawing.
456+
if (w <= 0f || h <= 0f) {
457+
kind = 0;
458+
family = GROUP;
459+
childCount = 0;
460+
children = null;
461+
vertexCount = 0;
462+
visible = false;
463+
return;
464+
}
465+
466+
// Determine the values of rx and ry from attributes
467+
String rxAttr = element.getString("rx");
468+
String ryAttr = element.getString("ry");
469+
float rx = rxAttr == null ? -1f : parseUnitSize(rxAttr, svgWidth);
470+
float ry = ryAttr == null ? -1f : parseUnitSize(ryAttr, svgHeight);
471+
if (rx < 0f && ry > 0f)
472+
rx = ry;
473+
if (rx > 0f && ry < 0f)
474+
ry = rx;
475+
if (rx > w/2)
476+
rx = w/2;
477+
if (ry > h/2)
478+
ry = h/2;
479+
480+
// Determine the vertices
481+
if (rx <= 0f || ry <= 0f) {
482+
kind = RECT;
483+
family = PRIMITIVE;
484+
params = new float[] {x, y, w, h};
485+
}
486+
else if (rx == ry) {
487+
kind = RECT;
488+
family = PRIMITIVE;
489+
params = new float[] {x, y, w, h, rx};
490+
}
491+
else {
492+
kind = 0;
493+
family = PATH;
494+
close = true;
495+
vertexCount = 16;
496+
vertices = new float[vertexCount][2];
497+
vertexCodes = new int[8];
498+
parsePathCode(VERTEX);
499+
vertices[0][X] = x;
500+
vertices[0][Y] = y + ry;
501+
parsePathCode(BEZIER_VERTEX);
502+
vertices[1][X] = x;
503+
vertices[1][Y] = y + 0.4476f * ry;
504+
vertices[2][X] = x + 0.4476f * rx;
505+
vertices[2][Y] = y;
506+
vertices[3][X] = x + rx;
507+
vertices[3][Y] = y;
508+
parsePathCode(VERTEX);
509+
vertices[4][X] = x + w - rx;
510+
vertices[4][Y] = y;
511+
parsePathCode(BEZIER_VERTEX);
512+
vertices[5][X] = x + w - 0.4476f * rx;
513+
vertices[5][Y] = y;
514+
vertices[6][X] = x + w;
515+
vertices[6][Y] = y + 0.4476f * ry;
516+
vertices[7][X] = x + w;
517+
vertices[7][Y] = y + ry;
518+
parsePathCode(VERTEX);
519+
vertices[8][X] = x + w;
520+
vertices[8][Y] = y + h - ry;
521+
parsePathCode(BEZIER_VERTEX);
522+
vertices[9][X] = x + w;
523+
vertices[9][Y] = y + h - 0.4476f * ry;
524+
vertices[10][X] = x + w - 0.4476f * rx;
525+
vertices[10][Y] = y + h;
526+
vertices[11][X] = x + w - rx;
527+
vertices[11][Y] = y + h;
528+
parsePathCode(VERTEX);
529+
vertices[12][X] = x + rx;
530+
vertices[12][Y] = y + h;
531+
parsePathCode(BEZIER_VERTEX);
532+
vertices[13][X] = x + 0.4476f * rx;
533+
vertices[13][Y] = y + h;
534+
vertices[14][X] = x;
535+
vertices[14][Y] = y + h - 0.4476f * ry;
536+
vertices[15][X] = x;
537+
vertices[15][Y] = y + h- ry;
538+
}
452539
}
453540

454541

0 commit comments

Comments
 (0)