tags:

views:

145

answers:

2

Drawing circles on PostScript is easy, and I'm surprised that PDF apparently doesn't carry over those same primitives. There are plenty of commercial libraries that will do it, but shouldn't it be simpler than that?

There are also some tricks using Bézier curves, but you don't get a perfect circle and you have to draw them in connecting segments. I don't need a perfect circle as long as it look close to perfect.

I'm doing this as an addition to the PDF-EasyPDF Perl module, but the language isn't the part I need help with.

+3  A: 

That's always going to be the case. PDF doesn't have circles in the primitives, only beziers. The PDF imaging model was built from the PostScript imaging model, which itself only provides circles using the arc/arcto primitives, which themselves are implemented in terms of beziers.

Oddly enough, I had call to do this exact task in some test code I'm working on that generates PDF's. Here's how I did it:

    private void DrawEllipse(PdfGraphics g, double xrad, double yrad)
    {
        const double magic = 0.551784;
        double xmagic = xrad * magic;
        double ymagic = yrad * magic;
        g.MoveTo(-xrad, 0);
        g.CurveTo(-xrad, ymagic, -xmagic, yrad, 0, yrad);
        g.CurveTo(xmagic, yrad, xrad, ymagic, xrad, 0);
        g.CurveTo(xrad, -ymagic, xmagic, -yrad, 0, -yrad);
        g.CurveTo(-xmagic, -yrad, -xrad, -ymagic, -xrad, 0);
    }

    private void DrawCircle(PdfGraphics g, double radius)
    {
        DrawEllipse(g, radius, radius);
    }

Assume that PdfGraphics is a class that spews out PDF commands, so g.MoveTo(x, y) will turn into "x y m" in the content stream. I took my math and my magic number from Don Lancaster's fabulous explanation (PDF, naturally). This assumes that the circle or ellipse will be drawn at the origin. To move it somewhere else, do a translation transform first or modify the code to subtract add in the desired origin. This code gives a worst case error of roughly 1/1250 (about .08 %) and average of 1/2500 (about .04%).

plinth
+2  A: 

plinth's answer is the same thing I eventually found. There's lots of tricky math that reduces to a magic constant and subdividing the task into four separate Bézier curves. I needed to do this in raw PDF commands, but the process is the same.

  • Move to the start of the first curve. This is the center of the circle minus the radius in whatever direction you like.

  • Figure out the end of the curve (the $x3 and $y3 values in this code). The subscripts come from the Bézier curve control point labels that most people use.

  • Figure out the control points. That's where the $magic value shows up.

  • When you are finished with one segment, do the next. There's nothing particular special about four segments other than it works nicely in cartesian coordinates with just additions and subtractions.

  • If you want a fill, you end with the f to paint the inside of the path you just created.

There's some refactoring that I could do, but as I was working with this is was much easier to see that I was getting the signs right by having separate blocks of code. This is the subroutine I was adding to PDF::EasyPDF:

sub make_magic_circle
    {
    my( $pdf, # PDF::EasyPDF object
        $center,
        $r   # radius
        ) = @_;

    my( $xc, $yc ) = $center->xy;

    my $magic = $r * 0.552;
    my( $x0p, $y0p ) = ( $xc - $r, $yc );
    $pdf->{stream} .= "$x0p $y0p m\n";

    {
    ( $x0p, $y0p ) = ( $xc - $r, $yc );
    my( $x1, $y1 ) = ( $x0p,               $y0p + $magic );
    my( $x2, $y2 ) = ( $x0p + $r - $magic, $y0p + $r     );
    my( $x3, $y3 ) = ( $x0p + $r,          $y0p + $r     );
    $pdf->{stream} .= "$x1 $y1 $x2 $y2 $x3 $y3 c\n";
    }

    {
    ( $x0p, $y0p ) = ( $xc, $yc + $r );
    my( $x1, $y1 ) = ( $x0p + $magic, $y0p               );
    my( $x2, $y2 ) = ( $x0p + $r,     $y0p - $r + $magic );
    my( $x3, $y3 ) = ( $x0p + $r,     $y0p - $r          );
    $pdf->{stream} .= "$x1 $y1 $x2 $y2 $x3 $y3 c\n";
    }

    {
    ( $x0p, $y0p ) = ( $xc + $r, $yc );
    my( $x1, $y1 ) = ( $x0p,               $y0p - $magic );
    my( $x2, $y2 ) = ( $x0p - $r + $magic, $y0p - $r     );
    my( $x3, $y3 ) = ( $x0p - $r,          $y0p - $r     );
    $pdf->{stream} .= "$x1 $y1 $x2 $y2 $x3 $y3 c\n";
    }

    {
    ( $x0p, $y0p ) = ( $xc, $yc - $r );
    my( $x1, $y1 ) = ( $x0p - $magic,               $y0p );
    my( $x2, $y2 ) = ( $x0p - $r, $y0p + $r - $magic    );
    my( $x3, $y3 ) = ( $x0p - $r,          $y0p + $r     );
    $pdf->{stream} .= "$x1 $y1 $x2 $y2 $x3 $y3 c\n";
    }

    $pdf->{stream} .= "f\n";
    }
brian d foy