如何用PDF图元绘制实心和实心圆? [英] How do you draw filled and unfilled circles with PDF primitives?

查看:631
本文介绍了如何用PDF图元绘制实心和实心圆?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在PostScript上绘制圆圈很容易,令我惊讶的是PDF显然没有继承那些相同的基元.有很多商业图书馆都可以做到这一点,但是它不应该比这更简单吗?

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.

我正在作为 PDF-EasyPDF Perl模块的补充,但语言不是我需要帮助的部分.

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

推荐答案

情况总是如此. PDF在图元中没有圆,只有贝塞尔曲线. PDF成像模型是基于PostScript成像模型构建的,该模型本身仅使用arc/arcto图元提供圆,而圆弧/弧图元本身是根据beziers实现的.

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.

奇怪的是,我已经打电话来在正在处理的生成PDF的一些测试代码中执行此确切任务.这是我的操作方式:

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

假定PdfGraphics是发出PDF命令的类,因此g.MoveTo(x, y)在内容流中将变成"x y m".我从Don Lancaster出色的解释中提取了数学和魔术数字(自然而然是PDF格式) .假设将在原点绘制圆形或椭圆形.要将其移动到其他位置,请先执行翻译转换或修改代码以减去所需原点中的add.此代码给出的最坏情况错误大约为1/1250(大约0.08%),平均为1/2500(大约.04%).

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%).

这篇关于如何用PDF图元绘制实心和实心圆?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆