How did you draw filled and unfilled circles using PDF primitives?

Drawing circles on PostScript is easy, and I am surprised that PDF does not seem to tolerate the same primitives. There are many commercial libraries that will do this, but shouldn't it be easier?

There are also some tricks using Bezier curves, but you don’t get a perfect circle, and you have to draw them in the connecting segments. I do not need a perfect circle if it is close to perfection.

I do this as an addition to the PDF EasyPDF Perl module, but the language is not the part that I need help with.

+5
source share
3 answers

. PDF , . PDF PostScript, arc/arcto, .

, , , , PDF . :

    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);
    }

, PdfGraphics - , PDF, g.MoveTo(x, y) "x y m" . Lancaster (PDF, ). , . , , . 1/1250 ( 0,08%) 1/2500 ( 0,04%).

+5

- , . , . PDF, .

  • . .

  • ( $x3 $y3 ). BΓ©zier, .

  • . , $magic.

  • , . , , .

  • , f, .

- , , , , , . , 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";
    }
+4

, ( , ).

, " " rounded.

, , , , ... .

+1

All Articles