Question

I am looking some algorithm or some way or even some references which can enable me generate set of rectangles from CGPathRef.

Consider following cases:

Add a rectangle in path

CGMutablePathRef pathref = CGPathCreateMutable();
CGPathAddRect(pathref, NULL, CGRectMake(10, 10, 200, 300));
CGPathCloseSubpath(pathref);

In above case we have only one rectangle stored in path, so when we fill it only one rectangle is draw, thus algorithm returns the 1 rect.

Adding Circle/Ellipse to Path

CGMutablePathRef pathref = CGPathCreateMutable();
CGPathAddEllipseInRect(pathref, NULL, CGRectMake(10, 10, 500, 500));
CGPathCloseSubpath(pathref);

In this case we have circle stored in path, so when we paint it we can imagine lot of retables are filled filled, thus algorithm returns the n rects.

Bit more complex path

CGMutablePathRef pathref = CGPathCreateMutable();
CGPathAddRect(pathref, NULL, CGRectMake(10, 10, 200, 300));
CGPathAddEllipseInRect(pathref, NULL, CGRectMake(10, 10, 500, 500));
CGPathCloseSubpath(pathref);

and so on.

In above question I am primarily interested in filling, may be later I will have work with stroke too. Hopefully similar approach as in filling should work.

Can someone guide me on how this can be achieved?

Was it helpful?

Solution

Take a look at the blog post Wrapping text around a shape with CoreText. The main routine in it is copyRectangularPathsForPath:height:, which takes an (almost) arbitrary path and returns you an array of rectangular paths of a given height that would fill it. The "almost" is because as written, it can't handle paths that would require disjoint multiple rectangles on the same line. This code could be extended to support those kinds of shapes, though.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top