Swift : PDF context and Clip

Seems the graphics context in PDF is “upside down” (0 is at the bottom)

I’ve dealt with this PITA for almost everything.

But setting a proper CLIP rectangle is a real pain.

The Graphics “page” is (792 x 612) [landscape]

The normal coordinates for the desired clip area is another CGRect, but its Y is at the TOP (like normal)

I can’t seem to find the right equation to “flip” the Clip Rect withn the Page Rect

Only the Y value needs to be altered… X is fine

so I have PageRect (0,0,792,612) and ClipRect (size varies)

IIRC the vertical 0 value was set to the bottom in the PDF standard way back when. I know that with DynaPDF you can flip the orientation but I don’t have it in front of me to see what it is. I’d imagine Swift has something similar.

it wasn’t a matter of flipping the PDF context, that I’ve done
it was a matter of flipping the coor of my cliprect to properly match the PDF page

// solution
public func clip(to:CGRect) {
        var r : CGRect = to
        r = CGRect(x:r.minX,y:flipY(r.minY),width:r.width,height: -r.height)
        r.origin.y    -= margin.y