Bloc icon indicating copy to clipboard operation
Bloc copied to clipboard

SVG: parse String once and keep a sequence of commands to draw each time

Open tinchodias opened this issue 1 year ago • 2 comments

Informal test from my Workspace:

"batman"
pathString := 'M305.378,347.01c48.53,0,87.874,22.104,87.874,49.368c0,27.265-39.344,49.367-87.874,49.367
					c-48.531,0-87.874-22.103-87.874-49.367C217.504,369.114,256.847,347.01,305.378,347.01L305.378,347.01z'.
					
pathString := 'M335.381,362.068c0,0,8.182,11.324,4.426,17.192c-2.754,4.305-7.377,5.384-11.266,5.384
					c-11.438,0-11.85-6.506-11.85-6.506l-1.571-19.8l-5.343,8.486h-8.8l-5.343-8.486l-1.571,19.8c0,0-0.413,6.506-11.851,6.506
					c-3.888,0-8.51-1.08-11.265-5.384c-3.756-5.869,4.426-17.192,4.426-17.192c-22.527,6.229-38.03,19.251-38.03,34.31
					c0,11.756,9.451,22.27,24.313,29.281c-1.76-2.483-6.095-9.704-1.222-14.834c5.972-6.285,15.4-2.513,19.486,3.772
					c0,0,0.314-7.229,7.543-6.285c7.229,0.941,16.657,9.429,17.914,19.484c1.257-10.056,10.686-18.543,17.914-19.484
					c7.229-0.944,7.543,6.285,7.543,6.285c4.086-6.285,13.515-10.058,19.487-3.772c4.872,5.13,0.537,12.351-1.223,14.834
					c14.862-7.012,24.313-17.525,24.313-29.281C373.411,381.32,357.907,368.298,335.381,362.068L335.381,362.068z M305.378,351.931
					c43.694,0,79.116,19.9,79.116,44.447s-35.422,44.448-79.116,44.448c-43.695,0-79.118-19.901-79.118-44.448
					S261.683,351.931,305.378,351.931L305.378,351.931z'.

pathString := '
	M335.381,362.068
	c0,0,8.182,11.324,4.426,17.192
	c-2.754,4.305-7.377,5.384-11.266,5.384
	c-11.438,0-11.85-6.506-11.85-6.506
	l-1.571-19.8
	l-5.343,8.486
	h-8.8
	z'.

"tree"
pathString := 'M1000,200 Q950,400 800,500 Q600,600 750,650 T700,850 Q400,1100 600,1100 T500,1400   Q250,1600 450,1600 L850,1600 Q950,1600 950,1700 h95'.

pathString := '
		M100,20
      Q95,40 80,50
      Q60,60 75,65
      h40'.

pathString := '
		M100,20
      L95,40 80,50
      L60,60 75,65
      h40'.


pathString := '
	M 5,0
	Q 20,35 35,40
	'.

handler := BlSvgPathCanvasCommandHandler new.

(BlSvgPathParser
	on: pathString readStream
	handler: handler)
	readUpToEnd.

aSurface := AeCairoImageSurface
	  extent: 500 asPoint
	  format: AeCairoSurfaceFormat rgb24.
aContext := aSurface newContext.

"aContext useFillRuleWinding. "
"Paint background"
aContext
	sourceColor: Color paleGreen;
	paint.

aContext scaleBy: 5 asPoint.

handler commands do: [ :each | each aeApplyOn: aContext ].

aContext
	sourceColor: Color green;
	fillPreserve;
	sourceColor: Color black;
	stroke.

aSurface.

tinchodias avatar Jun 17 '24 22:06 tinchodias

What is this PR?

Ducasse avatar Aug 22 '24 09:08 Ducasse

Should we turn this into an issue?

Ducasse avatar Aug 22 '24 09:08 Ducasse

@Ducasse it's work on a refactory of SVG parser, as it wasn't complete and I had to freeze it, I pushed to a branch but also created this PR to put the code snippet in some place. I could put it on my notes too, but tried this way of doing it.

tinchodias avatar Jan 31 '25 22:01 tinchodias