Get position of PShape

I’m loading an SVG using loadShape and I want to get the position of a child PShape so I can draw a bounding box around it. I tried looking through the PShape class and also tried getVertexX (which results in a NPE since there are no vertices in a SVG).
I’m also curious about finding the width and height of a PShape. I have tested this for the loaded SVG (parent shape) and it works well (spits out actual numbers), but when I try and find the values for a child, I get 0.0 for both.

Thanks again!

There are sometimes vertices in an SVG. It depends on the original SVG data and how you create it / load it. Can you provide an example?

Yes,
SVG in question

PShape s;
void setup(){
  size(1000,1000);
  s = loadShape("tex.svg");
  s = s.getChild("eq");
  s.disableStyle();
}

void draw(){
  background(0);
  translate(width/2,height/2);
  noStroke();
  PShape ch = s.getChild(7);
  println(ch.width);
  shape(ch);
}

I am using JLatexMath to generate these SVG’s, if there is a specific attribute that I must add in the creation of the SVG, I believe I can do that! (Not sure if processing supports pulling extraneous attributes from an SVG)

Looking at your SVG markup in a text editor (it is just a text file, after all), it looks like your SVG generator uses a heavily nested style, and you need to go down one level further to get the vertex data you want.

I tried a debugging sketch like this:

/**
 * SVGpositions -- debugging a nested SVG file to get vertex data
 * 2021-09-22 Jeremy Douglass - Processing 3.5.4
 * https://discourse.processing.org/t/get-position-of-pshape/32362/4
 */
void setup(){
  size(1000,1000);
  background(0);
  stroke(255);
  strokeWeight(2);
  noFill();

  PShape tex = loadShape("tex.svg");
  printShape(tex);

  // tex.disableStyle();

  translate(width/5,height/5);
  PShape tex_eq = tex.getChild("eq");
  printShape(tex_eq);

  translate(width/5,height/5);
  PShape tex_eq_7 = tex_eq.getChild(7);
  printShape(tex_eq_7);

  translate(2*width/5,2*height/5);
  PShape tex_eq_7_0 = tex_eq_7.getChild(0);
  printShape(tex_eq_7_0);

  for(int i=0; i<tex_eq_7_0.getVertexCount(); i++) {
    PVector pv = tex_eq_7_0.getVertex(i);
    point(pv.x, pv.y);
  }
  
  saveFrame("SVGpositions.png");

}

void printShape(PShape s) {
  rect(0, 0, s.width, s.height);
  shape(s);
  println("w: ", s.width, "  child#: ", s.getChildCount(), " vertex#: ", s.getVertexCount());
}

The console output is:

getVertexCount() only works with PATH or GEOMETRY shapes
w: 453.0 child#: 2 vertex#: 0
w: 453.0 child#: 13 vertex#: 0
w: 0.0 child#: 1 vertex#: 0
w: 0.0 child#: 0 vertex#: 35

Notice that we get a complaint about trying to get vertices at all on the top level, but there are 35 vertices at the fourth level down.

Notice also that the third level doesn’t have a width – notice that it is rendered at the same size, but in an odd location compared to the origin (white dot).

The fourth level we can see the point data – and when we draw the point data manually, it appears on top of the font character – but the character is no longer scaled according to its parent shapes.

Hope this helps get you on the right track.

2 Likes