can I separate SVG rendering stroke width from shape size? (line fonts for plotting)

Hi,

Rendering an SVG. Have disabled the SVG’s style. My problem is that even though I’m setting the stroke weight through processing, it still scales that stroke when I render the SVG at different sizes.

What I want is the shape its self to scale but the stroke to remain constant. This is ultimately going to be plotted with a real pen so I want to visualize that.

As an aside I’m planning to create an entire ‘line font’ as SVG’s and an accompanying processing lib for this purpose (plotting normal fonts sucks), which I’ll be happy to share if I can get it to work.

PShape h;

void setup() {
  size(640, 360);
  h = loadShape("h.svg");
  h.disableStyle();
  noFill();
  strokeWeight(0.02);
  noLoop();
} 

void draw(){
  background(255);
  shape(h, 110, 90, 100, 100); 
  shape(h, 280, 90,400,400);    
}

Hello,
I can but guess without seeing the svg, but if it was converted from a ttf it doesn’t matter what you do with strokeWidth because those glyphs are built assuming noStroke but fill()

Thanks for replying. It’s a single un-closed spline that I traced manually - that’s the whole point because when I plot this with a pen I don’t want it to outline the characters as it does with regular fonts.

Well then I suggest you could parse the svg as text strings and grab your line points coordinates and draw them “manually” with bezier()

roger that I’ll have a go

1 Like

Can you share h.svg?

My first guess (untested) based on what you are describing is something like this:

void shapeStroke(PShape s, float x, float y, float w, float h, float basewidth, float baseweight){
  strokeWeight(baseweight * w/basewidth);
  shape(s, x, y, w, h);
}

So that you can call your shape with different dimensions and it will counterbalance the strokeWeight by the same ratio, given an assumed base size and base weight. The idea would be to scale the shape but keep line thicknesses the same.

…although I’m surprised at what you are describing. I wonder if you have a parent and child shape, so disableStyles isn’t actually doing anything…?

1 Like

Hello!
Thanks for the reply, I didn’t notice it until now!

Your function worked, assuming you know the width of PShape s. Is there a way to calculate that?

I’ve attached the svg - I went in with a text editor and removed everything except the lines for the actual path but with the same result. Also attached the original for completeness.

That depends on how you created the PShape and what access you have to its contents.

  • you can check if the PShape.width / .height was loaded as metadata when you loaded the PShape. PShape::width / Reference / Processing.org
  • If it contains a vertex list then you can iterate over all vertices and get minx, miny, maxx, maxy to form a bounding box – and maxx-minx is the width.
  • If those fields don’t have (useful) values and you can’t get the vertices, then you just need to specify it manually.

PShape is a really complex class, and svg is a complex spec that has multiple ways of specifying dimensions, viewports, and the coordinates of their contents – these don’t always map cleanly to the width/height that you may want, so it tends to depend a lot on your data.

That said, I’m not actually sure what the reliable way is of generating svg files that correctly populate PShape.width / .height – I’ve never tried. If you discover that you are able to do that reliably, could you post about it here?

Edit

For your h.svg, which is a simple svg, the PShape.width approach from the reference works fine:

// https://processing.org/reference/PShape_width.html
PShape s;
void setup() {
  s = loadShape("h.svg");
  println(s.width);  // Prints "6.0", the width of the shape
  println(s.height);  // Prints "9.0", the width of the shape
}
1 Like